From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796360642; b=BoW8sCJu0mkH+iSZlUQ19380zi2+ihjOAOjKAn2mTk5CtDjtmzfOn8v13pFzXCTQKS5u URfi6bDQrUHNIcNFK/myrw95k22y9gV6khDsAUgfvIo8qH7aG0h1zYwqCCCfOD/rDaBMB gAZyBjdOkrHDkmoVfpIryZkW584DUlbGd6uMPDr5lBtRbWeVpp3uJihN+6H2K85o687Er hvJ0QZvsbuu2riikMIoxpjaah89YAfGdlZzA5g143VTzK/NmwIXrsoCBVgFJO4KcQry3h 2K6z/JRQtdXjDZCVBsPue5//7RqvPLkEYIh93CwquP9BWMVYFOG0XRWswibNae2aKow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796360642; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=6ZDwPmmGhH/tYhD6lGCra2X/dzCWOEkUonA26u/9r0M=; b=D2KApnuZ+onh/9HJEK1rH7JwpxqAYcflwR4K/18ve3LuExv5SIrT+jVzRXl3JXLKfUHs RoaHlR/Qnyj6oUefBd9AGpTqcrZZwQfwIAX04PSmocrCCsbO+Yrf61nJnvHYTxSmHuMST vH+8q4TDRPZ+6TY24mFsO2jCzUa+OkZIF3ESIMpgzMnHClESpxiTg5wWMh6x+VCAWeh7q UFRyvdUAL9XCje89IBuovd3q7QJ+JzuJ8dWRC724Xu1UR6iCfgwMY0FxSXIBzjKVRLo7K lWfgc3LR8hgXv3jG6Wyw55i76BqBYNtP+vdHw+F/TT0W3ScSeR1maqaajt5uG9EMqog== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=khbMG3Hj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 777DB606A339 for ; Fri, 4 Dec 2026 05:04:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=khbMG3Hj Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77D9C1F000CE; Fri, 4 Dec 2026 05:04:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796360642; bh=6ZDwPmmGhH/tYhD6lGCra2X/dzCWOEkUonA26u/9r0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=khbMG3HjncqpNfP7tc74ddHngu9Nz6lA2THDXkEriplsAaSiPJR+SLV6BLTSb3My7 8OVFGRJu9hg5r0Kl3JISE7kJxxmIGCI2lEAUErhdJulc6crs6KdGpcmsPfFtrRjQy9 iC/Io5Tu5Gh1patow3+/Y05SClq+YROLQksQ1ONXPX7uRWCYtnU4kRRf6h5AzjrgZf tKTyXcinLqJqmuGifkGKqobsYDrIH3/px0eErru5Hhsr0bEYPSBubhuuwQ9NuQ2ygV pCBuV17pkIe4ly3EoWVRa0WRi/aHk0JnX1LrRa88BRPag7mg3Dj4VXDzkJGWm6pYgP IEFcyFprCzJfg== From: Sasha Levin To: stable@vger.kernel.org Cc: Miklos Szeredi , Sashiko , Sasha Levin Subject: [PATCH 6.6.y] virtiofs: fix use-after-free of fs->tag on device removal Date: Fri, 4 Dec 2026 00:04:00 -0500 Message-ID: <20261204050242.2708858-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120238-subsystem-carwash-965b@gregkh> References: <2026120238-subsystem-carwash-965b@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Miklos Szeredi [ Upstream commit 6df984631391616dafd9b989a3505d93ca4fd235 ] The virtio_fs object is refcounted via its kobject, so a mount can keep it alive well beyond the lifetime of the virtio device binding. The tag string, however, was allocated with devm_kmalloc() and is therefore freed by devres_release_all() as soon as the device is unbound, i.e. right after virtio_fs_remove() returns (see device_unbind_cleanup() in drivers/base/dd.c). virtio_fs_get_tree() looks up the instance, takes a kobject reference and then drops virtio_fs_mutex before doing the sleepable part of the mount (allocations, fuse_conn_init(), sget_fc()). A concurrent device removal can complete in that window: CPU 0 (mount) CPU 1 (remove) ------------- -------------- virtio_fs_get_tree() virtio_fs_find_instance() // reference taken, mutex dropped virtio_fs_remove() list_del_init(&fs->list); // driver core unbinds device // devres frees fs->tag virtio_fs_fill_super() mutex_lock(&virtio_fs_mutex); if (list_empty(&fs->list)) pr_info(... fs->tag); <-- use-after-free The virtio_fs object itself is still valid here, only its tag has been freed behind its back. Fix the lifetime mismatch at the source: allocate the tag with kmalloc() and free it from virtio_fs_ktype_release(), so that it lives exactly as long as the object embedding it. All probe error paths already drop the final kobject reference, so they are covered. Merely dropping the tag from the error message would paper over this instance while leaving a devres pointer embedded in an object that routinely outlives its device. The other readers of fs->tag are unaffected: tag_show() is fenced off by the kobject_del() in virtio_fs_remove(), the automatic KOBJ_REMOVE uevent is emitted from __kobject_del() while the device is still bound, and both virtio_fs_add_instance() and virtio_fs_find_instance() only walk instances still on virtio_fs_instances. Fixes: a62a8ef9d97d ("virtio-fs: add virtiofs filesystem") Cc: Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-0328b3a1-043b-468e-8425-6a613d91b5ef Signed-off-by: Miklos Szeredi [ sashal: Switched devm_kmalloc() to kmalloc() for the tag and freed it in release_virtio_fs_obj() (kref-based) instead of the kobject release function, plus an added kfree(fs->tag) in the probe error path. ] Signed-off-by: Sasha Levin --- fs/fuse/virtio_fs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c index d78ad4cc4fc94..b29512e330976 100644 --- a/fs/fuse/virtio_fs.c +++ b/fs/fuse/virtio_fs.c @@ -165,6 +165,7 @@ static void release_virtio_fs_obj(struct kref *ref) { struct virtio_fs *vfs = container_of(ref, struct virtio_fs, refcount); + kfree(vfs->tag); kfree(vfs->vqs); kfree(vfs); } @@ -299,7 +300,11 @@ static void virtio_fs_free_devs(struct virtio_fs *fs) } } -/* Read filesystem name from virtio config into fs->tag (must kfree()). */ +/* + * Read filesystem name from virtio config into fs->tag. Freed by + * release_virtio_fs_obj() so that it stays valid for as long as the + * virtio_fs object itself, which may outlive the virtio device. + */ static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) { char tag_buf[sizeof_field(struct virtio_fs_config, tag)]; @@ -315,7 +320,7 @@ static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) end = &tag_buf[sizeof(tag_buf)]; len = end - tag_buf; - fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL); + fs->tag = kmalloc(len + 1, GFP_KERNEL); if (!fs->tag) return -ENOMEM; memcpy(fs->tag, tag_buf, len); @@ -958,6 +963,7 @@ static int virtio_fs_probe(struct virtio_device *vdev) out: vdev->priv = NULL; + kfree(fs->tag); kfree(fs); return ret; } -- 2.53.0