From: Bobby Eshleman <bobbyeshleman@meta.com>
To: Stefano Garzarella <sgarzare@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
George Zhang <georgezhang@vmware.com>,
Andy King <acking@vmware.com>, Dmitry Torokhov <dtor@vmware.com>,
Willem de Bruijn <willemb@google.com>
Cc: <virtualization@lists.linux.dev>, <netdev@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, Kyle Zeng <kylebot@openai.com>,
Michal Luczaj <mhal@rbox.co>,
Bobby Eshleman <bobbyeshleman@meta.com>
Subject: [PATCH net v3] vsock: only undo SS_CONNECTING when connect() fails
Date: Mon, 30 Nov 2026 16:52:53 -0800 [thread overview]
Message-ID: <20261130-vsock-connect-close-v3-1-7a9abc4e3474@meta.com> (raw)
vsock_connect() sleeps with the socket unlocked. Meanwhile the transport
can complete the handshake, setting sock->state to SS_CONNECTED, and then
reset the connection, e.g. in vhost_vsock_reset_orphans(),
virtio_vsock_reset_sock() or vmci_transport_handle_detach(). When
connect() gets the lock back, it finds sk_err set and moves the socket
back to SS_UNCONNECTED. Userspace can then connect() the socket again,
and a connect() to a CID on another transport makes
vsock_assign_transport() release the transport the socket was connected
on.
Only move the socket back to SS_UNCONNECTED if it is still
SS_CONNECTING. A socket that got connected stays SS_CONNECTED, and
another connect() fails with -EISCONN.
Likewise, only move sk_state to TCP_CLOSE if it is still TCP_SYN_SENT.
Otherwise a second thread blocked in connect() on the same socket can
time out after the first one failed and called listen(), and overwrite
TCP_LISTEN.
Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
Reported-by: Kyle Zeng <kylebot@openai.com>
Link: https://lore.kernel.org/netdev/20260909-vsock-connect-reset-closing-v1-1-50298b9ccfbf@rbox.co/
Suggested-by: Michal Luczaj <mhal@rbox.co>
Reviewed-by: Michal Luczaj <mhal@rbox.co>
Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
---
Changes in v3:
- Explain why vsock_connect_close() undoes only the state that connect()
set (Stefano)
- Pick up Michal's RoB
- Link to v2: https://lore.kernel.org/r/20261117-vsock-connect-close-v2-1-db5388323770@meta.com
Changes in v2:
- Only reset sk_state if it is still TCP_SYN_SENT, so a concurrent
connect() caller that times out late doesn't overwrite TCP_LISTEN
(Sashiko).
- Drop Michal's Reviewed-by since the code changed.
- Link to v1: https://lore.kernel.org/r/20261116-vsock-connect-close-v1-1-43fb125973dd@meta.com
---
net/vmw_vsock/af_vsock.c | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 1d7d53542724..782bcb2eb956 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1704,6 +1704,21 @@ static void vsock_connect_timeout(struct work_struct *work)
sock_put(sk);
}
+/* Undo only the state this connect() attempt set. While we slept, the
+ * transport may have connected and then reset the socket (it is still in
+ * vsock_connected_table, so it must not go back to SS_UNCONNECTED), or
+ * another thread may have turned it into a listener.
+ */
+static void vsock_connect_close(struct socket *sock)
+{
+ struct sock *sk = sock->sk;
+
+ if (sk->sk_state == TCP_SYN_SENT)
+ sk->sk_state = TCP_CLOSE;
+ if (sock->state == SS_CONNECTING)
+ sock->state = SS_UNCONNECTED;
+}
+
static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
int addr_len, int flags)
{
@@ -1862,8 +1877,7 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
* sk_state == TCP_SYN_SENT, which hereby we break.
* In such case VIRTIO_VSOCK_OP_RST will follow.
*/
- sk->sk_state = TCP_CLOSE;
- sock->state = SS_UNCONNECTED;
+ vsock_connect_close(sock);
/* Try to cancel VIRTIO_VSOCK_OP_REQUEST skb sent out by
* transport->connect().
@@ -1877,10 +1891,8 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
}
err = sock_error(sk);
- if (err) {
- sk->sk_state = TCP_CLOSE;
- sock->state = SS_UNCONNECTED;
- }
+ if (err)
+ vsock_connect_close(sock);
out_wait:
finish_wait(sk_sleep(sk), &wait);
---
base-commit: 76fca30e8d34eb151fcf4f363b51c21b7e8c4709
change-id: 20261116-vsock-connect-close-1beb7cb51e7a
Best regards,
--
Bobby Eshleman <bobbyeshleman@meta.com>
next reply other threads:[~2026-12-01 0:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 0:52 Bobby Eshleman [this message]
2026-12-01 0:58 ` [PATCH net v3] vsock: only undo SS_CONNECTING when connect() fails netdev-bot+sinfo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130-vsock-connect-close-v3-1-7a9abc4e3474@meta.com \
--to=bobbyeshleman@meta.com \
--cc=acking@vmware.com \
--cc=davem@davemloft.net \
--cc=dtor@vmware.com \
--cc=edumazet@kernel.org \
--cc=georgezhang@vmware.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kylebot@openai.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhal@rbox.co \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sgarzare@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox