descriptor.c: Fix buffer read overflow caught by valgrind
authorVitali Lovich <vlovich@aliph.com>
Thu, 17 Mar 2011 00:33:57 +0000 (17:33 -0700)
committerPeter Stuge <peter@stuge.se>
Sun, 24 Jul 2011 20:29:09 +0000 (22:29 +0200)
In parse_interface() an unexpected descriptor would be parsed without
validating the descriptor's length.  It is possible for size to be 0 at
this point, which means that the parsing would read past the end of the
source buffer.  Fix #83 by checking the length of the remaining buffer
before parsing.

libusb/descriptor.c

index 11480e8..d6ec46c 100644 (file)
@@ -257,11 +257,13 @@ static int parse_interface(libusb_context *ctx,
                }
 
                /* Did we hit an unexpected descriptor? */
-               usbi_parse_descriptor(buffer, "bb", &header, 0);
-               if ((size >= DESC_HEADER_LENGTH) &&
-                               ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
-                                (header.bDescriptorType == LIBUSB_DT_DEVICE)))
-                       return parsed;
+               if (size >= DESC_HEADER_LENGTH) {
+                       usbi_parse_descriptor(buffer, "bb", &header, 0);
+                       if ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
+                           (header.bDescriptorType == LIBUSB_DT_DEVICE)) {
+                               return parsed;
+                       }
+               }
 
                if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
                        usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);