fix(libkernel): refuse socketpair records longer than the send buffer with EMSGSIZE #18767

Merged
EEkebin merged 1 commit from upstream-pr/18767 into main 2026-11-12 16:09:42 +00:00
EEkebin commented 2026-11-12 07:15:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EEkebin on 2026-11-12T07:15:39Z.


What

A Unix SOCK_DGRAM or SOCK_SEQPACKET socketpair end now refuses a record longer than its send buffer with EMSGSIZE, as FreeBSD 11.4's sosend_generic does for atomic protocols (resid > so_snd.sb_hiwat). The limit is the end's own SO_SNDBUF: 2048 bytes for datagrams (net.local.dgram.maxdgram) and 8192 for seqpacket by default, or whatever the guest set. Before this, the host sent such records: a loopback UDP socket on Windows and an AF_UNIX socket with a ~200 KiB buffer on Linux.

The check follows sosend_generic's order. A send-shut end still fails with EPIPE, and an end whose peer is gone keeps its current errors. The check comes before the null-buffer EFAULT, because FreeBSD copies the data only after this check. send, sendto, sendmsg, write and writev all reach it, because write/writev go through send and sendmsg goes through sendto. The debt entry no longer lists the missing EMSGSIZE.

Tested

Windows 11 Enterprise 10.0.26200, i7-13700K, WinLibs GCC (MinGW-w64), Release:

  • guest_socket_pair checks the datagram pair. Without the library change it aborts (0xc0000409); with it, it passes.
  • The new check covers:
    • the default SO_SNDBUF;
    • send, sendto, sendmsg, write and a null buffer at one byte over the limit;
    • nothing queued after a refused send;
    • a record exactly at the limit and a larger one after raising SO_SNDBUF;
    • SO_SNDBUF belonging to each end separately;
    • EPIPE winning after shutdown(SHUT_WR).
  • ctest -R 'socket|poll|select|kqueue|dup|pipe|sendfile|fstat|recv|send|shutdown|fcntl|net': 323/323 passed.
  • The seqpacket arm (8192 bytes) is compiled only on Linux and has not been run locally.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5273244937 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18767). Originally posted by [EEkebin](https://github.com/EEkebin) on 2026-11-12T07:15:39Z. --- ### What A Unix `SOCK_DGRAM` or `SOCK_SEQPACKET` socketpair end now refuses a record longer than its send buffer with `EMSGSIZE`, as FreeBSD 11.4's [`sosend_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c) does for atomic protocols (`resid > so_snd.sb_hiwat`). The limit is the end's own `SO_SNDBUF`: 2048 bytes for datagrams (`net.local.dgram.maxdgram`) and 8192 for seqpacket by default, or whatever the guest set. Before this, the host sent such records: a loopback UDP socket on Windows and an `AF_UNIX` socket with a ~200 KiB buffer on Linux. The check follows `sosend_generic`'s order. A send-shut end still fails with `EPIPE`, and an end whose peer is gone keeps its current errors. The check comes before the null-buffer `EFAULT`, because FreeBSD copies the data only after this check. `send`, `sendto`, `sendmsg`, `write` and `writev` all reach it, because `write`/`writev` go through `send` and `sendmsg` goes through `sendto`. The debt entry no longer lists the missing `EMSGSIZE`. ### Tested Windows 11 Enterprise 10.0.26200, i7-13700K, WinLibs GCC (MinGW-w64), Release: - `guest_socket_pair` checks the datagram pair. Without the library change it aborts (0xc0000409); with it, it passes. - The new check covers: - the default `SO_SNDBUF`; - `send`, `sendto`, `sendmsg`, `write` and a null buffer at one byte over the limit; - nothing queued after a refused send; - a record exactly at the limit and a larger one after raising `SO_SNDBUF`; - `SO_SNDBUF` belonging to each end separately; - `EPIPE` winning after `shutdown(SHUT_WR)`. - `ctest -R 'socket|poll|select|kqueue|dup|pipe|sendfile|fstat|recv|send|shutdown|fcntl|net'`: 323/323 passed. - The seqpacket arm (8192 bytes) is compiled only on Linux and has not been run locally. ### 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
PrivacyOnion commented 2026-11-12 08:35:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T08:35:25Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 5a7a749d12:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_socket_pair_tests built cleanly.
  • CTest (guest_socket_pair, guest_recvmsg_memory):
    1/2 Test #4584: guest_socket_pair ................   Passed    0.01 sec
    2/2 Test #4585: guest_recvmsg_memory .............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libkernel/Socket/include/SocketOptions.hpp, core/libs/prx/libkernel/Socket/src/SocketOptions.cpp, core/libs/prx/libkernel/Socket/src/Udp.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4585: guest_recvmsg_memory .............   Passed    0.01 sec
    2/2 Test #4584: guest_socket_pair ................Subprocess aborted***Exception:   0.12 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified RecordOverSendBuffer in core/libs/prx/libkernel/Socket/src/Udp.cpp restricts the EMSGSIZE check strictly to atomic GuestUnix sockets (SOCK_DGRAM and SOCK_SEQPACKET) with active peers.
    • Verified thread safety in Udp.cpp: RecordOverSendBuffer acquires socket.modeMutex before inspecting sendShut and calling GuestSockets::SendBufferSize, synchronizing with setsockopt and shutdown.
    • Verified FreeBSD sosend_generic check ordering in send_nid_postfix and sendto_nid_postfix: flags validation (45) and send-shutdown EPIPE (32) take precedence, while EMSGSIZE (40) precedes the null-pointer EFAULT (14) check.
    • Verified GuestSockets::SendBufferSize in core/libs/prx/libkernel/Socket/src/SocketOptions.cpp correctly lazily initializes default buffer sizes (2048 bytes for datagram, 8192 for seqpacket) via EnsureBuffers and returns updated limits set by setsockopt SO_SNDBUF.
    • Verified test coverage in core/libs/tests/GuestSocketPair.cpp: exercises send, sendto, sendmsg, write, null-buffer inputs, boundary sizing, independent per-end SO_SNDBUF values, and shutdown(SHUT_WR) precedence with complete descriptor cleanup.
    • Verified AGENTS.md compliance: zero code comments introduced, no title-specific special-casing, and debt documentation in docs/dev/debt/libkernel.md correctly updated.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5871440293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18767#pullrequestreview-5871440293). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T08:35:25Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `5a7a749d12`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_socket_pair_tests` built cleanly. - **CTest** (`guest_socket_pair`, `guest_recvmsg_memory`): ``` 1/2 Test #4584: guest_socket_pair ................ Passed 0.01 sec 2/2 Test #4585: guest_recvmsg_memory ............. Passed 0.01 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libkernel/Socket/include/SocketOptions.hpp`, `core/libs/prx/libkernel/Socket/src/SocketOptions.cpp`, `core/libs/prx/libkernel/Socket/src/Udp.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #4585: guest_recvmsg_memory ............. Passed 0.01 sec 2/2 Test #4584: guest_socket_pair ................Subprocess aborted***Exception: 0.12 sec 50% tests passed, 1 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - Verified RecordOverSendBuffer in core/libs/prx/libkernel/Socket/src/Udp.cpp restricts the EMSGSIZE check strictly to atomic GuestUnix sockets (SOCK_DGRAM and SOCK_SEQPACKET) with active peers. - Verified thread safety in Udp.cpp: RecordOverSendBuffer acquires socket.modeMutex before inspecting sendShut and calling GuestSockets::SendBufferSize, synchronizing with setsockopt and shutdown. - Verified FreeBSD sosend_generic check ordering in send_nid_postfix and sendto_nid_postfix: flags validation (45) and send-shutdown EPIPE (32) take precedence, while EMSGSIZE (40) precedes the null-pointer EFAULT (14) check. - Verified GuestSockets::SendBufferSize in core/libs/prx/libkernel/Socket/src/SocketOptions.cpp correctly lazily initializes default buffer sizes (2048 bytes for datagram, 8192 for seqpacket) via EnsureBuffers and returns updated limits set by setsockopt SO_SNDBUF. - Verified test coverage in core/libs/tests/GuestSocketPair.cpp: exercises send, sendto, sendmsg, write, null-buffer inputs, boundary sizing, independent per-end SO_SNDBUF values, and shutdown(SHUT_WR) precedence with complete descriptor cleanup. - Verified AGENTS.md compliance: zero code comments introduced, no title-specific special-casing, and debt documentation in docs/dev/debt/libkernel.md correctly updated. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Looks Good To Me
PrivacyOnion commented 2026-11-12 10:14:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T10:14:18Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 5a7a749d12:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_socket_pair_tests built cleanly.
  • CTest (guest_socket_pair, guest_recvmsg_memory):
    1/2 Test #4584: guest_socket_pair ................   Passed    0.02 sec
    2/2 Test #4585: guest_recvmsg_memory .............   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libkernel/Socket/include/SocketOptions.hpp, core/libs/prx/libkernel/Socket/src/SocketOptions.cpp, core/libs/prx/libkernel/Socket/src/Udp.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4585: guest_recvmsg_memory .............   Passed    0.02 sec
    2/2 Test #4584: guest_socket_pair ................Subprocess aborted***Exception:   0.34 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libkernel/Socket/src/Udp.cpp (RecordOverSendBuffer): Verified socket filtering applies strictly to GuestUnix with atomic protocols (type 2 SOCK_DGRAM and type 5 SOCK_SEQPACKET), correctly excluding SOCK_STREAM (type 1) and non-Unix sockets.
    • core/libs/prx/libkernel/Socket/src/Udp.cpp (send_nid_postfix, sendto_nid_postfix): Verified check precedence matches FreeBSD 11.4 sosend_generic, properly executing RecordOverSendBuffer before null buffer EFAULT (14) while allowing sendShut (EPIPE 32) and peer.expired() error handling to take priority.
    • core/libs/prx/libkernel/Socket/src/SocketOptions.cpp (SendBufferSize, EnsureBuffers) & SocketOptions.hpp: Verified SendBufferSize properly initializes and queries state.sendBuffer (defaulting to 2048 bytes for GuestUnix datagram and 8192 for seqpacket) and reflects dynamic changes applied through setsockopt(SOL_SOCKET, SO_SNDBUF).
    • core/libs/prx/libkernel/Socket/src/Udp.cpp (modeMutex concurrency): Verified thread safety across RecordOverSendBuffer, ensuring socket.modeMutex guards reads of sendShut and access to socket.options without introducing recursive mutex deadlocks.
    • core/libs/tests/GuestSocketPair.cpp (CheckRecordOverSendBuffer): Verified unit tests thoroughly test send, sendto, sendmsg, write, boundary conditions at limit and limit + 1, buffer reallocation, socketpair end independence, and proper platform isolation (#if !defined(_WIN32) && !defined(APPLE)) for SOCK_SEQPACKET.
    • AGENTS.md & docs/dev/debt/libkernel.md compliance: Verified zero code comments were added, libkernel technical debt documentation was accurately updated, and no memory or descriptor leaks exist.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5872655438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18767#pullrequestreview-5872655438). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T10:14:18Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `5a7a749d12`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_socket_pair_tests` built cleanly. - **CTest** (`guest_socket_pair`, `guest_recvmsg_memory`): ``` 1/2 Test #4584: guest_socket_pair ................ Passed 0.02 sec 2/2 Test #4585: guest_recvmsg_memory ............. Passed 0.02 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libkernel/Socket/include/SocketOptions.hpp`, `core/libs/prx/libkernel/Socket/src/SocketOptions.cpp`, `core/libs/prx/libkernel/Socket/src/Udp.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #4585: guest_recvmsg_memory ............. Passed 0.02 sec 2/2 Test #4584: guest_socket_pair ................Subprocess aborted***Exception: 0.34 sec 50% tests passed, 1 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libkernel/Socket/src/Udp.cpp (RecordOverSendBuffer): Verified socket filtering applies strictly to GuestUnix with atomic protocols (type 2 SOCK_DGRAM and type 5 SOCK_SEQPACKET), correctly excluding SOCK_STREAM (type 1) and non-Unix sockets. - core/libs/prx/libkernel/Socket/src/Udp.cpp (send_nid_postfix, sendto_nid_postfix): Verified check precedence matches FreeBSD 11.4 sosend_generic, properly executing RecordOverSendBuffer before null buffer EFAULT (14) while allowing sendShut (EPIPE 32) and peer.expired() error handling to take priority. - core/libs/prx/libkernel/Socket/src/SocketOptions.cpp (SendBufferSize, EnsureBuffers) & SocketOptions.hpp: Verified SendBufferSize properly initializes and queries state.sendBuffer (defaulting to 2048 bytes for GuestUnix datagram and 8192 for seqpacket) and reflects dynamic changes applied through setsockopt(SOL_SOCKET, SO_SNDBUF). - core/libs/prx/libkernel/Socket/src/Udp.cpp (modeMutex concurrency): Verified thread safety across RecordOverSendBuffer, ensuring socket.modeMutex guards reads of sendShut and access to socket.options without introducing recursive mutex deadlocks. - core/libs/tests/GuestSocketPair.cpp (CheckRecordOverSendBuffer): Verified unit tests thoroughly test send, sendto, sendmsg, write, boundary conditions at limit and limit + 1, buffer reallocation, socketpair end independence, and proper platform isolation (#if !defined(_WIN32) && !defined(__APPLE__)) for SOCK_SEQPACKET. - AGENTS.md & docs/dev/debt/libkernel.md compliance: Verified zero code comments were added, libkernel technical debt documentation was accurately updated, and no memory or descriptor leaks exist. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18767
No description provided.