fix(libkernel): reject mprotect of host memory that is not a guest thread stack #4169
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4169"
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 oneandonlydean on 2026-10-12T00:26:47Z.
What
guest_kernel_stack_protectaborts in the Windows job onmainsince #3788 (run 38181250683, 2 of 908 tests fail, this is one of them;Exit code 0xc0000409). @screamm bisected it in #4088:mprotectover 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
DoMprotectthat 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: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, prot0xc2, described as a thread stack) is such a range. It ends on a 1 MiB boundary, as Windows stack reservations do, and0xc2has 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_protectnow also requires that0xc2succeeds and the stack stays writable (the #3143 shape);VirtualAlloc/mmap) fails and keeps its protection.CheckMprotectOfHostMemoryOutsideTheGuestRegistryinguest_memoryis 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
mainbca7a29d:libs;guest_kernel_stack_protectpasses with the new checks, 20 of 20 repeats; the new checks also pass onmainthere, because the removed path is Windows-only;ctest -R "agc_write|guest_kernel|guest_memory|write": 26 of 26 pass, as onmain.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Fixes #4088.
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, prot0xc2) on this branch and say whethersceKernelMprotectstill succeeds there? If that range isn't inside one live guest thread's stack it will thrownot registeredagain, and I'd rather know which memory it is than keep the wide path.@screamm could you run
guest_kernel_stack_protectandguest_memoryon Windows with this branch?@Feuris this is the other option from #4088 next to your #4113; only one of the two should go in.
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.
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.
main)core/libs/prx/libkernel/DirectMemory/DirectMemory.cppL924-927core/libs/tests/GuestKernelStackProtect.cppL84Imported 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_protectpassing with this). @xXPBSXx the question stands: if the #3143 call throwsnot registeredagain onmain, please post the range and what memory it is, and I will handle that case specifically.