fix(relinker): bound the entry point collector's header and symbol reads #2196
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2196"
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 reyani-dev on 2026-10-09T11:28:24Z.
What
Refs #1666, which asks to review the adjacent table reads for the same arithmetic pattern; this is one such finding.
The CFG/GOT filter (
unused-filter=1, which runs on non-PLT references) collects entry points inUnusedNidFilter/EntryPointCollector.cpp. It checked every position asoffset + size > file sizeor<= file size: the program headers,PT_DYNAMIC, the init/fini arrays, the section headers, the section name table, thesh_linkheader and the symbol entries. With an offset near 2^64 the sum wraps, the check passes, and the uncheckedread*helpersmemcpyfrom outside the input. The relinker validates none of these section header fields before this point.Measured on
main(d70b8998), with the read helpers temporarily instrumented to report out-of-range reads:e_shoff = 2^64 - 32with two section headers reads 8 bytes at offset -8 and 4 bytes at -28, both before the input buffer, and the relink still exits 0.e_shoff = 2^64 - 64ande_shentsize = 0x840, section 1 is read from file offset 0x800. ASHT_SYMTABthere adds a function that nothing else reaches to the entry points, so theGLOB_DATreference it loads is kept (CFG/GOT filtering: 1 -> 1). The same happens with an in-bounds section header whose symbol table offset wraps (sh_offset = 2^64 - 24).The fix checks each position by subtraction (
base <= size && offset <= size - base && length <= size - base - offset), the same formRelinkerPipelineuses for RELA entries since #917. Out-of-range tables keep their existing handling: program headers andPT_DYNAMICstill throw, and the best-effort section and array scans still stop.RelativeRelocationIndex.cpphas the same pattern (pos + RelaEntSize,phPos + 56,segOff + segSz), but it is not reachable: by the time it runs, the pipeline has already rejected a wrappinge_phoff(FileByteOffset out of bounds),PT_DYNAMIC(Dynamic segment out of bounds) and RELA table. I checked by instrumenting its read helpers the same way. So it is left unchanged.Tested
Windows 11, WinLibs GCC 15.2.0 r7, relinker-only Release build,
-DBUILD_TESTING=ON:New
entry_point_boundstest (Python, relinker fixture,--windows unused-filter=1), with aGLOB_DATreference loaded only by a function that only a symbol makes reachable:main1 -> 01 -> 01 -> 11 -> 11 -> 11 -> 01 -> 11 -> 0cteston the relinker-only build: 41/41 passed.tools/check_conventions.pyandtools/check_test_registration.pypass, andtools/pr_overlap.py --branchreports no overlap.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T11:56:07Z.
Full Linux and Windows builds and tests for this head (
fcba0b0d), using the project's Build workflow. I can't start the upstream manual run, so I ran the samebuild.ymlin my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into currentmain(d70b8998):ctest: 517/517 passedctest: 508/508 passedRun: https://github.com/AchrafReyani/AnyPS5/actions/runs/37923946113
Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T12:34:52Z.
To clarify the note about
RelativeRelocationIndex.cppin the description, given #2221 and #2222: I only checked the wrapping, out-of-file case (offsets near 2^64), which the pipeline rejects before that code runs. #2221 is a different case: a relocation table that stays inside the file but runs past itsPT_LOADsegment, and #2222 fixes it. The two PRs touch different files and merge cleanly (git merge-tree).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:18:01Z.
Please rebase:
core/relinker/CMakeLists.txtconflicts with main now, so the build can't merge it. #2330 changes the same lambda inEntryPointCollector.cpp; whichever of the two lands second will need another rebase.Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T05:25:03Z.
Rebased onto current
main(b1599a94). The only conflict was incore/relinker/CMakeLists.txt, wheremainadded thewindows_diagnostic_fixture_isolationtest next to this PR'sentry_point_bounds; both are kept.Relinker-only build on Windows:
ctestpasses 56/56, includingentry_point_bounds, andcheck_conventions.pypasses. The full Linux and Windows build for this head is running and will be posted here. Noted on #2330: I'll rebase again if it lands first.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T05:41:47Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/relinker/relinker/tests/test_entry_point_bounds.pyImported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T06:03:20Z.
Full Linux and Windows builds and tests for this head (
b1599a94), using the project's Build workflow. I can't start the upstream manual run, so I ran the samebuild.ymlin my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into currentmain(bc40a6b5):ctest: 599/599 passedctest: 607/607 passedRun: https://github.com/AchrafReyani/AnyPS5/actions/runs/38027423754