fix(relinker): validate the PT_TLS layout when reading program headers #7098

Merged
jotitadev merged 2 commits from upstream-pr/7098 into main 2026-10-18 18:24:33 +00:00
jotitadev commented 2026-10-17 11:20:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T11:20:48Z.


What

Linux conversion accepts a PT_TLS that the Windows and macOS targets refuse, and writes it unchanged into the output program header table. Four layouts, each on a minimal executable (the ConversionTests fixture plus the PT_TLS headers in the table), --skip-sce-module:

PT_TLS Linux --windows / --macos
p_vaddr 0x900000, p_filesz 8, outside every PT_LOAD exit 0 ELF address is not contained in PT_LOAD memory
p_filesz 16 > p_memsz 8 exit 0 Invalid or unsupported ELF TLS layout
p_align 3 exit 0 Invalid or unsupported ELF TLS layout
two PT_TLS headers exit 0 Multiple ELF TLS segments

The dynamic loader takes the TLS initialization image from p_vaddr (relative to the load base) and copies p_filesz bytes of it, then clears p_memsz - p_filesz, so with the first layout it reads memory the image does not map and with the second it clears a wrapped length. Its TLS offset rounding assumes a power-of-two alignment, and with two headers it keeps only one of them. Guest modules (GuestImageReader) already refuse the size, alignment and duplicate cases but not the address one.

ElfReader::ReadProgramHeaders now rejects a second PT_TLS, a PT_TLS whose file size exceeds its memory size or whose alignment is not 0 or a power of two (same messages as the Windows and macOS writers), and a non-empty initialization image that is not inside the memory of one PT_LOAD (after the uncovered PT_SCE_RELRO promotion). An empty PT_TLS placeholder (p_filesz 0) is still accepted wherever it points, as test_empty_tls expects.

elf_reader_bounds has six new cases: images inside load memory plus empty placeholders (accepted), a non-empty image inside a PT_SCE_RELRO that no PT_LOAD covers (accepted, which pins the TLS check after the RELRO promotion), images below, across the end of, and past the load (rejected), file size over memory size, alignment 3, and a second PT_TLS. The four rejection cases fail without the ElfReader change.

Found by fuzzing the input parsing (Relinker::Conversion under libFuzzer with ASan and UBSan, executables and guest modules, Linux/Windows/macOS targets). No sanitizer reports came out of that run; this was the clearest accepted-invalid layout.

Tested

macOS 27.2 on an Apple M2 Max, Xcode 27, relinker-only Release build with BUILD_TESTING=ON, arm64, on main 5d9dcc42d:

  • 173/173 pass, elf_reader_bounds included (173/173 on main too).
  • The four layouts above now fail with exit 2 on all three targets (checked before rebasing, on main ce991f1ec).
  • tools/check_conventions.py --base origin/main passes.

Linux x86_64 (Ubuntu 26.04, GCC 15.2, CMake 4.2.3), relinker-only Release build with BUILD_TESTING=ON, on main 5d9dcc42d:

  • Test change alone (without the ElfReader change): elf_reader_bounds fails on exactly the four rejection cases (TLS image outside load memory, TLS file size exceeds memory size, TLS alignment is not a power of two, second TLS segment: "Invalid program header was accepted"); the accepted-layouts case passes.
  • Uncovered PT_SCE_RELRO case (added after review): passes on this branch, and fails with PT_TLS initialization image is outside PT_LOAD memory when the TLS loop is moved before the RELRO promotion.
  • This branch: elf_reader_bounds and elf_offsets pass; the full suite is 180/182 with and without this change (the same two file_writer_binary/file_writer_text failures, which come from the host's coreutils and also fail on main). No new compiler warnings.

Not tested on a Windows host, and the loader behaviour above comes from the glibc sources, not from running the old output.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899625865 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7098). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-17T11:20:48Z. --- ### What Linux conversion accepts a `PT_TLS` that the Windows and macOS targets refuse, and writes it unchanged into the output program header table. Four layouts, each on a minimal executable (the `ConversionTests` fixture plus the `PT_TLS` headers in the table), `--skip-sce-module`: | `PT_TLS` | Linux | `--windows` / `--macos` | |---|---|---| | `p_vaddr` 0x900000, `p_filesz` 8, outside every `PT_LOAD` | exit 0 | `ELF address is not contained in PT_LOAD memory` | | `p_filesz` 16 > `p_memsz` 8 | exit 0 | `Invalid or unsupported ELF TLS layout` | | `p_align` 3 | exit 0 | `Invalid or unsupported ELF TLS layout` | | two `PT_TLS` headers | exit 0 | `Multiple ELF TLS segments` | The dynamic loader takes the TLS initialization image from `p_vaddr` (relative to the load base) and copies `p_filesz` bytes of it, then clears `p_memsz - p_filesz`, so with the first layout it reads memory the image does not map and with the second it clears a wrapped length. Its TLS offset rounding assumes a power-of-two alignment, and with two headers it keeps only one of them. Guest modules (`GuestImageReader`) already refuse the size, alignment and duplicate cases but not the address one. `ElfReader::ReadProgramHeaders` now rejects a second `PT_TLS`, a `PT_TLS` whose file size exceeds its memory size or whose alignment is not 0 or a power of two (same messages as the Windows and macOS writers), and a non-empty initialization image that is not inside the memory of one `PT_LOAD` (after the uncovered `PT_SCE_RELRO` promotion). An empty `PT_TLS` placeholder (`p_filesz` 0) is still accepted wherever it points, as `test_empty_tls` expects. `elf_reader_bounds` has six new cases: images inside load memory plus empty placeholders (accepted), a non-empty image inside a `PT_SCE_RELRO` that no `PT_LOAD` covers (accepted, which pins the TLS check after the RELRO promotion), images below, across the end of, and past the load (rejected), file size over memory size, alignment 3, and a second `PT_TLS`. The four rejection cases fail without the `ElfReader` change. Found by fuzzing the input parsing (`Relinker::Conversion` under libFuzzer with ASan and UBSan, executables and guest modules, Linux/Windows/macOS targets). No sanitizer reports came out of that run; this was the clearest accepted-invalid layout. ### Tested macOS 27.2 on an Apple M2 Max, Xcode 27, relinker-only Release build with `BUILD_TESTING=ON`, arm64, on `main` 5d9dcc42d: - 173/173 pass, `elf_reader_bounds` included (173/173 on `main` too). - The four layouts above now fail with exit 2 on all three targets (checked before rebasing, on `main` ce991f1ec). - `tools/check_conventions.py --base origin/main` passes. Linux x86_64 (Ubuntu 26.04, GCC 15.2, CMake 4.2.3), relinker-only Release build with `BUILD_TESTING=ON`, on `main` 5d9dcc42d: - Test change alone (without the `ElfReader` change): `elf_reader_bounds` fails on exactly the four rejection cases (`TLS image outside load memory`, `TLS file size exceeds memory size`, `TLS alignment is not a power of two`, `second TLS segment`: "Invalid program header was accepted"); the accepted-layouts case passes. - Uncovered `PT_SCE_RELRO` case (added after review): passes on this branch, and fails with `PT_TLS initialization image is outside PT_LOAD memory` when the TLS loop is moved before the RELRO promotion. - This branch: `elf_reader_bounds` and `elf_offsets` pass; the full suite is 180/182 with and without this change (the same two `file_writer_binary`/`file_writer_text` failures, which come from the host's coreutils and also fail on `main`). No new compiler warnings. Not tested on a Windows host, and the loader behaviour above comes from the glibc sources, not from running the old output. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
OskarLGS commented 2026-10-17 21:20:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by OskarLGS on 2026-10-17T21:20:33Z.

Review: COMMENTED


Reviewed on Linux x86-64 (GCC 13, relinker-only build), merged into current main: no conflicts, all 187 ctest tests pass and check_conventions.py passes. With the ElfReader.cpp change reverted, the four new rejection cases in elf_reader_bounds fail, so the tests cover the fix.

Checked:

  • An uncovered PT_SCE_RELRO is turned into a PT_LOAD before the new loop runs, so a PS4-layout PT_TLS that lies in it is still accepted.
  • Guest modules reach this check: GuestImageReader reads its headers through ElfReader::ReadProgramHeaders.
  • The Windows and macOS writers already enforce the same size and alignment limits, and both copy the TLS image from the image's virtual addresses (WindowsTlsTemplateBuilder through image.GetRva), so requiring the image to lie inside PT_LOAD memory matches what they read.
  • The bounds arithmetic does not overflow for the edge addresses the test uses.

Suggestion: no test covers the PS4 case above, which relies on the RELRO conversion loop running first. A fixture with a non-empty PT_TLS inside a PT_SCE_RELRO that no PT_LOAD covers, expected to be accepted, would keep that ordering from regressing.

Review done with AI assistance (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5560147362 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7098#pullrequestreview-5560147362). Originally posted by [OskarLGS](https://github.com/OskarLGS) on 2026-10-17T21:20:33Z. **Review:** COMMENTED --- Reviewed on Linux x86-64 (GCC 13, relinker-only build), merged into current main: no conflicts, all 187 ctest tests pass and check_conventions.py passes. With the ElfReader.cpp change reverted, the four new rejection cases in elf_reader_bounds fail, so the tests cover the fix. Checked: - An uncovered PT_SCE_RELRO is turned into a PT_LOAD before the new loop runs, so a PS4-layout PT_TLS that lies in it is still accepted. - Guest modules reach this check: GuestImageReader reads its headers through ElfReader::ReadProgramHeaders. - The Windows and macOS writers already enforce the same size and alignment limits, and both copy the TLS image from the image's virtual addresses (WindowsTlsTemplateBuilder through image.GetRva), so requiring the image to lie inside PT_LOAD memory matches what they read. - The bounds arithmetic does not overflow for the edge addresses the test uses. Suggestion: no test covers the PS4 case above, which relies on the RELRO conversion loop running first. A fixture with a non-empty PT_TLS inside a PT_SCE_RELRO that no PT_LOAD covers, expected to be accepted, would keep that ordering from regressing. Review done with AI assistance (Claude Code).
Baconzido commented 2026-10-17 21:41:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Baconzido on 2026-10-17T21:41:42Z.


macOS result for the relinker-only build, since the description lists Windows as untested.

Apple M5 Pro (arm64), macOS 27.0.1, AppleClang 21.0.0, CMake 4.4.4, Python 3.14.7, Release relinker-only, BUILD_TESTING=ON. Head ce316bfce merged onto main 922dc8999 (clean merge).

  • With the PR: full ctest 178/178; elf_reader_bounds and elf_offsets pass.
  • Negative control: ElfReader.cpp reverted to main, test file kept: elf_reader_bounds fails on exactly the four rejection cases (TLS image outside load memory, TLS file size exceeds memory size, TLS alignment is not a power of two, second TLS segment: "Invalid program header was accepted"). Restoring the change passes again.
  • main alone: 178/178.

Not checked: Windows and Linux; I did not run the four conversion layouts through the CLI on the three targets, only the unit cases.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6241606241 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7098#issuecomment-6241606241). Originally posted by [Baconzido](https://github.com/Baconzido) on 2026-10-17T21:41:42Z. --- macOS result for the relinker-only build, since the description lists Windows as untested. Apple M5 Pro (arm64), macOS 27.0.1, AppleClang 21.0.0, CMake 4.4.4, Python 3.14.7, Release relinker-only, `BUILD_TESTING=ON`. Head `ce316bfce` merged onto `main` `922dc8999` (clean merge). - With the PR: full ctest 178/178; `elf_reader_bounds` and `elf_offsets` pass. - Negative control: `ElfReader.cpp` reverted to `main`, test file kept: `elf_reader_bounds` fails on exactly the four rejection cases (`TLS image outside load memory`, `TLS file size exceeds memory size`, `TLS alignment is not a power of two`, `second TLS segment`: "Invalid program header was accepted"). Restoring the change passes again. - `main` alone: 178/178. Not checked: Windows and Linux; I did not run the four conversion layouts through the CLI on the three targets, only the unit cases. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
jotitadev commented 2026-10-17 23:07:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T23:07:46Z.


@OskarLGS thanks, added in 9f000e861: TLS image inside uncovered SCE RELRO in elf_reader_bounds. It turns the fixture's only PT_LOAD into a PT_SCE_RELRO and puts a non-empty PT_TLS (p_vaddr 0x1008, p_filesz 8, p_memsz 16) inside it, then expects the headers to be accepted with the RELRO promoted to PT_LOAD.

Linux x86_64 (Ubuntu 26.04, GCC 15.2), relinker-only Release, BUILD_TESTING=ON, on main 5d9dcc42d:

  • The new case passes on this branch; full suite 180/182, same as main (the two file_writer_binary/file_writer_text host coreutils failures).
  • With the TLS loop moved before the RELRO promotion loop, only the new case fails: PT_TLS initialization image is outside PT_LOAD memory. Restoring the order passes again.
  • tools/check_conventions.py passes; the branch still merges cleanly into current main.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6242847350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7098#issuecomment-6242847350). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-17T23:07:46Z. --- @OskarLGS thanks, added in 9f000e861: `TLS image inside uncovered SCE RELRO` in `elf_reader_bounds`. It turns the fixture's only `PT_LOAD` into a `PT_SCE_RELRO` and puts a non-empty `PT_TLS` (`p_vaddr` 0x1008, `p_filesz` 8, `p_memsz` 16) inside it, then expects the headers to be accepted with the RELRO promoted to `PT_LOAD`. Linux x86_64 (Ubuntu 26.04, GCC 15.2), relinker-only Release, `BUILD_TESTING=ON`, on `main` 5d9dcc42d: - The new case passes on this branch; full suite 180/182, same as `main` (the two `file_writer_binary`/`file_writer_text` host coreutils failures). - With the TLS loop moved before the RELRO promotion loop, only the new case fails: `PT_TLS initialization image is outside PT_LOAD memory`. Restoring the order passes again. - `tools/check_conventions.py` passes; the branch still merges cleanly into current `main`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
screamm commented 2026-10-18 15:04:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T15:04:13Z.


Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head 9f000e861 scratch-merged onto main a62c724c3 (clean; #7037 merges on top of it cleanly too). Relinker, its tests and libs built; no full-tree build, no GPU involved.

  • elf_reader_bounds, elf_offsets, empty_tls, entry_point_bounds, init_array_roots, executable_segments, tls_diagnostics, guest_tls, relinker_conversion: 3 of 3 with --repeat until-fail:3.
  • Negative control: ElfReader.cpp from main, test file kept: elf_reader_bounds fails on exactly TLS image outside load memory, TLS file size exceeds memory size, TLS alignment is not a power of two and second TLS segment ("Invalid program header was accepted"); the accepted-layout and uncovered-RELRO cases pass. With the change all pass.
  • All 103 registered relinker tests, 3 repeats: 98 pass; 4 Python tests hit their 20-30 s limits while other builds were running on the host and pass when run directly (3 of 3); windows_import_modules needs the helper DLLs, which my targeted build lacked at first, and passes once built.
  • Relinking every local title (51 with an eboot.elf) with main's relinker and with this PR: --windows gives identical SHA-256 for all 312 output files (50 rc 0, PPSA99995 rc 2 in both with the same Unsupported Windows guest TLS instruction failure), and the Linux target gives identical SHA-256 for all 122 files, all rc 0. No local title has a PT_TLS the new checks refuse.
  • CLI check on hand-made ELFs (one PT_LOAD covering 0..0x1000, --skip-sce-module): main accepts outside-load, filesz > memsz, align 3 and two-PT_TLS on Linux and refuses them on Windows/macOS; with the PR all four are refused (rc 2) on the Linux, Windows and macOS targets. A PT_TLS inside the load and an empty placeholder at 0x900000 give the same output as main.
  • tools/check_conventions.py --base origin/main and git diff --check: pass.

One small thing in the diff: PT_TLS initialization image is outside PT_LOAD memory prints tls.Offset (0x800 for my case), while the Windows/macOS message it replaces printed the address (ELF address is not contained in PT_LOAD memory (offset 0x900000)). tls.MappedAddress would be the more useful value there; not blocking.

Not checked: macOS host, running a relinked executable, full-tree build, the non-GPU suite.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251437644 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7098#issuecomment-6251437644). Originally posted by [screamm](https://github.com/screamm) on 2026-10-18T15:04:13Z. --- Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head `9f000e861` scratch-merged onto main `a62c724c3` (clean; #7037 merges on top of it cleanly too). Relinker, its tests and `libs` built; no full-tree build, no GPU involved. - `elf_reader_bounds`, `elf_offsets`, `empty_tls`, `entry_point_bounds`, `init_array_roots`, `executable_segments`, `tls_diagnostics`, `guest_tls`, `relinker_conversion`: 3 of 3 with `--repeat until-fail:3`. - Negative control: `ElfReader.cpp` from main, test file kept: `elf_reader_bounds` fails on exactly `TLS image outside load memory`, `TLS file size exceeds memory size`, `TLS alignment is not a power of two` and `second TLS segment` ("Invalid program header was accepted"); the accepted-layout and uncovered-RELRO cases pass. With the change all pass. - All 103 registered relinker tests, 3 repeats: 98 pass; 4 Python tests hit their 20-30 s limits while other builds were running on the host and pass when run directly (3 of 3); `windows_import_modules` needs the helper DLLs, which my targeted build lacked at first, and passes once built. - Relinking every local title (51 with an `eboot.elf`) with main's relinker and with this PR: `--windows` gives identical SHA-256 for all 312 output files (50 rc 0, PPSA99995 rc 2 in both with the same `Unsupported Windows guest TLS instruction` failure), and the Linux target gives identical SHA-256 for all 122 files, all rc 0. No local title has a `PT_TLS` the new checks refuse. - CLI check on hand-made ELFs (one `PT_LOAD` covering 0..0x1000, `--skip-sce-module`): main accepts outside-load, filesz > memsz, align 3 and two-`PT_TLS` on Linux and refuses them on Windows/macOS; with the PR all four are refused (rc 2) on the Linux, Windows and macOS targets. A `PT_TLS` inside the load and an empty placeholder at 0x900000 give the same output as main. - `tools/check_conventions.py --base origin/main` and `git diff --check`: pass. One small thing in the diff: `PT_TLS initialization image is outside PT_LOAD memory` prints `tls.Offset` (0x800 for my case), while the Windows/macOS message it replaces printed the address (`ELF address is not contained in PT_LOAD memory (offset 0x900000)`). `tls.MappedAddress` would be the more useful value there; not blocking. Not checked: macOS host, running a relinked executable, full-tree build, the non-GPU suite. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7098
No description provided.