From: Stjepan Poljak <stjepan.poljak@gmail.com>
To: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowangio@gmail.com>
Cc: "Eugenio Pérez" <eperezma@redhat.com>,
"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Jiri Pirko" <jiri@resnulli.us>,
"Paolo Abeni" <pabeni@redhat.com>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
virtualization@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] virtio: Remove debugfs entries when device_add() fails
Date: Mon, 30 Nov 2026 20:20:27 +0100 [thread overview]
Message-ID: <20261130192040.1931044-1-stjepan.poljak@gmail.com> (raw)
register_virtio_device() creates the device's debugfs directory before
calling device_add(), but the device_add() error path does not remove
it. The files in that directory keep the virtio device as their private
data, and the caller is required to drop its reference with put_device()
on failure, which frees the device. Reading any of the leftover files
afterwards dereferences freed memory.
The device index is also returned to the IDA, so the next device to
reuse it fails to create its "virtioN" debugfs directory because the
stale one still exists.
Commit 3f9a0fceb730 ("virtio: fix use-after-free in
unregister_virtio_device()") fixed the same kind of lifetime problem on
the unregister side. Remove the debugfs entries on the device_add()
failure path as well, before the caller can free the device.
Reproduced on x86_64 with CONFIG_KASAN and CONFIG_VIRTIO_DEBUG by
rebinding a virtio-mmio balloon device with fail-nth slab fault
injection: once an allocation inside device_add() fails, the stale
debugfs directory remains, and reading its device_features file
triggers a slab-use-after-free in vm_get_features(). With this patch
applied, no directory is left behind and no report occurs.
Fixes: 96a8326d69ff ("virtio: add debugfs infrastructure to allow to debug virtio features")
Cc: stable@vger.kernel.org
Signed-off-by: Stjepan Poljak <stjepan.poljak@gmail.com>
---
drivers/virtio/virtio.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index c8be1a3f479d..22cc2d71c5a3 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -580,11 +580,12 @@ int register_virtio_device(struct virtio_device *dev)
*/
err = device_add(&dev->dev);
if (err)
- goto out_of_node_put;
+ goto out_debug_exit;
return 0;
-out_of_node_put:
+out_debug_exit:
+ virtio_debug_device_exit(dev);
of_node_put(dev->dev.of_node);
out_ida_remove:
ida_free(&virtio_index_ida, dev->index);
base-commit: 44293796526f4343639f87b2acc53811c78b5d54
--
2.53.0
reply other threads:[~2026-11-30 19:20 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261130192040.1931044-1-stjepan.poljak@gmail.com \
--to=stjepan.poljak@gmail.com \
--cc=eperezma@redhat.com \
--cc=jasowangio@gmail.com \
--cc=jiri@resnulli.us \
--cc=kmehltretter@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=pabeni@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
/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
all inboxes | Powered by JetHome®