Repository navigation
fix(libkernel): fail mutex and rwlock setname_np with EFAULT on an unusable pointer - #17222
Conversation
…usable pointer Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
PrivacyOnion
left a comment
There was a problem hiding this comment.
Finding (Blocking)
core/libs/tests/ProbeFault.cpp appears to have been committed inadvertently:
- It adds a 335-line standalone test scratchpad containing a 315-case switch with raw
0x10calls. - It is not registered in
CMakeLists.txtor mentioned in the PR description. - It fails
git diff --check upstream/main...HEADdue to trailing whitespace on every line.
Please remove core/libs/tests/ProbeFault.cpp from the commit.
Code & Tests Verification
The changes in Mutex.cpp, Rwlock.cpp, and GuestPthreadSetname.cpp are sound:
- Name truncation boundary (1024 bytes ->
ENAMETOOLONG), page boundary termination, and bad pointer detection (EFAULT) are cleanly implemented viaCopyGuestString_nid_no_patchandGuestCopyIn_nid_no_patch. - Conventions check passes once the untracked test file is removed.
- Please also remove the
🤖 Generated with [Claude Code]footer below the checklist; the AI-assisted checkbox can stay.
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
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.
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.
|
No CMake target builds it.
Keep both debt entries in docs/dev/debt/libkernel.md.
|
I pushed two commits on top of fe8d762: one drops core/libs/tests/ProbeFault.cpp, which no CMake target builds, and one merges main, keeping both entries at the end of docs/dev/debt/libkernel.md. The Mutex.cpp and Rwlock.cpp changes and the GuestPthreadSetname checks are unchanged. |
|
Windows check of head Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release,
AI-assisted: yes. |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 0e9b4a0e4c:
- Conventions:
tools/check_conventions.pypassed;git diff --checkclean. - Build:
guest_pthread_setname_testsbuilt cleanly. - CTest (
guest_pthread_setname):1/1 Test #4354: guest_pthread_setname ............ Passed 0.03 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel/Pthread/src/Mutex.cpp,core/libs/prx/libkernel/Pthread/src/Rwlock.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #4354: guest_pthread_setname ............***Exception: SegFault 0.38 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified pointer probe safety in core/libs/prx/libkernel/Pthread/src/Mutex.cpp and Rwlock.cpp: pthread_mutex_setname_np_nid_postfix and pthread_rwlock_setname_np_nid_postfix validate guest pointers using GuestCopyIn_nid_no_patch(&probe, ..., sizeof(probe)), correctly returning posixFault / POSIX_EFAULT (14) on unusable pointers (such as 0x10 or guard pages) before dereferencing.
- Verified string copy and bound checking in core/libs/prx/libkernel/Pthread/src/Mutex.cpp and Rwlock.cpp: CopyGuestString_nid_no_patch safely constrains string reads to maxMutexName / MAX_RWLOCK_NAME (1024 bytes), returning EFAULT (14) on unreadable or page-edge unterminated inputs and ENAMETOOLONG (63) on oversized strings without altering object state.
- Verified error code semantics and validation ordering: existing order is preserved across Mutex.cpp and Rwlock.cpp where posixAgain / POSIX_EAGAIN (35) on already-named objects and POSIX_EINVAL (22) on null rwlock names precede string copying, a null mutex name returns 0 leaving the object unnamed, and static/destroyed handles throw.
- Verified thread safety and lock scoping: pthread_mutex_setname_np_nid_postfix performs state checks and assignments under InitializationMutex(), while pthread_rwlock_setname_np_nid_postfix holds current->_state across copy and mutation, preventing TOCTOU races on _name and _named.
- Verified test coverage in core/libs/tests/GuestPthreadSetname.cpp: TestMutexBadPointers and TestRwlockBadPointers exercise bad handles (0x10 and unreadable pages), bad names (0x10, unreadable page, 8-byte unterminated buffer at page edge), 1024-byte overflow (GuestEnametoolong = 63), null mutex name, and exact page boundary strings.
- Verified repository conventions and AGENTS.md compliance: zero code comments introduced in C++ sources, unmeasured console behavior documented in docs/dev/debt/libkernel.md, and no extraneous files or stubs added.
- Minor: please drop the
Generated withfooter from the description; the AI-assisted checkbox can stay.
Looks Good To Me
What
pthread_mutex_setname_npandpthread_rwlock_setname_npread the guest mutex or lock pointer and the guestnamestring without checking them, so an unusable pointer such as0x10or an unreadable page crashed the host (std::stringwas built straight from the rawname). The console passes both to the module and the kernel, which fault on a bad pointer, so there is no measured error to copy; this is a host-safety fallback, as for the mergedpthread_barrier_setname_np. A mutex or lock pointer that cannot be read now returnsEFAULT(14) withGuestCopyIn_nid_no_patch, and the name is read withCopyGuestString_nid_no_patchand a 1024-byte bound (the boundpthread_barrier_setname_npuses): an unreadable or unterminated-at-a-page-edgenamegivesEFAULTand anameof 1024 bytes or more givesENAMETOOLONG(63). The existing order is kept: theEAGAINfor an already named object and, for a lock, theEINVALfor a nullnamecome before the name is read, a null mutexnamestill returns 0 and leaves it unnamed, and null, statically initialized and destroyed objects still throw. The new debt entry records that theEFAULTand the bound are unmeasured.Tested
Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release,
-DBUILD_TESTING=ON, onmainf5fe807.guest_pthread_setname: new checks for the mutex and lock pointers0x10and an unreadable page, and for anameof0x10, an unreadable page and 8 bytes ending at a page edge without a terminator (EFAULT, the object stays unnamed), a 1024-byte name (ENAMETOOLONG), a null mutex name (0, unnamed) and a name that ends exactly at the page edge (0, stored). WithPthread/src/Mutex.cppandPthread/src/Rwlock.cppreverted tomainthe test crashes (SegFault); with this change it passes, three runs out of three.python tools/check_conventions.py --base upstream/main: passes.Overlaps
pr_overlap.pylists fix(libkernel): return EFAULT from cancellation state and type setters for unwritable pointers #13582, fix(libkernel): fail inet_ntop with EFAULT on an unmapped source or destination #15524, fix(libkernel): fail pthread_rename_np and scePthreadRename with EFAULT on an unreadable name #15567, feat(libkernel): add LAN relay settings and a loopback skeleton (off by default) #17212, fix(libkernel): fail pthread_attr_get_np with EFAULT on an unreadable attribute pointer #17216 and fix(libkernel): fail condition variable init, destroy and setname with EFAULT on an unusable pointer #17220, and each row isdocs/dev/debt/libkernel.mdat the same appended place as this PR's entry, which keeps both lines.pthread_suspend_np) and feat(libkernel_sys): implement pthread_suspend_all_np and pthread_single_np #12642 (pthread_suspend_all_np) conflict withmainthemselves, sopr_overlap.pydid not compare them; their hunks inPthread/src/Mutex.cppare inTimedlockandscePthreadMutexLock, away frompthread_mutex_setname_npat the end of the file and from the constants this PR adds at the top.Posix/Rwlock.cpp, notsrc/Rwlock.cpp, so the hunks do not touch.Checklist
main; no other open PR implements the same functionNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code