mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Krzysztof Woś" <krzysztof.wos@gmail.com>
To: Stefano Garzarella <sgarzare@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Michael S. Tsirkin <mst@redhat.com>
Cc: "Bobby Eshleman" <bobbyeshleman@meta.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	virtualization@lists.linux.dev, netdev@vger.kernel.org,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid
Date: Thu, 03 Dec 2026 06:22:39 +0000	[thread overview]
Message-ID: <179627895906.335073.17960904565008606745@gmail.com> (raw)

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)

             reply	other threads:[~2026-12-03  6:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  6:22 Krzysztof Woś [this message]
2026-12-03 10:43 ` Stefano Garzarella
2026-12-03 12:13   ` Krzysztof Woś

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179627895906.335073.17960904565008606745@gmail.com \
    --to=krzysztof.wos@gmail.com \
    --cc=bobbyeshleman@meta.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=sgarzare@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®