test(relinker): audit ELF header validation and section link bounds #15651
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15651"
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-11-03T23:47:18Z.
Depends on: none
What
ElfReaderandElfReaderBoundsTests.cpp, asserting strict validation of header sizes, SELF container signatures, ELF magic numbers, ELF64 class, little-endian data encoding, object versions, x86-64 machine type, and program header entry sizes.SHT_SYMTAB,SHT_DYNSYM,SHT_RELA,SHT_REL) and enforcesh_linkboundary checks on symbol, relocation, and dynamic section associations.PT_NULLsegments preserve unused fields and audit positive powers-of-two program header alignment.docs/dev/debt/relinker.md.Tested
ElfReaderBoundsTests.cppwith zero failures.python tools/check_conventions.py --base upstream/main --head HEAD: all checks passed with 0 findings.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 XairRrR on 2026-11-03T23:57:53Z.
Review: DISMISSED
Reviewed — changes requested
The new
ElfReaderchecks break two existing relinker tests.guest_load_rangesfails because a program-header alignment error now has a different message.optional_relafails because aDT_RELSZerror now has a different message. Both tests pass again when the production files are restored frommain.What I ran
Head
c2d64c45a5b12aab76be11c85e23d162f603358e,main1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged intomainwith--no-ff(merged treea0c8808eea49c475f5f8d1be621388c795198863), Release build with GCC 14.2.0 and Ninja, Debian 13.Results
git diff --checkcheck_conventions.pyguest_load_ranges,optional_rela. 1 known failure onmain:unwrap_self_..._groupelf_reader_boundsdynamic_table_bounds,rela_bounds,reloc_table_bounds,jmprel_table_boundsguest_load_rangesmain's production filesoptional_relamain's production filesguest_load_ranges
optional_rela
The failure baseline came from an older
main(1e658521a0). The negative control removes that doubt: with onlyElfReader.cppandElfConstants.hpprestored frommain(1c8c0efc8c), both tests pass. The failures therefore come from this PR.Negative control
I restored
core/relinker/relinker/src/parsing/ElfReader.cppandcore/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hppfrommainand rebuilt.elf_reader_boundsfails in these cases:SHT_DYNSYM,SHT_RELAandSHT_RELentry sizesh_link section header index out of boundsstill passes without the PR. The fixture setssh_name = 21, which is beyond the 17-byte.shstrtab, somainalready rejects that section for its name. The test never reaches the newsh_linkcheck.main: header size, SELF magic, ELF magic, class, encoding, version, machine, object version and phentsize. They test behaviour that already exists, which is fine for an audit, but they are not evidence for the code this PR adds.Afterwards I restored the merged files and rebuilt.
git status --porcelainis empty, andelf_reader_boundspasses again.Findings
Blocking
guest_load_rangesregresses (core/relinker/relinker/src/parsing/ElfReader.cpp:147-150).ReadProgramHeadersnow throwsProgram header alignment is not a power of twofor every program header, including emptyPT_LOADsegments.GuestImageReader(core/relinker/relinker/src/guest/GuestImageReader.cpp:51) already rejects these segments withInvalid load segment alignment, andcore/relinker/relinker/tests/GuestLoadRangesTests.cpp:60-64expects that message.GuestLoadRangesTests.cpp, explaining in the PR why the error moves toElfReader.optional_relaregresses, andDT_RELhandling contradicts the pipeline (ElfReader.cpp:322-336).DT_REL:RelinkerPipeline.cpp:249-250,RelativeRelocationIndex.cpp:214-215,GuestImageReader.cpp:253andStrictNidFilter.cpp:45all throwDT_REL relocations are not supported, andtest_optional_rela.py:126-127checks that message.DT_RELSZandDT_RELENTsize checks run earlier inReadDynamicTags, so aDT_RELSZof 24 now fails withInvalid DT_RELSZ valueinstead.DT_PLTRELcheck accepts17(DT_REL), while the pipeline accepts onlyDT_RELA (7)(RelinkerPipeline.cpp:242,RelativeRelocationIndex.cpp:309).DT_REL,DT_RELSZandDT_RELENTchecks and the17exception. Validation ofDT_RELA,DT_RELASZandDT_RELAENTshould also stay in one place: the pipeline already reportsUnsupported DT_RELAENTandInvalid DT_RELASZ(test_optional_rela.py:119-120).sh_linktest does not cover the new check (core/relinker/relinker/tests/ElfReaderBoundsTests.cpp:470-476).main, becausesh_name = 21is already out of bounds.sh_nameand change onlysh_link. Also assert the messageSection link index out of bounds, as the other newReject*cases do.DYNSYM/RELA/RELentry sizes) and seven dynamic-tag checks, all of which change behaviour.ElfReaderBoundsTests.cpp. The two other relinker tests this change breaks were evidently not run.Non-blocking
ElfReader.cpp:322,:334: the literal16for REL entries, and the literals24,7and17, duplicate constants that exist or could exist next tokRelaEntrySizeandkSymEntrySize. This becomes moot if theDT_RELchecks are dropped (blocking finding 2).ElfReader.cpp:233: the offset0x28is written as a literal, while the neighbouring checks use named offsets (kShdrAlignOffset,kShdrEntSizeOffset).docs/dev/debt/relinker.mdlists "dynamic entry size and alignment boundaries" as unmeasured on the console. If theDT_RELchecks go, cut the entry to what remains.Not verified
p_alignorsh_addralignvalues, or section links ≥e_shnum, which the new checks would now reject.To get this to a good state
guest_load_rangesandoptional_relapass. Either remove the checks that duplicate validation done later in the pipeline (DT_REL*,DT_PLTREL,DT_RELA*, andPT_LOADalignment), or move the expectations deliberately and explain why in the PR.sh_linktest so that it fails without the new check.