Skip to content

fix(libkernel): check vector I/O lengths and descriptors before offsets - #9732

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
simonbronk:fix/libkernel-vector-io-argument-order
Oct 26, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
simonbronk:fix/libkernel-vector-io-argument-order

Conversation

@simonbronk

@simonbronk simonbronk commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

Argument checks in libkernel's vector and positioned I/O now run in the order FreeBSD 11.4 uses:

  • Total length. readv, writev, preadv, pwritev and their sceKernel forms fail with EINVAL when the iovec lengths add up to more than SSIZE_MAX, before the descriptor is looked up, as copyinuio does. Only the Windows path had this check. On Linux a closed descriptor gave EBADF, and guest sockets, the random device and archived files received the oversized table.
  • Descriptor before offset. kern_preadv and kern_pwritev get the file with fget_read/fget_write, then return ESPIPE for a file without DFLAG_SEEKABLE, and only then EINVAL for a negative offset. preadv and pwritev checked the offset first. They now use PositionedArgumentError, the check pread and pwrite already use, on Linux and Windows, and that check now follows the same order for all eight functions:
    • EBADF for 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 an O_RDONLY descriptor through pwrite/pwritev, or O_WRONLY through pread/preadv, gave EINVAL or EFAULT);
    • ESPIPE for anything that is not a vnode, character device or the random device: pipes, guest sockets and kqueues. A kqueue gave EINVAL for a negative offset and read 0 bytes from its host /dev/null placeholder otherwise, so it now gets ESPIPE for every offset. FreeBSD marks pipes, sockets and kqueues readable and writable, so none of them reaches the access check;
    • then EINVAL for a negative offset, or EFAULT for a null buffer.

File::GrantsAccess reads the host descriptor's access mode through the helper fcntl(F_GETFL) already uses. FreeBSD passes a negative offset on a character device to the driver instead of rejecting it; that still returns EINVAL here and is in the debt entry. Nothing is measured on a PS5; the readv debt 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_vector now has 30 checks for this change: the 14 from before (a closed descriptor and a pipe with offset -1, a table totalling SSIZE_MAX + 1 bytes) and 16 new ones:

  • an O_RDONLY file through sceKernelPwrite and sceKernelPwritev/pwritev with offset -1, and pwrite with a null buffer; an O_WRONLY file the same way through the read forms; each expects EBADF, and the opposite direction with offset -1 still gets EINVAL;
  • a kqueue through sceKernelPreadv, sceKernelPwritev, preadv and pwritev with offset -1 and 0, sceKernelPread at 0 and pwrite at -1, each expecting ESPIPE.

Run with the test's abort turned off to list the failing checks, all on main a6fc8a4:

  • main with only the new test file: the 30 checks fail.
  • The previous head of this PR with the new test file: the 16 new checks fail, the 14 earlier ones pass.
  • This branch: cmake --build --target libkernel guest_file_vector_tests exits 0 and guest_file_vector passes.

The branch was then rebased onto main d6e7ab4; 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 three guest_kernel_app_archive modes and five guest_named_shm tests pass. guest_named_shm_concurrency (the container runs as root, so unlink of a read-only object gets no EACCES) and guest_closed_standard_descriptors (child exits with 74) fail the same way on main a6fc8a4. The only build error on both is the existing atrac9_gradient_tests link failure.

Windows not built or run; the Windows sceKernelPread/sceKernelPwrite and TransferIovecs paths change, and GrantsAccess uses the Windows access helper fcntl(F_GETFL) uses. A guest socket with a negative offset and a character device have no test here. check_conventions.py --base origin/main passes. pr_overlap.py reports no PR with the same change, only #8726 next to the edited debt line. No title tested.

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
  • 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

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8520 core/libs/prx/libkernel/File/src/Stdio.cpp L1361-1362, L1373-1374, L1463-1464, L1474-1475

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.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7447 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8010 docs/dev/debt/libkernel.md
PR 8605 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9284 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/File/src/Stdio.cpp
docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9509 docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
docs/dev/debt/libkernel.md
PR 9575 docs/dev/debt/libkernel.md
PR 9823 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9947 docs/dev/debt/libkernel.md
PR 10179 docs/dev/debt/libkernel.md
PR 10319 docs/dev/debt/libkernel.md
PR 10385 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10621 docs/dev/debt/libkernel.md
PR 10666 docs/dev/debt/libkernel.md
PR 10755 core/libs/prx/libkernel/File/src/Stdio.cpp

@MarcZX10

Copy link
Copy Markdown
Contributor

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 in Depends on if the second needs the first, and preserve both debt entries during the rebase. This head is also 4 commits behind current main, so rerun the focused vector-I/O cases and overlap check after refreshing it. The current PR CI marks product Linux/Windows jobs skipped.

@MarcZX10 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in docs/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.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to EINVAL. That means preadv on an O_WRONLY descriptor and pwritev on an O_RDONLY descriptor return EINVAL, where FreeBSD 11.4 checks fget_read/fget_write first and returns EBADF. Likewise, a negative offset on a non-seekable descriptor other than a pipe/socket can return EINVAL instead of ESPIPE. FreeBSD's ordering is visible in kern_preadv and kern_pwritev; fget_read/fget_write request the corresponding access flags in kern_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 current main b7d1d47a), and Stdio.cpp has 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-only passed for Stdio.cpp and GuestFileVector.cpp; diff-check and conventions passed. A host POSIX probe returned EBADF for preadv(O_WRONLY, offset=-1) and pwritev(O_RDONLY, offset=-1); this is not a PS5 or FreeBSD runtime test. No configured build is available here, so I could not run guest_file_vector. The PR's 14-case run on its older base remains author-reported.

@MarcZX10

MarcZX10 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

I’ve noticed that asking you to hold this PR was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course.

@simonbronk
simonbronk force-pushed the fix/libkernel-vector-io-argument-order branch from eb900c3 to acac3b1 Compare October 22, 2026 19:20
@simonbronk

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks, you were right on both points. Rebased on main d6e7ab4 and fixed in acac3b1:

  • Access mode. PositionedArgumentError now takes the direction and returns EBADF for a file, directory or character device not open in that direction, before ESPIPE and EINVAL, as fget_read/fget_write do in kern_preadv/kern_pwritev. This covers pread/pwrite and their sceKernel forms too, since they share the check, including the null-buffer case (pread on an O_WRONLY descriptor with a null buffer gave EFAULT).
  • Seekability. ESPIPE now goes to anything that is not a vnode, character device or the random device, not only pipes and guest sockets. The case I tested is a kqueue: with a negative offset it gave EINVAL, and with offset 0 it read 0 bytes from its host /dev/null placeholder, so it now gets ESPIPE for every offset. Pipes, sockets and kqueues are FREAD|FWRITE in FreeBSD, so the access check doesn't apply to them, and checking seekability first gives the same result.

The 16 new checks fail on the previous head and pass here; the Tested section has the runs. One gap I left in the debt entry: FreeBSD lets a negative offset through on a VCHR vnode, and here it still returns EINVAL.

AI-assisted: yes

@cookie42073-cmd

Copy link
Copy Markdown
Contributor

Hey folks, I picked up the Windows validation for simonbronk's current fix. The error-ordering failures reproduce here, and the patch clears them on the pinned main revision below.

Windows 11 Pro x64, build 26300; WinLibs GCC 15.2.0 r7; Release, BUILD_TESTING=ON. Baseline: 9d6adeb5500c6b65e6591bc8e0a3c71b38c4b3e9. Candidate: that same main plus the original acac3b1c00a97443536b6697bb2f7ad46a0c57b5 commit, merged locally as dfa032ec7956b21d607940ae50b6d3c8fe4aa2ed. This is a local integration check, not a change to your branch.

Check Main Main + this PR
Full Windows build Pass Pass
Unmodified PR guest_file_vector fixture Fails at line 308, read-only sceKernelPwrite(..., -1) Pass; five additional fresh-process runs also pass
Supplemental diagnostic run of the 30 new regression checks 21 fail, 9 pass 30 pass
Selected CPU regression suite 19/19 pass with main's original fixture 19/19 pass; repeated after restoring the unmodified PR fixture and rebuilding

The 21 failing checks are eight access-mode cases, four closed-descriptor cases, four negative-offset pipe cases and five negative-offset kqueue cases. The six oversized-vector checks and three nonnegative-offset kqueue checks already pass on Windows, so the Linux failure count should not be reused for this platform.

The diagnostic overlay only lets those 30 checks continue after a failure. All existing assertions and the four new setup/correct-direction controls stay fatal. It is separate from the proposed patch, and I removed it before the final full build and 19-test run. The final checkout is clean. Receipts include source hashes, executable hashes and both patched and actual unpatched libkernel/libc hashes; libc stayed unchanged between sides. Conventions and whitespace checks pass.

The 19 tests cover vector I/O and buffer protection, filesystem, AIO (including bad descriptors), fcntl, append streams/writes, UDP/TCP, socket read/write and descriptor handling, kqueue, random devices and all three app-archive modes. Full CTest, Linux, physical PS5 measurements and title execution were not run in this check. The FreeBSD reference and character-device gap remain as documented. The adjacent #8726 debt-entry ordering still needs the authors' agreement; this run does not settle it or validate a combined stack.

Main advanced to 16319642a4c84e861188b75583ce853dfbe0a101 after these runs through #6946. I checked its changed-file list: none of this PR's five files changed. That newer AGC/shader stack was not built or tested in this comparison.

This is simonbronk's fix; I'm contributing the Windows comparison and logs. AI-assisted, with a separate review of the raw results before posting.

AnyPS5-9732-Windows-validation-20261023-v2.zip

ZIP SHA-256: 919e7885758c3fd4a64efbd889a13e2cf28a75867061d0f9b2fc93f00fd0ce93.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10
adriadam10 force-pushed the fix/libkernel-vector-io-argument-order branch from acac3b1 to 7a3f27c Compare October 25, 2026 19:06
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 25, 2026 19:08

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the fix/libkernel-vector-io-argument-order branch from 7a3f27c to 88e86a7 Compare October 26, 2026 09:47
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@adriadam10
adriadam10 merged commit b82c33e into boykopovar:main Oct 26, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the argument order against FreeBSD 11.4 (copyinuio, kern_preadv/kern_pwritev) and the new guest_file_vector cases, which fail on main. The full Build failed because this PR removed IsPipeDescriptor, which main now uses in sceKernelFchmod (Stdio.cpp:1599); I restored the helper and resolved the docs/dev/debt/libkernel.md conflict. Rebuilt and ran the libkernel-scoped ctest (374 tests) green; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants