Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796279042; b=Py5g30V+9LlNB/SMZBPbVtJEkDf7StUMte1hknP+oV+/n7+kYqh/zPtcxjpj8bTB8Nlh Y/dAB/dUyJVkMi9e3NCJX0cgqNx/3BF9+aeJ8Wx4ci7fJTSbP1LujtvL0xNV5hLX0/1uw RjVZKrXCL+3OvZyv2tvcJgn7BwKnCMLJrh4pmadtwv+ghvdNgBaW4spfa4pkI6n0vaoTU bWoIUnrAQj6C1CXYl/Qr2tREWnZCb5Tzz0SmLF4+kvvnBaCEI6R2QTgSY20hG48xQ7rpx jE6VtvRfr3wLxOndWDDW63AbNYhVON8pThcOwq2Qh1pRw0ClHZgeOzgdDCjFZ8ldwsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796279042; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=ZqrU1pLQSH08cJP1lU2TM6lDnb3xnBb9W07Iwf4v4b0=; b=1FjEKz85KJC8XZ0Mryv36Nx6Nar1Kted2vBR6kdvWXWS4lvdA5lhZhgeaAPEOZolzdAw NIMnuQGm+N9h2w70K8aQ8V/HizdNu9ZNDl1b9iH7J1QYSpSIgWrMhUAj2cTdhm4f5tXET /roX86k+w+tM5TbtvLvu4UFwxd0sOChk6W/YyGqi9+t9/B2uRx98vgtryxpNWATuqoygM atOdmeyrFbdzFxAlaB9dUyVw5XZ47+yYwzsIMkQfd7d07MTDPjk1oPJsk0kC+fnPSzv9K ECRsLLizL+0NmpLyQRlouo8JVRPJmsHIHeIz7nr8ZcY1v9SFVYjqIjai7Osju3rkP7w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rxUyl8At; arc=none smtp.remote-ip=74.125.82.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97A29606A32F for ; Thu, 3 Dec 2026 06:24:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 97A29606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rxUyl8At Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-386ab9ab619so21205303eec.1 for ; Wed, 02 Dec 2026 22:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796279041; x=1796883841; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZqrU1pLQSH08cJP1lU2TM6lDnb3xnBb9W07Iwf4v4b0=; b=rxUyl8AtXlRPLK9LjvDDmWKJABtejv8S4kX5e5sOpHaxgUlkp0gfl5ZfDgli80rTkD JOILshMt+zdsKc7r559umrLvC8CnQMrDvFyg/Pho7YOS0NVsduPZOPMZnjAxul9LKf0F pn6ugzLN3WgXpy+8N/6WAdFF4vmVbemg78Kgoc86CHUJZjPH33q51ziaXwonk5u+iWVS FZEmyhpOiJLgXUnATp9/p9go58ODKjkRGVXvPoEiheWiVybW6Rb2+NlatXsGr7pIbsZX pKZgb1df2zg65VzvjZomwY5tn7sJIy482ujgQc6z4Vaw9FeYLtHIrFHru2E9qWey1Rz8 A1vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796279041; x=1796883841; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ZqrU1pLQSH08cJP1lU2TM6lDnb3xnBb9W07Iwf4v4b0=; b=2oPJEvM0R/ZCxeb9fizdc4ILPaiLP9cdzbephTR1p719S7FnCEwMEDNwBl3u2RalTN iGj+UbBPCFx7DHNVpwkL3qh5Lp54ogXEcIZDhsnBlvsP5cERrzo25ueHMRBcqN2N31d2 TjnSvNrVlGKYQdWXD9R0FriYvIZQOEh+eJXtcimIaK+W/pnGm2FOJ+WlDAB9qA75cbrR ajlUq4y+pUhTjw/RwtJgkycp5N/8gRYOFG6IsH7Y0QtJul+PavggrJViA45sPSfjF7kC wa7hoFpjzlYeHbSg7ko43Go7tmPej+iAdEbzYXcun5nTzegVmlundZrY+s5vpegbhHo0 nx6g== X-Forwarded-Encrypted: i=1; AL+hErFdRgVtcL6oWz1vh+Sa0xjyye6q0t4LAzQDhjONAH1zGJAWqFm4/B3jmjvFuHlIA1HPPhA=@vger.kernel.org X-Gm-Message-State: APuU4Z8Kya5MP8N8DwhEeh4HJXGcRwEBP8PVUWw06rZUP9zUhaImuzKk uSrIdhVA0dy5VGoHNVahgClwAMhIwnJ/qLtz1A9j9Jljzow3SZ3GHdG5 X-Gm-Gg: AZFdTZeUCvFdRTzvorWslhLnrVUFaZkuQbXxzrKVfGa96/4wPlHQJd/cGsTRSzpWIRY beG7QnDFCk/J0y6VGr6wBY+yOw1LLYVZt5OlDA+FEMro2p53xdmWOjayyAgyNJdSxeAeYO14gEv SDbiT4fJKKzJKtNeW2Kpzmd2aOLTIk0VX01ii5jiZYs3uOQNVvi2lx+BKWUw4G2LCzGFzATIGdw Rco2RlWgUatmeXnxM9QrMM7BXzmTslXHZ03P7k5zDrRcH387xo4RXdevXdRzCM4x0zk3rRgg1m8 Sp5qJ3KTPcFMHgDXYK2/sOon6CToDLFhjHRQmTHAfzFrMfdKABIzbBc+BkkfDF43uWZwgR65sjg O1+hzQwN/HqOwXzBwbQTz7qhQvWf9OJt0+u9+ic8x1OEE5AnyykfJjefr3qWT6qFius5wfdWZwT /35Gj6WCca3cht24Uoaw1gA8IQPciq1FEYCjlK9Ys61Y9D0g/SMwFrBN++kvk3hBedzK9RPULDM TnD6L0Qsmsx3Ijg7gWdJfb8ei8KBkqvUxSd/KyAwV5AZfg2Ghs5Uj8CY42ufjz4xQCFSrgqajLu 5MCw3bHSDUG8L2jytXIdtKKropx0O09Kq0Sf0uVXO261xRjfygTctBL0Ko6+86OqZN3Rcp8dFAk xM6dZYDLtGZ7FEQHvmNsOnrX4jNLD6RYy2VpFjvwyqzePMc5RsQ== X-Received: by 2002:a05:7300:ce0f:b0:386:b827:de97 with SMTP id 5a478bee46e88-386b827e2ffmr33863444eec.34.1796279040655; Wed, 02 Dec 2026 22:24:00 -0800 (PST) Received: from [127.0.1.1] (M106072044160.v4.enabler.ne.jp. [106.72.44.160]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385672c6f31sm122877211eec.15.2026.12.02.22.23.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 22:24:00 -0800 (PST) From: Krzysztof =?utf-8?q?Wo=C5=9B?= To: Stefano Garzarella , Stefan Hajnoczi , Michael S. Tsirkin Cc: Bobby Eshleman , Jason Wang , Eugenio =?utf-8?q?P=C3=A9rez?= , 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 Message-ID: <179627895906.335073.17960904565008606745@gmail.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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ś --- 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)