fix(relinker): require the Linux entry, relocation offsets and DT_PLTGOT in PT_LOAD memory #10402
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10402"
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 roboterspinne on 2026-10-23T21:33:38Z.
What
The Linux output did not check that
e_entry, relocation offsets and DT_PLTGOT lie in PT_LOAD memory: the relink returned 0 and the output crashed at load. Windows and macOS already reject such inputs throughWindowsLoadImage(WindowsLoadImage.cpp:56-70, used for relocation targets inWindowsRelocationBuilder.cpp:31and for DT_PLTGOT inWindowsPePatcher.cpp:43-44andMacOsMachOPatcher.cpp:65-66).LinuxElfPatcher::Patchnow checks, before both output paths (fixed base and--lazy-binding) and with the same rule asGetRva(the whole range inside one PT_LOAD'sMemorySize):e_entry: 1 byte in a PT_LOAD withPF_X, or in a PT_LOAD whosep_flagsis 0, because the Linux output maps such a segment RWX (ProgramHeaderLayoutBuilder::_fixLoadFlags, sincebc14e4743c).r_offset, over the bytes ld.so writes: 4 forPC32and32, 8 otherwise.R_X86_64_NONEnever reaches this check:extractReladrops it (RelinkerPipeline.cpp:353onmain),SysVDynamicSectionBuilderrebuilds both tables from the extracted references (SysVDynamicSectionBuilder.cpp:153and163), andextractLocalappends only RELATIVE and TLS entries.R_X86_64_COPYdoes not reach it either, because #10658 rejects it in validation.R_X86_64_32Sdoes not reach it, becauseValidationPolicyrejects type 11 (#9220) and #10357 removed the constant.--lazy-binding, where glibc'self_machine_runtime_setupwrites GOT[1] and GOT[2].Messages name the field, for example
e_entry 0x7ffff000 is not inside an executable PT_LOAD segmentandRELA r_offset 0xffc (8 bytes) does not fit inside a PT_LOAD segment.ElfReaderturns aPT_SCE_RELROthat no PT_LOAD covers into a PT_LOAD beforePatch, so the usual PS5 GOT layout stays accepted. Windows, macOS, guest modules and the Linux output layout are unchanged.test_linux_load_alignment.pyandtest_linux_eh_frame_header.pysete_entryto 0x4000, a file offset that their only PT_LOAD (p_flagsR|X,p_offset0x4000,p_vaddr0,p_memsz0x1000) maps at virtual address 0. The virtual address 0x4000 lies outside every PT_LOAD, so it is still rejected after thep_flags0 change (e_entry 0x4000 is not inside an executable PT_LOAD segment). They now use 0 and check the same things as before.Tested
Ubuntu 24.04 x86_64, glibc 2.39, relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.linux_load_bounds, in the default mode and with--lazy-binding(case list as of the fourth commit, which was tested on Windows only, see below; the rejected cases are unchanged since the first commit): 15 rejected cases (entry outside a load or in a non-executable one; RELA and JMPREL offsets outside a load or crossing its end; DT_PLTGOT outside a load; lazy DT_PLTGOT too close to its end) and 19 accepted ones (4-byte types in the last bytes of a load, offsets in.bssand in an uncoveredPT_SCE_RELRO, an entry in a PT_LOAD whosep_flagsis 0 with its output segment checked to be RWX, eager DT_PLTGOT at 0xff0). WithLinuxElfPatcher.cppfrommain, all 15 rejected cases relink with rc 0.ctest --test-dir build -j2: 241/241 passed.main, thetest_linux_entry_argvfixture withe_entry = 0x7ffff000and thetest_optional_pltfixture with a RELATIVEr_offset = 0x7ffff000relink with rc 0 and the outputs exit with SIGSEGV. On this branch both fail with rc 2 and the messages above.git merge-treeof this branch with #7532, #10564, #10607 and #10658, which also changeLinuxElfPatcher.cpp, is clean.python3 tools/check_conventions.py --base upstream/mainwith this title and description passed.linux_load_boundspasses in both modes (0.69 s);ctest -j 6: 235/236 passed, the one failure isselect_tests, a known local Windows failure (it cannot create its symlink fixture);check_conventions.pywith this title and description passed.main(dca72a82da), same host and build:unused_filter_indirectexpects the load bounds error for the wrapped GOT slot ofgot-slot-address-size-underflowand still checks that the filter keeps its import; against amainrelinker that case relinks with rc 0.ctest -j 6: 241/244 passed,long_pathsskipped;select_tests(the known local Windows failure) andpatch_advisor_unit(unchanged frommain, fails here because the build path contains spaces) are unrelated;check_conventions.pywith this title and description passed.66473085c2), after review: same host and build, after merging currentmain(4930ee9e55, merging upstream/mainc7d6660ef8).linux_load_boundsaddsentry-in-flagless-loadin both modes and checks that the output PT_LOAD holding the original entry is RWX; it dropsnone-outside, which also relinks with rc 0 now that the skip is gone. Counter-check: with theb15f3eac86relinker the new case fails in both modes with rc 2 andFAIL: e_entry 0x200 is not inside an executable PT_LOAD segment; with this commit it passes.e_entry0x4000 in both fixtures still fails withe_entry 0x4000 is not inside an executable PT_LOAD segment, so their edits stay. Passed:linux_load_bounds(1.25 s),unused_filter_indirect,linux_eh_frame_header,linux_load_alignment,jmprel_table_bounds,entry_point_bounds,linux_fixed_base,game_patches,init_array_roots,gnu_hash_analysis,linux_load_pages,optional_pltand every otherlinux_*test. Full suite:ctest --test-dir build-relinker -j 3on4930ee9e55: 262 tests, 259 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture),patch_advisor_unit(path with spaces); both fail again when rerun alone.check_conventions.py --base upstream/main(c7d6660ef8) with this title and description passed.Checklist
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 MarcZX10 on 2026-10-23T21:59:36Z.
Review: APPROVED
Reviewed the Linux PT_LOAD validation and the new synthetic test matrix. The checks are applied before fixed-base and lazy-binding output, skip R_X86_64_NONE, account for PC32/32 and COPY widths, and include BSS/RELRO cases. The existing tests were corrected from file offsets to their mapped virtual entry addresses. Hosted relinker matrix and conventions pass; full product builds are skipped by repository policy. No blocking issue found.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:08:03Z.
Review: CHANGES_REQUESTED
Blocking correctness issue in the new relocation-width check:
RelocationWidth()returns 8 forR_X86_64_32S, but the x86-64 psABI specifies a 32-bit relocation field for that type. This repository acceptsR_X86_64_32SinRebasedRelocations(), so a valid 32S relocation whose 4-byte field ends at the PT_LOAD boundary will be rejected as requiring 8 mapped bytes. Please classifyR_X86_64_32Sas width 4 and add a boundary fixture analogous toabs32-past-load-endthat succeeds when the 4-byte field fits. The ABI’s relocation table listsR_X86_64_32Sasword32: https://refspecs.linuxfoundation.org/elf/x86_64-SysV-psABI.pdfImported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T09:56:11Z.
Review: COMMENTED
Checked head
b15f3eac86, merged locally onto main16025845e5(clean merge). Relinker-only Release build, Ubuntu 24.04, GCC 13.What I ran:
LinuxElfPatcher.cpp:linux_load_boundsandunused_filter_indirectfail, and all 15 rejected runs relink with rc 0. So the new test still guards the change on current main.b10424a47bdoesn't apply anymore.ValidationPolicy.cpp:9-22only accepts NONE, 64, PC32, GLOB_DAT, JUMP_SLOT, RELATIVE, 32, DTPMOD64, DTPOFF64 and TPOFF64, andR_X86_64_32Sisn't in the tree. #10658 is merged, so COPY never reachesPatcheither.RelocationWidthmatches the psABI field size for every accepted type.RequireLoadedcan't wrap:address - MappedAddress < MemorySizeis checked before the size subtraction.Two notes, neither blocking:
p_flags0 is now rejected, but the output maps that segment RWX (_fixLoadFlags,ProgramHeaderLayoutBuilder.cpp:127). The entry check reads the original flags instead. Repro:fixture(linux=True)fromtest_jmprel_table_bounds.pywith the first PT_LOAD'sp_flagsset to 0. On main it relinks with rc 0 andreadelf -lshows that segment asRWE. On this branch it fails withe_entry 0x200 is not inside an executable PT_LOAD segment. I don't know if flags-0 loads show up in real inputs. If they do,header.Flags == 0 || (header.Flags & flags) == flagsinRequireLoadedwould match what the output actually maps.!= R_X86_64_NONEskip atLinuxElfPatcher.cpp:378never sees a NONE entry.extractReladrops NONE atRelinkerPipeline.cpp:351before the tables are built, sonone-outsidealready relinks on main. Same reasoning as leaving out the 32S case: the skip and that case could both go.