fix(libkernel): protect ranges with unmapped pages as measured on a PS5 #9882
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9882"
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 orezraey on 2026-10-22T20:30:33Z.
What
sceKernelMprotect,mprotectandsceKernelMtypeprotectover ranges with unmapped pages, measured on a retail PS5 (firmware 13.60) with two homebrew probes. The sources and console logs are in this gist (mproton flexible memory,mtypeon direct memory).On the console the three calls change the mapped parts of the range, skip the gaps and return 0 in every case measured:
sceKernelMtypeprotectretypes only the mapped pages, and the physical pages behind an unmapped virtual page keep their type. Before this change, AnyPS5 returned, for a range that started in a gap or was wholly unmapped,SCE_KERNEL_ERROR_EINVAL(mprotect: -1 withEINVAL). The debt entry said this was kept only because a test pinned it. Those three calls now pass the start check by, and the debt entry records the measurement.Before, the guest module image a range touched was found from its first page only. Since a range may now start in a gap, every image the range overlaps is registered, on Windows from each region's
VirtualQueryand on Linux from eachPT_LOADsegment that overlaps. A host image is still refused where the range starts, as the debt entry for foreign images describes, and is skipped later in the range, as before.DoMprotectandDoMtypeprotectkeep their check by default, so the batch map and memory pool protect operations, which were not measured, still returnEINVALfor an unmapped start.One console difference stays open and is recorded in the debt entry: on the console,
sceKernelMtypeprotectover a range with any flexible page returns0x8002002dand changes nothing; it was measured with a direct page followed by a flexible one. Here it protects the range and returns 0, because flexible memory mapped throughmmapisn't tracked.Tests:
GuestMprotectUnmappednow expects 0 for an unmapped start and for a wholly unmapped range. It also checks a direct-memorysceKernelMtypeprotectover an unmapped first page: the protection and type of the mapped pages, and the type of the page behind the gap.GuestMemoryexpects 0 frommprotecton a fully unmapped page.GuestKernelStackProtect, protecting AnyPS5's own host pages now returns 0 and leaves them writable, as for any memory the guest doesn't have mapped. The range that straddles the bottom of a guest thread stack must leave the page below the stack unchanged on both systems, and returns 0 on Windows. Its Linux return value is not checked; on Linux the range overlaps the host guard page. The one-page check two pages below the stack was removed, because its result depends on whatever the host mapped there.Tested
main(d5140548fc, with #11005; thelibkernel.mdconflict keeps this PR's rewrittensceKernelMprotect, sceKernelMtypeprotectentry and #11005'ssceKernelMprotect protection bitsentry):e2d2ff05c7.guest_mprotect_unmapped,guest_memory,guest_kernel_stack_protect,guest_file_vector_memory,guest_mlock,guest_memory_pool_statsandguest_protection_reportpass three runs in a row on Windows 11 (MinGW, GCC 15.2) and on Linux (Arch on WSL2, GCC, Release).check_conventionspasses.main(4a6005955f, with #10898; theDoMprotectconflict keeps this PR's mapped-start check only for the calls that require it and compares it with main'spointer, which the macOS trim may move):2d0b949dd4.guest_mprotect_unmapped,guest_memory,guest_kernel_stack_protect,guest_file_vector_memory,guest_apr_read_stackandguest_mlockpass three runs in a row on Windows (MinGW); the other five pass three runs in a row on Linux (WSL, GCC), whereguest_apr_read_stackis not registered.check_conventionspasses.Windows 11, MinGW (winlibs).
guest_mprotect_unmapped,guest_kernel_stack_protectandguest_memorypass three runs in a row.mtypeand the files that callmprotect(guest_apr_read_stack,guest_file_vector_memoryand the AGC memory tests).main1da03c6f34(merging main's protection-bit mask in POSIXmprotect): the 19 guest memory, AMPR, APR, flexible memory, stack protection andmprotecttests pass three runs in a row.tools/check_conventions.pypasses.guest_mprotect_unmapped,guest_kernel_stack_protectandguest_memorypass three runs in a row.guest_module_dir, which first failed on a stalerelinker.exefrom an older build.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)libkernel.mdentry on the line next to the one this PR rewrites (merge order only); #11005 and #10706 have merged; #12854 (slopgobblercobbler) addsmincorechecks on theGuestMemory.cpplines next to themprotectexpectation this PR changes (merge order only)Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:30:12Z.
Review: APPROVED
Checked against main
87666fc5d6, FreeBSD release/11.4.0sys/vm/vm_map.candvm_mmap.c, and the gist logs:mprot.console.logandmtype.console.log, including the type of the physical page behind a gap staying 0, and the flexible0x8002002dranges (mixed, and all-flexible ones such asno_gap) changing nothing. The new tests follow the logs.vm_map_protect(vm_map.c:1999) takes the next entry when the start is unmapped and has no hole check, andkern_mprotectreturns 0 forKERN_SUCCESS, so unmapped starts, gaps and fully unmapped ranges all return 0 there too.git merge-treeagainst current main is clean.past_endthe probes query only the two mapped pages, so the log doesn't show that the page past the mapping was unmapped or left unchanged. Not a blocker; maybe worth a note if you rerun the probes.DoMprotect, it seems to reach the existing host-stackEACCESthrow, while Windows returns 0. I couldn't run it on Linux; if that's right, a short line in the debt file would make the difference visible.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.