Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
MarrasFederico:fix/libkernel-stream-send-enotconn
Nov 7, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
MarrasFederico:fix/libkernel-stream-send-enotconn

Conversation

@MarrasFederico

Copy link
Copy Markdown
Contributor

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 b28bb77. Windows and macOS were not compiled here. No title was run.

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

…NOTCONN

send, sendto without an address, sendmsg and write on a stream socket on
which connect was never called, that was not accepted and that was not
received in a message now fail with ENOTCONN without asking the host, as
FreeBSD 11.4's sosend_generic does. Linux answered EPIPE and raised its
own SIGPIPE, which killed the guest.

@BunnyFl BunnyFl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 mashingaan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MarrasFederico

Copy link
Copy Markdown
Contributor Author

@mashingaan thanks, you're right. Done in b83067a.

  • PassedSocket no longer marks every received socket as connected. A received stream socket counts as connected only when the host reports a peer (getpeername succeeds). A fresh or listening socket therefore keeps the ENOTCONN guard. A socket still connecting also gets ENOTCONN, which matches sosend_generic with no address. Datagram sockets are unchanged.
  • New CheckPassedFreshStream in guest_socket_rights: it passes a new AF_INET stream socket through SCM_RIGHTS, then checks that send, sendto without an address and write on the received descriptor all fail with ENOTCONN and that the process survives.
    • Without the change, the test process is killed by SIGPIPE (exit 141).
    • With it, the test passes, and the connected-socket case in the same file still passes.
  • guest_socket_rights, guest_socket_send_before_connect, guest_socket_pair, guest_tcp and guest_poll pass under unshare -rn, and check_conventions passes. The debt entry now describes the received-socket rule.

@XairRrR XairRrR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@adriadam10 adriadam10 self-assigned this Nov 7, 2026
@adriadam10
adriadam10 dismissed mashingaan’s stale review November 7, 2026 00:25

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@adriadam10
adriadam10 merged commit 30a863d into boykopovar:main Nov 7, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the ENOTCONN ordering against FreeBSD 11.4 sosend_generic, the send/sendto/sendmsg/write paths, the SCM_RIGHTS fix for fresh sockets and its test, and the debt entry; merged.

@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks @adriadam10 for reviewing the FreeBSD ordering, the SCM_RIGHTS interaction and the regression coverage, and for merging this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants