fix(libkernel): refuse a foreign image only where the requested address lies on Linux #18314
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18314"
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 dantdmdarealguy on 2026-11-11T07:06:35Z.
What
guest_kernel_stack_protectfails on Linux CI now and then withGuestKernelStackProtect failed at line 276:Line 276 calls
mprotect(PROT_READ) on a fresh 256 KiB hostmmap. The test expects 0, because the range holds no guest memory. Sometimes it gets -1 with errno 22 (EINVAL).On Linux,
DoMprotectrounds the start down to the 16 KiB guest page. It then looks for loaded images over that rounded range. It refused the call as "Memory protection of a foreign image is not supported" whenever the rounded start fell inside a host library segment. When the hostmmaplands directly above a library whose last segment ends partway through a 16 KiB page, the rounded start falls in that library, even though the requested range touches no image.In every failing run I caught, the mapping started at
0x…1f000, directly abovelibc.prx's.bss, which ends at0x…1e000. The rounded start0x…1c000was inside that segment.The change tests the requested address, not the rounded start, against each segment of a host image. This changes the result in two directions:
0x…1d000, rounded start0x…1c000, call at0x…1d800.mainlet this through (mprotectreturned 0 without changing anything); it now throwsinvalid_argument, whichmprotectreports as EINVAL. This is the rule33b6d56437set out ("refuse a foreign image only where a protected range starts"). It already applied to every other address in the image, and Windows applies it to the whole image (cursor == firstonVirtualQuery, wherefirstalways lies in the same 64 KiB-aligned allocation as the address).No caller depends on the old acceptance:
sceKernelMprotect,sceKernelMtypeprotectandmprotectpassrequireMappedStart = false; onmainsuch a request returned 0 without protecting anything.sceKernelBatchMap, the memory pool exports,Apr.cppandThread.cpppasstrue; onmainthey already got EINVAL (no registered span at the start).Apr.cpp,Thread.cpp,PosixMemory.cpp:133) only pass guest mappings they created.Guest module images are still registered wherever they overlap, and host images elsewhere in the range are still skipped. Windows is unchanged.
Tests, on Linux only, as two cases of
guest_mprotect_unmapped_testsregistered withSKIP_RETURN_CODE 77.guest_mprotect_unmappedruns the existing checks and passes on its own.guest_mprotect_unmapped_host_image_abovemaps a 16 KiB host page right after the end of a host image segment whose end is not 16 KiB-aligned. It requires thatmprotectandsceKernelMprotectreturn 0 and leave the page writable.guest_mprotect_unmapped_host_image_insidecalls 0x800 bytes into a host image segment whose start is not 16 KiB-aligned and whose 16 KiB page below it holds no loaded segment. It requires EINVAL frommprotectandinvalid_argumentfromsceKernelMprotect.mainand pass with this change.Tested
Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742).
guest_mprotect_unmapped_host_image_above:mainfails 100/100 ("mprotect of host pages whose 16 KiB page starts in a host image must succeed"); this change passes 300/300, never skipped.guest_mprotect_unmapped_host_image_inside:mainfails 100/100 ("mprotect starting inside a host image must fail with EINVAL…"); this change passes 300/300, never skipped. Locally the layout it finds islibgcc_s.so.1's first segment, at a 4 KiB-aligned base with an anonymous mapping below it.guest_mprotect_unmapped: passes on both.***Skippedin ctest;guest_mprotect_unmappedstill passes.guest_kernel_stack_protect_tests, 8 in parallel, fails 2/400, 11/1000 and 25/3000 onmain, every time at line 276, and 0/3000 and 0/2000 with the change.libstarget both succeed with no warnings in the touched files.ctest -R "guest_mprotect|guest_kernel_stack_protect|guest_memory|guest_thread_stack_guard|guest_low_address_memory|guest_protection_report|patched_libraries": 15/15 pass. The host-image cases are not registered on Windows.tools/check_test_registration.pyandtools/check_conventions.py --base origin/mainpass.GuestImageSearchline into a loop for its host-only stack pages and keeps the rounded-start check, so whichever of the two merges second needs a one-line rebase. #7149 also changesDirectMemory.cppbut doesn't touchcontainsStart,GuestImageSearchor the foreign-image refusal. No open PR changesGuestMprotectUnmapped.cpp.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 PrivacyOnion on 2026-11-11T07:11:19Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
664a94ad5f:tools/check_conventions.pypassed;git diff --checkclean.guest_mprotect_unmapped_testsbuilt cleanly.guest_mprotect_unmapped):core/libs/prx/libkernel/DirectMemory/DirectMemory.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T16:46:24Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
664a94ad5f56de06450e3ecef44d7b759b9c3f31, basemain.repo_read.py: a search for the changed names, thencatranges ofDirectMemory.cpp(lines 1440–1600) and ofGuestMprotectUnmapped.cpp(lines 1–158, the whole file).Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1481tests the requested address against each segment, not the rounded start. Take a host segment that begins at0x…1d000in the same 16 KiB page as the rounded start0x…1c000, and a call at0x…1d800. The old check (search.start >= start) did not refuse it. The new check refuses it with "Memory protection of a foreign image is not supported". The description says a call starting inside a host library "still throws", which reads as no change. It is a new refusal. Say so in the description, or confirm that no caller depends on the old behaviour.core/libs/tests/GuestMprotectUnmapped.cpp:94prints a message and returns success when no loaded image end qualifies. CI would then report a pass with the regression check skipped. Failing there, or counting the skip as a failure, would make the check reliable.Not verified
sceKernelMprotectpassesrequireMappedStart = false. A caller atcore/libs/prx/libkernel/DirectMemory/Export.cpp:343does, but I did not confirm that it is the function the test calls. If it passestrue, the host-page calls in the new test returnEINVALrather than 0.TrimToRegisteredandGuestAllocations::Mutationtreat host pages that are not registered. The test depends on those pages being skipped silently, and I did not read that code.GuestAllocationsGuestPagescan changefirstthere, and I did not check how that interacts with the newrequestedfield.To get this to a good state
Nothing is required.
Optional, once:
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T02:12:43Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the code reviewed; the author-reported checks were not run in this review.
What I ran
8b6cfb6d04cc7fb3644335c5af7c65b4216615f, plus the Linux guest allocation registration implementation.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
files.jsonwas unavailable, so excluded-file details are not verified.To get this to a good state
Nothing is required.