fix(libkernel): fail pthread_attr_get_np with EFAULT on an unreadable attribute pointer #17216

Merged
Gruzzberry merged 2 commits from upstream-pr/17216 into main 2026-11-13 23:30:27 +00:00
Gruzzberry commented 2026-11-08 14:52:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-08T14:52:28Z.


What

pthread_attr_get_np read the guest attribute pointer without checking it, so an unusable pointer such as 0x10 or an unreadable page crashed the host. FreeBSD 11.4's _pthread_attr_get_np reads *dst in its first check, where the process faults, so libthr has no error to copy here; this is a host-safety fallback. The attribute slot is now read with GuestCopyIn_nid_no_patch and an unreadable pointer returns EFAULT. The order of the other checks is unchanged: a null thread or attribute pointer gives EINVAL, then the EFAULT, then EINVAL for an attribute that was never initialised, then ESRCH for a thread that is not live. The success path fills the same fields as before. The new debt entry records that the EFAULT is 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_attr: new checks for the attribute pointer 0x10 and an unreadable page (EFAULT with the calling thread, with an unknown thread and with a null thread, which stays EINVAL), a null attribute pointer, an uninitialised attribute and a null thread (EINVAL), an unknown thread with a valid attribute (ESRCH) and the success path. With Posix/Attr.cpp reverted to main the test crashes; with this change it passes, three runs out of three.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.

Overlaps

  • #9938 (honor caller-provided pthread stacks) adds one line to pthread_attr_get_np in Posix/Attr.cpp, next to the lines this PR edits: proposed on #9938 that it goes first and this PR rebases.
  • #14190 edits core/libs/tests/GuestPthreadAttr.cpp in CheckEndedThreads, away from the function this PR adds at the end of the file; the hunks do not touch.
  • The other listed PRs only append to docs/dev/debt/libkernel.md at the same place as this one, which keeps both lines.

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: #9938; merge order: after #9938 (neighbouring lines)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5212301609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17216). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-08T14:52:28Z. --- ### What `pthread_attr_get_np` read the guest attribute pointer without checking it, so an unusable pointer such as `0x10` or an unreadable page crashed the host. FreeBSD 11.4's [`_pthread_attr_get_np`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_attr.c) reads `*dst` in its first check, where the process faults, so libthr has no error to copy here; this is a host-safety fallback. The attribute slot is now read with `GuestCopyIn_nid_no_patch` and an unreadable pointer returns `EFAULT`. The order of the other checks is unchanged: a null thread or attribute pointer gives `EINVAL`, then the `EFAULT`, then `EINVAL` for an attribute that was never initialised, then `ESRCH` for a thread that is not live. The success path fills the same fields as before. The new debt entry records that the `EFAULT` is 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_attr`: new checks for the attribute pointer `0x10` and an unreadable page (`EFAULT` with the calling thread, with an unknown thread and with a null thread, which stays `EINVAL`), a null attribute pointer, an uninitialised attribute and a null thread (`EINVAL`), an unknown thread with a valid attribute (`ESRCH`) and the success path. With `Posix/Attr.cpp` reverted to `main` the test crashes; with this change it passes, three runs out of three. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. ### Overlaps - #9938 (`honor caller-provided pthread stacks`) adds one line to `pthread_attr_get_np` in `Posix/Attr.cpp`, next to the lines this PR edits: proposed on #9938 that it goes first and this PR rebases. - #14190 edits `core/libs/tests/GuestPthreadAttr.cpp` in `CheckEndedThreads`, away from the function this PR adds at the end of the file; the hunks do not touch. - The other listed PRs only append to `docs/dev/debt/libkernel.md` at the same place as this one, which keeps both lines. ### 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: #9938; merge order: after #9938 (neighbouring lines) - [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:22 +00:00 (Migrated from github.com)

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

Review: APPROVED


What I ran

  • Head commit: e113f944810316a09ca6b137cb773b89446aacd9
  • Merge-base: f5fe8073533b671946ab8999ebbee85f4f78c597 (merges cleanly with current main 828e44310ec98912f041d0e51b8adc809848741b).
  • Environment: Windows 11 x86-64, MinGW-w64 GCC 15.2.0 (WinLibs UCRT POSIX), CMake 3.31, Ninja 1.13.

Tools and Checks

  • python tools/check_conventions.py --base upstream/main --head pr-17216: 0 errors.
  • git diff --check upstream/main...pr-17216: clean.

Windows Test Suite & Negative Control

  • guest_pthread_attr: Passed (0.57 s).
  • guest_pthread_attr_stack: Passed (0.04 s).
  • guest_pthread_attr_errors: Passed (0.05 s).
  • Negative control: Restoring core/libs/prx/libkernel/Pthread/Posix/Attr.cpp from main reproduces the unhandled exception on read of 0x0000000000000010 (0xc0000005, SIGSEGV). With this PR, the bad pointer cleanly returns EFAULT.

Code Observation (Non-blocking)

core/libs/prx/libkernel/Pthread/Posix/Attr.cpp already defines the static helper LoadAttribute(const PthreadAttr* attr, PthreadAttr& handle) at line 16, which performs the exact null, GuestCopyIn, and handle validation logic used by 14 other functions in the same file. In pthread_attr_get_np_nid_postfix, invoking if (const int error = LoadAttribute(attr, handle)) return error; would avoid inlining those 4 lines.

Also, please remove the 🤖 Generated with [Claude Code] footer below the checklist per repository conventions; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5818471491 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17216#pullrequestreview-5818471491). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-08T15:34:22Z. **Review:** APPROVED --- ### What I ran - **Head commit:** `e113f944810316a09ca6b137cb773b89446aacd9` - **Merge-base:** `f5fe8073533b671946ab8999ebbee85f4f78c597` (merges cleanly with current `main` `828e44310ec98912f041d0e51b8adc809848741b`). - **Environment:** Windows 11 x86-64, MinGW-w64 GCC 15.2.0 (WinLibs UCRT POSIX), CMake 3.31, Ninja 1.13. ### Tools and Checks - `python tools/check_conventions.py --base upstream/main --head pr-17216`: 0 errors. - `git diff --check upstream/main...pr-17216`: clean. ### Windows Test Suite & Negative Control - `guest_pthread_attr`: Passed (0.57 s). - `guest_pthread_attr_stack`: Passed (0.04 s). - `guest_pthread_attr_errors`: Passed (0.05 s). - **Negative control:** Restoring `core/libs/prx/libkernel/Pthread/Posix/Attr.cpp` from `main` reproduces the unhandled exception on `read of 0x0000000000000010` (`0xc0000005`, `SIGSEGV`). With this PR, the bad pointer cleanly returns `EFAULT`. ### Code Observation (Non-blocking) `core/libs/prx/libkernel/Pthread/Posix/Attr.cpp` already defines the static helper `LoadAttribute(const PthreadAttr* attr, PthreadAttr& handle)` at line 16, which performs the exact null, `GuestCopyIn`, and handle validation logic used by 14 other functions in the same file. In `pthread_attr_get_np_nid_postfix`, invoking `if (const int error = LoadAttribute(attr, handle)) return error;` would avoid inlining those 4 lines. Also, please remove the `🤖 Generated with [Claude Code]` footer below the checklist per repository conventions; 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!17216
No description provided.