netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vhost/vsock: fix reset orphans race with close timeout
@ 2018-12-06 19:14 Stefan Hajnoczi
  2018-12-09  5:25 ` David Miller
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Hajnoczi @ 2018-12-06 19:14 UTC (permalink / raw)
  To: kvm
  Cc: Michael S. Tsirkin, netdev, Maximilian Riemensberger,
	Graham Whaley, Stefan Hajnoczi

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.

Reported-by: Maximilian Riemensberger <riemensberger@cadami.net>
Cc: Graham Whaley <graham.whaley@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 drivers/vhost/vsock.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 34bc3ab40c6d..731e2ea2aeca 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -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)
-- 
2.19.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] vhost/vsock: fix reset orphans race with close timeout
  2018-12-06 19:14 [PATCH] vhost/vsock: fix reset orphans race with close timeout Stefan Hajnoczi
@ 2018-12-09  5:25 ` David Miller
  2018-12-09 14:28   ` Michael S. Tsirkin
  0 siblings, 1 reply; 3+ messages in thread
From: David Miller @ 2018-12-09  5:25 UTC (permalink / raw)
  To: stefanha; +Cc: kvm, mst, netdev, riemensberger, graham.whaley

From: Stefan Hajnoczi <stefanha@redhat.com>
Date: Thu,  6 Dec 2018 19:14:34 +0000

> 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.
> 
> Reported-by: Maximilian Riemensberger <riemensberger@cadami.net>
> Cc: Graham Whaley <graham.whaley@gmail.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] vhost/vsock: fix reset orphans race with close timeout
  2018-12-09  5:25 ` David Miller
@ 2018-12-09 14:28   ` Michael S. Tsirkin
  0 siblings, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2018-12-09 14:28 UTC (permalink / raw)
  To: David Miller; +Cc: stefanha, kvm, netdev, riemensberger, graham.whaley

On Sat, Dec 08, 2018 at 09:25:44PM -0800, David Miller wrote:
> From: Stefan Hajnoczi <stefanha@redhat.com>
> Date: Thu,  6 Dec 2018 19:14:34 +0000
> 
> > 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.
> > 
> > Reported-by: Maximilian Riemensberger <riemensberger@cadami.net>
> > Cc: Graham Whaley <graham.whaley@gmail.com>
> > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> 
> 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-12-09 14:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-12-06 19:14 [PATCH] vhost/vsock: fix reset orphans race with close timeout Stefan Hajnoczi
2018-12-09  5:25 ` David Miller
2018-12-09 14:28   ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).