2022-06-06 04:20:43

by Xiaohui Zhang

[permalink] [raw]
Subject: [PATCH 1/1] comedi: ni_usb6501: fix transfer-buffer overflows

From: xiaohuizhang98 <[email protected]>

We detected a suspected bug with our code clone detection tool.

Similar to the handling of vmk80xx_alloc_usb_buffers in commit
a23461c47482("comedi: vmk80xx: fix transfer-buffer overflows"),
we thought a patch might be needed here as well.

The driver uses endpoint-sized USB transfer buffers but up until
recently had no sanity checks on the sizes.

Signed-off-by: xiaohuizhang98 <[email protected]>
---
drivers/comedi/drivers/ni_usb6501.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/comedi/drivers/ni_usb6501.c b/drivers/comedi/drivers/ni_usb6501.c
index 0dd9edf7bced..3e2b9f4d840b 100644
--- a/drivers/comedi/drivers/ni_usb6501.c
+++ b/drivers/comedi/drivers/ni_usb6501.c
@@ -90,6 +90,7 @@
#include <linux/comedi/comedi_usb.h>

#define NI6501_TIMEOUT 1000
+#define MIN_BUF_SIZE 64

/* Port request packets */
static const u8 READ_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
@@ -459,12 +460,12 @@ static int ni6501_alloc_usb_buffers(struct comedi_device *dev)
struct ni6501_private *devpriv = dev->private;
size_t size;

- size = usb_endpoint_maxp(devpriv->ep_rx);
+ size = max(usb_endpoint_maxp(devpriv->ep_rx), MIN_BUF_SIZE);
devpriv->usb_rx_buf = kzalloc(size, GFP_KERNEL);
if (!devpriv->usb_rx_buf)
return -ENOMEM;

- size = usb_endpoint_maxp(devpriv->ep_tx);
+ size = max(usb_endpoint_maxp(devpriv->ep_rx), MIN_BUF_SIZE);
devpriv->usb_tx_buf = kzalloc(size, GFP_KERNEL);
if (!devpriv->usb_tx_buf)
return -ENOMEM;
--
2.17.1


2022-06-06 10:35:17

by Ian Abbott

[permalink] [raw]
Subject: Re: [PATCH 1/1] comedi: ni_usb6501: fix transfer-buffer overflows

On 05/06/2022 13:43, Xiaohui Zhang wrote:
> From: xiaohuizhang98 <[email protected]>
>
> We detected a suspected bug with our code clone detection tool.
>
> Similar to the handling of vmk80xx_alloc_usb_buffers in commit
> a23461c47482("comedi: vmk80xx: fix transfer-buffer overflows"),
> we thought a patch might be needed here as well.
>
> The driver uses endpoint-sized USB transfer buffers but up until
> recently had no sanity checks on the sizes.
>
> Signed-off-by: xiaohuizhang98 <[email protected]>
> ---
> drivers/comedi/drivers/ni_usb6501.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/comedi/drivers/ni_usb6501.c b/drivers/comedi/drivers/ni_usb6501.c
> index 0dd9edf7bced..3e2b9f4d840b 100644
> --- a/drivers/comedi/drivers/ni_usb6501.c
> +++ b/drivers/comedi/drivers/ni_usb6501.c
> @@ -90,6 +90,7 @@
> #include <linux/comedi/comedi_usb.h>
>
> #define NI6501_TIMEOUT 1000
> +#define MIN_BUF_SIZE 64
>
> /* Port request packets */
> static const u8 READ_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
> @@ -459,12 +460,12 @@ static int ni6501_alloc_usb_buffers(struct comedi_device *dev)
> struct ni6501_private *devpriv = dev->private;
> size_t size;
>
> - size = usb_endpoint_maxp(devpriv->ep_rx);
> + size = max(usb_endpoint_maxp(devpriv->ep_rx), MIN_BUF_SIZE);
> devpriv->usb_rx_buf = kzalloc(size, GFP_KERNEL);
> if (!devpriv->usb_rx_buf)
> return -ENOMEM;
>
> - size = usb_endpoint_maxp(devpriv->ep_tx);
> + size = max(usb_endpoint_maxp(devpriv->ep_rx), MIN_BUF_SIZE);
> devpriv->usb_tx_buf = kzalloc(size, GFP_KERNEL);
> if (!devpriv->usb_tx_buf)
> return -ENOMEM;

Actually, there is a typo in the last bit of the patch that was copied
from the similar patch to vmk80xx. It is using the wrong endpoint to
determine the size for usb_tx_buf.

--
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company )=-
-=( registered in England & Wales. Regd. number: 02862268. )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || http://www.mev.co.uk )=-