Repository navigation
fix(relinker): require the Linux entry, relocation offsets and DT_PLTGOT in PT_LOAD memory - #10402
Conversation
…GOT in PT_LOAD memory The Windows and macOS patchers reject an entry point outside executable PT_LOAD memory and a relocation offset or DT_PLTGOT outside PT_LOAD memory through WindowsLoadImage::GetRva. The Linux patcher copied these values unchecked and reported success; with a bad entry or relocation offset the output crashed with SIGSEGV at load. LinuxElfPatcher::Patch now requires e_entry in an executable load and every non-NONE RELA and JMPREL r_offset and DT_PLTGOT to fit inside one PT_LOAD's MemorySize, in the fixed-base and --lazy-binding paths. A relocation needs the bytes ld.so writes: 4 for PC32 and 32, the output symbol's st_size (at least 1) for COPY, and 8 otherwise. DT_PLTGOT needs 8 bytes with eager binding and 24 with --lazy-binding, where ld.so writes GOT[1] and GOT[2]. Two test fixtures that set e_entry to a file offset outside their only PT_LOAD now use the matching virtual address.
MarcZX10
left a comment
There was a problem hiding this comment.
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.
MarcZX10
left a comment
There was a problem hiding this comment.
Blocking correctness issue in the new relocation-width check: RelocationWidth() returns 8 for R_X86_64_32S, but the x86-64 psABI specifies a 32-bit relocation field for that type. This repository accepts R_X86_64_32S in RebasedRelocations(), so a valid 32S relocation whose 4-byte field ends at the PT_LOAD boundary will be rejected as requiring 8 mapped bytes. Please classify R_X86_64_32S as width 4 and add a boundary fixture analogous to abs32-past-load-end that succeeds when the 4-byte field fits. The ABI’s relocation table lists R_X86_64_32S as word32: https://refspecs.linuxfoundation.org/elf/x86_64-SysV-psABI.pdf
Thanks for the close look. |
RelocationWidth took the width of a COPY relocation from the output symbol table, where every import has st_size 0, so the width was always one byte and the two copy-* boundary cases passed for that reason alone. boykopovar#10658 rejects R_X86_64_COPY in validation, so the type no longer reaches this check; until it merges, COPY takes the 8-byte default like every other type. The three copy-* cases go with it.
|
Thanks for the look. Two changes since:
|
The got-slot-address-size-underflow case puts its GOT slot at the 32-bit wrapped address 0xfffff008, outside every PT_LOAD. The unused filter still keeps the import, which the case keeps checking through the CFG/GOT filtering line, and the Linux patcher now rejects the relocation, so the case expects exit 2 with that message instead of an output file.
|
CI on 9bf8860 failed in unused_filter_indirect, a test newer than this branch. Its case got-slot-address-size-underflow puts a GOT slot at the wrapped address 0xfffff008, outside every PT_LOAD, and expected exit 0. With this PR the Linux patcher rejects that relocation, which is the behaviour the PR adds. 46f03a9 merges current main and b15f3ea makes the case expect the rejection while still checking that the filter keeps the import. |
ahmadmuj
left a comment
There was a problem hiding this comment.
Checked head b15f3ea, merged locally onto main 1602584 (clean merge). Relinker-only Release build, Ubuntu 24.04, GCC 13.
What I ran:
- Full relinker ctest on the merge: 266/266 pass.
- Same merge with main's
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. - The 32S request from b10424a doesn'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:
- An entry in a PT_LOAD with
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. - The
!= 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.
|
@roboterspinne please reconcile the flags-zero entry case noted in the latest review. RequireLoaded checks original PF_X and rejects an in-range entry when p_flags is 0, but ProgramHeaderLayoutBuilder::_fixLoadFlags deliberately maps that same PT_LOAD to RWX. The new check runs before that normalization, so it rejects an input the existing output layout accepts as executable. Please preserve the established flags-zero behavior in the entry check and add an accepted fixture in both default and lazy-binding modes, while retaining rejection of nonzero non-executable flags and out-of-range entries. The relocation-width/NONE notes do not need a blocking change. I confirmed the source mismatch; I have not reproduced it on a title. |
The Linux output maps a PT_LOAD whose p_flags is 0 as RWX, so the e_entry check now accepts an entry inside such a segment instead of rejecting it. A new test case relinks that input in both Linux modes and checks that the output maps the entry's segment RWX. The R_X86_64_NONE skip in the relocation offset loop is removed, together with its none-outside case, because extractRela drops NONE before the relocation tables are rebuilt.
|
@oneandonlydean @ahmadmuj Done in 6647308, plus a merge of current
@MarcZX10 the 32S width request no longer applies: |
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, since bc14e47).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 fix(relinker): reject R_X86_64_COPY relocations #10658 rejects it in validation.R_X86_64_32Sdoes not reach it, becauseValidationPolicyrejects type 11 (fix(relinker): reject link-time relocation types in executables #9220) and refactor(relinker): remove relocation types validation already rejects #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 fix(linux): enter the guest entry on a main stack at the console's address #7532, fix(relinker): reject overlapping PT_LOAD segments in Linux output #10564, fix(relinker): validate every entry of the Linux run path #10607 and fix(relinker): reject R_X86_64_COPY relocations #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(dca72a8), 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.main(4930ee9, merging upstream/main c7d6660).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 the b15f3ea relinker 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 3on 4930ee9: 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(c7d6660) 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)