Skip to contentSkip to conversation

check VirtiQueue Vring objects

PATCH Superseded P J P 4 participants 7 messages Nov 24, 2017

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

P J P

author
#0 lore raw

cover check VirtiQueue Vring objects

From: Prasad J Pandit <[email protected]>

Hello,

An user could attempt to use an uninitialised VirtQueue object or set Vring object with undue values, raising an unexpected exception in Qemu. This patch set fixes this issue and also adds a unit test to the suite.

Thank you.
--
Prasad J Pandit (2):
virtio: check VirtQueue Vring object is set
tests: add test to check VirtQueue object

 hw/virtio/virtio.c      | 11 ++++++++---
 tests/virtio-blk-test.c | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 1/2 virtio: check VirtQueue Vring object is set

From: Prasad J Pandit <[email protected]>

An user could attempt to use an uninitialised VirtQueue object or unset Vring.align leading to a arithmetic exception. Add check to avoid it.

---
 hw/virtio/virtio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Updated: add vring.num check to virtio_queue_update_rings
-> https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg04499.html

hw/virtio/virtio.c +8 -3
index 5884ce3480..0940f1daf3 100644
@@ -182,7 +182,7 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int n)
 {
     VRing *vring = &vdev->vq[n].vring;
 
-    if (!vring->desc) {
+    if (!vdev->vq[n].vring.num || !vring->desc || !vring->align) {
         /* not yet setup -> nothing to do */
         return;
     }
@@ -1414,6 +1414,9 @@ void virtio_config_modern_writel(VirtIODevice *vdev,
 
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
 {
+    if (!vdev->vq[n].vring.num) {
+        return;
+    }
     vdev->vq[n].vring.desc = addr;
     virtio_queue_update_rings(vdev, n);
 }
@@ -1494,8 +1497,10 @@ void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
      */
     assert(k->has_variable_vring_alignment);
 
-    vdev->vq[n].vring.align = align;
-    virtio_queue_update_rings(vdev, n);
+    if (align) {
+        vdev->vq[n].vring.align = align;
+        virtio_queue_update_rings(vdev, n);
+    }
 }
 
 static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
Signature · 1 line
2.13.6
author
#4 lore raw

PATCH 2/2 tests: add test to check VirtQueue object

In reply to P J P

From: Prasad J Pandit <[email protected]>

An uninitialised VirtQueue object or one with Vring.align field set to zero(0) could lead to arithmetic exceptions. Add a unit test to validate it.

---
 tests/virtio-blk-test.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/virtio-blk-test.c +21 -0
index e6fb9bac87..d00ceb0501 100644
@@ -674,6 +674,26 @@ static void pci_hotplug(void)
     qtest_shutdown(qs);
 }
 
+static void test_vring_align(void)
+{
+    QPCIBar bar0;
+    QOSState *qs;
+    QPCIDevice *dev;
+
+    qs = pci_test_start();
+    dev = qpci_device_find(qs->pcibus, QPCI_DEVFN(4, 0));
+    g_assert(dev != NULL);
+
+    qpci_device_enable(dev);
+    bar0 = qpci_iomap(dev, 0, NULL);
+
+    qpci_io_writeb(dev, bar0, VIRTIO_PCI_QUEUE_SEL, 2);
+    qpci_io_writel(dev, bar0, VIRTIO_PCI_QUEUE_PFN, 1);
+
+    g_free(dev);
+    qtest_shutdown(qs);
+}
+
 static void mmio_basic(void)
 {
     QVirtioMMIODevice *dev;
@@ -724,6 +744,7 @@ int main(int argc, char **argv)
         qtest_add_func("/virtio/blk/pci/basic", pci_basic);
         qtest_add_func("/virtio/blk/pci/indirect", pci_indirect);
         qtest_add_func("/virtio/blk/pci/config", pci_config);
+        qtest_add_func("/virtio/blk/pci/vring", test_vring_align);
         if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
             qtest_add_func("/virtio/blk/pci/msix", pci_msix);
             qtest_add_func("/virtio/blk/pci/idx", pci_idx);
Signature · 1 line
2.13.6
#2 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] virtio: check VirtQueue Vring object is set

In reply to P J P
Quoted 20 lines · On Fri, 24 Nov 2017 13:25:41 +0530 P J P <[email protected]> wrote:

From: Prasad J Pandit <[email protected]>

An user could attempt to use an uninitialised VirtQueue object or unset Vring.align leading to a arithmetic exception. Add check to avoid it.

---
 hw/virtio/virtio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Updated: add vring.num check to virtio_queue_update_rings
-> https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg04499.html

hw/virtio/virtio.c +0 -0
index 5884ce3480..0940f1daf3 100644

hw/virtio/virtio.c void virtio_queue_update_rings(VirtIODevice *vdev, int n) · line 186

Quoted 6 lines
hw/virtio/virtio.c +1 -1
@@ -182,7 +182,7 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int n)
 {
     VRing *vring = &vdev->vq[n].vring;
 
-    if (!vring->desc) {
+    if (!vdev->vq[n].vring.num || !vring->desc || !vring->align) {
         /* not yet setup -> nothing to do */

The check now implies "not yet setup or non-existing queue", no?

(Not that the check is wrong.)

Quoted 15 lines
hw/virtio/virtio.c +3 -0
         return;
     }
@@ -1414,6 +1414,9 @@ void virtio_config_modern_writel(VirtIODevice *vdev,
 
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
 {
+    if (!vdev->vq[n].vring.num) {
+        return;
+    }
     vdev->vq[n].vring.desc = addr;
     virtio_queue_update_rings(vdev, n);
 }
@@ -1494,8 +1497,10 @@ void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
      */
     assert(k->has_variable_vring_alignment);

hw/virtio/virtio.c void virtio_queue_set_align(VirtIODevice *vdev, int n, int align) · line 1506

Quoted 10 lines
hw/virtio/virtio.c +4 -2
 
-    vdev->vq[n].vring.align = align;
-    virtio_queue_update_rings(vdev, n);
+    if (align) {
+        vdev->vq[n].vring.align = align;
+        virtio_queue_update_rings(vdev, n);
+    }
 }
 
 static bool virtio_queue_notify_aio_vq(VirtQueue *vq)

I think you also need to check for num in virtio_queue_set_rings().

Reviewed-by on the v1 series
#6 lore raw
In reply to P J P
Quoted 15 lines · P J P

From: Prasad J Pandit <[email protected]>

Hello,

An user could attempt to use an uninitialised VirtQueue object or set Vring object with undue values, raising an unexpected exception in Qemu. This patch set fixes this issue and also adds a unit test to the suite.

Thank you.
--
Prasad J Pandit (2):
virtio: check VirtQueue Vring object is set
tests: add test to check VirtQueue object

Quoted 4 lines
 hw/virtio/virtio.c      | 11 ++++++++---
 tests/virtio-blk-test.c | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)
#5 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] tests: add test to check VirtQueue object

In reply to P J P
Quoted 16 lines · P J P

From: Prasad J Pandit <[email protected]>

An uninitialised VirtQueue object or one with Vring.align field set to zero(0) could lead to arithmetic exceptions. Add a unit test to validate it.

---
 tests/virtio-blk-test.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/virtio-blk-test.c +0 -0
index e6fb9bac87..d00ceb0501 100644

tests/virtio-blk-test.c static void pci_hotplug(void) · line 677

Quoted 4 lines
tests/virtio-blk-test.c +1 -0
@@ -674,6 +674,26 @@ static void pci_hotplug(void)
     qtest_shutdown(qs);
 }
 
+static void test_vring_align(void)

It's important to include a doc comment so the purpose of the test is clear:

/*
* Check that setting the vring addr on a non-existent virtqueue does
* not crash.
*/

I think the _align name is confusing since the vring alignment is not directly affected by this test case. Please call it test_nonexistent_virtqueue() instead.

Quoted 18 lines
tests/virtio-blk-test.c +18 -0
+{
+    QPCIBar bar0;
+    QOSState *qs;
+    QPCIDevice *dev;
+
+    qs = pci_test_start();
+    dev = qpci_device_find(qs->pcibus, QPCI_DEVFN(4, 0));
+    g_assert(dev != NULL);
+
+    qpci_device_enable(dev);
+    bar0 = qpci_iomap(dev, 0, NULL);
+
+    qpci_io_writeb(dev, bar0, VIRTIO_PCI_QUEUE_SEL, 2);
+    qpci_io_writel(dev, bar0, VIRTIO_PCI_QUEUE_PFN, 1);
+
+    g_free(dev);
+    qtest_shutdown(qs);
+}
Reviewed-by on v1 1/2
#3 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] virtio: check VirtQueue Vring object is set

In reply to P J P
Quoted 6 lines · P J P

From: Prasad J Pandit <[email protected]>

An user could attempt to use an uninitialised VirtQueue object or unset Vring.align leading to a arithmetic exception. Add check to avoid it.

Quoted 8 lines
---
 hw/virtio/virtio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Updated: add vring.num check to virtio_queue_update_rings
-> https://lists.gnu.org/archive/html/qemu-devel/2017-11/msg04499.html