Skip to contentSkip to conversation

virtio: abort on zero config length

PATCH In review Jason Wang 5 participants 11 messages Apr 25, 2013 – Apr 26, 2013

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 virtio: abort on zero config length

In fact we don't support zero length config length for virtio device. And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Jason Wang

In fact we don't support zero length config length for virtio device. And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

Quoted 31 lines
---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
#2 lore raw
In reply to Jason Wang
Quoted 1 line · Jason Wang

In fact we don't support zero length config length for virtio device.

virtio-rng?

Quoted 2 lines

And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

Not sure what you mean, but virtio-rng has a zero length config space.

Regards,

Anthony Liguori

Quoted 33 lines
---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
#3 lore raw
Quoted 5 lines · Anthony Liguori
Quoted 1 line · Jason Wang

In fact we don't support zero length config length for virtio device.

virtio-rng?

It has config_len == 0? In that case guest using virtio-rng can crash qemu or read qemu memory:

uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)

{
    uint8_t val;

    vdev->get_config(vdev, vdev->config);

    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

        return (uint32_t)-1;

    val = ldub_p(vdev->config + addr);
    return val;
}

how about corrupting qemu memory? That too:

void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)

{
    uint8_t val = data;

    if (addr > (vdev->config_len - sizeof(val)))
        return;

    stb_p(vdev->config + addr, val);

    if (vdev->set_config)
        vdev->set_config(vdev, vdev->config);
}
Quoted 42 lines
Quoted 2 lines

And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

Not sure what you mean, but virtio-rng has a zero length config space.

Regards,

Anthony Liguori

Quoted 33 lines
---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
#4 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 5 lines · Anthony Liguori
Quoted 1 line · Jason Wang

In fact we don't support zero length config length for virtio device.

virtio-rng?

It has config_len == 0? In that case guest using virtio-rng can crash qemu or read qemu memory:

uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)

Quoted 8 lines
{
    uint8_t val;

    vdev->get_config(vdev, vdev->config);

    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

There's no requirement that config_len is >= 4 so this would fail equally awfully with config_len=1|2|3.

Regards,

Anthony Liguori

Quoted 67 lines

        return (uint32_t)-1;

    val = ldub_p(vdev->config + addr);
    return val;
}

how about corrupting qemu memory? That too:

void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)

{
    uint8_t val = data;

    if (addr > (vdev->config_len - sizeof(val)))
        return;

    stb_p(vdev->config + addr, val);

    if (vdev->set_config)
        vdev->set_config(vdev, vdev->config);
}
Quoted 42 lines
Quoted 2 lines

And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

Not sure what you mean, but virtio-rng has a zero length config space.

Regards,

Anthony Liguori

Quoted 33 lines
---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
author
#5 lore raw
Quoted 14 lines · Anthony Liguori
Quoted 12 lines · Michael S. Tsirkin
Quoted 4 lines · Anthony Liguori
Quoted 1 line · Jason Wang

In fact we don't support zero length config length for virtio device.

virtio-rng?

It has config_len == 0? In that case guest using virtio-rng can crash qemu or read qemu memory:

uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)

{
    uint8_t val;
Quoted 7 lines
Quoted 5 lines
    vdev->get_config(vdev, vdev->config);

    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Ok, but since the discussion is in public list, no need for CVE then.

Quoted 3 lines

There's no requirement that config_len is >= 4 so this would fail equally awfully with config_len=1|2|3.

Will check if (addr + size > vdev->config_len) in the caller for both read and write.

Thanks

Quoted 69 lines

Regards,

Anthony Liguori

Quoted 64 lines
        return (uint32_t)-1;

    val = ldub_p(vdev->config + addr);
    return val;
}

how about corrupting qemu memory? That too:

void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)

{
    uint8_t val = data;

    if (addr > (vdev->config_len - sizeof(val)))
        return;

    stb_p(vdev->config + addr, val);

    if (vdev->set_config)
        vdev->set_config(vdev, vdev->config);
}
Quoted 40 lines
Quoted 2 lines

And it can lead outbound memory access. So abort on zero config length to catch the bug earlier.

Not sure what you mean, but virtio-rng has a zero length config space.

Regards,

Anthony Liguori

Quoted 32 lines
---
 hw/virtio/virtio.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
hw/virtio/virtio.c +2 -5
index 1c2282c..a6fa667 100644
@@ -923,6 +923,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
                  uint16_t device_id, size_t config_size)
 {
     int i;
+    assert(config_size);
     vdev->device_id = device_id;
     vdev->status = 0;
     vdev->isr = 0;
@@ -938,11 +939,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
 
     vdev->name = name;
     vdev->config_len = config_size;
-    if (vdev->config_len) {
-        vdev->config = g_malloc0(config_size);
-    } else {
-        vdev->config = NULL;
-    }
+    vdev->config = g_malloc0(config_size);
     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                      vdev);
 }
Signature · 1 line
1.7.1
#6 lore raw
Quoted 7 lines · Jason Wang
Quoted 5 lines
Quoted 3 lines
    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Ok, but since the discussion is in public list, no need for CVE then.

Wrong. CVEs are useful even for publicly disclosed bugs. It tells people whether they need to upgrade in order to avoid a vulnerability.

What we don't need is embargo. But we do need a CVE.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#7 lore raw
Quoted 12 lines · Eric Blake
Quoted 6 lines · Jason Wang
Quoted 5 lines
Quoted 3 lines
    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Ok, but since the discussion is in public list, no need for CVE then.

Wrong. CVEs are useful even for publicly disclosed bugs. It tells people whether they need to upgrade in order to avoid a vulnerability.

What we don't need is embargo. But we do need a CVE.

True, thanks for the correction.

#10 lore raw
In reply to Eric Blake
Quoted 11 lines · Eric Blake
Quoted 7 lines · Jason Wang
Quoted 5 lines
Quoted 3 lines
    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Ok, but since the discussion is in public list, no need for CVE then.

Wrong. CVEs are useful even for publicly disclosed bugs. It tells people whether they need to upgrade in order to avoid a vulnerability.

Small addition (since my English parser turns "whether" into "whether or not"): a CVE tells people *that* (not "if") they should upgrade. Lack of a CVE mention in a commit doesn't imply -- at least in, ugh, another big project -- that the fix is without security consequences ("no CVE fix, I don't need it").

(Apologies for hair-splitting.)

Laszlo

#8 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang
Quoted 6 lines · Eric Blake
Quoted 5 lines · Jason Wang
Quoted 5 lines
Quoted 3 lines
    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Ok, but since the discussion is in public list, no need for CVE then.

Wrong. CVEs are useful even for publicly disclosed bugs. It tells people whether they need to upgrade in order to avoid a vulnerability.

What we don't need is embargo. But we do need a CVE.

True, thanks for the correction.

I think we never shipped QEMU release with this bug. So no need for CVEs. I'm not sure upstream has to bother with CVEs - we can just say this is downstream work.

Signature · 1 line
MST
#9 lore raw
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Jason Wang
Quoted 10 lines · Eric Blake
Quoted 6 lines · Jason Wang
Quoted 5 lines
Quoted 3 lines
    if (addr > (vdev->config_len - sizeof(val)))

^^^^^^^^^ quiz: spot a bug above if config_len is 0 :)

Then we need to fix these bugs and allocate a CVE. virtio-rng has shipped. This code is also dumb.

Ok, but since the discussion is in public list, no need for CVE then.

Wrong. CVEs are useful even for publicly disclosed bugs. It tells people whether they need to upgrade in order to avoid a vulnerability.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

What we don't need is embargo. But we do need a CVE.

True, thanks for the correction.

I think we never shipped QEMU release with this bug. So no need for CVEs. I'm not sure upstream has to bother with CVEs - we can just say this is downstream work.

Uh, we certainly do. QEMU 1.4 had virtio-rng and therefore had this bug so we need to allocate a CVE.

Regards,

Anthony Liguori

Quoted 3 lines
Signature · 2 lines
-- 
MST