Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
Gruzzberry:fix/pthread-mutex-rwlock-setname-efault
Nov 12, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
Gruzzberry:fix/pthread-mutex-rwlock-setname-efault

Conversation

@Gruzzberry

Copy link
Copy Markdown
Contributor

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 f5fe807.

  • 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

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

…usable pointer

Co-Authored-By: Claude Sonnet 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.

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.

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head fe8d7622e4, merged onto main 828e44310e (47873265e1, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_pthread_setname passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 8, 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 17638 core/libs/prx/libkernel/Pthread/src/Mutex.cpp L28, L367-370
core/libs/prx/libkernel/Pthread/src/Rwlock.cpp L218
core/libs/tests/GuestPthreadSetname.cpp L8, L17
PR 17885 docs/dev/debt/libkernel.md L433
PR 18035 docs/dev/debt/libkernel.md L433
PR 18542 docs/dev/debt/libkernel.md L433
PR 18547 docs/dev/debt/libkernel.md L433
PR 18548 docs/dev/debt/libkernel.md L433

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 5021 docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11625 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 docs/dev/debt/libkernel.md
PR 12642 core/libs/prx/libkernel/Pthread/src/Mutex.cpp
PR 12682 docs/dev/debt/libkernel.md
PR 12688 docs/dev/debt/libkernel.md
PR 12700 docs/dev/debt/libkernel.md
PR 12965 docs/dev/debt/libkernel.md
PR 14103 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libkernel.md
PR 14497 docs/dev/debt/libkernel.md
PR 14756 docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 15095 docs/dev/debt/libkernel.md
PR 17553 docs/dev/debt/libkernel.md
PR 17584 docs/dev/debt/libkernel.md
PR 17585 docs/dev/debt/libkernel.md
PR 17609 docs/dev/debt/libkernel.md
PR 17867 docs/dev/debt/libkernel.md
PR 17897 docs/dev/debt/libkernel.md
PR 17908 docs/dev/debt/libkernel.md
PR 18197 docs/dev/debt/libkernel.md
PR 18462 docs/dev/debt/libkernel.md
PR 18515 docs/dev/debt/libkernel.md

No CMake target builds it.
Keep both debt entries in docs/dev/debt/libkernel.md.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 11, 2026
@gabichuuuuu

Copy link
Copy Markdown
Contributor

Windows check of head 0e9b4a0e4c, merged onto main 28237a8567 (clean merge, built together with other open PRs that touch different files).

Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release, BUILD_TESTING=ON, Python 3.15.

  • Tests built from this PR's test files: guest_pthread_setname.
  • All of them pass in 3 of 3 runs.
  • Negative control: with this PR's code reverted to main (files it adds removed) and rebuilt, guest_pthread_setname fails in 2 of 2 runs, so the tests detect the change.

AI-assisted: yes.

@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 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

@oneandonlydean
oneandonlydean merged commit f041b3f into boykopovar:main Nov 12, 2026
13 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.

5 participants