[PATCH] hw/virtio/virtio-crypto: Drain in-flight operations before unrealizing (CVE-2026-50625)

Thomas Huth posted 1 patch 4 days, 15 hours ago
include/system/cryptodev.h |  9 +++++++++
backends/cryptodev.c       | 19 +++++++++++++++++++
hw/virtio/virtio-crypto.c  |  2 ++
3 files changed, 30 insertions(+)
[PATCH] hw/virtio/virtio-crypto: Drain in-flight operations before unrealizing (CVE-2026-50625)
Posted by Thomas Huth 4 days, 15 hours ago
virtio_crypto_device_unrealize() does not drain or cancel requests that
are sitting in the cryptodev backend's throttle delay queue. So when
hot-unplugging the device, the throttle timer callback can still run and
complete the queued operation later through the virtio_crypto_req_complete()
callback, which dereferences the already-freed VirtIOCrypto device and its
deleted virtqueue, so we end up with a host use-after-free bug here.

Fix it by draining the queue first before deleting the virtqueue and the
device.

CVE: CVE-2026-50625
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3918
Reported-by: Jia Jia <physicalmtea@gmail.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 This problem now has been reported multiple time already, i.e. even
 if the virtio-crypto device is marked as deprecated, I think we
 really should fix this to avoid that we even get more tickets about
 this:
 - https://gitlab.com/qemu-project/qemu/-/work_items/4071
 - https://gitlab.com/qemu-project/qemu/-/work_items/4158
 - https://gitlab.com/qemu-project/qemu/-/work_items/4904
 Disclaimer: The idea of the patch is based on the suggestions in the
 (AI-generated) tickets, and I also asked AI to double-check it (and it
 came up with the idea of using throttle_timers_detach_aio_context() to
 make sure that there are no interfering timers around), so AI has been
 involved in the creation of this patch, but I assembled the patch
 manually and I double-checked that it fixes the reproducer from ticket
 3918 indeed.

 include/system/cryptodev.h |  9 +++++++++
 backends/cryptodev.c       | 19 +++++++++++++++++++
 hw/virtio/virtio-crypto.c  |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/include/system/cryptodev.h b/include/system/cryptodev.h
index b20822df0dc..a160a6781a7 100644
--- a/include/system/cryptodev.h
+++ b/include/system/cryptodev.h
@@ -389,6 +389,15 @@ int cryptodev_backend_close_session(
            CryptoDevCompletionFunc cb,
            void *opaque);
 
+/**
+ * cryptodev_backend_drain_requests:
+ * @backend: the cryptodev backend object
+ *
+ * Drain crypto operations that might still be in flight on a throttled
+ * backend.
+ */
+void cryptodev_backend_drain_requests(CryptoDevBackend *backend);
+
 /**
  * cryptodev_backend_crypto_operation:
  * @backend: the cryptodev backend object
diff --git a/backends/cryptodev.c b/backends/cryptodev.c
index e8f2b18f201..1f035d0cd71 100644
--- a/backends/cryptodev.c
+++ b/backends/cryptodev.c
@@ -262,6 +262,25 @@ static void cryptodev_backend_throttle_timer_cb(void *opaque)
     }
 }
 
+void cryptodev_backend_drain_requests(CryptoDevBackend *backend)
+{
+    CryptoDevBackendOpInfo *opinfo, *tmp;
+
+    /* Cancels pending timer, prevent it from firing during or after drain */
+    if (throttle_enabled(&backend->tc)) {
+        throttle_timers_detach_aio_context(&backend->tt);
+    }
+
+    QTAILQ_FOREACH_SAFE(opinfo, &backend->opinfos, next, tmp) {
+        QTAILQ_REMOVE(&backend->opinfos, opinfo, next);
+        cryptodev_backend_operation(backend, opinfo);
+    }
+
+    if (throttle_enabled(&backend->tc)) {
+        throttle_timers_attach_aio_context(&backend->tt, qemu_get_aio_context());
+    }
+}
+
 int cryptodev_backend_crypto_operation(
                  CryptoDevBackend *backend,
                  CryptoDevBackendOpInfo *op_info)
diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index 85c7279aae2..6d053e7d936 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -1116,6 +1116,8 @@ static void virtio_crypto_device_unrealize(DeviceState *dev)
     VirtIOCryptoQueue *q;
     int i;
 
+    cryptodev_backend_drain_requests(vcrypto->cryptodev);
+
     for (i = 0; i < vcrypto->max_queues; i++) {
         virtio_delete_queue(vcrypto->vqs[i].dataq);
         q = &vcrypto->vqs[i];
-- 
2.56.1