Skip to content

Commit 57999d1

Browse files
Dan Carpentergregkh
authored andcommitted
USB: devio: Prevent integer overflow in proc_do_submiturb()
There used to be an integer overflow check in proc_do_submiturb() but we removed it. It turns out that it's still required. The uurb->buffer_length variable is a signed integer and it's controlled by the user. It can lead to an integer overflow when we do: num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); If we strip away the macro then that line looks like this: num_sgs = (uurb->buffer_length + USB_SG_SIZE - 1) / USB_SG_SIZE; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ It's the first addition which can overflow. Fixes: 1129d27 ("USB: Increase usbfs transfer limit") Cc: stable <[email protected]> Signed-off-by: Dan Carpenter <[email protected]> Acked-by: Alan Stern <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1fbbb78 commit 57999d1

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

drivers/usb/core/devio.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ module_param(usbfs_memory_mb, uint, 0644);
140140
MODULE_PARM_DESC(usbfs_memory_mb,
141141
"maximum MB allowed for usbfs buffers (0 = no limit)");
142142

143+
/* Hard limit, necessary to avoid arithmetic overflow */
144+
#define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
145+
143146
static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
144147

145148
/* Check whether it's okay to allocate more memory for a transfer */
@@ -1460,6 +1463,8 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb
14601463
USBDEVFS_URB_ZERO_PACKET |
14611464
USBDEVFS_URB_NO_INTERRUPT))
14621465
return -EINVAL;
1466+
if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX)
1467+
return -EINVAL;
14631468
if (uurb->buffer_length > 0 && !uurb->buffer)
14641469
return -EINVAL;
14651470
if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&

0 commit comments

Comments
 (0)