fix(relinker): reject overflowing ELF file offsets #7037
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7037"
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 namanipie on 2026-10-17T07:01:56Z.
What
Hardens
CodeInstructionCollectorandEntryPointCollectoragainst 64-bit integer overflow when translating virtual addresses to ELF file offsets. Previously, aPT_LOADsegment with an offset nearUINT64_MAXcould wrap around to a small offset within file bounds and bypass range checks. Adds a discriminating regression test inrelinker_conversion_testscoveringCodeInstructionCollector; theEntryPointCollectorcheck is defense in depth.Tested
Windows 11 x86_64, MinGW-w64 GCC 15.2.0:
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ONrelinker_conversion_testspassed: verified discriminating red/green behavior onCodeInstructionCollectorwrapping test (mainfails with "Invalid conversion was accepted", passes on this branch) acrossDT_INIT_ARRAY,DT_FINI_ARRAY, andDT_PREINIT_ARRAY, with controlsctest --test-dir build-relinker -R "^relinker_conversion$" --repeat until-fail:20passed 20/20ctest --test-dir build-relinkerpassed 181 / 181 testspython tools/check_conventions.py --base origin/mainpassedChecklist
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 fogydes on 2026-10-17T07:14:37Z.
Review: COMMENTED
Windows run and verification of head
bcf46ec0daonmain6a32550fd:Windows 11 x86-64, WinLibs GCC 15.2.0, Ninja Release,
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON.relinker_conversion_testsbuilt cleanly with zero compiler warnings.ctest --test-dir build-relinker -R "^relinker_conversion$" --repeat until-fail:20: passed 20/20.python tools/check_conventions.py --base origin/main: passed with 0 findings.python tools/pr_overlap.py: 0 confirmed collisions across all open PRs.Code Review: The upper-bound checks (
relative > std::numeric_limits<std::uint64_t>::max() - header.Offset) inCodeInstructionCollector.cppandEntryPointCollector.cppsafely guard against 64-bit integer wrapping before computing file offsets, preventing out-of-bounds segments from mapping to wrapped in-bounds offsets.AI-assisted: yes. I ran the builds, tests, and checks above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T07:35:32Z.
Thanks, this is the direct-call guard I asked about on #6392. Since #5741 already rejects that input in ElfReader::ReadProgramHeaders, could you change 'fixes #6392' in the description to say it hardens the two collectors, and add a test for the EntryPointCollector hunk? Happy to merge after that.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T10:12:38Z.
Thanks. The guard itself looks right. Two things before it can merge:
ConversionTests.cpp:177-181the case usesOffset = 2^64-17withMappedAddress = 0. The first lookup hasrelative = 0, so nothing wraps, and main's existingrange(offset, size)already rejects the huge offset. Please chooseOffsetand the looked-up address so thatOffset + relativewraps to a small offset inside the image. Then main'srange()accepts it and only your new check rejects it, so the test fails on main and passes here. Please add the same kind of case for the init/fini array path inEntryPointCollector, which has no test yet.Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-17T10:22:52Z.
Tested the requested direct-call paths on exact head
bcf46ec0da38080d3a030aeb6e79b8e5e81c0801(Ubuntu 24.04 x86-64, GCC 13.3, Release relinker-only). There is a guard-order constraint on theEntryPointCollectortest request.A discriminating
CodeInstructionCollectorfixture works. I used a real executable segment at file offset0x200, VA0x2000, with returns at0x2000and0x2008; a flags-zero dataPT_LOADwith offsetUINT64_MAX-63, VA0x8000, file/memory size0x410; and an eight-byte init/fini/preinit array at VA0x8400. Its file offset wraps to0x3c0.For all three array types:
0x3c0yields{0x2000}; replacing only that word with0x2008yields{0x2000,0x2008}. The wrapped read changes the collected roots.Code analysis: file range exceeds image.0x300, array word at0x700): both implementations accept, with exactly the expected root sets.That supplies six red/green overflow cases plus six valid controls. I also reproduced the current
relinker_conversion_testspassing with both collector objects reverted to the parent and the PR's new test retained.EntryPointCollector::Collectalready callsElfReader(elfBytes).ReadProgramHeaders()itself. Even a direct call rejects all six wrapping cases on both parent and fix withSegment offset out of bounds, beforecollectArray. This follows from the production checks: the reader boundsfileOffset + fileSizeagainst the image using subtraction, andcollectArrayboundsrelative + arraySzagainstfileSize. With those checks intact,fileOffset + relativecannot overflow. A rejection test through this public API therefore covers the existing reader guard, not the added overflow branch. I did not bypass/mock that reader to claim new-hunk coverage.The parent control rebuilds only the two changed collector objects; the remaining production archive and test object are identical. No CLI/title exposure or current-main merge result is claimed.
Standalone direct-call probe used for these results
The probe links against the normal production
librelinker_core.a; its relocation-index input is empty. It prints the exact root sets and rejection messages above.Imported from boykopovar/AnyPS5. Originally posted by namanipie on 2026-10-17T11:03:40Z.
Updated head to
abfe81309bc42e72b705c1a655a5f2b3764f3e88:Closes #6392from the description and updated the summary to state that this hardens both collectors.ConversionTests.cpp, replaced the previous non-discriminating test with a fixture using a real executable segment (Flags = 1) at file offset0x200, VA0x2000, a dynamic segment, and a non-executable dataPT_LOAD(Flags = 0) at offsetUINT64_MAX - 63, VA0x8000, file size0x410. The initializer array at VA0x8400wraps to file offset0x3c0.main(parent), the wrapped read is accepted and modifies the collected roots (or returns without throwing), causing the test assertion to fail withInvalid conversion was accepted.fileOffsetcatches the integer overflow and throwsCode analysis: file range exceeds image, passing all 6 wrapping rejection cases and 6 non-wrapping controls acrossDT_INIT_ARRAY,DT_FINI_ARRAY, andDT_PREINIT_ARRAY.EntryPointCollector::Collectdirectly callsElfReader(elfBytes).ReadProgramHeaders(), which checks_rangeFitsagainst the image and throwsSegment offset out of boundsbeforecollectArraycan be reached. The guard inEntryPointCollector.cppremains in place as an internal direct-call defense-in-depth hardening.Ran checks and verification on Windows 11 x86-64, MinGW-w64 GCC 15.2.0, Ninja Release:
relinker_conversion_testsbuilt cleanly with zero compiler warnings.ctest --test-dir build-relinker -R "^relinker_conversion$" --repeat until-fail:20: passed 20/20.ctest --test-dir build-relinker: passed 181/181 (100%).python tools/check_conventions.py --base origin/main: passed with 0 findings.Imported from boykopovar/AnyPS5. Originally posted by Baconzido on 2026-10-18T07:50:55Z.
Red/green on macOS for the current head, since the validations above are on
bcf46ec0dand the new test's red run is only the author's.Apple M5 Pro (arm64), macOS 27.0.1, AppleClang 21.0.0, CMake 4.4.4, Release relinker-only (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON). Headabfe81309merged ontomain1c523bdb4(clean merge).relinker_conversionpasses 20/20 with--repeat until-fail:20. No compiler warnings in the rebuilt objects.main,ConversionTests.cppkept:relinker_conversionfails withInvalid conversion was accepted.CodeInstructionCollector.cppreverted: fails the same way. The new test covers this hunk.EntryPointCollector.cppreverted:relinker_conversionpasses. This matches @simonbbby's note:EntryPointCollector::CollectrunsReadProgramHeaders()first, so that hunk cannot be reached through the public API and the test does not cover it.Not checked: Linux, x86-64 under Rosetta.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T15:04:15Z.
Windows run of the current head, since the earlier Windows result is for
bcf46ec0d. Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, headabfe81309scratch-merged onto maina62c724c3(clean; #7098 merges alongside it cleanly). Relinker, its tests andlibsbuilt; no full-tree build, no GPU involved.relinker_conversion,--repeat until-fail:3: 3 of 3.relinker_conversion_testsfails withInvalid conversion was accepted; onlyCodeInstructionCollector.cppfrom main: same failure; onlyEntryPointCollector.cppfrom main: passes. This agrees with the macOS result above: the test covers theCodeInstructionCollectorhunk, theEntryPointCollectorhunk is not reached becauseCollectrunsReadProgramHeaders()first.PT_LOADhasp_offset2^64-64 or 2^64-256, main and this PR both stop withSegment offset out of boundson the Linux, Windows and macOS targets, so the new checks are only reachable through directCollectcalls, as the test does.eboot.elf) with main's relinker and with this PR:--windowsgives identical SHA-256 for all 312 output files (PPSA99995 rc 2 in both, sameUnsupported Windows guest TLS instructionfailure); the Linux target gives identical SHA-256 for all 122 files.tools/check_conventions.py --base origin/mainandgit diff --check: pass.Small suggestion: the PR body still says the regression test covers both collectors; it covers
CodeInstructionCollectoronly, so the description could say theEntryPointCollectorchange is defence in depth.Not checked: Linux and macOS hosts, running a relinked executable, full-tree build, the non-GPU suite.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).