fix(elfpatcher): reject an entry-stub rel32 distance that only fits after a 64-bit wrap #12554
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12554"
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 dkdhanda8 on 2026-10-28T19:26:01Z.
What
BuildEntryStub(the stub every target's patcher calls before the guest's real entry point) computed its rel32 call displacement asstatic_cast<std::int64_t>(realEntryVaddr - callNextVaddr)— an unsigned difference cast to signed — and range-checked the result. Two addresses more than 2^63 apart wrap into a small in-range value (entry = 0xFFFFFFFFFFFFF000,callNext = 0x1015wraps to-0x2015), so the rel32 check passed and the stub called an address ~2^64 away from the entry point instead of failing. This is the same correctness gap #12307 fixed for the Linux patcher's five sites; the entry stub is the shared builder both that PR's body and its review thread point at, and it sits inelfpatcher/general, so a crafted ELF reaches it through every output target.ComputeExtraBlockVaddranchors the extra block at the page end of the highest kept PT_LOAD, which comes from the input program headers, ande_entryis guest-controlled, so the pair is attacker-reachable. With the change the builder throwsEntry stub call exceeds rel32 rangefor such inputs; without it, main writes a wrapped rel32 and produces a broken image with no diagnostic.Changed:
core/relinker/elfpatcher/src/general/EntryStubBuilder.cpptakes the sharedSignedDistance(to, from, out)helper (elfpatcher/general/AddressDistance.hpp, identical to the one in #12307): it classifies both addresses by the 2^63 boundary — within one half the modular difference equals the mathematical one — and for opposite halves takes the high-minus-low magnitude as a plain subtraction, rejecting a magnitudeint64cannot hold. Regression casestubRejectsAnEntryBeyondTheWrapBoundaryinEntryStubTests.cpp: stub at 0x1000, entry at0xFFFFFFFFFFFFF000; onmainthe builder emits a wrapped rel32, with the change it throws with the rel32 message.Not changed:
StubBodyBuilder::Finish's constant fixups subtract two offsets that are both bounded by the stub body itself, so no pair can wrap; the macOS and Windows patchers' own displacement math runs in 32-bit RVA space; the Linux patcher's five sites are #12307.Fixes: none (new finding, same malformed-input method as #10653 and #12307).
Branch:
fix/entry-stub-rel32-wrap, based onmain319621ba2.Depends on: #12307 (shares the identical
AddressDistance.hpp; if #12307 lands first the files merge cleanly, otherwise both add the same bytes).Tested
entry_stub_tests: onmainthe new case fails with "an entry point more than 2^63 from the stub was accepted with a wrapped displacement"; with the change the whole suite passes (Windows 11, MinGW-w64 GCC 15.2.0, Ninja, Release).linux_load_pages_tests(links the changed builder) passes with the change.tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repository (attach them to this PR)AI-assisted (ZCode): the finding, the fix and the verification were prepared with AI assistance; every line was reviewed and all runs were executed locally.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T19:34:32Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The exact head
62e201bd8fb8d744ca292ab97dc557b7f0976bd8has a reproducible signed-overflow case inSignedDistance, and its new helper conflicts with the same helper added by declared dependency #12307.What I ran
62e201bd8fb8d744ca292ab97dc557b7f0976bd8against base319621ba2e578bd053b0baba2877fe70520e0737; merge preview is clean (3eeac2ff11f66eab8dcc70d9fe77ea4ea5d21d22).git diff --check refs/review/current-main...refs/review/12554— passed with no output.gh pr checks 12554: Ubuntu relinker, relinker ASan, conventions, eligibility, intake and labels pass; Windows and macOS relinker jobs are pending; product jobs are skipped.Results
INT64_MINboundaryAddressDistance.hppRaw UBSan and merge output
Negative control
SignedDistance(0, 1ULL << 63, out)must not invoke signed overflow-fsanitize=undefined -fno-sanitize-recover=undefinedFindings
Blocking
INT64_MINboundary. Whenmagnitude == 2^63andhigh != to, the helper converts2^63toint64_tand negates it. UBSan confirms negatingINT64_MINis undefined. Return the representableINT64_MINexplicitly for this negative boundary, or reject this magnitude; add a regression assertion under UBSan.AddressDistance.hpppath, but the PR versions differ: #12307 rejectsmagnitude >= 2^63, while this head accepts the negative-equal case. The combined merge preview reports an add/add conflict, contrary to the description that the files merge cleanly or add identical bytes. Please restack on #12307 and reuse/reconcile the helper, then rerun the focused test and overlap check.Non-blocking
No other source issue identified in the small call-site change.
Sources
d9ab85865a09be22241ae6631e0d1fa8e800b273Not verified
I did not run the full relinker suite, Windows/macOS builds, or a console/title run. Those hosted jobs are pending or skipped as listed.
To get this to a good state
Fix or reject the negative
2^63magnitude without signed overflow, add an exact boundary regression, and reconcile this branch with #12307 so the shared helper has one agreed implementation. I will rerun the sanitizer reproducer, focused tests, overlap check and merge preview on the updated head.