Linux virtualization list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Bobby Eshleman" <bobbyeshleman@meta.com>
Cc: virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
Date: Thu, 19 Nov 2026 01:34:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-222284@kernel.org> (raw)
In-Reply-To: <20261117-vsock-connect-close-v2-1-db5388323770@meta.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] vsock: incomplete handling of post-handshake reset in
  vsock_connect()
- [Medium] vsock: concurrent vsock_connect() waiter stays in loop and
  clobbers retry
--

commit 82a88284866b93cccb5d48f9bd76a86ebb091f06
Author: Bobby Eshleman <bobbyeshleman@meta.com>

vsock: only undo SS_CONNECTING when connect() fails

This commit adds vsock_connect_close() so vsock_connect() only resets
sk->sk_state to TCP_CLOSE when it is still TCP_SYN_SENT and sock->state to
SS_UNCONNECTED when it is still SS_CONNECTING.

Link: https://lore.kernel.org/netdev/20260909-vsock-connect-reset-closing-v1-1-50298b9ccfbf@rbox.co/

> diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> index 1d7d53542724f..ce958b48295b9 100644
> --- a/net/vmw_vsock/af_vsock.c
> +++ b/net/vmw_vsock/af_vsock.c
> @@ -1704,6 +1704,19 @@ static void vsock_connect_timeout(struct work_struct *work)
>  	sock_put(sk);
>  }
>  
> +/* Avoid breaking the socket when another thread has changed its state while
> + * we were asleep.
> + */
> +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;
> +}

[Severity: Medium]
Can a second thread blocked in vsock_connect() stay in the wait loop after
the first attempt fails, and later clobber a retry connect() on the same
socket?

CPU0                              CPU1
vsock_connect()
  sk->sk_state = TCP_SYN_SENT
  sock->state = SS_CONNECTING
  release_sock(sk)
                                  vsock_connect()
                                    /* sees SS_CONNECTING */
                                    release_sock(sk)
  /* timeout or peer RST */
  lock_sock(sk)
  sock_error(sk) /* clears sk_err */
  vsock_connect_close(sock)
    sk->sk_state = TCP_CLOSE
    sock->state = SS_UNCONNECTED
  release_sock(sk)
                                  lock_sock(sk)
                                  /* sk_state == TCP_CLOSE && sk_err == 0,
                                   * so while loop continues
                                   */
                                  release_sock(sk)
  vsock_connect() /* retry */
    sk->sk_state = TCP_SYN_SENT
    sock->state = SS_CONNECTING
    release_sock(sk)
                                  /* timeout or signal */
                                  lock_sock(sk)
                                  vsock_connect_close(sock)
                                    /* resets retry's TCP_SYN_SENT and
                                     * SS_CONNECTING
                                     */
                                  vsock_transport_cancel_pkt(vsk)

Because the wait loop only exits on TCP_ESTABLISHED, TCP_CLOSING or
sk->sk_err != 0, CPU1 does not see that the socket left TCP_SYN_SENT once
CPU0 clears sk->sk_err (or times out with sk->sk_err == 0).  When CPU1 later
times out or gets a signal, vsock_connect_close() sees TCP_SYN_SENT and
SS_CONNECTING from the new connect() attempt, resets them to TCP_CLOSE and
SS_UNCONNECTED while CPU0 sleeps, and cancels the new attempt's packets.
Even if userspace called listen() instead of retrying connect(), CPU1 still
calls vsock_transport_cancel_pkt() on timeout.

Also, if another thread consumes sk->sk_err via getsockopt(SO_ERROR) or
recvmsg(MSG_ERRQUEUE) before vsock_connect() calls sock_error(sk), err is 0,
so vsock_connect() skips vsock_connect_close() and returns 0 while leaving
sock->state as SS_CONNECTING with sk->sk_state == TCP_CLOSE.

> @@ -1862,8 +1875,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 +1889,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);

[Severity: Medium]
When the transport completes the handshake (setting sock->state to
SS_CONNECTED and sk->sk_state to TCP_ESTABLISHED) and then resets the
socket to TCP_CLOSE while vsock_connect() is sleeping, does vsock_connect()
still mishandle the post-handshake reset in the wait loop and error path?

First, in vmci_transport_handle_detach(), a detach after the socket has
already reached TCP_ESTABLISHED sets sk->sk_state = TCP_CLOSE without
setting sk->sk_err:

net/vmw_vsock/vmci_transport.c:vmci_transport_handle_detach() {
    ...
			if (sk->sk_state == TCP_SYN_SENT) {
				sk->sk_state = TCP_CLOSE;
				sk->sk_err = ECONNRESET;
				sk_error_report(sk);
				return;
			}
			sk->sk_state = TCP_CLOSE;
    ...
}

When vsock_connect() wakes up, sk->sk_state is neither TCP_ESTABLISHED nor
TCP_CLOSING and sk->sk_err is 0, so it re-enters schedule_timeout() and
blocks until connect_timeout expires.

Second, if a timeout or signal coincides with a post-handshake reset to
TCP_CLOSE, vsock_connect() enters the signal/timeout branch and calls
vsock_transport_cancel_pkt(vsk) on a socket whose sock->state is already
SS_CONNECTED.  If another thread called sendmsg() once sock->state became
SS_CONNECTED, virtio_transport_cancel_pkt() purges queued
VIRTIO_VSOCK_OP_RW packets without decrementing vvs->bytes_unsent, leaving
unsent-byte accounting for SIOCOUTQ and vsock_linger() out of sync.

Third, when virtio_vsock_reset_sock() or vhost_vsock_reset_orphans() sets
sk->sk_err = ECONNRESET after the handshake completed, vsock_connect()
exits the loop, consumes sk->sk_err via sock_error(sk), and returns
-ECONNRESET from connect() while leaving sock->state as SS_CONNECTED.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-vsock-connect-close-v2-1-db5388323770@meta.com?part=1

  parent reply	other threads:[~2026-11-19  1:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  1:34 [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails Bobby Eshleman
2026-11-18  1:38 ` netdev-bot+sinfo
2026-11-19  1:34 ` sashiko-bot [this message]
2026-11-19  2:27 ` netdev-bot+sashiko

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=sashiko-outbox-222284@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bobbyeshleman@meta.com \
    --cc=eperezma@redhat.com \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=virtualization@lists.linux.dev \
    /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