fix(libkernel): check the send and recv buffers as sendto and recvfrom do #24382

Merged
Dr-Coconut merged 1 commit from upstream-pr/24382 into main 2026-11-21 13:17:39 +00:00
Dr-Coconut commented 2026-11-20 15:30:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dr-Coconut on 2026-11-20T15:30:42Z.


What

send and recv handed the guest buffer to the host call without the checks sendto and recvfrom make, so an unreadable or unwritable buffer got whatever the host did, a gap the libkernel debt file records. FreeBSD 11.4's libc implements them as sendto and recvfrom without an address, so this gives them the same checks, reusing the helpers already in Udp.cpp:

  • On Windows, send rejects a buffer that is not wholly readable with EFAULT through SourceFaults, as sendto's connected path does. That also prepares guest memory for the host read, which send skipped.
  • On Windows, recv receives only into the writable prefix (GuestPrefix), and fails with EFAULT when the pending data does not fit it (PendingFits), leaving stream data queued and dropping a datagram (DropDatagram), as recvfrom does.
  • Linux already matched sendto and recvfrom. macOS is unchanged: recvfrom drops a faulted datagram there and recv still does not; I could not build or test macOS, so the debt fragment records that difference.

The new CheckSendReceiveFaults in GuestUdp.cpp mirrors CheckPointerFaults for send and recv on connected stream and datagram sockets: null, unmapped, unreadable, read-only and page-crossing buffers, a receive whose last writable byte takes a one-byte message, and a datagram that does not fit (dropped, the next one received). The debt fragment rewrites the entry to cover all four calls without the gap.

Tested

Commit 2c7575b2ea on main 7fa48cd1c6, Release, in a workflow on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/44762193787

  • Windows (GitHub-hosted windows-latest, WinLibs GCC 15.2.0 15.2.0posix-14.0.0-ucrt-r7), with the change: guest_udp, guest_tcp, guest_socket_pair and guest_kernel_socket_dontwait pass.
  • Windows with Udp.cpp restored to main and the new test kept: guest_udp aborts (0xc0000409); the other three pass. main's own guest_udp passes, so the abort is in the new CheckSendReceiveFaults cases.
  • Linux (GitHub-hosted ubuntu-latest, image ubuntu-26.04, GCC 15.2.0), with the change: all four pass. The change does not alter Linux, so this shows the new cases hold there.

Not run on macOS. No title tested. tools/check_conventions.py and tools/debt_fold.py --check pass. tools/pr_overlap.py --branch merged 25 open pull requests that share files with this branch and reported one conflict: #23341 changes the same two lines of recv (ReceiveShut to ReceiveEnded, and WSAEMSGSIZE for type 5), as it does in recvfrom, so whichever lands second keeps both. It could not compare six that conflict with main; compared by hand, #8605 changes GuestSockets::Stat, and #11896, #12682, #12688, #12700 and #12965 restructure recv without adding these checks, so they overlap textually only.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5415896237 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24382). Originally posted by [Dr-Coconut](https://github.com/Dr-Coconut) on 2026-11-20T15:30:42Z. --- ### What `send` and `recv` handed the guest buffer to the host call without the checks `sendto` and `recvfrom` make, so an unreadable or unwritable buffer got whatever the host did, a gap the libkernel debt file records. FreeBSD 11.4's libc implements them as [`sendto`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/net/send.c) and [`recvfrom`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/net/recv.c) without an address, so this gives them the same checks, reusing the helpers already in `Udp.cpp`: - On Windows, `send` rejects a buffer that is not wholly readable with `EFAULT` through `SourceFaults`, as `sendto`'s connected path does. That also prepares guest memory for the host read, which `send` skipped. - On Windows, `recv` receives only into the writable prefix (`GuestPrefix`), and fails with `EFAULT` when the pending data does not fit it (`PendingFits`), leaving stream data queued and dropping a datagram (`DropDatagram`), as `recvfrom` does. - Linux already matched `sendto` and `recvfrom`. macOS is unchanged: `recvfrom` drops a faulted datagram there and `recv` still does not; I could not build or test macOS, so the debt fragment records that difference. The new `CheckSendReceiveFaults` in `GuestUdp.cpp` mirrors `CheckPointerFaults` for `send` and `recv` on connected stream and datagram sockets: null, unmapped, unreadable, read-only and page-crossing buffers, a receive whose last writable byte takes a one-byte message, and a datagram that does not fit (dropped, the next one received). The debt fragment rewrites the entry to cover all four calls without the gap. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Commit 2c7575b2ea on `main` 7fa48cd1c6, Release, in a workflow on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/44762193787 - Windows (GitHub-hosted `windows-latest`, WinLibs GCC 15.2.0 `15.2.0posix-14.0.0-ucrt-r7`), with the change: `guest_udp`, `guest_tcp`, `guest_socket_pair` and `guest_kernel_socket_dontwait` pass. - Windows with `Udp.cpp` restored to `main` and the new test kept: `guest_udp` aborts (`0xc0000409`); the other three pass. `main`'s own `guest_udp` passes, so the abort is in the new `CheckSendReceiveFaults` cases. - Linux (GitHub-hosted `ubuntu-latest`, image ubuntu-26.04, GCC 15.2.0), with the change: all four pass. The change does not alter Linux, so this shows the new cases hold there. Not run on macOS. No title tested. `tools/check_conventions.py` and `tools/debt_fold.py --check` pass. `tools/pr_overlap.py --branch` merged 25 open pull requests that share files with this branch and reported one conflict: #23341 changes the same two lines of `recv` (`ReceiveShut` to `ReceiveEnded`, and `WSAEMSGSIZE` for type 5), as it does in `recvfrom`, so whichever lands second keeps both. It could not compare six that conflict with `main`; compared by hand, #8605 changes `GuestSockets::Stat`, and #11896, #12682, #12688, #12700 and #12965 restructure `recv` without adding these checks, so they overlap textually only. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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)
PrivacyOnion commented 2026-11-20 15:38:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T15:38:17Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 2c7575b2ea:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_udp_tests built cleanly.
  • CTest (guest_udp):
    1/1 Test #5849: guest_udp ........................   Passed    0.78 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Socket/src/Udp.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5849: guest_udp ........................   Passed    0.79 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified SourceFaults(buffer, length) check in send_nid_postfix correctly returns EFAULT (14) on Windows before any socket send operation.
    • Verified GuestPrefix and PendingFits logic in recv_nid_postfix correctly limits buffer length, detects insufficient writable capacity for pending datagrams, drops datagrams via DropDatagram, and returns EFAULT (14) on Windows.
    • Verified WSAEMSGSIZE handling for SOCK_DGRAM (type == 2) returns the truncated limit on Windows matching recvfrom behavior.
    • Verified test coverage in CheckSendReceiveFaults in GuestUdp.cpp thoroughly tests null, unreadable, read-only, crossing, and partial page boundary buffers on both TCP (stream) and UDP (datagram) sockets.
    • Verified technical debt fragment docs/dev/debt/changes/fix-libkernel-send-recv-buffer-checks.md follows required format and conventions with no prohibited code comments.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5995263549 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24382#pullrequestreview-5995263549). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T15:38:17Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2c7575b2ea`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_udp_tests` built cleanly. - **CTest** (`guest_udp`): ``` 1/1 Test #5849: guest_udp ........................ Passed 0.78 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Socket/src/Udp.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5849: guest_udp ........................ Passed 0.79 sec 100% tests passed, 0 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified SourceFaults(buffer, length) check in send_nid_postfix correctly returns EFAULT (14) on Windows before any socket send operation. - Verified GuestPrefix and PendingFits logic in recv_nid_postfix correctly limits buffer length, detects insufficient writable capacity for pending datagrams, drops datagrams via DropDatagram, and returns EFAULT (14) on Windows. - Verified WSAEMSGSIZE handling for SOCK_DGRAM (type == 2) returns the truncated limit on Windows matching recvfrom behavior. - Verified test coverage in CheckSendReceiveFaults in GuestUdp.cpp thoroughly tests null, unreadable, read-only, crossing, and partial page boundary buffers on both TCP (stream) and UDP (datagram) sockets. - Verified technical debt fragment docs/dev/debt/changes/fix-libkernel-send-recv-buffer-checks.md follows required format and conventions with no prohibited code comments. Looks Good To Me
MarcZX10 commented 2026-11-20 15:45:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T15:45:13Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 2c7575b2ea4aa42758afab92764912096e789aef using the prepared diff and repo_read.py. I did not run commands or tests. The description reports Windows and Linux test runs; these were not independently run. I read core/libs/prx/libkernel/Socket/src/Udp.cpp, core/libs/tests/GuestUdp.cpp, and docs/dev/debt/changes/fix-libkernel-send-recv-buffer-checks.md.

Results

Check Result Note
Diff and targeted source review passed Changes and relevant helpers read
Tests in this review not run Author-reported Windows and Linux runs only
macOS behavior not run Description says it was not tested

Findings

Blocking

None.

Non-blocking

  • The Windows receive checks inspect pending stream data before the actual receive. As the debt entry notes, data arriving after that check or use of MSG_WAITALL can still produce a short success where FreeBSD would return EFAULT (Udp.cpp:1981-1987). The debt entry accurately records this limitation.

Not verified

  • I did not run the reported test commands, build the change, or independently check the claimed negative control.
  • macOS behavior and physical-console behavior were not tested in this review.
  • The prepared bundle reports all three changed files as included and reviewed; no excluded changed files are listed.

To get this to a good state

Nothing is required. The optional stream receive edge case is documented in the debt entry.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5995358985 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24382#pullrequestreview-5995358985). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T15:45:13Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `2c7575b2ea4aa42758afab92764912096e789aef` using the prepared diff and `repo_read.py`. I did not run commands or tests. The description reports Windows and Linux test runs; these were not independently run. I read `core/libs/prx/libkernel/Socket/src/Udp.cpp`, `core/libs/tests/GuestUdp.cpp`, and `docs/dev/debt/changes/fix-libkernel-send-recv-buffer-checks.md`. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Changes and relevant helpers read | | Tests in this review | not run | Author-reported Windows and Linux runs only | | macOS behavior | not run | Description says it was not tested | ## Findings ### Blocking None. ### Non-blocking - The Windows receive checks inspect pending stream data before the actual receive. As the debt entry notes, data arriving after that check or use of `MSG_WAITALL` can still produce a short success where FreeBSD would return `EFAULT` (`Udp.cpp:1981-1987`). The debt entry accurately records this limitation. ## Not verified - I did not run the reported test commands, build the change, or independently check the claimed negative control. - macOS behavior and physical-console behavior were not tested in this review. - The prepared bundle reports all three changed files as included and reviewed; no excluded changed files are listed. ## To get this to a good state Nothing is required. The optional stream receive edge case is documented in the debt entry.
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!24382
No description provided.