Skip to contentSkip to conversation

vhost/vsock: fix reset orphans race with close timeout

PATCH In review Stefan Hajnoczi 3 participants 3 messages Dec 6, 2018 – Dec 9, 2018

Subsystems (5)
lore
Share & tools
lore

Stefan Hajnoczi

author maintainer
#0 lore raw

PATCH 1/1 vhost/vsock: fix reset orphans race with close timeout

If a local process has closed a connected socket and hasn't received a RST packet yet, then the socket remains in the table until a timeout expires.

When a vhost_vsock instance is released with the timeout still pending, the socket is never freed because vhost_vsock has already set the SOCK_DONE flag.

Check if the close timer is pending and let it close the socket. This prevents the race which can leak sockets.

---
 drivers/vhost/vsock.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/vhost/vsock.c +15 -7
index 34bc3ab40c6d..731e2ea2aeca 100644
@@ -563,13 +563,21 @@ static void vhost_vsock_reset_orphans(struct sock *sk)
 	 * executing.
 	 */
 
-	if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
-		sock_set_flag(sk, SOCK_DONE);
-		vsk->peer_shutdown = SHUTDOWN_MASK;
-		sk->sk_state = SS_UNCONNECTED;
-		sk->sk_err = ECONNRESET;
-		sk->sk_error_report(sk);
-	}
+	/* If the peer is still valid, no need to reset connection */
+	if (vhost_vsock_get(vsk->remote_addr.svm_cid))
+		return;
+
+	/* If the close timeout is pending, let it expire.  This avoids races
+	 * with the timeout callback.
+	 */
+	if (vsk->close_work_scheduled)
+		return;
+
+	sock_set_flag(sk, SOCK_DONE);
+	vsk->peer_shutdown = SHUTDOWN_MASK;
+	sk->sk_state = SS_UNCONNECTED;
+	sk->sk_err = ECONNRESET;
+	sk->sk_error_report(sk);
 }
 
 static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
Signature · 1 line
2.19.2
maintainer
#1 lore raw

From: Stefan Hajnoczi <[email protected]>
Date: Thu, 6 Dec 2018 19:14:34 +0000

Quoted 8 lines

If a local process has closed a connected socket and hasn't received a RST packet yet, then the socket remains in the table until a timeout expires.

When a vhost_vsock instance is released with the timeout still pending, the socket is never freed because vhost_vsock has already set the SOCK_DONE flag.

Quoted 6 lines

Check if the close timer is pending and let it close the socket. This prevents the race which can leak sockets.

Michael please review, and let me know if you want me to apply this directly and queue it up for -stable.

#2 lore raw
Quoted 14 lines · David Miller

From: Stefan Hajnoczi <[email protected]>
Date: Thu, 6 Dec 2018 19:14:34 +0000

Quoted 11 lines

If a local process has closed a connected socket and hasn't received a RST packet yet, then the socket remains in the table until a timeout expires.

When a vhost_vsock instance is released with the timeout still pending, the socket is never freed because vhost_vsock has already set the SOCK_DONE flag.

Check if the close timer is pending and let it close the socket. This prevents the race which can leak sockets.

Quoted 6 lines
Quoted 3 lines

Michael please review, and let me know if you want me to apply this directly and queue it up for -stable.

I sent this to Linus already.