fix(libkernel): hold the thread registry across posix pthread accesses #21742

Merged
AasishDairelSahayaGrinspan merged 1 commit from upstream-pr/21742 into main 2026-11-27 17:43:52 +00:00
AasishDairelSahayaGrinspan commented 2026-11-16 18:07:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-11-16T18:07:41Z.


What

Four POSIX pthread wrappers dereferenced a PthreadPrivate outside the thread registry lock: pthread_getname_np read _joined before taking any lock, scePthreadGetschedparam and scePthreadSetschedparam read and stored schedPolicy after the priority call had returned and released the lock, and pthread_getcpuclockid read the thread's CPU clock id with no lock at all. A detached thread's object is deleted the moment it ends, so each was a use-after-free read or write (or an ABA onto a recycled object).

Each dereference now happens under the existing LockRegisteredGuestThread, and the wrapper answers ESRCH when the thread is gone, matching the sibling exports. pthread_getname_np releases the lock before calling scePthreadGetname, which takes it again itself.

Residual, out of scope: the registry has no generation counter, so a recycled allocation landing on the same address between the priority call and the re-acquire could still read or store the wrong thread's schedPolicy — value confusion, not memory unsafety.

Tested

Linux (Omarchy 4.0.2), host build Release with BUILD_TESTING=ON:

  • guest_pthread_released_handle — Passed, 0.01 s (each of its 20 release rounds now also requires ESRCH from all six wrappers on the ended detached handle)
  • guest_pthread_getname — 0.01 s
  • guest_pthread_schedparam — 0.01 s
  • guest_pthread_final_release_race — 0.01 s
  • guest_kernel_sys_pthread with its three modes — 0.10 s total
  • guest_thread_cpu_clock — 0.82 s

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5342363024 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21742). Originally posted by [AasishDairelSahayaGrinspan](https://github.com/AasishDairelSahayaGrinspan) on 2026-11-16T18:07:41Z. --- ### What Four POSIX pthread wrappers dereferenced a `PthreadPrivate` outside the thread registry lock: `pthread_getname_np` read `_joined` before taking any lock, `scePthreadGetschedparam` and `scePthreadSetschedparam` read and stored `schedPolicy` after the priority call had returned and released the lock, and `pthread_getcpuclockid` read the thread's CPU clock id with no lock at all. A detached thread's object is deleted the moment it ends, so each was a use-after-free read or write (or an ABA onto a recycled object). Each dereference now happens under the existing `LockRegisteredGuestThread`, and the wrapper answers `ESRCH` when the thread is gone, matching the sibling exports. `pthread_getname_np` releases the lock before calling `scePthreadGetname`, which takes it again itself. Residual, out of scope: the registry has no generation counter, so a recycled allocation landing on the same address between the priority call and the re-acquire could still read or store the wrong thread's `schedPolicy` — value confusion, not memory unsafety. ### Tested Linux (Omarchy 4.0.2), host build `Release` with `BUILD_TESTING=ON`: - `guest_pthread_released_handle` — Passed, 0.01 s (each of its 20 release rounds now also requires `ESRCH` from all six wrappers on the ended detached handle) - `guest_pthread_getname` — 0.01 s - `guest_pthread_schedparam` — 0.01 s - `guest_pthread_final_release_race` — 0.01 s - `guest_kernel_sys_pthread` with its three modes — 0.10 s total - `guest_thread_cpu_clock` — 0.82 s ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-16 18:14:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T18:14:01Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_pthread_released_handle_tests built cleanly.
  • CTest (guest_pthread_released_handle):
    1/1 Test #5199: guest_pthread_released_handle ....   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Pthread/Posix/Thread.cpp, core/libs/prx/libkernel/Pthread/include/Pthread.hpp, core/libs/prx/libkernel/Pthread/src/Thread.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5199: guest_pthread_released_handle ....Subprocess aborted***Exception:   0.40 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that scePthreadGetschedparam and scePthreadSetschedparam acquire LockRegisteredGuestThread(thread) before reading or writing thread->schedPolicy and return SCE_KERNEL_ERROR_ESRCH if the thread is unregistered.
    • Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that pthread_getname_np_nid_postfix acquires LockRegisteredGuestThread(thread) before checking thread->_joined, returns PosixThread::GUEST_ESRCH if unregistered or joined, and unlocks registered before calling scePthreadGetname to avoid recursive lock deadlock on CpuClocks().lock.
    • Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that pthread_getcpuclockid_nid_postfix acquires LockRegisteredGuestThread(thread) after null checks on thread (GUEST_EINVAL) and clockId (guestFault = 14), returning PosixThread::GUEST_ESRCH if unregistered before calling GuestThreadCpuClockId(thread).
    • Verified in core/libs/prx/libkernel/Pthread/include/Pthread.hpp and core/libs/prx/libkernel/Pthread/src/Thread.cpp that LockRegisteredGuestThread now has external linkage matching its declaration and safely holds CpuClocks().lock across registry lookup.
    • Verified in core/libs/tests/GuestPthreadReleasedHandle.cpp and docs/dev/debt/changes/fix-libkernel-posix-pthread-registry.md that all six wrapper paths are tested against released detached thread handles for ESRCH and documented accurately.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5925065804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21742#pullrequestreview-5925065804). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T18:14:01Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `f4d92c494b`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_pthread_released_handle_tests` built cleanly. - **CTest** (`guest_pthread_released_handle`): ``` 1/1 Test #5199: guest_pthread_released_handle .... Passed 0.04 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Pthread/Posix/Thread.cpp`, `core/libs/prx/libkernel/Pthread/include/Pthread.hpp`, `core/libs/prx/libkernel/Pthread/src/Thread.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5199: guest_pthread_released_handle ....Subprocess aborted***Exception: 0.40 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that scePthreadGetschedparam and scePthreadSetschedparam acquire LockRegisteredGuestThread(thread) before reading or writing thread->schedPolicy and return SCE_KERNEL_ERROR_ESRCH if the thread is unregistered. - Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that pthread_getname_np_nid_postfix acquires LockRegisteredGuestThread(thread) before checking thread->_joined, returns PosixThread::GUEST_ESRCH if unregistered or joined, and unlocks registered before calling scePthreadGetname to avoid recursive lock deadlock on CpuClocks().lock. - Verified in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp that pthread_getcpuclockid_nid_postfix acquires LockRegisteredGuestThread(thread) after null checks on thread (GUEST_EINVAL) and clockId (guestFault = 14), returning PosixThread::GUEST_ESRCH if unregistered before calling GuestThreadCpuClockId(thread). - Verified in core/libs/prx/libkernel/Pthread/include/Pthread.hpp and core/libs/prx/libkernel/Pthread/src/Thread.cpp that LockRegisteredGuestThread now has external linkage matching its declaration and safely holds CpuClocks().lock across registry lookup. - Verified in core/libs/tests/GuestPthreadReleasedHandle.cpp and docs/dev/debt/changes/fix-libkernel-posix-pthread-registry.md that all six wrapper paths are tested against released detached thread handles for ESRCH and documented accurately. Looks Good To Me
MarcZX10 commented 2026-11-20 11:31:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:31:24Z.

Review: APPROVED


Reviewed — looks good

The reviewed changes at f4d92c494bead2841bbbb08bbcfe0cc35f712d03 address the reported released-thread accesses; no blocking defect was verified in the files read.

What I ran

Read the prepared description, commit list, manifest, diff, and each changed file listed as reviewed. Inspected the priority accessors and thread registry locking code through repo_read.py. No tests or build commands were run; the test results in the description are author-reported.

Results

Check Result Note
Changed code review passed Five reviewed files
Build and tests not run Results supplied by author only

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build or tests were run in this review. The author's reported host test runs were not independently verified. All files listed as reviewed in the manifest were read; no files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5991995654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21742#pullrequestreview-5991995654). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:31:24Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed changes at `f4d92c494bead2841bbbb08bbcfe0cc35f712d03` address the reported released-thread accesses; no blocking defect was verified in the files read. ## What I ran Read the prepared description, commit list, manifest, diff, and each changed file listed as reviewed. Inspected the priority accessors and thread registry locking code through `repo_read.py`. No tests or build commands were run; the test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Five reviewed files | | Build and tests | not run | Results supplied by author only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No build or tests were run in this review. The author's reported host test runs were not independently verified. All files listed as reviewed in the manifest were read; no files were excluded. ## To get this to a good state Nothing is required.
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!21742
No description provided.