Skip to contentSkip to conversation

virtio: avoid packed vring virtio_queue_empty() infinite loops (CVE-2026-16457)

PATCH In review Stefan Hajnoczi 3 participants 4 messages Jul 21

lore
Share & tools
lore
#0 lore raw

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
index f4d86a36553..3795e3c8c68 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
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/3968

---
  hw/virtio/virtio.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/virtio/virtio.c +0 -0
index f4d86a36553..3795e3c8c68 100644

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 -0
      if (unlikely(!vq->vring.desc)) {
          return 1;
      }
#2 lore raw
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/3968

---
  hw/virtio/virtio.c | 4 ++++

  1 file changed, 4 insertions(+)
hw/virtio/virtio.c +0 -0
index f4d86a36553..3795e3c8c68 100644

hw/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

But why not run this check in virtio_queue_empty() entry?

To be clear:

-- >8 --

hw/virtio/virtio.c +3 -0
index f4d86a36553..019c9c9e293 100644
@@ -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.

#3 lore raw
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/3968

---
  hw/virtio/virtio.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/virtio/virtio.c +0 -0
index f4d86a36553..3795e3c8c68 100644

hw/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

But why not run this check in virtio_queue_empty() entry?

To be clear:

-- >8 --

hw/virtio/virtio.c +0 -0
index f4d86a36553..019c9c9e293 100644

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 -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.