fix(relinker): reject executable exports outside PT_LOAD and PT_TLS memory #10646

Merged
roboterspinne merged 1 commit from upstream-pr/10646 into main 2026-10-25 11:03:56 +00:00
roboterspinne commented 2026-10-24 14:34:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T14:34:56Z.


What

RelinkerPipeline::Relink copies every export of the input executable (a defined symbol that is not local, hidden or internal, with DT_SCE_SYMTABSZ) into the output .dynsym as it is, without looking at st_value, st_size, st_shndx or the type. GuestImageReader.cpp has required of every symbol of every shipped .prx that [st_value, st_value + max(st_size, 1)) lies inside a PT_LOAD segment, executable for STT_FUNC, that a non-TLS symbol's section index is below SHN_LORESERVE unless it is SHN_ABS, and that an STT_TLS symbol lies inside PT_TLS; MacOsMachOPatcher.cpp applied the bounds and the section rule to the executable's exports on macOS. Of the thirteen rejected shapes in the new test, main accepts twelve on the default Linux output (it rejects a section index of 0xff01 at the fixed base), all thirteen with --lazy-binding, ten on Windows (its code analysis rejects an STT_FUNC export with a nonzero size outside the code) and one on macOS (a size-0 STT_FUNC export in the writable segment). In the Linux output such an export is a dlsym or GOT target outside the image.

  • Rule. The export loop applies the guest-module rule before it keeps the symbol: an STT_TLS export needs a regular section index, a PT_TLS header and st_value + st_size <= p_memsz; a non-TLS export in SHN_ABS is kept without a range check, as the Linux patcher leaves it unrebased and macOS exports it as absolute; every other export needs a section index below SHN_LORESERVE and [st_value, st_value + max(st_size, 1)) inside one PT_LOAD, with PF_X for STT_FUNC. The check runs in the pipeline, so an export it rejects fails with the same line on all four outputs, for example Executable export Foo at 0x9000 (6 bytes) does not fit inside an executable PT_LOAD segment or Executable TLS export exceeds PT_TLS: Foo, with the file offset of the symbol entry. Local, hidden, internal and undefined symbols are not exports and are untouched, and so is an input without DT_SCE_SYMTABSZ, which exports nothing through this loop.
  • Known effects. A size-0 export exactly at the end of a PT_LOAD that no other PT_LOAD continues is rejected, as it is for guest modules and on macOS; #4985, which would have accepted such symbols, was closed because no title is known to reach it. A size-0 STT_FUNC export in a non-executable segment is rejected on every output; every output accepted it before. A size-0 STT_TLS export exactly at the end of PT_TLS passes, as for guest modules, and the macOS writer still rejects it later, as before. A PT_SCE_RELRO range counts as loaded, because ElfReader turns an uncovered PT_SCE_RELRO into a PT_LOAD. The guest-module rule has applied to every symbol of every bundled module since 1379ec001b; the one title it caught exported SHN_ABS symbols (PPSA12544, #698), which stay exempt here.
  • Tests. New test_executable_export_bounds.py: twelve accepted shapes (a function in text and at the last text byte, an object inside and one filling its segment to the end, a size-0 object inside, a weak object, an SHN_ABS object, a TLS export inside PT_TLS and one filling it, and a hidden, an internal and a local symbol outside the image) and thirteen rejected ones (outside every load, past or across a segment end, size-0 at a segment end, functions in the writable segment with and without a size, a protected symbol outside, a 0xff01 section, TLS beyond, without or in a special section of PT_TLS), each relinked for the default and --lazy-binding Linux, Windows and macOS outputs; a rejected case asserts the identical FAIL line, offset included, on all four. test_macos_executable_exports.py expects the pipeline's line for its unmapped, tls-missing and reserved cases, which the macOS checks no longer reach. Those checks (MacOsMachOPatcher.cpp lines 89 to 96) and the fixed-base section check in LinuxElfPatcher.cpp are now unreachable for executable exports; they stay for a follow-up PR, because #9408 edits the lines right after the macOS ones.
  • Overlap. pr_overlap.py cannot compare this branch with #5021, #7173 and #9408 (core/relinker/CMakeLists.txt) or #8076 (RelinkerPipeline.cpp), because those do not merge with main, and reports no other shared lines. Checked by hand: their CMakeLists.txt hunks start at lines 52 to 390, 505 and 542, this PR adds two lines after 567; #8076 changes RelinkerPipeline.cpp from line 374 (unwind ranges), not the export loop. #10402 adds the same containment predicate and wording to LinuxElfPatcher.cpp for e_entry, relocation offsets and DT_PLTGOT; the two share no lines.

Tested

Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew, relinked outputs not run.

  • ctest --test-dir build-relinker -j 6: 242 tests, 240 passed, long_paths skipped; select_tests fails on this machine on main as well (FileExistsError in tools/tests/test_select_tests.py, a Windows symlink problem unrelated to this change).
  • executable_export_bounds against a main build (6cd36e6517) with this test file: all 48 accepted runs pass; of the 52 rejected runs, 36 exit 0 with an output file and 16 exit 2 with a target-specific line (Linux: the 0xff01 section; Windows: the three nonzero-size STT_FUNC exports, in code analysis; macOS: all but the size-0 function in data). With this change all 100 runs behave as asserted, in 2.7 s.
  • macos_executable_exports fails against this change with its old expectations (not contained in PT_LOAD is no longer printed) and passes with the three updated ones.
  • python tools/check_conventions.py --base upstream/main with this title and description passed.
  • Not tested on Linux or macOS; the changed code is host-independent and CI runs both tests on all three hosts. On the Windows CI runner, executable_tls_relocations with about 170 relinker runs takes 0.5 s (#10607's job), so the 100 runs here stay far below 30 s.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4996280278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10646). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T14:34:56Z. --- ### What `RelinkerPipeline::Relink` copies every export of the input executable (a defined symbol that is not local, hidden or internal, with `DT_SCE_SYMTABSZ`) into the output `.dynsym` as it is, without looking at `st_value`, `st_size`, `st_shndx` or the type. `GuestImageReader.cpp` has required of every symbol of every shipped `.prx` that `[st_value, st_value + max(st_size, 1))` lies inside a `PT_LOAD` segment, executable for `STT_FUNC`, that a non-TLS symbol's section index is below `SHN_LORESERVE` unless it is `SHN_ABS`, and that an `STT_TLS` symbol lies inside `PT_TLS`; `MacOsMachOPatcher.cpp` applied the bounds and the section rule to the executable's exports on macOS. Of the thirteen rejected shapes in the new test, `main` accepts twelve on the default Linux output (it rejects a section index of 0xff01 at the fixed base), all thirteen with `--lazy-binding`, ten on Windows (its code analysis rejects an `STT_FUNC` export with a nonzero size outside the code) and one on macOS (a size-0 `STT_FUNC` export in the writable segment). In the Linux output such an export is a `dlsym` or GOT target outside the image. - **Rule.** The export loop applies the guest-module rule before it keeps the symbol: an `STT_TLS` export needs a regular section index, a `PT_TLS` header and `st_value + st_size <= p_memsz`; a non-TLS export in `SHN_ABS` is kept without a range check, as the Linux patcher leaves it unrebased and macOS exports it as absolute; every other export needs a section index below `SHN_LORESERVE` and `[st_value, st_value + max(st_size, 1))` inside one `PT_LOAD`, with `PF_X` for `STT_FUNC`. The check runs in the pipeline, so an export it rejects fails with the same line on all four outputs, for example `Executable export Foo at 0x9000 (6 bytes) does not fit inside an executable PT_LOAD segment` or `Executable TLS export exceeds PT_TLS: Foo`, with the file offset of the symbol entry. Local, hidden, internal and undefined symbols are not exports and are untouched, and so is an input without `DT_SCE_SYMTABSZ`, which exports nothing through this loop. - **Known effects.** A size-0 export exactly at the end of a `PT_LOAD` that no other `PT_LOAD` continues is rejected, as it is for guest modules and on macOS; #4985, which would have accepted such symbols, was closed because no title is known to reach it. A size-0 `STT_FUNC` export in a non-executable segment is rejected on every output; every output accepted it before. A size-0 `STT_TLS` export exactly at the end of `PT_TLS` passes, as for guest modules, and the macOS writer still rejects it later, as before. A `PT_SCE_RELRO` range counts as loaded, because `ElfReader` turns an uncovered `PT_SCE_RELRO` into a `PT_LOAD`. The guest-module rule has applied to every symbol of every bundled module since 1379ec001b; the one title it caught exported `SHN_ABS` symbols (PPSA12544, #698), which stay exempt here. - **Tests.** New `test_executable_export_bounds.py`: twelve accepted shapes (a function in text and at the last text byte, an object inside and one filling its segment to the end, a size-0 object inside, a weak object, an `SHN_ABS` object, a TLS export inside `PT_TLS` and one filling it, and a hidden, an internal and a local symbol outside the image) and thirteen rejected ones (outside every load, past or across a segment end, size-0 at a segment end, functions in the writable segment with and without a size, a protected symbol outside, a 0xff01 section, TLS beyond, without or in a special section of `PT_TLS`), each relinked for the default and `--lazy-binding` Linux, Windows and macOS outputs; a rejected case asserts the identical `FAIL` line, offset included, on all four. `test_macos_executable_exports.py` expects the pipeline's line for its `unmapped`, `tls-missing` and `reserved` cases, which the macOS checks no longer reach. Those checks (`MacOsMachOPatcher.cpp` lines 89 to 96) and the fixed-base section check in `LinuxElfPatcher.cpp` are now unreachable for executable exports; they stay for a follow-up PR, because #9408 edits the lines right after the macOS ones. - **Overlap.** `pr_overlap.py` cannot compare this branch with #5021, #7173 and #9408 (`core/relinker/CMakeLists.txt`) or #8076 (`RelinkerPipeline.cpp`), because those do not merge with `main`, and reports no other shared lines. Checked by hand: their `CMakeLists.txt` hunks start at lines 52 to 390, 505 and 542, this PR adds two lines after 567; #8076 changes `RelinkerPipeline.cpp` from line 374 (unwind ranges), not the export loop. #10402 adds the same containment predicate and wording to `LinuxElfPatcher.cpp` for `e_entry`, relocation offsets and `DT_PLTGOT`; the two share no lines. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew, relinked outputs not run. - `ctest --test-dir build-relinker -j 6`: 242 tests, 240 passed, `long_paths` skipped; `select_tests` fails on this machine on `main` as well (`FileExistsError` in `tools/tests/test_select_tests.py`, a Windows symlink problem unrelated to this change). - `executable_export_bounds` against a `main` build (6cd36e6517) with this test file: all 48 accepted runs pass; of the 52 rejected runs, 36 exit 0 with an output file and 16 exit 2 with a target-specific line (Linux: the 0xff01 section; Windows: the three nonzero-size `STT_FUNC` exports, in code analysis; macOS: all but the size-0 function in data). With this change all 100 runs behave as asserted, in 2.7 s. - `macos_executable_exports` fails against this change with its old expectations (`not contained in PT_LOAD` is no longer printed) and passes with the three updated ones. - `python tools/check_conventions.py --base upstream/main` with this title and description passed. - Not tested on Linux or macOS; the changed code is host-independent and CI runs both tests on all three hosts. On the Windows CI runner, `executable_tls_relocations` with about 170 relinker runs takes 0.5 s (#10607's job), so the 100 runs here stay far below 30 s. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-10-24 14:45:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:45:08Z.

Review: APPROVED


Reviewed exact head f1a7f913c726b782dcdaf11fb496fd3915b80eb3. The pipeline now applies the guest-module export bounds at the common export-copy point, preserving the SHN_ABS exemption and validating TLS exports against PT_TLS; the fixture covers accepted and rejected symbol shapes across Linux default/lazy binding, Windows, and macOS outputs. git diff --check and the local conventions check pass. I built the relinker-only Release configuration locally and ran the full CTest suite: 242/245 passed. The directly affected executable_export_bounds and macos_executable_exports tests both pass. The other local failures were pr_overlap_encoding (its git-root test harness failed in this isolated worktree) and two host instruction tests exiting with Illegal; I have not attributed those to this change. Hosted relinker tests pass on Ubuntu, Windows, macOS ARM64 and macOS x86_64, and relinker ASan passes. Product builds were skipped by eligibility. The additional Windows 11 fixture runs in the description are author-reported, not independently reproduced. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638644529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10646#pullrequestreview-5638644529). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T14:45:08Z. **Review:** APPROVED --- Reviewed exact head `f1a7f913c726b782dcdaf11fb496fd3915b80eb3`. The pipeline now applies the guest-module export bounds at the common export-copy point, preserving the `SHN_ABS` exemption and validating TLS exports against `PT_TLS`; the fixture covers accepted and rejected symbol shapes across Linux default/lazy binding, Windows, and macOS outputs. `git diff --check` and the local conventions check pass. I built the relinker-only Release configuration locally and ran the full CTest suite: 242/245 passed. The directly affected `executable_export_bounds` and `macos_executable_exports` tests both pass. The other local failures were `pr_overlap_encoding` (its git-root test harness failed in this isolated worktree) and two host instruction tests exiting with `Illegal`; I have not attributed those to this change. Hosted relinker tests pass on Ubuntu, Windows, macOS ARM64 and macOS x86_64, and relinker ASan passes. Product builds were skipped by eligibility. The additional Windows 11 fixture runs in the description are author-reported, not independently reproduced. No blocking issue found.
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!10646
No description provided.