Skip to contentSkip to conversation

Harden virtio migration load paths against crafted streams

PATCH Superseded Laurent Vivier 3 participants 8 messages Jul 10 – Jul 29

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Harden virtio migration load paths against crafted streams

A crafted migration stream can crash the destination QEMU process through unvalidated fields in the virtio device state: an unbounded allocation in virtio-serial, reachable assertions in the shared virtqueue element deserializer, and assert()/exit(1) calls in virtio-scsi request loading.

These are hardening fixes: the destination QEMU is in a paused pre-start state and the source VM is unaffected by a failed migration.

Patch 1 validates the virtio-serial nr_active_ports count against the configured maximum before allocating the post-load array.

Patch 2 replaces the assertions in qemu_get_virtqueue_element() with a bounds check returning NULL, and updates all callers (virtio-serial, virtio-blk, virtio-scsi, scsi-bus) to handle the failure gracefully.

Patch 3 replaces the remaining assert() and exit(1) calls in virtio_scsi_load_request() with proper error returns.

Tested with migration round-trips for virtio-serial (0 to 511 ports), virtio-blk (1-2 disks), and virtio-scsi (1-2 disks), plus the original PoC reproducers for issues #3801 and #3802.

Laurent Vivier (3):
hw/char/virtio-serial-bus: validate nr_active_ports from migration
stream
hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid
state
hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid
stream

 hw/block/virtio-blk.c       |  4 ++++
 hw/char/virtio-serial-bus.c |  7 +++++++
 hw/scsi/scsi-bus.c          |  4 ++++
 hw/scsi/virtio-scsi.c       | 20 ++++++++++++++++----
 hw/virtio/virtio.c          | 11 ++++-------
 5 files changed, 35 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/3 hw/char/virtio-serial-bus: validate nr_active_ports from migration stream

The migration restore path reads nr_active_ports from the incoming stream and passes it directly to fetch_active_ports_list(), which uses it to size a heap allocation. A crafted migration stream can set this field to a very large value, causing QEMU to attempt a multi-gigabyte allocation and abort.

Fix this by checking nr_active_ports against the configured max_virtserial_ports before calling fetch_active_ports_list().

Fixes 6663a1956eb6 ("virtio-serial-bus: Maintain guest and host port open/close state")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3801

---
 hw/char/virtio-serial-bus.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/char/virtio-serial-bus.c +4 -0
index c1973f0248fc..80f1b308aa53 100644
@@ -804,6 +804,10 @@ static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
 
     qemu_get_be32s(f, &nr_active_ports);
 
+    if (nr_active_ports > max_nr_ports) {
+        return -EINVAL;
+    }
+
     if (nr_active_ports) {
         ret = fetch_active_ports_list(f, s, nr_active_ports);
         if (ret) {
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/3 hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid state

In reply to Laurent Vivier

qemu_get_virtqueue_element() uses assert() to check that the in_num and out_num fields deserialized from the migration stream do not exceed VIRTQUEUE_MAX_SIZE. A crafted migration stream can set these fields to invalid values, hitting the assertion and aborting the destination QEMU process.

Replace the assertions with a bounds check that returns NULL on failure. Update all callers (virtio-serial-bus, virtio-blk, virtio-scsi) to handle the NULL return and fail the migration gracefully.

Fixes 6bdc21c050a2 ("virtio: fix up max size checks")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3802

---
 hw/block/virtio-blk.c       |  4 ++++
 hw/char/virtio-serial-bus.c |  3 +++
 hw/scsi/scsi-bus.c          |  4 ++++
 hw/scsi/virtio-scsi.c       |  4 ++++
 hw/virtio/virtio.c          | 11 ++++-------
 5 files changed, 19 insertions(+), 7 deletions(-)
hw/block/virtio-blk.c +4 -0
index 6b92066aff4c..42c0f2553f18 100644
@@ -1384,6 +1384,10 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
         }
 
         req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
+        if (!req) {
+            return -EINVAL;
+        }
+
         virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
 
         WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
hw/char/virtio-serial-bus.c +3 -0
index 80f1b308aa53..4ed8840b2efe 100644
@@ -763,6 +763,9 @@ static int fetch_active_ports_list(QEMUFile *f,
 
             port->elem =
                 qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
+            if (!port->elem) {
+                return -EINVAL;
+            }
 
             /*
              *  Port was throttled on source machine.  Let's
hw/scsi/scsi-bus.c +4 -0
index dccb2f25b2af..96c4844374c5 100644
@@ -1915,6 +1915,10 @@ static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
         req->retry = (sbyte == 1);
         if (bus->info->load_request) {
             req->hba_private = bus->info->load_request(f, req);
+            if (!req->hba_private) {
+                scsi_req_unref(req);
+                return -EINVAL;
+            }
         }
         if (req->ops->load_request) {
             req->ops->load_request(f, req);
hw/scsi/virtio-scsi.c +4 -0
index 6c7376801190..8dd0b88a30c4 100644
@@ -274,6 +274,10 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     assert(n < vs->conf.num_queues);
     req = qemu_get_virtqueue_element(vdev, f,
                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
+    if (!req) {
+        return NULL;
+    }
+
     virtio_scsi_init_req(s, vs->cmd_vqs[n], req);
 
     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
hw/virtio/virtio.c +4 -7
index f4d86a365530..7bccfdde33e7 100644
@@ -2167,13 +2167,10 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
 
     qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
 
-    /* TODO: teach all callers that this can fail, and return failure instead
-     * of asserting here.
-     * This is just one thing (there are probably more) that must be
-     * fixed before we can allow NDEBUG compilation.
-     */
-    assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
-    assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
+    if (data.in_num > ARRAY_SIZE(data.in_addr) ||
+        data.out_num > ARRAY_SIZE(data.out_addr)) {
+        return NULL;
+    }
 
     elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
     elem->index = data.index;
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid stream

In reply to Laurent Vivier

virtio_scsi_load_request() uses assert() and exit(1) for conditions that can be triggered by a crafted migration stream: an out-of-range queue index, a malformed SCSI request, or a command mode mismatch.

Replace these with proper error returns so the migration fails gracefully instead of aborting the destination QEMU process.

Fixes 5db1764cc1f6 ("virtio-scsi: add migration support")
Fixes d2ad7dd46e72 ("virtio-scsi: add multiqueue capability")
Fixes 36b15c79aa1b ("virtio-scsi: start preparing for any_layout")
---
 hw/scsi/virtio-scsi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/scsi/virtio-scsi.c +12 -4
index 8dd0b88a30c4..b34cda8fd85a 100644
@@ -271,7 +271,10 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     uint32_t n;
 
     qemu_get_be32s(f, &n);
-    assert(n < vs->conf.num_queues);
+    if (n >= vs->conf.num_queues) {
+        return NULL;
+    }
+
     req = qemu_get_virtqueue_element(vdev, f,
                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
     if (!req) {
@@ -283,14 +286,19 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
                               sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
         error_report("invalid SCSI request migration data");
-        exit(1);
+        virtio_scsi_free_req(req);
+        return NULL;
     }
 
     scsi_req_ref(sreq);
     req->sreq = sreq;
-    if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
-        assert(req->sreq->cmd.mode == req->mode);
+    if (req->sreq->cmd.mode != SCSI_XFER_NONE &&
+        req->sreq->cmd.mode != req->mode) {
+        scsi_req_unref(sreq);
+        virtio_scsi_free_req(req);
+        return NULL;
     }
+
     return req;
 }
 
Signature · 1 line
2.54.0
#4 lore raw
In reply to Laurent Vivier
Quoted 23 lines · Laurent Vivier

A crafted migration stream can crash the destination QEMU process through unvalidated fields in the virtio device state: an unbounded allocation in virtio-serial, reachable assertions in the shared virtqueue element deserializer, and assert()/exit(1) calls in virtio-scsi request loading.

These are hardening fixes: the destination QEMU is in a paused pre-start state and the source VM is unaffected by a failed migration.

Patch 1 validates the virtio-serial nr_active_ports count against the configured maximum before allocating the post-load array.

Patch 2 replaces the assertions in qemu_get_virtqueue_element() with a bounds check returning NULL, and updates all callers (virtio-serial, virtio-blk, virtio-scsi, scsi-bus) to handle the failure gracefully.

Patch 3 replaces the remaining assert() and exit(1) calls in virtio_scsi_load_request() with proper error returns.

Tested with migration round-trips for virtio-serial (0 to 511 ports), virtio-blk (1-2 disks), and virtio-scsi (1-2 disks), plus the original PoC reproducers for issues #3801 and #3802.

Quoted 7 lines

Laurent Vivier (3):
hw/char/virtio-serial-bus: validate nr_active_ports from migration
stream
hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid
state
hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid
stream

Kevin objected to my version of this, I guess same will apply?

Quoted 6 lines
 hw/block/virtio-blk.c       |  4 ++++
 hw/char/virtio-serial-bus.c |  7 +++++++
 hw/scsi/scsi-bus.c          |  4 ++++
 hw/scsi/virtio-scsi.c       | 20 ++++++++++++++++----
 hw/virtio/virtio.c          | 11 ++++-------
 5 files changed, 35 insertions(+), 11 deletions(-)

A similar exit in mptsas?

Quoted 2 lines
Signature · 1 line
2.54.0
#5 lore raw

Am 25.07.2026 um 17:34 hat Michael S. Tsirkin geschrieben:

Quoted 32 lines · Laurent Vivier
Quoted 23 lines

A crafted migration stream can crash the destination QEMU process through unvalidated fields in the virtio device state: an unbounded allocation in virtio-serial, reachable assertions in the shared virtqueue element deserializer, and assert()/exit(1) calls in virtio-scsi request loading.

These are hardening fixes: the destination QEMU is in a paused pre-start state and the source VM is unaffected by a failed migration.

Patch 1 validates the virtio-serial nr_active_ports count against the configured maximum before allocating the post-load array.

Patch 2 replaces the assertions in qemu_get_virtqueue_element() with a bounds check returning NULL, and updates all callers (virtio-serial, virtio-blk, virtio-scsi, scsi-bus) to handle the failure gracefully.

Patch 3 replaces the remaining assert() and exit(1) calls in virtio_scsi_load_request() with proper error returns.

Tested with migration round-trips for virtio-serial (0 to 511 ports), virtio-blk (1-2 disks), and virtio-scsi (1-2 disks), plus the original PoC reproducers for issues #3801 and #3802.

Quoted 7 lines

Laurent Vivier (3):
hw/char/virtio-serial-bus: validate nr_active_ports from migration
stream
hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid
state
hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid
stream

Quoted 1 line

Kevin objected to my version of this, I guess same will apply?

Objecting is a big word. I suggested some improvements around passing errors to the caller instead of trying to handle everything locally. But yes, at the first sight I think they would apply here, too.

Kevin

#6 lore raw
Quoted 33 lines · Kevin Wolf

Am 25.07.2026 um 17:34 hat Michael S. Tsirkin geschrieben:

Quoted 32 lines · Laurent Vivier
Quoted 30 lines

A crafted migration stream can crash the destination QEMU process through unvalidated fields in the virtio device state: an unbounded allocation in virtio-serial, reachable assertions in the shared virtqueue element deserializer, and assert()/exit(1) calls in virtio-scsi request loading.

These are hardening fixes: the destination QEMU is in a paused pre-start state and the source VM is unaffected by a failed migration.

Patch 1 validates the virtio-serial nr_active_ports count against the configured maximum before allocating the post-load array.

Patch 2 replaces the assertions in qemu_get_virtqueue_element() with a bounds check returning NULL, and updates all callers (virtio-serial, virtio-blk, virtio-scsi, scsi-bus) to handle the failure gracefully.

Patch 3 replaces the remaining assert() and exit(1) calls in virtio_scsi_load_request() with proper error returns.

Tested with migration round-trips for virtio-serial (0 to 511 ports), virtio-blk (1-2 disks), and virtio-scsi (1-2 disks), plus the original PoC reproducers for issues #3801 and #3802.

Laurent Vivier (3):
hw/char/virtio-serial-bus: validate nr_active_ports from migration
stream
hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid
state
hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid
stream

Quoted 5 lines
Quoted 1 line

Kevin objected to my version of this, I guess same will apply?

Objecting is a big word. I suggested some improvements around passing errors to the caller instead of trying to handle everything locally. But yes, at the first sight I think they would apply here, too.

Michael,

will you rework your series or do you prefer I update mine?

Thanks,
Laurent

#7 lore raw
Quoted 40 lines · Laurent Vivier
Quoted 33 lines · Kevin Wolf

Am 25.07.2026 um 17:34 hat Michael S. Tsirkin geschrieben:

Quoted 32 lines · Laurent Vivier
Quoted 30 lines

A crafted migration stream can crash the destination QEMU process through unvalidated fields in the virtio device state: an unbounded allocation in virtio-serial, reachable assertions in the shared virtqueue element deserializer, and assert()/exit(1) calls in virtio-scsi request loading.

These are hardening fixes: the destination QEMU is in a paused pre-start state and the source VM is unaffected by a failed migration.

Patch 1 validates the virtio-serial nr_active_ports count against the configured maximum before allocating the post-load array.

Patch 2 replaces the assertions in qemu_get_virtqueue_element() with a bounds check returning NULL, and updates all callers (virtio-serial, virtio-blk, virtio-scsi, scsi-bus) to handle the failure gracefully.

Patch 3 replaces the remaining assert() and exit(1) calls in virtio_scsi_load_request() with proper error returns.

Tested with migration round-trips for virtio-serial (0 to 511 ports), virtio-blk (1-2 disks), and virtio-scsi (1-2 disks), plus the original PoC reproducers for issues #3801 and #3802.

Laurent Vivier (3):
hw/char/virtio-serial-bus: validate nr_active_ports from migration
stream
hw/virtio: return NULL from qemu_get_virtqueue_element() on invalid
state
hw/scsi/virtio-scsi: harden virtio_scsi_load_request() against invalid
stream

Quoted 5 lines
Quoted 1 line

Kevin objected to my version of this, I guess same will apply?

Objecting is a big word. I suggested some improvements around passing errors to the caller instead of trying to handle everything locally. But yes, at the first sight I think they would apply here, too.

Quoted 6 lines

Michael,

will you rework your series or do you prefer I update mine?

Thanks,
Laurent

Pls go ahead, thanks a lot!