fix(libkernel): fail a send on a never-connected stream socket with ENOTCONN #15135
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15135"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-02T17:51:13Z.
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, onmainb28bb77217. 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)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 hasPR_IMPLOPCL, so with no address and no connection it takes theaddr == NULLbranch and returnsENOTCONNfor any length, afterSBS_CANTSENDMORE/so_error, which a never-connected socket can't have.sendmsgwithout a name reaches the check throughsendto_nid_postfix(Udp.cpp:1952) andwritethroughsend_nid_postfix(:2508), so all four paths in the description get it.connectStartedis set undermodeMutexbefore the hostconnect, andSendBeforeConnectreads it under the same lock.Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto
main:guest_socket_send_before_connectpasses. Withmain'sUdp.cppit also passes here: main already returnsENOTCONNfor these sends on Windows (presumably from Winsock's ownWSAENOTCONN; I didn't trace the mapping), so the bug and the negative control are Linux-side (where the host'sEPIPE/SIGPIPEcame 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 withENOBUFS) before the check, so "beforeEFAULT" holds forsend/sendto/writebut not forsendmsg. That ordering predates this PR.Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T20:29:26Z.
Review: DISMISSED
PassedSocket(int native) unconditionally sets
connectStarted = true.SCM_RIGHTScan also carry a fresh, unconnected stream socket, so after receiving that descriptorSendBeforeConnectwill skip the newENOTCONNguard. A subsequent send is delegated to the host again and can reproduce the LinuxEPIPEplusSIGPIPEfailure this PR fixes. The newGuestSocketSendBeforeConnecttest 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 throughSCM_RIGHTSand verifies the send returnsENOTCONNwithout killing the process.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'sUdp.cpprestored, both the new test and the extended test are killed bySIGPIPE.What I ran
Head
b83067a484129701bb780ba2b0b430b1a0f56d1c,main1e658521a0a599872612bb91949311e2956b5283, merged with--no-ffin a clean worktree. Debian 13 VM, GCC 14.2.0, Release, Ninja,BUILD_TESTING=ON.Results
git diff --checkcheck_conventions.pymain(guest_socket_options_multicastamong them)guest_socket_send_before_connect,guest_socket_rights,guest_socket_pair,guest_tcp,guest_pollguest_poll, 4.5 s)check_skips.pyguest_net_ctlskipped, which also happens onmainNegative control
I restored only
core/libs/prx/libkernel/Socket/src/Udp.cppfrommainand rebuilt. Both tests failed:ctest output
After I restored the merged file and rebuilt, both tests passed again, and
git status --porcelainwas empty. This confirms the author's exit-141 claim for both tests. It also confirms that the newCheckPassedFreshStreamcovers theSCM_RIGHTScase that mashingaan raised. The fix inb83067a4(judging a received stream socket bygetpeername) addresses that request, and the new test catches a regression there.Findings
Blocking
None.
Non-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.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.sendtowith an address and an in-progress non-blockingconnect.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.sendmsg, the iov is still copied into a host buffer before the check. This predates the PR and is not a regression.Not verified
ENOTCONNonmain.connectcase above was not run.sendmsgpath; it reaches the check throughsendto_nid_postfixas BunnyFl describes, but no test callssendmsgon an unconnected stream socket.