fix(libkernel): skip the host-only pages around a guest thread stack in mprotect on Linux #15040
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15040"
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 Ragbie on 2026-11-02T11:42:45Z.
What
On Linux,
mprotectandsceKernelMprotectover the host-only pages around a guest thread's stack now return 0 and leave those pages unchanged. Before, they returned -1 withEACCES. ThesceKernelMprotectdebt entry recorded the gap ("On Linux a range over the host-only pages around a guest thread's stack still fails withEACCES, where Windows returns 0"), and the #9882 discussion left the Linux return of the straddling range as a follow-up.The same debt entry records the PS5 (firmware 13.60) measurement:
mprotectandsceKernelMprotectchange the mapped parts of a range, skip its gaps and return 0. On FreeBSD 11.4, libthr places the stack guard in the stack's own mapping (thr_stack.c), so on the console the pages around a thread stack are guest memory.On Linux a guest thread's stack is a guest allocation (
AllocateGuestStack) that the host thread runs on. The guard, the spare host stack below the guest stack and the host's frames above it are host-only.9b6e7f66c4keeps the guest from changing them by refusing any range that touches them. This PR treats them as gaps instead:GuestThreadStackHostRangesreturns the host-only ranges a range meets.GuestThreadStackHostRangeOverlapsis built on it.DoMprotectcuts the request around those ranges and protects the parts beside them.EINVALwhen the range starts in a skipped page, as for an unmapped start.The debt entry now states what is left: those pages are skipped where the console's guard takes the protection.
Tested
guest_kernel_stack_protect:-1).0x11) leaves the page below the stack unchanged.0x11protect from that page into the stack splits into two general-path parts around the host guard. The pin waiter hook records whether the earlier page already changed when the pin is found, then releases the pin. The test requires the earlier page unchanged at that point, and both parts protected after the call returns 0.Linux (Ubuntu 24.04 container, GCC 13.3.0):
GuestKernelStackProtect failed at line 235(the host guard page returns -1).4c52e758e0) with the new test:GuestKernelStackProtect failed at line 254. The waiter ran (line 253 passed), but the earlier part had already been protected.28a71b481e: of the 239 tests matchingmprotect|memory|stack|pthread|mmap|thread|mtype|apr|ampr|flexible|pool|mlock|protectionoutsideagc, all pass.guest_mmap_executableskips itself.Linux ASan + UBSan (flags of the Sanitizers workflow, same container, ASLR off with
setarch -R),guest_kernel_stack_protect, 30 runs each:9bda0c0326: 30 of 30 fail at line 245. The worker stack sits at the start of the guest arena, so no guest page can be mapped below it.9bda0c0326: 30 of 30 pass. The test maps a guest page before it starts the worker, so the worker stack lies above the arena start, and unmaps that page before the check.Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with
libs, base28a71b481e:ctest:99% tests passed, 8 tests failed out of 6249. None of them is caused by this branch:performance_bisectfails withWinError 1314, because this user cannot create symbolic links.agc_driver_upload_cache_read_only_flushis not built:UploadCacheReadOnlyFlush.cppcallssetenv, whichmaindoes not declare on Windows (#19899, #22373 and #22634 fix it).agc_driver_image_plane_address_on_line,agc_driver_image_sample_lod_clampandagc_driver_stencil_plane_memoryalso fail on the base commit.agc_driver_image_sample_lod_clamp_shader_laneandagc_driver_msaa_dcc_unwatchalso fail with the base commit's libraries.agc_driver_store_over_untrackedis intermittent on the base commit too: it fails 26 of 40 runs there and 26 of 40 and 30 of 40 on this branch, with the same message.macOS: not tested.
GuestThreadStackHostRangesreturns no ranges there, soDoMprotectkeeps its path; the GPU-bit case runs there as on Windows.The three-page read-only case is Linux-only because Windows skips the whole straddling range: the host thread stack is not in the guest allocation registry, so the stack part is not protected either. That is a separate Windows gap and goes in its own PR.
Overlaps: #7149 and #7631 also change
DoMprotect; both conflict withmainalready, and a trial merge with this branch conflicts in the same files only. #14466, #9684 and #7532 merge with this branch without conflicts (git merge-tree). #16401 adds#include <vector>at the same line ofPthread.hpp; whichever lands second keeps both includes, so either order works. #18314 is merged: the parts now share the address the caller asked for, so a foreign image is still refused only where that address lies.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T11:45:09Z.
Review: CHANGES_REQUESTED
I found a pin-safety regression in the split protection path. The existing single-range path gathers every registered span and calls
RequireUnpinnedon all of them before the first protection change. Here each part is sent throughProtectGuestRangeseparately, and each recursive call checks and then protects its own spans. If a later part beyond a host-stack gap is pinned, the call can fail after an earlier part has already changed protection. Please preflight all surviving spans across every part before applying any protection, and add a focused regression for a pinned later span.The affected call is
core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1463at head4c52e758e0b9f2f6c39833871cc1bb6d4bdab525.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T21:25:57Z.
Review: APPROVED
Reviewed — looks good
The reviewed change is consistent with its stated goal and I found no blocking defects in the changed code.
What I ran
Reviewed head
9bda0c03264d407581859e0dc552b448b5df9098against basemainusingrepo_read.pyto inspect the prepared diff and targeted source context. No shell commands, builds, or tests were run in this review.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Runtime behavior and test results were not independently verified. The author reports Linux and Windows test runs; macOS and physical-console behavior were not tested. The changed files reviewed were
core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp,core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/tests/GuestKernelStackProtect.cpp, anddocs/dev/debt/libkernel.md. No changed files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T12:42:30Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; I found no verified blocking defect in the changed code.
What I ran
Reviewed head
e9b10a8e1d7d684acdb166c30a0cda15686b83dc. I inspected the prepared diff and all five changed files listed as reviewed in the manifest. No commands or tests were run in this review; test results below are author-reported.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not run tests or builds and did not independently verify the author-reported Linux, Windows, or firmware results. No changed files were excluded:
DirectMemory.cpp,Pthread.hpp,Thread.cpp,GuestKernelStackProtect.cpp, andlibkernel.mdwere all read;files.jsonwas not available in the prepared review listing, so its excluded-file list was not independently checked.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:16:33Z.
Review: COMMENTED
Checked the branch sources and built the focused tests against the existing Linux runtime.
GuestKernelStackProtectpassed.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.