fix(relinker): bound DT_RELA table range #1446
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1446"
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 sktharunbalaji13-cmd on 2026-10-08T06:52:37Z.
What
The relinker accepted an ELF whose declared relocation table did not lie inside the input file, and then wrote that offset into the output it generated.
The defect is specific to an empty relocation table. #917 added the overflow-safe per-entry bound (
relaEntryPos,RelinkerPipeline.cpp), but that check only runs inside theextractRelaloop, and the loop body never executes whenDT_RELASZis0. Nothing reads the table, so nothing rejects it, and the bad offset travels on — as @Dextroid17 measured by scanning the produced file for each(tag, value)pair:So the conversion hands a title a dynamic table pointing outside the file, with a size of zero. That is the "does exactly what it should or throws" case rather than a message preference, and it is why this is worth merging instead of cosmetics.
This is also what separates it from the table-level check @oneandonlydean asked to drop from #1272. That hunk was about the read — no read can leave the file, because
relaEntryPosalready bounds it. This is about what gets written, whichrelaEntryPoscannot reach.The fix validates the declared table range before the table is consumed, using the same shape as the existing dynamic string-table guard:
The first term short-circuits, so the subtraction cannot wrap and
offset > size || size > size - offsetdecides both an offset past EOF and an offset plus size past EOF, for the system-V path (already translated byTranslateVirtualAddress) and the SonyDT_OS_RELApath (raw). #1424 covers the separateDT_JMPREL/DT_PLTRELSZtable and is already merged; this PR covers theDT_RELA/DT_OS_RELAtable itself.Malformed inputs now fail with the table named, in both output modes:
Tests —
tests/RelocTableBoundsTests.cpp(CTestreloc_table_bounds) is C++ onElfFixture.hpp/RelinkerProcess.hpp, registered explicitly incore/relinker/CMakeLists.txtlikeRelaBoundsTests.cpp, and runs every case under--windowsand--to-intel(the fixture's spare program headers are what let the Linux path reach the code under test). It keeps valid controls (an empty in-file table, an emptyDT_OS_RELAtable, a table holding one relocation entry), both tag families, and a case whose address no segment maps.rela_bounds(#917) expectation, updated deliberately. Both of its cases are now answered by the table guard before any entry is read — case 1 has an offset nearUINT64_MAX, case 2 hasoffset + sizepast the end — soRelaBoundsTests.cpp:38now expectsRelocation table is out of bounds. Keeping the oldRelocation entry out of boundsexpectation there would mean removing the guard for those inputs, which is the defect itself. The two cases are unchanged otherwise and still require a non-zero exit, so the regression is not weakened: with the guard reverted,rela_boundsfails with "Relinker did not report the table as out of bounds for a table whose offset is near UINT64_MAX".Tested
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON): full relinker CTest suite 39/39 passed, includingreloc_table_bounds,rela_bounds,jmprel_table_boundsandstring_table_boundscmake --build build-linux --target relinker rela_bounds_tests reloc_table_bounds_testsctest --test-dir build-linux -R '^(reloc_table_bounds|rela_bounds)$'→ 2/2 passedcmake --build build --target relinker, affected relinker CTest suite 25/25 passedgit diff --checkpython tools/check_conventions.py ...→ All conventions checks passedRelinkerPipeline.cppguard reverted,reloc_table_boundsfails with "Relinker accepted an empty relocation table whose offset is past the end of the file for --windows" andrela_boundsfails as above; with the guard restored both pass--to-intelno longer stops atNot enough program header slots: the fixture carries the spare program headers, and both modes reject the malformed tables with the message abovelibs, FFmpeg and guest targets are not built there; the Linux run covers the relinker in full.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed inTechnicalDebt.mdfiles or images added to the repositoryDT_JMPREL/DT_PLTRELSZand is merged; this PR is independently scoped to theDT_RELAtable rangeImported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T12:27:33Z.
Correction to my first read, with measurements: I had this filed as covered by #917, and it is not.
Running your own script against a clean current
main(1081602d) says the same thing you do, and says something useful about which case is the one that gets through:So the non-empty case is already rejected — that is #917's per-entry bound,
relaEntryPosatRelinkerPipeline.cpp:190, and it is why those cases in your script pass onmain. The case that gets through is the empty table with an out-of-range offset: the loop body never runs, so the bound is never reached, and the conversion succeeds.And the offset does reach the output, which is what makes this worth merging rather than cosmetic. Scanning the produced file for each
(tag, value)pair:So the conversion writes an offset outside the file into the dynamic table it hands to a title, with a size of 0. That is the "does exactly what it should or throws" case rather than a message preference.
One thing worth saying in the body, because it is the difference from the change I withdrew. In #1272 I carried the same table-level check as a second hunk and @oneandonlydean told me to drop it — "
#917 is on main and now boundsextractRelativethroughrelaEntryPos, so that hunk is no longer needed". That was about the **read**, and he was right: no read can leave the file. This is about what gets **written**, which is a caserelaEntryPos` cannot reach. Naming that distinction in the description is what separates this from the hunk that was dropped.One test note. Your script only exercises
--windows. Under--to-intelthis fixture fails earlier and for an unrelated reason —FAIL: Not enough program header slots: need 5, available 3, the image carries three program headers — so the Linux path is never reached. If the OS tag matters for the Linux output too, a fixture with five program headers would cover it.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T13:19:16Z.
Two things that would let you cover the Linux half of this, since your fixture currently stops before it reaches the code under test.
The
--to-intelrun never gets there. With this image it exits withFAIL: Not enough program header slots: need 5, available 3— the image declares exactly three program headers (e_phnum = 3at offset 56) and the Linux conversion wants spare slots. The rest of the suite already solves this:core/relinker/relinker/tests/ElfFixture.hppdeclarese_phnum = 2 + kSpareProgramHeaders(five) and writes the three spare entries as type0x6FFFFF01. Adding spare slots to your fixture — declared, even if empty — is what makes the Linux path reachable, and then the same assertion runs under both modes.If you would rather keep it in Python,
test_guest_intel_trampolines.py:127is the pattern for invoking--to-intelfrom a test in this tree.On the switch itself, purely as a suggestion: with the non-empty case ignored and a bogus-but-unused offset accepted today, the check you are adding is what makes the conversion refuse to hand a title an out-of-range table offset. I measured that the offset is carried into the output in both the
DT_OS_RELAandDT_OS_JMPRELcases, which is the argument I would put in the description — it is what separates this from the same check I was told to drop from #1272, where the bound was about the read.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:51:18Z.
Review: CHANGES_REQUESTED
The guard itself looks right (same shape as the string-table check, and the empty-table-with-bad-offset case is real, as @Dextroid17 measured). Merged onto current
main(b3fcd6d6c), though, two tests fail:reloc_table_bounds(yours) fails onrange-past-eof(rela=0xFF0, relasz=0x100): it exits withInvalid DT_RELASZ value(RelinkerPipeline.cpp:148, 0x100 is not a multiple of 24) before reaching the new check, so that case does not test it. Use a size that is a multiple of 24 (e.g. 0x108 at 0xFF0).rela_bounds(#917, onmain) fails:RelaBoundsTests.cpp:38expectsRelocation entry out of boundsfor a table at an offset near UINT64_MAX, and now getsRelocation table is out of bounds(RelinkerPipeline.cpp:166). Either keep that case reaching the old message or update that expectation deliberately and say so in the PR.relinker/tests/*Tests.cpponElfFixture.hpp/RelinkerProcess.hpp, registered explicitly incore/relinker/CMakeLists.txt); please port the new one, asRelaBoundsTests.cppdoes.ElfFixture.hppalso has the spare program headers, so the same cases run under--to-intelas well as--windows(your fixture stops atNot enough program header slotsthere, as Dextroid17 noted).relaEntryPos(#917) cannot reach.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T14:06:52Z.
Review: CHANGES_REQUESTED
Merged onto current
main,reloc_table_boundsnow passes (therange-past-eofsize is fixed), but two things from the earlier review are still open:rela_bounds(#917, onmain) fails:RelaBoundsTests.cpp:38expectsRelocation entry out of boundsfor a table at an offset near UINT64_MAX, and now getsRelocation table is out of bounds(RelinkerPipeline.cpp:168). Either keep that case reaching the old message, or update that expectation deliberately and say so in the PR.relinker/tests/*Tests.cpponElfFixture.hpp/RelinkerProcess.hpp, registered explicitly incore/relinker/CMakeLists.txt), asRelaBoundsTests.cppdoes.ElfFixture.hppalso has the spare program headers, so the same cases run under--to-intelas well as--windows.relaEntryPos(#917) cannot reach (see @Dextroid17's measurements above).Checked: the new test fails without the
RelinkerPipeline.cppchange.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T14:17:32Z.
Ran your fixture in both conversion modes, against your branch and against
main, to answer the "does this need the Linux path covered too" question with numbers.First, good news: this now merges cleanly onto current
main— no conflicts, unlike most of the relinker queue this morning.With your change, every case is rejected in both modes — and it needs no spare program header slots, because your guard runs upstream of the converter:
On
main, the same fixtures: the non-empty case is rejected by #917's per-entry bound in both modes, and the empty-table cases go through —--windowswrites an output, and--to-inteldoes too once the image declares spare program header slots, with the same garbage inside:So this is not a Windows-only case: the Linux path keeps the bogus offset too, once it gets far enough to run. That is the strongest version of the argument for the description, and it is worth a
--to-intelcase in the script so the claim is covered by the Linux job as well as the Windows one — with your guard everything rejects before the converter, so both modes assert the same message with no output.One caveat on my method, so you do not chase it: the spare slots I declared are
PT_LOOS+1with zero size, exactly asElfFixture.hppwrites its spares. Those make the Windows path fail earlier withFAIL: Code analysis: invalid SCE tableonmain, which is an artifact of my synthetic headers rather than of your change — ignore the with-spares Windows rows. The Linux rows are the meaningful ones.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T14:44:10Z.
Two things from @adriadam10's review are still open, and both have short, concrete answers — here is the pattern, so the port is mechanical rather than a rewrite.
1. The C++ port.
RelaBoundsTests.cpp(onmain, from #917) is the whole template: same fixture, same helpers, same assertions, one file. What you need:The includes give you everything:
ElfFixture.hpp(Bytes,MakeExecutable,Write),RelinkerProcess.hpp(TempDirectory,WriteFile,RunRelinkerreturning{ExitCode, Output}). The shape that already works:Two things that make this cheaper than the Python version:
RelaBoundsTests.cppwrites the OS RELA pair at slots 4 and 5 of the C++ fixture's dynamic array; your Python fixture builds its own tag list, so the indices are not portable. Copy the C++ constants, not the Python list.MakeExecutablealready declares2 + kSpareProgramHeadersprogram headers (ElfFixture.hpp:20,60,65), so the same image reaches the converter under--to-intelas well as--windows— loop over the two modes and assert the same message and a non-zero exit in each, which is what your new Python case was reaching for. Asserting!std::filesystem::exists(output)after the run is also possible here, since the test names the output path itself.Also fold in @adriadam10's size fix: keep
range-past-eofat a multiple of 24 (0x108at0xFF0), otherwise it exits withInvalid DT_RELASZ valuebefore reaching your check.2. The
rela_boundsexpectation — I think it should be updated, deliberately, and named in the body.RelaBoundsTests.cpphas two cases and both go through one helper that asserts"Relocation entry out of bounds":So the helper's expectation is what fails, and both inputs — not just the one the review names — now reach
Relocation table is out of boundsfirst. That is the right message for a table-shaped input, and I could not construct an input that still reaches the entry-level path once the table is validated: a table that passes the size check has every whole entry inside the file by construction. So the honest change is to update that one expectation to the table-level message and say in the description that you are deliberately changing an expectation in #917's test, with the reason. If @adriadam10 would rather keep an entry-level case alive, the person to ask is him — he offered both routes.I checked the same on the JMPREL side: #1424 landed the matching table check today, so after both, neither relocation reader relies on the per-entry bound for malformed tables.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T04:41:08Z.
I checked the current CI failure on head
4da534fed782d12444f4c3f016e2b333dde15438. The Linux job stops before the build intools/check_test_registration.pywith:This looks like
maindrift rather than a relocation-test failure:guest_formatting_testsis now registered with CTest, but remains in the checker’s staleKNOWN_UNRUNlist on this branch. Rebasing onto currentmainand removing that exception should let Linux CI proceed. The current Windows job, all three relinker jobs, Conventions and Progress pass.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T11:23:04Z.
Verified from here, and your diagnosis is right. On
upstream/mainthere is no longer aguest_formatting_testsline intools/check_test_registration.py, and the test is registered (core/libs/prx/libc/CMakeLists.txt:245). So theyou are seeing is the drift you describe — the stale exception was dropped upstream after your base — and a rebase plus deleting that local entry is the whole fix. Nothing about the DT_RELA change is implicated.
Worth noting the checker is earning its keep here: it caught a stale exception rather than a missing registration, which is the same rule from the other side.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:29:09Z.
Review: APPROVED
Checked on a Linux build merged onto current
main: relinker ctest scope 42/42 passes. Reverting only theRelinkerPipeline.cppguard makes bothreloc_table_boundsandrela_boundsfail; with it they pass. Therela_boundsexpectation change is correct (the table guard now answers both cases before any entry is read).The branch conflicted with
mainonly incore/relinker/CMakeLists.txt(next todynamic_table_bounds); I rebased it keeping both entries. Merging once CI is green.