From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796279042; b=26K0GotmUFkbREsO29cMoTuDeHGAK7OtsYVh2GoXiMNnUX3z2vi7eCIH8FN+aEOtB8Px uxpH45+glT9r0ML6visR0k0R7s7nea/1TGzEPylsOcwJWU6OLHXC9v5PQT3Ey+TI0h+Us HXxVQY3t3ZIGB0X/eX6x4L3+Ggjcp9ITvE9p99LM5FmPXgvUHeVRXE17Kps2VfyFvoi2m rA7KwR3MqsjnO0epDObMQuqxwcoJhbiM2IwidRFY5T0XRxj0xOkHlBzEXZcYR/wl2dgd6 JwJA4pWZKndQKcXfAr3Kd159qu25UUBYCvf6D+MaasdNkYK0OdQCWZmet+/sceLAl4A== 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=0Dwkr/YtFrWBhlfyzRrxB6lk6t5jdlvuQMDpfUSS/vB8C1YPirEgJcwOy1vFRsVJszH7 XCHdDq/X6X7B9WpPaiSRRQFCkIcnP4ubkTTxSXX+pX7PlV6VWkSkiJ/he5PDj0Lcta4qM XIneZrOXmpuCrfRpG+Uin9YoX8WkzFSVPJwsz0QGpdvU+mIV8rjqX/tspwPb14+HdvQow Je3bODTnoCMphPy/zvFh4KgQtehQq0frvmldsscNlwP5NSv2gbsBBabmJeAmCAsbxeYTa BpCskIe9a3DZweWh10PJwwPe2sKtgK5xMSTYXJalCF2kCL1NBT44pOJH8jUf+p7LS4g== 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.45 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (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 A7A93606A330 for ; Thu, 3 Dec 2026 06:24:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A7A93606A330 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-dl1-f45.google.com with SMTP id a92af1059eb24-14394530ec6so9971860c88.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=EP5jWIN1UPFMBn/7ihQeJVIIksG4L25oOUyQG+jdmX07opPTzqAj9OISCxExzzIcSV a+vJrNM5K6tgQsNpj26L2X1zhzLcgh0ALdhUnslCTc9f1pA7TyBGR1Sku/zBJlZrtELL MJN5dpGicfRHrXZKlkKXSM4x6DC11GHuh0fo312d/Fh3SIxEHf7GWyKyAfp/2XVhPEfu uNkNIrUd2vennjQMLRqkVrAA7UBMhNVwK6dnvKaLRgqWgT5I/mACr1Ficgmc5ArpPKLb Fz/uByf1k84zAGjLSlBqsw+JRUWKZpmPN0VxWZOC1xw3VNZzX+INmIyiXQ+Z+XQhpPCQ oCTQ== X-Forwarded-Encrypted: i=1; AL+hErFhAzK5knfDhRNQ5WY/uErJ9lxkoSMYSrYVrKMiaCvt5hEWFgrYqMC3jBYuAtRZTXYvHJ9iRW0=@vger.kernel.org X-Gm-Message-State: APuU4Z+7a9fcJ3iikmAcAALLoMQBphANTrPFSC8zhwrKt2kOpdy9381w 5GiDSrhcx3BaL9h1G4HaASJhpwY2lQe2tVAsX5xtEydbHK5ewLJTl0xO X-Gm-Gg: AZFdTZeZac+7d/1a5FaMHoYG7zkIJxEXkvwFhXTYVEqZSNW4TwYVoRj/nIQfi9Rvgx3 6yg/GccgVnzRI79fEeijvvnoL/5fU/QFchWoc3V9/hh5AhuLiozVWZo8ZOI4ZxJUVxLxx+b2PG3 VoHhu9dbejussZM9aNq37l8eJwxldwVQjKBVCZlUebuMCEYdKEV9Z0+tRTeNopDptS5UZo7HUoL 0+yJEbZZ53P9BzbYyIFkqUnQkE0vSRZCW3RhPWxG33eGXSzJBwUX1JkX/Bz/k8chbNOVn8GkyOf zvoPrTfuELmHge92SRYs8/fFD7IYz+Uxlrlcu/hloQiSMFVKoXOLzN7+ZEGG2R5jG1RqUUZEHpj pt5bZgRr3jYEcFizZCqLkyhNBdMlq5eaPajuIIv42F8DyS0DdvCM5+FOZ31Uo2UwIdgitE5bvSO pNro4b/6mIJg5FAukcMwslCNCu5zNSmA5dEIs7VLSUkHVncD9QBOpmrQIT1vPN6bu2jyFCD/cVn E54pcd7iiL69ELUBl2KuwxQDzhvDEb9FReWK8DffbDdIUOLNd56OFq5BZlDqluXvME5c/UIyn1Y wcJgMviVl0V33NeTAhV7wrIC+aZ8Ly2SHgHC8bw9L67GsoLd6qvi1JesE9pM5DJsZ5lJJ+PO40i s1gl5Y5NdLmQTw4G2VhOsZI2XAoI2VHqZzdgNN+fPayATB2aHBQ== 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: netdev@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)