aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2016-12-09 15:24:24 -0500
committerFelipe Balbi <felipe.balbi@linux.intel.com>2017-01-02 03:55:28 -0500
commit1c069b057dcf64fada952eaa868d35f02bb0cfc2 (patch)
treead1be498fb99e8c558475dc03fa3474bb81ba400
parentadd333a81a16abbd4f106266a2553677a165725f (diff)
USB: gadgetfs: fix checks of wTotalLength in config descriptors
Andrey Konovalov's fuzz testing of gadgetfs showed that we should improve the driver's checks for valid configuration descriptors passed in by the user. In particular, the driver needs to verify that the wTotalLength value in the descriptor is not too short (smaller than USB_DT_CONFIG_SIZE). And the check for whether wTotalLength is too large has to be changed, because the driver assumes there is always enough room remaining in the buffer to hold a device descriptor (at least USB_DT_DEVICE_SIZE bytes). This patch adds the additional check and fixes the existing check. It may do a little more than strictly necessary, but one extra check won't hurt. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> CC: Andrey Konovalov <andreyknvl@google.com> CC: <stable@vger.kernel.org> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
-rw-r--r--drivers/usb/gadget/legacy/inode.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index f1ca33942607..08e5ecc05079 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1734,10 +1734,12 @@ static struct usb_gadget_driver gadgetfs_driver = {
1734 * such as configuration notifications. 1734 * such as configuration notifications.
1735 */ 1735 */
1736 1736
1737static int is_valid_config (struct usb_config_descriptor *config) 1737static int is_valid_config(struct usb_config_descriptor *config,
1738 unsigned int total)
1738{ 1739{
1739 return config->bDescriptorType == USB_DT_CONFIG 1740 return config->bDescriptorType == USB_DT_CONFIG
1740 && config->bLength == USB_DT_CONFIG_SIZE 1741 && config->bLength == USB_DT_CONFIG_SIZE
1742 && total >= USB_DT_CONFIG_SIZE
1741 && config->bConfigurationValue != 0 1743 && config->bConfigurationValue != 0
1742 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0 1744 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1743 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0; 1745 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
@@ -1787,7 +1789,8 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1787 /* full or low speed config */ 1789 /* full or low speed config */
1788 dev->config = (void *) kbuf; 1790 dev->config = (void *) kbuf;
1789 total = le16_to_cpu(dev->config->wTotalLength); 1791 total = le16_to_cpu(dev->config->wTotalLength);
1790 if (!is_valid_config (dev->config) || total >= length) 1792 if (!is_valid_config(dev->config, total) ||
1793 total > length - USB_DT_DEVICE_SIZE)
1791 goto fail; 1794 goto fail;
1792 kbuf += total; 1795 kbuf += total;
1793 length -= total; 1796 length -= total;
@@ -1796,7 +1799,8 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1796 if (kbuf [1] == USB_DT_CONFIG) { 1799 if (kbuf [1] == USB_DT_CONFIG) {
1797 dev->hs_config = (void *) kbuf; 1800 dev->hs_config = (void *) kbuf;
1798 total = le16_to_cpu(dev->hs_config->wTotalLength); 1801 total = le16_to_cpu(dev->hs_config->wTotalLength);
1799 if (!is_valid_config (dev->hs_config) || total >= length) 1802 if (!is_valid_config(dev->hs_config, total) ||
1803 total > length - USB_DT_DEVICE_SIZE)
1800 goto fail; 1804 goto fail;
1801 kbuf += total; 1805 kbuf += total;
1802 length -= total; 1806 length -= total;