Skip to content

fix(relinker): refuse GS traps that overwrite a code entry point in macOS output - #11883

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
roboterspinne:fix/relinker-macos-gs-trap-entry-roots
Nov 2, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
roboterspinne:fix/relinker-macos-gs-trap-entry-roots

Conversation

@roboterspinne

Copy link
Copy Markdown
Contributor

What

--macos replaces a guest GS memory instruction with UD2 and NOPs and, of the code entries into it, refuses only a relative branch (#7706); since #11090, --windows also refuses any other code entry inside the trapped bytes, but --macos does not. On the untouched base 17dffab (#11488 at 9ef8b4b merged with main 801d5b9), 65 48 8b 04 25 28 00 00 00 c3 at 0x210 with an entry root at 0x214 (a STT_FUNC symbol, an R_X86_64_RELATIVE pointer, a REX.W lea target or a DT_INIT_ARRAY entry) converts with --macos with exit 0, and the Mach-O holds 0f 0b 90 90 90 90 90 90 90 c3 at file offset 0x10210, so the entry at 0x214 runs NOPs and ret instead of and eax,0x28; ret; --windows refuses all four inputs with GS trap would overwrite an executable entry point (offset 0x214). maximilianfv noted this gap in the review of #11488 and suggested a follow-up PR.

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.

  • Counter-check: with MacOsImage.cpp of the untouched base 17dffab and the extended test, macos_gs_traps fails with entry-relative: expected the GS trap to be refused at offset 0x214: 0 after 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 (566fbb1) the four root inputs exit 2 with GS trap would overwrite an executable entry point (offset 0x214) and no output, the controls are unchanged, and macos_gs_traps passes in 1.1 s.
  • ctest -R 'gs_trap|macos_gs|tls_function_coverage|sce_code_extent|macos_tls|windows_tls' -j 4 on 566fbb1: 43 tests passed in 25.1 s, among them gs_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_traps and tls_function_coverage_gs_traps_implicit.
  • Full suite: ctest --test-dir build-relinker -j 8 on 566fbb1: 369 tests, 366 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture) and patch_advisor_unit (path with spaces, which open test(tools): compare quoted and resolved paths in the patch advisor test #10667 fixes); both failed again alone.
  • python tools/check_conventions.py --base upstream/main with this title and description passed on 566fbb1.
  • Not tested on Linux or macOS; CI runs the tests on all three hosts.

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: fix(relinker): protect macOS TLS code entry roots #11488
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

The macOS TLS scan asked the code collector for no entry roots, so it
replaced an fs access with jmp rel32 and NOPs even when a pointer
target taken as data lay inside it, and the relink succeeded with the
referenced bytes changed. The Windows TLS builder has refused this
since boykopovar#5890 and boykopovar#8166. The macOS scan now collects the same entry roots
and fails with "TLS rewrite would overwrite an executable entry point"
when one lies strictly inside the rewritten instruction. Every input
it refuses is already refused by --windows; --macos used to rewrite it
silently.
…acOS output

In macOS output, a guest GS memory instruction became UD2 and NOP
padding even when a function symbol, a relocated pointer, a RIP-relative
lea target or an initializer array entry pointed into it after its first
byte, so that entry ran the padding instead of the guest code. The macOS
image writer now refuses such a trap with "GS trap would overwrite an
executable entry point" at the entry's file offset, the rule the Windows
TLS builder applies since boykopovar#11090. The check runs after the existing
"Branch enters a guest GS instruction" check, so branch targets keep
their message. Outputs without such an entry are unchanged.
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10861 core/relinker/elfpatcher/src/macos/MacOsImage.cpp L294
PR 15032 core/relinker/elfpatcher/src/macos/MacOsImage.cpp L294

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 docs/dev/debt/relinker.md
PR 7173 docs/dev/debt/relinker.md
PR 7531 docs/dev/debt/relinker.md
PR 10249 core/relinker/elfpatcher/src/macos/MacOsImage.cpp
PR 10479 docs/dev/debt/relinker.md
PR 11192 docs/dev/debt/relinker.md
PR 12678 docs/dev/debt/relinker.md
PR 14212 docs/dev/debt/relinker.md

@rooklz rooklz 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 exact head 566fbb17ef. Note for merge ordering: the branch contains #11488 (head 9ef8b4b75a, fix commit c13d32870f) merged with main 801d5b9bc1 at 17dffabc20, so until #11488 merges the PR diff folds both changesets; the GS-trap delta of this PR is the GsTrap::Entry field, the entryRoots collection in ScanTls, and the WriteMacOsImage refusal.

Checked line-by-line against the Windows builder (#11090):

  • ScanTls collects the same entryRoots from CodeInstructionCollector (roots plus dataTargets, CodeInstructionCollector.cpp:570-572), so the debt line's "pointer target it takes as data" is covered. The trap's Entry uses the identical upper_bound(*instruction) / *entry - *instruction < Length guard and header.Offset + *entry - header.MappedAddress file-offset math as WindowsTlsBuilder.
  • WriteMacOsImage refuses in the same order as the Windows gsTraps loop: relative-branch target first (Branch enters a guest GS instruction), then GS trap would overwrite an executable entry point with the entered byte's file offset.
  • The rewritten fs: accesses keep the pre-existing check order (unsupported-encoding refusal first) and now throw TLS rewrite would overwrite an executable entry point where the Windows builder calls checkProtectedEntry; an insidePrevious 0x65 decode is skipped by the macOS loop guards in exactly the cases Windows's 0x65 && !insidePrevious condition skips it.

Verified locally (ANYPS5_RELINKER_ONLY, macOS): macos_gs_traps, sce_code_extent, windows_tls_protected_entries and gs_trap_entry_roots pass, full suite 361/361. The new check_entry_roots cases 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)

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