diff mbox series

USB: core: Check buffer length matches wLength for control transfers

Message ID 20210526153244.GA1400430@rowland.harvard.edu (mailing list archive)
State New
Headers show
Series USB: core: Check buffer length matches wLength for control transfers | expand

Commit Message

Alan Stern May 26, 2021, 3:32 p.m. UTC
A type of inconsistency that can show up in control URBs is when the
setup packet's wLength value does not match the URB's
transfer_buffer_length field.  The two should always be equal;
differences could lead to information leaks or undefined behavior for
OUT transfers or overruns for IN transfers.

This patch adds a test for such mismatches during URB submission.  If
the test fails, the submission is rejected with a -EBADR error code
(which is not used elsewhere in the USB core), and a debugging message
is logged for people interested in tracking down these errors.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Johan Hovold <johan@kernel.org>

---


[as1961]


 Documentation/driver-api/usb/error-codes.rst |    3 +++
 drivers/usb/core/urb.c                       |    6 ++++++
 2 files changed, 9 insertions(+)

Comments

Johan Hovold May 27, 2021, 8:23 a.m. UTC | #1
On Wed, May 26, 2021 at 11:32:44AM -0400, Alan Stern wrote:
> A type of inconsistency that can show up in control URBs is when the
> setup packet's wLength value does not match the URB's
> transfer_buffer_length field.  The two should always be equal;
> differences could lead to information leaks or undefined behavior for
> OUT transfers or overruns for IN transfers.
> 
> This patch adds a test for such mismatches during URB submission.  If
> the test fails, the submission is rejected with a -EBADR error code
> (which is not used elsewhere in the USB core), and a debugging message
> is logged for people interested in tracking down these errors.
> 
> Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> CC: Johan Hovold <johan@kernel.org>

Looks good.

Reviewed-by: Johan Hovold <johan@kernel.org>

> ---
> 
> 
> [as1961]
> 
> 
>  Documentation/driver-api/usb/error-codes.rst |    3 +++
>  drivers/usb/core/urb.c                       |    6 ++++++
>  2 files changed, 9 insertions(+)
> 
> Index: usb-devel/drivers/usb/core/urb.c
> ===================================================================
> --- usb-devel.orig/drivers/usb/core/urb.c
> +++ usb-devel/drivers/usb/core/urb.c
> @@ -410,6 +410,12 @@ int usb_submit_urb(struct urb *urb, gfp_
>  		dev_WARN_ONCE(&dev->dev, (usb_pipeout(urb->pipe) != is_out),
>  				"BOGUS control dir, pipe %x doesn't match bRequestType %x\n",
>  				urb->pipe, setup->bRequestType);
> +		if (le16_to_cpu(setup->wLength) != urb->transfer_buffer_length) {
> +			dev_dbg(&dev->dev, "BOGUS control len %d doesn't match transfer length %d\n",
> +					le16_to_cpu(setup->wLength),
> +					urb->transfer_buffer_length);
> +			return -EBADR;
> +		}
>  	} else {
>  		is_out = usb_endpoint_dir_out(&ep->desc);
>  	}
> Index: usb-devel/Documentation/driver-api/usb/error-codes.rst
> ===================================================================
> --- usb-devel.orig/Documentation/driver-api/usb/error-codes.rst
> +++ usb-devel/Documentation/driver-api/usb/error-codes.rst
> @@ -61,6 +61,9 @@ USB-specific:
>  			(c) requested data transfer length is invalid: negative
>  			    or too large for the host controller.
>  
> +``-EBADR``		The wLength value in a control URB's setup packet does
> +			not match the URB's transfer_buffer_length.
> +
>  ``-ENOSPC``		This request would overcommit the usb bandwidth reserved
>  			for periodic transfers (interrupt, isochronous).
>
Greg KH May 27, 2021, 11:46 a.m. UTC | #2
On Wed, May 26, 2021 at 11:32:44AM -0400, Alan Stern wrote:
> A type of inconsistency that can show up in control URBs is when the
> setup packet's wLength value does not match the URB's
> transfer_buffer_length field.  The two should always be equal;
> differences could lead to information leaks or undefined behavior for
> OUT transfers or overruns for IN transfers.
> 
> This patch adds a test for such mismatches during URB submission.  If
> the test fails, the submission is rejected with a -EBADR error code
> (which is not used elsewhere in the USB core), and a debugging message
> is logged for people interested in tracking down these errors.
> 
> Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
> CC: Johan Hovold <johan@kernel.org>
> 
> ---
> 
> 
> [as1961]
> 
> 
>  Documentation/driver-api/usb/error-codes.rst |    3 +++
>  drivers/usb/core/urb.c                       |    6 ++++++
>  2 files changed, 9 insertions(+)
> 
> Index: usb-devel/drivers/usb/core/urb.c
> ===================================================================
> --- usb-devel.orig/drivers/usb/core/urb.c
> +++ usb-devel/drivers/usb/core/urb.c
> @@ -410,6 +410,12 @@ int usb_submit_urb(struct urb *urb, gfp_
>  		dev_WARN_ONCE(&dev->dev, (usb_pipeout(urb->pipe) != is_out),
>  				"BOGUS control dir, pipe %x doesn't match bRequestType %x\n",
>  				urb->pipe, setup->bRequestType);
> +		if (le16_to_cpu(setup->wLength) != urb->transfer_buffer_length) {
> +			dev_dbg(&dev->dev, "BOGUS control len %d doesn't match transfer length %d\n",
> +					le16_to_cpu(setup->wLength),
> +					urb->transfer_buffer_length);
> +			return -EBADR;
> +		}

Looks good, let's see if this catches anything :)

now queued up, thanks.

greg k-h
diff mbox series

Patch

Index: usb-devel/drivers/usb/core/urb.c
===================================================================
--- usb-devel.orig/drivers/usb/core/urb.c
+++ usb-devel/drivers/usb/core/urb.c
@@ -410,6 +410,12 @@  int usb_submit_urb(struct urb *urb, gfp_
 		dev_WARN_ONCE(&dev->dev, (usb_pipeout(urb->pipe) != is_out),
 				"BOGUS control dir, pipe %x doesn't match bRequestType %x\n",
 				urb->pipe, setup->bRequestType);
+		if (le16_to_cpu(setup->wLength) != urb->transfer_buffer_length) {
+			dev_dbg(&dev->dev, "BOGUS control len %d doesn't match transfer length %d\n",
+					le16_to_cpu(setup->wLength),
+					urb->transfer_buffer_length);
+			return -EBADR;
+		}
 	} else {
 		is_out = usb_endpoint_dir_out(&ep->desc);
 	}
Index: usb-devel/Documentation/driver-api/usb/error-codes.rst
===================================================================
--- usb-devel.orig/Documentation/driver-api/usb/error-codes.rst
+++ usb-devel/Documentation/driver-api/usb/error-codes.rst
@@ -61,6 +61,9 @@  USB-specific:
 			(c) requested data transfer length is invalid: negative
 			    or too large for the host controller.
 
+``-EBADR``		The wLength value in a control URB's setup packet does
+			not match the URB's transfer_buffer_length.
+
 ``-ENOSPC``		This request would overcommit the usb bandwidth reserved
 			for periodic transfers (interrupt, isochronous).