# Re: \[PATCH v2 1/2\] comedi: ni\_usb6501: fix NULL-deref in command paths

- From: Ian Abbott \<abbotti@mev.co.uk\>
- Date: 2021-10-28 09:51:51 
- List: linux-usb
- Page: https://ratatoskr.run/linux-usb/2021/10/1556646
- Thread: https://ratatoskr.run/linux-usb/2021/10/1555474/t.md

```text
On 27/10/2021 10:35, Johan Hovold wrote:
> The driver uses endpoint-sized USB transfer buffers but had no sanity
> checks on the sizes. This can lead to zero-size-pointer dereferences or
> overflowed transfer buffers in ni6501_port_command() and
> ni6501_counter_command() if a (malicious) device has smaller max-packet
> sizes than expected (or when doing descriptor fuzz testing).
>
> Add the missing sanity checks to probe().
>
> Fixes: a03bb00e50ab ("staging: comedi: add NI USB-6501 support")
> Cc: <redacted>      # 3.18
> Cc: Luca Ellero <redacted>
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---
>   drivers/comedi/drivers/ni_usb6501.c | 10 ++++++++++
>   1 file changed, 10 insertions(+)
>
> diff --git a/drivers/comedi/drivers/ni_usb6501.c b/drivers/comedi/drivers/ni_usb6501.c
> index 5b6d9d783b2f..c42987b74b1d 100644
> --- a/drivers/comedi/drivers/ni_usb6501.c
> +++ b/drivers/comedi/drivers/ni_usb6501.c
> @@ -144,6 +144,10 @@ static const u8 READ_COUNTER_RESPONSE[]	= {0x00, 0x01, 0x00, 0x10,
>   					   0x00, 0x00, 0x00, 0x02,
>   					   0x00, 0x00, 0x00, 0x00};
>   
> +/* Largest supported packets */
> +static const size_t TX_MAX_SIZE	= sizeof(SET_PORT_DIR_REQUEST);
> +static const size_t RX_MAX_SIZE	= sizeof(READ_PORT_RESPONSE);
> +
>   enum commands {
>   	READ_PORT,
>   	WRITE_PORT,
> @@ -501,6 +505,12 @@ static int ni6501_find_endpoints(struct comedi_device *dev)
>   	if (!devpriv->ep_rx || !devpriv->ep_tx)
>   		return -ENODEV;
>   
> +	if (usb_endpoint_maxp(devpriv->ep_rx) < RX_MAX_SIZE)
> +		return -ENODEV;
> +
> +	if (usb_endpoint_maxp(devpriv->ep_tx) < TX_MAX_SIZE)
> +		return -ENODEV;
> +
>   	return 0;
>   }
>   
>

Looks good, thanks!

Reviewed-by: Ian Abbott <abbotti@mev.co.uk>

-- 
-=( Ian Abbott [ref] || 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. || www.mev.co.uk )=-
```
