Strict NID filtering reads relative relocations beyond PT_LOAD bounds #2221

Closed
opened 2026-10-09 12:05:38 +00:00 by EshaanJaiswal · 1 comment
EshaanJaiswal commented 2026-10-09 12:05:38 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:05:38Z.


Bug

RelativeRelocationIndex validates only the starting address of DT_RELA and DT_JMPREL tables. A malformed ELF can declare a relocation table that extends beyond its file-backed PT_LOAD segment.

This can cause the parser to read bytes outside the mapped segment as relocation entries.

Expected behavior

The complete relocation table should be validated before any entries are read. Invalid metadata should produce a clear error instead of being accepted.

Reproduction

Use a synthetic ELF fixture with:

  • A file-backed PT_LOAD.
  • DT_RELA or DT_JMPREL near the end of that segment.
  • A table size extending beyond the segment.
  • Enough bytes in the file for the current parser to continue reading.

The same behavior should also be tested with unaligned relocation sizes and values near UINT64_MAX.

Proposed fix

Validate the complete relocation range against the containing PT_LOAD and the input file before reading entries.

Also reject relocation table sizes that are not multiples of sizeof(Elf64_Rela).

The regression test runs entirely on a normal host and requires no PS5, controller, firmware, or game files.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5777802473 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2221). Originally posted by [EshaanJaiswal](https://github.com/EshaanJaiswal) on 2026-10-09T12:05:38Z. --- ## Bug `RelativeRelocationIndex` validates only the starting address of `DT_RELA` and `DT_JMPREL` tables. A malformed ELF can declare a relocation table that extends beyond its file-backed `PT_LOAD` segment. This can cause the parser to read bytes outside the mapped segment as relocation entries. ## Expected behavior The complete relocation table should be validated before any entries are read. Invalid metadata should produce a clear error instead of being accepted. ## Reproduction Use a synthetic ELF fixture with: - A file-backed `PT_LOAD`. - `DT_RELA` or `DT_JMPREL` near the end of that segment. - A table size extending beyond the segment. - Enough bytes in the file for the current parser to continue reading. The same behavior should also be tested with unaligned relocation sizes and values near `UINT64_MAX`. ## Proposed fix Validate the complete relocation range against the containing `PT_LOAD` and the input file before reading entries. Also reject relocation table sizes that are not multiples of `sizeof(Elf64_Rela)`. The regression test runs entirely on a normal host and requires no PS5, controller, firmware, or game files.
ycbuzi commented 2026-10-09 23:10:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-09T23:10:40Z.


Confirmed and fixed on current main (c9d42619). Candidate commit, since intake is paused for this account: 3f4480e on fix/relocation-table-bounds.

Root cause

vaddrToFileOffset accepted a table whose start fell inside a file-backed PT_LOAD, and collectRelativeEntries then walked tableSize from there. The per-entry guard compared against the whole input file:

if (pos + RelaEntSize > elfBytes.size())
    throw RelinkerException("Relocation entry out of bounds", pos);

So a table that reached past its segment but stayed inside the file was accepted, and bytes from other segments or from file padding were read as relocation entries. There was also no check that tableSize is a multiple of sizeof(Elf64_Rela): the loop condition off + RelaEntSize <= tableSize silently dropped a trailing partial entry.

Change

  • Validate the complete table range against the segment that contains its start, and reject a table that reaches past it.
  • Reject a tableSize that is not a multiple of the entry size.
  • Bound each PT_LOAD file range to the input, which makes the fileOffset + offsetInSegment arithmetic unable to wrap, so no separate UINT64_MAX guard is needed in the lookup.
  • Rewrite the PT_DYNAMIC bound as segOff > size || segSz > size - segOff instead of segOff + segSz > size, which could overflow.

Tests

BuildRelativeRelocationIndex had no test, so RelocationTableBoundsTests.cpp is new: a table inside its segment is indexed, and a DT_RELA table past the segment, a DT_JMPREL/DT_PLTRELSZ table past the segment, an unaligned size, a size near the end of the address space, an address inside no segment, and PT_LOAD/PT_DYNAMIC ranges past the file are all rejected.

The fixture deliberately leaves padding after the segment so the overlong table still lands inside the file, which is the case the previous check missed. Reverting only the source change makes the test fail with A relocation table extending past its PT_LOAD was accepted, so it does exercise the bug rather than the new error path.

It is registered cross-platform rather than under the Linux-only block, because it drives the function in process and needs no relinker binary, temp directory or ELF on disk.

Verification

Windows 11, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), CMake 4.3.1, Ninja 1.13.2, Release:

100% tests passed, 0 tests failed out of 46

python tools/check_conventions.py --base origin/main passes.

One note for reviewers: this removes the "Relocation entry out of bounds" throw from RelativeRelocationIndex.cpp, but RelaBoundsTests is unaffected — the message it asserts on comes from RelinkerPipeline.cpp:195, a separate path that this change does not touch. RelaBoundsTests is Linux-only, so I could not execute it here; that reasoning is from reading the source, not from a run.

Not tested on Linux (no machine available).

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090820727 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2221#issuecomment-6090820727). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-09T23:10:40Z. --- Confirmed and fixed on current `main` (`c9d42619`). Candidate commit, since intake is paused for this account: [`3f4480e`](https://github.com/ycbuzi/AnyPS5/commit/3f4480ec4ac919949f228392f647d8579555a84a) on `fix/relocation-table-bounds`. ## Root cause `vaddrToFileOffset` accepted a table whose **start** fell inside a file-backed `PT_LOAD`, and `collectRelativeEntries` then walked `tableSize` from there. The per-entry guard compared against the whole input file: ```cpp if (pos + RelaEntSize > elfBytes.size()) throw RelinkerException("Relocation entry out of bounds", pos); ``` So a table that reached past its segment but stayed inside the file was accepted, and bytes from other segments or from file padding were read as relocation entries. There was also no check that `tableSize` is a multiple of `sizeof(Elf64_Rela)`: the loop condition `off + RelaEntSize <= tableSize` silently dropped a trailing partial entry. ## Change - Validate the complete table range against the segment that contains its start, and reject a table that reaches past it. - Reject a `tableSize` that is not a multiple of the entry size. - Bound each `PT_LOAD` file range to the input, which makes the `fileOffset + offsetInSegment` arithmetic unable to wrap, so no separate `UINT64_MAX` guard is needed in the lookup. - Rewrite the `PT_DYNAMIC` bound as `segOff > size || segSz > size - segOff` instead of `segOff + segSz > size`, which could overflow. ## Tests `BuildRelativeRelocationIndex` had no test, so `RelocationTableBoundsTests.cpp` is new: a table inside its segment is indexed, and a `DT_RELA` table past the segment, a `DT_JMPREL`/`DT_PLTRELSZ` table past the segment, an unaligned size, a size near the end of the address space, an address inside no segment, and `PT_LOAD`/`PT_DYNAMIC` ranges past the file are all rejected. The fixture deliberately leaves padding after the segment so the overlong table still lands **inside the file**, which is the case the previous check missed. Reverting only the source change makes the test fail with `A relocation table extending past its PT_LOAD was accepted`, so it does exercise the bug rather than the new error path. It is registered cross-platform rather than under the Linux-only block, because it drives the function in process and needs no relinker binary, temp directory or ELF on disk. ## Verification Windows 11, WinLibs GCC 15.2.0 r7 (`x86_64-ucrt-posix-seh`), CMake 4.3.1, Ninja 1.13.2, Release: ``` 100% tests passed, 0 tests failed out of 46 ``` `python tools/check_conventions.py --base origin/main` passes. One note for reviewers: this removes the `"Relocation entry out of bounds"` throw from `RelativeRelocationIndex.cpp`, but **`RelaBoundsTests` is unaffected** — the message it asserts on comes from `RelinkerPipeline.cpp:195`, a separate path that this change does not touch. `RelaBoundsTests` is Linux-only, so I could not execute it here; that reasoning is from reading the source, not from a run. Not tested on Linux (no machine available). AI-assisted: yes (Claude Code).
Sign in to join this conversation.
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#2221
No description provided.