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

Merged
simonbronk merged 1 commit from upstream-pr/9732 into main 2026-10-26 10:54:16 +00:00
simonbronk commented 2026-10-22 10:18:27 +00:00 (Migrated from github.com)

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:

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

  • 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 d6e7ab42df; 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 a6fc8a467d. 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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4965774435 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9732). Originally posted by [simonbronk](https://github.com/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: - **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`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/subr_uio.c#L401-L434) 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`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L2749-L2763) and [`kern_pwritev`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L3129-L3142) get the file with [`fget_read`/`fget_write`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c), 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` a6fc8a467d: - `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` d6e7ab42df; 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` a6fc8a467d. 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-22 11:55:59 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6340129908 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9732#issuecomment-6340129908). Originally posted by [MarcZX10](https://github.com/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 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 commented 2026-10-22 11:59:39 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613780204 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9732#pullrequestreview-5613780204). Originally posted by [MarcZX10](https://github.com/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 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 commented 2026-10-22 15:14:52 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5616481301 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9732#pullrequestreview-5616481301). Originally posted by [MarcZX10](https://github.com/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 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`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L2749-L2763) and [`kern_pwritev`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L3129-L3142); `fget_read`/`fget_write` request the corresponding access flags in [`kern_descrip.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L9611-L9624). 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9732
No description provided.