fix(relinker): refuse GS traps that overwrite a code entry point in macOS output #11883
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11883"
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-27T11:14:26Z.
What
--macosreplaces a guest GS memory instruction with UD2 and NOPs and, of the code entries into it, refuses only a relative branch (#7706); since #11090,--windowsalso refuses any other code entry inside the trapped bytes, but--macosdoes not. On the untouched base17dffabc20(#11488 at9ef8b4b75amerged withmain801d5b9bc1),65 48 8b 04 25 28 00 00 00 c3at 0x210 with an entry root at 0x214 (aSTT_FUNCsymbol, anR_X86_64_RELATIVEpointer, a REX.Wleatarget or aDT_INIT_ARRAYentry) converts with--macoswith exit 0, and the Mach-O holds0f 0b 90 90 90 90 90 90 90 c3at file offset 0x10210, so the entry at 0x214 runs NOPs andretinstead ofand eax,0x28; ret;--windowsrefuses all four inputs withGS trap would overwrite an executable entry point (offset 0x214). maximilianfv noted this gap in the review of #11488 and suggested a follow-up PR.ScanTlsinMacOsImage.cpprecords for each trapped GS memory instruction[start, start + length)the first collector entry root withstart < root < start + length, with the sameentryRoots.upper_boundlookup and file-offset convention asWindowsTlsBuilder::Build(#11090). In theWriteMacOsImagetrap loop, after the existingBranch enters a guest GS instructioncheck and before theAMD-only instruction is also a guest GS accesscheck, such a root throwsGS trap would overwrite an executable entry point; the CLI exits 2 and writes no output.entryRootsalso contains relative branch targets, so the branch check stays first and keeps its message, whichmacos_gs_trapspins for--macos(its offset is pinned for--windowsbytls_function_coverage_rejected). A root at the instruction start is still trapped as before. The entry roots come from #11488, which makesScanTlspass&entryRootsinstead ofnullptrtoCodeInstructionCollector::Collect; this PR therefore depends on #11488, and its diff includes the changes of #11488 (ScanTlsand onetest_sce_code_extent.pycase) until #11488 merges. A new entry before the macOS GS entry indocs/dev/debt/relinker.mdlists both macOS messages, the entries each covers, and that no option skips either check; the Windows entry is unchanged.test_macos_gs_traps.py(macos_gs_traps) now also converts with--macosthe entry-root images oftest_windows_tls_protected_entries.py(make_image, whichtest_gs_trap_entry_roots.pyuses for--windows) with65 48 8b 04 25 28 00 00 00 c3at 0x210: rootsrelative,symbol,leaandinitializermust exit 2 with the message,(offset 0x214)and no output; controlssafeandaccess-startmust convert, with0f 0b 90 90 90 90 90 90 90 c3and without the original instruction in the executable ranges of the Mach-O.test_gs_trap_entry_roots.pyis unchanged. No new test file and no CMake change.pr_overlap.pyreports no shared lines with open pull requests that merge withmain; the macOS cases sit intest_macos_gs_traps.pyand the debt entry is a new bullet, so Felipebmaitan's #11628, #11661, #11689 and #11698, which rewritetest_gs_trap_entry_roots.pyand the Windows GS entry, merge with this branch without conflict. It cannot compare this branch with #5021 (docs/dev/debt/relinker.md), #10249 (MacOsImage.cpp) and #10861 (MacOsImage.cpp,test_sce_code_extent.py,docs/dev/debt/relinker.md), because those do not merge withmain. Checked by hand: trial merges with #5021 and #10249 conflict in exactly the filesmaindoes. With #10861 the only conflict outsideCodeInstructionCollector.cpp, which conflicts withmaintoo, is #11488'sentryRootsargument ofCollectinScanTls, the same conflict #11488 alone has; none of this PR's own lines is involved.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.MacOsImage.cppof the untouched base17dffabc20and the extended test,macos_gs_trapsfails withentry-relative: expected the GS trap to be refused at offset 0x214: 0after all earlier cases passed. Run case by case, the four root inputs exited 0 with an output file, and both controls converted with the trapped bytes and without the original instruction. With the change (566fbb17ef) the four root inputs exit 2 withGS trap would overwrite an executable entry point (offset 0x214)and no output, the controls are unchanged, andmacos_gs_trapspasses in 1.1 s.ctest -R 'gs_trap|macos_gs|tls_function_coverage|sce_code_extent|macos_tls|windows_tls' -j 4on566fbb17ef: 43 tests passed in 25.1 s, among themgs_trap_entry_roots(0.2 s),macos_gs_traps(1.0 s),sce_code_extent(1.7 s),windows_tls_protected_entries(0.4 s),tls_function_coverage_rejected(0.4 s),macos_tls_overlap,macos_tls_exports,macos_tls_alu,tls_function_coverage_gs_trapsandtls_function_coverage_gs_traps_implicit.ctest --test-dir build-relinker -j 8on566fbb17ef: 369 tests, 366 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture) andpatch_advisor_unit(path with spaces, which open #10667 fixes); both failed again alone.python tools/check_conventions.py --base upstream/mainwith this title and description passed on566fbb17ef.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 rooklz on 2026-10-27T11:43:01Z.
Review: APPROVED
Reviewed exact head
566fbb17ef. Note for merge ordering: the branch contains #11488 (head9ef8b4b75a, fix commitc13d32870f) merged with main801d5b9bc1at17dffabc20, so until #11488 merges the PR diff folds both changesets; the GS-trap delta of this PR is theGsTrap::Entryfield, theentryRootscollection inScanTls, and theWriteMacOsImagerefusal.Checked line-by-line against the Windows builder (#11090):
ScanTlscollects the sameentryRootsfromCodeInstructionCollector(roots plusdataTargets, CodeInstructionCollector.cpp:570-572), so the debt line's "pointer target it takes as data" is covered. The trap'sEntryuses the identicalupper_bound(*instruction)/*entry - *instruction < Lengthguard andheader.Offset + *entry - header.MappedAddressfile-offset math as WindowsTlsBuilder.WriteMacOsImagerefuses in the same order as the WindowsgsTrapsloop: relative-branch target first (Branch enters a guest GS instruction), thenGS trap would overwrite an executable entry pointwith the entered byte's file offset.fs:accesses keep the pre-existing check order (unsupported-encoding refusal first) and now throwTLS rewrite would overwrite an executable entry pointwhere the Windows builder callscheckProtectedEntry; aninsidePrevious0x65decode is skipped by the macOS loop guards in exactly the cases Windows's0x65 && !insidePreviouscondition skips it.Verified locally (
ANYPS5_RELINKER_ONLY, macOS):macos_gs_traps,sce_code_extent,windows_tls_protected_entriesandgs_trap_entry_rootspass, full suite 361/361. The newcheck_entry_rootscases reuse the Windows protected-entry fixture for all four root kinds plus two negative roots, and the refusal offsets match the entered byte (0x214 inside the 9-byte GS load). The debt line mirrors the reviewed Windows wording accurately.(AI-assisted: yes)