Repository navigation
fix(libkernel): fail a send on a never-connected stream socket with ENOTCONN - #15135
adriadam10 merged 2 commits into
Conversation
…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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
@mashingaan thanks, you're right. Done in b83067a.
|
XairRrR
left a comment
There was a problem hiding this comment.
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
connectfailed still goes to the host.connectStartedis set before the hostconnect(core/libs/prx/libkernel/Socket/src/Udp.cpp:1496-1498, head) and is never cleared, even when the call fails withECONNREFUSEDorETIMEDOUT.- After such a failure the socket is again unconnected. FreeBSD's
sosend_genericwould returnENOTCONN, while Linuxtcp_sendmsgreturnsEPIPEand raisesSIGPIPEunlessMSG_NOSIGNALorSO_NOSIGPIPEis 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
sendtowith an address and an in-progress non-blockingconnect. - Suggested fix: clear
connectStartedundermodeMutexwhen the hostconnectfails 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.
- After such a failure the socket is again unconnected. FreeBSD's
- 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
ENOTCONNonmain. - Console behaviour: the debt entry says these paths were not measured on a PS5.
- The failed-
connectcase above was not run. - I did not separately test the
sendmsgpath; it reaches the check throughsendto_nid_postfixas BunnyFl describes, but no test callssendmsgon an unconnected stream socket.
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.
|
Checked the ENOTCONN ordering against FreeBSD 11.4 |
|
Thanks @adriadam10 for reviewing the FreeBSD ordering, the SCM_RIGHTS interaction and the regression coverage, and for merging this. |
What
send,sendtowithout an address,sendmsgandwriteon a TCP socket that was never connected (a new socket, or a listening one) went to the host. On Linux the host answeredEPIPEand raised its ownSIGPIPE, which killed the guest beforeSO_NOSIGPIPEorMSG_NOSIGNALcould matter. FreeBSD 11.4'ssosend_genericfails such a send withENOTCONN: TCP hasPR_CONNREQUIRED|PR_IMPLOPCL(in_proto.c), so with no address and no connection it returnsENOTCONN. That check runs afterSBS_CANTSENDMOREandso_error, neither of which a never-connected socket can have, and before the data is copied in, so it also comes beforeEFAULT.A guest socket now records whether
connectwas called on it.accept,socketpairand a socket received throughSCM_RIGHTScount as connected, and the host decides those as before. A stream socket that does not qualify fails these sends withENOTCONNand no signal. Unchanged:sendtowith an address;connectis in progress;EDESTADDRREQmapping.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, onmainb28bb77. Windows and macOS were not compiled here. No title was run.guest_socket_send_before_connecttest: it coverssend(including zero-length andMSG_NOSIGNAL),sendtowithout an address andwriteon a new socket and on a listening one, and then a normal exchange afterconnectandaccept.main'sUdp.cppthe test process is killed bySIGPIPE(exit 141).SCM_RIGHTScase, the existingguest_socket_rightstest fails, because a received connected socket would be refused. With it, the test passes.ctest -R '^guest_|^net_|public_headers' -j12(1482 tests) underunshare -rnwithloup: everything passes exceptguest_socket_options_multicast, which fails onmaintoo on this host.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no confirmed overlap. Its not-compared PRs were inspected by hand:Udp.cppin other functions: ioctl,MSG_WAITALLreceive, macOS pipe readiness, and forwardingsceNetSocketdescriptors, which are not in this socket table.CMakeLists.txtordocs/dev/debt/libkernel.md.SIGPIPEon Linux) is complementary: this change stops the signal for sockets that were never connected.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)