net/virtio-user: fix overflow
PATCH 1/1 net/virtio-user: fix overflow
This commit fixes an array overflow when number of queues is higher than 8.
--- drivers/net/virtio/virtio_pci.h | 3 ++- drivers/net/virtio/virtio_user/virtio_user_dev.c | 2 +- drivers/net/virtio/virtio_user/virtio_user_dev.h | 6 +++--- drivers/net/virtio/virtio_user_ethdev.c | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_pci.h +2 -1
@@ -160,7 +160,8 @@/* * Maximum number of virtqueues per device. */ -#define VIRTIO_MAX_VIRTQUEUES 8 +#define VIRTIO_MAX_VIRTQUEUE_PAIRS 8 +#define VIRTIO_MAX_VIRTQUEUES VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1/* Common configuration */ #define VIRTIO_PCI_CAP_COMMON_CFG 1
drivers/net/virtio/virtio_user/virtio_user_dev.c +1 -1
@@ -234,7 +234,7 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)uint32_t i, q; dev->vhostfd =-1 ; -for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES *2 +1 ; ++i) { +for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES ; ++i) { dev->kickfds[i] =-1 ; dev->callfds[i] =-1 ; }
drivers/net/virtio/virtio_user/virtio_user_dev.h +3 -3
@@ -49,8 +49,8 @@ struct virtio_user_dev {int *tapfds;/* for both vhost_user and vhost_kernel */ -int callfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; -int kickfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +int callfds[VIRTIO_MAX_VIRTQUEUES ]; +int kickfds[VIRTIO_MAX_VIRTQUEUES ];int mac_specified;uint32_t max_queue_pairs;uint32_t queue_pairs;
@@ -62,7 +62,7 @@ struct virtio_user_dev {uint8_t status;uint8_t mac_addr[ETHER_ADDR_LEN ];char path[PATH_MAX ]; -struct vring vrings[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +struct vring vrings[VIRTIO_MAX_VIRTQUEUES ];struct virtio_user_backend_ops *ops; };
drivers/net/virtio/virtio_user_ethdev.c +7 -0
@@ -433,6 +433,13 @@ goto end; } +if (queues >VIRTIO_MAX_VIRTQUEUE_PAIRS ) { +PMD_INIT_LOG (ERR ,"arg %s %u exceeds the limit %u" , +VIRTIO_USER_ARG_QUEUES_NUM , queues, +VIRTIO_MAX_VIRTQUEUE_PAIRS ); + goto end; + } + eth_dev =virtio_user_eth_dev_alloc (name);if (!eth_dev) {PMD_INIT_LOG (ERR ,"virtio_user fails to alloc device" );
Signature · 1 line
1.8.3.1
Quoted 1 line · Wenfeng Liu
This commit fixes an array overflow when number of queues is higher than 8.
Firstly, this commit log could be a bit more informative, to something like:
virtio-user limits the qeueue number to 8 but provides no limit check against the queue number input from user. If a bigger queue number (> 8) is given, there is an overflow issue. Doing a sanity check could avoid it.
Quoted 30 lines
Fixes 37a7eb2ae816 ("net/virtio-user: add device emulation layer")Signed-off-by Wenfeng Liu <[email protected]>--- drivers/net/virtio/virtio_pci.h | 3 ++- drivers/net/virtio/virtio_user/virtio_user_dev.c | 2 +- drivers/net/virtio/virtio_user/virtio_user_dev.h | 6 +++--- drivers/net/virtio/virtio_user_ethdev.c | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-)drivers/net/virtio/virtio_pci.h +2
-1@@ -160,7 +160,8 @@/* * Maximum number of virtqueues per device. */ -#define VIRTIO_MAX_VIRTQUEUES 8 +#define VIRTIO_MAX_VIRTQUEUE_PAIRS 8 +#define VIRTIO_MAX_VIRTQUEUES VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1/* Common configuration */ #define VIRTIO_PCI_CAP_COMMON_CFG 1drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0
drivers/net/virtio/virtio_user/virtio_user_dev.c int virtio_user_stop_device(struct virtio_user_dev *dev) · line 237
Quoted 5 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +1
-1@@ -234,7 +234,7 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)uint32_t i, q; dev->vhostfd =-1 ; -for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES *2 +1 ; ++i) { +for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES ; ++i) {
Right, we don't need setup callfd and kickfd for the ctrl-queue.
Quoted 19 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0dev->kickfds[i] =-1 ; dev->callfds[i] =-1 ; }drivers/net/virtio/virtio_user/virtio_user_dev.h +2
-2@@ -49,8 +49,8 @@ struct virtio_user_dev {int *tapfds;/* for both vhost_user and vhost_kernel */ -int callfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; -int kickfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +int callfds[VIRTIO_MAX_VIRTQUEUES ]; +int kickfds[VIRTIO_MAX_VIRTQUEUES ];int mac_specified;uint32_t max_queue_pairs;uint32_t queue_pairs;
drivers/net/virtio/virtio_user/virtio_user_dev.h struct virtio_user_dev { · line 65
Quoted 5 lines
drivers/net/virtio/virtio_user/virtio_user_dev.h +1
-1@@ -62,7 +62,7 @@ struct virtio_user_dev {uint8_t status;uint8_t mac_addr[ETHER_ADDR_LEN ];char path[PATH_MAX ]; -struct vring vrings[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +struct vring vrings[VIRTIO_MAX_VIRTQUEUES ];
Have you actually tested your patch? You are removing the vring for ctrl-queue, which is wrong to me.
Quoted 8 lines
drivers/net/virtio/virtio_user/virtio_user_dev.h +0
-0struct virtio_user_backend_ops *ops; };drivers/net/virtio/virtio_user_ethdev.c +0
-0
drivers/net/virtio/virtio_user_ethdev.c line 441
Quoted 9 lines
drivers/net/virtio/virtio_user_ethdev.c +6
-0@@ -433,6 +433,13 @@ goto end; } +if (queues >VIRTIO_MAX_VIRTQUEUE_PAIRS ) { +PMD_INIT_LOG (ERR ,"arg %s %u exceeds the limit %u" , +VIRTIO_USER_ARG_QUEUES_NUM , queues, +VIRTIO_MAX_VIRTQUEUE_PAIRS ); + goto end; + }
Yes, we need this check. So, to me, you were actually doing two things in this patch:
- check the queue number, to avoid overflow
- remove the callfds and kickfds for ctrl-queue.
That said, please do them in two patches.
Thanks.
--yliu
Quoted 6 lines
drivers/net/virtio/virtio_user_ethdev.c +1
-0+ eth_dev =virtio_user_eth_dev_alloc (name);if (!eth_dev) {PMD_INIT_LOG (ERR ,"virtio_user fails to alloc device" );Signature · 1 line
1.8.3.1
Hi Yuanhan,
Quoted 2 lines · Wenfeng Liu
Quoted 1 line
This commit fixes an array overflow when number of queues is higher than
8.
Quoted 9 lines
Firstly, this commit log could be a bit more informative, to something like:
virtio-user limits the qeueue number to 8 but provides no limit check against the queue number input from user. If a bigger queue number (> 8) is given, there is an overflow issue. Doing a sanity check could avoid it.
Sure, I will revise the commit log accordingly.
Quoted 23 lines
Quoted 23 lines
Fixes 37a7eb2ae816 ("net/virtio-user: add device emulation layer")Signed-off-by Wenfeng Liu <[email protected]>--- drivers/net/virtio/virtio_pci.h | 3 ++- drivers/net/virtio/virtio_user/virtio_user_dev.c | 2 +- drivers/net/virtio/virtio_user/virtio_user_dev.h | 6 +++--- drivers/net/virtio/virtio_user_ethdev.c | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/drivers/net/virtio/virtio_pci.hindex 59e45c4..bd940b4 100644drivers/net/virtio/virtio_pci.h +2
-1@@ -160,7 +160,8 @@/* * Maximum number of virtqueues per device. */ -#define VIRTIO_MAX_VIRTQUEUES 8 +#define VIRTIO_MAX_VIRTQUEUE_PAIRS 8 +#define VIRTIO_MAX_VIRTQUEUES VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1
Quoted 8 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c int virtio_user_stop_device(struct virtio_user_dev
Quoted 8 lines
drivers/net/virtio/virtio_pci.h +0
-0/* Common configuration */ #define VIRTIO_PCI_CAP_COMMON_CFG 1drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0@@ -234,7 +234,7 @@ int virtio_user_stop_device(struct virtio_user_dev
*dev)
Quoted 7 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c int virtio_user_stop_device(struct virtio_user_dev *dev) · line 237
Quoted 5 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +1
-1uint32_t i, q; dev->vhostfd =-1 ; -for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES *2 +1 ; ++i) { +for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES ; ++i) {Right, we don't need setup callfd and kickfd for the ctrl-queue.
I did not remove the ctrl-queue. I just redefined the MACRO according to DRY principle:
#define VIRTIO_MAX_VIRTQUEUES VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1 I noticed that I missed the bracket in the MACRO and will add it in next version.
Quoted 12 lines
Quoted 12 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0dev->kickfds[i] =-1 ; dev->callfds[i] =-1 ; }drivers/net/virtio/virtio_user/virtio_user_dev.h +0
-0@@ -49,8 +49,8 @@ struct virtio_user_dev {int *tapfds;
Quoted 16 lines
Quoted 9 lines
drivers/net/virtio/virtio_user/virtio_user_dev.h +2
-2/* for both vhost_user and vhost_kernel */ -int callfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; -int kickfds[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +int callfds[VIRTIO_MAX_VIRTQUEUES ]; +int kickfds[VIRTIO_MAX_VIRTQUEUES ];int mac_specified;uint32_t max_queue_pairs;uint32_t queue_pairs;drivers/net/virtio/virtio_user/virtio_user_dev.h struct virtio_user_dev { · line 65
Quoted 5 lines
drivers/net/virtio/virtio_user/virtio_user_dev.h +1
-1@@ -62,7 +62,7 @@ struct virtio_user_dev {uint8_t status;uint8_t mac_addr[ETHER_ADDR_LEN ];char path[PATH_MAX ]; -struct vring vrings[VIRTIO_MAX_VIRTQUEUES *2 +1 ]; +struct vring vrings[VIRTIO_MAX_VIRTQUEUES ];Have you actually tested your patch? You are removing the vring for
ctrl-queue, which is wrong to me.
Quoted 12 lines
Quoted 12 lines
drivers/net/virtio/virtio_user/virtio_user_dev.h +0
-0struct virtio_user_backend_ops *ops; };drivers/net/virtio/virtio_user_ethdev.c +0
-0@@ -433,6 +433,13 @@ goto end; }
Quoted 8 lines
drivers/net/virtio/virtio_user_ethdev.c line 441
Quoted 6 lines
drivers/net/virtio/virtio_user_ethdev.c +6
-0+if (queues >VIRTIO_MAX_VIRTQUEUE_PAIRS ) { +PMD_INIT_LOG (ERR ,"arg %s %u exceeds the limit %u" , +VIRTIO_USER_ARG_QUEUES_NUM , queues, +VIRTIO_MAX_VIRTQUEUE_PAIRS ); + goto end; + }Yes, we need this check. So, to me, you were actually doing two things in
this patch:
Quoted 15 lines
- check the queue number, to avoid overflow
- remove the callfds and kickfds for ctrl-queue.That said, please do them in two patches.
Thanks.
--yliuQuoted 6 lines
drivers/net/virtio/virtio_user_ethdev.c +1
-1+ eth_dev =virtio_user_eth_dev_alloc (name);if (!eth_dev) {PMD_INIT_LOG (ERR ,"virtio_user fails to alloc device" ); --1.8.3.1
Regards,
liuwf
Quoted 51 lines · Wenfeng Liu
Hi Yuanhan,
Quoted 2 lines · Wenfeng Liu
Quoted 1 line
This commit fixes an array overflow when number of queues is higher than
8.
Quoted 9 lines
Firstly, this commit log could be a bit more informative, to something like:
virtio-user limits the qeueue number to 8 but provides no limit check against the queue number input from user. If a bigger queue number (> 8) is given, there is an overflow issue. Doing a sanity check could avoid it.
Sure, I will revise the commit log accordingly.
Quoted 23 lines
Quoted 23 lines
Fixes 37a7eb2ae816 ("net/virtio-user: add device emulation layer")Signed-off-by Wenfeng Liu <[email protected]>--- drivers/net/virtio/virtio_pci.h | 3 ++- drivers/net/virtio/virtio_user/virtio_user_dev.c | 2 +- drivers/net/virtio/virtio_user/virtio_user_dev.h | 6 +++--- drivers/net/virtio/virtio_user_ethdev.c | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/drivers/net/virtio/virtio_pci.hindex 59e45c4..bd940b4 100644drivers/net/virtio/virtio_pci.h +2
-1@@ -160,7 +160,8 @@/* * Maximum number of virtqueues per device. */ -#define VIRTIO_MAX_VIRTQUEUES 8 +#define VIRTIO_MAX_VIRTQUEUE_PAIRS 8 +#define VIRTIO_MAX_VIRTQUEUES VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1Quoted 8 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c int virtio_user_stop_device(struct virtio_user_dev
Quoted 8 lines
drivers/net/virtio/virtio_pci.h +0
-0/* Common configuration */ #define VIRTIO_PCI_CAP_COMMON_CFG 1drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0@@ -234,7 +234,7 @@ int virtio_user_stop_device(struct virtio_user_dev*dev)
Quoted 2 lines
Quoted 2 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +0
-0uint32_t i, q;
Quoted 8 lines
Quoted 5 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c int virtio_user_stop_device(struct virtio_user_dev *dev) · line 237
Quoted 3 lines
drivers/net/virtio/virtio_user/virtio_user_dev.c +1
-1dev->vhostfd =-1 ; -for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES *2 +1 ; ++i) { +for (i =0 ; i <VIRTIO_MAX_VIRTQUEUES ; ++i) {Right, we don't need setup callfd and kickfd for the ctrl-queue.
I did not remove the ctrl-queue. I just redefined the MACRO according to DRY principle:
Oh, right, sorry, I overlooked it. Then for this patch, it's okay to me.
Quoted 3 lines
#define
VIRTIO_MAX_VIRTQUEUESVIRTIO_MAX_VIRTQUEUE_PAIRS* 2 + 1 I noticed that I missed the bracket in the MACRO and will add it in next version.
Yes, please. Also, please put a "Cc: [email protected]" before your
Signed-of-by: it's a candidate for stable releases.
--yliu