fix(libkernel): fail pthread_rename_np and scePthreadRename with EFAULT on an unreadable name #15567

Merged
Gruzzberry merged 1 commit from upstream-pr/15567 into main 2026-11-12 20:21:35 +00:00
Gruzzberry commented 2026-11-03 19:05:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-03T19:05:21Z.


What

pthread_rename_np took the length of the guest name with strlen, and scePthreadRename assigned it to a std::string, both through the raw pointer, so a non-null name that is not readable, such as 0x10 or one that runs into an inaccessible page, faulted the host. They now copy the name with CopyGuestString_nid_no_patch and fail with EFAULT (14 from pthread_rename_np, SCE_KERNEL_ERROR_EFAULT from scePthreadRename).

The copy comes before the thread is looked up, so an unreadable name wins over ESRCH; that order was chosen for the host and is not derived from FreeBSD (whose libthr finds the thread before it calls thr_set_name, which in turn runs copyinstr) or from a console. A null thread or null name still gives EINVAL first, as before. Both read at most 32 bytes (31 characters and the terminator), so a longer name still gives ENAMETOOLONG, as the console measurements on main (#17091) pin for both functions, without reading past the 32nd byte. The strlen check that #17091 added to scePthreadRename is replaced by this bounded copy.

The Sony functions were not measured with a bad name pointer, so this is a host-safety fix, not console behaviour. The entry is in docs/dev/debt/libkernel.md.

Tested

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

  • guest_pthread_rename: new CheckUnusableNames covers a name at 0x10, on a PROT_NONE page and a 31-byte name that runs into one (EFAULT from both functions, the stored name unchanged and the caller's errno untouched); a null thread with a bad name (EINVAL); a 32-byte name ending exactly at the unreadable page (ENAMETOOLONG, nothing read past it); a 31-character name ending exactly at it (stored); an unknown thread with a good name (ESRCH); a null name (EINVAL); and an unterminated 16 KiB name for scePthreadRename (ENAMETOOLONG). On main the test fails; with this change it passes, three runs out of three. The existing names, ENAMETOOLONG sizes and Windows thread description cases still pass.
  • Full suite (3924 tests) on this branch: 98 failed, and the same 98 fail when rerun alone. 95 are the relinker and ELF tool tests that main gained recently (elf_header, guest_*, windows_*, macos_*, restricted_undefined_symbols_* and the like): their Python driver cannot start the relinker executable, which is not in this build tree. vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted fail because of the WindowsApps Python on this machine. agc_driver_store_over_untracked and agc_driver_direct_write_completion are GPU tests; with Thread.cpp and Posix/Thread.cpp reverted to main the second still fails, so they do not depend on this change. Nothing thread related fails.
  • Rebased onto main 7f64d6f057 (2026-11-13), resolving the conflict with #17091 as above: guest_pthread_rename, including the #17091 cases, passes three runs out of three. The full-suite run above is from the previous base.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run.

Overlaps

tools/pr_overlap.py reports no conflicting file or export. It lists #3986, #4563, #8592, #9447, #12130 and #12642 as sharing Thread.cpp or Posix/Thread.cpp without comparing them, because those PRs conflict with main themselves; none of their diffs changes scePthreadRename, pthread_rename_np or the code around them (#12130 and #12642 only call scePthreadRename with short host names in new tests, which still work). The other listed PRs only append to docs/dev/debt/libkernel.md.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5142962604 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15567). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-03T19:05:21Z. --- ### What `pthread_rename_np` took the length of the guest name with `strlen`, and `scePthreadRename` assigned it to a `std::string`, both through the raw pointer, so a non-null name that is not readable, such as 0x10 or one that runs into an inaccessible page, faulted the host. They now copy the name with `CopyGuestString_nid_no_patch` and fail with `EFAULT` (14 from `pthread_rename_np`, `SCE_KERNEL_ERROR_EFAULT` from `scePthreadRename`). The copy comes before the thread is looked up, so an unreadable name wins over `ESRCH`; that order was chosen for the host and is not derived from FreeBSD (whose libthr finds the thread before it calls `thr_set_name`, which in turn runs `copyinstr`) or from a console. A null thread or null name still gives `EINVAL` first, as before. Both read at most 32 bytes (31 characters and the terminator), so a longer name still gives `ENAMETOOLONG`, as the console measurements on `main` (#17091) pin for both functions, without reading past the 32nd byte. The `strlen` check that #17091 added to `scePthreadRename` is replaced by this bounded copy. The Sony functions were not measured with a bad name pointer, so this is a host-safety fix, not console behaviour. The entry is in `docs/dev/debt/libkernel.md`. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_pthread_rename`: new `CheckUnusableNames` covers a name at 0x10, on a `PROT_NONE` page and a 31-byte name that runs into one (`EFAULT` from both functions, the stored name unchanged and the caller's errno untouched); a null thread with a bad name (`EINVAL`); a 32-byte name ending exactly at the unreadable page (`ENAMETOOLONG`, nothing read past it); a 31-character name ending exactly at it (stored); an unknown thread with a good name (`ESRCH`); a null name (`EINVAL`); and an unterminated 16 KiB name for `scePthreadRename` (`ENAMETOOLONG`). On `main` the test fails; with this change it passes, three runs out of three. The existing names, `ENAMETOOLONG` sizes and Windows thread description cases still pass. - Full suite (3924 tests) on this branch: 98 failed, and the same 98 fail when rerun alone. 95 are the relinker and ELF tool tests that main gained recently (`elf_header`, `guest_*`, `windows_*`, `macos_*`, `restricted_undefined_symbols_*` and the like): their Python driver cannot start the `relinker` executable, which is not in this build tree. `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted` fail because of the WindowsApps Python on this machine. `agc_driver_store_over_untracked` and `agc_driver_direct_write_completion` are GPU tests; with `Thread.cpp` and `Posix/Thread.cpp` reverted to `main` the second still fails, so they do not depend on this change. Nothing thread related fails. - Rebased onto `main` 7f64d6f057 (2026-11-13), resolving the conflict with #17091 as above: `guest_pthread_rename`, including the #17091 cases, passes three runs out of three. The full-suite run above is from the previous base. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. ### Overlaps `tools/pr_overlap.py` reports no conflicting file or export. It lists #3986, #4563, #8592, #9447, #12130 and #12642 as sharing `Thread.cpp` or `Posix/Thread.cpp` without comparing them, because those PRs conflict with `main` themselves; none of their diffs changes `scePthreadRename`, `pthread_rename_np` or the code around them (#12130 and #12642 only call `scePthreadRename` with short host names in new tests, which still work). The other listed PRs only append to `docs/dev/debt/libkernel.md`. ### 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 - [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)
gabichuuuuu commented 2026-11-08 21:44:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T21:44:30Z.

Review: COMMENTED


Windows run of head cd61402131, merged onto main 095ff72367 in one tree with other open PRs that touch different files (each PR's test is its own file, so they don't interact). Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_pthread_rename with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's two Thread.cpp files and this PR's test: 5 / 5 runs fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819795201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15567#pullrequestreview-5819795201). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T21:44:30Z. **Review:** COMMENTED --- Windows run of head `cd61402131`, merged onto `main` `095ff72367` in one tree with other open PRs that touch different files (each PR's test is its own file, so they don't interact). Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `guest_pthread_rename` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s two `Thread.cpp` files and this PR's test: 5 / 5 runs fail.
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!15567
No description provided.