hw/virtio/virtio-crypto: Drain in-flight operations before unrealizing (CVE-2026-50625)
PATCH 1/1 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
---
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(+)
include/system/cryptodev.h +9 -0
@@ -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
backends/cryptodev.c +19 -0
@@ -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)
hw/virtio/virtio-crypto.c +2 -0
@@ -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];
Signature · 1 line
2.56.1