fix(libkernel): reject mprotect of host memory that is not a guest thread stack #4169

Merged
oneandonlydean merged 1 commit from upstream-pr/4169 into main 2026-10-12 01:41:35 +00:00
oneandonlydean commented 2026-10-12 00:26:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T00:26:47Z.


What

guest_kernel_stack_protect aborts in the Windows job on main since #3788 (run 38181250683, 2 of 908 tests fail, this is one of them; Exit code 0xc0000409). @screamm bisected it in #4088: mprotect over a range that crosses a guest stack's lower bound returns 0 where the test, and #2982, expect a failure.

#3788 added a Windows path to DoMprotect that applies the native protection to any committed private region that is not a registered guest allocation. That covers much more than the case it was written for:

  • a range that crosses out of a guest stack into the host margin below it (the failing check);
  • the host margin itself, other threads' stacks and host heap pages;
  • none of it is recorded, so nothing is restored at thread exit.

This removes that path. Host memory outside the guest registry is rejected again, as before #3788, and the one kind of host memory a title legitimately holds, a live guest thread's stack, keeps the path #2982 added: a range inside [stackStart, stackEnd) is protected natively, recorded and restored at thread exit.

The case behind #3788 (#3143: 0xf8380000+0x580000, prot 0xc2, described as a thread stack) is such a range. It ends on a 1 MiB boundary, as Windows stack reservations do, and 0xc2 has no GPU bits (0x30), so the in-stack path takes it. That report predates #2982, which was merged after the branch was written; neither #3788 nor this change was run with the title.

Test changes:

  • guest_kernel_stack_protect now also requires that
    • a range inside the host margin below the stack fails;
    • the whole stack with prot 0xc2 succeeds and the stack stays writable (the #3143 shape);
    • freshly mapped host memory (VirtualAlloc / mmap) fails and keeps its protection.
  • CheckMprotectOfHostMemoryOutsideTheGuestRegistry in guest_memory is removed: it required the behaviour this change rejects.

#4113 addresses the same failure by keeping the general path and skipping it for stack allocations; it also lets the in-stack path accept GPU protection bits. This change takes the other option from #4088 and leaves the GPU check alone.

Tested

Windows was not run here; the Windows job on this pull request is the check for the failing test. The changed files compile for Windows (llvm-mingw, x86_64, objects only, not linked).

Linux (CachyOS, GCC 16, Release, CI's configure flags) on main bca7a29d:

  • full build and libs;
  • guest_kernel_stack_protect passes with the new checks, 20 of 20 repeats; the new checks also pass on main there, because the removed path is Windows-only;
  • ctest -R "agc_write|guest_kernel|guest_memory|write": 26 of 26 pass, as on main.

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 in TechnicalDebt
  • 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: none
  • AI-assisted: yes (Claude Code)

Fixes #4088.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823504005 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4169). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T00:26:47Z. --- ### What `guest_kernel_stack_protect` aborts in the Windows job on `main` since #3788 ([run 38181250683](https://github.com/boykopovar/AnyPS5/actions/runs/38181250683), 2 of 908 tests fail, this is one of them; `Exit code 0xc0000409`). @screamm bisected it in #4088: `mprotect` over a range that crosses a guest stack's lower bound returns 0 where the test, and #2982, expect a failure. #3788 added a Windows path to `DoMprotect` that applies the native protection to any committed private region that is not a registered guest allocation. That covers much more than the case it was written for: - a range that crosses out of a guest stack into the host margin below it (the failing check); - the host margin itself, other threads' stacks and host heap pages; - none of it is recorded, so nothing is restored at thread exit. This removes that path. Host memory outside the guest registry is rejected again, as before #3788, and the one kind of host memory a title legitimately holds, a live guest thread's stack, keeps the path #2982 added: a range inside `[stackStart, stackEnd)` is protected natively, recorded and restored at thread exit. The case behind #3788 (#3143: `0xf8380000+0x580000`, prot `0xc2`, described as a thread stack) is such a range. It ends on a 1 MiB boundary, as Windows stack reservations do, and `0xc2` has no GPU bits (`0x30`), so the in-stack path takes it. That report predates #2982, which was merged after the branch was written; neither #3788 nor this change was run with the title. Test changes: - `guest_kernel_stack_protect` now also requires that - a range inside the host margin below the stack fails; - the whole stack with prot `0xc2` succeeds and the stack stays writable (the #3143 shape); - freshly mapped host memory (`VirtualAlloc` / `mmap`) fails and keeps its protection. - `CheckMprotectOfHostMemoryOutsideTheGuestRegistry` in `guest_memory` is removed: it required the behaviour this change rejects. #4113 addresses the same failure by keeping the general path and skipping it for stack allocations; it also lets the in-stack path accept GPU protection bits. This change takes the other option from #4088 and leaves the GPU check alone. ### Tested Windows was not run here; the Windows job on this pull request is the check for the failing test. The changed files compile for Windows (llvm-mingw, x86_64, objects only, not linked). Linux (CachyOS, GCC 16, Release, CI's configure flags) on `main` bca7a29d: - full build and `libs`; - `guest_kernel_stack_protect` passes with the new checks, 20 of 20 repeats; the new checks also pass on `main` there, because the removed path is Windows-only; - `ctest -R "agc_write|guest_kernel|guest_memory|write"`: 26 of 26 pass, as on `main`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes (Claude Code) Fixes #4088.
oneandonlydean commented 2026-10-12 00:26:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T00:26:53Z.


@xXPBSXx this takes back the general path from #3788, so it needs your title: could you run the #3143 case (0xf8380000+0x580000, prot 0xc2) on this branch and say whether sceKernelMprotect still succeeds there? If that range isn't inside one live guest thread's stack it will throw not registered again, and I'd rather know which memory it is than keep the wide path.

@screamm could you run guest_kernel_stack_protect and guest_memory on Windows with this branch?

@Feuris this is the other option from #4088 next to your #4113; only one of the two should go in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115590300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4169#issuecomment-6115590300). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T00:26:53Z. --- @xXPBSXx this takes back the general path from #3788, so it needs your title: could you run the #3143 case (`0xf8380000+0x580000`, prot `0xc2`) on this branch and say whether `sceKernelMprotect` still succeeds there? If that range isn't inside one live guest thread's stack it will throw `not registered` again, and I'd rather know which memory it is than keep the wide path. @screamm could you run `guest_kernel_stack_protect` and `guest_memory` on Windows with this branch? @Feuris this is the other option from #4088 next to your #4113; only one of the two should go in.
Feuris commented 2026-10-12 00:35:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Feuris on 2026-10-12T00:35:57Z.


Thanks @oneandonlydean for the heads up! Happy to go with whichever approach the team prefers once @xXPBSXx confirms whether the RE4 range was indeed inside a guest stack.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115671924 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4169#issuecomment-6115671924). Originally posted by [Feuris](https://github.com/Feuris) on 2026-10-12T00:35:57Z. --- Thanks @oneandonlydean for the heads up! Happy to go with whichever approach the team prefers once @xXPBSXx confirms whether the RE4 range was indeed inside a guest stack.
github-actions[bot] commented 2026-10-12 00:37:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T00:37:13Z.


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
#4113 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp L924-927
core/libs/tests/GuestKernelStackProtect.cpp L84
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115683542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4169#issuecomment-6115683542). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T00:37:13Z. --- <!-- pr-overlap --> 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 | |---|---|---|---| | #4113 | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` [L924-927](https://github.com/boykopovar/AnyPS5/blob/d0ca839cc9d63cdfe29787476b8df46efea9741c/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp#L924-L927)<br>`core/libs/tests/GuestKernelStackProtect.cpp` [L84](https://github.com/boykopovar/AnyPS5/blob/d0ca839cc9d63cdfe29787476b8df46efea9741c/core/libs/tests/GuestKernelStackProtect.cpp#L84) | | |
oneandonlydean commented 2026-10-12 01:41:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T01:41:38Z.


Merged so the Windows job is green again (run 38188028069 showed guest_kernel_stack_protect passing with this). @xXPBSXx the question stands: if the #3143 call throws not registered again on main, please post the range and what memory it is, and I will handle that case specifically.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116249283 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4169#issuecomment-6116249283). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T01:41:38Z. --- Merged so the Windows job is green again (run 38188028069 showed `guest_kernel_stack_protect` passing with this). @xXPBSXx the question stands: if the #3143 call throws `not registered` again on `main`, please post the range and what memory it is, and I will handle that case specifically.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4169
No description provided.