Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libkernel-range-name-guest-copy
Nov 16, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/libkernel-range-name-guest-copy

Conversation

@milescoviello

@milescoviello milescoviello commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

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:

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

sceKernelSetVirtualRangeName and the named-mapping calls read the name in
place with strnlen, so an unreadable name crashed the host. The name is now
copied through CopyGuestString_nid_no_patch, page by page and stopping at
the terminator. An unreadable name returns EFAULT from
sceKernelSetVirtualRangeName, as a null one does, leaves a named mapping
unnamed, and throws in sceKernelMapNamedSystemFlexibleMemory, as a null
name does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 16945 core/libs/prx/libkernel/DirectMemory/Export.cpp L368
PR 17718 core/libs/prx/libkernel/DirectMemory/Export.cpp L368, L733-734, L737-738
PR 21569 core/libs/prx/libkernel/DirectMemory/Export.cpp L119-120, L627-629

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 16372 core/libs/tests/GuestMemory.cpp
PR 17936 core/libs/prx/libkernel/DirectMemory/Export.cpp

@oneandonlydean
oneandonlydean merged commit 2357a9c into boykopovar:main Nov 16, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants