virtio-net: validate backend queue numbers against bus limitation
PATCH 1/2 virtio-net: validate backend queue numbers against bus limitation
We don't validate the backend queue numbers against bus limitation, this will easily crash qemu if it exceeds the limitation. Fixing this by doing the validation and fail early.
--- hw/net/virtio-net.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/net/virtio-net.c +7 -0
@@ -1588,6 +1588,13 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)virtio_init (vdev,"virtio-net" ,VIRTIO_ID_NET , n->config_size); n->max_queues =MAX (n->nic_conf.peers.queues,1 ); +if (n->max_queues *2 +1 >VIRTIO_PCI_QUEUE_MAX ) { +error_setg (errp,"Invalid number of queues (= %" PRIu32"), " +"must be a postive integer less than %d." , + n->max_queues, (VIRTIO_PCI_QUEUE_MAX -1 ) /2 ); +virtio_cleanup (vdev); +return ; + } n->vqs =g_malloc0 (sizeof (VirtIONetQueue) * n->max_queues); n->vqs[0 ].rx_vq =virtio_add_queue (vdev,256 , virtio_net_handle_rx); n->curr_queues =1 ;
Signature · 1 line
2.1.0
PATCH 2/2 virtio-net: fix the upper bound when trying to delete queues
Virtqueue were indexed from zero, so don't delete virtqueue whose index is n->max_queues * 2 + 1.
--- hw/net/virtio-net.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/virtio-net.c +1 -1
@@ -1309,7 +1309,7 @@ static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) n->multiqueue = multiqueue; -for (i =2 ; i <= n->max_queues *2 +1 ; i++) { +for (i =2 ; i < n->max_queues *2 +1 ; i++) {virtio_del_queue (vdev, i); }
Signature · 1 line
2.1.0
Quoted 3 lines · Jason Wang
We don't validate the backend queue numbers against bus limitation, this will easily crash qemu if it exceeds the limitation. Fixing this by doing the validation and fail early.
Can you please include the qemu command line that crashes?
Quoted 26 lines
Cc qemu-stable <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/net/virtio-net.c | 7 +++++++ 1 file changed, 7 insertions(+)hw/net/virtio-net.c +7
-0@@ -1588,6 +1588,13 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)virtio_init (vdev,"virtio-net" ,VIRTIO_ID_NET , n->config_size); n->max_queues =MAX (n->nic_conf.peers.queues,1 ); +if (n->max_queues *2 +1 >VIRTIO_PCI_QUEUE_MAX ) { +error_setg (errp,"Invalid number of queues (= %" PRIu32"), " +"must be a postive integer less than %d." , + n->max_queues, (VIRTIO_PCI_QUEUE_MAX -1 ) /2 ); +virtio_cleanup (vdev); +return ; + } n->vqs =g_malloc0 (sizeof (VirtIONetQueue) * n->max_queues); n->vqs[0 ].rx_vq =virtio_add_queue (vdev,256 , virtio_net_handle_rx); n->curr_queues =1 ;Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH for 2.3 2/2] virtio-net: fix the upper bound when trying to delete queues
In reply to Jason WangQuoted 2 lines · Jason Wang
Virtqueue were indexed from zero, so don't delete virtqueue whose index is
n->max_queues* 2 + 1.
But what's the current behaviour?
Can it lead to aborts? virtio_del_queue does:
if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) {
abort();
}Quoted 22 lines
Cc Michael S. Tsirkin <[email protected]>Cc qemu-stable <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/net/virtio-net.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/net/virtio-net.c +1
-1@@ -1309,7 +1309,7 @@ static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) n->multiqueue = multiqueue; -for (i =2 ; i <= n->max_queues *2 +1 ; i++) { +for (i =2 ; i < n->max_queues *2 +1 ; i++) {virtio_del_queue (vdev, i); }Signature · 1 line
2.1.0
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Wang
We don't validate the backend queue numbers against bus limitation, this will easily crash qemu if it exceeds the limitation. Fixing
this
by doing the validation and fail early.Can you please include the qemu command line that crashes?
Ok, will include it in v2. cli is something like:
qemu-system-x86_64 -netdev tap,id=hn0,queues=256 -device virtio-net-pci,netdev=hn0 -enable-kvm
Re: [Qemu-devel] [PATCH for 2.3 2/2] virtio-net: fix the upper bound when trying to delete queues
In reply to Michael S. TsirkinQuoted 10 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Wang
Virtqueue were indexed from zero, so don't delete virtqueue whose index is
n->max_queues* 2 + 1.But what's the current behaviour?
Can it lead to aborts?
virtio_del_queuedoes:if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) { abort(); }
Yes, it will hit abort() above.
Quoted 27 lines
Quoted 26 lines
Cc Michael S. Tsirkin <[email protected]>Cc qemu-stable <[email protected]>Signed-off-by Jason Wang <[email protected]>---
hw/net/virtio-net.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)diff --git
a/hw/net/virtio-net.cb/hw/net/virtio-net.cindex 59f76bc..b6fac9c 100644
---a/hw/net/virtio-net.c
+++b/hw/net/virtio-net.c
@@ -1309,7 +1309,7 @@ static voidvirtio_net_set_multiqueue(VirtIONet *n, int multiqueue)n->multiqueue = multiqueue;- for (i = 2; i <=
n->max_queues* 2 + 1; i++) {+ for (i = 2; i < n->max_queues * 2 + 1; i++) { virtio_del_queue(vdev, i); }--
2.1.0
Re: [Qemu-devel] [PATCH for 2.3 2/2] virtio-net: fix the upper bound when trying to delete queues
Quoted 7 lines · Jason Wang
Quoted 4 lines · Michael S. Tsirkin
Quoted 2 lines · Jason Wang
Virtqueue were indexed from zero, so don't delete virtqueue whose index is
n->max_queues* 2 + 1.
Quoted 8 lines
Quoted 6 lines
But what's the current behaviour?
Can it lead to aborts?
virtio_del_queuedoes:if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) { abort(); }Yes, it will hit
abort()above.
With patch 1/1 on top, there will be no crash. Will drop this from V2. This patch was only needed for more virtio queues series.
Quoted 24 lines
Quoted 23 lines
Quoted 22 lines
Cc Michael S. Tsirkin <[email protected]>Cc qemu-stable <[email protected]>Signed-off-by Jason Wang <[email protected]>---
hw/net/virtio-net.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)diff --git
a/hw/net/virtio-net.cb/hw/net/virtio-net.c
index 59f76bc..b6fac9c 100644
---a/hw/net/virtio-net.c
+++b/hw/net/virtio-net.c
@@ -1309,7 +1309,7 @@ static voidvirtio_net_set_multiqueue(VirtIONet *n, int multiqueue)n->multiqueue = multiqueue; - for (i = 2; i <= n->max_queues * 2 + 1; i++) { + for (i = 2; i < n->max_queues * 2 + 1; i++) { virtio_del_queue(vdev, i); }-- 2.1.0