提交 7157e2e2 编写于 作者: S Stefan Hajnoczi 提交者: Michael S. Tsirkin

virtio: guard against negative vq notifies

The virtio_queue_notify() function checks that the virtqueue number is
less than the maximum number of virtqueues.  A signed comparison is used
but the virtqueue number could be negative if a buggy or malicious guest
is run.  This results in memory accesses outside of the virtqueue array.

It is risky doing input validation in common code instead of at the
guest<->host boundary.  Note that virtio_queue_set_addr(),
virtio_queue_get_addr(), virtio_queue_get_num(), and many other virtio
functions do *not* validate the virtqueue number argument.

Instead of fixing the comparison in virtio_queue_notify(), move the
comparison to the virtio bindings (just like VIRTIO_PCI_QUEUE_SEL) where
we have a uint32_t value and can avoid ever calling into common virtio
code if the virtqueue number is invalid.
Signed-off-by: NStefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: NMichael S. Tsirkin <mst@redhat.com>
上级 e75ccf2c
...@@ -146,7 +146,9 @@ static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset, ...@@ -146,7 +146,9 @@ static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset,
vdev->queue_sel = value; vdev->queue_sel = value;
break; break;
case SYBORG_VIRTIO_QUEUE_NOTIFY: case SYBORG_VIRTIO_QUEUE_NOTIFY:
virtio_queue_notify(vdev, value); if (value < VIRTIO_PCI_QUEUE_MAX) {
virtio_queue_notify(vdev, value);
}
break; break;
case SYBORG_VIRTIO_STATUS: case SYBORG_VIRTIO_STATUS:
virtio_set_status(vdev, value & 0xFF); virtio_set_status(vdev, value & 0xFF);
......
...@@ -348,7 +348,9 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) ...@@ -348,7 +348,9 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
vdev->queue_sel = val; vdev->queue_sel = val;
break; break;
case VIRTIO_PCI_QUEUE_NOTIFY: case VIRTIO_PCI_QUEUE_NOTIFY:
virtio_queue_notify(vdev, val); if (val < VIRTIO_PCI_QUEUE_MAX) {
virtio_queue_notify(vdev, val);
}
break; break;
case VIRTIO_PCI_STATUS: case VIRTIO_PCI_STATUS:
if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
......
...@@ -585,9 +585,7 @@ void virtio_queue_notify_vq(VirtQueue *vq) ...@@ -585,9 +585,7 @@ void virtio_queue_notify_vq(VirtQueue *vq)
void virtio_queue_notify(VirtIODevice *vdev, int n) void virtio_queue_notify(VirtIODevice *vdev, int n)
{ {
if (n < VIRTIO_PCI_QUEUE_MAX) { virtio_queue_notify_vq(&vdev->vq[n]);
virtio_queue_notify_vq(&vdev->vq[n]);
}
} }
uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册