fix(libkernel): fail mutex and rwlock setname_np with EFAULT on an unusable pointer #17222

Merged
Gruzzberry merged 3 commits from upstream-pr/17222 into main 2026-11-12 06:16:51 +00:00
Gruzzberry commented 2026-11-08 15:14:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-08T15:14:10Z.


What

pthread_mutex_setname_np and pthread_rwlock_setname_np read the guest mutex or lock pointer and the guest name string without checking them, so an unusable pointer such as 0x10 or an unreadable page crashed the host (std::string was built straight from the raw name). 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 merged pthread_barrier_setname_np. A mutex or lock pointer that cannot be read now returns EFAULT (14) with GuestCopyIn_nid_no_patch, and the name is read with CopyGuestString_nid_no_patch and a 1024-byte bound (the bound pthread_barrier_setname_np uses): an unreadable or unterminated-at-a-page-edge name gives EFAULT and a name of 1024 bytes or more gives ENAMETOOLONG (63). The existing order is kept: the EAGAIN for an already named object and, for a lock, the EINVAL for a null name come before the name is read, a null mutex name still returns 0 and leaves it unnamed, and null, statically initialized and destroyed objects still throw. The new debt entry records that the EFAULT and the bound are unmeasured.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on main f5fe807353.

  • guest_pthread_setname: new checks for the mutex and lock pointers 0x10 and an unreadable page, and for a name of 0x10, 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). With Pthread/src/Mutex.cpp and Pthread/src/Rwlock.cpp reverted to main the test crashes (SegFault); with this change it passes, three runs out of three.
  • The other mutex, read-write lock and destroyed-object tests that build here (18 tests) pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run, the full suite.

Overlaps

  • pr_overlap.py lists #13582, #15524, #15567, #17212, #17216 and #17220, and each row is docs/dev/debt/libkernel.md at the same appended place as this PR's entry, which keeps both lines.
  • #12130 (pthread_suspend_np) and #12642 (pthread_suspend_all_np) conflict with main themselves, so pr_overlap.py did not compare them; their hunks in Pthread/src/Mutex.cpp are in Timedlock and scePthreadMutexLock, away from pthread_mutex_setname_np at the end of the file and from the constants this PR adds at the top.
  • #14108 edits Posix/Rwlock.cpp, not src/Rwlock.cpp, so the hunks do not touch.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none; merge order: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5212461103 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17222). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-08T15:14:10Z. --- ### What `pthread_mutex_setname_np` and `pthread_rwlock_setname_np` read the guest mutex or lock pointer and the guest `name` string without checking them, so an unusable pointer such as `0x10` or an unreadable page crashed the host (`std::string` was built straight from the raw `name`). 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 merged `pthread_barrier_setname_np`. A mutex or lock pointer that cannot be read now returns `EFAULT` (14) with `GuestCopyIn_nid_no_patch`, and the name is read with `CopyGuestString_nid_no_patch` and a 1024-byte bound (the bound `pthread_barrier_setname_np` uses): an unreadable or unterminated-at-a-page-edge `name` gives `EFAULT` and a `name` of 1024 bytes or more gives `ENAMETOOLONG` (63). The existing order is kept: the `EAGAIN` for an already named object and, for a lock, the `EINVAL` for a null `name` come before the name is read, a null mutex `name` still returns 0 and leaves it unnamed, and null, statically initialized and destroyed objects still throw. The new debt entry records that the `EFAULT` and the bound are unmeasured. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` f5fe807353. - `guest_pthread_setname`: new checks for the mutex and lock pointers `0x10` and an unreadable page, and for a `name` of `0x10`, 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). With `Pthread/src/Mutex.cpp` and `Pthread/src/Rwlock.cpp` reverted to `main` the test crashes (`SegFault`); with this change it passes, three runs out of three. - The other mutex, read-write lock and destroyed-object tests that build here (18 tests) pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run, the full suite. ### Overlaps - `pr_overlap.py` lists #13582, #15524, #15567, #17212, #17216 and #17220, and each row is `docs/dev/debt/libkernel.md` at the same appended place as this PR's entry, which keeps both lines. - #12130 (`pthread_suspend_np`) and #12642 (`pthread_suspend_all_np`) conflict with `main` themselves, so `pr_overlap.py` did not compare them; their hunks in `Pthread/src/Mutex.cpp` are in `Timedlock` and `scePthreadMutexLock`, away from `pthread_mutex_setname_np` at the end of the file and from the constants this PR adds at the top. - #14108 edits `Posix/Rwlock.cpp`, not `src/Rwlock.cpp`, so the hunks do not touch. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none; merge order: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-08 15:34:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-08T15:34:47Z.

Review: CHANGES_REQUESTED


Finding (Blocking)

core/libs/tests/ProbeFault.cpp appears to have been committed inadvertently:

  1. It adds a 335-line standalone test scratchpad containing a 315-case switch with raw 0x10 calls.
  2. It is not registered in CMakeLists.txt or mentioned in the PR description.
  3. It fails git diff --check upstream/main...HEAD due 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 via CopyGuestString_nid_no_patch and GuestCopyIn_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.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5818472884 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17222#pullrequestreview-5818472884). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-08T15:34:47Z. **Review:** CHANGES_REQUESTED --- ### Finding (Blocking) `core/libs/tests/ProbeFault.cpp` appears to have been committed inadvertently: 1. It adds a 335-line standalone test scratchpad containing a 315-case switch with raw `0x10` calls. 2. It is not registered in `CMakeLists.txt` or mentioned in the PR description. 3. It fails `git diff --check upstream/main...HEAD` due 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 via `CopyGuestString_nid_no_patch` and `GuestCopyIn_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.
PrivacyOnion commented 2026-11-12 06:09:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:09:45Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_pthread_setname_tests built 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.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4354: guest_pthread_setname ............***Exception: SegFault  0.38 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • 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 with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869898286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17222#pullrequestreview-5869898286). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:09:45Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `0e9b4a0e4c`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_pthread_setname_tests` built 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.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4354: guest_pthread_setname ............***Exception: SegFault 0.38 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **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 with` footer from the description; the AI-assisted checkbox can stay. 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!17222
No description provided.