Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Krzysztof Woś" <krzysztof.wos@gmail.com>
Cc: virtualization@lists.linux.dev, kvm@vger.kernel.org,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid
Date: Fri, 04 Dec 2026 06:24:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-249206@kernel.org> (raw)
In-Reply-To: <179627895906.335073.17960904565008606745@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] vhost/vsock: vhost_vsock_set_cid() clears existing CID on
  -EADDRINUSE error
- [Medium] vhost/vsock: vhost_vsock_handle_tx_kick() accepts src_cid 0
  when guest_cid is 0
--

commit 67b5df7bb320d8c43ffa983b2feb46d9a787de1a
Author: Krzysztof Woś <krzysztof.wos@gmail.com>

vhost/vsock: wait for RCU grace period before rehashing guest_cid

This commit serializes vhost_vsock_set_cid() with vsock->dev.mutex, waits
for an RCU grace period before rehashing an existing guest_cid, returns
early when setting the same CID, and uses READ_ONCE()/WRITE_ONCE() for
vsock->guest_cid accesses.

> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 88a310390eb0a..e8000c72c3f81 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c

[ ... ]

> @@ -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,

[Severity: Medium]
Can vhost_vsock_handle_tx_kick() accept packets with src_cid 0 when
vsock->guest_cid is cleared to 0?

vhost_vsock_set_cid() now clears vsock->guest_cid to 0 before
synchronize_rcu() while vhost_vsock_handle_tx_kick() can run under
vq->mutex.  During that window, a guest packet with src_cid 0 and host
dst_cid matches 0 == 0 and enters virtio_transport_recv_pkt().

This lets an untrusted guest bypass the fast drop path and reach host
socket lookup, locking, and reset packet allocation.  Loading guest_cid
with READ_ONCE() and checking guest_cid != 0, as vhost_vsock_get() does,
would prevent matching the unassigned sentinel.

[ ... ]

> @@ -842,28 +843,53 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)

[ ... ]

> -	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);

[Severity: Medium]
Should a failed VHOST_VSOCK_SET_GUEST_CID call destroy the device's
previous guest_cid?

Before this patch, an -EADDRINUSE return left the existing CID intact and
hashed.  Now, if another VM claims the target CID during synchronize_rcu(),
the re-check returns -EADDRINUSE after vsock->hash has been unlinked and
vsock->guest_cid cleared to 0.

Host-to-guest sends to the old CID then fail with -ENODEV and
vhost_transport_cancel_pkt() can no longer find the device to purge queued
packets, while existing connections remain stranded without an orphan
reset.

Allocating an RCU-freed hash entry wrapper (hlist_node, rcu_head, vsock,
and immutable guest_cid) before taking vhost_vsock_mutex would let the new
entry be inserted before retiring the old one with kfree_rcu(), avoiding
both synchronize_rcu() and this destructive error path.

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

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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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ś
2026-12-04  6:24 ` sashiko-bot [this message]

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=sashiko-outbox-249206@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=krzysztof.wos@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --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