When connect() is scheduled out awaiting TCP_SYN_SENT -> TCP_ESTABLISHED,
socket may already transition TCP_ESTABLISHED -> TCP_CLOSING
(VIRTIO_VSOCK_OP_RST) or have sk_err set (VIRTIO_VSOCK_OP_RW). Short
circuit the logic; do not assume a non-zero sk_err signifies a failed
connect. connect() should return the status of connection attempt itself,
not what followed.

Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
Reported-by: Hyunwoo Kim <[email protected]>
Link: https://lore.kernel.org/netdev/anzT1fREOSyHT99k@v4bel/
Signed-off-by: Michal Luczaj <[email protected]>
---
 net/vmw_vsock/af_vsock.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 5cecd1c9c6e5..ff4140aaf1f3 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1846,15 +1846,19 @@ static int vsock_connect(struct socket *sock, struct 
sockaddr_unsized *addr,
                timeout = schedule_timeout(timeout);
                lock_sock(sk);
 
-               /* Connection established. Whatever happens to socket once we
-                * release it, that's not connect()'s concern. No need to go
+               /* Connection was established. Whatever happens to socket once
+                * we release it, that's not connect()'s concern. No need to go
                 * into signal and timeout handling. Call it a day.
                 *
                 * Note that allowing to "reset" an already established socket
                 * here is racy and insecure.
                 */
-               if (sk->sk_state == TCP_ESTABLISHED)
-                       break;
+               if (sk->sk_state == TCP_ESTABLISHED ||
+                   sk->sk_state == TCP_CLOSING) {
+                       err = 0;
+                       finish_wait(sk_sleep(sk), &wait);
+                       goto out;
+               }
 
                /* If connection was _not_ established and a signal/timeout came
                 * to be, we want the socket's state reset. We keep the binding.
@@ -1877,8 +1881,7 @@ static int vsock_connect(struct socket *sock, struct 
sockaddr_unsized *addr,
 out_wait:
        finish_wait(sk_sleep(sk), &wait);
 out_reset:
-       if (err)
-               vsock_unconnected_reset(sk);
+       vsock_unconnected_reset(sk);
 out:
        release_sock(sk);
        return err;

-- 
2.55.0


Reply via email to