Re: [PATCH v2] vhost/vsock: wait for RCU grace period before rehashing CID
From: Michael S. Tsirkin
Date: Tue Dec 01 2026 - 11:58:58 EST
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@xxxxxxxxxxxxxxx
> Signed-off-by: Bilal Hashim <AeeSec@xxxxxxx>
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);