fix(libkernel): copy virtual range names through a checked guest copy #21565
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21565"
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 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 forsceKernelMapNamedFlexibleMemory,sceKernelMapNamedFlexibleMemoryInternalandsceKernelMapNamedDirectMemory;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.errnois kept. What happens with an unreadable name:sceKernelSetVirtualRangeNamereturnsEFAULT(0x8002000e), as it already does for a null name, and keeps the earlier name.sceKernelMapNamedSystemFlexibleMemorythrows before mapping, as it does for a null name. That case is unmeasured.What stays the same:
ENAMETOOLONGfor 32 bytes or more insceKernelSetVirtualRangeName, the throw in the system call, and the named-mapping calls still cut a longer name to 31 bytes.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.
sceKernelMapNamedDirectMemoryandsceKernelMapNamedSystemFlexibleMemoryhandle long and empty names, from PS5 measurements.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_memorygains these cases:sceKernelSetVirtualRangeNamewith an unmapped (0x10) name returnsEFAULT, keepserrnoat 71, and the earlier name stays.PROT_NONE(sceKernelMprotect):"edge"with its terminator on the last readable byte is set."xxx"without a terminator before the boundary returnsEFAULT.sceKernelMapNamedFlexibleMemorywith an unmapped name maps, and the range is unnamed.guest_memory,guest_system_flexible_memoryandguest_virtual_query_argumentspass, 3 runs of 3.main'sExport.cppand the new cases,guest_memoryends infatal SIGSEGV (code 1) at address 0x0000000000000010.tools/check_conventions.pypasses on the branch and this body. The fragment folds;tools/debt_fold.py --checkfails only onmain's owndocs-nkwebkit-drop-readded-count-lines.md, which #21458, #21512 and #21531 fix.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-16T12:12:40Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
eb715598d7:tools/check_conventions.pypassed;git diff --checkclean.guest_memory_testsbuilt cleanly.guest_memory,guest_mmap_fixed_anonymous,guest_mmap_executable):core/libs/prx/libkernel/DirectMemory/Export.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.CopyRangeNameandCopyMappedNameincore/libs/prx/libkernel/DirectMemory/Export.cppsave and restoreerrnoaroundCopyGuestString_nid_no_patchandGuestCopyIn_nid_no_patch.sceKernelSetVirtualRangeNamechecksnamefornullptr(SCE_KERNEL_ERROR_EFAULT), mapsENAMETOOLONG(63) for strings of 32 bytes or more toSCE_KERNEL_ERROR_ENAMETOOLONGand unreadable memory toSCE_KERNEL_ERROR_EFAULTbefore modifying range names.CopyMappedNamehandles strings up to 30 bytes viaCopyRangeName(name, 31, copy)and truncates strings of 31+ bytes viaGuestCopyIn_nid_no_patch(bytes, name, 31), leaving the mapped range unnamed on unreadable memory inNameMappedRange.sceKernelMapNamedSystemFlexibleMemoryvalidateslen, checksname == nullptr, and throwsstd::runtime_erroronerror == 63(32+ bytes) orerror != 0(unreadable name) prior to mapping.NameVirtualRangelockingg_rangeNameLockacrossEraseRangeNamesandg_rangeNames.emplaceafter guest string copy completes.core/libs/tests/GuestMemory.cppcover unreadable pointer0x10,errnopreservation, page boundary NUL-terminated and unterminated strings withsceKernelMprotect, and truncation/unreadable names insceKernelMapNamedFlexibleMemory.Looks Good To Me