* [PATCH] virtio: Remove debugfs entries when device_add() fails
@ 2026-11-30 19:20 Stjepan Poljak
0 siblings, 0 replies; only message in thread
From: Stjepan Poljak @ 2026-11-30 19:20 UTC (permalink / raw)
To: Michael S. Tsirkin, Jason Wang
Cc: Eugenio Pérez, Xuan Zhuo, Jiri Pirko, Paolo Abeni,
Karl Mehltretter, virtualization, linux-kernel
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 19:20 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 19:20 [PATCH] virtio: Remove debugfs entries when device_add() fails Stjepan Poljak
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®