mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid
@ 2026-12-03  6:22 Krzysztof Woś
  2026-12-03 10:43 ` Stefano Garzarella
  0 siblings, 1 reply; 3+ messages in thread
From: Krzysztof Woś @ 2026-12-03  6:22 UTC (permalink / raw)
  To: Stefano Garzarella, Stefan Hajnoczi, Michael S. Tsirkin
  Cc: Bobby Eshleman, Jason Wang, Eugenio Pérez, virtualization,
	netdev, kvm, linux-kernel, stable

vhost_vsock_set_cid() (drivers/vhost/vsock.c:832-867) updates a struct
vhost_vsock's guest_cid and re-inserts its embedded struct hlist_node
hash into the global 256-bucket RCU hash table vhost_vsock_hash
(DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8), line 46) by calling
hash_del_rcu(&vsock->hash) followed immediately by
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) with no
intervening synchronize_rcu() grace period.

In addition:

1. No early return when other == vsock: Re-invoking
   VHOST_VSOCK_SET_GUEST_CID with the device's existing CID falls
   through to hash_del_rcu() + hash_add_rcu() on the same bucket without
   a grace period.
2. Unsynchronized plain accesses to vsock->guest_cid:
   vhost_vsock_set_cid() performs a plain store (vsock->guest_cid =
   guest_cid;, line 862) while vhost_vsock_get() (line 81, under
   rcu_read_lock()) and vhost_vsock_handle_tx_kick() (line 575, under
   vq->mutex only) perform plain loads without READ_ONCE()/WRITE_ONCE().
   (By contrast, vsock->ever_started in the same file already uses
   READ_ONCE() at line 311 and WRITE_ONCE() at line 640.)

Overwriting vsock->hash.next via hash_add_rcu() before active RCU
readers have exited their read-side critical sections violates the core
hlist_del_rcu() contract: readers currently positioned on the rehashed
node are silently redirected into the destination bucket (or back to the
head of the same bucket). As a result, concurrent lockless lookups for
unrelated live VMs sharing the same 8-bit hash bucket can falsely return
NULL—causing dropped packets (-ENODEV), missed cancel_pkt purges,
erroneous ECONNRESET teardown of healthy VMs' sockets in
vhost_vsock_reset_orphans(), or repeated/looping traversal within an
rcu_read_lock() critical section.

Fix summary:

- Return 0 immediately when other == vsock (eliminates redundant
  same-CID rehash).
- Serialize per-device CID updates with vsock->dev.mutex.
- When reassigning an already-hashed vsock (vsock->guest_cid != 0):
  unlink via hash_del_rcu(), clear vsock->guest_cid to 0 (WRITE_ONCE),
  release vhost_vsock_mutex, call synchronize_rcu(), re-check
  vsock_find_cid(guest_cid) and (under vhost_vsock_mutex)
  vhost_vsock_get(guest_cid, vsock->net) before re-inserting. Clearing
  vsock->guest_cid to 0 while unlinked ensures that if the re-check
  loses a race (-EADDRINUSE), vhost_vsock_dev_release() will not
  double-delete &vsock->hash.
- Use READ_ONCE()/WRITE_ONCE() for vsock->guest_cid on lockless
  read/write paths.

Fixes: 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")
Cc: stable@vger.kernel.org
Signed-off-by: Krzysztof Woś <krzysztof.wos@gmail.com>
---
Base-commit: d06ce6f1eb5e (torvalds/linux)

Locking notes:

Two locking invariants matter when inserting synchronize_rcu() into
vhost_vsock_set_cid():

1. Do NOT hold global vhost_vsock_mutex across synchronize_rcu():
   Holding a global mutex across an RCU grace period stalls all
   concurrent vhost_vsock_set_cid() and vhost_vsock_dev_release() calls
   system-wide and is poor kernel practice.
2. **Serialize concurrent VHOST_VSOCK_SET_GUEST_CID calls on the same
   vsock fd**: vhost_vsock_dev_ioctl() is an unlocked_ioctl and
   currently does not hold vsock->dev.mutex when calling
   vhost_vsock_set_cid(). If vhost_vsock_mutex is dropped around
   synchronize_rcu(), two threads invoking SET_GUEST_CID concurrently on
   the same fd would race on vsock->hash (risking double hash_del_rcu()
   or concurrent hash_add_rcu()). Taking vsock->dev.mutex as the outer
   per-device lock in vhost_vsock_set_cid() prevents same-fd races and
   establishes a clean lock hierarchy (vsock->dev.mutex ->
   vhost_vsock_mutex), as vhost_vsock_dev_release() releases
   vhost_vsock_mutex before calling vhost_vsock_stop() (lines 804 vs.
   819) and no other site nests the two locks.

Note on vsock_find_cid() re-check: Calling vsock_find_cid(guest_cid)
after synchronize_rcu() and before re-acquiring vhost_vsock_mutex
preserves the exact existing lock ordering (never calling
vsock_find_cid() while holding vhost_vsock_mutex).

Tradeoff of Option A: If VM A attempts to change its CID from C1 to C2
and VM B claims C2 during the synchronize_rcu() window, VM A's ioctl
returns -EADDRINUSE and VM A is left unhashed (guest_cid == 0) rather
than retaining C1. Because changing a live VM's CID concurrently with
another VM racing for the same CID is already an exceptional error path
(and existing callers do not expect live sockets on C1 to survive a CID
change), leaving guest_cid == 0 is safe and prevents double-unlink on
release.

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 88a310390..e8000c72c 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)
 
 	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(struct vhost_work *work)
 		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,
@@ -832,6 +832,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 {
 	struct vhost_vsock *other;
+	int ret = 0;
 
 	/* Refuse reserved CIDs */
 	if (guest_cid <= VMADDR_CID_HOST ||
@@ -842,28 +843,53 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	if (guest_cid > U32_MAX)
 		return -EINVAL;
 
+	mutex_lock(&vsock->dev.mutex);
+
 	/* Refuse if CID is assigned to the guest->host transport (i.e. nested
 	 * VM), to make the loopback work.
 	 */
-	if (vsock_find_cid(guest_cid))
-		return -EADDRINUSE;
+	if (vsock_find_cid(guest_cid)) {
+		ret = -EADDRINUSE;
+		goto out_dev;
+	}
 
 	/* Refuse if CID is already in use */
 	mutex_lock(&vhost_vsock_mutex);
 	other = vhost_vsock_get(guest_cid, vsock->net);
-	if (other && other != vsock) {
-		mutex_unlock(&vhost_vsock_mutex);
-		return -EADDRINUSE;
+	if (other) {
+		if (other != vsock)
+			ret = -EADDRINUSE;
+		goto out_hash;
 	}
 
-	if (vsock->guest_cid)
+	if (vsock->guest_cid) {
 		hash_del_rcu(&vsock->hash);
+		WRITE_ONCE(vsock->guest_cid, 0);
+		mutex_unlock(&vhost_vsock_mutex);
+
+		/* Wait for concurrent RCU readers before reusing vsock->hash */
+		synchronize_rcu();
 
-	vsock->guest_cid = guest_cid;
+		if (vsock_find_cid(guest_cid)) {
+			ret = -EADDRINUSE;
+			goto out_dev;
+		}
+
+		mutex_lock(&vhost_vsock_mutex);
+		if (vhost_vsock_get(guest_cid, vsock->net)) {
+			ret = -EADDRINUSE;
+			goto out_hash;
+		}
+	}
+
+	WRITE_ONCE(vsock->guest_cid, guest_cid);
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
+out_hash:
 	mutex_unlock(&vhost_vsock_mutex);
+out_dev:
+	mutex_unlock(&vsock->dev.mutex);
 
-	return 0;
+	return ret;
 }
 
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)

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

* Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid
  2026-12-03  6:22 [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid Krzysztof Woś
@ 2026-12-03 10:43 ` Stefano Garzarella
  2026-12-03 12:13   ` Krzysztof Woś
  0 siblings, 1 reply; 3+ messages in thread
From: Stefano Garzarella @ 2026-12-03 10:43 UTC (permalink / raw)
  To: Krzysztof Woś
  Cc: Stefan Hajnoczi, Michael S. Tsirkin, Bobby Eshleman, Jason Wang,
	Eugenio Pérez, virtualization, netdev, kvm, linux-kernel,
	stable

On Thu, Dec 03, 2026 at 06:22:39AM +0000, Krzysztof Woś wrote:
>vhost_vsock_set_cid() (drivers/vhost/vsock.c:832-867) updates a struct
>vhost_vsock's guest_cid and re-inserts its embedded struct hlist_node
>hash into the global 256-bucket RCU hash table vhost_vsock_hash
>(DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8), line 46) by calling
>hash_del_rcu(&vsock->hash) followed immediately by
>hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) with no
>intervening synchronize_rcu() grace period.
>
>In addition:
>
>1. No early return when other == vsock: Re-invoking
>   VHOST_VSOCK_SET_GUEST_CID with the device's existing CID falls
>   through to hash_del_rcu() + hash_add_rcu() on the same bucket without
>   a grace period.
>2. Unsynchronized plain accesses to vsock->guest_cid:
>   vhost_vsock_set_cid() performs a plain store (vsock->guest_cid =
>   guest_cid;, line 862) while vhost_vsock_get() (line 81, under
>   rcu_read_lock()) and vhost_vsock_handle_tx_kick() (line 575, under
>   vq->mutex only) perform plain loads without READ_ONCE()/WRITE_ONCE().
>   (By contrast, vsock->ever_started in the same file already uses
>   READ_ONCE() at line 311 and WRITE_ONCE() at line 640.)
>
>Overwriting vsock->hash.next via hash_add_rcu() before active RCU
>readers have exited their read-side critical sections violates the core
>hlist_del_rcu() contract: readers currently positioned on the rehashed
>node are silently redirected into the destination bucket (or back to the
>head of the same bucket). As a result, concurrent lockless lookups for
>unrelated live VMs sharing the same 8-bit hash bucket can falsely return
>NULL—causing dropped packets (-ENODEV), missed cancel_pkt purges,
>erroneous ECONNRESET teardown of healthy VMs' sockets in
>vhost_vsock_reset_orphans(), or repeated/looping traversal within an
>rcu_read_lock() critical section.
>
>Fix summary:
>
>- Return 0 immediately when other == vsock (eliminates redundant
>  same-CID rehash).
>- Serialize per-device CID updates with vsock->dev.mutex.
>- When reassigning an already-hashed vsock (vsock->guest_cid != 0):
>  unlink via hash_del_rcu(), clear vsock->guest_cid to 0 (WRITE_ONCE),
>  release vhost_vsock_mutex, call synchronize_rcu(), re-check
>  vsock_find_cid(guest_cid) and (under vhost_vsock_mutex)
>  vhost_vsock_get(guest_cid, vsock->net) before re-inserting. Clearing
>  vsock->guest_cid to 0 while unlinked ensures that if the re-check
>  loses a race (-EADDRINUSE), vhost_vsock_dev_release() will not
>  double-delete &vsock->hash.
>- Use READ_ONCE()/WRITE_ONCE() for vsock->guest_cid on lockless
>  read/write paths.

1 commit for all of those fixes?

BTW it seems your LLMs are founding the same issue in the last weeks, 
so, please, before posting a patch, can you check if there are patches 
fixing the same issue?

https://lore.kernel.org/netdev/20261129050514.1278990-1-phind.uet@gmail.com/
https://lore.kernel.org/netdev/179602872770.13688.17108715679213800277@126.com/

Please test those patches already posted, and comment on them if you 
think they are not complete.

Stefano

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

* Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid
  2026-12-03 10:43 ` Stefano Garzarella
@ 2026-12-03 12:13   ` Krzysztof Woś
  0 siblings, 0 replies; 3+ messages in thread
From: Krzysztof Woś @ 2026-12-03 12:13 UTC (permalink / raw)
  To: Stefano Garzarella
  Cc: Stefan Hajnoczi, Michael S. Tsirkin, Bobby Eshleman, Jason Wang,
	Eugenio Pérez, virtualization, netdev, kvm, linux-kernel,
	stable

On Thu, Dec 03, 2026 at 11:43:29AM +0100, Stefano Garzarella wrote:
> 1 commit for all of those fixes?
>
> BTW it seems your LLMs are founding the same issue in the last weeks,
> so, please, before posting a patch, can you check if there are patches
> fixing the same issue?
>
> Please test those patches already posted, and comment on them if you
> think they are not complete.

Hi Stefano,

My sincere apologies for the duplicate submission and for bundling
multiple changes into a single commit. This is one of my first kernel
submissions: I checked torvalds/linux HEAD (d06ce6f1eb5e) without
checking net-next and lore.kernel.org/netdev first, and I should also
have split distinct changes into separate commits. Going forward I will
always check net-next and lore/Patchwork before posting.

Please drop/ignore this patch in Patchwork in favor of Nguyen Dinh Phi's
merged net-next commit c68d8b4cb2e7 ("vhost/vsock: annotate lockless
accesses to guest_cid") and Bilal Hashim's v2 thread
(179614298864.2872.3567081270371906851@126.com).

Looking through Bilal's v2 and Michael's hlist_nulls follow-up there,
two small notes in case they help that discussion:

1. On Michael's question whether synchronize_rcu() runs on every VM
   boot: on initial VM boot vsock->guest_cid is 0 (kzalloc'd in
   vhost_vsock_dev_open()), so synchronize_rcu() is skipped and only
   triggers if user space calls VHOST_VSOCK_SET_GUEST_CID again to
   reassign an already-configured fd. Still, holding global
   vhost_vsock_mutex across synchronize_rcu() blocks unrelated VMs'
   vhost_vsock_dev_release() and SET_GUEST_CID calls during that grace
   period, and leaving vsock->guest_cid non-zero across synchronize_rcu()
   lets vhost_vsock_handle_tx_kick() keep accepting TX packets with the
   unlinked CID until guest_cid is updated.

2. On Michael's hlist_nulls sketch: get_nulls_value(node) != bucket
   catches cross-bucket moves at end-of-chain, without needing
   synchronize_rcu(), if other == vsock returns early. One subtlety is
   same-bucket CID reassignment (e.g. CID 3 -> 259, both hashing to the
   same 8-bit bucket): if two instances in the same bucket are
   repeatedly re-added at head via hlist_nulls_add_head_rcu() without a
   grace period, a concurrent RCU reader positioned on them can bounce
   between the two nodes without reaching the nulls marker. Also,
   hlist_nulls_add_head_rcu() updates n->next via WRITE_ONCE() before
   publishing head->first, so rehashing an existing live node (unlike a
   freshly allocated SLAB_TYPESAFE_BY_RCU node) needs smp_wmb() between
   updating guest_cid and overwriting n->next.

Thanks, and sorry again for the noise,
Krzysztof Woś

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

end of thread, other threads:[~2026-12-03 13:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  6:22 [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid Krzysztof Woś
2026-12-03 10:43 ` Stefano Garzarella
2026-12-03 12:13   ` Krzysztof Woś

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®