Skip to content

fix(relinker): require the Linux entry, relocation offsets and DT_PLTGOT in PT_LOAD memory - #10402

Merged
oneandonlydean merged 6 commits into
boykopovar:mainfrom
roboterspinne:fix/relinker-linux-load-bounds
Oct 26, 2026
Merged

oneandonlydean merged 6 commits into
boykopovar:mainfrom
roboterspinne:fix/relinker-linux-load-bounds

Conversation

@roboterspinne

@roboterspinne roboterspinne commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

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 through WindowsLoadImage (WindowsLoadImage.cpp:56-70, used for relocation targets in WindowsRelocationBuilder.cpp:31 and for DT_PLTGOT in WindowsPePatcher.cpp:43-44 and MacOsMachOPatcher.cpp:65-66).

LinuxElfPatcher::Patch now checks, before both output paths (fixed base and --lazy-binding) and with the same rule as GetRva (the whole range inside one PT_LOAD's MemorySize):

  • e_entry: 1 byte in a PT_LOAD with PF_X, or in a PT_LOAD whose p_flags is 0, because the Linux output maps such a segment RWX (ProgramHeaderLayoutBuilder::_fixLoadFlags, since bc14e47).
  • Every RELA and JMPREL r_offset, over the bytes ld.so writes: 4 for PC32 and 32, 8 otherwise. R_X86_64_NONE never reaches this check: extractRela drops it (RelinkerPipeline.cpp:353 on main), SysVDynamicSectionBuilder rebuilds both tables from the extracted references (SysVDynamicSectionBuilder.cpp:153 and 163), and extractLocal appends only RELATIVE and TLS entries. R_X86_64_COPY does not reach it either, because fix(relinker): reject R_X86_64_COPY relocations #10658 rejects it in validation. R_X86_64_32S does not reach it, because ValidationPolicy rejects 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.
  • DT_PLTGOT when a JMPREL table exists: 8 bytes, or 24 with --lazy-binding, where glibc's elf_machine_runtime_setup writes GOT[1] and GOT[2].

Messages name the field, for example e_entry 0x7ffff000 is not inside an executable PT_LOAD segment and RELA r_offset 0xffc (8 bytes) does not fit inside a PT_LOAD segment. ElfReader turns a PT_SCE_RELRO that no PT_LOAD covers into a PT_LOAD before Patch, so the usual PS5 GOT layout stays accepted. Windows, macOS, guest modules and the Linux output layout are unchanged.

test_linux_load_alignment.py and test_linux_eh_frame_header.py set e_entry to 0x4000, a file offset that their only PT_LOAD (p_flags R|X, p_offset 0x4000, p_vaddr 0, p_memsz 0x1000) maps at virtual address 0. The virtual address 0x4000 lies outside every PT_LOAD, so it is still rejected after the p_flags 0 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.

  • New 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 .bss and in an uncovered PT_SCE_RELRO, an entry in a PT_LOAD whose p_flags is 0 with its output segment checked to be RWX, eager DT_PLTGOT at 0xff0). With LinuxElfPatcher.cpp from main, all 15 rejected cases relink with rc 0.
  • First commit: ctest --test-dir build -j2: 241/241 passed.
  • On main, the test_linux_entry_argv fixture with e_entry = 0x7ffff000 and the test_optional_plt fixture with a RELATIVE r_offset = 0x7ffff000 relink with rc 0 and the outputs exit with SIGSEGV. On this branch both fail with rc 2 and the messages above.
  • git merge-tree of 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 change LinuxElfPatcher.cpp, is clean.
  • python3 tools/check_conventions.py --base upstream/main with this title and description passed.
  • Second commit, Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build: linux_load_bounds passes in both modes (0.69 s); ctest -j 6: 235/236 passed, the one failure is select_tests, a known local Windows failure (it cannot create its symlink fixture); check_conventions.py with this title and description passed.
  • Third commit, after merging current main (dca72a8), same host and build: unused_filter_indirect expects the load bounds error for the wrapped GOT slot of got-slot-address-size-underflow and still checks that the filter keeps its import; against a main relinker that case relinks with rc 0. ctest -j 6: 241/244 passed, long_paths skipped; select_tests (the known local Windows failure) and patch_advisor_unit (unchanged from main, fails here because the build path contains spaces) are unrelated; check_conventions.py with this title and description passed.
  • Fourth commit (6647308), after review: same host and build, after merging current main (4930ee9, merging upstream/main c7d6660). linux_load_bounds adds entry-in-flagless-load in both modes and checks that the output PT_LOAD holding the original entry is RWX; it drops none-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 and FAIL: e_entry 0x200 is not inside an executable PT_LOAD segment; with this commit it passes. e_entry 0x4000 in both fixtures still fails with e_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_plt and every other linux_* test. Full suite: ctest --test-dir build-relinker -j 3 on 4930ee9: 262 tests, 259 passed, long_paths skipped; failures known on this machine on main as 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

…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.
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
PR 6713 core/relinker/CMakeLists.txt

@roboterspinne

Copy link
Copy Markdown
Contributor Author

Checked manually: this PR adds two lines at 527-530 of core/relinker/CMakeLists.txt. #5021 changes lines 505-515, #9408 lines 542-549, and #6044 rewrites 415-511 and 541 onward. No overlap.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@roboterspinne

Copy link
Copy Markdown
Contributor Author

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. R_X86_64_32S cannot reach RelocationWidth(): since #9220, ValidationPolicy rejects type 11 for every DT_RELA and DT_JMPREL entry (RelinkerPipeline.cpp:349) before LinuxElfPatcher::Patch runs, and Patch only sees tables built from validated entries. On this branch, a 32S relocation fails with Unsupported relocation type 0xb and leaves no output, whether it points into the last 4 bytes of a load or elsewhere, in the default mode and with --lazy-binding, so a boundary fixture that accepts a 32S relocation cannot be built. test_linux_fixed_base.py (unsupported-type) and test_executable_tls_relocations.py already pin that rejection. I left 32S out on purpose: a case R_X86_64_32S here would be unreachable, and #10357 removes that constant because the fixed-base whitelist's 32S case became unreachable for the same reason. If you still want the width listed, I can add it, but it could not be tested from the command line.

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.
@roboterspinne

Copy link
Copy Markdown
Contributor Author

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.
@roboterspinne

Copy link
Copy Markdown
Contributor Author

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 ahmadmuj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_bounds and unused_filter_indirect fail, 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-22 only accepts NONE, 64, PC32, GLOB_DAT, JUMP_SLOT, RELATIVE, 32, DTPMOD64, DTPOFF64 and TPOFF64, and R_X86_64_32S isn't in the tree. #10658 is merged, so COPY never reaches Patch either. RelocationWidth matches the psABI field size for every accepted type.
  • RequireLoaded can't wrap: address - MappedAddress < MemorySize is checked before the size subtraction.

Two notes, neither blocking:

  1. An entry in a PT_LOAD with p_flags 0 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) from test_jmprel_table_bounds.py with the first PT_LOAD's p_flags set to 0. On main it relinks with rc 0 and readelf -l shows that segment as RWE. On this branch it fails with e_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) == flags in RequireLoaded would match what the output actually maps.
  2. The != R_X86_64_NONE skip at LinuxElfPatcher.cpp:378 never sees a NONE entry. extractRela drops NONE at RelinkerPipeline.cpp:351 before the tables are built, so none-outside already relinks on main. Same reasoning as leaving out the 32S case: the skip and that case could both go.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@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.
@roboterspinne

Copy link
Copy Markdown
Contributor Author

@oneandonlydean @ahmadmuj Done in 6647308, plus a merge of current main (4930ee9):

  • RequireLoaded now accepts a PT_LOAD whose p_flags is 0 (header.Flags == 0 || (header.Flags & flags) == flags), matching _fixLoadFlags. The new entry-in-flagless-load case relinks with rc 0 in the default mode and with --lazy-binding and checks that the output maps the entry's segment RWX; with b15f3ea it fails in both modes with e_entry 0x200 is not inside an executable PT_LOAD segment. entry-not-executable (nonzero flags without PF_X) and entry-outside are still rejected in both modes.
  • The unreachable R_X86_64_NONE skip and its none-outside case are removed: extractRela drops NONE before the tables are rebuilt, and that input still relinks with rc 0.
    Full relinker suite on Windows: 259/262 passed, long_paths skipped; select_tests and patch_advisor_unit fail on this machine on main as well.

@MarcZX10 the 32S width request no longer applies: ValidationPolicy rejects type 11 and #10357 removed the constant, as ahmadmuj confirmed above. Could you take another look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants