fix(relinker): reject Linux relocations into non-writable PT_LOAD segments #13977
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13977"
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-31T14:22:08Z.
What
On
60bdb06135(line numbers below refer to it) the Linux patcher checks everyDT_RELAandDT_JMPRELr_offsetwithRequireLoaded(LinuxElfPatcher.cpp:115-120) but passes no required flags (line 427), so a relocation may target aPT_LOADwithoutPF_W. The rebuilt Linux dynamic section never declares text relocations: it takes onlyDT_NEEDEDfrom the input, so an inputDT_TEXTRELis dropped, and itsDT_FLAGSisDF_BIND_NOWwithout--lazy-bindingand absent with it. The gABI allows a relocation to modify a non-writable segment only whenDT_TEXTRELis present, and glibc's_dl_relocate_object(elf/dl-reloc.c) makes such segments writable only for an object withDT_TEXTREL; this comes from reading the glibc source, not from running an output. For aR_X86_64_RELATIVE,R_X86_64_64orR_X86_64_GLOB_DATinDT_RELAor anR_X86_64_JUMP_SLOTinDT_JMPRELwhoser_offsetlies in the fixture'sPF_R | PF_XPT_LOAD, Linux and--lazy-bindingoutput were written with exit 0; aRELATIVEcase withDT_TEXTRELin the input converted the same way, without the tag in the output. The same holds for the lazyDT_PLTGOT, where ld.so stores its link map and resolver (elf_machine_runtime_setup):RequireLoadedchecks it with no required flags as well (line 429), so aDT_PLTGOTinside code converted with--lazy-binding. The other targets already refuse most of these:--macosrefuses all four (Mach-O fixup target is not in a writable segment,MacOsImage.cpp:227, orRuntime import target is not writable), and--windowsrefuses the three that bind imports (WindowsLoadImage.cpp:154) and accepts only theRELATIVE, which becomes a PE base relocation that the Windows loader applies to read-only pages itself. The guest image reader already refusesDT_TEXTRELandDF_TEXTRELin bundled modules (GuestImageReader.cpp:87-90).RequireLoadednow getsPF_Wfor everyDT_RELAandDT_JMPRELentry the patcher writes, and forDT_PLTGOTunder--lazy-binding, so such an input fails at relink time withRELA r_offset 0x300 (8 bytes) does not fit inside a writable PT_LOAD segment(orJMPREL …,DT_PLTGOT …) instead of in the dynamic loader.R_X86_64_NONEentries never reach the patcher (RelinkerPipeline.cpp:395-396), and without--lazy-bindingDT_PLTGOTstill only has to lie in aPT_LOAD. APT_LOADwhosep_flagsis 0 still passes, as it does fore_entry(line 117): the Linux output maps itPF_R | PF_W | PF_X(_fixLoadFlags,ProgramHeaderLayoutBuilder.cpp:126-127, frombc14e4743c), so the kernel'smake_protgives the relocation target a writable page;entry-in-flagless-loadchecks those output flags. The message for anr_offsetoutside everyPT_LOAD, and for a lazyDT_PLTGOTthere, now also names a writable segment.DT_TEXTRELthrough. Passing it through would make ld.so remap code pages writable at load, while macOS output and bundled modules already refuse the same inputs. On #11045 wachi35676 mapped all 129,623RELATIVE,R_X86_64_64,GLOB_DATandJUMP_SLOTtargets of 24 PS5 ELF files (the homebrew eboots of PPSA99005, PPSA47402, PPSA99581 and PPSA99995, theirsce_modulePRX files and 16 wine PRX modules of PPSA99995) to theirPT_LOADand found every one in aPF_Wsegment.linux_load_boundsgains cases forRELATIVEandR_X86_64_32into aPF_R | PF_XPT_LOAD, aRELAinto a read-only.bss-likePT_LOAD, aJMPRELinto code, and accepted cases forRELATIVEinto anRWXand a flaglessPT_LOAD, each for the default,--linux-fixed-base 0x800000and--lazy-binding, plus aDT_PLTGOTinside code that converts eagerly and fails with--lazy-binding; it andunused_filter_indirectexpect the new message text. Six existing tests placed GOT slots or relocation targets in the singlePF_R | PF_XPT_LOADofmain_fixtureor of the C++ executable fixture and now fail by design; their fixtures give these targets a writable segment.importing_executableintest_guest_symbol_names.pysetsPF_Won its singlePT_LOAD, which becomesPF_R | PF_W | PF_X(only when it has imports, becauseinit_array_rootscounts the code regions of the import-free fixture), astest_game_patches.pyandRelocTableBoundsTests.cppdo on theirs.strict_fixtureintest_duplicate_relocations.pyinstead splits its copy intoPF_R | PF_Xat 0x4000 andPF_R | PF_Wat 0x4800, because the strict filter refuses a writable code segment (StrictNidFilter.cpp,unsupported executable segment layout), and the separate helper of the same name intest_import_symbol_types.pykeeps that split. Both halves of the split lie in one 4 KiB page, and neither image is run.main_fixtureitself is unchanged.7487bb1108. The earlier head50b37bb1a9split the sharedimporting_executablethe same way.agc_driver_guest_memory_lifetime_convertedandlinux_guest_qualified_importstake that helper, set its firstPT_LOADtoPF_R | PF_W | PF_Xand run the converted executable on Linux, where the kernel maps every laterPT_LOADover its page-aligned range withMAP_FIXED(fs/binfmt_elf.c,elf_mapandload_elf_binary; read in the kernel source, not run here). ThePF_R | PF_Whalf then replaced the page that holds the guest code, and the code lost execute permission; oneandonlydean reported both tests failing on Linux. With the singlePF_R | PF_W | PF_XPT_LOADboth tests get the same input as onmain.On 18 November 2026
pr_overlap.py --branchon7487bb1108reported no overlap with the open pull requests and none that it could not compare (main28c4880966). #15903, which it could not compare earlier because that pull request conflicted withmain, has since been updated (head bdd07bbaf4) and can be merged into thatmain; it still shares onlyRelocTableBoundsTests.cppwith this pull request, and a trial merge of the two on thatmainis clean. #10917 and #11487, named here before, have been merged.Tested
Windows 11, WinLibs GCC 15.2.0, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), on7487bb1108(mainef4b9389b6merged in4fef80c055), compared with the same build ofmainef4b9389b6:ctest --test-dir build-relinker -j 6 --timeout 300: 949 tests on both. The same 9 fail on both,select_testsand its four parts, the threecheck_compile_test_*tests andpatch_advisor_json_integration, which fail on this machine without this change;long_pathsis skipped on both. The relinker tests that takeimporting_executableor that this pull request changes pass, 15 in all, among themlinux_guest_qualified_importsandduplicate_relocations.agc_driver_guest_memory_lifetime_convertedis registered on Linux x86-64 only and did not run, andlinux_guest_qualified_importsran its conversions but not its executions, which are Linux-only. What was checked instead: the input thatagc_driver_guest_memory_lifetime_convertedconverts (importing_executablewith its imports andPT_LOAD[0]set to 7) has the same SHA-256 on this head as onmain(8bbffd524cb38ba3…), and a different one with50b37bb1a9merged ontomain. For theexecutable-two-providerscase oflinux_guest_qualified_imports, this head andmainwrite byte-identical files (6 of 6, includinginput.elfandoutput.elf). Mapping thePT_LOADs of thatoutput.elfpage by page, a later one replacing an earlier one, leaves the guest code page 0x404000PF_R | PF_W | PF_Xon this head and onmain, andPF_R | PF_Wwith50b37bb1a9merged ontomain.guest_deferred_dependenciesalso takesimporting_executablewith imports and runs its output on Linux, but does not setPF_W | PF_Xitself. Its converted code page stays executable with the split of50b37bb1a9(thePF_R | PF_Whalf lands on its own page) and with this head, whereoutput.elfdiffers frommainin one byte, thatp_flags.python tools/check_conventions.py --base upstream/mainwith this title and description passed on7487bb1108.Earlier, Windows 11, WinLibs GCC 15.2.0, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), on50b37bb1a9(based on60bdb06135;mainat73f4f7e06echanged none of these files and merges cleanly):LinuxElfPatcher.cppof60bdb06135, the four relocation types above into aPF_R | PF_XPT_LOADconvert with exit 0 for Linux and--lazy-binding, and theRELATIVEcase with aDT_TEXTRELin the input converts withoutDT_TEXTRELin the output and withDT_FLAGS0x8; with this change all of them fail with the new message,--windowsand--macosbehave as described above, and the same input with anRWXPT_LOADstill converts.guest_symbol_names,guest_registry,import_symbol_types,duplicate_relocations,game_patchesandreloc_table_boundswithdoes not fit inside a writable PT_LOAD segment; with them these six,linux_load_bounds,unused_filter_indirect,init_array_roots,needed_libraries,guest_deferred_dependencies,sce_code_gapsandsce_module_code_extentpass.ctest --test-dir build-relinker -j 5on50b37bb1a9: 543 tests, 537 passed,long_pathsskipped;annotate_module, a Python test oftools/annotate_module.pythat does not run the relinker, timed out under the parallel load and passed alone in 15.5 to 16 s in three of four runs on this machine;select_testsand its three partsselect_tests_SelectGraphStateTests,select_tests_SelectBuildTestsandselect_tests_SelectArgumentTestsfail with the Windows symlink fixture'sFileExistsError, as onmainon this machine; this change touches nothing undertools/.python tools/check_conventions.py --base 60bdb06135with this title and description passed on50b37bb1a9.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-31T14:25:40Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
At head
50b37bb1a98edde1d306775c870a20440fd26814on base73f4f7e06e4f210a8c6570e335fd654dc0a86134, the new writable-segment check still accepts aPT_LOADwithp_flags == 0as writable.What I ran
Linux x86_64, Python 3.11.2, GCC 12.2.0, Git 2.39.5. The dependency/merge preview and source checks were run; I did not build or run C++ tests locally. Relinker CI was still running when checked.
Results
Precheck output
Negative control
PF_Wis rejectedrelative-in-flaglesscase setsp_flagsto zero and expects successFindings
Blocking
RequireLoadedstill acceptsheader.Flags == 0, and the newrelative-in-flaglesscase expects that relocation to pass. In Linux, the ELF loader derivesPROT_WRITEfromPF_W; a zerop_flagsvalue therefore does not describe a writable segment. The relocation is accepted by this check despite the PR's stated requirement to reject targets outside writablePT_LOADsegments.Keep the zero-flags shortcut only for calls that request no permission check. For example:
Update the flagless relocation test to expect rejection, and retain a separate containment-only test if that behavior is needed.
Non-blocking
None identified.
Sources
ffc253263a1375a65fa6c9f62a893e9767fbebfaRequireLoadedcheck at reviewed headNot verified
No local build or C++ tests. Hosted relinker jobs had not completed. The local precheck's ABI/test-quality helper commands were unavailable from its isolated snapshot; that is a tooling limitation, not a PR test result.
To get this to a good state
Require
PF_Wfor writable relocation targets even when the segment has no flags, and change the flagless case to assert rejection. I will rerun the same checks and inspect the results on the new head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T15:21:44Z.
Review: APPROVED
Re-checked — same head, looks good
I’m correcting my earlier change request: zero-flag
PT_LOADsegments are normalized to RWX before the Linux output ELF is written, so the relocation case I previously flagged does target a writable output mapping.What I ran
Reviewed head
50b37bb1a98edde1d306775c870a20440fd26814against base73f4f7e06e4f210a8c6570e335fd654dc0a86134; this is the same code head as my earlier review. The live merge preview was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. No local build or compilation was run.Results
pr_test_quality.pyhelper. The check did not run there; I ran that helper separately against the exact head.linux,windows,register-context, andwrite-watchjobs were skipped.RequireLoadedexception for zero flags matches the actual output transformation; the earlier blocking concern is resolved by existing code, not by a new PR commit.Selected unedited check output
Findings
Blocking
None confirmed after reviewing the author’s evidence and the unchanged implementation path.
Non-blocking
I did not run a local negative control. The macOS ARM relinker job passed during this re-check; the previously reviewed CI jobs also passed at the current head.
Sources
RequireLoadedchecks the original input headers.WriteLayoutapplies_fixLoadFlagsto every keptPT_LOADbefore serializing it;_fixLoadFlagsmaps zero flags toPF_R | PF_W | PF_X.relative-in-flaglesscase covers the transformed segment in normal, fixed-base, and lazy-binding modes.R_X86_64_NONEbefore constructing relocation tables for the Linux patcher.Not verified
I did not build or run tests locally. The hosted relinker CI passed on Ubuntu, Windows, macOS x86_64, macOS ARM, and ASAN; generic platform jobs listed above were skipped. No local runtime or title integration was tested.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T22:07:37Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the checked-in tests and reported runs support the writable-target checks.
What I ran
Reviewed head
7487bb11083f0dc35928124b440e3b4accbff0fdfrom the prepared diff and pinned repository reader. No tests or builds were run during this review; the Windows test results in the description are author-reported.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or build were run. Linux execution and the Linux-only tests were not independently verified. Read all eight changed files listed in the manifest; no changed files were excluded.
To get this to a good state
Nothing is required.