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 <[email protected]>

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/[email protected]/

> 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/[email protected]?part=1

Reply via email to