Windows: guest_kernel_stack_protect aborts since #3788, sceKernelMprotect accepts a range crossing a guest stack's lower bound #4088
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 screamm on 2026-10-11T22:59:10Z.
Environment: Windows 11 Pro 10.0.26220, WinLibs GCC 15.2.0 r7, Ninja, Release,
BUILD_TESTING=ON;main1a7b96b5bplus #4021 (needed forlibkernelto compile).Reproduction
First bad merge:
dc70a66cb, #3788 "fix(libkernel): allow sceKernelMprotect on committed host memory outside the guest registry" by @xXPBSXx. Its parent3929c21fbpasses; the merge fails.Failure
CTest reports
Exit code 0xc0000409. The test prints:The test's
Requirecalls a barestd::abort()with no message. I rebuilt the test source withRequireprinting its line, plus aVirtualQueryof the range. Output:The abort site is the last check in
Worker,core/libs/tests/GuestKernelStackProtect.cpp:84. A protection change on[start - 16 KiB, start + 16 KiB), which crosses the stack's lower bound, must fail. It now returns 0.Analysis
On Windows, a guest thread runs on its host thread's stack.
_beginthreadexis given gueststackSize + HOST_STACK_MARGIN(1 MiB), and the stack is committed as oneMEM_PRIVATE/PAGE_READWRITEregion. The guest stack is the upper part of that region, and the 1 MiB below it is the host margin. Above, the stack is0xfc00100000..0xfc00200000; the committed region starts at allocation base0xfc00000000.#2982 (@TheAggie) let
DoMprotectchange pages inside a live guest stack directly. Its commit message states: "Ranges that cross the stack's bounds are still rejected." The test pins that rule.#3788 added a Windows fast path in
DoMprotect(core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:924):The crossing range skips the stack branch because it is not inside
[stackStart, stackEnd). It is not a registered guest allocation either. Before #3788 it fell intomutation.Protect, which threw "not registered", somprotect_nid_postfixreturned -1. NowVirtualQueryatstart - 16 KiBsees a committed private region of 0x104000 bytes, which is at least the requested size, so the fast path callsVirtualProtectand returns 0.This only affects Windows: the fast path is inside
#ifdef _WIN32.The test failure points to a wider problem. The fast path lets a guest change the protection of any committed private host memory that is not in the guest registry. That includes the 1 MiB host-stack margin below every guest stack (
PROT_NONEthere would crash host code that runs deeper on that thread), other threads' stacks, and host heap pages. It also returns beforeRecordProtectionandNoteGuestStackProtection, so those changes are not recorded and are not restored when the thread exits.The motivating case in #3143 (RE4 PPSA07411,
0xf8380000+0x580000, prot0xc2) was described as a thread stack. That range skipped #2982's stack branch only because its protection had the GPU bits set ((prot & GuestProtGpuReadWrite) == 0is false).Possible minimal fixes (not built or tested)
GuestThreadStack(first, ...)orGuestThreadStack(end - 1, ...)is true. The test's crossing range would then hitGuestThreadStack(end - 1)and fall back to the rejecting path. A range lying entirely inside the host margin would still pass, so a stronger check comparesmemory.AllocationBasewith the allocation base of eachliveStacksentry.Option 2 needs confirmation that the RE4 range lies inside a single guest stack. That is not settled by #3143.
Found and bisected with Claude Code assistance (five full Windows builds, each with #4021 applied to make
libkernelcompile); reviewed before posting.