fix(relinker): validate guest module executable segments against image bounds #5681

Closed
fogydes wants to merge 1 commit from upstream-pr/5681 into main
fogydes commented 2026-10-14 06:12:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-14T06:12:50Z.


What

  • Validates guest module executable segment bounds against \image.Bytes.size()\ before extracting code buffers for syscall analysis in \GuestModuleBuilder::Build.
  • Validates executable segment mapped address ranges against 64-bit address space limits to prevent virtual address overflow.
  • Adds regression C++ test \GuestExecutableSegmentBoundsTests.cpp\ covering out-of-bounds offset/filesize and wrapping virtual address space.

Input Triggers

  • Malformed or truncated guest module / PRX dumps where program headers declare an offset or file size exceeding the file payload.
  • Modules with corrupted mapped virtual addresses near 2^64 - 1 that wrap during code section analysis.

Tested

  • Linux x86_64: \guest_executable_segment_bounds_tests\ exercises both out-of-bounds segments and address space overflow.
  • Windows 11 x86_64: full relinker Ninja build passed with zero warnings.
  • \python tools/check_conventions.py --base origin/main: passed with zero findings.
  • \python tools/pr_overlap.py: zero conflicts with open pull requests.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4853760332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-14T06:12:50Z. --- ### What - Validates guest module executable segment bounds against \image.Bytes.size()\ before extracting code buffers for syscall analysis in \GuestModuleBuilder::Build\. - Validates executable segment mapped address ranges against 64-bit address space limits to prevent virtual address overflow. - Adds regression C++ test \GuestExecutableSegmentBoundsTests.cpp\ covering out-of-bounds offset/filesize and wrapping virtual address space. ### Input Triggers - Malformed or truncated guest module / PRX dumps where program headers declare an offset or file size exceeding the file payload. - Modules with corrupted mapped virtual addresses near 2^64 - 1 that wrap during code section analysis. ### Tested - Linux x86_64: \guest_executable_segment_bounds_tests\ exercises both out-of-bounds segments and address space overflow. - Windows 11 x86_64: full relinker Ninja build passed with zero warnings. - \python tools/check_conventions.py --base origin/main\: passed with zero findings. - \python tools/pr_overlap.py\: zero conflicts with open pull requests. ### Checklist - [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
adriadam10 commented 2026-10-15 11:57:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:57:23Z.

Review: CHANGES_REQUESTED


Thanks for the PR. Points to address:

  • No test covers the new checks. executable_segments already existed and does not exercise an out-of-bounds segment, an address-range overflow or a relocation table whose size is not a multiple of 24. Please add a C++ test (see core/relinker/relinker/tests/ElfFixture.hpp, registered in core/relinker/CMakeLists.txt) that builds such a guest module and fails on main and passes with the change, one case per check.
  • The description says "Adds safe ptrdiff_t casting for byte vector iterators", but the diff has no such change. Please make the description match the diff.
  • Two topics: segment bounds (GuestModuleBuilder.cpp:322-330) and relocation table size (GuestModuleBuilder.cpp:380). Split the second into its own PR, or explain why they are one change.
  • Please say what input triggers each case (a malformed module, a real one), since neither condition is reachable from the CI fixtures today.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5532700059 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681#pullrequestreview-5532700059). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:57:23Z. **Review:** CHANGES_REQUESTED --- Thanks for the PR. Points to address: - No test covers the new checks. `executable_segments` already existed and does not exercise an out-of-bounds segment, an address-range overflow or a relocation table whose size is not a multiple of 24. Please add a C++ test (see `core/relinker/relinker/tests/ElfFixture.hpp`, registered in `core/relinker/CMakeLists.txt`) that builds such a guest module and fails on `main` and passes with the change, one case per check. - The description says "Adds safe `ptrdiff_t` casting for byte vector iterators", but the diff has no such change. Please make the description match the diff. - Two topics: segment bounds (`GuestModuleBuilder.cpp:322-330`) and relocation table size (`GuestModuleBuilder.cpp:380`). Split the second into its own PR, or explain why they are one change. - Please say what input triggers each case (a malformed module, a real one), since neither condition is reachable from the CI fixtures today.
fogydes commented 2026-10-15 12:07:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-15T12:07:44Z.


Thanks for the review @adriadam10! I have addressed all points:

  1. C++ unit test added: Added core/relinker/relinker/tests/GuestExecutableSegmentBoundsTests.cpp using ElfFixture.hpp and registered guest_executable_segment_bounds_tests in core/relinker/CMakeLists.txt. It verifies:
    • Out-of-bounds guest executable segment offset/filesize rejects with "Guest executable segment exceeds image".
    • Wrapping mapped virtual address ranges near 2^{64}-1 reject with "Guest executable segment range exceeds address space".
  2. Single topic scope: Dropped the relocation table check (table->size() % 24 != 0) from this PR to isolate it strictly to guest executable segment bounds.
  3. PR description updated: Removed the ptrdiff_t reference to match the diff.
  4. Input triggers explained: Described the triggers in the PR body (malformed or truncated PRX dumps where program headers specify offsets/sizes beyond the file payload, and corrupted virtual address allocations).

Rebased cleanly onto latest main (b3f9f89e6).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191670782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681#issuecomment-6191670782). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-15T12:07:44Z. --- Thanks for the review @adriadam10! I have addressed all points: 1. **C++ unit test added**: Added `core/relinker/relinker/tests/GuestExecutableSegmentBoundsTests.cpp` using `ElfFixture.hpp` and registered `guest_executable_segment_bounds_tests` in `core/relinker/CMakeLists.txt`. It verifies: - Out-of-bounds guest executable segment offset/filesize rejects with `"Guest executable segment exceeds image"`. - Wrapping mapped virtual address ranges near $2^{64}-1$ reject with `"Guest executable segment range exceeds address space"`. 2. **Single topic scope**: Dropped the relocation table check (`table->size() % 24 != 0`) from this PR to isolate it strictly to guest executable segment bounds. 3. **PR description updated**: Removed the `ptrdiff_t` reference to match the diff. 4. **Input triggers explained**: Described the triggers in the PR body (malformed or truncated PRX dumps where program headers specify offsets/sizes beyond the file payload, and corrupted virtual address allocations). Rebased cleanly onto latest `main` (`b3f9f89e6`).
adriadam10 commented 2026-10-15 12:10:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:10:10Z.

Review: CHANGES_REQUESTED


Thanks for the update. Two problems:

  • guest_executable_segment_bounds fails on this head (run on the PR merged onto main). Case 1 (offset/size past the end of the file) is rejected with ELF range exceeds file: offset=0x4000, size=0x9000, fileSize=0x8000, not with Guest executable segment exceeds image.
  • The reason is that the new checks in GuestModuleBuilder.cpp:326-334 cannot be reached: GuestImageReader::Read already runs range(header.Offset, header.FileSize) for every header (GuestImageReader.cpp:118) and rejects MemorySize > max - MappedAddress as "Invalid load segment" (GuestImageReader.cpp:120) before Build sees the image. The test's third case would hit that second error too.

Unless you can build a module that passes GuestImageReader::Read and still violates one of the new conditions (and show the test failing on main), the validation is already there and this PR has nothing to add. If you find such a case, adjust the test to expect it and make the Tested section match what CI actually ran.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5532857956 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681#pullrequestreview-5532857956). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T12:10:10Z. **Review:** CHANGES_REQUESTED --- Thanks for the update. Two problems: - `guest_executable_segment_bounds` fails on this head (run on the PR merged onto main). Case 1 (offset/size past the end of the file) is rejected with `ELF range exceeds file: offset=0x4000, size=0x9000, fileSize=0x8000`, not with `Guest executable segment exceeds image`. - The reason is that the new checks in `GuestModuleBuilder.cpp:326-334` cannot be reached: `GuestImageReader::Read` already runs `range(header.Offset, header.FileSize)` for every header (`GuestImageReader.cpp:118`) and rejects `MemorySize > max - MappedAddress` as "Invalid load segment" (`GuestImageReader.cpp:120`) before `Build` sees the image. The test's third case would hit that second error too. Unless you can build a module that passes `GuestImageReader::Read` and still violates one of the new conditions (and show the test failing on `main`), the validation is already there and this PR has nothing to add. If you find such a case, adjust the test to expect it and make the Tested section match what CI actually ran.
github-actions[bot] commented 2026-10-15 12:22:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:22:18Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
#3891 core/relinker/CMakeLists.txt
#4156 core/relinker/CMakeLists.txt
#4396 core/relinker/CMakeLists.txt
#4397 core/relinker/CMakeLists.txt
#4422 core/relinker/CMakeLists.txt
#4423 core/relinker/CMakeLists.txt
#4424 core/relinker/CMakeLists.txt
#4520 core/relinker/CMakeLists.txt
#4521 core/relinker/CMakeLists.txt
#4522 core/relinker/CMakeLists.txt
#4523 core/relinker/CMakeLists.txt
#4566 core/relinker/CMakeLists.txt
#4599 core/relinker/CMakeLists.txt
#4718 core/relinker/CMakeLists.txt
#4740 core/relinker/CMakeLists.txt
#4749 core/relinker/CMakeLists.txt
#4760 core/relinker/CMakeLists.txt
#4813 core/relinker/CMakeLists.txt
#5021 core/relinker/CMakeLists.txt
#6044 core/relinker/CMakeLists.txt
#6112 core/relinker/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191933484 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681#issuecomment-6191933484). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:22:18Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | #3891 | `core/relinker/CMakeLists.txt` | | #4156 | `core/relinker/CMakeLists.txt` | | #4396 | `core/relinker/CMakeLists.txt` | | #4397 | `core/relinker/CMakeLists.txt` | | #4422 | `core/relinker/CMakeLists.txt` | | #4423 | `core/relinker/CMakeLists.txt` | | #4424 | `core/relinker/CMakeLists.txt` | | #4520 | `core/relinker/CMakeLists.txt` | | #4521 | `core/relinker/CMakeLists.txt` | | #4522 | `core/relinker/CMakeLists.txt` | | #4523 | `core/relinker/CMakeLists.txt` | | #4566 | `core/relinker/CMakeLists.txt` | | #4599 | `core/relinker/CMakeLists.txt` | | #4718 | `core/relinker/CMakeLists.txt` | | #4740 | `core/relinker/CMakeLists.txt` | | #4749 | `core/relinker/CMakeLists.txt` | | #4760 | `core/relinker/CMakeLists.txt` | | #4813 | `core/relinker/CMakeLists.txt` | | #5021 | `core/relinker/CMakeLists.txt` | | #6044 | `core/relinker/CMakeLists.txt` | | #6112 | `core/relinker/CMakeLists.txt` |
fogydes commented 2026-10-15 12:48:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-15T12:48:40Z.


Thanks for the detailed review @adriadam10. You're completely right: GuestImageReader::Read already validates range(header.Offset, header.FileSize) and checks MemorySize > max - MappedAddress for all headers before GuestModuleBuilder::Build receives the image, making the checks in GuestModuleBuilder redundant. Closing this PR accordingly.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192436012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5681#issuecomment-6192436012). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-15T12:48:40Z. --- Thanks for the detailed review @adriadam10. You're completely right: `GuestImageReader::Read` already validates `range(header.Offset, header.FileSize)` and checks `MemorySize > max - MappedAddress` for all headers before `GuestModuleBuilder::Build` receives the image, making the checks in `GuestModuleBuilder` redundant. Closing this PR accordingly.

Pull request closed

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!5681
No description provided.