* [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
@ 2026-11-30 4:59 Hu Xinyao
2026-11-30 13:23 ` Michal Luczaj
2026-12-01 17:05 ` Stefano Garzarella
0 siblings, 2 replies; 6+ messages in thread
From: Hu Xinyao @ 2026-11-30 4:59 UTC (permalink / raw)
To: Stefan Hajnoczi, Stefano Garzarella
Cc: Bobby Eshleman, Michael S. Tsirkin, Jason Wang, Eugenio Perez,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
virtualization, kvm, netdev, linux-kernel
virtio_transport_close() takes a socket reference and schedules
vsk->close_work with vsk->close_work_scheduled set.
Once a worker dequeues close_work, WORK_STRUCT_PENDING_BIT is cleared
before virtio_transport_close_timeout() acquires lock_sock(sk). If a
peer RST or SHUTDOWN arrives while close_timeout is waiting for the
socket lock, virtio_transport_do_close(vsk, true) sets SOCK_DONE and
calls virtio_transport_cancel_close_work(vsk, true), where
cancel_delayed_work() returns false and skips releasing the close_work
socket reference and removing the socket.
When virtio_transport_close_timeout() then acquires lock_sock(sk), it
sees SOCK_DONE already set, skips virtio_transport_do_close(vsk, false),
and clears vsk->close_work_scheduled, permanently leaking the socket
reference (and on OP_RST, leaving the socket in the connected table).
Call virtio_transport_cancel_close_work(vsk, false) when SOCK_DONE is
already set so whichever side runs last completes the cleanup.
Found during a GLM-5.4-assisted code audit and verified manually against
mainline; compile-tested only (not triggered at runtime).
Fixes: 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
Cc: stable@vger.kernel.org
Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
---
net/vmw_vsock/virtio_transport_common.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 1e762a4..9dade6e 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
(void)virtio_transport_reset(vsk, NULL);
virtio_transport_do_close(vsk, false);
+ } else {
+ virtio_transport_cancel_close_work(vsk, false);
}
- vsk->close_work_scheduled = false;
-
release_sock(sk);
sock_put(sk);
}
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
2026-11-30 4:59 [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close Hu Xinyao
@ 2026-11-30 13:23 ` Michal Luczaj
2026-12-01 9:10 ` Hu Xinyao
2026-12-01 17:05 ` Stefano Garzarella
1 sibling, 1 reply; 6+ messages in thread
From: Michal Luczaj @ 2026-11-30 13:23 UTC (permalink / raw)
To: Hu Xinyao, Stefan Hajnoczi, Stefano Garzarella
Cc: Bobby Eshleman, Michael S. Tsirkin, Jason Wang, Eugenio Perez,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
virtualization, kvm, netdev, linux-kernel
On 11/30/26 05:59, Hu Xinyao wrote:
> virtio_transport_close() takes a socket reference and schedules
> vsk->close_work with vsk->close_work_scheduled set.
>
> Once a worker dequeues close_work, WORK_STRUCT_PENDING_BIT is cleared
> before virtio_transport_close_timeout() acquires lock_sock(sk). If a
> peer RST or SHUTDOWN arrives while close_timeout is waiting for the
> socket lock, virtio_transport_do_close(vsk, true) sets SOCK_DONE and
> calls virtio_transport_cancel_close_work(vsk, true), where
> cancel_delayed_work() returns false and skips releasing the close_work
> socket reference and removing the socket.
>
> When virtio_transport_close_timeout() then acquires lock_sock(sk), it
> sees SOCK_DONE already set, skips virtio_transport_do_close(vsk, false),
> and clears vsk->close_work_scheduled, permanently leaking the socket
> reference (and on OP_RST, leaving the socket in the connected table).
Right, the leak is real. And netdev bot reported it some time ago [1].
Not sure what's the status of that series, though.
[1]
https://lore.kernel.org/netdev/179003303663.2160803.2038691427232135222@kernel.org/
> Call virtio_transport_cancel_close_work(vsk, false) when SOCK_DONE is
> already set so whichever side runs last completes the cleanup.
>
> Found during a GLM-5.4-assisted code audit and verified manually against
> mainline; compile-tested only (not triggered at runtime).
I'm really curious: did you/GLM try to trigger the leak (and fail), or you
just didn't try triggering it in the first place?
thanks,
Michal
> Fixes: 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
> ---
> net/vmw_vsock/virtio_transport_common.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
> index 1e762a4..9dade6e 100644
> --- a/net/vmw_vsock/virtio_transport_common.c
> +++ b/net/vmw_vsock/virtio_transport_common.c
> @@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
> (void)virtio_transport_reset(vsk, NULL);
>
> virtio_transport_do_close(vsk, false);
> + } else {
> + virtio_transport_cancel_close_work(vsk, false);
> }
>
> - vsk->close_work_scheduled = false;
> -
> release_sock(sk);
> sock_put(sk);
> }
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
2026-11-30 13:23 ` Michal Luczaj
@ 2026-12-01 9:10 ` Hu Xinyao
0 siblings, 0 replies; 6+ messages in thread
From: Hu Xinyao @ 2026-12-01 9:10 UTC (permalink / raw)
To: Michal Luczaj
Cc: Stefan Hajnoczi, Stefano Garzarella, Bobby Eshleman,
Michael S. Tsirkin, Jason Wang, Eugenio Perez, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, virtualization, kvm,
netdev, linux-kernel, Hu Xinyao
On Mon, Nov 30, 2026 at 02:23:11PM +0100, Michal Luczaj wrote:
> Right, the leak is real. And netdev bot reported it some time ago [1].
> Not sure what's the status of that series, though.
>
> [1]
> https://lore.kernel.org/netdev/179003303663.2160803.2038691427232135222@kernel.org/
>
> > Found during a GLM-5.4-assisted code audit and verified manually against
> > mainline; compile-tested only (not triggered at runtime).
>
> I'm really curious: did you/GLM try to trigger the leak (and fail), or you
> just didn't try triggering it in the first place?
Hi Michal,
Thanks for checking this and for the pointer to [1]!
To answer your question honestly: we didn't try triggering it at runtime
before sending v1 -- GLM-5.4 flagged the window between work dequeue
(clearing WORK_STRUCT_PENDING_BIT) and lock_sock(sk) during a static
audit pass, and we manually traced virtio_transport_close_timeout() vs
virtio_transport_do_close(vsk, true) against mainline and compile-tested
the patch.
Looking at current net / net-next, virtio_transport_common.c still skips
cleanup in virtio_transport_close_timeout() when SOCK_DONE is already
set, so virtio / vsock_loopback / vhost-vsock remain affected. We also
booted a QEMU kernel with CONFIG_VSOCKETS_LOOPBACK=y, CONFIG_KASAN=y,
and CONFIG_PROVE_LOCKING=y to verify the close paths with the fix
applied. Happy to follow up with a regression test under
tools/testing/vsock/ if that would be useful.
Best regards,
Hu Xinyao
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
2026-11-30 4:59 [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close Hu Xinyao
2026-11-30 13:23 ` Michal Luczaj
@ 2026-12-01 17:05 ` Stefano Garzarella
2026-12-02 3:29 ` Hu Xinyao
1 sibling, 1 reply; 6+ messages in thread
From: Stefano Garzarella @ 2026-12-01 17:05 UTC (permalink / raw)
To: Hu Xinyao
Cc: Stefan Hajnoczi, Bobby Eshleman, Michael S. Tsirkin, Jason Wang,
Eugenio Perez, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, virtualization, kvm, netdev, linux-kernel
On Mon, Nov 30, 2026 at 01:59:42PM +0900, Hu Xinyao wrote:
>virtio_transport_close() takes a socket reference and schedules
>vsk->close_work with vsk->close_work_scheduled set.
>
>Once a worker dequeues close_work, WORK_STRUCT_PENDING_BIT is cleared
>before virtio_transport_close_timeout() acquires lock_sock(sk). If a
>peer RST or SHUTDOWN arrives while close_timeout is waiting for the
>socket lock, virtio_transport_do_close(vsk, true) sets SOCK_DONE and
>calls virtio_transport_cancel_close_work(vsk, true), where
>cancel_delayed_work() returns false and skips releasing the close_work
>socket reference and removing the socket.
>
>When virtio_transport_close_timeout() then acquires lock_sock(sk), it
>sees SOCK_DONE already set, skips virtio_transport_do_close(vsk, false),
>and clears vsk->close_work_scheduled, permanently leaking the socket
>reference (and on OP_RST, leaving the socket in the connected table).
>
>Call virtio_transport_cancel_close_work(vsk, false) when SOCK_DONE is
>already set so whichever side runs last completes the cleanup.
>
>Found during a GLM-5.4-assisted code audit and verified manually against
>mainline; compile-tested only (not triggered at runtime).
>
>Fixes: 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")
>Cc: stable@vger.kernel.org
>Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
>---
> net/vmw_vsock/virtio_transport_common.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index 1e762a4..9dade6e 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
> (void)virtio_transport_reset(vsk, NULL);
>
> virtio_transport_do_close(vsk, false);
>+ } else {
>+ virtio_transport_cancel_close_work(vsk, false);
> }
>
>- vsk->close_work_scheduled = false;
Why we are removing this?
Thanks,
Stefano
>-
> release_sock(sk);
> sock_put(sk);
> }
>--
>2.43.0
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
2026-12-01 17:05 ` Stefano Garzarella
@ 2026-12-02 3:29 ` Hu Xinyao
2026-12-02 8:29 ` Stefano Garzarella
0 siblings, 1 reply; 6+ messages in thread
From: Hu Xinyao @ 2026-12-02 3:29 UTC (permalink / raw)
To: Stefano Garzarella
Cc: Stefan Hajnoczi, Bobby Eshleman, Michael S. Tsirkin, Jason Wang,
Eugenio Perez, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, virtualization, kvm, netdev, linux-kernel,
Hu Xinyao
On Tue, Dec 01, 2026 at 06:05:30PM +0100, Stefano Garzarella wrote:
> >diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
> >index 1e762a4..9dade6e 100644
> >--- a/net/vmw_vsock/virtio_transport_common.c
> >+++ b/net/vmw_vsock/virtio_transport_common.c
> >@@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
> > (void)virtio_transport_reset(vsk, NULL);
> >
> > virtio_transport_do_close(vsk, false);
> >+ } else {
> >+ virtio_transport_cancel_close_work(vsk, false);
> > }
> >
> >- vsk->close_work_scheduled = false;
>
> Why we are removing this?
Hi Stefano,
With the else branch added, both branches now call
virtio_transport_cancel_close_work(vsk, false) (the !SOCK_DONE branch
via virtio_transport_do_close(vsk, false), and the SOCK_DONE branch
directly).
In virtio_transport_cancel_close_work(vsk, false), !cancel_timeout is
unconditionally true, so whenever vsk->close_work_scheduled is set it
already clears vsk->close_work_scheduled = false (along with calling
virtio_transport_remove_sock(vsk) and sock_put(sk)), and if it was
already false it remains false. Previously, the trailing assignment only
had an effect when SOCK_DONE was already set and do_close() was skipped,
clearing the flag without releasing the socket reference or removing the
socket.
Sorry for not spelling that out in the commit message (since
virtio_transport_cancel_close_work() is outside the diff context).
Alternatively, we could avoid the else branch and just replace
"vsk->close_work_scheduled = false;" with an unconditional
"virtio_transport_cancel_close_work(vsk, false);" after the if
(!sock_flag(sk, SOCK_DONE)) block, which is a no-op if do_close()
already ran. Happy to send a v2 whichever way you prefer.
Thanks,
Hu Xinyao
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close
2026-12-02 3:29 ` Hu Xinyao
@ 2026-12-02 8:29 ` Stefano Garzarella
0 siblings, 0 replies; 6+ messages in thread
From: Stefano Garzarella @ 2026-12-02 8:29 UTC (permalink / raw)
To: Hu Xinyao
Cc: Stefan Hajnoczi, Bobby Eshleman, Michael S. Tsirkin, Jason Wang,
Eugenio Perez, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, virtualization, kvm, netdev, linux-kernel
On Wed, Dec 02, 2026 at 12:29:14PM +0900, Hu Xinyao wrote:
>On Tue, Dec 01, 2026 at 06:05:30PM +0100, Stefano Garzarella wrote:
>> >diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> >index 1e762a4..9dade6e 100644
>> >--- a/net/vmw_vsock/virtio_transport_common.c
>> >+++ b/net/vmw_vsock/virtio_transport_common.c
>> >@@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
>> > (void)virtio_transport_reset(vsk, NULL);
>> >
>> > virtio_transport_do_close(vsk, false);
>> >+ } else {
>> >+ virtio_transport_cancel_close_work(vsk, false);
>> > }
>> >
>> >- vsk->close_work_scheduled = false;
>>
>> Why we are removing this?
>
>Hi Stefano,
>
>With the else branch added, both branches now call
>virtio_transport_cancel_close_work(vsk, false) (the !SOCK_DONE branch
>via virtio_transport_do_close(vsk, false), and the SOCK_DONE branch
>directly).
Okay, I see now.
>
>In virtio_transport_cancel_close_work(vsk, false), !cancel_timeout is
>unconditionally true, so whenever vsk->close_work_scheduled is set it
>already clears vsk->close_work_scheduled = false (along with calling
>virtio_transport_remove_sock(vsk) and sock_put(sk)), and if it was
>already false it remains false. Previously, the trailing assignment only
>had an effect when SOCK_DONE was already set and do_close() was skipped,
>clearing the flag without releasing the socket reference or removing the
>socket.
>
>Sorry for not spelling that out in the commit message (since
>virtio_transport_cancel_close_work() is outside the diff context).
>Alternatively, we could avoid the else branch and just replace
>"vsk->close_work_scheduled = false;" with an unconditional
>"virtio_transport_cancel_close_work(vsk, false);" after the if
>(!sock_flag(sk, SOCK_DONE)) block, which is a no-op if do_close()
>already ran. Happy to send a v2 whichever way you prefer.
Just mention this in the commit message. Without too many words,
including only the necessary information. The commit message is already
hard enough to understand...
This is just how I'd have wrote it (just a suggestion, feel free to
write as you prefer):
virtio_transport_close() takes a socket reference and schedules
vsk->close_work, which runs virtio_transport_close_timeout(). If the
peer resets the connection after the work started running but before
it takes the socket lock, nobody drops that reference:
CPU0 (close_work) CPU1 (rx: OP_RST)
close_timeout()
lock_sock()
lock_sock() /* waits */ do_close(vsk, true)
SOCK_DONE = 1
cancel_delayed_work() == false
/* ref not dropped */
release_sock()
SOCK_DONE set, skip do_close()
close_work_scheduled = false /* ref leaked */
Fix it by letting virtio_transport_close_timeout() complete the cleanup
when SOCK_DONE is already set. The explicit "close_work_scheduled =
false" is no longer needed, since virtio_transport_cancel_close_work()
clears it in both branches.
...
Thanks,
Stefano
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 8:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 4:59 [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close Hu Xinyao
2026-11-30 13:23 ` Michal Luczaj
2026-12-01 9:10 ` Hu Xinyao
2026-12-01 17:05 ` Stefano Garzarella
2026-12-02 3:29 ` Hu Xinyao
2026-12-02 8:29 ` Stefano Garzarella
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®