fix(libkernel): protect ranges with unmapped pages as measured on a PS5 #9882

Merged
orezraey merged 9 commits from upstream-pr/9882 into main 2026-11-02 07:59:06 +00:00
orezraey commented 2026-10-22 20:30:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-22T20:30:33Z.


What

sceKernelMprotect, mprotect and sceKernelMtypeprotect over 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 (mprot on flexible memory, mtype on 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:

  • a gap in the middle;
  • a gap at the end;
  • a range that runs past the mapping;
  • a range that starts in a gap, the first page included;
  • a range that covers only a gap;
  • a range that is wholly unmapped.

sceKernelMtypeprotect retypes 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 with EINVAL). 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 VirtualQuery and on Linux from each PT_LOAD segment 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. DoMprotect and DoMtypeprotect keep their check by default, so the batch map and memory pool protect operations, which were not measured, still return EINVAL for an unmapped start.

One console difference stays open and is recorded in the debt entry: on the console, sceKernelMtypeprotect over a range with any flexible page returns 0x8002002d and 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 through mmap isn't tracked.

Tests:

  • GuestMprotectUnmapped now expects 0 for an unmapped start and for a wholly unmapped range. It also checks a direct-memory sceKernelMtypeprotect over an unmapped first page: the protection and type of the mapped pages, and the type of the page behind the gap.
  • GuestMemory expects 0 from mprotect on a fully unmapped page.
  • In 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

  • Head after merging main (d5140548fc, with #11005; the libkernel.md conflict keeps this PR's rewritten sceKernelMprotect, sceKernelMtypeprotect entry and #11005's sceKernelMprotect protection bits entry): e2d2ff05c7. guest_mprotect_unmapped, guest_memory, guest_kernel_stack_protect, guest_file_vector_memory, guest_mlock, guest_memory_pool_stats and guest_protection_report pass three runs in a row on Windows 11 (MinGW, GCC 15.2) and on Linux (Arch on WSL2, GCC, Release). check_conventions passes.
  • Head after merging main (4a6005955f, with #10898; the DoMprotect conflict keeps this PR's mapped-start check only for the calls that require it and compares it with main's pointer, which the macOS trim may move): 2d0b949dd4. guest_mprotect_unmapped, guest_memory, guest_kernel_stack_protect, guest_file_vector_memory, guest_apr_read_stack and guest_mlock pass three runs in a row on Windows (MinGW); the other five pass three runs in a row on Linux (WSL, GCC), where guest_apr_read_stack is not registered. check_conventions passes.
    Windows 11, MinGW (winlibs).
  • guest_mprotect_unmapped, guest_kernel_stack_protect and guest_memory pass three runs in a row.
  • So do the other tests whose names match memory, stack, mmap, shm, JIT, pool, batch, mtype and the files that call mprotect (guest_apr_read_stack, guest_file_vector_memory and the AGC memory tests).
  • Rebased on main 1da03c6f34 (merging main's protection-bit mask in POSIX mprotect): the 19 guest memory, AMPR, APR, flexible memory, stack protection and mprotect tests pass three runs in a row.
  • tools/check_conventions.py passes.
  • Linux (Arch on WSL2, GCC, Release, this branch before the rebase): guest_mprotect_unmapped, guest_kernel_stack_protect and guest_memory pass three runs in a row.
  • All 65 guest memory, stack, protection and module tests pass after a full rebuild, including guest_module_dir, which first failed on a stale relinker.exe from an older build.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #9947 (klosixgaming-arch) adds a libkernel.md entry on the line next to the one this PR rewrites (merge order only); #11005 and #10706 have merged; #12854 (slopgobblercobbler) adds mincore checks on the GuestMemory.cpp lines next to the mprotect expectation this PR changes (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4973704701 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9882). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-22T20:30:33Z. --- ### What `sceKernelMprotect`, `mprotect` and `sceKernelMtypeprotect` over 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](https://gist.github.com/orezraey/810385b1955e6c721b8d7eda2d629791) (`mprot` on flexible memory, `mtype` on 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: - a gap in the middle; - a gap at the end; - a range that runs past the mapping; - a range that starts in a gap, the first page included; - a range that covers only a gap; - a range that is wholly unmapped. `sceKernelMtypeprotect` retypes 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 with `EINVAL`). 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 `VirtualQuery` and on Linux from each `PT_LOAD` segment 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. `DoMprotect` and `DoMtypeprotect` keep their check by default, so the batch map and memory pool protect operations, which were not measured, still return `EINVAL` for an unmapped start. One console difference stays open and is recorded in the debt entry: on the console, `sceKernelMtypeprotect` over a range with any flexible page returns `0x8002002d` and 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 through `mmap` isn't tracked. Tests: - `GuestMprotectUnmapped` now expects 0 for an unmapped start and for a wholly unmapped range. It also checks a direct-memory `sceKernelMtypeprotect` over an unmapped first page: the protection and type of the mapped pages, and the type of the page behind the gap. - `GuestMemory` expects 0 from `mprotect` on a fully unmapped page. - In `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 - Head after merging `main` (`d5140548fc`, with #11005; the `libkernel.md` conflict keeps this PR's rewritten `sceKernelMprotect, sceKernelMtypeprotect` entry and #11005's `sceKernelMprotect protection bits` entry): `e2d2ff05c7`. `guest_mprotect_unmapped`, `guest_memory`, `guest_kernel_stack_protect`, `guest_file_vector_memory`, `guest_mlock`, `guest_memory_pool_stats` and `guest_protection_report` pass three runs in a row on Windows 11 (MinGW, GCC 15.2) and on Linux (Arch on WSL2, GCC, Release). `check_conventions` passes. - Head after merging `main` (`4a6005955f`, with #10898; the `DoMprotect` conflict keeps this PR's mapped-start check only for the calls that require it and compares it with main's `pointer`, which the macOS trim may move): `2d0b949dd4`. `guest_mprotect_unmapped`, `guest_memory`, `guest_kernel_stack_protect`, `guest_file_vector_memory`, `guest_apr_read_stack` and `guest_mlock` pass three runs in a row on Windows (MinGW); the other five pass three runs in a row on Linux (WSL, GCC), where `guest_apr_read_stack` is not registered. `check_conventions` passes. Windows 11, MinGW (winlibs). - `guest_mprotect_unmapped`, `guest_kernel_stack_protect` and `guest_memory` pass three runs in a row. - So do the other tests whose names match memory, stack, mmap, shm, JIT, pool, batch, `mtype` and the files that call `mprotect` (`guest_apr_read_stack`, `guest_file_vector_memory` and the AGC memory tests). - Rebased on `main` 1da03c6f34 (merging main's protection-bit mask in POSIX `mprotect`): the 19 guest memory, AMPR, APR, flexible memory, stack protection and `mprotect` tests pass three runs in a row. - `tools/check_conventions.py` passes. - Linux (Arch on WSL2, GCC, Release, this branch before the rebase): `guest_mprotect_unmapped`, `guest_kernel_stack_protect` and `guest_memory` pass three runs in a row. - All 65 guest memory, stack, protection and module tests pass after a full rebuild, including `guest_module_dir`, which first failed on a stale `relinker.exe` from an older build. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #9947 (klosixgaming-arch) adds a `libkernel.md` entry on the line next to the one this PR rewrites (merge order only); #11005 and #10706 have merged; #12854 (slopgobblercobbler) adds `mincore` checks on the `GuestMemory.cpp` lines next to the `mprotect` expectation this PR changes (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 12:30:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:30:12Z.

Review: APPROVED


Checked against main 87666fc5d6, FreeBSD release/11.4.0 sys/vm/vm_map.c and vm_mmap.c, and the gist logs:

  • Every case listed in the description matches mprot.console.log and mtype.console.log, including the type of the physical page behind a gap staying 0, and the flexible 0x8002002d ranges (mixed, and all-flexible ones such as no_gap) changing nothing. The new tests follow the logs.
  • FreeBSD 11.4 agrees: vm_map_protect (vm_map.c:1999) takes the next entry when the start is unmapped and has no hole check, and kern_mprotect returns 0 for KERN_SUCCESS, so unmapped starts, gaps and fully unmapped ranges all return 0 there too.
  • git merge-tree against current main is clean.
  • Minor: in past_end the 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.
  • Optional: on Linux the straddling stack range now goes unchecked. From reading DoMprotect, it seems to reach the existing host-stack EACCES throw, 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638240423 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9882#pullrequestreview-5638240423). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:30:12Z. **Review:** APPROVED --- Checked against main 87666fc5d6, FreeBSD release/11.4.0 `sys/vm/vm_map.c` and `vm_mmap.c`, and the gist logs: - Every case listed in the description matches `mprot.console.log` and `mtype.console.log`, including the type of the physical page behind a gap staying 0, and the flexible `0x8002002d` ranges (`mixed`, and all-flexible ones such as `no_gap`) changing nothing. The new tests follow the logs. - FreeBSD 11.4 agrees: `vm_map_protect` (`vm_map.c:1999`) takes the next entry when the start is unmapped and has no hole check, and `kern_mprotect` returns 0 for `KERN_SUCCESS`, so unmapped starts, gaps and fully unmapped ranges all return 0 there too. - `git merge-tree` against current main is clean. - Minor: in `past_end` the 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. - Optional: on Linux the straddling stack range now goes unchecked. From reading `DoMprotect`, it seems to reach the existing host-stack `EACCES` throw, 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9882
No description provided.