Commit 902c849979 for qemu.org

commit 902c849979ce2be367dc0a643310d6b45b76bcd4
Author: Thomas Huth <thuth@redhat.com>
Date:   Wed Nov 18 13:39:48 2026 +0100

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

    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>
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
    Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
    Message-ID: <20261118123948.221366-1-thuth@redhat.com>

diff --git a/backends/cryptodev.c b/backends/cryptodev.c
index 46059faa26..faf5592bc8 100644
--- a/backends/cryptodev.c
+++ b/backends/cryptodev.c
@@ -263,6 +263,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 85c7279aae..6d053e7d93 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];
diff --git a/include/system/cryptodev.h b/include/system/cryptodev.h
index b20822df0d..a160a6781a 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