Skip to contentSkip to conversation

USB Device buffer overflow

PATCH In review Szymon Heidrich 4 participants 16 messages Nov 16, 2022 – Dec 19, 2022 USB

lore
Share & tools
lore
#0 lore raw

Hello,

Similar to CVE-2021-39685 affecting the Linux kernel U-Boot is vulnerable to a buffer overflow present in the USB Gadget stack. Handling of a control transfer request with wLength larger than USB_BUFSIZ (4096) may result in a buffer overflow.

The buffer for USB control endpoint is allocated in the composite_bind function implemented in drivers/usb/gadget/composite.c. The buffer size is set to USB_BUFSIZ (4096) bytes.

Quoted 9 lines
	/* preallocate control response and buffer */
	cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
	if (!cdev->req)
		goto fail;
	cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
	if (!cdev->req->buf)
		goto fail;
	cdev->req->complete = composite_setup_complete;
	gadget->ep0->driver_data = cdev;

In the composite_setup function data transfer phase is set up to the length of "value" bytes which in multiple cases may be controlled by an attacker (is set to wLength).

Quoted 10 lines
	if (value >= 0) {
		req->length = value;
		req->zero = value < w_length;
		value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
		if (value < 0) {
			debug("ep_queue --> %d\n", value);
			req->status = 0;
			composite_setup_complete(gadget->ep0, req);
		}
	}

In example the OS descriptor handler may be forced to set value to w_length.

Quoted 11 lines
} else {
    /* "extended compatibility ID"s */
    count = count_ext_compat(os_desc_cfg);
    buf[8] = count;
    count *= 24; /* 24 B/ext compat desc */
    count += 16; /* header */
    put_unaligned_le32(count, buf);
    buf += 16;
    fill_ext_compat(os_desc_cfg, buf);
    value = w_length;
}

Execution of this code path for wLength set to a value larger then USB_BUFSIZ will result in a buffer overflow. Since wLength is a double byte value it may have values up to 0xffff.

Besides the common OS descriptor handler this issue may be exploited for some of the available gadgets e.g. f_dfu, f_sdp where "value" is derived from wLength.

drivers/usb/gadget/f_dfu.c

Quoted 13 lines

static int handle_dnload(struct usb_gadget *gadget, u16 len)

{
	struct usb_composite_dev *cdev = get_gadget_data(gadget);
	struct usb_request *req = cdev->req;
	struct f_dfu *f_dfu = req->context;

	if (len == 0)
		f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;

	req->complete = dnload_request_complete;

	return len;
}

drivers/usb/gadget/f_sdp.c

Quoted 12 lines
if (req_type == USB_TYPE_CLASS) {
   int report = w_value & HID_REPORT_ID_MASK;

    /* HID (SDP) request */
    switch (ctrl->bRequest) {
    case HID_REQ_SET_REPORT:
        switch (report) {
        case 1:
            value = SDP_COMMAND_LEN + 1;
            req->complete = sdp_rx_command_complete;
            sdp_func->ep_int_enable = false;
            break;
Quoted 8 lines
        case 2:
            value = len;
            req->complete = sdp_rx_data_complete;
            sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
            break;
        }
    }
}

Please find attached a patch addressing this issue.
Depending on request direction wLength larger than USB_BUFSIZ will result in either endpoint stall or value trim.

Best regards,
Szymon

From 1723d976ab0387be0d91b177011559cb07a66e03 Mon Sep 17 00:00:00 2001
From: Szymon Heidrich <[email protected]>
Date: Wed, 9 Nov 2022 17:55:34 +0100
Subject: [PATCH] Prevent buffer overflow on USB control endpoint

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
 drivers/usb/gadget/composite.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +11 -0
index 2a309e62..cb89f6dc 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
 	u8				endp;
 	struct usb_configuration	*c;
 
+	if (w_length > USB_BUFSIZ) {
+		if (ctrl->bRequestType & USB_DIR_IN) {
+			/* Cast away the const, we are going to overwrite on purpose. */
+			__le16 *temp = (__le16 *)&ctrl->wLength;
+			*temp = cpu_to_le16(USB_BUFSIZ);
+			w_length = USB_BUFSIZ;
+		} else {
+			goto done;
+		}
+	}
+
 	/*
 	 * partial re-init of the response message; the function or the
 	 * gadget might need to intercept e.g. a control-OUT completion
Signature · 1 line
2.38.1
#1 lore raw

Hello Szymon,

Looks like a generalization of CVE-2022-2347 I found earlier. While both I and Venkatesh Yadav Abbarapu of AMD made patches for that CVE localized to DFU, given the presence of the same problematic pattern elsewhere, the bounds check aspect of that CVE fix would perhaps be better in a centralized location like what you did here.

Sultan

Quoted 98 lines · Szymon Heidrich

Hello,

Similar to CVE-2021-39685 affecting the Linux kernel U-Boot is vulnerable to a buffer overflow present in the USB Gadget stack. Handling of a control transfer request with wLength larger than USB_BUFSIZ (4096) may result in a buffer overflow.

The buffer for USB control endpoint is allocated in the composite_bind function implemented in drivers/usb/gadget/composite.c. The buffer size is set to USB_BUFSIZ (4096) bytes.

Quoted 9 lines
   /* preallocate control response and buffer */
   cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
   if (!cdev->req)
       goto fail;
   cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
   if (!cdev->req->buf)
       goto fail;
   cdev->req->complete = composite_setup_complete;
   gadget->ep0->driver_data = cdev;

In the composite_setup function data transfer phase is set up to the length of "value" bytes which in multiple cases may be controlled by an attacker (is set to wLength).

Quoted 10 lines
   if (value >= 0) {
       req->length = value;
       req->zero = value < w_length;
       value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
       if (value < 0) {
           debug("ep_queue --> %d\n", value);
           req->status = 0;
           composite_setup_complete(gadget->ep0, req);
       }
   }

In example the OS descriptor handler may be forced to set value to w_length.

Quoted 11 lines
} else {
   /* "extended compatibility ID"s */
   count = count_ext_compat(os_desc_cfg);
   buf[8] = count;
   count *= 24; /* 24 B/ext compat desc */
   count += 16; /* header */
   put_unaligned_le32(count, buf);
   buf += 16;
   fill_ext_compat(os_desc_cfg, buf);
   value = w_length;
}

Execution of this code path for wLength set to a value larger then USB_BUFSIZ will result in a buffer overflow. Since wLength is a double byte value it may have values up to 0xffff.

Besides the common OS descriptor handler this issue may be exploited for some of the available gadgets e.g. f_dfu, f_sdp where "value" is derived from wLength.

drivers/usb/gadget/f_dfu.c

Quoted 13 lines

static int handle_dnload(struct usb_gadget *gadget, u16 len)

{
   struct usb_composite_dev *cdev = get_gadget_data(gadget);
   struct usb_request *req = cdev->req;
   struct f_dfu *f_dfu = req->context;

   if (len == 0)
       f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;

   req->complete = dnload_request_complete;

   return len;
}

drivers/usb/gadget/f_sdp.c

Quoted 12 lines

if (req_type == USB_TYPE_CLASS) {
int report = w_value & HID_REPORT_ID_MASK;

   /* HID (SDP) request */
   switch (ctrl->bRequest) {
   case HID_REQ_SET_REPORT:
       switch (report) {
       case 1:
           value = SDP_COMMAND_LEN + 1;
           req->complete = sdp_rx_command_complete;
           sdp_func->ep_int_enable = false;
           break;
Quoted 8 lines
       case 2:
           value = len;
           req->complete = sdp_rx_data_complete;
           sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
           break;
       }
   }
}

Please find attached a patch addressing this issue.
Depending on request direction wLength larger than USB_BUFSIZ will result in either endpoint stall or value trim.

Best regards,
Szymon <0001-Prevent-buffer-overflow-on-USB-control-endpoint.patch>

#2 lore raw
In reply to Szymon Heidrich

Hi Szymon,

[Adding Marek]

Quoted 91 lines · Szymon Heidrich

Hello,

Similar to CVE-2021-39685 affecting the Linux kernel U-Boot is vulnerable to a buffer overflow present in the USB Gadget stack. Handling of a control transfer request with wLength larger than USB_BUFSIZ (4096) may result in a buffer overflow.

The buffer for USB control endpoint is allocated in the composite_bind function implemented in drivers/usb/gadget/composite.c. The buffer size is set to USB_BUFSIZ (4096) bytes.

Quoted 9 lines
      /* preallocate control response and buffer */
      cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
      if (!cdev->req)
              goto fail;
      cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
      if (!cdev->req->buf)
              goto fail;
      cdev->req->complete = composite_setup_complete;
      gadget->ep0->driver_data = cdev;

In the composite_setup function data transfer phase is set up to the length of "value" bytes which in multiple cases may be controlled by an attacker (is set to wLength).

Quoted 10 lines
      if (value >= 0) {
              req->length = value;
              req->zero = value < w_length;
              value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
              if (value < 0) {
                      debug("ep_queue --> %d\n", value);
                      req->status = 0;
                      composite_setup_complete(gadget->ep0, req);
              }
      }

In example the OS descriptor handler may be forced to set value to w_length.

Quoted 11 lines
} else {
    /* "extended compatibility ID"s */
    count = count_ext_compat(os_desc_cfg);
    buf[8] = count;
    count *= 24; /* 24 B/ext compat desc */
    count += 16; /* header */
    put_unaligned_le32(count, buf);
    buf += 16;
    fill_ext_compat(os_desc_cfg, buf);
    value = w_length;
}

Execution of this code path for wLength set to a value larger then USB_BUFSIZ will result in a buffer overflow. Since wLength is a double byte value it may have values up to 0xffff.

Besides the common OS descriptor handler this issue may be exploited for some of the available gadgets e.g. f_dfu, f_sdp where "value" is derived from wLength.

drivers/usb/gadget/f_dfu.c

Quoted 13 lines

static int handle_dnload(struct usb_gadget *gadget, u16 len)

{
      struct usb_composite_dev *cdev = get_gadget_data(gadget);
      struct usb_request *req = cdev->req;
      struct f_dfu *f_dfu = req->context;

      if (len == 0)
              f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;

      req->complete = dnload_request_complete;

      return len;
}

drivers/usb/gadget/f_sdp.c

Quoted 12 lines
if (req_type == USB_TYPE_CLASS) {
   int report = w_value & HID_REPORT_ID_MASK;

    /* HID (SDP) request */
    switch (ctrl->bRequest) {
    case HID_REQ_SET_REPORT:
        switch (report) {
        case 1:
            value = SDP_COMMAND_LEN + 1;
            req->complete = sdp_rx_command_complete;
            sdp_func->ep_int_enable = false;
            break;
Quoted 8 lines
        case 2:
            value = len;
            req->complete = sdp_rx_data_complete;
            sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
            break;
        }
    }
}
Quoted 3 lines

Please find attached a patch addressing this issue.
Depending on request direction wLength larger than USB_BUFSIZ will result in either endpoint stall or value trim.

Thanks for the patch. Please submit it via git send-email instead of attachment.

#3 lore raw

PATCH 1/1 Prevent buffer overflow on USB control endpoint

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
 drivers/usb/gadget/composite.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +11 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
 	u8				endp;
 	struct usb_configuration	*c;
 
+	if (w_length > USB_BUFSIZ) {
+		if (ctrl->bRequestType & USB_DIR_IN) {
+			/* Cast away the const, we are going to overwrite on purpose. */
+			__le16 *temp = (__le16 *)&ctrl->wLength;
+			*temp = cpu_to_le16(USB_BUFSIZ);
+			w_length = USB_BUFSIZ;
+		} else {
+			goto done;
+		}
+	}
+
 	/*
 	 * partial re-init of the response message; the function or the
 	 * gadget might need to intercept e.g. a control-OUT completion
Signature · 1 line
2.38.1
#4 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

[Adding Lukasz and Marek]

Quoted 35 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
 drivers/usb/gadget/composite.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +11 -1
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
        u8                              endp;
        struct usb_configuration        *c;
 
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;
+               } else {
+                       goto done;
+               }
+       }
+
        /*
         * partial re-init of the response message; the function or the
         * gadget might need to intercept e.g. a control-OUT completion
--

2.38.1

#5 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
  drivers/usb/gadget/composite.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
         u8                              endp;
         struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Where do such long packets come from ?

What is the test-case ?

#6 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
  drivers/usb/gadget/composite.c | 11 +++++++++++

  1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
          u8                              endp;
          struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

#7 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
   drivers/usb/gadget/composite.c | 11 +++++++++++
   1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
          u8                              endp;
          struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Why shouldn't the patch do simple

w_length = min(w_length, USB_BUFSIZ);

?

#8 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 47 lines · Marek Vasut
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
   drivers/usb/gadget/composite.c | 11 +++++++++++

   1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
           u8                              endp;
           struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Quoted 5 lines

Why shouldn't the patch do simple

w_length = min(w_length, USB_BUFSIZ);

?

The composite_setup function in composite.c uses w_length but function specific setup functions are passed the struct usb_ctrlrequest *ctrl so both must be updated. If only w_length is updated using min(w_length, USB_BUFSIZ) than function specific setup will still receive the original request including unaltered wLength so it may again return a number of bytes greater than buffer size resulting in an overflow.

#9 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 50 lines · Szymon Heidrich
Quoted 49 lines · Marek Vasut
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
    drivers/usb/gadget/composite.c | 11 +++++++++++
    1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
           u8                              endp;
           struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Why shouldn't the patch do simple

Quoted 7 lines
Quoted 3 lines

w_length = min(w_length, USB_BUFSIZ);

?

The composite_setup function in composite.c uses w_length but function specific setup functions are passed the struct usb_ctrlrequest *ctrl so both must be updated. If only w_length is updated using min(w_length, USB_BUFSIZ) than function specific setup will still receive the original request including unaltered wLength so it may again return a number of bytes greater than buffer size resulting in an overflow.

Can you fill this sanitization into the setup wrapper here ?

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

That way, it would apply for all the gadget drivers and you won't have to deal with the const overwrite , right ?

#10 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 59 lines · Marek Vasut
Quoted 50 lines · Szymon Heidrich
Quoted 49 lines · Marek Vasut
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
    drivers/usb/gadget/composite.c | 11 +++++++++++

    1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
            u8                              endp;
            struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Why shouldn't the patch do simple

Quoted 7 lines
Quoted 3 lines

w_length = min(w_length, USB_BUFSIZ);

?

The composite_setup function in composite.c uses w_length but function specific setup functions are passed the struct usb_ctrlrequest *ctrl so both must be updated. If only w_length is updated using min(w_length, USB_BUFSIZ) than function specific setup will still receive the original request including unaltered wLength so it may again return a number of bytes greater than buffer size resulting in an overflow.

Quoted 5 lines

Can you fill this sanitization into the setup wrapper here ?

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

That way, it would apply for all the gadget drivers and you won't have to deal with the const overwrite , right ?

Sure, that should be fine.
I'll provide a patch when when this is merged, OK?

#11 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 60 lines · Szymon Heidrich
Quoted 59 lines · Marek Vasut
Quoted 57 lines · Szymon Heidrich
Quoted 47 lines · Marek Vasut
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
     drivers/usb/gadget/composite.c | 11 +++++++++++
     1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
            u8                              endp;
            struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Quoted 5 lines

Why shouldn't the patch do simple

w_length = min(w_length, USB_BUFSIZ);

?

The composite_setup function in composite.c uses w_length but function specific setup functions are passed the struct usb_ctrlrequest *ctrl so both must be updated. If only w_length is updated using min(w_length, USB_BUFSIZ) than function specific setup will still receive the original request including unaltered wLength so it may again return a number of bytes greater than buffer size resulting in an overflow.

Quoted 8 lines
Quoted 5 lines

Can you fill this sanitization into the setup wrapper here ?

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

That way, it would apply for all the gadget drivers and you won't have to deal with the const overwrite , right ?

Sure, that should be fine.
I'll provide a patch when when this is merged, OK?

Please review/test the aforementioned patch and let me know if it works for you, the patch can still be tweaked one way or the other.

#12 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

In reply to Szymon Heidrich
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
  drivers/usb/gadget/composite.c | 11 +++++++++++

  1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
          u8                              endp;
          struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Is there anything additional required from my side?

#13 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 47 lines · Szymon Heidrich
Quoted 37 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
   drivers/usb/gadget/composite.c | 11 +++++++++++
   1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
          u8                              endp;
          struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

Quoted 8 lines

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Quoted 1 line

Is there anything additional required from my side?

Sorry for the delay, I am still processing outstanding email.

#14 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 48 lines · Marek Vasut
Quoted 47 lines · Szymon Heidrich
Quoted 45 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
   drivers/usb/gadget/composite.c | 11 +++++++++++

   1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
           u8                              endp;
           struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Quoted 3 lines
Quoted 1 line

Is there anything additional required from my side?

Sorry for the delay, I am still processing outstanding email.

Could you please review this patch?

#15 lore raw

Re: [PATCH] Prevent buffer overflow on USB control endpoint

Quoted 49 lines · Szymon Heidrich
Quoted 48 lines · Marek Vasut
Quoted 47 lines · Szymon Heidrich
Quoted 45 lines · Szymon Heidrich
Quoted 32 lines · Marek Vasut
Quoted 22 lines · Fabio Estevam

[Adding Lukasz and Marek]

Quoted 18 lines · Szymon Heidrich

Assure that the control endpoint buffer of size USB_BUFSIZ (4096) can not be overflown during handling of USB control transfer requests with wLength greater than USB_BUFSIZ.

---
    drivers/usb/gadget/composite.c | 11 +++++++++++
    1 file changed, 11 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 2a309e624e..cb89f6dca9 100644
@@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
           u8                              endp;
           struct usb_configuration        *c;
 
Quoted 6 lines

drivers/usb/gadget/composite.c composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) · line 1027

Quoted 6 lines
drivers/usb/gadget/composite.c +6 -0
+       if (w_length > USB_BUFSIZ) {
+               if (ctrl->bRequestType & USB_DIR_IN) {
+                       /* Cast away the const, we are going to overwrite on purpose. */
+                       __le16 *temp = (__le16 *)&ctrl->wLength;
+                       *temp = cpu_to_le16(USB_BUFSIZ);
+                       w_length = USB_BUFSIZ;

Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ?

Quoted 3 lines

Where do such long packets come from ?

What is the test-case ?

The USB host will not attempt to retrieve more than wLenght bytes during transfer phase.
If the device would erroneously attempt to provide more data it would result in an unexpected state.
In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Is there anything additional required from my side?

Sorry for the delay, I am still processing outstanding email.

Could you please review this patch?

I have it (or rather, one outstanding reply) in the pipeline, sorry for the delay.