fix(relinker): bound the jump relocation table range #1424
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1424"
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 muhzain05 on 2026-10-08T04:19:20Z.
What
DT_JMPREL/DT_PLTRELSZwere not range-checked against the input file.With the Sony
DT_OS_JMPRELvariant the offset is returned raw, withoutTranslateVirtualAddressand without a bounds check, andDT_OS_PLTRELSZis read the same way. The declared range was then used without being compared to the image:DT_PLTRELSZis zero, theextractRelaandextractRelativeloop conditions (relaSize >= relaEntSize) never execute, so no per-entry check ever sees the offset. Onmainan ELF withDT_OS_JMPREL = 0xFFFFFFFFFFFFFFFFandDT_OS_PLTRELSZ = 0exits 0 and writes an output executable.DT_OS_tag pair withDT_PLTRELSZzero, conversion succeeds and the out-of-range offset is carried into the generated output: scanning the produced file showsDT_OS_JMPREL 0xffffffffffffffffwithDT_OS_PLTRELSZ 0(and0x1001for the past-EOF case). The SysVDT_JMPREL/DT_PLTRELSZpair is not emitted for these inputs. The offset also reachesSysVDynamicSectionBuilder::BuildDynamicSectionasoriginalJmprelOffset, andUnusedNidFilter::CompactPltwith--unused-filter=2.This is an output-correctness problem, not a diagnostic preference: the relinker writes a jump-relocation table location that lies outside the input image into the executable it produces, instead of throwing.
Validate the declared range with the same overflow-safe subtraction-style check already used for
DT_STRTAB/DT_STRSZ, placed next to it and before the table is read. The string table range is already validated unconditionally, whether or not a string is read, andstring_table_boundsasserts an out-of-range table is rejected; this follows that existing behaviour for the jump relocation table. The range and termination requirements for the dynamic array follow the ELF dynamic section specification. The guard is conditional onhasPltRelocationsbecausedynJmpRelOffsetanddynJmpRelSizeare only read insideif (hasPltRelocations)and otherwise stay zero: an executable with no PLT relocation table declares no range to validate, andtest_optional_pltcovers that case.Coverage, stated precisely. The new test has 10 cases. Three are a behaviour change, where
mainaccepts the input and writes an executable and this change rejects it:mainoffset-max-empty-table(DT_OS_JMPREL=UINT64_MAX, size 0)offset-wrap-empty-tableoffset-past-eof-empty-tableThe malformed cases with a non-zero size (
offset-max,offset-wrap,size-past-eof,size-max) already fail onmainwithRelocation entry out of boundsfrom the per-entry check added in #917. This change only makes the diagnostic name the jump relocation table and fail before the table is read. That is not new rejection behaviour and is not claimed as one.os-valid,sysv-validandempty-tableare controls that must keep converting successfully. Both tag forms are covered because only theDT_OS_JMPRELvariant bypassesTranslateVirtualAddress; the SysVDT_JMPRELpath is already rejected by the address translation.Related work. This covers the
DT_JMPREL/DT_PLTRELSZtable-range validation at the dynamic-table level only. It does not touchextractRela,extractRelativeor the symbol-table read:extractRelativethroughrelaEntryPos, so both per-entry readers are bounded onmain. Not touched.DT_RELA/DT_RELASZ. No shared identifiers and no shared tags; it guards the relocation table where this guards the jump relocation table. The two guards sit on adjacent lines, so whichever merges second needs a rebase.The layers are distinct: #917 is per-entry relocation read safety, #1272 is symbol-table entry read safety, and this is table-level
DT_JMPREL/DT_PLTRELSZrange validation.#917 and #1272 protect reads: no relocation or symbol access may leave the input image. This protects a different invariant - an invalid declared jump-relocation table must not be accepted and propagated into the output just because its size is zero. With
DT_PLTRELSZzero,extractRelaandextractRelativenever enter their loop, sorelaEntryPosis never reached and no read occurs to be bounded. The table-level check and the per-entry checks are therefore complementary. A similar table-level hunk was dropped from #1272 once #917 made the reads safe; #1424 remains necessary because the zero-size OS JMPREL value is still carried into the generated output even though no out-of-bounds read occurs.Tested
Windows 11, MinGW-w64 GCC 16.2.0 (
x86_64-ucrt-posix-seh, WinLibs), CMake 4.4.4, Ninja 1.13.2, Release withBUILD_TESTING=ON. No title, firmware or console execution. Linux is unverified.b1c5f10d. The new test was run unmodified against a pristineb1c5f10dbuild in a separate worktree and fails:offset-max-empty-tableexits 0 and writes the output file.ctest -R jmprel_table_boundspasses and all 10 cases hold.main, so there are no pre-existing failures to separate.git diff --checkis clean, andpython3 tools/check_conventions.py --base origin/mainreports no findings.test_jmprel_table_bounds.pyexercises the Windows output path (--windows). This synthetic fixture does not reach the Linux or--to-intelpaths: both stop earlier withNot enough program header slots: need 5, available 3, so no Linux behaviour is claimed from it.I configured the
core/relinkersubproject on its own rather than the full tree, so thecore/libssuites were not run; they are unrelated to this change.rela_boundsandto_intel_linux_stubsare registered only underif(UNIX AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")and so do not run on Windows.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)Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T11:44:27Z.
Looping in @Dextroid17, @sktharunbalaji13-cmd, and @adriadam10 since the surrounding relinker hardening has moved quite a bit.
#1424 is still intentionally scoped to the DT_JMPREL / DT_PLTRELSZ table-range check only. #917 now owns the per-entry relocation bounds, #1272 is down to the symbol-table entry read, and #1446 is the sibling DT_RELA / DT_RELASZ table-range check.
I rebased this onto current main and re-ran the regression: the three zero-size malformed JMPREL cases are still accepted on pristine main, so this gap remains distinct. The PR is mergeable again and the diff is still just the same 3 files / +103 lines.
Mentioning you all mainly so the merge order stays coordinated and nobody accidentally folds or duplicates the JMPREL side while the adjacent PRs move.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T12:27:34Z.
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): the case that fails is the empty table with an out-of-range offset (pltrelsz=0), where the conversion exits 0 and writes an output. A non-empty table with a bad offset is already rejected by #917's per-entry bound —relaEntryPosatRelinkerPipeline.cpp:190, shared byextractRelaandextractRelative— because the bound lives inside the loop, which an empty table never enters.DT_JMPRELis not emitted at all for these inputs; the garbage is in the OS pair. Scanning the produced file for each(tag, value)pair:So the conversion writes an offset outside the file into the output's dynamic table, with a size of 0. That is the "does exactly what it should or throws" case, not a message preference — which is why I think this is worth merging.
Worth saying in the body, because it is the difference from the change I withdrew. In #1272 I carried a table-level check as a second hunk and @oneandonlydean had me 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 is written. Naming that distinction separates your change from the hunk that was rejected.Two smaller points: your script only exercises
--windows(under--to-intelthis fixture stops earlier withNot enough program header slots: need 5, available 3, so the Linux path is never reached), andhasPltRelocations &&in the guard reads as intentional — I assume it is the "no PLT at all" case, but it may be worth one clause in the description so the next reader does not have to work it out.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T12:33:00Z.
Thanks, this distinction is really helpful. The read-vs-written-output framing makes the scope much clearer, especially given the table-level hunk that was dropped from #1272. I’ll update the body to call out that the zero-size OS-tag case propagates the out-of-range JMPREL offset into the output dynamic table, and I’ll clarify the Windows-only fixture coverage plus the hasPltRelocations guard.
written with AI
Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T13:31:25Z.
Thanks — I reproduced this independently and your read-vs-output framing is much clearer.
Confirmed that the zero-size OS-tag cases still succeed on current
mainand the produced dynamic table carries the invalidDT_OS_JMPRELvalue withDT_OS_PLTRELSZ = 0, while the non-zero cases are already caught by #917.I’ve updated the body to make that distinction explicit, clarify that the synthetic regression only reaches the Windows output path, and explain the
hasPltRelocationsguard. No code change needed.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:39:28Z.
Checked: build + full ctest on Linux with the PR merged onto current
main(458/458 pass);jmprel_table_boundsfails without theRelinkerPipeline.cppchange and passes with it. The empty-table cases (DT_PLTRELSZ= 0) are indeed not reached by the per-entry bound from #917, and Dextroid17 reproduced the same onmain. Merging once CI is green; #1446 will need a rebase on the adjacent line.