fix(relinker): validate program header table bounds before reading #11993
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11993"
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 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()), throwingRelinkerException("FileByteOffset out of bounds", header.ProgramHeaderOffset).This ensures consistent table-level error precedence and failure offset reporting:
header.ProgramHeaderOffset) prior to per-entry layout validation (such as entry 0PT_LOADfile size exceeding memory size).header.ProgramHeaderOffsetrather than deep inside an entry field read.e_phoffeven whene_phnum == 0, matching the symmetry ofElfReader::ReadSectionHeaders.Also extends
"program header table out of bounds"incore/relinker/relinker/tests/ElfReaderBoundsTests.cppwith discriminating assertions checking exactFailureOffsetfor 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
elf_reader_bounds_tests(all 40 cases passed, including enhanced "program header table out of bounds").mainwhen table bounds check is omitted (FAIL: program header table out of bounds: Unexpected parsed value).python tools/check_conventions.py --base upstream/main --head HEADpassed with 0 findings.git diff --check upstream/main...HEADclean (exit 0).fb1a66fe38merged onto1092ba2720.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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/mainpasses,pr_overlap --branchreports no overlap.The new test passes without the fix. I built
elf_reader_bounds_testsfrom head's test file withElfReader.cppreverted toupstream/main(cccdaa2734): every case passes, including "program header table out of bounds". On main every_readU32At/_readU64Atalready range-checks with_rangeFitsand 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(sameFixture()):e_phoff = 0x300-55(straddles end)FileByteOffset out of bounds @0x2f9... @0x2c9... @0x70... @0x40e_phnum=20overruns, entry 0 PT_LOAD filesz>memszPT_LOAD file size exceeds memory size @0x240FileByteOffset out of bounds @0x40e_phnum=20overruns, entry 0 segment OOBSegment offset out of bounds @0x10000FileByteOffset out of bounds @0x40e_phnum=0,e_phoff=~0FileByteOffset out of bounds @0xffffffffffffffffSo the real effects are:
FailureOffset: a truncated table is now reported as a whole (ate_phoff) before per-entry validation. That seems like the useful part, and C/D show it.e_phnum == 0, a nonzero or out-of-rangee_phoffis now refused where main accepted it (_rangeFits(off, 0, size)fails whenoff > size). This matchesReadSectionHeaders, 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:
FailureOffset == e_phofffor A/B, fails on base and passes on head. If (2) is intended, add E as an explicit case. If it isn't, guard withProgramHeaderCount != 0."Section header table out of bounds"for symmetry. Not blocking.