fix(relinker): overflow-safe RELA entry bounds check #917
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/917"
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 TidalImpact on 2026-10-06T16:33:22Z.
What
Make the RELA relocation entry bounds check overflow-safe.
The previous check used
pos + relaEntSize, which could overflow before the bounds check. The new check validates the offset and remaining buffer size separately.Tested
Windows, x64 Release build.
relinker.exebuilds successfully and starts.ctestrun attempted; many tests were reported asNot Runin this Windows environment.Checklist
main; no other open PR implements the same functions.mdfiles or images addedImported from boykopovar/AnyPS5. Originally posted by TidalImpact on 2026-10-07T05:27:14Z.
Hi, I’m not sure I fully understand the mention of the old PR.
As I understand it, #917 already fixes the overflow-safe RELA entry bounds check that was part of #298, but #917 is still open, while #930 has already been merged and handles a different part of the issue.
I just wanted to ask whether there is still anything you would like me to change or add to #917, or if I should just wait for further instructions.
I just want to make sure I’m following the intended approach.
Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-07T11:03:53Z.
I noticed a related bounds issue nearby that #917 doesn’t seem to cover.
dynSymTabOffsetcan come directly fromDT_OS_SYMTAB, and the latersymOff + 4 > raw.size()check can overflow for an offset nearUINT64_MAX.DT_OS_SYMTABSZalso appears to be defined but unused for bounding symbol indices.extractRelativenearby also has no independent bounds check and currently seems to rely onextractRelarunning first over the same ranges.I’d be happy to send a separate PR with focused malformed-ELF regression tests, depending on #917, unless you’d prefer this folded into the same work.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T20:22:05Z.
Review: CHANGES_REQUESTED
Thanks, the overflow-safe check in
extractRelalooks right to me: with a hugerelaOff,pos = relaOff + offwrapped and passed the old check. Two things before this can merge:RelinkerPipeline.cpp:189-191). The PR needs a test that fails onmainand passes here, e.g. a synthetic ELF whoseDT_RELA/DT_OS_RELAoffset is nearUINT64_MAX(and one with size not fitting in the file), expecting "Relocation entry out of bounds". Relinker tests live next to the code (seecore/relinker/relinker/tests/, and the ELF fixture helpers there); tests register explicitly incore/relinker/CMakeLists.txt.extractRelativehas the same pattern (RelinkerPipeline.cpp:294-295):relaOff + offwith no bounds check of its own, relying onextractRelahaving run first over the same ranges. Either give it the same check (preferably through one shared helper instead of two copies of the expression) or say why it can't be reached with different ranges.Also note #1194 touches the same function for a different defect (partial trailing entries); no conflict expected, but whichever lands second will need a rebase.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T20:29:16Z.
@muhzain05's finding is real, and it is one instance of a pattern in this file. Mapping it here in case it saves whoever fixes it the sweep — I have not started a branch, and I am happy to stay off it.
The dynamic string table is bounds-checked (
RelinkerPipeline.cpp:160). The other three offsets read from the same dynamic section are not:dynStrTabOffset/dynStrTabSizeDT_STRTAB/DT_STRSZdynSymTabOffsetDT_OS_SYMTAB/DT_SYMTAB, line 124dynRelaOffset/dynRelaSizeDT_RELA/DT_RELASZ, lines 143-144dynJmpRelOffset/dynJmpRelSizeDT_JMPREL, line 137DT_PLTRELSZis only checked to be a multiple of 24Three read sites then rely on a per-entry check that can wrap, or on nothing:
1. The symbol table, line 210.
symOff + 4 > raw.size()wraps, while thememcpyusessymOffitself:2.
extractRelative, line 293.pos = relaOff + off, thenmemcpy(raw.data() + pos, 8)with no bounds check at all.extractRelaat least had one — the one this PR repairs — while its sibling never did, andrelaOffis unchecked.3. The shape of the fix. This PR's check is right for its own site, but bounding these offsets once, where they are read, covers all three and duplicates the idiom the string table already uses at line 160 (
offset > size || size > size - offset). That would leave per-entry arithmetic with nothing to defend.What I did not do: I have not built a crafted ELF to drive any of this end to end. The evidence above is the code path and the arithmetic, not a run — saying so rather than implying a reproducer I do not have.
@muhzain05 are you taking this? If you are I will stay out of it. If not, I will pick it up and bound the offsets at the read rather than at each use.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T21:19:06Z.
Update, so nobody duplicates it: I picked this up as #1272. It bounds the read rather than bounding the offsets up front, deliberately — an unused-but-bogus table must not start failing titles that work today.
Two things it does not touch, so they are still open if either of you wants one:
extractRela(line 191) — this PR (#917) already repairs it.DT_RELA/DT_RELASZorDT_JMPRELthemselves. #1272 makes each read safe; a table that lies outside the file but is never read is still accepted, and when the per-entry arithmetic does not wrap, nothing names the table as the problem.If you want either, say so and I will stay out of it.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T21:41:47Z.
@adriadam10 asked for a test that fails on
mainand passes here. I verified it, built it, and it is below ready to paste — plus one thing that will block it.First, the blocker: this branch predates the fixture.
core/relinker/relinker/tests/ElfFixture.hppexists onmain(added after your merge-base5895ea721a43) but not onfix/rela-bounds-check:So the advice to "see the ELF fixture helpers there" only lands after a rebase, and the test cannot compile as the branch stands. I applied your diff to current
main(it applies cleanly) and worked from there.The test, both cases you listed. Two files:
core/relinker/relinker/tests/RelaBoundsTests.cpp(new, verbatim below) and five lines incore/relinker/CMakeLists.txt, in the sameif(UNIX AND ... x86_64)block asto_intel_linux_stubs_tests:Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T02:49:01Z.
Thanks, I checked the current open PRs and I don’t see anyone taking the
DT_JMPRELbounds side, so I’ll take that one.I’ll keep it separate from #1272’s per-read bounds work and focus specifically on validating the
DT_JMPREL/DT_PLTRELSZtable range with a malformed-ELF regression that fails on currentmain.I’ll leave
extractRela,extractRelative, and the symbol-table read work to #917/#1272.Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T04:23:59Z.
@Dextroid17 Took the DT_JMPREL / DT_PLTRELSZ bounds side as discussed, PR is up now as #1424.
I kept it separate from #917/#1272 and focused only on the table-range validation path.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T05:58:32Z.
Review: APPROVED
Thanks. The overflow-safe check in
extractRela/extractRelativeis correct: with a hugerelaOff,relaOff + offused to wrap and pass the old check.What I added on your branch (fix-merge):
main(the test needsElfFixture.hpp).RelaBoundsTests.cpp+ CMake entry, from @Dextroid17's test in this thread (co-authored): aDT_OS_RELAoffset nearUINT64_MAX, and a table that doesn't fit in the file. The first case fails onmainwithout the fix and passes with it; the second passes on both (coverage only).conventionsjob was failing on them).Full build + ctest green locally (416/416). Merging once CI is green. #1272 will need a normal rebase afterwards.