fix(libkernel): fail readv and writev on a socket with EFAULT for an unusable buffer #17248

Merged
Gruzzberry merged 1 commit from upstream-pr/17248 into main 2026-11-12 23:23:51 +00:00
Gruzzberry commented 2026-11-08 16:10:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-08T16:10:01Z.


What

readv and writev (and sceKernelReadv, sceKernelWritev) on a guest socket gathered the caller's buffers with a raw memcpy from, and scattered the received bytes with a raw memcpy to, the guest iovec bases. The iovec array itself is already checked, but a base such as 0x10 or a read-only page crashed the host. FreeBSD 11.4's kern_writev and kern_readv move the data with uiomove, so an unusable buffer fails with EFAULT (sosend and soreceive return the error from uiomove). GuestSockets::Writev now gathers with GuestCopyIn_nid_no_patch and GuestSockets::Readv scatters with GuestCopyOut_nid_no_patch, and each returns EFAULT (SCE_KERNEL_ERROR_EFAULT from the sce calls) when a copy fails. A writev whose gather fails sends nothing. A readv has already taken the bytes off the socket when the scatter fails, as in the kernel, where soreceive has dequeued the data when uiomove faults, so those bytes are lost. The null-base check, the length checks and the zero-length cases are unchanged. There is no debt entry because the errors follow the source.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on main f5fe807353.

  • guest_socket_read_write: new checks that sceKernelWritev and writev on a connected TCP socket fail with SCE_KERNEL_ERROR_EFAULT and EFAULT for a base of 0x10, that a vector with a usable first buffer and an unusable second one sends nothing (the peer is not readable), and that readv with a base of 0x10 fails with EFAULT after the 8 bytes sent beforehand (the socket is empty afterwards). With Socket/src/Udp.cpp reverted to main the test crashes (SegFault); with this change it passes, three runs out of three.
  • The other socket, UDP, TCP and vector I/O tests that build here (54 tests) pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run, the full suite.

Overlaps

  • #12700 (cancel blocked POSIX writes with partial progress) and #12965 (support deferred cancellation in POSIX accept) replace the block around GuestSocketReadGuest_nid_no_patch, GuestSockets::Read, Write, Readv and Writev in Socket/src/Udp.cpp, where this PR changes two lines each in Readv and Writev. Both already conflict with main, so pr_overlap.py did not compare them. This PR goes first and they rebase onto it (two lines); asked on both.
  • #12682 and #12688 (same author) also edit Udp.cpp and GuestSocketReadWrite.cpp, in other functions (SendError, WaitForTransfer, send and recv, socket state) and not in Readv or Writev; #11896, #15785, #16826, #7088 and #5021 touch Udp.cpp elsewhere.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none; merge order: before #12700, #12965
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5212878834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17248). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-08T16:10:01Z. --- ### What `readv` and `writev` (and `sceKernelReadv`, `sceKernelWritev`) on a guest socket gathered the caller's buffers with a raw `memcpy` from, and scattered the received bytes with a raw `memcpy` to, the guest `iovec` bases. The iovec array itself is already checked, but a base such as `0x10` or a read-only page crashed the host. FreeBSD 11.4's `kern_writev` and `kern_readv` move the data with `uiomove`, so an unusable buffer fails with `EFAULT` (`sosend` and `soreceive` return the error from `uiomove`). `GuestSockets::Writev` now gathers with `GuestCopyIn_nid_no_patch` and `GuestSockets::Readv` scatters with `GuestCopyOut_nid_no_patch`, and each returns `EFAULT` (`SCE_KERNEL_ERROR_EFAULT` from the `sce` calls) when a copy fails. A `writev` whose gather fails sends nothing. A `readv` has already taken the bytes off the socket when the scatter fails, as in the kernel, where `soreceive` has dequeued the data when `uiomove` faults, so those bytes are lost. The null-base check, the length checks and the zero-length cases are unchanged. There is no debt entry because the errors follow the source. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` f5fe807353. - `guest_socket_read_write`: new checks that `sceKernelWritev` and `writev` on a connected TCP socket fail with `SCE_KERNEL_ERROR_EFAULT` and `EFAULT` for a base of `0x10`, that a vector with a usable first buffer and an unusable second one sends nothing (the peer is not readable), and that `readv` with a base of `0x10` fails with `EFAULT` after the 8 bytes sent beforehand (the socket is empty afterwards). With `Socket/src/Udp.cpp` reverted to `main` the test crashes (`SegFault`); with this change it passes, three runs out of three. - The other socket, UDP, TCP and vector I/O tests that build here (54 tests) pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run, the full suite. ### Overlaps - #12700 (`cancel blocked POSIX writes with partial progress`) and #12965 (`support deferred cancellation in POSIX accept`) replace the block around `GuestSocketReadGuest_nid_no_patch`, `GuestSockets::Read`, `Write`, `Readv` and `Writev` in `Socket/src/Udp.cpp`, where this PR changes two lines each in `Readv` and `Writev`. Both already conflict with `main`, so `pr_overlap.py` did not compare them. This PR goes first and they rebase onto it (two lines); asked on both. - #12682 and #12688 (same author) also edit `Udp.cpp` and `GuestSocketReadWrite.cpp`, in other functions (`SendError`, `WaitForTransfer`, `send` and `recv`, socket state) and not in `Readv` or `Writev`; #11896, #15785, #16826, #7088 and #5021 touch `Udp.cpp` elsewhere. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none; merge order: before #12700, #12965 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-08 23:41:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T23:41:17Z.

Review: COMMENTED


Windows run of head fa596cd2ab, merged onto main 286b14d2f7 in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_socket_read_write with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's Udp.cpp and this PR's test: 5 / 5 runs fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5820226390 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17248#pullrequestreview-5820226390). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T23:41:17Z. **Review:** COMMENTED --- Windows run of head `fa596cd2ab`, merged onto `main` `286b14d2f7` in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `guest_socket_read_write` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `Udp.cpp` and this PR's test: 5 / 5 runs fail.
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!17248
No description provided.