fix(relinker): check PT_LOAD and uncovered PT_SCE_RELRO file ranges #5741

Merged
IshmamDC217 merged 2 commits from upstream-pr/5741 into main 2026-10-15 13:07:44 +00:00
IshmamDC217 commented 2026-10-14 08:52:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-14T08:52:07Z.


What

ElfReader::ReadProgramHeaders checked that a PT_LOAD's file size does not exceed its memory size, but not that its file range lies inside the input.

  • The Windows and macOS writers reject such a segment later (Invalid PT_LOAD file range).
  • The Linux patcher keeps the original PT_LOADs and appends its own block at the old end of the file, so the conversion succeeds. A load that reaches past the input then maps the relinker's own data into the guest segment: .dynstr (\0$ORIGIN/libs…), .dynsym, .dynamic and the entry stub.

A PT_SCE_RELRO that no PT_LOAD covers (the PS4 layout) is converted to a PT_LOAD after the per-header loop, so it skipped both checks.

  • A file size larger than its memory size was accepted for Linux; readelf reports the resulting segment as invalid.
  • Windows and macOS rejected it with the unrelated file-range error.

This change rejects a PT_LOAD whose file range exceeds the input, reusing the existing Segment offset out of bounds error, and runs both checks on an uncovered PT_SCE_RELRO before converting it.

Tested

Linux x86-64, GCC 13, relinker-only build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON): all 158 tests pass.

Three new cases in ElfReaderBoundsTests.cpp (elf_reader_bounds):

  • a PT_LOAD reaching past the end of the file;
  • an uncovered PT_SCE_RELRO with file size larger than memory size;
  • an uncovered PT_SCE_RELRO reaching past the end of the file.

Before the change all three fail with Invalid program header was accepted. After it they pass.

End to end, the test_sce_relro.py fixture with a PT_LOAD at 0x2000 + 0x2000 in a 0x3000-byte file:

  • Before the change: the Linux conversion exited 0, and the output's offsets 0x3000+ held the relinker's .dynstr.
  • After the change: Linux, --windows and --macos all fail with Segment offset out of bounds (offset 0x2000).

python3 tools/check_conventions.py --base origin/main passes. Windows and macOS were not built.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01P9JfvrCsztJtbkd5SATAzL

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4855367417 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741). Originally posted by [IshmamDC217](https://github.com/IshmamDC217) on 2026-10-14T08:52:07Z. --- ### What `ElfReader::ReadProgramHeaders` checked that a `PT_LOAD`'s file size does not exceed its memory size, but not that its file range lies inside the input. - The Windows and macOS writers reject such a segment later (`Invalid PT_LOAD file range`). - The Linux patcher keeps the original `PT_LOAD`s and appends its own block at the old end of the file, so the conversion succeeds. A load that reaches past the input then maps the relinker's own data into the guest segment: `.dynstr` (`\0$ORIGIN/libs…`), `.dynsym`, `.dynamic` and the entry stub. A `PT_SCE_RELRO` that no `PT_LOAD` covers (the PS4 layout) is converted to a `PT_LOAD` after the per-header loop, so it skipped both checks. - A file size larger than its memory size was accepted for Linux; `readelf` reports the resulting segment as invalid. - Windows and macOS rejected it with the unrelated file-range error. This change rejects a `PT_LOAD` whose file range exceeds the input, reusing the existing `Segment offset out of bounds` error, and runs both checks on an uncovered `PT_SCE_RELRO` before converting it. ### Tested Linux x86-64, GCC 13, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`): all 158 tests pass. Three new cases in `ElfReaderBoundsTests.cpp` (`elf_reader_bounds`): - a `PT_LOAD` reaching past the end of the file; - an uncovered `PT_SCE_RELRO` with file size larger than memory size; - an uncovered `PT_SCE_RELRO` reaching past the end of the file. Before the change all three fail with `Invalid program header was accepted`. After it they pass. End to end, the `test_sce_relro.py` fixture with a `PT_LOAD` at 0x2000 + 0x2000 in a 0x3000-byte file: - **Before the change:** the Linux conversion exited 0, and the output's offsets 0x3000+ held the relinker's `.dynstr`. - **After the change:** Linux, `--windows` and `--macos` all fail with `Segment offset out of bounds (offset 0x2000)`. `python3 tools/check_conventions.py --base origin/main` passes. Windows and macOS were not built. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01P9JfvrCsztJtbkd5SATAzL
wachi35676 commented 2026-10-14 13:46:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T13:46:12Z.


Validated at head 3b75f928 (base main 2582ec11, no relinker diff vs the PR's parent) on Windows 11, MinGW-w64 g++ 15.2.0, relinker-only Release build.

  • elf_reader_bounds with this PR's test file built against base: all 3 new cases fail with Invalid program header was accepted; on head they pass.
  • Full relinker ctest: 153/153 on base, 153/153 on head (incl. sce_relro, elf_offsets, linux_load_alignment).
  • Reproduced on base with the test_sce_relro.py fixture (0x3000 bytes):
    • RW PT_LOAD 0x2000+0x2000: Linux exits 0. readelf shows LOAD 0x002000 FileSiz 0x2000 in a 0x32fe-byte output, and offset 0x3000 holds \0$ORIGIN/libs.
    • Uncovered RELRO with filesz > memsz: Linux exits 0, readelf: Error: the segment's file size is larger than its memory size.
    • Head rejects both on Linux, --windows and --macos.
  • Offset 0xffffffffffffff00 + 0x200 is rejected without wrapping. bss PT_LOAD/uncovered RELRO (filesz < memsz) still convert on all three targets. Executable loads were already caught by ReadSegment; this mainly closes the data-segment and RELRO gaps.

Nits (non-blocking):

  • TranslateVirtualAddress's PT_LOAD file mapping out of bounds check is now unreachable; the two translation … file … cases now pass via ReadProgramHeaders.
  • PT_SCE_RELRO file range exceeds the input file differs from the … out of bounds wording used nearby (Dynamic segment out of bounds, Section offset out of bounds).
  • A filesz-0 PT_LOAD with offset past EOF is now rejected on Linux too (Windows/macOS already did).
  • An exact-fit acceptance case (offset + filesz == file size) would pin the boundary.

Not tested: native Linux/macOS builds, real PS4/PS5 titles, running the outputs.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6169844091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741#issuecomment-6169844091). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T13:46:12Z. --- Validated at head `3b75f928` (base `main` `2582ec11`, no relinker diff vs the PR's parent) on Windows 11, MinGW-w64 g++ 15.2.0, relinker-only Release build. - `elf_reader_bounds` with this PR's test file built against base: all 3 new cases fail with `Invalid program header was accepted`; on head they pass. - Full relinker ctest: 153/153 on base, 153/153 on head (incl. `sce_relro`, `elf_offsets`, `linux_load_alignment`). - Reproduced on base with the `test_sce_relro.py` fixture (0x3000 bytes): - RW `PT_LOAD` 0x2000+0x2000: Linux exits 0. `readelf` shows `LOAD 0x002000 FileSiz 0x2000` in a 0x32fe-byte output, and offset 0x3000 holds `\0$ORIGIN/libs`. - Uncovered RELRO with filesz > memsz: Linux exits 0, `readelf: Error: the segment's file size is larger than its memory size`. - Head rejects both on Linux, `--windows` and `--macos`. - Offset `0xffffffffffffff00` + 0x200 is rejected without wrapping. bss `PT_LOAD`/uncovered RELRO (filesz < memsz) still convert on all three targets. Executable loads were already caught by `ReadSegment`; this mainly closes the data-segment and RELRO gaps. Nits (non-blocking): - `TranslateVirtualAddress`'s `PT_LOAD file mapping out of bounds` check is now unreachable; the two `translation … file …` cases now pass via `ReadProgramHeaders`. - `PT_SCE_RELRO file range exceeds the input file` differs from the `… out of bounds` wording used nearby (`Dynamic segment out of bounds`, `Section offset out of bounds`). - A filesz-0 `PT_LOAD` with offset past EOF is now rejected on Linux too (Windows/macOS already did). - An exact-fit acceptance case (offset + filesz == file size) would pin the boundary. Not tested: native Linux/macOS builds, real PS4/PS5 titles, running the outputs. AI-assisted: yes (Claude Code); results above were run on this machine.
IshmamDC217 commented 2026-10-14 14:19:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-14T14:19:51Z.


@wachi35676 Thanks for validating on Windows. Pushed 2ab7a63a: removed the now-unreachable range check in TranslateVirtualAddress (the two translation cases now fail in ReadProgramHeaders), switched the uncovered RELRO range error to Segment offset out of bounds to match PT_LOAD, and added an exact-fit case (offset + filesz == file size) for both PT_LOAD and uncovered PT_SCE_RELRO. I left the filesz-0 case as is: rejecting an offset past EOF on Linux matches what Windows and macOS already do. Relinker-only ctest 158/158 on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6170535609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741#issuecomment-6170535609). Originally posted by [IshmamDC217](https://github.com/IshmamDC217) on 2026-10-14T14:19:51Z. --- @wachi35676 Thanks for validating on Windows. Pushed 2ab7a63a: removed the now-unreachable range check in TranslateVirtualAddress (the two translation cases now fail in ReadProgramHeaders), switched the uncovered RELRO range error to Segment offset out of bounds to match PT_LOAD, and added an exact-fit case (offset + filesz == file size) for both PT_LOAD and uncovered PT_SCE_RELRO. I left the filesz-0 case as is: rejecting an offset past EOF on Linux matches what Windows and macOS already do. Relinker-only ctest 158/158 on Linux.
wachi35676 commented 2026-10-14 14:52:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T14:52:32Z.


Follow-up: re-validated at 2ab7a63a3 (Windows 11, MinGW g++, relinker-only Release). Thanks for the quick turnaround.

  • Relinker ctest: 153/153 on the head, 160/160 with the head merged into current main (f0854887d). It merges cleanly.
  • Removing the check from TranslateVirtualAddress is safe. It calls ReadProgramHeaders() itself, so every PT_LOAD it can match has already passed one of the two new range checks. The two translation tests still pass and now act as end-to-end guards through that path. No test expected the old PT_LOAD file mapping out of bounds text.
  • The uncovered RELRO range error now reads Segment offset out of bounds, matching PT_LOAD and the existing test_elf_offsets.py expectation. Resolved.
  • The exact-fit case is exact: 0x240 + 0xc0 == 0x300 for both PT_LOAD and SCE RELRO. Changing the check to an off-by-one (FileSize + 1) makes it fail. With the CLI on all three targets, a segment ending exactly at EOF is accepted (rc=0) and one byte past it is rejected (rc=2). Resolved.
  • On base, the new range tests still fail (3/3) and pass on the head. The earlier crafted inputs give the same results as before: the bad ones are rejected on linux/windows/macos, and PT_LOAD/RELRO bss is accepted.
  • filesz-0 with offset past EOF: agreed. On base, --windows and --macos already reject it (Invalid PT_LOAD file range (offset 0x5000)), so rejecting it on Linux makes the three targets consistent.

Not tested: Linux/macOS hosts, real game binaries.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6171208468 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741#issuecomment-6171208468). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T14:52:32Z. --- Follow-up: re-validated at `2ab7a63a3` (Windows 11, MinGW g++, relinker-only Release). Thanks for the quick turnaround. - Relinker ctest: 153/153 on the head, 160/160 with the head merged into current `main` (`f0854887d`). It merges cleanly. - Removing the check from `TranslateVirtualAddress` is safe. It calls `ReadProgramHeaders()` itself, so every PT_LOAD it can match has already passed one of the two new range checks. The two translation tests still pass and now act as end-to-end guards through that path. No test expected the old `PT_LOAD file mapping out of bounds` text. - The uncovered RELRO range error now reads `Segment offset out of bounds`, matching PT_LOAD and the existing `test_elf_offsets.py` expectation. Resolved. - The exact-fit case is exact: 0x240 + 0xc0 == 0x300 for both PT_LOAD and SCE RELRO. Changing the check to an off-by-one (`FileSize + 1`) makes it fail. With the CLI on all three targets, a segment ending exactly at EOF is accepted (rc=0) and one byte past it is rejected (rc=2). Resolved. - On base, the new range tests still fail (3/3) and pass on the head. The earlier crafted inputs give the same results as before: the bad ones are rejected on linux/windows/macos, and PT_LOAD/RELRO bss is accepted. - filesz-0 with offset past EOF: agreed. On base, `--windows` and `--macos` already reject it (`Invalid PT_LOAD file range (offset 0x5000)`), so rejecting it on Linux makes the three targets consistent. Not tested: Linux/macOS hosts, real game binaries. AI-assisted: yes (Claude Code); results above were run on this machine.
IshmamDC217 commented 2026-10-15 11:17:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-15T11:17:33Z.


Rebased onto current main (8276a1d05); relinker-only ctest 172/172 on Linux and it still merges cleanly.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190789187 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741#issuecomment-6190789187). Originally posted by [IshmamDC217](https://github.com/IshmamDC217) on 2026-10-15T11:17:33Z. --- Rebased onto current main (8276a1d05); relinker-only ctest 172/172 on Linux and it still merges cleanly.
adriadam10 commented 2026-10-15 12:22:40 +00:00 (Migrated from github.com)

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


Checked on Linux (relinker scope, 134/134 ctest): the three new elf_reader_bounds cases fail without the ElfReader.cpp change and pass with it; _rangeFits is overflow-safe and the exact-fit cases are covered. Dropping the check in TranslateVirtualAddress is safe since it goes through ReadProgramHeaders. Also confirmed by @wachi35676 on Windows/MinGW; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191940091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5741#issuecomment-6191940091). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T12:22:40Z. --- Checked on Linux (relinker scope, 134/134 ctest): the three new `elf_reader_bounds` cases fail without the `ElfReader.cpp` change and pass with it; `_rangeFits` is overflow-safe and the exact-fit cases are covered. Dropping the check in `TranslateVirtualAddress` is safe since it goes through `ReadProgramHeaders`. Also confirmed by @wachi35676 on Windows/MinGW; 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!5741
No description provided.