fix(relinker): validate program header table bounds before reading #11993

Merged
slopgobblercobbler merged 1 commit from upstream-pr/11993 into main 2026-11-02 12:28:59 +00:00
slopgobblercobbler commented 2026-10-27 15:51:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-27T15:51:21Z.


What

In ElfReader::ReadProgramHeaders, validates the program header table bounds before entering the per-entry loop using _rangeFits(header.ProgramHeaderOffset, tableSize, _fileBuffer.size()), throwing RelinkerException("FileByteOffset out of bounds", header.ProgramHeaderOffset).

This ensures consistent table-level error precedence and failure offset reporting:

  1. Error precedence: A truncated or out-of-bounds program header table is reported as a whole (at header.ProgramHeaderOffset) prior to per-entry layout validation (such as entry 0 PT_LOAD file size exceeding memory size).
  2. Offset accuracy: Failure offsets for truncated or straddling tables are pinned directly at header.ProgramHeaderOffset rather than deep inside an entry field read.
  3. Out-of-bounds offsets with zero headers: Disallows an out-of-range e_phoff even when e_phnum == 0, matching the symmetry of ElfReader::ReadSectionHeaders.

Also extends "program header table out of bounds" in core/relinker/relinker/tests/ElfReaderBoundsTests.cpp with discriminating assertions checking exact FailureOffset for straddling/truncated tables, precedence over per-entry validation, and zero headers with an out-of-range table offset (RED on main, GREEN on head).

Tested

  • Unit test elf_reader_bounds_tests (all 40 cases passed, including enhanced "program header table out of bounds").
  • Negative control confirmed: test fails on main when table bounds check is omitted (FAIL: program header table out of bounds: Unexpected parsed value).
  • python tools/check_conventions.py --base upstream/main --head HEAD passed with 0 findings.
  • git diff --check upstream/main...HEAD clean (exit 0).
  • Windows 11 verification courtesy of @JJenkx: Windows 11 Enterprise 10.0.22621, WinLibs GCC 15.2.0 UCRT r7, Ninja Release build, all 376 relinker CTests passed in 3 runs with head fb1a66fe38 merged onto 1092ba2720.
  • macOS lane build and test verification courtesy of @rooklz (263/263 build, 366/366 ctest passed).

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5036923382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11993). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-27T15:51:21Z. --- ### What In `ElfReader::ReadProgramHeaders`, validates the program header table bounds before entering the per-entry loop using `_rangeFits(header.ProgramHeaderOffset, tableSize, _fileBuffer.size())`, throwing `RelinkerException("FileByteOffset out of bounds", header.ProgramHeaderOffset)`. This ensures consistent table-level error precedence and failure offset reporting: 1. Error precedence: A truncated or out-of-bounds program header table is reported as a whole (at `header.ProgramHeaderOffset`) prior to per-entry layout validation (such as entry 0 `PT_LOAD` file size exceeding memory size). 2. Offset accuracy: Failure offsets for truncated or straddling tables are pinned directly at `header.ProgramHeaderOffset` rather than deep inside an entry field read. 3. Out-of-bounds offsets with zero headers: Disallows an out-of-range `e_phoff` even when `e_phnum == 0`, matching the symmetry of `ElfReader::ReadSectionHeaders`. Also extends `"program header table out of bounds"` in `core/relinker/relinker/tests/ElfReaderBoundsTests.cpp` with discriminating assertions checking exact `FailureOffset` for straddling/truncated tables, precedence over per-entry validation, and zero headers with an out-of-range table offset (RED on main, GREEN on head). ### Tested - Unit test `elf_reader_bounds_tests` (all 40 cases passed, including enhanced "program header table out of bounds"). - Negative control confirmed: test fails on `main` when table bounds check is omitted (FAIL: program header table out of bounds: Unexpected parsed value). - `python tools/check_conventions.py --base upstream/main --head HEAD` passed with 0 findings. - `git diff --check upstream/main...HEAD` clean (exit 0). - Windows 11 verification courtesy of @JJenkx: Windows 11 Enterprise 10.0.22621, WinLibs GCC 15.2.0 UCRT r7, Ninja Release build, all 376 relinker CTests passed in 3 runs with head `fb1a66fe38` merged onto `1092ba2720`. - macOS lane build and test verification courtesy of @rooklz (263/263 build, 366/366 ctest passed). ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-28 02:05:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T02:05:06Z.

Review: COMMENTED


Reproduced on macOS (Apple clang, relinker-only lane) at fb1a66fe38. Head build 263/263, ctest 366/366, check_conventions --base upstream/main passes, pr_overlap --branch reports no overlap.

The new test passes without the fix. I built elf_reader_bounds_tests from head's test file with ElfReader.cpp reverted to upstream/main (cccdaa2734): every case passes, including "program header table out of bounds". On main every _readU32At/_readU64At already range-checks with _rangeFits and throws the same "FileByteOffset out of bounds". So base never read past the buffer, and the "underflows or non-atomic offset reads" in the description don't happen. The message assertion can't tell base from head either.

What the patch does change, from a standalone probe linked against base and against head ElfReader.cpp (same Fixture()):

input base head
A: e_phoff = 0x300-55 (straddles end) FileByteOffset out of bounds @0x2f9 ... @0x2c9
B: file truncated to 64+56-1 ... @0x70 ... @0x40
C: e_phnum=20 overruns, entry 0 PT_LOAD filesz>memsz PT_LOAD file size exceeds memory size @0x240 FileByteOffset out of bounds @0x40
D: e_phnum=20 overruns, entry 0 segment OOB Segment offset out of bounds @0x10000 FileByteOffset out of bounds @0x40
E: e_phnum=0, e_phoff=~0 accepted, 0 headers FileByteOffset out of bounds @0xffffffffffffffff

So the real effects are:

  1. Error precedence and FailureOffset: a truncated table is now reported as a whole (at e_phoff) before per-entry validation. That seems like the useful part, and C/D show it.
  2. A new rejection: with e_phnum == 0, a nonzero or out-of-range e_phoff is now refused where main accepted it (_rangeFits(off, 0, size) fails when off > size). This matches ReadSectionHeaders, which already does this, so it may well be intended. But it's an acceptance change, and neither the description nor the tests mention it.

Suggestions:

  • Make the test discriminate. C or D (the per-entry error vs the table error), or an assertion on FailureOffset == e_phoff for A/B, fails on base and passes on head. If (2) is intended, add E as an explicit case. If it isn't, guard with ProgramHeaderCount != 0.
  • Reword the What section to describe the precedence/offset change (and E) rather than an out-of-bounds read. Optionally use a table-specific message like the section path's "Section header table out of bounds" for symmetry. Not blocking.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5676891415 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11993#pullrequestreview-5676891415). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T02:05:06Z. **Review:** COMMENTED --- Reproduced on macOS (Apple clang, relinker-only lane) at fb1a66fe38. Head build 263/263, ctest 366/366, `check_conventions --base upstream/main` passes, `pr_overlap --branch` reports no overlap. **The new test passes without the fix.** I built `elf_reader_bounds_tests` from head's test file with `ElfReader.cpp` reverted to `upstream/main` (cccdaa2734): every case passes, including "program header table out of bounds". On main every `_readU32At`/`_readU64At` already range-checks with `_rangeFits` and throws the same `"FileByteOffset out of bounds"`. So base never read past the buffer, and the "underflows or non-atomic offset reads" in the description don't happen. The message assertion can't tell base from head either. What the patch *does* change, from a standalone probe linked against base and against head `ElfReader.cpp` (same `Fixture()`): | input | base | head | |---|---|---| | A: `e_phoff = 0x300-55` (straddles end) | `FileByteOffset out of bounds @0x2f9` | `... @0x2c9` | | B: file truncated to 64+56-1 | `... @0x70` | `... @0x40` | | C: `e_phnum=20` overruns, entry 0 PT_LOAD filesz>memsz | `PT_LOAD file size exceeds memory size @0x240` | `FileByteOffset out of bounds @0x40` | | D: `e_phnum=20` overruns, entry 0 segment OOB | `Segment offset out of bounds @0x10000` | `FileByteOffset out of bounds @0x40` | | E: `e_phnum=0`, `e_phoff=~0` | **accepted, 0 headers** | **`FileByteOffset out of bounds @0xffffffffffffffff`** | So the real effects are: 1. Error precedence and `FailureOffset`: a truncated table is now reported as a whole (at `e_phoff`) before per-entry validation. That seems like the useful part, and C/D show it. 2. A new rejection: with `e_phnum == 0`, a nonzero or out-of-range `e_phoff` is now refused where main accepted it (`_rangeFits(off, 0, size)` fails when `off > size`). This matches `ReadSectionHeaders`, which already does this, so it may well be intended. But it's an acceptance change, and neither the description nor the tests mention it. Suggestions: - Make the test discriminate. C or D (the per-entry error vs the table error), or an assertion on `FailureOffset == e_phoff` for A/B, fails on base and passes on head. If (2) is intended, add E as an explicit case. If it isn't, guard with `ProgramHeaderCount != 0`. - Reword the What section to describe the precedence/offset change (and E) rather than an out-of-bounds read. Optionally use a table-specific message like the section path's `"Section header table out of bounds"` for symmetry. Not blocking.
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!11993
No description provided.