Strict NID filtering reads relative relocations beyond PT_LOAD bounds #2221
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:05:38Z.
Bug
RelativeRelocationIndexvalidates only the starting address ofDT_RELAandDT_JMPRELtables. A malformed ELF can declare a relocation table that extends beyond its file-backedPT_LOADsegment.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:
PT_LOAD.DT_RELAorDT_JMPRELnear the end of that segment.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_LOADand 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.
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:3f4480eonfix/relocation-table-bounds.Root cause
vaddrToFileOffsetaccepted a table whose start fell inside a file-backedPT_LOAD, andcollectRelativeEntriesthen walkedtableSizefrom there. The per-entry guard compared against the whole input file: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
tableSizeis a multiple ofsizeof(Elf64_Rela): the loop conditionoff + RelaEntSize <= tableSizesilently dropped a trailing partial entry.Change
tableSizethat is not a multiple of the entry size.PT_LOADfile range to the input, which makes thefileOffset + offsetInSegmentarithmetic unable to wrap, so no separateUINT64_MAXguard is needed in the lookup.PT_DYNAMICbound assegOff > size || segSz > size - segOffinstead ofsegOff + segSz > size, which could overflow.Tests
BuildRelativeRelocationIndexhad no test, soRelocationTableBoundsTests.cppis new: a table inside its segment is indexed, and aDT_RELAtable past the segment, aDT_JMPREL/DT_PLTRELSZtable past the segment, an unaligned size, a size near the end of the address space, an address inside no segment, andPT_LOAD/PT_DYNAMICranges 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:python tools/check_conventions.py --base origin/mainpasses.One note for reviewers: this removes the
"Relocation entry out of bounds"throw fromRelativeRelocationIndex.cpp, butRelaBoundsTestsis unaffected — the message it asserts on comes fromRelinkerPipeline.cpp:195, a separate path that this change does not touch.RelaBoundsTestsis 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).