[PATCH net-next v6 1/8] vsock: only undo SS_CONNECTING when connect() fails

From: Bobby Eshleman

Date: Fri Nov 13 2026 - 22:22:51 EST


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.

Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
Suggested-by: Michal Luczaj <mhal@xxxxxxx>
Link: https://lore.kernel.org/netdev/179447093204.2199510.8895677421994612994@xxxxxxxxxx/
Signed-off-by: Bobby Eshleman <bobbyeshleman@xxxxxxxx>
---
net/vmw_vsock/af_vsock.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 483af15a5109..dc81cd47f099 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1703,6 +1703,16 @@ static void vsock_connect_timeout(struct work_struct *work)
sock_put(sk);
}

+/* Avoid breaking the socket when another thread has changed its sock->state
+ * while we were asleep.
+ */
+static void vsock_connect_close(struct socket *sock)
+{
+ sock->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)
{
@@ -1861,8 +1871,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().
@@ -1876,10 +1885,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);

--
2.53.1-Meta