Skip to content

fix(libkernel): read pthread_rwlock_destroy's rwlock slot through scePthreadRwlockDestroy - #2081

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

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

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#28302, originally opened by kallmek3nny.

Source commit: 9a4207320a12e98f6bc054efee52e0fe97a1eb0f. 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

pthread_rwlock_destroy read *rwlock directly to return 0 for a statically initialized (null) rwlock before calling scePthreadRwlockDestroy, so an unreadable rwlock pointer crashed the host (tools/guest_pointer_audit.py libkernel lists it as pthread_rwlock_destroy_nid_postfix: parameter rwlock (dereference)).

scePthreadRwlockDestroy already reads the slot with GuestCopyIn_nid_no_patch: it returns EFAULT for an unreadable slot and SCE_OK for a null one. The direct read is dropped, so the POSIX form now returns EFAULT (14) for an unreadable rwlock, through the existing toPosix mapping, and keeps returning 0 for a null slot without changing it. A null rwlock pointer still throws.

Tests: TestRwlockInitAndDestroyLifecycle in core/libs/tests/GuestKernelPthreadRwlockBounds.cpp now checks that pthread_rwlock_destroy and scePthreadRwlockDestroy return EFAULT for the unmapped pointer 0x10, and that pthread_rwlock_destroy of a null slot returns 0 and leaves it null.

Overlap:

Tested

Windows 11, WinLibs GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7), Ninja, Release, -DBUILD_TESTING=ON, base 1f27e2abfa. No title or homebrew.

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

Summary by CodeRabbit

  • Bug Fixes
    • Corrected read-write lock destruction behavior for null handles and invalid lock-pointer addresses, returning the appropriate result instead of incorrectly reporting success.

…PthreadRwlockDestroy

pthread_rwlock_destroy read *rwlock directly to return 0 for a statically initialized rwlock before calling scePthreadRwlockDestroy, so an unreadable rwlock pointer crashed the host. scePthreadRwlockDestroy already reads the slot with GuestCopyIn_nid_no_patch, returning EFAULT for an unreadable one and 0 for a null slot, so the direct read is dropped and the POSIX form now returns EFAULT (14) for an unreadable rwlock, as other pthread rwlock functions do. A null rwlock still throws.
@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Rwlock destruction

Layer / File(s) Summary
Destroy behavior and lifecycle checks
core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp, core/libs/tests/GuestKernelPthreadRwlockBounds.cpp
The wrapper passes non-null rwlock pointers to scePthreadRwlockDestroy and converts the result with toPosix. Tests check error codes for unreadable pointer addresses and success when destroying an initialized-null handle.

Change: Bug fix

Merge Risk: Minimal · up to 9a420

Destroying a rwlock through the POSIX API now returns EFAULT for an unreadable lock slot instead of faulting. Destroying a null handle still succeeds. Tests cover both cases, and no merge-blocking risk remains.

Security architecture risk: Minimal · up to 9a420

libkernel now routes POSIX rwlock destruction through the existing checked SCE path before reading the guest slot. An unreadable slot can return EFAULT instead of faulting on the wrapper’s direct read. A readable slot that stays empty still returns success without a write, and live locks follow the existing destruction path.

Security review details

Security Blast Radius

  • observed — The production change is confined to the existing POSIX destroy entrypoint. Direct SCE callers and the lock-deletion branch are unchanged.

Trust Boundaries and Controls

  • observed — The guest-supplied slot address now reaches libc’s checked copy through SCE before the first direct slot access. This is a readability probe, not a guarantee that the slot remains mapped or unchanged between the probe and the later load.
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: 1
  • Waived: $0.25 (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: 3975fe45-400b-4f40-a11a-9efd57c29076
Commits

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

Files selected for processing: 2

  • core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp
  • core/libs/tests/GuestKernelPthreadRwlockBounds.cpp
Files with no reviewable changes: 1

  • core/libs/prx/libkernel/Pthread/Posix/Rwlock.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.

1 participant