fix(relinker): reject main-executable PT_LOAD flags outside PF_R, PF_W and PF_X #13862
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13862"
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-31T10:03:57Z.
What
On
45e39c7ad1(line numbers below refer to it) aPT_LOADwhosep_flagshas a bit other thanPF_R,PF_WandPF_Xis refused by every reader except the main executable's path to Linux output:WindowsLoadImagethrowsUnsupported PT_LOAD permission flagsfor(header.Flags & ~7u) != 0(WindowsLoadImage.cpp:23-24), which--windowsand--macosboth build (MacOsMachOPatcher.cpp:65), and the guest image reader fails such a segment asInvalid load segment(GuestImageReader.cpp:48).RelinkerPipeline::Relinkreads the executable's program headers (line 81) and walks them (line 89) without that check, and for Linux outputProgramHeaderLayoutBuilder::_fixLoadFlagsreturnsoriginalFlags | PF_R(line 128), so the unknown bits are copied into the output'sp_flags. An executable whose writablePT_LOADhasp_flags0xe,0x100006or0x80000006converted with exit 0 for Linux,--lazy-bindingand--linux-fixed-base 0x400000, and the output'sPT_LOADkept0xe,0x100006and0x80000006; an executablePT_LOADwithp_flags0xdalso converted for Linux.--windowsand--macosfailed on all of them withUnsupported PT_LOAD permission flags.Unsupported PT_LOAD permission flagsat the segment's file offset for everyPT_LOADwith(p_flags & ~7) != 0, writable or executable, on every target and before any output is built. The message and the offset are those ofWindowsLoadImage, so an input that fails only on its flags fails on Windows and macOS with the same text as before, one step earlier; an input that also has anotherPT_LOADerror thatWindowsLoadImagechecks first, such asEmpty PT_LOAD segment, now reports the flags first.PF_R,PF_WandPF_Xin any combination, including 0, are unchanged, and so are the flags of every other segment type and guest modules.PF_MASKOS(0x0ff00000) andPF_MASKPROC(0xf0000000) for operating-system and processor flags, and Linux ignores bits it does not know. No flag in these ranges is defined anywhere in the repository, and Windows output, macOS output and guest modules already refuse all of them, so an executable that converts for Windows today has none; this applies the same rule to Linux output instead of keeping one target that passes unknown bits through.pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare and cannot compare #10792 (607bcb9291) and #10861 (2ac648f312), which also changeRelinkerPipeline.cppbut do not merge withmain. Checked by hand: a trial merge of this branch with each of them conflicts only in the files thatmainwith it already conflicts in (SyscallScanner.cppfor #10792;CodeInstructionCollector.cppanddocs/dev/debt/relinker.mdfor #10861), andRelinkerPipeline.cppmerges cleanly. In that file #10792 inserts after line 30 and changes lines 463 to 469, and #10861 inserts after lines 5, 7 and 30 and changes lines 434 to 471, 538 to 561 and 673, all away from line 89. The check sits in the pipeline rather than next to thePT_LOADsize check ofElfReader.cpp:143becauseElfReaderalso reads guest modules, whose reader reports such a segment asInvalid load segment, and because open #11993 changes the start ofElfReader::ReadProgramHeaders.Tested
Windows 11, WinLibs GCC 15.2.0, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), based on45e39c7ad1(mainatc3c45d73abchanged none of these files and merges cleanly):executable_segmentsgains a case that sets0x8,0x100000and0x80000000on top of the flags of the writable (PF_R | PF_W) and of the executable (PF_R | PF_X)PT_LOADof its fixture and expects exit 2,Unsupported PT_LOAD permission flagsand no output for Linux,--lazy-binding,--linux-fixed-base 0x400000,--windowsand--macos. Built with the pipeline file of45e39c7ad1it fails at the first case (exit 0 on Linux); onc408446182it passes.ctest --test-dir build-relinker -j 5on0e7140fa4d(c408446182only adds the executable-segment case to that test): 543 tests, 538 passed,long_pathsskipped;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/.4d6f5458c0(after review): the case now expects the whole diagnostic line,FAIL: Unsupported PT_LOAD permission flags (offset 0x200)for the executablePT_LOAD, whosep_offsetis 0x200, andFAIL: Unsupported PT_LOAD permission flagswith no offset for the writable one, whosep_offsetis 0 (core/relinker/main.cppappends the offset only when it is nonzero).ctest --test-dir build -R '^executable_segments$' --output-on-failure: 1/1 passed (2.78 s). Counter-checks: run against a relinker built frommain(without the new check) it fails at the first case,(64, 8, []); with the check temporarily throwing offset 0 instead ofph.Offset(not committed) it fails at(176, 8, []).python tools/check_conventions.py --base upstream/main --head HEADwith this title and description passed on4d6f5458c0.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 adam25136 on 2026-10-31T21:58:53Z.
Review: COMMENTED
Reviewed
c408446182merged intomainf445f826a9. AI-assisted (Claude); each point checked by hand.The description's claims match the tree (
WindowsLoadImage.cpp:23-24,GuestImageReader.cpp:48,ProgramHeaderLayoutBuilder.cpp:126-128), andConversion.cpp:95is the only construction ofRelinkerPipeline, so every main-executable conversion reaches the new check. The new case fails againstmain's relinker (header64,0x8, Linux output, exit 0) and passes on this branch.Non-blocking:
test_executable_segments.py:146matches only the message, while the description says the error is reported at the segment's file offset asWindowsLoadImagedoes.main.cpp:483prints the offset only when it is nonzero, so theheader == 176cases can pin it with"Unsupported PT_LOAD permission flags (offset 0x200)"; Linux and--windowsoutput both print it on this branch. Theheader == 64segment hasp_offset0 and prints none.Looks correct to me.