Skip to contentSkip to conversation

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

PATCH In review Laurent Vivier 4 participants 6 messages Jul 8 – Jul 9

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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().

Cc: [email protected]
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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Laurent Vivier

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

Quoted 6 lines

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3801

---
 hw/char/virtio-serial-bus.c | 4 ++++
 1 file changed, 4 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v1 1/1
#2 lore raw
In reply to Laurent Vivier
Quoted 21 lines · Laurent Vivier

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

Cc: [email protected]
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 +0 -0
index c1973f0248fc..80f1b308aa53 100644

hw/char/virtio-serial-bus.c static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f, · line 813

Quoted 10 lines
hw/char/virtio-serial-bus.c +4 -0
@@ -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) {
Acked-by on v1 1/1
#3 lore raw
In reply to Laurent Vivier
Quoted 6 lines · Laurent Vivier

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.

Quoted 6 lines

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

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3801

Quoted 21 lines
---
 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
#4 lore raw
In reply to Laurent Vivier
Quoted 10 lines · Laurent Vivier

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

Can you add a Fixes tag pls?

Quoted 2 lines

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

to make sure who is merging this me?

Quoted 21 lines
---
 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
#5 lore raw
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Laurent Vivier

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.

Quoted 6 lines
Quoted 4 lines

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

Can you add a Fixes tag pls?

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

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

to make sure who is merging this me?

Yes, please merge it through your tree

Thanks,
Laurent

Quoted 23 lines
Quoted 22 lines
---
  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