virtio: avoid packed vring virtio_queue_empty() infinite loops (CVE-2026-16457)
PATCH 1/1 virtio: avoid packed vring virtio_queue_empty() infinite loops (CVE-2026-16457)
Virtqueue handler functions in device emulation code often look something like this:
while (!virtio_queue_empty(vq)) {
...pop and process virtqueue element...
}virtio-blk, virtio-scsi, virtio-crypto, and vhost-shadow-virtqueue use this pattern.
The device may break (i.e. hit an error that requires device reset) during the loop. virtio_queue_empty() returns 1 for broken split vrings but not for broken packed vrings, leading to an infinite loop.
Adjust the packed vring behavior to match split vrings and avoid infinite loops.
Fixes: CVE-2026-16457
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3968
--- hw/virtio/virtio.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/virtio/virtio.c +4 -0
@@ -763,6 +763,10 @@ static int virtio_queue_packed_empty_rcu(VirtQueue *vq)struct VRingPackedDesc desc;VRingMemoryRegionCaches *cache; +if (virtio_device_disabled (vq->vdev)) { +return 1 ; + } +if (unlikely (!vq->vring.desc)) {return 1 ; }
Signature · 1 line
2.55.0
Quoted 30 lines · Stefan Hajnoczi
Virtqueue handler functions in device emulation code often look something like this:
while (!virtio_queue_empty(vq)) { ...pop and process virtqueue element... }virtio-blk, virtio-scsi, virtio-crypto, and vhost-shadow-virtqueue use this pattern.
The device may break (i.e. hit an error that requires device reset) during the loop.
virtio_queue_empty()returns 1 for broken split vrings but not for broken packed vrings, leading to an infinite loop.Adjust the packed vring behavior to match split vrings and avoid infinite loops.
Fixes: CVE-2026-16457
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3968Reported-by Anatol Belski <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/virtio/virtio.c | 4 ++++ 1 file changed, 4 insertions(+)hw/virtio/virtio.c +0
-0
hw/virtio/virtio.c static int virtio_queue_packed_empty_rcu(VirtQueue *vq) · line 768
Quoted 6 lines
hw/virtio/virtio.c +3
-0@@ -763,6 +763,10 @@ static int virtio_queue_packed_empty_rcu(VirtQueue *vq)struct VRingPackedDesc desc;VRingMemoryRegionCaches *cache; +if (virtio_device_disabled (vq->vdev)) { +return 1 ; + }
Correct, so:
But why not run this check in virtio_queue_empty() entry?
Quoted 3 lines
hw/virtio/virtio.c +0
-0if (unlikely (!vq->vring.desc)) {return 1 ; }
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 30 lines · Stefan Hajnoczi
Virtqueue handler functions in device emulation code often look something like this:
while (!virtio_queue_empty(vq)) {
...pop and process virtqueue element...
}virtio-blk, virtio-scsi, virtio-crypto, and vhost-shadow-virtqueue use this pattern.
The device may break (i.e. hit an error that requires device reset) during the loop.
virtio_queue_empty()returns 1 for broken split vrings but not for broken packed vrings, leading to an infinite loop.Adjust the packed vring behavior to match split vrings and avoid infinite loops.
Fixes: CVE-2026-16457
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3968Reported-by Anatol Belski <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
hw/virtio/virtio.c| 4 ++++1 file changed, 4 insertions(+)hw/virtio/virtio.c +0
-0hw/virtio/virtio.c static int · line 768
Quoted 6 lines
hw/virtio/virtio.c +3
-0@@ -763,6 +763,10 @@ static int virtio_queue_packed_empty_rcu(VirtQueue *vq) struct VRingPackedDesc desc; VRingMemoryRegionCaches *cache; + if (virtio_device_disabled(vq->vdev)) { + return 1; + }Correct, so:
Quoted 3 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>But why not run this check in
virtio_queue_empty()entry?
To be clear:
-- >8 --
hw/virtio/virtio.c +3 -0
@@ -786,6 +786,9 @@ static int virtio_queue_packed_empty(VirtQueue *vq)int virtio_queue_empty (VirtQueue *vq) { +if (virtio_device_disabled (vq->vdev)) { +return 1 ; + }if (virtio_vdev_has_feature (vq->vdev,VIRTIO_F_RING_PACKED )) {return virtio_queue_packed_empty (vq); }else {
---
Having:
static inline bool virtio_device_disabled(VirtIODevice *vdev)
{
return unlikely(vdev->disabled || vdev->broken);
}that would avoid the RCU_READ_LOCK access.
Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 30 lines · Stefan Hajnoczi
Virtqueue handler functions in device emulation code often look something like this:
while (!virtio_queue_empty(vq)) { ...pop and process virtqueue element... }virtio-blk, virtio-scsi, virtio-crypto, and vhost-shadow-virtqueue use this pattern.
The device may break (i.e. hit an error that requires device reset) during the loop.
virtio_queue_empty()returns 1 for broken split vrings but not for broken packed vrings, leading to an infinite loop.Adjust the packed vring behavior to match split vrings and avoid infinite loops.
Fixes: CVE-2026-16457
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3968Reported-by Anatol Belski <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/virtio/virtio.c | 4 ++++ 1 file changed, 4 insertions(+)hw/virtio/virtio.c +0
-0hw/virtio/virtio.c static int · line 768
Quoted 6 lines
hw/virtio/virtio.c +3
-0@@ -763,6 +763,10 @@ static int virtio_queue_packed_empty_rcu(VirtQueue *vq)struct VRingPackedDesc desc;VRingMemoryRegionCaches *cache; +if (virtio_device_disabled (vq->vdev)) { +return 1 ; + }Correct, so:
Quoted 3 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>But why not run this check in
virtio_queue_empty()entry?To be clear:
-- >8 --
hw/virtio/virtio.c +0
-0
hw/virtio/virtio.c static int virtio_queue_packed_empty(VirtQueue *vq) · line 791
Quoted 6 lines
hw/virtio/virtio.c +3
-0@@ -786,6 +786,9 @@ static int virtio_queue_packed_empty(VirtQueue *vq)int virtio_queue_empty (VirtQueue *vq) { +if (virtio_device_disabled (vq->vdev)) { +return 1 ; + }
The _rcu() version of the functions still needs to check this as well, so I don't think it can be extracted cleanly.
Stefan
Quoted 14 lines
hw/virtio/virtio.c +0
-1if (virtio_vdev_has_feature (vq->vdev,VIRTIO_F_RING_PACKED )) {return virtio_queue_packed_empty (vq); }else { ---Having:
static inline bool
virtio_device_disabled(VirtIODevice *vdev){ return unlikely(vdev->disabled || vdev->broken); }that would avoid the
RCU_READ_LOCKaccess.