Repository navigation
fix(libc): store _Mtx_init's and _Cnd_init's handles through checked guest copies - #24466
Conversation
…guest copies _Mtx_init, _Cnd_init, _Cnd_destroy and std::__sce_v2::_Mtx_init_with_name read and wrote the caller's handle slot straight through the guest pointer, so an unmapped or read-only slot crashed the host. The slot is now read with GuestCopyIn_nid_no_patch and written with GuestCopyOut_nid_no_patch, and an unusable slot throws std::invalid_argument, as a null slot already does. _Mtx_init and _Cnd_init clear the slot before allocating, so a rejected slot allocates nothing, and free the new object if the final store fails. _Cnd_destroy clears the slot before it frees the condition, so a read-only slot leaves the condition alive.
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against a4af382870:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_libc_threads_testsbuilt cleanly. - CTest (
guest_libc_threads):1/1 Test #1298: guest_libc_threads ............... Passed 0.51 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libc/src/Threads.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #1298: guest_libc_threads ...............***Exception: SegFault 3.48 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Threads.cpp: _Mtx_init_nid_postfix correctly validates and writes nullptr to the guest handle slot before allocating DinkumwareMutex, and if the second GuestCopyOut fails, it deallocates the mutex before throwing std::invalid_argument to prevent leaks.
- Threads.cpp: _Cnd_init_nid_postfix similarly tests handle writability by writing nullptr prior to allocation and frees DinkumwareCondition on secondary GuestCopyOut failure.
- Threads.cpp: _Cnd_destroy_nid_postfix safely reads current handle with GuestCopyIn, verifies non-null, checks writability by clearing handle slot with GuestCopyOut BEFORE destroying condition to avoid leaving a dangling pointer if unwritable.
- Threads.cpp: std::__sce_v2::_Mtx_init_with_name clears the guest mutex slot using GuestCopyOut before invoking sync.mutexattrInit / scePthreadMutexInit, preventing unmapped/read-only pointer faults.
- GuestLibcThreads.cpp: UnusableHandlePointers test exercises read-only and unmapped pointers across all modified functions on both POSIX (mprotect) and Windows (VirtualProtect) without leaking resources or leaving dirty state.
Looks Good To Me
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
The changed code and tests at head a4af382870aacf105785e345e35ea06c29ad4d85 show no verified blocking defect; I could not run the reported checks in this review session.
What I ran
Read the prepared diff and all changed lines in both changed files at the pinned head. No commands or tests were run.
Results
| Check | Result | Note |
|---|---|---|
| Changed lines reviewed | passed | Both changed files read |
| Author-reported test suite | not run | Pass reported in the description |
| Review-session tests | not run | No command execution available |
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not run the test suite, mutation check, or reported audit tools. The author reports these passed on Arch Linux with GCC in a Release build. No files were excluded from the supplied changed-file listing; core/libs/prx/libc/src/Threads.cpp and core/libs/tests/GuestLibcThreads.cpp were read.
To get this to a good state
Nothing is required.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Windows test run of a4af382 merged onto current Existing runs. PrivacyOnion ran the test and the same negative control on Linux at a4af382. The PR body reports Arch Linux runs. I found no Windows run at this head. What this run adds. The read-only slots here come from Code.
PR body. One line is broader than the test. The body says each init function throws "for an unmapped ( Suggestion. One loop, which I added locally only, after for (void** const slot : {unmapped, readOnlyNull}) {
RequireThrows<std::invalid_argument>([&] { _Mtx_init_nid_postfix(slot, plain); });
RequireThrows<std::invalid_argument>([&] { _Mtx_init_with_name_nid_postfix(slot, plain, "slot"); });
RequireThrows<std::invalid_argument>([&] { _Mtx_init_with_default_name_override_nid_postfix(slot, plain, "slot"); });
RequireThrows<std::invalid_argument>([&] { _Cnd_init_nid_postfix(slot); });
RequireThrows<std::invalid_argument>([&] { _Cnd_init_with_name_nid_postfix(slot, "slot"); });
RequireThrows<std::invalid_argument>([&] { _Cnd_init_with_default_name_override_nid_postfix(slot, "slot"); });
RequireThrows<std::invalid_argument>([&] { _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(slot, plain, "slot"); });
RequireThrows<std::invalid_argument>([&] { _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(slot, plain); });
}With the loop, the merge passes 3/3. The loop catches no extra mutation. It only makes the test match the body. Negative control and mutations. Each row is one change to
Tests.
On On the merge,
AI-assisted: yes |
|
Windows result for head Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release,
AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine. |
What
_Mtx_init,_Cnd_init,_Cnd_destroyandstd::__sce_v2::_Mtx_init_with_name(Threads.cpp) read and wrote the caller's handle slot (void**) straight through the guest pointer, so an unmapped or read-only slot crashed the host. The slot now goes throughGuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch, and an unusable slot throwsstd::invalid_argument, as a null slot already does:_Mtx_initand_Cnd_initclear the slot before allocating, as before, so a rejected slot allocates nothing. If the final store of the new handle fails, the new object is freed before the throw._Mtx_init_with_name,_Mtx_init_with_default_name_overrideand_Cnd_init_with_default_name_overrideforward to them and are covered too._Cnd_destroycopies the handle in and resolves it from the host copy. It clears the slot before freeing the condition, so a read-only slot throws and leaves the condition alive and usable instead of freeing it and then faulting.std::__sce_v2::_Mtx_init_with_name(and_Mtx_init, which forwards to it) clears the slot with a checked write, so an unwritable slot throws before the libkernelscePthreadMutexInitcall writes through it.The mutex and condition objects themselves are host allocations owned by libc (
new DinkumwareMutex/DinkumwareCondition), so the handle values are opaque host pointers, not guest memory. Only the slot that holds them is guest memory.Results for valid slots are unchanged.
Not changed:
_Mtx_destroy(open #18347 edits it), and the timed waits andTimespec64helpers (#22515).Tested
Arch Linux, GCC, Release build. Re-run after rebasing onto
origin/main0a025bd.ctest --test-dir build -R '^(guest_ampr_threads|guest_cnd_at_thread_exit|guest_libc_cnd_init_default_name|guest_libc_cnd_timed_wait|guest_libc_internal_mtx|guest_libc_internal_pad|guest_libc_mutex_init_with_name|guest_libc_named_exports|guest_libc_shared_mutex|guest_libc_static_imports|guest_libc_threads|public_headers)$': passed.UnusableHandlePointersinGuestLibcThreads.cpp:0x10) slot and for a slot on a read-only page._Cnd_destroythrows for an unmapped slot and returns for a read-only slot holding null._Cnd_destroyon a read-only slot that holds a live condition throws. The condition still signals and is destroyed normally afterwards._Mtx_initstores makes the new test abort.python3 tools/progress.py --module libc: implemented count unchanged (1850).python3 tools/guest_pointer_audit.py libcno longer lists the four functions.tools/pr_overlap.py --branch fix/libc-mutex-condition-init-guest-pointers: #1835 and #9447 conflict withmainand could not be compared. #9447 is a stale PR that addsstd::__sce_v2::_Mtx_init_with_name, whichmainalready implements. #1835 does not touch these functions.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)