* [PATCH] virtio: Remove debugfs entries when device_add() fails
@ 2026-11-30 19:20 Stjepan Poljak
2026-11-30 19:24 ` sashiko-bot
0 siblings, 1 reply; 2+ messages 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 related [flat|nested] 2+ messages in thread
* Re: [PATCH] virtio: Remove debugfs entries when device_add() fails
2026-11-30 19:20 [PATCH] virtio: Remove debugfs entries when device_add() fails Stjepan Poljak
@ 2026-11-30 19:24 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 19:24 UTC (permalink / raw)
To: Stjepan Poljak; +Cc: virtualization, Michael S. Tsirkin, Eugenio Perez
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130192040.1931044-1-stjepan.poljak@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 19:24 UTC | newest]
Thread overview: 2+ messages (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
2026-11-30 19:24 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox