Repository navigation
fix(libkernel): return EFAULT from the AIO submit calls for an unwritable id output before running the requests - #19702
Conversation
…able id output before running the requests SubmitCommands checked the id output only for null and Publish stored the ids raw, so an unmapped or read-only id crashed the process after every read and write had already run. The submit calls now check the id output with a host-write guard and GuestWritable before reserving or running any request and return SCE_KERNEL_ERROR_EFAULT, as the poll, wait, cancel, delete and set-param exports do for an unwritable output. Publish copies the ids out with GuestCopyOut outside the registry lock and releases the slots if that copy fails. GuestAio passes an unmapped and a read-only id to all four submit calls and checks that the buffers, results and file stay untouched.
|
Linux run for this branch: 39df4bf merged onto
Code: if |
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
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.
|
What
SubmitCommands(core/libs/prx/libkernel/Aio/src/Aio.cpp:221-234onmain1d1ff11), which backssceKernelAioSubmitReadCommands,sceKernelAioSubmitWriteCommandsand theirMultipleforms (:357-371), checks the id output only for null (:222). It then reserves the ids (:230), runs every request with real file reads and writes (:231) and callsReservation::Publish(:232), which storesoutput[index] = ids[index]through the raw guest pointer underg_mutex(:117-124). So an unmapped or read-only id output crashes the host with an access violation after the reads and writes have already happened. The poll, wait, cancel and delete exports andsceKernelAioSetParamin the same file copy their output withGuestCopyOut_nid_no_patchand returnSCE_KERNEL_ERROR_EFAULTwhen they cannot (CopyStateOut:244-246,CopyStatesOut:252-255,sceKernelAioSetParam:354), documented as a host-safety fallback indocs/dev/debt/libkernel.md:84(#14417, #18169). For a call with side effects before its output,sceZlibGetResult(core/libs/prx/libSceZlib/Export.cpp:133-141) checks the outputs first with aGuestArena::HostWriteguard andGuestWritable_nid_no_patch, saving and restoringerrno. Console behaviour of the submit calls on a bad id output is unmeasured; #18766's PS5 probe did not cover them.EFAULT; bad size or priority:EINVAL; unreadable request array or null result:EFAULT) and before theReservation,SubmitCommandschecks that the id output is writable for(multiple ? size : 1)int32_tvalues, with aGuestArena::HostWriteguard andGuestWritable_nid_no_patch(core/libs/prx/libc/src/General.cpp:827), and returnsSCE_KERNEL_ERROR_EFAULTwithout reserving or running any request.Reservation::Publishmarks the slots completed underg_mutex, then copies the ids out withGuestCopyOut_nid_no_patch(General.cpp:864) outside the lock, and returns false if that copy fails;publishedthen stays false, the destructor frees the slots andSubmitCommandsreturnsSCE_KERNEL_ERROR_EFAULT. That path is only reachable if the page is unmapped or reprotected between the check and the copy. A valid id output takes the same path with the same ids and states.RunRequest, its rawreq.resultstores and the payload handling are not touched.docs/dev/debt/libkernel.md:84said the submit calls still access the requests, results and ids directly, which has been stale for the requests since fix(libkernel): copy AIO submit requests before use #18823. It now says they copy the requests in and the ids out, returnSCE_KERNEL_ERROR_EFAULTfor an unwritable id output, checked before any request runs, and still write the results directly; console fault behaviour stays unmeasured. The entry is 993 characters (limit 1000).guest_aio(core/libs/tests/GuestAio.cpp:457-481) opens the data file read/write and, for the unmapped address0x10and aPAGE_READONLY/PROT_READpage fromMapReadOnlyPage, calls each of the four submit functions with one and with two requests whose buffer holdsmnopand whose results start as{-1, 0}. Each call must returnSCE_KERNEL_ERROR_EFAULT, the buffer must still holdmnop(no read ran), both results must be unchanged and, after all 16 calls, the file contents must equal a snapshot taken before them (no write ran). No new ctest is registered.pr_overlap.py --branchreports no shared lines or exports with the open pull requests it can compare (mainf36f094). It cannot compare fix(libkernel): provide default arguments block for DT_INIT on Linux #6149, feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek #6936, feat(libkernel): report open, read and close notes on watched files #7121, fix(threads): run Windows pthread and small fiber stacks in guest VA range #7149, feat(libc): support archive-backed stdio reads #7447, feat(libkernel): implement four pthread_attr forms #9156, feat(libkernel): implement the named ksem semaphores #9293, feat(libkernel_sys): implement POSIX message queues #9297, feat(libkernel_sys): implement kmq_notify registration #9302, feat(libkernel_sys): implement sigqueue #9389, feat(libkernel): keep the SIGIO owner of a pipe for F_GETOWN and F_SETOWN #9557, feat(libkernel): answer tcgetpgrp and tcsetpgrp on a pipe with its SIGIO owner #9566, fix(libkernel): honor caller-provided pthread stacks #9938, perf(agc): store an unimported label at once when unfinished work leaves guest memory alone #10442, feat(libkernel): implement __Ux86_64_setcontext #11215, feat(relinker): execute supported scalar guest GS accesses on Windows #11628, feat(relinker): support short scalar guest GS instructions #11661, feat(relinker): support SSE guest GS memory accesses #11689, feat(relinker): support VEX guest GS memory accesses #11698, fix(libkernel): wait for the rest of a MSG_WAITALL stream receive in the guest layer #11896, feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700, feat(libkernel): support deferred cancellation in POSIX accept #12965, fix(libkernel): validate null pointers in pthread rwlock functions #14108, fix(libkernel): validate clock_settime before EPERM #17551, feat(libSceRtc): implement the measured sceRtcSetConf timezone contract #17609, fix(libkernel): setname on a condition variable, mutex, rwlock and barrier checks the name like the console #17638, UFC 6 profiling: tracking draft (not for merge) #18515, which do not merge withmain; all of them sharedocs/dev/debt/libkernel.md, and feat(libkernel): report open, read and close notes on watched files #7121 alsocore/libs/prx/libkernel/Aio/src/Aio.cpp. Checked by hand: a trial merge of this branch with each of them conflicts in the same files and the same hunks asmainwith it, and none of them changesSubmitCommands,Reservation::Publishor the submit exports.pr_overlap.py --nameslists only feat(libs): mirror libkernel exports through libkernel_web #18112 and feat(libs): mirror libkernel exports through libkernel_sys #18190 for the touched exports; they mirror libkernel exports through libkernel_web and libkernel_sys and do not changeAio.cpp.Tested
Windows 11, WinLibs GCC 15.2.0, Ninja, Release,
BUILD_TESTING=ON, build directory configured through its 8.3 short path. Built withcmake --build <build> --parallel 4 --target guest_aio_tests guest_aio_registry_tests guest_memory_tests guest_kernel_app_archive_tests agc_driver_storage_file_write_tests(guest_aio_registry_testscompilesAio.cppdirectly,core/libs/prx/libkernel/CMakeLists.txt:664-666). Before every run the.prxfiles in<build>/testswere refreshed from<build>/core/libs/libs/unpatchedandlibkernel.prx(andlibSceAgcDriver.prxfor the AGC test) had the same MD5 in both places.Aio.cppof 1d1ff11:ctest --test-dir <build> -R '^guest_aio$' --output-on-failurefailed with***Exception: SegFault,FATAL: unhandled exception 0xc0000005,rip ... libkernel.prx+0xc8be3,write of 0x0000000000000010, the store inPublishafter the request had run.ctest --test-dir <build> -R '^guest_aio$' --output-on-failurepassed (1/1).GuestAio.cpp,GuestAioRegistry.cpp,GuestKernelAppArchive.cpp,GuestMemory.cpp),guest_aio_registry_testsalso compilingAio.cppdirectly:ctest --test-dir <build> -R '^(guest_aio(_.+)?|guest_memory|guest_kernel_app_archive_.+)$'passed 12/12 (guest_aio,guest_aio_identity,guest_aio_admission,guest_aio_rollback,guest_aio_identity-exhaustion,guest_aio_processing,guest_aio_processing-rollback,guest_aio_bad_descriptors(Windows only),guest_kernel_app_archive_archive,guest_kernel_app_archive_plain,guest_kernel_app_archive_mmap,guest_memory), 3.72 s in total.agc_driver_storage_file_write(core/libs/prx/libSceAgcDriver/tests/execution/StorageFileWrite.cpp), which submits writes with a valid id while the AGC host-write hook is set, now hit by the id check and the copy-out:ctest --test-dir <build> -R '^agc_driver_storage_file_write$' --output-on-failurepassed (1/1, 3.01 s); after the debt wording change the 12 ctests above passed again.Aio.cppreverted to 1d1ff11 and the test kept,guest_aiofailed again with the samewrite of 0x0000000000000010inlibkernel.prx+0xc8be3; with the change restored and rebuilt,guest_aiopassed and-R '^guest_aio(_.+)?$'passed 8/8.MapReadOnlyPageusesmmap(PROT_READ)there.GuestWritable_nid_no_patchrejects an unmapped or non-writable range through the host mappings on Linux andmach_vm_regionon macOS (General.cpp:842-856), and an address no mapping covers givesstd::nulloptfromHostMapping::Find(core/libs/prx/libc/include/specifics/linux/HostMapping.hpp:115-129) rather than an exception, the non-WindowsGuestArenaBeginHostWrite_nid_postfix(core/libs/prx/libc/src/GuestArena.cpp:941-949) does not make a read-only page writable (on macOS it only restores the protection a watched page already has,core/libs/prx/libc/src/GuestWriteWatch.cpp:603-615), so the check fails there too, and without the change the raw store faults on those hosts as well.python tools/check_conventions.py --base upstream/main --head HEAD(upstreammain= 1d1ff11) with this title and description passed.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)