Skip to contentSkip to conversation

virtio-mmio: fixes to QueueNum, QueueNumMax

PATCH Superseded Peter Maydell 2 participants 10 messages Jul 25, 2013 – Jul 28, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtio-mmio: fixes to QueueNum, QueueNumMax

These patches fix a couple of bugs in virtio-mmio's handling of the registers that deal with the queue size:

* as mst points out, letting the guest flip a queue between "exists" and "doesn't exist" is a bad idea
* QueueNumMax wasn't reading the correct value for nonexistent queues

This doesn't include any change to the behaviour of queuesize on reset (discussed in other thread); the current behaviour is not a problem for well-behaved guests, and safe in the face of badly-behaved guests, and currently improving the reset behaviour is blocked by an unrelated bug.

Peter Maydell (2):
hw/virtio/virtio: Don't allow guests to add/remove queues hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

 hw/virtio/virtio-mmio.c |    3 +++
 hw/virtio/virtio.c      |   10 +++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/2 hw/virtio/virtio: Don't allow guests to add/remove queues

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +7 -3
index 09f62c6..d5b0502 100644
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||
+        (num > VIRTQUEUE_MAX_SIZE) ||
+        (num < 0)) {
+        return;
     }
+    vdev->vq[n].vring.num = num;
+    virtqueue_init(&vdev->vq[n]);
 }
 
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 15 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +0 -0
index 09f62c6..d5b0502 100644

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 677

Quoted 8 lines
hw/virtio/virtio.c +2 -3
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||

Cleaner (imho)

    !num != !vdev->vq[n].vring.num

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 678

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num > VIRTQUEUE_MAX_SIZE) ||

Pls don't put () around simple math.
It has natural precedence wrt <> so it just makes it look like lisp.

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 679

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num < 0)) {

How does it ever get negative?
assert (num >= 0) instead?

Quoted 9 lines
hw/virtio/virtio.c +3 -0
+        return;
     }
+    vdev->vq[n].vring.num = num;
+    virtqueue_init(&vdev->vq[n]);
 }
 
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 27 lines · Michael S. Tsirkin
Quoted 15 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +0 -0
index 09f62c6..d5b0502 100644

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 677

Quoted 8 lines
hw/virtio/virtio.c +2 -3
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||

Cleaner (imho)

Quoted 1 line
    !num != !vdev->vq[n].vring.num

I think that's more confusing, and you really don't want "guards so we don't let the guest do bad things" to be confusing to read.

Quoted 3 lines

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 679

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num < 0)) {

How does it ever get negative?

If the guest maliciously writes a value with bit 31 set to the register...

-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 28 lines · Peter Maydell
Quoted 27 lines · Michael S. Tsirkin
Quoted 15 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +0 -0
index 09f62c6..d5b0502 100644

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 677

Quoted 8 lines
hw/virtio/virtio.c +2 -3
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||

Cleaner (imho)

Quoted 5 lines
Quoted 1 line
    !num != !vdev->vq[n].vring.num

I think that's more confusing, and you really don't want "guards so we don't let the guest do bad things" to be confusing to read.

Confusing to whom? That's really subjective. You can use cast to bool or !! if you prefer.

     (bool)num != (bool)vdev->vq[n].vring.num

Point is, most other code in this file uses (x) and !(x) and not != 0.
That's objective, so please, find a way to not test ==0/!= 0.

Quoted 8 lines
Quoted 3 lines

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 679

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num < 0)) {

How does it ever get negative?

If the guest maliciously writes a value with bit 31 set to the register...

-- PMM

Make the argument unsigned then?

Signature · 1 line
MST
#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 31 lines · Michael S. Tsirkin
Quoted 30 lines · Peter Maydell
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +0 -0
index 09f62c6..d5b0502 100644

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 677

Quoted 8 lines
hw/virtio/virtio.c +2 -3
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||

Cleaner (imho)

    !num != !vdev->vq[n].vring.num
Quoted 7 lines
Quoted 3 lines

I think that's more confusing, and you really don't want "guards so we don't let the guest do bad things" to be confusing to read.

Confusing to whom? That's really subjective. You can use cast to bool or !! if you prefer.

     (bool)num != (bool)vdev->vq[n].vring.num

This is still confusing. We're trying to say "if the number is currently zero, don't let it go non-zero; if it's non-zero, don't let it go zero", and the clear way to say that is exactly how I wrote it. This isn't a critical code path so there's no speed justification for obfuscating what we're doing.

Quoted 3 lines

Point is, most other code in this file uses (x) and !(x) and not != 0.
That's objective, so please, find a way to not test ==0/!= 0.

   if ((!num && vdev->vq[n].vring.num) ||
       (num && !vdev->vq[n].vring.num) ||
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 679

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num < 0)) {

How does it ever get negative?

If the guest maliciously writes a value with bit 31 set to the register...

Make the argument unsigned then?

Would make this function inconsistent with the existing get_num() function.

-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 40 lines · Peter Maydell
Quoted 31 lines · Michael S. Tsirkin
Quoted 30 lines · Peter Maydell
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +0 -0
index 09f62c6..d5b0502 100644

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 677

Quoted 8 lines
hw/virtio/virtio.c +2 -3
@@ -673,10 +673,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    if ((num == 0 && vdev->vq[n].vring.num != 0) ||
+        (num != 0 && vdev->vq[n].vring.num == 0) ||

Cleaner (imho)

    !num != !vdev->vq[n].vring.num
Quoted 7 lines
Quoted 3 lines

I think that's more confusing, and you really don't want "guards so we don't let the guest do bad things" to be confusing to read.

Confusing to whom? That's really subjective. You can use cast to bool or !! if you prefer.

     (bool)num != (bool)vdev->vq[n].vring.num
Quoted 6 lines

This is still confusing. We're trying to say "if the number is currently zero, don't let it go non-zero; if it's non-zero, don't let it go zero", and the clear way to say that is exactly how I wrote it. This isn't a critical code path so there's no speed justification for obfuscating what we're doing.

What you write is too low level, you have to squint to figure out it is correct. What you are really trying to say is "don't allow guest change between zero and non zero values". That's why it's clearer my way: we test "zero" status with !x (or non zero status with (bool)cast) and make sure it is not changed.

Quoted 6 lines
Quoted 3 lines

Point is, most other code in this file uses (x) and !(x) and not != 0.
That's objective, so please, find a way to not test ==0/!= 0.

   if ((!num && vdev->vq[n].vring.num) ||
       (num && !vdev->vq[n].vring.num) ||

Better, though != is still slightly clearer IMO.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

hw/virtio/virtio.c hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n) · line 679

Quoted 1 line
hw/virtio/virtio.c +1 -0
+        (num < 0)) {

How does it ever get negative?

If the guest maliciously writes a value with bit 31 set to the register...

Quoted 6 lines
Quoted 1 line

Make the argument unsigned then?

Would make this function inconsistent with the existing get_num() function.

-- PMM

Let's fix that one too?

#7 lore raw

PATCH 2/2 hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

In reply to Peter Maydell

The virtio-mmio spec says that QueueNumMax must read zero for queues which are unavailable; implement this, rather than always returning VIRTQUEUE_MAX_SIZE.

---
 hw/virtio/virtio-mmio.c |    3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-mmio.c +3 -0
index 54d6679..aefb7e4 100644
@@ -151,6 +151,9 @@ static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
         }
         return proxy->host_features;
     case VIRTIO_MMIO_QUEUENUMMAX:
+        if (virtio_queue_get_num(vdev, vdev->queue_sel) == 0) {
+            return 0;
+        }
         return VIRTQUEUE_MAX_SIZE;
     case VIRTIO_MMIO_QUEUEPFN:
         return virtio_queue_get_addr(vdev, vdev->queue_sel)
Signature · 1 line
1.7.9.5
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

Quoted 14 lines · Peter Maydell

The virtio-mmio spec says that QueueNumMax must read zero for queues which are unavailable; implement this, rather than always returning VIRTQUEUE_MAX_SIZE.

---
 hw/virtio/virtio-mmio.c |    3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-mmio.c +0 -0
index 54d6679..aefb7e4 100644

hw/virtio/virtio-mmio.c static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size) · line 156

Quoted 6 lines
hw/virtio/virtio-mmio.c +3 -0
@@ -151,6 +151,9 @@ static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
         }
         return proxy->host_features;
     case VIRTIO_MMIO_QUEUENUMMAX:
+        if (virtio_queue_get_num(vdev, vdev->queue_sel) == 0) {
+            return 0;
+        }

All other callers do:

       if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {

So please make this one consistent and use the shorter form.

Quoted 5 lines
hw/virtio/virtio-mmio.c +0 -0
         return VIRTQUEUE_MAX_SIZE;
     case VIRTIO_MMIO_QUEUEPFN:
         return virtio_queue_get_addr(vdev, vdev->queue_sel)
Signature · 1 line
1.7.9.5
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

Quoted 10 lines · Michael S. Tsirkin

hw/virtio/virtio-mmio.c static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size) · line 156

Quoted 4 lines · Peter Maydell

case VIRTIO_MMIO_QUEUENUMMAX:

hw/virtio/virtio-mmio.c +3 -0
+        if (virtio_queue_get_num(vdev, vdev->queue_sel) == 0) {
+            return 0;
+        }

All other callers do:

       if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {

So please make this one consistent and use the shorter form.

OK.

-- PMM