Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID
@ 2026-11-30  8:52 Bilal Hashim
  2026-12-01  8:52 ` sashiko-bot
  2026-12-01 14:56 ` Stefano Garzarella
  0 siblings, 2 replies; 5+ messages in thread
From: Bilal Hashim @ 2026-11-30  8:52 UTC (permalink / raw)
  To: Stefan Hajnoczi, Stefano Garzarella, Michael S. Tsirkin,
	Jason Wang, Eugenio Perez
  Cc: kvm, virtualization, netdev, linux-kernel

In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
assigned, hash_del_rcu(&vsock->hash) is immediately followed by
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
waiting for an RCU grace period.

Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
its next pointer while concurrent RCU readers in vhost_vsock_get()
(called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
This can cause RCU readers to:
- jump to another hash bucket or terminate early, missing unrelated active
  vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
  to reset healthy connections or vhost_transport_send_pkt() to return
  -ENODEV); or
- loop indefinitely inside an RCU read-side critical section (RCU stall /
  soft lockup) when two instances in the same bucket are repeatedly
  rehashed via VHOST_VSOCK_SET_GUEST_CID.

Fix this by returning early when the CID is unchanged, calling
synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
and re-inserting into vhost_vsock_hash, and annotating concurrent accesses
to vsock->guest_cid with WRITE_ONCE()/READ_ONCE().

Found by manual code inspection.

Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 drivers/vhost/vsock.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_g
 
 	hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid,
 				   lockdep_is_held(&vhost_vsock_mutex)) {
-		u32 other_cid = vsock->guest_cid;
+		u32 other_cid = READ_ONCE(vsock->guest_cid);
 
 		/* Skip instances that have no CID yet */
 		if (other_cid == 0)
@@ -572,7 +572,7 @@ static void vhost_vsock_handle_tx_kick(s
 		hdr = virtio_vsock_hdr(skb);
 
 		/* Only accept correctly addressed packets */
-		if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid &&
+		if (le64_to_cpu(hdr->src_cid) == READ_ONCE(vsock->guest_cid) &&
 		    le64_to_cpu(hdr->dst_cid) ==
 		    vhost_transport_get_local_cid())
 			virtio_transport_recv_pkt(&vhost_transport, skb,
@@ -856,10 +856,17 @@ static int vhost_vsock_set_cid(struct vh
 		return -EADDRINUSE;
 	}
 
-	if (vsock->guest_cid)
+	if (vsock->guest_cid == guest_cid) {
+		mutex_unlock(&vhost_vsock_mutex);
+		return 0;
+	}
+
+	if (vsock->guest_cid) {
 		hash_del_rcu(&vsock->hash);
+		synchronize_rcu();
+	}
 
-	vsock->guest_cid = guest_cid;
+	WRITE_ONCE(vsock->guest_cid, guest_cid);
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
 	mutex_unlock(&vhost_vsock_mutex);
 


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

* Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID
  2026-11-30  8:52 [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID Bilal Hashim
@ 2026-12-01  8:52 ` sashiko-bot
  2026-12-01 14:56 ` Stefano Garzarella
  1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01  8:52 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: kvm, virtualization, Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] vhost/vsock: Fixes trailer cites initial driver commit instead
  of 834e772c8db0
--

commit 77ca38e1302ecde9680d0e62c90492c21e199dba
Author: Bilal Hashim <AeeSec@126.com>

> vhost/vsock: wait for RCU grace period before rehashing CID
>
> In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
> assigned, hash_del_rcu(&vsock->hash) is immediately followed by
> hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
> waiting for an RCU grace period.
>
> Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
> its next pointer while concurrent RCU readers in vhost_vsock_get()
> (called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
> vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
> This can cause RCU readers to:
> - jump to another hash bucket or terminate early, missing unrelated active
>   vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
>   to reset healthy connections or vhost_transport_send_pkt() to return
>   -ENODEV); or
> - loop indefinitely inside an RCU read-side critical section (RCU stall /
>   soft lockup) when two instances in the same bucket are repeatedly
>   rehashed via VHOST_VSOCK_SET_GUEST_CID.
>
> Fix this by returning early when the CID is unchanged, calling
> synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
> and re-inserting into vhost_vsock_hash, and annotating concurrent accesses
> to vsock->guest_cid with WRITE_ONCE()/READ_ONCE().
>
> Found by manual code inspection.
>
> Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
> Cc: stable@vger.kernel.org
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

[Severity: Low]
In 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko"), vhost_vsock instances
were kept on a spinlock-protected list (vhost_vsock_list), and
vhost_vsock_set_cid() only updated vsock->guest_cid under vhost_vsock_lock
without unlinking or relinking any node.  The RCU hashtable
vhost_vsock_hash and the hash_del_rcu()/hash_add_rcu() rehash sequence in
vhost_vsock_set_cid() were introduced later by 834e772c8db0 ("vhost/vsock:
fix use-after-free in network stack callers").

Should the Fixes tag be updated to:

  Fixes: 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179602872770.13688.17108715679213800277@126.com?part=1

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

* Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID
  2026-11-30  8:52 [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID Bilal Hashim
  2026-12-01  8:52 ` sashiko-bot
@ 2026-12-01 14:56 ` Stefano Garzarella
  2026-12-01 16:36   ` [PATCH v2] " Bilal Hashim
  1 sibling, 1 reply; 5+ messages in thread
From: Stefano Garzarella @ 2026-12-01 14:56 UTC (permalink / raw)
  To: Bilal Hashim
  Cc: Stefan Hajnoczi, Michael S. Tsirkin, Jason Wang, Eugenio Perez,
	kvm, virtualization, netdev, linux-kernel

On Mon, Nov 30, 2026 at 04:52:07PM +0800, Bilal Hashim wrote:
>In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
>assigned, hash_del_rcu(&vsock->hash) is immediately followed by
>hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
>waiting for an RCU grace period.
>
>Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
>its next pointer while concurrent RCU readers in vhost_vsock_get()
>(called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
>vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
>This can cause RCU readers to:
>- jump to another hash bucket or terminate early, missing unrelated active
>  vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
>  to reset healthy connections or vhost_transport_send_pkt() to return
>  -ENODEV); or
>- loop indefinitely inside an RCU read-side critical section (RCU stall /
>  soft lockup) when two instances in the same bucket are repeatedly
>  rehashed via VHOST_VSOCK_SET_GUEST_CID.
>
>Fix this by returning early when the CID is unchanged, calling
>synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
>and re-inserting into vhost_vsock_hash, and annotating concurrent accesses
>to vsock->guest_cid with WRITE_ONCE()/READ_ONCE().
>
>Found by manual code inspection.

mmm, really, it seems related to https://lore.kernel.org/all/sashiko-outbox-222286@kernel.org/
no?

Also why mixing READ_ONCE/WRITE_ONCE in this patch?

FYI that is fixed by https://lore.kernel.org/all/20261129050514.1278990-1-phind.uet@gmail.com/

Please, sync with Nguyen about how to handle both patches. The best will 
be to have a single series, or just rebase this on top of the other 
patch that I guess is going to be merged.

Stefano

>
>Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
>Cc: stable@vger.kernel.org
>Signed-off-by: Bilal Hashim <AeeSec@126.com>
>---
> drivers/vhost/vsock.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
>--- a/drivers/vhost/vsock.c
>+++ b/drivers/vhost/vsock.c
>@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_g
>
> 	hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid,
> 				   lockdep_is_held(&vhost_vsock_mutex)) {
>-		u32 other_cid = vsock->guest_cid;
>+		u32 other_cid = READ_ONCE(vsock->guest_cid);
>
> 		/* Skip instances that have no CID yet */
> 		if (other_cid == 0)
>@@ -572,7 +572,7 @@ static void vhost_vsock_handle_tx_kick(s
> 		hdr = virtio_vsock_hdr(skb);
>
> 		/* Only accept correctly addressed packets */
>-		if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid &&
>+		if (le64_to_cpu(hdr->src_cid) == READ_ONCE(vsock->guest_cid) &&
> 		    le64_to_cpu(hdr->dst_cid) ==
> 		    vhost_transport_get_local_cid())
> 			virtio_transport_recv_pkt(&vhost_transport, skb,
>@@ -856,10 +856,17 @@ static int vhost_vsock_set_cid(struct vh
> 		return -EADDRINUSE;
> 	}
>
>-	if (vsock->guest_cid)
>+	if (vsock->guest_cid == guest_cid) {
>+		mutex_unlock(&vhost_vsock_mutex);
>+		return 0;
>+	}
>+
>+	if (vsock->guest_cid) {
> 		hash_del_rcu(&vsock->hash);
>+		synchronize_rcu();
>+	}
>
>-	vsock->guest_cid = guest_cid;
>+	WRITE_ONCE(vsock->guest_cid, guest_cid);
> 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
> 	mutex_unlock(&vhost_vsock_mutex);
>
>
>

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

* [PATCH v2] vhost/vsock: wait for RCU grace period before rehashing CID
  2026-12-01 14:56 ` Stefano Garzarella
@ 2026-12-01 16:36   ` Bilal Hashim
  2026-12-01 16:58     ` Michael S. Tsirkin
  0 siblings, 1 reply; 5+ messages in thread
From: Bilal Hashim @ 2026-12-01 16:36 UTC (permalink / raw)
  To: Stefano Garzarella, Stefan Hajnoczi, Michael S. Tsirkin,
	Jason Wang, Eugenio Perez
  Cc: phind.uet, kvm, virtualization, netdev, linux-kernel

In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
assigned, hash_del_rcu(&vsock->hash) is immediately followed by
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
waiting for an RCU grace period.

Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
its next pointer while concurrent RCU readers in vhost_vsock_get()
(called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
This can cause RCU readers to:
- jump to another hash bucket or terminate early, missing unrelated active
  vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
  to reset healthy connections or vhost_transport_send_pkt() to return
  -ENODEV); or
- loop indefinitely inside an RCU read-side critical section (RCU stall /
  soft lockup) when two instances in the same bucket are repeatedly
  rehashed via VHOST_VSOCK_SET_GUEST_CID.

Fix this by returning early when the CID is unchanged and calling
synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
and re-inserting into vhost_vsock_hash.

Found by manual code inspection.

Fixes: 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
Changes in v2:
- Drop READ_ONCE()/WRITE_ONCE() changes on vsock->guest_cid, which are
  already addressed separately by Nguyen's patch (thanks to Stefano
  Garzarella for pointing this out).
- Update Fixes: tag to 834e772c8db0 ("vhost/vsock: fix use-after-free in
  network stack callers"), which introduced vhost_vsock_hash.
---
 drivers/vhost/vsock.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 88a3103..622b9cc 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 		return -EADDRINUSE;
 	}
 
-	if (vsock->guest_cid)
+	if (vsock->guest_cid == guest_cid) {
+		mutex_unlock(&vhost_vsock_mutex);
+		return 0;
+	}
+
+	if (vsock->guest_cid) {
 		hash_del_rcu(&vsock->hash);
+		synchronize_rcu();
+	}
 
 	vsock->guest_cid = guest_cid;
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);


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

* Re: [PATCH v2] vhost/vsock: wait for RCU grace period before rehashing CID
  2026-12-01 16:36   ` [PATCH v2] " Bilal Hashim
@ 2026-12-01 16:58     ` Michael S. Tsirkin
  0 siblings, 0 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-12-01 16:58 UTC (permalink / raw)
  To: Bilal Hashim
  Cc: Stefano Garzarella, Stefan Hajnoczi, Jason Wang, Eugenio Perez,
	phind.uet, kvm, virtualization, netdev, linux-kernel

On Wed, Dec 02, 2026 at 12:36:28AM +0800, Bilal Hashim wrote:
> In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
> assigned, hash_del_rcu(&vsock->hash) is immediately followed by
> hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
> waiting for an RCU grace period.
> 
> Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
> its next pointer while concurrent RCU readers in vhost_vsock_get()
> (called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
> vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
> This can cause RCU readers to:
> - jump to another hash bucket or terminate early, missing unrelated active
>   vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
>   to reset healthy connections or vhost_transport_send_pkt() to return
>   -ENODEV); or
> - loop indefinitely inside an RCU read-side critical section (RCU stall /
>   soft lockup) when two instances in the same bucket are repeatedly
>   rehashed via VHOST_VSOCK_SET_GUEST_CID.
> 
> Fix this by returning early when the CID is unchanged and calling
> synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
> and re-inserting into vhost_vsock_hash.
> 
> Found by manual code inspection.
> 
> Fixes: 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

Some process comments:
- pls include net/net-next [PATCH ...] to indicate the tree this is
  against
- send new versions as a new thread not as reply to the old one


> ---
> Changes in v2:
> - Drop READ_ONCE()/WRITE_ONCE() changes on vsock->guest_cid, which are
>   already addressed separately by Nguyen's patch (thanks to Stefano
>   Garzarella for pointing this out).
> - Update Fixes: tag to 834e772c8db0 ("vhost/vsock: fix use-after-free in
>   network stack callers"), which introduced vhost_vsock_hash.
> ---
>  drivers/vhost/vsock.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 88a3103..622b9cc 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c
> @@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
>  		return -EADDRINUSE;
>  	}
>  
> -	if (vsock->guest_cid)
> +	if (vsock->guest_cid == guest_cid) {
> +		mutex_unlock(&vhost_vsock_mutex);
> +		return 0;
> +	}
> +
> +	if (vsock->guest_cid) {
>  		hash_del_rcu(&vsock->hash);
> +		synchronize_rcu();
> +	}
>  
>  	vsock->guest_cid = guest_cid;
>  	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);

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

end of thread, other threads:[~2026-12-01 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  8:52 [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID Bilal Hashim
2026-12-01  8:52 ` sashiko-bot
2026-12-01 14:56 ` Stefano Garzarella
2026-12-01 16:36   ` [PATCH v2] " Bilal Hashim
2026-12-01 16:58     ` 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