fix(libkernel): read and write the event flag and mutex init pointers through checked copies #22055
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22055"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-17T03:30:38Z.
What
Two groups in libkernel that read or wrote guest pointers directly, so an unmapped pointer crashed the host. One commit each.
Event flags (
EventFlag/src/EventFlag.cpp):sceKernelPollEventFlagandsceKernelWaitEventFlagwrote the result pattern throughresult_patin place, on every path (satisfied, busy, deleted, canceled, timed out). The wait also read*timeoutand wrote the remaining time back in place.GuestCopyOut_nid_no_patch.Consumereturns the pattern instead of writing it.GuestCopyIn_nid_no_patch, still only when it actually waits, and writes the remaining time back throughGuestCopyOut_nid_no_patch.SCE_KERNEL_ERROR_EFAULTwitherrnopreserved, assceKernelCancelEventFlagalready does for its wait count.StoreWaitCountbecame theStoreGuesttemplate that all three use.Mutex init (
Pthread/src/Mutex.cpp):scePthreadMutexInitread*attrand stored the new mutex into*mutexin place. It now reads the attribute handle throughGuestCopyIn_nid_no_patch, and checks the slot reads and writes back before the store, aspthread_mutex_initalready does (_slotUsable). An inaccessible attribute pointer or slot returnsSCE_KERNEL_ERROR_EFAULT, the resultLoadAttributeandscePthreadMutexDestroygive for the same pointers. A fourth commit (after review) also copies the attribute object the handle points to throughGuestCopyIn_nid_no_patch, inscePthreadMutexInitand inpthread_mutex_init, which checked the type through the handle in place before calling it. Both work from the copy, so a readable slot holding an unmapped handle returnsSCE_KERNEL_ERROR_EFAULT(EFAULTfor the POSIX call).Rebased (11-17) onto
mainwith #22073, which added a zero-timeout early return tosceKernelWaitEventFlag. That return now reads the timeout from the checked copy (limit == 0) and reports the pattern throughReportPattern, like the other paths.Every result for valid pointers is unchanged. The debt fragment records both as host-safety fallbacks, and notes that a satisfied poll or wait has already cleared its bits when the pattern cannot be written.
The scan also flagged
sigaction,sigaltstackandkevent, but those already copy throughGuestCopyIn/GuestCopyOutonmain, so they are not touched.Tested
Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release,
-DBUILD_TESTING=ON:19b55246e7(MarcZX10's review):pthread_mutex_initdecides the defaultErrorChecktype from the attribute handle it already copied through the checked copy, and reads the new mutex from the slot through a checked copy, instead of reading*attrand*mutexagain.guest_pthread_mutex_errors,guest_pthread_mutexattr,guest_posix_mutex_trylock,guest_pthread_mutex_contention, the threeguest_event_flag*tests,guest_kernel_errorsandguest_semaphorepass in each of 3 runs. There is no test for the race the old second read allowed (the attribute becoming inaccessible between the two reads).898d1d056c(CodeRabbit's suggestion):guest_pthread_mutex_errorsalso passes a readable, read-only slot holdingnullptrtoscePthreadMutexInit, which must returnSCE_KERNEL_ERROR_EFAULTand leave itnullptr. The six tests pass in each of 3 runs (Linux; the WindowsVirtualProtectpath of the new case has not been run).fc183c4ffc(main008763512bmerged in; corrected 11-27: this line first namedfffb30ef5b, a latermaincommit):mainhad rewritten the event flag waiters (a waiter list withreleaseWaiter, and its own checkedStoreWaitCount), so the merge re-applies this change ontomain's version ofEventFlag.cpp:result_patand the remaining timeout are written, and the timeout read, through the checked copies,Consumereturns the previous pattern instead of writing it, andmain's waiter handling is kept as it is.guest_event_flag_wake,guest_event_flag_clear_all_queue,guest_event_flag_released_waiter,guest_pthread_mutex_errors,guest_kernel_errorsandguest_semaphorepass in each of 3 runs.guest_event_flag_wakegains a case, run in each of its 20 rounds witherrnoheld at 71:0x10) result pointer makessceKernelPollEventFlag(satisfied and busy) andsceKernelWaitEventFlag(satisfied) returnSCE_KERNEL_ERROR_EFAULTand keeperrno;SCE_KERNEL_ERROR_EFAULTbefore it waits, leaving no waiter;mprotect, orVirtualProtecton Windows) is read, the wait times out, and the write-back of the remaining time fails: the call returnsSCE_KERNEL_ERROR_EFAULT, keepserrno, writes the result pattern and leaves the timeout at its value (third commit, added after review).guest_pthread_mutex_errors:CheckBadPointersalso callsscePthreadMutexInitwith an unreadable attribute pointer and with an unwritable slot, both0x10and aPROT_NONEguard page; each returnsSCE_KERNEL_ERROR_EFAULT, and the slot passed with the bad attribute stays null. With the fourth commit it also passes an attribute slot that holds the unreadable pointer as the handle, toscePthreadMutexInit(SCE_KERNEL_ERROR_EFAULT) andpthread_mutex_init(EFAULT), and the slot stays null.guest_event_flag_wakepasses 3 runs again.main'sEventFlag.cppandMutex.cppand the new cases, both tests end infatal SIGSEGV (code 1) at address 0x0000000000000010.main7ca28abfd5,guest_event_flag_wake,guest_pthread_mutex_errorsand #22073'sguest_kernel_errorsandguest_semaphorepass, 3 runs of 4, at the rebase and again with the fourth commit. Withmain'sEventFlag.cppandMutex.cpp(andPosix/Mutex.cppfor the fourth commit), the mutex and event flag tests end infatal SIGSEGV (code 1) at address 0x10again.git merge-treeagainstmainis clean. None of the four open PRs that editPthread/src/Mutex.cpp(#21464, #17638, #12642, #12130) touchscePthreadMutexInit, and #18515 (a profiling draft) is the only other open PR onEventFlag.cpp. Merging this branch intomainadds no conflicts for any of the five; #18515 already conflicts withmaininEventFlag.cpp.tools/check_conventions.pypasses on the branch and this body.tools/debt_fold.py --checkreports nothing for this fragment (mainitself fails it on two LibreSSL3 fragments that change the same entry).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T03:36:04Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
a1d6a472f4:tools/check_conventions.pypassed.guest_event_flag_wake_tests,guest_pthread_mutex_errors_testsbuilt cleanly.guest_pthread_mutex_errors,guest_event_flag_wake):core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp,core/libs/prx/libkernel/Pthread/src/Mutex.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T03:55:10Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional. The event flag and mutex init changes do what the description says in the code I read, but no build or test was run in this pass.
What I ran
a1d6a472f4adeaf1b66a98cae26711f211c46020, basemain.LoadAttributeincore/libs/prx/libkernel/Pthread/src/Mutex.cpp, plus the_patternand_waitersdeclarations inEventFlag.cpp.Results
main; not reproducedFindings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/Pthread/src/Mutex.cpp:222and:226-228: the attribute handle is now read throughGuestCopyIn_nid_no_patch, but the handle's fields (type,protocol,ceiling) are still read in place. A readable attribute slot holding an unmapped handle still faults the host. This is the same shape asLoadAttribute, and the code was not made worse, but the description's "inaccessible pointer returnsSCE_KERNEL_ERROR_EFAULT" covers only the attribute pointer.core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp:379: the remaining-time write-back has no test. A timeout that is readable but not writable would exercise it;GuestEventFlagWake.cpponly covers an unreadable timeout.Not verified
debt_foldcheck: not run here. The author's reports are not confirmed.sigaction,sigaltstackandkeventalready use the checked copies: not checked.EventFlag.cppandMutex.cpp. No files were excluded by rule.To get this to a good state
Nothing is required. The two non-blocking points above are optional.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T04:12:28Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
11e1aabbb4:tools/check_conventions.pypassed.guest_event_flag_wake_tests,guest_pthread_mutex_errors_testsbuilt cleanly.guest_pthread_mutex_errors,guest_event_flag_wake):core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp,core/libs/prx/libkernel/Pthread/src/Mutex.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T06:02:02Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; the points below are optional.
The change does what the description says at head 11e1aabbb40d8718017f8c6b095a87c2498ccf1b: every changed hunk reads correctly, and I found no blocking defect. I did not build or run anything in this review.
What I ran
repo_read.py:Results
_waitersisint;GuestCopyIn_nid_no_patch(dst, src, size)check_conventions.pyanddebt_fold.pynot runFindings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/Pthread/src/Mutex.cpp:222readshandle->typein place. The attribute pointer and the slot are now checked, but a slot that holds an unmapped handle still faults the host. The debt note's host-safety wording covers the pointer and the slot, which is accurate, so this is a known limit and a candidate for a follow-up.core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp:379writes the remaining time after a timed wait. If that write fails on a wait that was already satisfied, the function returnsSCE_KERNEL_ERROR_EFAULT(line 397) after the bits were consumed. The debt note records this. Keep that note in the PR.Sources
github.com/boykopovar/AnyPS5@11e1aabbb4/core/libs/prx/libkernel/Pthread/src/Mutex.cpp (L222)github.com/boykopovar/AnyPS5@11e1aabbb4/core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp (L379)Not verified
GuestEventFlagWakeandGuestPthreadMutexErrors, were not built or run. The author's 3 runs and the negative control (SIGSEGV at 0x10 withmain's sources) are author-reported and not reproduced.ReadOnlyTimeout(VirtualAlloc,VirtualProtect) was read in the diff only and was not compiled.scePthreadMutexInitwas compared withpthread_mutex_initonly by name:_slotUsableexists inPthread/Posix/Mutex.cpp(line 42), but its body and its use there were not read.tools/check_conventions.pyandtools/debt_fold.pywere not run, so the debt fragment's format is not verified beyond reading it.diff.patch;EventFlag.cpplines 40–110 and 338–407;Mutex.cpplines 1–60 and the diff. Files excluded by rule: none.To get this to a good state
Nothing is required. Optional: keep the debt note as it is, and consider a follow-up for the unvalidated
handleread inscePthreadMutexInit. The author's test runs would be worth repeating on the new head if the branch changes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T08:48:35Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional. This review is diff-level only, so it is a COMMENT and not an approval.
What I ran
e0c3a32260e51635927fa0c0c168ffb4ea71df6c, basemain. No merged-preview check was run.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/Pthread/src/Mutex.cpp:222:scePthreadMutexInitreads the attribute handle through the checked copy, but then dereferences it in place (handle->type,handle->protocol,handle->ceiling). A guest can store an unmapped pointer in the attribute slot, so the host can still fault on this path. This is not a regression, since the old code also read(*attr)in place, but the title says the attribute is read through checked copies. Either check the attribute object too, or narrow the description to say so.core/libs/tests/GuestPthreadMutexErrors.cpp:88: the new cases cover an unreadable attribute pointer and an unwritable slot. Nothing covers an attribute slot that holds an unmapped handle, which is the case in the previous point.Not verified
files.jsonlists all five underreviewed, but I did not read them in full.scePthreadMutexattrInit: I found its location (core/libs/prx/libkernel/Pthread/src/Mutex.cpp) but did not read its body, so the claim that the attribute handle points at the library's own object is not confirmed.GuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patch: I relied on their use in existing callers and did not read their definitions.TimedWait::DeadlineNanosandTimedWait::RemainingMicros: not read.mainwith #22073, the merge-tree result, and the open-PR overlap claims: not checked.tools/check_conventions.pyandtools/debt_fold.py: not run.Excluded by rule: none. Read: the description, commits, manifest, diff, and the house-rule sections. The five reviewed files were read as diff hunks only.
To get this to a good state
Nothing is required.
Optional, as above: check the attribute object as well as the slot in
scePthreadMutexInit, or narrow the title and description. Add a test for an unmapped attribute handle.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T09:05:41Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
fedfeb1217fc186874346d574e9c811ad5e8bd62, basemain.manifest.json,files.jsonanddiff.patchfrom the prepared review folder.Results
Findings
Blocking
None found in the hunks read.
Non-blocking
core/libs/prx/libkernel/EventFlag/src/EventFlag.cpp:384: a wait that is satisfied and then fails to write back the remaining time still returnsSCE_KERNEL_ERROR_EFAULTafter the bits were cleared. The PR body and the debt fragment already record this.core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp:85:pthread_mutex_initcopies the attribute object, andscePthreadMutexInitcopies it again. The result is correct, but the second copy could be dropped.Not verified
GuestEventFlagWake.cpp,GuestPthreadMutexErrors.cpp), including the WindowsReadOnlyTimeoutpath.Api.hppprovidesGuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patchwith the signatures used incore/libs/prx/libkernel/Pthread/src/Mutex.cpp. I confirmed the calls exist in the repository, not their declarations.scePthreadMutexInit(core/libs/prx/libkernel/Pthread/src/Mutex.cpp:235), which runs underInitializationMutex.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T13:06:56Z.
Review: COMMENTED
0deb96c2-c64d-433b-b25d-8bd5f6129fd2Reviewing files that changed from the base of the PR and between
008763512bandfc183c4ffc.core/libs/prx/libkernel/EventFlag/src/EventFlag.cppcore/libs/prx/libkernel/Pthread/Posix/Mutex.cppcore/libs/prx/libkernel/Pthread/src/Mutex.cppcore/libs/tests/GuestEventFlagWake.cppcore/libs/tests/GuestPthreadMutexErrors.cppdocs/dev/debt/changes/fix-libkernel-eventflag-mutex-guest-copies.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T14:42:38Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The POSIX mutex initializer can still crash on an inaccessible attribute pointer after the checked copy succeeds.
What I ran
Reviewed head
898d1d056cb7ef266fdb2e5e66742dd700f3ef66; no commands or tests were run. Read all six changed files listed as reviewed inmanifest.json, along with the diff and prepared description and commit list.Results
Findings
Blocking
core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp:91: afterscePthreadMutexInitsafely copies the attribute handle and pointed-to object, this wrapper still evaluates!*attrdirectly. A non-null inaccessibleattrtherefore reaches an unchecked guest-memory read and can crash the host, contrary to the claimed checked-copy behavior. Use the already checked local handle (and preserve it across the call) to decide whether to set the default type.Non-blocking
None.
Not verified
No tests or builds were run. All six changed files were read; no changed files were excluded (
files.jsonwas not present in the prepared review folder).To get this to a good state
Remove the unchecked
*attrread from the POSIX initializer and use its checked local handle; then rerun the mutex error tests, including the inaccessible attribute-pointer case.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T15:06:43Z.
Review: APPROVED
Reviewed — looks good
The reviewed change addresses the described guest-pointer accesses, and the inspected code provides no verified blocking defect; runtime checks were not run in this review.
What I ran
Read the prepared change description, commit list, manifest, diff, and full text of all six changed files using the pinned-head reader. No tests or prechecks were run. The author reports Linux test runs in the description; those were not performed here.
Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No runtime tests, prechecks, or Windows checks were run. The author reports Linux test runs and a negative control in the description; these reports were not independently verified.
To get this to a good state
Nothing is required.