fix(libkernel): fail a send on a never-connected stream socket with ENOTCONN #15135

Merged
MarrasFederico merged 2 commits from upstream-pr/15135 into main 2026-11-07 00:58:28 +00:00
MarrasFederico commented 2026-11-02 17:51:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-02T17:51:13Z.


What

send, sendto without an address, sendmsg and write on a TCP socket that was never connected (a new socket, or a listening one) went to the host. On Linux the host answered EPIPE and raised its own SIGPIPE, which killed the guest before SO_NOSIGPIPE or MSG_NOSIGNAL could matter. FreeBSD 11.4's sosend_generic fails such a send with ENOTCONN: TCP has PR_CONNREQUIRED|PR_IMPLOPCL (in_proto.c), so with no address and no connection it returns ENOTCONN. That check runs after SBS_CANTSENDMORE and so_error, neither of which a never-connected socket can have, and before the data is copied in, so it also comes before EFAULT.

A guest socket now records whether connect was called on it. accept, socketpair and a socket received through SCM_RIGHTS count as connected, and the host decides those as before. A stream socket that does not qualify fails these sends with ENOTCONN and no signal. Unchanged:

  • a sendto with an address;
  • a send while a non-blocking connect is in progress;
  • datagram sockets, which keep the EDESTADDRREQ mapping.

The debt entry records the first two cases. Neither path was measured on a console.

Tested

Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, BUILD_TESTING=ON, on main b28bb77217. Windows and macOS were not compiled here. No title was run.

  • New guest_socket_send_before_connect test: it covers send (including zero-length and MSG_NOSIGNAL), sendto without an address and write on a new socket and on a listening one, and then a normal exchange after connect and accept.
    • With main's Udp.cpp the test process is killed by SIGPIPE (exit 141).
    • With this change it passes.
  • Without the SCM_RIGHTS case, the existing guest_socket_rights test fails, because a received connected socket would be refused. With it, the test passes.
  • ctest -R '^guest_|^net_|public_headers' -j12 (1482 tests) under unshare -rn with lo up: everything passes except guest_socket_options_multicast, which fails on main too on this host.
  • python3 tools/check_conventions.py --base origin/main passes.
  • tools/pr_overlap.py --branch reports no confirmed overlap. Its not-compared PRs were inspected by hand:
    • #11219, #11896, #13560 and #14310 touch Udp.cpp in other functions: ioctl, MSG_WAITALL receive, macOS pipe readiness, and forwarding sceNetSocket descriptors, which are not in this socket table.
    • #13486 is a relinker change.
    • The rest touch only other lines of CMakeLists.txt or docs/dev/debt/libkernel.md.
    • #11002 (catching SIGPIPE on Linux) is complementary: this change stops the signal for sockets that were never connected.

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:5124863880 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15135). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-02T17:51:13Z. --- ### What `send`, `sendto` without an address, `sendmsg` and `write` on a TCP socket that was never connected (a new socket, or a listening one) went to the host. On Linux the host answered `EPIPE` and raised its own `SIGPIPE`, which killed the guest before `SO_NOSIGPIPE` or `MSG_NOSIGNAL` could matter. FreeBSD 11.4's [`sosend_generic`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L1260-L1292) fails such a send with `ENOTCONN`: TCP has `PR_CONNREQUIRED|PR_IMPLOPCL` ([`in_proto.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/in_proto.c#L137)), so with no address and no connection it returns `ENOTCONN`. That check runs after `SBS_CANTSENDMORE` and `so_error`, neither of which a never-connected socket can have, and before the data is copied in, so it also comes before `EFAULT`. A guest socket now records whether `connect` was called on it. `accept`, `socketpair` and a socket received through `SCM_RIGHTS` count as connected, and the host decides those as before. A stream socket that does not qualify fails these sends with `ENOTCONN` and no signal. Unchanged: - a `sendto` with an address; - a send while a non-blocking `connect` is in progress; - datagram sockets, which keep the `EDESTADDRREQ` mapping. The debt entry records the first two cases. Neither path was measured on a console. ### 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. --> Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` b28bb77217. Windows and macOS were not compiled here. No title was run. - New `guest_socket_send_before_connect` test: it covers `send` (including zero-length and `MSG_NOSIGNAL`), `sendto` without an address and `write` on a new socket and on a listening one, and then a normal exchange after `connect` and `accept`. - With `main`'s `Udp.cpp` the test process is killed by `SIGPIPE` (exit 141). - With this change it passes. - Without the `SCM_RIGHTS` case, the existing `guest_socket_rights` test fails, because a received connected socket would be refused. With it, the test passes. - `ctest -R '^guest_|^net_|public_headers' -j12` (1482 tests) under `unshare -rn` with `lo` up: everything passes except `guest_socket_options_multicast`, which fails on `main` too on this host. - `python3 tools/check_conventions.py --base origin/main` passes. - `tools/pr_overlap.py --branch` reports no confirmed overlap. Its not-compared PRs were inspected by hand: - #11219, #11896, #13560 and #14310 touch `Udp.cpp` in other functions: ioctl, `MSG_WAITALL` receive, macOS pipe readiness, and forwarding `sceNetSocket` descriptors, which are not in this socket table. - #13486 is a relinker change. - The rest touch only other lines of `CMakeLists.txt` or `docs/dev/debt/libkernel.md`. - #11002 (catching `SIGPIPE` on Linux) is complementary: this change stops the signal for sockets that were never connected. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
BunnyFl commented 2026-11-02 19:49:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T19:49:56Z.

Review: APPROVED


Reads right against FreeBSD 11.4's sosend_generic: TCP has PR_IMPLOPCL, so with no address and no connection it takes the addr == NULL branch and returns ENOTCONN for any length, after SBS_CANTSENDMORE/so_error, which a never-connected socket can't have. sendmsg without a name reaches the check through sendto_nid_postfix (Udp.cpp:1952) and write through send_nid_postfix (:2508), so all four paths in the description get it. connectStarted is set under modeMutex before the host connect, and SendBeforeConnect reads it under the same lock.

Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto main: guest_socket_send_before_connect passes. With main's Udp.cpp it also passes here: main already returns ENOTCONN for these sends on Windows (presumably from Winsock's own WSAENOTCONN; I didn't trace the mapping), so the bug and the negative control are Linux-side (where the host's EPIPE/SIGPIPE came from). Worth saying in Tested that Windows was unaffected.

Non-blocking: for sendmsg, the iov data is still copied into the host buffer (and a huge total can fail with ENOBUFS) before the check, so "before EFAULT" holds for send/sendto/write but not for sendmsg. That ordering predates this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5742427481 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15135#pullrequestreview-5742427481). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T19:49:56Z. **Review:** APPROVED --- Reads right against FreeBSD 11.4's `sosend_generic`: TCP has `PR_IMPLOPCL`, so with no address and no connection it takes the `addr == NULL` branch and returns `ENOTCONN` for any length, after `SBS_CANTSENDMORE`/`so_error`, which a never-connected socket can't have. `sendmsg` without a name reaches the check through `sendto_nid_postfix` (`Udp.cpp:1952`) and `write` through `send_nid_postfix` (`:2508`), so all four paths in the description get it. `connectStarted` is set under `modeMutex` before the host `connect`, and `SendBeforeConnect` reads it under the same lock. Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto `main`: `guest_socket_send_before_connect` passes. With `main`'s `Udp.cpp` it also passes here: main already returns `ENOTCONN` for these sends on Windows (presumably from Winsock's own `WSAENOTCONN`; I didn't trace the mapping), so the bug and the negative control are Linux-side (where the host's `EPIPE`/`SIGPIPE` came from). Worth saying in Tested that Windows was unaffected. Non-blocking: for `sendmsg`, the iov data is still copied into the host buffer (and a huge total can fail with `ENOBUFS`) before the check, so "before `EFAULT`" holds for `send`/`sendto`/`write` but not for `sendmsg`. That ordering predates this PR.
mashingaan commented 2026-11-02 20:29:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T20:29:26Z.

Review: DISMISSED


PassedSocket(int native) unconditionally sets connectStarted = true. SCM_RIGHTS can also carry a fresh, unconnected stream socket, so after receiving that descriptor SendBeforeConnect will skip the new ENOTCONN guard. A subsequent send is delegated to the host again and can reproduce the Linux EPIPE plus SIGPIPE failure this PR fixes. The new GuestSocketSendBeforeConnect test does not cover descriptor passing, and the existing connected-rights case cannot catch this. Please preserve the connection-attempt state across descriptor passing or determine it reliably, then add a regression that passes a fresh stream socket through SCM_RIGHTS and verifies the send returns ENOTCONN without killing the process.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5742889186 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15135#pullrequestreview-5742889186). Originally posted by [mashingaan](https://github.com/mashingaan) on 2026-11-02T20:29:26Z. **Review:** DISMISSED --- PassedSocket(int native) unconditionally sets `connectStarted = true`. `SCM_RIGHTS` can also carry a fresh, unconnected stream socket, so after receiving that descriptor `SendBeforeConnect` will skip the new `ENOTCONN` guard. A subsequent send is delegated to the host again and can reproduce the Linux `EPIPE` plus `SIGPIPE` failure this PR fixes. The new `GuestSocketSendBeforeConnect` test does not cover descriptor passing, and the existing connected-rights case cannot catch this. Please preserve the connection-attempt state across descriptor passing or determine it reliably, then add a regression that passes a fresh stream socket through `SCM_RIGHTS` and verifies the send returns `ENOTCONN` without killing the process.
XairRrR commented 2026-11-03 19:11:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T19:11:44Z.

Review: APPROVED


Reviewed — approved

The ENOTCONN guard for stream sockets that were never connected works as described. The focused socket tests pass, and with main's Udp.cpp restored, both the new test and the extended test are killed by SIGPIPE.

What I ran

Head b83067a484129701bb780ba2b0b430b1a0f56d1c, main 1e658521a0a599872612bb91949311e2956b5283, merged with --no-ff in a clean worktree. Debian 13 VM, GCC 14.2.0, Release, Ninja, BUILD_TESTING=ON.

git diff --check 1e658521a0a599872612bb91949311e2956b5283...b83067a484129701bb780ba2b0b430b1a0f56d1c
python3 tools/check_conventions.py --base 1e658521a0a599872612bb91949311e2956b5283
ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights|guest_socket_pair|guest_tcp|guest_poll)$"
# negative control
git show 1e658521a0a599872612bb91949311e2956b5283:core/libs/prx/libkernel/Socket/src/Udp.cpp > core/libs/prx/libkernel/Socket/src/Udp.cpp
ninja -C build -j32 -l32 guest_socket_send_before_connect_tests guest_socket_rights_tests
ctest --test-dir build --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights)$"
git show 27599e5f19fcc244d91cfa582d3f763ec3683a54:core/libs/prx/libkernel/Socket/src/Udp.cpp > core/libs/prx/libkernel/Socket/src/Udp.cpp
ninja -C build -j32 -l32 guest_socket_send_before_connect_tests guest_socket_rights_tests
ctest --test-dir build --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights)$"
git status --porcelain

Results

Check Result
git diff --check clean
check_conventions.py passed
Build (script, merged tree) ok
Selected tests (script, 3738 run) no new failures, no timeouts; the 9 known failures also fail on main (guest_socket_options_multicast among them)
guest_socket_send_before_connect, guest_socket_rights, guest_socket_pair, guest_tcp, guest_poll 5/5 passed (slowest: guest_poll, 4.5 s)
check_skips.py guest_net_ctl skipped, which also happens on main
Validation-layer comparison not applicable: the PR changes no AGC, shader or VideoOut files

Negative control

I restored only core/libs/prx/libkernel/Socket/src/Udp.cpp from main and rebuilt. Both tests failed:

ctest output
1/2 Test #3460: guest_socket_send_before_connect ...SIGPIPE***Exception:   0.01 sec
2/2 Test #3464: guest_socket_rights ................SIGPIPE***Exception:   0.01 sec
0% tests passed, 2 tests failed out of 2

After I restored the merged file and rebuilt, both tests passed again, and git status --porcelain was empty. This confirms the author's exit-141 claim for both tests. It also confirms that the new CheckPassedFreshStream covers the SCM_RIGHTS case that mashingaan raised. The fix in b83067a4 (judging a received stream socket by getpeername) addresses that request, and the new test catches a regression there.

Findings

Blocking

None.

Non-blocking

  • A stream socket whose blocking connect failed still goes to the host. connectStarted is set before the host connect (core/libs/prx/libkernel/Socket/src/Udp.cpp:1496-1498, head) and is never cleared, even when the call fails with ECONNREFUSED or ETIMEDOUT.
    • After such a failure the socket is again unconnected. FreeBSD's sosend_generic would return ENOTCONN, while Linux tcp_sendmsg returns EPIPE and raises SIGPIPE unless MSG_NOSIGNAL or SO_NOSIGPIPE is set. That is the same crash this PR fixes, reached through a common pattern: a connect that fails, followed by a send in an error path.
    • The debt entry does not list this case; it lists only sendto with an address and an in-progress non-blocking connect.
    • Suggested fix: clear connectStarted under modeMutex when the host connect fails with anything other than in-progress, already-connecting or interrupted. Otherwise, add the case to the debt entry. I reasoned this from the code and Linux's TCP send path and did not run it.
  • I confirm BunnyFl's ordering note: for sendmsg, the iov is still copied into a host buffer before the check. This predates the PR and is not a regression.

Not verified

  • Windows and macOS builds. BunnyFl reports that Windows already returned ENOTCONN on main.
  • Console behaviour: the debt entry says these paths were not measured on a PS5.
  • The failed-connect case above was not run.
  • I did not separately test the sendmsg path; it reaches the check through sendto_nid_postfix as BunnyFl describes, but no test calls sendmsg on an unconnected stream socket.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5758057066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15135#pullrequestreview-5758057066). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T19:11:44Z. **Review:** APPROVED --- # Reviewed — approved The ENOTCONN guard for stream sockets that were never connected works as described. The focused socket tests pass, and with `main`'s `Udp.cpp` restored, both the new test and the extended test are killed by `SIGPIPE`. ## What I ran Head `b83067a484129701bb780ba2b0b430b1a0f56d1c`, `main` `1e658521a0a599872612bb91949311e2956b5283`, merged with `--no-ff` in a clean worktree. Debian 13 VM, GCC 14.2.0, Release, Ninja, `BUILD_TESTING=ON`. ``` git diff --check 1e658521a0a599872612bb91949311e2956b5283...b83067a484129701bb780ba2b0b430b1a0f56d1c python3 tools/check_conventions.py --base 1e658521a0a599872612bb91949311e2956b5283 ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights|guest_socket_pair|guest_tcp|guest_poll)$" # negative control git show 1e658521a0a599872612bb91949311e2956b5283:core/libs/prx/libkernel/Socket/src/Udp.cpp > core/libs/prx/libkernel/Socket/src/Udp.cpp ninja -C build -j32 -l32 guest_socket_send_before_connect_tests guest_socket_rights_tests ctest --test-dir build --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights)$" git show 27599e5f19fcc244d91cfa582d3f763ec3683a54:core/libs/prx/libkernel/Socket/src/Udp.cpp > core/libs/prx/libkernel/Socket/src/Udp.cpp ninja -C build -j32 -l32 guest_socket_send_before_connect_tests guest_socket_rights_tests ctest --test-dir build --output-on-failure -R "^(guest_socket_send_before_connect|guest_socket_rights)$" git status --porcelain ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | passed | | Build (script, merged tree) | ok | | Selected tests (script, 3738 run) | no new failures, no timeouts; the 9 known failures also fail on `main` (`guest_socket_options_multicast` among them) | | `guest_socket_send_before_connect`, `guest_socket_rights`, `guest_socket_pair`, `guest_tcp`, `guest_poll` | 5/5 passed (slowest: `guest_poll`, 4.5 s) | | `check_skips.py` | `guest_net_ctl` skipped, which also happens on `main` | | Validation-layer comparison | not applicable: the PR changes no AGC, shader or VideoOut files | ## Negative control I restored only `core/libs/prx/libkernel/Socket/src/Udp.cpp` from `main` and rebuilt. Both tests failed: <details><summary>ctest output</summary> ``` 1/2 Test #3460: guest_socket_send_before_connect ...SIGPIPE***Exception: 0.01 sec 2/2 Test #3464: guest_socket_rights ................SIGPIPE***Exception: 0.01 sec 0% tests passed, 2 tests failed out of 2 ``` </details> After I restored the merged file and rebuilt, both tests passed again, and `git status --porcelain` was empty. This confirms the author's exit-141 claim for both tests. It also confirms that the new `CheckPassedFreshStream` covers the `SCM_RIGHTS` case that mashingaan raised. The fix in b83067a4 (judging a received stream socket by `getpeername`) addresses that request, and the new test catches a regression there. ## Findings ### Blocking None. ### Non-blocking - **A stream socket whose blocking `connect` failed still goes to the host.** `connectStarted` is set before the host `connect` (`core/libs/prx/libkernel/Socket/src/Udp.cpp:1496-1498`, [head](https://github.com/boykopovar/AnyPS5/blob/b83067a484129701bb780ba2b0b430b1a0f56d1c/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1473-L1475)) and is never cleared, even when the call fails with `ECONNREFUSED` or `ETIMEDOUT`. - After such a failure the socket is again unconnected. FreeBSD's `sosend_generic` would return `ENOTCONN`, while Linux `tcp_sendmsg` returns `EPIPE` and raises `SIGPIPE` unless `MSG_NOSIGNAL` or `SO_NOSIGPIPE` is set. That is the same crash this PR fixes, reached through a common pattern: a connect that fails, followed by a send in an error path. - The debt entry does not list this case; it lists only `sendto` with an address and an in-progress non-blocking `connect`. - Suggested fix: clear `connectStarted` under `modeMutex` when the host `connect` fails with anything other than in-progress, already-connecting or interrupted. Otherwise, add the case to the debt entry. I reasoned this from the code and Linux's TCP send path and did not run it. - I confirm BunnyFl's ordering note: for `sendmsg`, the iov is still copied into a host buffer before the check. This predates the PR and is not a regression. ## Not verified - Windows and macOS builds. BunnyFl reports that Windows already returned `ENOTCONN` on `main`. - Console behaviour: the debt entry says these paths were not measured on a PS5. - The failed-`connect` case above was not run. - I did not separately test the `sendmsg` path; it reaches the check through `sendto_nid_postfix` as BunnyFl describes, but no test calls `sendmsg` on an unconnected stream socket. <!-- anyps5-bots job=2043 head=b83067a484129701bb780ba2b0b430b1a0f56d1c -->
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!15135
No description provided.