fix(relinker): reject overlapping PT_LOAD segments before sizing the Windows image #1238

Merged
hashaam1217 merged 1 commit from upstream-pr/1238 into main 2026-10-08 09:14:58 +00:00
hashaam1217 commented 2026-10-07 19:06:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-07T19:06:15Z.


What

relinker --windows crashed with SIGSEGV on an ELF whose first PT_LOAD segment reaches past the end of the last one. WindowsLoadImage sorts the segments, sizes data and pageFlags from the end of the last segment, and checked for overlap inside the loop that copies each segment and marks its pages. The first segment's page loop ran past pageFlags before the next segment's overlap check could throw (AddressSanitizer: heap-buffer-overflow in WindowsLoadImage::WindowsLoadImage).

Overlap is now checked in a pass of its own before anything is sized, so such an input fails with "Overlapping PT_LOAD memory ranges". Valid inputs are unchanged: when no segments overlap, the last segment's end is the largest end.

Found by fuzzing the relinker: an AddressSanitizer + UBSan build, inputs mutated from the fixtures of the relinker Python tests, in Linux, --windows, --to-intel and guest-module modes. The fuzzer is not part of this PR.

Tested

Ubuntu 24.04.5, GCC 13.3.0. Full build and ctest: 399/399 passed.

  • executable_segments: two new cases. The first segment's memory size is set to 2^52, overlapping the other segments and reaching past the last, and to 0x1300, overlapping only the next one. Both must fail with "Overlapping PT_LOAD memory ranges" and write no output. With the relinker from main the first case dies with SIGSEGV (returncode=-11).
  • AddressSanitizer build: the 20,000 fuzz inputs that hit this overflow 35 times hit it 0 times after the change.
  • tools/check_conventions.py --base origin/main: passed.
  • Windows was not run.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4777202717 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1238). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-07T19:06:15Z. --- ### What `relinker --windows` crashed with SIGSEGV on an ELF whose first PT_LOAD segment reaches past the end of the last one. `WindowsLoadImage` sorts the segments, sizes `data` and `pageFlags` from the end of the last segment, and checked for overlap inside the loop that copies each segment and marks its pages. The first segment's page loop ran past `pageFlags` before the next segment's overlap check could throw (AddressSanitizer: heap-buffer-overflow in `WindowsLoadImage::WindowsLoadImage`). Overlap is now checked in a pass of its own before anything is sized, so such an input fails with "Overlapping PT_LOAD memory ranges". Valid inputs are unchanged: when no segments overlap, the last segment's end is the largest end. Found by fuzzing the relinker: an AddressSanitizer + UBSan build, inputs mutated from the fixtures of the relinker Python tests, in Linux, `--windows`, `--to-intel` and guest-module modes. The fuzzer is not part of this PR. ### Tested Ubuntu 24.04.5, GCC 13.3.0. Full build and `ctest`: 399/399 passed. - `executable_segments`: two new cases. The first segment's memory size is set to 2^52, overlapping the other segments and reaching past the last, and to 0x1300, overlapping only the next one. Both must fail with "Overlapping PT_LOAD memory ranges" and write no output. With the relinker from `main` the first case dies with SIGSEGV (`returncode=-11`). - AddressSanitizer build: the 20,000 fuzz inputs that hit this overflow 35 times hit it 0 times after the change. - `tools/check_conventions.py --base origin/main`: passed. - Windows was not run. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-08 09:14:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:14:55Z.


Checked the overlap pre-pass against the diff, ran the build and full ctest on top of main (430/430) and reverted WindowsLoadImage.cpp to confirm executable_segments fails without it; the MappedAddress + MemorySize wrap is already rejected earlier in the constructor. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056661693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1238#issuecomment-6056661693). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:14:55Z. --- Checked the overlap pre-pass against the diff, ran the build and full ctest on top of main (430/430) and reverted `WindowsLoadImage.cpp` to confirm `executable_segments` fails without it; the `MappedAddress + MemorySize` wrap is already rejected earlier in the constructor. Merging.
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!1238
No description provided.