Skip to content

fix(libkernel): load scePthreadAttrSetstackaddr's attribute through a guest copy - #1099

Open
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/24470
Open

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/24470

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#24470, originally opened by domx4q.

Source commit: b93ff534e10606d6fba3546b40bd17cc8db1cfb7. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

scePthreadAttrSetstackaddr read the attribute handle with *attr straight from the guest slot, so an unmapped slot crashed the host. Every other scePthreadAttr* setter and getter already loads the handle through LoadAttribute, which copies it in with GuestCopyIn_nid_no_patch and returns SCE_KERNEL_ERROR_EFAULT for an unreadable slot. scePthreadAttrSetstackaddr now uses LoadAttribute too, as scePthreadAttrSetstack beside it does.

A null attr, a slot holding a null handle and a null addr still return SCE_KERNEL_ERROR_EINVAL. With a usable slot the result is the same as on main. The only change is for an unreadable slot: it now returns EFAULT instead of crashing, even when addr is null, because the slot is read first. The attribute object itself is a host allocation made by scePthreadAttrInit, so writing stackAddress through the loaded handle is not a guest access.

scePthreadAttrGet in the same file also reads *attr directly. It is left alone because open PRs boykopovar#8041 and boykopovar#9938 change its body.

Not changed (false positives or covered elsewhere): the rest of the libkernel tools/guest_pointer_audit.py list.

  • sysctl, sysctlbyname, sysctlnametomib, sigaction, sigaltstack, sceKernelFutimes, sceKernelCheckReachability, pthread_attr_getschedparam and scePthreadAttrGetschedparam already copy their arguments in or out and then rebind the parameter to the host copy, or only take a member's address for StoreOutput. The audit does not follow that rebinding.
  • JoinThread_nid_no_patch only gets host copies from its callers (pthread_timedjoin_np copies the deadline in, scePthreadJoin passes null).
  • scePthreadOnce, scePthreadMutexUnlock and _umtx_op deliberately work on the guest word with std::atomic_ref. scePthreadMutexDestroy already checks the slot with a guest copy first.
  • An open PR already edits most of the others (clocks, POSIX semaphores, rwlocks, conds, mutex init, spin locks, __tls_get_addr, ktimer_create, sockets, dlsym, unwind info, event flag and semaphore waits).

Overlap

Tested

Arch Linux, GCC, Release. guest_pthread_attr (core/libs/tests/GuestPthreadAttr.cpp) has new checks:

  • CheckBadPointers: an unmapped (0x10) slot and an unreadable guard page return SCE_KERNEL_ERROR_EFAULT, with a valid addr and with a null one.
  • a null, unset or destroyed attribute returns EINVAL. On a live attribute, a null addr returns EINVAL, and a set address reads back through pthread_attr_getstackaddr.

Mutation check (repeated after the rebase): with Pthread/src/Attr.cpp reset to origin/main and the new checks kept, guest_pthread_attr segfaults.

Re-run after rebasing onto origin/main 0a025bd: ctest passed all 421 tests registered by core/libs/prx/libkernel plus public_headers (422/422). tools/check_conventions.py --base origin/main passes. tools/guest_pointer_audit.py libkernel no longer lists scePthreadAttrSetstackaddr. tools/progress.py --module libkernel gives 719 done / 39 todo, the same as origin/main.

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 (libkernel thread attributes only; no AGC, recompiler or VideoOut path touched)
  • AI-assisted: yes (Claude Code)

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling when setting a thread attribute’s stack address. Invalid or unreadable attributes now return an appropriate error, and null stack addresses are rejected.
    • Valid attributes continue to accept non-null stack addresses, which can be retrieved through the POSIX stack-address getter.

… guest copy

scePthreadAttrSetstackaddr read the attribute handle straight from the
guest slot, so an unmapped slot crashed the host. It now loads the
handle with LoadAttribute, as scePthreadAttrSetstack and the other
attribute exports do: an unreadable slot returns
SCE_KERNEL_ERROR_EFAULT, and a null slot, a null handle or a null
address still return SCE_KERNEL_ERROR_EINVAL.
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Stack Address Setter

Layer / File(s) Summary
Attribute loading and stack-address validation
core/libs/prx/libkernel/Pthread/src/Attr.cpp, core/libs/tests/GuestPthreadAttr.cpp
The setter uses LoadAttribute and propagates its error. It rejects a null address and assigns a non-null address to the loaded handle. Tests check invalid attributes, null addresses, and retrieval through the POSIX getter.

Suggested reviewers: boykopovar, milescoviello

Change: Bug fix

Merge Risk: Minimal · up to b93ff

This change makes the stack-address setter return an error for unreadable attribute slots instead of crashing, and it keeps the existing errors for null inputs. It is a narrow change with tests and shows no merge-blocking risk.

Security architecture risk: Minimal · up to b93ff

libkernel now uses its existing checked guest-memory read for the stack-address setter. An unreadable attribute slot returns an error instead of being dereferenced directly, and valid calls update the same attribute field as before.

Security review details

Security Blast Radius

  • inferred — The observed production change affects callers of the existing setter and contains a failed slot read before the host attribute mutation. The other changed ranges add test assertions, not production entrypoints.

Trust Boundaries and Controls

  • observed — GuestCopyIn checks whether the handle bytes can be read; it does not authenticate or pin the object referenced by those bytes. Initialization and destruction still manage the host allocation, and the setter adds no new object-lifetime control.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 2
  • Waived: $0.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 24cad76a-9752-4d0a-9e44-4507c9d46469
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and b93ff53.

Files selected for processing: 2

  • core/libs/prx/libkernel/Pthread/src/Attr.cpp
  • core/libs/tests/GuestPthreadAttr.cpp

Limit details: You’ve used all 10 included reviews currently available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants