fix(libkernel): copy virtual range names through a checked guest copy #21565

Merged
milescoviello merged 1 commit from upstream-pr/21565 into main 2026-11-16 17:29:42 +00:00
milescoviello commented 2026-11-16 12:10:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-16T12:10:42Z.


What

Four exports read a guest name in place with strnlen:

  • sceKernelSetVirtualRangeName, which checks the length;
  • NameVirtualRange, which copies up to 31 bytes for sceKernelMapNamedFlexibleMemory, sceKernelMapNamedFlexibleMemoryInternal and sceKernelMapNamedDirectMemory;
  • sceKernelMapNamedSystemFlexibleMemory, which checks the length.

So an unreadable name crashed the host.

The name is now copied with CopyGuestString_nid_no_patch. It reads page by page, stops at the terminator and never reads past the length limit, so a short name that ends right before an unreadable page still works. errno is kept. What happens with an unreadable name:

  • sceKernelSetVirtualRangeName returns EFAULT (0x8002000e), as it already does for a null name, and keeps the earlier name.
  • The three named-mapping calls still map, and leave the range unnamed. They already ignored a naming failure, and a null name already maps unnamed.
  • sceKernelMapNamedSystemFlexibleMemory throws before mapping, as it does for a null name. That case is unmeasured.

What stays the same:

  • The length rules: ENAMETOOLONG for 32 bytes or more in sceKernelSetVirtualRangeName, the throw in the system call, and the named-mapping calls still cut a longer name to 31 bytes.
  • The checks before the name is read, and their order.

The two libkernel debt entries now say how an unreadable name is handled.

Overlap: three open PRs change the same lines and will conflict textually with this one, though none of them copies the name. Whichever merges second needs a rebase.

  • #16945 and #17718 (xXPBSXx, opened 11-08 and 11-09) change how sceKernelMapNamedDirectMemory and sceKernelMapNamedSystemFlexibleMemory handle long and empty names, from PS5 measurements.
  • #21569 (kali113) adds address bounds to sceKernelSetVirtualRangeName.

If #16945 or #17718 lands first, the copy here moves into their name checks unchanged.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

  • guest_memory gains these cases:
    • sceKernelSetVirtualRangeName with an unmapped (0x10) name returns EFAULT, keeps errno at 71, and the earlier name stays.
    • A name ending at a page whose next page is PROT_NONE (sceKernelMprotect): "edge" with its terminator on the last readable byte is set. "xxx" without a terminator before the boundary returns EFAULT.
    • sceKernelMapNamedFlexibleMemory with an unmapped name maps, and the range is unnamed.
    • With a 40-byte name, the range is named with its first 31 bytes.
  • guest_memory, guest_system_flexible_memory and guest_virtual_query_arguments pass, 3 runs of 3.
  • Negative control: with main's Export.cpp and the new cases, guest_memory ends in fatal SIGSEGV (code 1) at address 0x0000000000000010.
  • tools/check_conventions.py passes on the branch and this body. The fragment folds; tools/debt_fold.py --check fails only on main's own docs-nkwebkit-drop-readded-count-lines.md, which #21458, #21512 and #21531 fix.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5336723333 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21565). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-16T12:10:42Z. --- ### What Four exports read a guest name in place with `strnlen`: - `sceKernelSetVirtualRangeName`, which checks the length; - `NameVirtualRange`, which copies up to 31 bytes for `sceKernelMapNamedFlexibleMemory`, `sceKernelMapNamedFlexibleMemoryInternal` and `sceKernelMapNamedDirectMemory`; - `sceKernelMapNamedSystemFlexibleMemory`, which checks the length. So an unreadable name crashed the host. The name is now copied with `CopyGuestString_nid_no_patch`. It reads page by page, stops at the terminator and never reads past the length limit, so a short name that ends right before an unreadable page still works. `errno` is kept. What happens with an unreadable name: - `sceKernelSetVirtualRangeName` returns `EFAULT` (0x8002000e), as it already does for a null name, and keeps the earlier name. - The three named-mapping calls still map, and leave the range unnamed. They already ignored a naming failure, and a null name already maps unnamed. - `sceKernelMapNamedSystemFlexibleMemory` throws before mapping, as it does for a null name. That case is unmeasured. What stays the same: - The length rules: `ENAMETOOLONG` for 32 bytes or more in `sceKernelSetVirtualRangeName`, the throw in the system call, and the named-mapping calls still cut a longer name to 31 bytes. - The checks before the name is read, and their order. The two libkernel debt entries now say how an unreadable name is handled. Overlap: three open PRs change the same lines and will conflict textually with this one, though none of them copies the name. Whichever merges second needs a rebase. - #16945 and #17718 (xXPBSXx, opened 11-08 and 11-09) change how `sceKernelMapNamedDirectMemory` and `sceKernelMapNamedSystemFlexibleMemory` handle long and empty names, from PS5 measurements. - #21569 (kali113) adds address bounds to `sceKernelSetVirtualRangeName`. If #16945 or #17718 lands first, the copy here moves into their name checks unchanged. ### Tested Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, `-DBUILD_TESTING=ON`: - `guest_memory` gains these cases: - `sceKernelSetVirtualRangeName` with an unmapped (`0x10`) name returns `EFAULT`, keeps `errno` at 71, and the earlier name stays. - A name ending at a page whose next page is `PROT_NONE` (`sceKernelMprotect`): `"edge"` with its terminator on the last readable byte is set. `"xxx"` without a terminator before the boundary returns `EFAULT`. - `sceKernelMapNamedFlexibleMemory` with an unmapped name maps, and the range is unnamed. - With a 40-byte name, the range is named with its first 31 bytes. - `guest_memory`, `guest_system_flexible_memory` and `guest_virtual_query_arguments` pass, 3 runs of 3. - Negative control: with `main`'s `Export.cpp` and the new cases, `guest_memory` ends in `fatal SIGSEGV (code 1) at address 0x0000000000000010`. - `tools/check_conventions.py` passes on the branch and this body. The fragment folds; `tools/debt_fold.py --check` fails only on `main`'s own `docs-nkwebkit-drop-readded-count-lines.md`, which #21458, #21512 and #21531 fix. ### Checklist - [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) and the licence notice of code adapted from upstream (see AGENTS.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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-16 12:12:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T12:12:40Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against eb715598d7:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_memory_tests built cleanly.
  • CTest (guest_memory, guest_mmap_fixed_anonymous, guest_mmap_executable):
    1/3 Test #5367: guest_mmap_executable ............   Passed    0.01 sec
    2/3 Test #5366: guest_mmap_fixed_anonymous .......   Passed    0.01 sec
    3/3 Test #5365: guest_memory .....................   Passed    1.74 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libkernel/DirectMemory/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #5367: guest_mmap_executable ............   Passed    0.01 sec
    2/3 Test #5366: guest_mmap_fixed_anonymous .......   Passed    0.01 sec
    3/3 Test #5365: guest_memory .....................***Exception: SegFault  0.14 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified CopyRangeName and CopyMappedName in core/libs/prx/libkernel/DirectMemory/Export.cpp save and restore errno around CopyGuestString_nid_no_patch and GuestCopyIn_nid_no_patch.
    • Verified sceKernelSetVirtualRangeName checks name for nullptr (SCE_KERNEL_ERROR_EFAULT), maps ENAMETOOLONG (63) for strings of 32 bytes or more to SCE_KERNEL_ERROR_ENAMETOOLONG and unreadable memory to SCE_KERNEL_ERROR_EFAULT before modifying range names.
    • Verified CopyMappedName handles strings up to 30 bytes via CopyRangeName(name, 31, copy) and truncates strings of 31+ bytes via GuestCopyIn_nid_no_patch(bytes, name, 31), leaving the mapped range unnamed on unreadable memory in NameMappedRange.
    • Verified sceKernelMapNamedSystemFlexibleMemory validates len, checks name == nullptr, and throws std::runtime_error on error == 63 (32+ bytes) or error != 0 (unreadable name) prior to mapping.
    • Verified thread safety of NameVirtualRange locking g_rangeNameLock across EraseRangeNames and g_rangeNames.emplace after guest string copy completes.
    • Verified unit tests in core/libs/tests/GuestMemory.cpp cover unreadable pointer 0x10, errno preservation, page boundary NUL-terminated and unterminated strings with sceKernelMprotect, and truncation/unreadable names in sceKernelMapNamedFlexibleMemory.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5919755552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21565#pullrequestreview-5919755552). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T12:12:40Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `eb715598d7`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_memory_tests` built cleanly. - **CTest** (`guest_memory`, `guest_mmap_fixed_anonymous`, `guest_mmap_executable`): ``` 1/3 Test #5367: guest_mmap_executable ............ Passed 0.01 sec 2/3 Test #5366: guest_mmap_fixed_anonymous ....... Passed 0.01 sec 3/3 Test #5365: guest_memory ..................... Passed 1.74 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libkernel/DirectMemory/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #5367: guest_mmap_executable ............ Passed 0.01 sec 2/3 Test #5366: guest_mmap_fixed_anonymous ....... Passed 0.01 sec 3/3 Test #5365: guest_memory .....................***Exception: SegFault 0.14 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - Verified `CopyRangeName` and `CopyMappedName` in `core/libs/prx/libkernel/DirectMemory/Export.cpp` save and restore `errno` around `CopyGuestString_nid_no_patch` and `GuestCopyIn_nid_no_patch`. - Verified `sceKernelSetVirtualRangeName` checks `name` for `nullptr` (`SCE_KERNEL_ERROR_EFAULT`), maps `ENAMETOOLONG` (63) for strings of 32 bytes or more to `SCE_KERNEL_ERROR_ENAMETOOLONG` and unreadable memory to `SCE_KERNEL_ERROR_EFAULT` before modifying range names. - Verified `CopyMappedName` handles strings up to 30 bytes via `CopyRangeName(name, 31, copy)` and truncates strings of 31+ bytes via `GuestCopyIn_nid_no_patch(bytes, name, 31)`, leaving the mapped range unnamed on unreadable memory in `NameMappedRange`. - Verified `sceKernelMapNamedSystemFlexibleMemory` validates `len`, checks `name == nullptr`, and throws `std::runtime_error` on `error == 63` (32+ bytes) or `error != 0` (unreadable name) prior to mapping. - Verified thread safety of `NameVirtualRange` locking `g_rangeNameLock` across `EraseRangeNames` and `g_rangeNames.emplace` after guest string copy completes. - Verified unit tests in `core/libs/tests/GuestMemory.cpp` cover unreadable pointer `0x10`, `errno` preservation, page boundary NUL-terminated and unterminated strings with `sceKernelMprotect`, and truncation/unreadable names in `sceKernelMapNamedFlexibleMemory`. Looks Good To Me
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!21565
No description provided.