fix(libkernel): check vector I/O lengths and descriptors before offsets #9732
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9732"
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 simonbronk on 2026-10-22T10:18:27Z.
What
Argument checks in libkernel's vector and positioned I/O now run in the order FreeBSD 11.4 uses:
readv,writev,preadv,pwritevand theirsceKernelforms fail withEINVALwhen the iovec lengths add up to more thanSSIZE_MAX, before the descriptor is looked up, ascopyinuiodoes. Only the Windows path had this check. On Linux a closed descriptor gaveEBADF, and guest sockets, the random device and archived files received the oversized table.kern_preadvandkern_pwritevget the file withfget_read/fget_write, then returnESPIPEfor a file withoutDFLAG_SEEKABLE, and only thenEINVALfor a negative offset.preadvandpwritevchecked the offset first. They now usePositionedArgumentError, the checkpreadandpwritealready use, on Linux and Windows, and that check now follows the same order for all eight functions:EBADFfor a closed descriptor, and for a file, directory or character device that is not open in that direction (a negative offset or a null buffer on anO_RDONLYdescriptor throughpwrite/pwritev, orO_WRONLYthroughpread/preadv, gaveEINVALorEFAULT);ESPIPEfor anything that is not a vnode, character device or the random device: pipes, guest sockets and kqueues. A kqueue gaveEINVALfor a negative offset and read 0 bytes from its host/dev/nullplaceholder otherwise, so it now getsESPIPEfor every offset. FreeBSD marks pipes, sockets and kqueues readable and writable, so none of them reaches the access check;EINVALfor a negative offset, orEFAULTfor a null buffer.File::GrantsAccessreads the host descriptor's access mode through the helperfcntl(F_GETFL)already uses. FreeBSD passes a negative offset on a character device to the driver instead of rejecting it; that still returnsEINVALhere and is in the debt entry. Nothing is measured on a PS5; thereadvdebt entry says these rules follow FreeBSD.Tested
Linux, Ubuntu 24.04 x86-64 container under Rosetta on Apple silicon (Colima), GCC 13, RelWithDebInfo with ASan.
guest_file_vectornow has 30 checks for this change: the 14 from before (a closed descriptor and a pipe with offset -1, a table totallingSSIZE_MAX + 1bytes) and 16 new ones:O_RDONLYfile throughsceKernelPwriteandsceKernelPwritev/pwritevwith offset -1, andpwritewith a null buffer; anO_WRONLYfile the same way through the read forms; each expectsEBADF, and the opposite direction with offset -1 still getsEINVAL;sceKernelPreadv,sceKernelPwritev,preadvandpwritevwith offset -1 and 0,sceKernelPreadat 0 andpwriteat -1, each expectingESPIPE.Run with the test's abort turned off to list the failing checks, all on
maina6fc8a467d:mainwith only the new test file: the 30 checks fail.cmake --build --target libkernel guest_file_vector_testsexits 0 andguest_file_vectorpasses.The branch was then rebased onto
maind6e7ab42df; its 6 newer commits touch none of the changed files, and the only change since the runs is the shortened debt sentence.From the full build of this branch,
guest_file_vector_memory,guest_aio,guest_udp,guest_tcp,guest_socket_read_write,guest_socket_descriptors,guest_socket_descriptor_flags,guest_kqueue,guest_kernel_fcntl,guest_random,guest_posix_random,guest_filesystem,guest_append_streams,guest_append_writes, the threeguest_kernel_app_archivemodes and fiveguest_named_shmtests pass.guest_named_shm_concurrency(the container runs as root, sounlinkof a read-only object gets noEACCES) andguest_closed_standard_descriptors(child exits with 74) fail the same way onmaina6fc8a467d. The only build error on both is the existingatrac9_gradient_testslink failure.Windows not built or run; the Windows
sceKernelPread/sceKernelPwriteandTransferIovecspaths change, andGrantsAccessuses the Windows access helperfcntl(F_GETFL)uses. A guest socket with a negative offset and a character device have no test here.check_conventions.py --base origin/mainpasses.pr_overlap.pyreports no PR with the same change, only #8726 next to the edited debt line. No title tested.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:55:59Z.
I checked the reported overlap with #8726 manually. The code changes are independent, but the pairwise merge conflicts in
docs/dev/debt/libkernel.md; please agree which PR lands first, declare that order inDepends onif the second needs the first, and preserve both debt entries during the rebase. This head is also 4 commits behind currentmain, so rerun the focused vector-I/O cases and overlap check after refreshing it. The current PR CI marks product Linux/Windows jobs skipped.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:59:39Z.
Review: CHANGES_REQUESTED
The vector-length and descriptor/offset precedence changes are supported by a detailed focused regression report. The branch is 4 commits behind current
main. The pairwise merge with #8726 conflicts indocs/dev/debt/libkernel.md, although the code topics are independent; coordinate the order and preserve both debt entries. Product Linux/Windows jobs are skipped on the current PR.I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T15:14:52Z.
Review: DISMISSED
The new SSIZE_MAX ordering and the closed-descriptor/pipe cases are supported by the reported focused run. I found an uncovered error-precedence gap in the negative-offset handling.
PositionedArgumentError(d, false, offset)does not check the descriptor's read/write access mode or general seekability. With a negative offset, an open descriptor that is neither a guest socket nor a pipe falls through toEINVAL. That meanspreadvon anO_WRONLYdescriptor andpwritevon anO_RDONLYdescriptor returnEINVAL, where FreeBSD 11.4 checksfget_read/fget_writefirst and returnsEBADF. Likewise, a negative offset on a non-seekable descriptor other than a pipe/socket can returnEINVALinstead ofESPIPE. FreeBSD's ordering is visible inkern_preadvandkern_pwritev;fget_read/fget_writerequest the corresponding access flags inkern_descrip.c.Please add focused cases for a read-only and write-only descriptor with negative offsets, and for a representative non-seekable descriptor other than a pipe/socket, then order the checks to match FreeBSD. The existing tests cover a closed descriptor and a pipe, but neither verifies descriptor access mode.
I also checked the refreshed repository state. The PR is still based on
4777caca(54 commits behind currentmainb7d1d47a), andStdio.cpphas since changed on main. Please rebase and rerun the focused test on that base. The earlier request to coordinate the adjacent debt-line edit with open #8726 remains unresolved.Checks run locally on this exact head:
g++ -std=c++20 -fsyntax-onlypassed forStdio.cppandGuestFileVector.cpp; diff-check and conventions passed. A host POSIX probe returnedEBADFforpreadv(O_WRONLY, offset=-1)andpwritev(O_RDONLY, offset=-1); this is not a PS5 or FreeBSD runtime test. No configured build is available here, so I could not runguest_file_vector. The PR's 14-case run on its older base remains author-reported.