Skip to content

fix(relinker): trap inline SDK mount syscall - #8729

Closed
hyvvs wants to merge 1 commit into
boykopovar:mainfrom
hyvvs:fix/relinker-sdk-mount-stub
Closed

hyvvs wants to merge 1 commit into
boykopovar:mainfrom
hyvvs:fix/relinker-sdk-mount-stub

Conversation

@hyvvs

@hyvvs hyvvs commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What

Recognize the exact SYS_mount inline syscall sequence observed in public ps5-payload-dev ftpsrv v0.21.1 and websrv v0.35 payloads when --trap-sdk-syscall-stubs is explicitly enabled (issue #8624).

The scanner keeps its decoded-instruction-boundary and full-sequence checks. The trapped syscall is still replaced with UD2; this does not implement mount or claim that either payload can execute successfully. This patch recognizes only the inline SYS_mount sequence. As reported in #8624, ftpsrv-ps5 and websrv-ps5 also contain nmount and unmount wrappers; the mount-only change still fails conversion at nmount (ftpsrv: 0x70ec; websrv: 0xaebc). These remaining wrappers need a separate design decision and implementation if the option is broadened.

Extend the existing SDK inline-syscall fixture and near-match mutation checks, and record the remaining functional gap in the relinker technical debt.

This is a design proposal for maintainer review: #8624 explicitly leaves open whether SYS_mount belongs in the opt-in trap list. No unrelated syscall patterns are accepted.

Tested

  • Windows NT 10.0.26200, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja 1.13.2, CMake 4.4.4, Release relinker-only build.
  • Confirmed the new regression fixture fails on the original implementation at code offset 0x123413.
  • cmake --build build-relinker --parallel 6: passed.
  • ctest --test-dir build-relinker --output-on-failure -j 4 -R '^(sdk_mman_syscall|relinker_conversion|cli_args|elf_reader_bounds|call_site_index)$': 5/5 passed.
  • sdk_mman_syscall_tests: 1,285 near-match rejections passed.
  • python tools/check_conventions.py --base upstream/main: passed.
  • The baseline full CTest run (before this change) reported 203 passed / 27 failed out of 230; those failures were Windows security blocks (WinError 225) launching the relinker executable, not demonstrated code failures. No full post-change suite claim.
  • End-to-end conversion of the published payload ELFs and GPU/title execution were not tested.

Checklist

@MarcZX10

Copy link
Copy Markdown
Contributor

The exact-byte matcher and fixture look deliberately scoped: it requires the decoded sequence start, checks the full sequence within the code-section bound, and retains the near-match mutations. One design point before approval: #8624 leaves SYS_mount inclusion as an explicit contract decision, while the current option documentation describes trapping mmap/mprotect and rejecting other syscall instructions. If maintainers decide to broaden the opt-in, please update that user-facing description as part of the change; otherwise the implementation exceeds the documented scope. I only reviewed the patch and reported Windows test matrix here; the automatic checks are policy checks, with no hosted build job.

@hyvvs

hyvvs commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Ty for reviewing!

Agreed that including SYS_mount should be a maintainer decision,

I'll keep the PR as a draft pending that decision. If maintainers agree with the extension, I'll update the user-facing option description to include SYS_mount and clarify that it is trapped rather than implemented.

The relinker-only Windows build and focused regression tests passed locally. A hosted Windows CI build hasn't been run yet.

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 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 9332 docs/dev/debt/relinker.md L11-12

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 7918 docs/dev/debt/relinker.md
PR 8042 core/relinker/relinker/src/analysis/SyscallScanner.cpp
PR 8076 docs/dev/debt/relinker.md
PR 9408 docs/dev/debt/relinker.md

@MarcZX10

Copy link
Copy Markdown
Contributor

New exact-payload data was added to #8624 after this PR opened: recognizing only the mount wrapper still stops ftpsrv at nmount (0x70ec) and websrv at nmount (0xaebc); both also contain inline unmount wrappers. The three-wrapper change converts both payloads, while the mount-only version does not. Your body correctly says this PR does not claim successful execution, but Fixes #8624 would still close an issue whose reported conversion gap remains. Could you change that relation to a non-closing reference and state that this adds one recognized wrapper while nmount/unmount remain blockers (or coordinate with the measured three-wrapper follow-up if maintainers approve broadening the option)?

@hyvvs

hyvvs commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

I've updated the PR description to remove the closing reference to #8624 and clarify that this change only handles SYS_mount, while nmount and unmount remain blockers.

I'll keep the PR as a draft pending the maintainers' decision on whether to expand the supported wrappers.

@MarcZX10

Copy link
Copy Markdown
Contributor

I am reviewing #9549. Its trap-condition change edits the line where this PR renames IsSdkMmanSyscallStub to IsSdkInlineSyscallStub, so the two changes need an agreed landing order and a refreshed Depends on entry if one must land first. Could you coordinate with @DavidSamir on the integration order? #9549 currently calls the old name and would need that symbol updated if this PR merges first.

@DavidSamir

Copy link
Copy Markdown
Contributor

@hyvvs, a coordination question on landing order, as @MarcZX10 asked on #9549. #9549 turns the trap condition you rename in (IsSdkMmanSyscallStub to IsSdkInlineSyscallStub, SyscallScanner.cpp L293-294) into const bool sdkStub = ..., so whichever of the two merges second needs that one helper name updated; there is no other overlap.

You have kept this PR as a draft pending a decision on nmount and unmount, so I propose #9549 lands first and your PR updates the call inside sdkStub when it is rebased (one word). #9549 does not need yours, so it keeps Depends on: none. If you would rather land first, say so here and I will rebase #9549 onto yours and list #8729 under Depends on.

@hyvvs

hyvvs commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@DavidSamir @MarcZX10 Thanks for flagging the overlap.

I agree that #9549 should land first, since #8729 is still a draft pending the maintainer decision on the mount/nmount/unmount syscall wrappers.

Once #9549 merges, I'll rebase #8729 onto the updated main, preserve the new sdkStub diagnostic behavior, update the helper call to IsSdkInlineSyscallStub, and rerun the relevant tests and checks.

Happy to proceed with this

@hyvvs

hyvvs commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Just checking whether maintainers have a direction on extending --trap-sdk-syscall-stubs to the mount/nmount/unmount family.

The conversion measurements and remaining limitations are documented in #8624. I'm happy to implement the full family with the additional tests and documentation if that's the preferred direction.

Since this is my first contribution and the repository limits me to one open PR, I'd also appreciate knowing if this approach isn't something maintainers want to accept. In that case, I'll close the draft and focus on another contribution.

Thanks!

@hyvvs

hyvvs commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing this draft to free my first-contributor PR slot while the filesystem syscall trap scope remains undecided. The implementation, exact-match tests and measurements are preserved in the branch and in #8624. No runtime filesystem behavior is claimed; this can be revisited if maintainers later approve the opt-in scope. Thanks to everyone who provided measurements and feedback.

@hyvvs hyvvs closed this Oct 24, 2026
@kaiffeetasse

Copy link
Copy Markdown
Contributor

Since the maintainers' question is only about scope, here is the implementation so it is ready to apply if they extend the opt-in - no second PR from me.

Measured on relinker-only builds: recognising mount alone converts 10 of 22 PS5 assets in the public ps5-payload-dev catalog; the three __syscall6 wrappers together convert 22 of 22 (both payloads from #8624 included). The three wrappers do not share a byte sequence apart from the number, so each needs its own pattern with the number immediate masked.

+static constexpr std::array<std::uint8_t, 46> SDK_MOUNT_INLINE{
+    0x55, 0x48, 0x89, 0xE5, 0x41, 0x56, 0x53, 0x48, 0x63, 0xD2, 0x31, 0xDB,
+    0xB8, 0x00, 0x00, 0x00, 0x00, 0x49, 0x89, 0xCA, 0x45, 0x31, 0xC0, 0x45, 0x31, 0xC9,
+    0x0F, 0x05, 0x0F, 0x93, 0xC1, 0x49, 0x89, 0xC6, 0x49, 0xF7, 0xDE, 0x84, 0xC9,
+    0x4C, 0x0F, 0x45, 0xF0, 0x4D, 0x85, 0xF6,
+};
+
+static constexpr std::array<std::uint8_t, 48> SDK_NMOUNT_INLINE{
+    0x55, 0x48, 0x89, 0xE5, 0x41, 0x56, 0x53, 0x89, 0xF6, 0x48, 0x63, 0xD2, 0x31, 0xDB,
+    0xB8, 0x00, 0x00, 0x00, 0x00, 0x45, 0x31, 0xD2, 0x45, 0x31, 0xC0, 0x45, 0x31, 0xC9,
+    0x0F, 0x05, 0x0F, 0x93, 0xC1, 0x49, 0x89, 0xC6, 0x49, 0xF7, 0xDE, 0x84, 0xC9,
+    0x4C, 0x0F, 0x45, 0xF0, 0x4D, 0x85, 0xF6,
+};
+
+static constexpr std::array<std::uint8_t, 48> SDK_UNMOUNT_INLINE{
+    0x55, 0x48, 0x89, 0xE5, 0x41, 0x56, 0x53, 0x48, 0x63, 0xF6, 0x31, 0xDB,
+    0xB8, 0x00, 0x00, 0x00, 0x00, 0x31, 0xD2, 0x45, 0x31, 0xD2, 0x45, 0x31, 0xC0, 0x45, 0x31, 0xC9,
+    0x0F, 0x05, 0x0F, 0x93, 0xC1, 0x49, 0x89, 0xC6, 0x49, 0xF7, 0xDE, 0x84, 0xC9,
+    0x4C, 0x0F, 0x45, 0xF0, 0x4D, 0x85, 0xF6,
+};

MatchesSdkInlineStub gets two defaulted parameters and compares byte-wise, skipping the masked field:

-static bool MatchesSdkInlineStub(..., FileByteOffset codeOffset) {
+static bool MatchesSdkInlineStub(..., FileByteOffset codeOffset, std::size_t maskOffset = TSize, std::size_t maskLength = 0) {
     if (syscallOffset < beforeSyscall || syscallOffset > limit) return false;
     const auto start = syscallOffset - beforeSyscall;
-    if (pattern.size() > limit - start || !std::equal(...)) return false;
+    if (pattern.size() > limit - start) return false;
+    for (std::size_t i = 0; i < pattern.size(); ++i) {
+        if (i >= maskOffset && i < maskOffset + maskLength) continue;
+        if (code[start + i] != pattern[i]) return false;
+    }
     return recent.Contains(codeOffset + start);
 }

and the three call sites (the mount masks b8 15 at offset 13, nmount b8 7a 01 at 15, unmount b8 16 at 13, four bytes each):

+        || MatchesSdkInlineStub(code, syscallOffset, limit, SDK_MOUNT_INLINE, 26, recent, codeOffset, 13, 4)
+        || MatchesSdkInlineStub(code, syscallOffset, limit, SDK_NMOUNT_INLINE, 28, recent, codeOffset, 15, 4)
+        || MatchesSdkInlineStub(code, syscallOffset, limit, SDK_UNMOUNT_INLINE, 28, recent, codeOffset, 13, 4);

Fixtures: struct Fixture gains std::size_t maskOffset = 0; std::size_t maskLength = 0;; add the same three byte sequences with syscall 26 / 28 / 28 and masks 13/4, 15/4, 13/4, plus one case per fixture that changes the masked bytes only (0x16, 0x7b 01, 0x17) and still has to be trapped, which is what makes the masking testable.

Nothing else changes: the decoded-instruction-boundary check, the full-sequence requirement, the UD2 substitution and the refusal message all stay as they are. If you prefer to keep the PR mount-only, the two extra arrays and their three call sites can be lifted verbatim later.

AI-assisted: yes

kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Oct 26, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Oct 27, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Oct 28, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Oct 29, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Oct 30, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
kaiffeetasse pushed a commit to kaiffeetasse/AnyPS5 that referenced this pull request Nov 1, 2026
Recognize the inlined __syscall6 wrappers of ps5-payload-dev SDK libc/mount.c (mount, nmount, unmount) under --trap-sdk-syscall-stubs, with the four-byte syscall number immediate masked so an SDK build that renumbers the call still matches. The three wrappers differ in argument preparation, so each sequence is matched separately.

Observed in ftpsrv v0.21.1 (0x709a, 0x70ec, 0x713c) and websrv v0.35 (0xae6a, 0xaebc, 0xaf0c). Related: issue boykopovar#8624, open draft PR boykopovar#8729 covers mount only.
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