OSDN Git Service

USB: core: prevent malicious bNumInterfaces overflow
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / usb / core / config.c
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/usb/hcd.h>
4 #include <linux/usb/quirks.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
9 #include "usb.h"
10
11
12 #define USB_MAXALTSETTING               128     /* Hard limit */
13
14 #define USB_MAXCONFIG                   8       /* Arbitrary limit */
15
16
17 static inline const char *plural(int n)
18 {
19         return (n == 1 ? "" : "s");
20 }
21
22 static int find_next_descriptor(unsigned char *buffer, int size,
23     int dt1, int dt2, int *num_skipped)
24 {
25         struct usb_descriptor_header *h;
26         int n = 0;
27         unsigned char *buffer0 = buffer;
28
29         /* Find the next descriptor of type dt1 or dt2 */
30         while (size > 0) {
31                 h = (struct usb_descriptor_header *) buffer;
32                 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
33                         break;
34                 buffer += h->bLength;
35                 size -= h->bLength;
36                 ++n;
37         }
38
39         /* Store the number of descriptors skipped and return the
40          * number of bytes skipped */
41         if (num_skipped)
42                 *num_skipped = n;
43         return buffer - buffer0;
44 }
45
46 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
47                 int inum, int asnum, struct usb_host_endpoint *ep,
48                 unsigned char *buffer, int size)
49 {
50         struct usb_ss_ep_comp_descriptor *desc;
51         int max_tx;
52
53         /* The SuperSpeed endpoint companion descriptor is supposed to
54          * be the first thing immediately following the endpoint descriptor.
55          */
56         desc = (struct usb_ss_ep_comp_descriptor *) buffer;
57         if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
58                         size < USB_DT_SS_EP_COMP_SIZE) {
59                 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
60                                 " interface %d altsetting %d ep %d: "
61                                 "using minimum values\n",
62                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
63
64                 /* Fill in some default values.
65                  * Leave bmAttributes as zero, which will mean no streams for
66                  * bulk, and isoc won't support multiple bursts of packets.
67                  * With bursts of only one packet, and a Mult of 1, the max
68                  * amount of data moved per endpoint service interval is one
69                  * packet.
70                  */
71                 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
72                 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
73                 if (usb_endpoint_xfer_isoc(&ep->desc) ||
74                                 usb_endpoint_xfer_int(&ep->desc))
75                         ep->ss_ep_comp.wBytesPerInterval =
76                                         ep->desc.wMaxPacketSize;
77                 return;
78         }
79
80         memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
81
82         /* Check the various values */
83         if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
84                 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
85                                 "config %d interface %d altsetting %d ep %d: "
86                                 "setting to zero\n", desc->bMaxBurst,
87                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
88                 ep->ss_ep_comp.bMaxBurst = 0;
89         } else if (desc->bMaxBurst > 15) {
90                 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
91                                 "config %d interface %d altsetting %d ep %d: "
92                                 "setting to 15\n", desc->bMaxBurst,
93                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
94                 ep->ss_ep_comp.bMaxBurst = 15;
95         }
96
97         if ((usb_endpoint_xfer_control(&ep->desc) ||
98                         usb_endpoint_xfer_int(&ep->desc)) &&
99                                 desc->bmAttributes != 0) {
100                 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
101                                 "config %d interface %d altsetting %d ep %d: "
102                                 "setting to zero\n",
103                                 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
104                                 desc->bmAttributes,
105                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
106                 ep->ss_ep_comp.bmAttributes = 0;
107         } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
108                         desc->bmAttributes > 16) {
109                 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
110                                 "config %d interface %d altsetting %d ep %d: "
111                                 "setting to max\n",
112                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
113                 ep->ss_ep_comp.bmAttributes = 16;
114         } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
115                    USB_SS_MULT(desc->bmAttributes) > 3) {
116                 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
117                                 "config %d interface %d altsetting %d ep %d: "
118                                 "setting to 3\n",
119                                 USB_SS_MULT(desc->bmAttributes),
120                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
121                 ep->ss_ep_comp.bmAttributes = 2;
122         }
123
124         if (usb_endpoint_xfer_isoc(&ep->desc))
125                 max_tx = (desc->bMaxBurst + 1) *
126                         (USB_SS_MULT(desc->bmAttributes)) *
127                         usb_endpoint_maxp(&ep->desc);
128         else if (usb_endpoint_xfer_int(&ep->desc))
129                 max_tx = usb_endpoint_maxp(&ep->desc) *
130                         (desc->bMaxBurst + 1);
131         else
132                 max_tx = 999999;
133         if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
134                 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
135                                 "config %d interface %d altsetting %d ep %d: "
136                                 "setting to %d\n",
137                                 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
138                                 le16_to_cpu(desc->wBytesPerInterval),
139                                 cfgno, inum, asnum, ep->desc.bEndpointAddress,
140                                 max_tx);
141                 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
142         }
143 }
144
145 static const unsigned short low_speed_maxpacket_maxes[4] = {
146         [USB_ENDPOINT_XFER_CONTROL] = 8,
147         [USB_ENDPOINT_XFER_ISOC] = 0,
148         [USB_ENDPOINT_XFER_BULK] = 0,
149         [USB_ENDPOINT_XFER_INT] = 8,
150 };
151 static const unsigned short full_speed_maxpacket_maxes[4] = {
152         [USB_ENDPOINT_XFER_CONTROL] = 64,
153         [USB_ENDPOINT_XFER_ISOC] = 1023,
154         [USB_ENDPOINT_XFER_BULK] = 64,
155         [USB_ENDPOINT_XFER_INT] = 64,
156 };
157 static const unsigned short high_speed_maxpacket_maxes[4] = {
158         [USB_ENDPOINT_XFER_CONTROL] = 64,
159         [USB_ENDPOINT_XFER_ISOC] = 1024,
160         [USB_ENDPOINT_XFER_BULK] = 512,
161         [USB_ENDPOINT_XFER_INT] = 1024,
162 };
163 static const unsigned short super_speed_maxpacket_maxes[4] = {
164         [USB_ENDPOINT_XFER_CONTROL] = 512,
165         [USB_ENDPOINT_XFER_ISOC] = 1024,
166         [USB_ENDPOINT_XFER_BULK] = 1024,
167         [USB_ENDPOINT_XFER_INT] = 1024,
168 };
169
170 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
171     int asnum, struct usb_host_interface *ifp, int num_ep,
172     unsigned char *buffer, int size)
173 {
174         unsigned char *buffer0 = buffer;
175         struct usb_endpoint_descriptor *d;
176         struct usb_host_endpoint *endpoint;
177         int n, i, j, retval;
178         unsigned int maxp;
179         const unsigned short *maxpacket_maxes;
180
181         d = (struct usb_endpoint_descriptor *) buffer;
182         buffer += d->bLength;
183         size -= d->bLength;
184
185         if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
186                 n = USB_DT_ENDPOINT_AUDIO_SIZE;
187         else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
188                 n = USB_DT_ENDPOINT_SIZE;
189         else {
190                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
191                     "invalid endpoint descriptor of length %d, skipping\n",
192                     cfgno, inum, asnum, d->bLength);
193                 goto skip_to_next_endpoint_or_interface_descriptor;
194         }
195
196         i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
197         if (i >= 16 || i == 0) {
198                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
199                     "invalid endpoint with address 0x%X, skipping\n",
200                     cfgno, inum, asnum, d->bEndpointAddress);
201                 goto skip_to_next_endpoint_or_interface_descriptor;
202         }
203
204         /* Only store as many endpoints as we have room for */
205         if (ifp->desc.bNumEndpoints >= num_ep)
206                 goto skip_to_next_endpoint_or_interface_descriptor;
207
208         /* Check for duplicate endpoint addresses */
209         for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
210                 if (ifp->endpoint[i].desc.bEndpointAddress ==
211                     d->bEndpointAddress) {
212                         dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
213                             cfgno, inum, asnum, d->bEndpointAddress);
214                         goto skip_to_next_endpoint_or_interface_descriptor;
215                 }
216         }
217
218         endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
219         ++ifp->desc.bNumEndpoints;
220
221         memcpy(&endpoint->desc, d, n);
222         INIT_LIST_HEAD(&endpoint->urb_list);
223
224         /*
225          * Fix up bInterval values outside the legal range.
226          * Use 10 or 8 ms if no proper value can be guessed.
227          */
228         i = 0;          /* i = min, j = max, n = default */
229         j = 255;
230         if (usb_endpoint_xfer_int(d)) {
231                 i = 1;
232                 switch (to_usb_device(ddev)->speed) {
233                 case USB_SPEED_SUPER_PLUS:
234                 case USB_SPEED_SUPER:
235                 case USB_SPEED_HIGH:
236                         /*
237                          * Many device manufacturers are using full-speed
238                          * bInterval values in high-speed interrupt endpoint
239                          * descriptors. Try to fix those and fall back to an
240                          * 8-ms default value otherwise.
241                          */
242                         n = fls(d->bInterval*8);
243                         if (n == 0)
244                                 n = 7;  /* 8 ms = 2^(7-1) uframes */
245                         j = 16;
246
247                         /*
248                          * Adjust bInterval for quirked devices.
249                          */
250                         /*
251                          * This quirk fixes bIntervals reported in ms.
252                          */
253                         if (to_usb_device(ddev)->quirks &
254                                 USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
255                                 n = clamp(fls(d->bInterval) + 3, i, j);
256                                 i = j = n;
257                         }
258                         /*
259                          * This quirk fixes bIntervals reported in
260                          * linear microframes.
261                          */
262                         if (to_usb_device(ddev)->quirks &
263                                 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
264                                 n = clamp(fls(d->bInterval), i, j);
265                                 i = j = n;
266                         }
267                         break;
268                 default:                /* USB_SPEED_FULL or _LOW */
269                         /*
270                          * For low-speed, 10 ms is the official minimum.
271                          * But some "overclocked" devices might want faster
272                          * polling so we'll allow it.
273                          */
274                         n = 10;
275                         break;
276                 }
277         } else if (usb_endpoint_xfer_isoc(d)) {
278                 i = 1;
279                 j = 16;
280                 switch (to_usb_device(ddev)->speed) {
281                 case USB_SPEED_HIGH:
282                         n = 7;          /* 8 ms = 2^(7-1) uframes */
283                         break;
284                 default:                /* USB_SPEED_FULL */
285                         n = 4;          /* 8 ms = 2^(4-1) frames */
286                         break;
287                 }
288         }
289         if (d->bInterval < i || d->bInterval > j) {
290                 dev_warn(ddev, "config %d interface %d altsetting %d "
291                     "endpoint 0x%X has an invalid bInterval %d, "
292                     "changing to %d\n",
293                     cfgno, inum, asnum,
294                     d->bEndpointAddress, d->bInterval, n);
295                 endpoint->desc.bInterval = n;
296         }
297
298         /* Some buggy low-speed devices have Bulk endpoints, which is
299          * explicitly forbidden by the USB spec.  In an attempt to make
300          * them usable, we will try treating them as Interrupt endpoints.
301          */
302         if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
303                         usb_endpoint_xfer_bulk(d)) {
304                 dev_warn(ddev, "config %d interface %d altsetting %d "
305                     "endpoint 0x%X is Bulk; changing to Interrupt\n",
306                     cfgno, inum, asnum, d->bEndpointAddress);
307                 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
308                 endpoint->desc.bInterval = 1;
309                 if (usb_endpoint_maxp(&endpoint->desc) > 8)
310                         endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
311         }
312
313         /* Validate the wMaxPacketSize field */
314         maxp = usb_endpoint_maxp(&endpoint->desc);
315
316         /* Find the highest legal maxpacket size for this endpoint */
317         i = 0;          /* additional transactions per microframe */
318         switch (to_usb_device(ddev)->speed) {
319         case USB_SPEED_LOW:
320                 maxpacket_maxes = low_speed_maxpacket_maxes;
321                 break;
322         case USB_SPEED_FULL:
323                 maxpacket_maxes = full_speed_maxpacket_maxes;
324                 break;
325         case USB_SPEED_HIGH:
326                 /* Bits 12..11 are allowed only for HS periodic endpoints */
327                 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
328                         i = maxp & (BIT(12) | BIT(11));
329                         maxp &= ~i;
330                 }
331                 /* fallthrough */
332         default:
333                 maxpacket_maxes = high_speed_maxpacket_maxes;
334                 break;
335         case USB_SPEED_SUPER:
336         case USB_SPEED_SUPER_PLUS:
337                 maxpacket_maxes = super_speed_maxpacket_maxes;
338                 break;
339         }
340         j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
341
342         if (maxp > j) {
343                 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
344                     cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
345                 maxp = j;
346                 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
347         }
348
349         /*
350          * Some buggy high speed devices have bulk endpoints using
351          * maxpacket sizes other than 512.  High speed HCDs may not
352          * be able to handle that particular bug, so let's warn...
353          */
354         if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
355                         && usb_endpoint_xfer_bulk(d)) {
356                 if (maxp != 512)
357                         dev_warn(ddev, "config %d interface %d altsetting %d "
358                                 "bulk endpoint 0x%X has invalid maxpacket %d\n",
359                                 cfgno, inum, asnum, d->bEndpointAddress,
360                                 maxp);
361         }
362
363         /* Parse a possible SuperSpeed endpoint companion descriptor */
364         if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
365                 usb_parse_ss_endpoint_companion(ddev, cfgno,
366                                 inum, asnum, endpoint, buffer, size);
367
368         /* Skip over any Class Specific or Vendor Specific descriptors;
369          * find the next endpoint or interface descriptor */
370         endpoint->extra = buffer;
371         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
372                         USB_DT_INTERFACE, &n);
373         endpoint->extralen = i;
374         retval = buffer - buffer0 + i;
375         if (n > 0)
376                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
377                     n, plural(n), "endpoint");
378         return retval;
379
380 skip_to_next_endpoint_or_interface_descriptor:
381         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
382             USB_DT_INTERFACE, NULL);
383         return buffer - buffer0 + i;
384 }
385
386 void usb_release_interface_cache(struct kref *ref)
387 {
388         struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
389         int j;
390
391         for (j = 0; j < intfc->num_altsetting; j++) {
392                 struct usb_host_interface *alt = &intfc->altsetting[j];
393
394                 kfree(alt->endpoint);
395                 kfree(alt->string);
396         }
397         kfree(intfc);
398 }
399
400 static int usb_parse_interface(struct device *ddev, int cfgno,
401     struct usb_host_config *config, unsigned char *buffer, int size,
402     u8 inums[], u8 nalts[])
403 {
404         unsigned char *buffer0 = buffer;
405         struct usb_interface_descriptor *d;
406         int inum, asnum;
407         struct usb_interface_cache *intfc;
408         struct usb_host_interface *alt;
409         int i, n;
410         int len, retval;
411         int num_ep, num_ep_orig;
412
413         d = (struct usb_interface_descriptor *) buffer;
414         buffer += d->bLength;
415         size -= d->bLength;
416
417         if (d->bLength < USB_DT_INTERFACE_SIZE)
418                 goto skip_to_next_interface_descriptor;
419
420         /* Which interface entry is this? */
421         intfc = NULL;
422         inum = d->bInterfaceNumber;
423         for (i = 0; i < config->desc.bNumInterfaces; ++i) {
424                 if (inums[i] == inum) {
425                         intfc = config->intf_cache[i];
426                         break;
427                 }
428         }
429         if (!intfc || intfc->num_altsetting >= nalts[i])
430                 goto skip_to_next_interface_descriptor;
431
432         /* Check for duplicate altsetting entries */
433         asnum = d->bAlternateSetting;
434         for ((i = 0, alt = &intfc->altsetting[0]);
435               i < intfc->num_altsetting;
436              (++i, ++alt)) {
437                 if (alt->desc.bAlternateSetting == asnum) {
438                         dev_warn(ddev, "Duplicate descriptor for config %d "
439                             "interface %d altsetting %d, skipping\n",
440                             cfgno, inum, asnum);
441                         goto skip_to_next_interface_descriptor;
442                 }
443         }
444
445         ++intfc->num_altsetting;
446         memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
447
448         /* Skip over any Class Specific or Vendor Specific descriptors;
449          * find the first endpoint or interface descriptor */
450         alt->extra = buffer;
451         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
452             USB_DT_INTERFACE, &n);
453         alt->extralen = i;
454         if (n > 0)
455                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
456                     n, plural(n), "interface");
457         buffer += i;
458         size -= i;
459
460         /* Allocate space for the right(?) number of endpoints */
461         num_ep = num_ep_orig = alt->desc.bNumEndpoints;
462         alt->desc.bNumEndpoints = 0;            /* Use as a counter */
463         if (num_ep > USB_MAXENDPOINTS) {
464                 dev_warn(ddev, "too many endpoints for config %d interface %d "
465                     "altsetting %d: %d, using maximum allowed: %d\n",
466                     cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
467                 num_ep = USB_MAXENDPOINTS;
468         }
469
470         if (num_ep > 0) {
471                 /* Can't allocate 0 bytes */
472                 len = sizeof(struct usb_host_endpoint) * num_ep;
473                 alt->endpoint = kzalloc(len, GFP_KERNEL);
474                 if (!alt->endpoint)
475                         return -ENOMEM;
476         }
477
478         /* Parse all the endpoint descriptors */
479         n = 0;
480         while (size > 0) {
481                 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
482                      == USB_DT_INTERFACE)
483                         break;
484                 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
485                     num_ep, buffer, size);
486                 if (retval < 0)
487                         return retval;
488                 ++n;
489
490                 buffer += retval;
491                 size -= retval;
492         }
493
494         if (n != num_ep_orig)
495                 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
496                     "endpoint descriptor%s, different from the interface "
497                     "descriptor's value: %d\n",
498                     cfgno, inum, asnum, n, plural(n), num_ep_orig);
499         return buffer - buffer0;
500
501 skip_to_next_interface_descriptor:
502         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
503             USB_DT_INTERFACE, NULL);
504         return buffer - buffer0 + i;
505 }
506
507 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
508     struct usb_host_config *config, unsigned char *buffer, int size)
509 {
510         struct device *ddev = &dev->dev;
511         unsigned char *buffer0 = buffer;
512         int cfgno;
513         int nintf, nintf_orig;
514         int i, j, n;
515         struct usb_interface_cache *intfc;
516         unsigned char *buffer2;
517         int size2;
518         struct usb_descriptor_header *header;
519         int len, retval;
520         u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
521         unsigned iad_num = 0;
522
523         memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
524         nintf = nintf_orig = config->desc.bNumInterfaces;
525         config->desc.bNumInterfaces = 0;        // Adjusted later
526
527         if (config->desc.bDescriptorType != USB_DT_CONFIG ||
528             config->desc.bLength < USB_DT_CONFIG_SIZE ||
529             config->desc.bLength > size) {
530                 dev_err(ddev, "invalid descriptor for config index %d: "
531                     "type = 0x%X, length = %d\n", cfgidx,
532                     config->desc.bDescriptorType, config->desc.bLength);
533                 return -EINVAL;
534         }
535         cfgno = config->desc.bConfigurationValue;
536
537         buffer += config->desc.bLength;
538         size -= config->desc.bLength;
539
540         if (nintf > USB_MAXINTERFACES) {
541                 dev_warn(ddev, "config %d has too many interfaces: %d, "
542                     "using maximum allowed: %d\n",
543                     cfgno, nintf, USB_MAXINTERFACES);
544                 nintf = USB_MAXINTERFACES;
545         }
546
547         /* Go through the descriptors, checking their length and counting the
548          * number of altsettings for each interface */
549         n = 0;
550         for ((buffer2 = buffer, size2 = size);
551               size2 > 0;
552              (buffer2 += header->bLength, size2 -= header->bLength)) {
553
554                 if (size2 < sizeof(struct usb_descriptor_header)) {
555                         dev_warn(ddev, "config %d descriptor has %d excess "
556                             "byte%s, ignoring\n",
557                             cfgno, size2, plural(size2));
558                         break;
559                 }
560
561                 header = (struct usb_descriptor_header *) buffer2;
562                 if ((header->bLength > size2) || (header->bLength < 2)) {
563                         dev_warn(ddev, "config %d has an invalid descriptor "
564                             "of length %d, skipping remainder of the config\n",
565                             cfgno, header->bLength);
566                         break;
567                 }
568
569                 if (header->bDescriptorType == USB_DT_INTERFACE) {
570                         struct usb_interface_descriptor *d;
571                         int inum;
572
573                         d = (struct usb_interface_descriptor *) header;
574                         if (d->bLength < USB_DT_INTERFACE_SIZE) {
575                                 dev_warn(ddev, "config %d has an invalid "
576                                     "interface descriptor of length %d, "
577                                     "skipping\n", cfgno, d->bLength);
578                                 continue;
579                         }
580
581                         inum = d->bInterfaceNumber;
582
583                         if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
584                             n >= nintf_orig) {
585                                 dev_warn(ddev, "config %d has more interface "
586                                     "descriptors, than it declares in "
587                                     "bNumInterfaces, ignoring interface "
588                                     "number: %d\n", cfgno, inum);
589                                 continue;
590                         }
591
592                         if (inum >= nintf_orig)
593                                 dev_warn(ddev, "config %d has an invalid "
594                                     "interface number: %d but max is %d\n",
595                                     cfgno, inum, nintf_orig - 1);
596
597                         /* Have we already encountered this interface?
598                          * Count its altsettings */
599                         for (i = 0; i < n; ++i) {
600                                 if (inums[i] == inum)
601                                         break;
602                         }
603                         if (i < n) {
604                                 if (nalts[i] < 255)
605                                         ++nalts[i];
606                         } else if (n < USB_MAXINTERFACES) {
607                                 inums[n] = inum;
608                                 nalts[n] = 1;
609                                 ++n;
610                         }
611
612                 } else if (header->bDescriptorType ==
613                                 USB_DT_INTERFACE_ASSOCIATION) {
614                         struct usb_interface_assoc_descriptor *d;
615
616                         d = (struct usb_interface_assoc_descriptor *)header;
617                         if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
618                                 dev_warn(ddev,
619                                          "config %d has an invalid interface association descriptor of length %d, skipping\n",
620                                          cfgno, d->bLength);
621                                 continue;
622                         }
623
624                         if (iad_num == USB_MAXIADS) {
625                                 dev_warn(ddev, "found more Interface "
626                                                "Association Descriptors "
627                                                "than allocated for in "
628                                                "configuration %d\n", cfgno);
629                         } else {
630                                 config->intf_assoc[iad_num] = d;
631                                 iad_num++;
632                         }
633
634                 } else if (header->bDescriptorType == USB_DT_DEVICE ||
635                             header->bDescriptorType == USB_DT_CONFIG)
636                         dev_warn(ddev, "config %d contains an unexpected "
637                             "descriptor of type 0x%X, skipping\n",
638                             cfgno, header->bDescriptorType);
639
640         }       /* for ((buffer2 = buffer, size2 = size); ...) */
641         size = buffer2 - buffer;
642         config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
643
644         if (n != nintf)
645                 dev_warn(ddev, "config %d has %d interface%s, different from "
646                     "the descriptor's value: %d\n",
647                     cfgno, n, plural(n), nintf_orig);
648         else if (n == 0)
649                 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
650         config->desc.bNumInterfaces = nintf = n;
651
652         /* Check for missing interface numbers */
653         for (i = 0; i < nintf; ++i) {
654                 for (j = 0; j < nintf; ++j) {
655                         if (inums[j] == i)
656                                 break;
657                 }
658                 if (j >= nintf)
659                         dev_warn(ddev, "config %d has no interface number "
660                             "%d\n", cfgno, i);
661         }
662
663         /* Allocate the usb_interface_caches and altsetting arrays */
664         for (i = 0; i < nintf; ++i) {
665                 j = nalts[i];
666                 if (j > USB_MAXALTSETTING) {
667                         dev_warn(ddev, "too many alternate settings for "
668                             "config %d interface %d: %d, "
669                             "using maximum allowed: %d\n",
670                             cfgno, inums[i], j, USB_MAXALTSETTING);
671                         nalts[i] = j = USB_MAXALTSETTING;
672                 }
673
674                 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
675                 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
676                 if (!intfc)
677                         return -ENOMEM;
678                 kref_init(&intfc->ref);
679         }
680
681         /* FIXME: parse the BOS descriptor */
682
683         /* Skip over any Class Specific or Vendor Specific descriptors;
684          * find the first interface descriptor */
685         config->extra = buffer;
686         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
687             USB_DT_INTERFACE, &n);
688         config->extralen = i;
689         if (n > 0)
690                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
691                     n, plural(n), "configuration");
692         buffer += i;
693         size -= i;
694
695         /* Parse all the interface/altsetting descriptors */
696         while (size > 0) {
697                 retval = usb_parse_interface(ddev, cfgno, config,
698                     buffer, size, inums, nalts);
699                 if (retval < 0)
700                         return retval;
701
702                 buffer += retval;
703                 size -= retval;
704         }
705
706         /* Check for missing altsettings */
707         for (i = 0; i < nintf; ++i) {
708                 intfc = config->intf_cache[i];
709                 for (j = 0; j < intfc->num_altsetting; ++j) {
710                         for (n = 0; n < intfc->num_altsetting; ++n) {
711                                 if (intfc->altsetting[n].desc.
712                                     bAlternateSetting == j)
713                                         break;
714                         }
715                         if (n >= intfc->num_altsetting)
716                                 dev_warn(ddev, "config %d interface %d has no "
717                                     "altsetting %d\n", cfgno, inums[i], j);
718                 }
719         }
720
721         return 0;
722 }
723
724 /* hub-only!! ... and only exported for reset/reinit path.
725  * otherwise used internally on disconnect/destroy path
726  */
727 void usb_destroy_configuration(struct usb_device *dev)
728 {
729         int c, i;
730
731         if (!dev->config)
732                 return;
733
734         if (dev->rawdescriptors) {
735                 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
736                         kfree(dev->rawdescriptors[i]);
737
738                 kfree(dev->rawdescriptors);
739                 dev->rawdescriptors = NULL;
740         }
741
742         for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
743                 struct usb_host_config *cf = &dev->config[c];
744
745                 kfree(cf->string);
746                 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
747                         if (cf->intf_cache[i])
748                                 kref_put(&cf->intf_cache[i]->ref,
749                                           usb_release_interface_cache);
750                 }
751         }
752         kfree(dev->config);
753         dev->config = NULL;
754 }
755
756
757 /*
758  * Get the USB config descriptors, cache and parse'em
759  *
760  * hub-only!! ... and only in reset path, or usb_new_device()
761  * (used by real hubs and virtual root hubs)
762  */
763 int usb_get_configuration(struct usb_device *dev)
764 {
765         struct device *ddev = &dev->dev;
766         int ncfg = dev->descriptor.bNumConfigurations;
767         int result = 0;
768         unsigned int cfgno, length;
769         unsigned char *bigbuffer;
770         struct usb_config_descriptor *desc;
771
772         cfgno = 0;
773         result = -ENOMEM;
774         if (ncfg > USB_MAXCONFIG) {
775                 dev_warn(ddev, "too many configurations: %d, "
776                     "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
777                 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
778         }
779
780         if (ncfg < 1) {
781                 dev_err(ddev, "no configurations\n");
782                 return -EINVAL;
783         }
784
785         length = ncfg * sizeof(struct usb_host_config);
786         dev->config = kzalloc(length, GFP_KERNEL);
787         if (!dev->config)
788                 goto err2;
789
790         length = ncfg * sizeof(char *);
791         dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
792         if (!dev->rawdescriptors)
793                 goto err2;
794
795         desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
796         if (!desc)
797                 goto err2;
798
799         result = 0;
800         for (; cfgno < ncfg; cfgno++) {
801                 /* We grab just the first descriptor so we know how long
802                  * the whole configuration is */
803                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
804                     desc, USB_DT_CONFIG_SIZE);
805                 if (result < 0) {
806                         dev_err(ddev, "unable to read config index %d "
807                             "descriptor/%s: %d\n", cfgno, "start", result);
808                         if (result != -EPIPE)
809                                 goto err;
810                         dev_err(ddev, "chopping to %d config(s)\n", cfgno);
811                         dev->descriptor.bNumConfigurations = cfgno;
812                         break;
813                 } else if (result < 4) {
814                         dev_err(ddev, "config index %d descriptor too short "
815                             "(expected %i, got %i)\n", cfgno,
816                             USB_DT_CONFIG_SIZE, result);
817                         result = -EINVAL;
818                         goto err;
819                 }
820                 length = max((int) le16_to_cpu(desc->wTotalLength),
821                     USB_DT_CONFIG_SIZE);
822
823                 /* Now that we know the length, get the whole thing */
824                 bigbuffer = kmalloc(length, GFP_KERNEL);
825                 if (!bigbuffer) {
826                         result = -ENOMEM;
827                         goto err;
828                 }
829
830                 if (dev->quirks & USB_QUIRK_DELAY_INIT)
831                         msleep(200);
832
833                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
834                     bigbuffer, length);
835                 if (result < 0) {
836                         dev_err(ddev, "unable to read config index %d "
837                             "descriptor/%s\n", cfgno, "all");
838                         kfree(bigbuffer);
839                         goto err;
840                 }
841                 if (result < length) {
842                         dev_warn(ddev, "config index %d descriptor too short "
843                             "(expected %i, got %i)\n", cfgno, length, result);
844                         length = result;
845                 }
846
847                 dev->rawdescriptors[cfgno] = bigbuffer;
848
849                 result = usb_parse_configuration(dev, cfgno,
850                     &dev->config[cfgno], bigbuffer, length);
851                 if (result < 0) {
852                         ++cfgno;
853                         goto err;
854                 }
855         }
856         result = 0;
857
858 err:
859         kfree(desc);
860         dev->descriptor.bNumConfigurations = cfgno;
861 err2:
862         if (result == -ENOMEM)
863                 dev_err(ddev, "out of memory\n");
864         return result;
865 }
866
867 void usb_release_bos_descriptor(struct usb_device *dev)
868 {
869         if (dev->bos) {
870                 kfree(dev->bos->desc);
871                 kfree(dev->bos);
872                 dev->bos = NULL;
873         }
874 }
875
876 static const __u8 bos_desc_len[256] = {
877         [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
878         [USB_CAP_TYPE_EXT]          = USB_DT_USB_EXT_CAP_SIZE,
879         [USB_SS_CAP_TYPE]           = USB_DT_USB_SS_CAP_SIZE,
880         [USB_SSP_CAP_TYPE]          = USB_DT_USB_SSP_CAP_SIZE(1),
881         [CONTAINER_ID_TYPE]         = USB_DT_USB_SS_CONTN_ID_SIZE,
882         [USB_PTM_CAP_TYPE]          = USB_DT_USB_PTM_ID_SIZE,
883 };
884
885 /* Get BOS descriptor set */
886 int usb_get_bos_descriptor(struct usb_device *dev)
887 {
888         struct device *ddev = &dev->dev;
889         struct usb_bos_descriptor *bos;
890         struct usb_dev_cap_header *cap;
891         struct usb_ssp_cap_descriptor *ssp_cap;
892         unsigned char *buffer;
893         int length, total_len, num, i, ssac;
894         __u8 cap_type;
895         int ret;
896
897         bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
898         if (!bos)
899                 return -ENOMEM;
900
901         /* Get BOS descriptor */
902         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
903         if (ret < USB_DT_BOS_SIZE) {
904                 dev_err(ddev, "unable to get BOS descriptor\n");
905                 if (ret >= 0)
906                         ret = -ENOMSG;
907                 kfree(bos);
908                 return ret;
909         }
910
911         length = bos->bLength;
912         total_len = le16_to_cpu(bos->wTotalLength);
913         num = bos->bNumDeviceCaps;
914         kfree(bos);
915         if (total_len < length)
916                 return -EINVAL;
917
918         dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
919         if (!dev->bos)
920                 return -ENOMEM;
921
922         /* Now let's get the whole BOS descriptor set */
923         buffer = kzalloc(total_len, GFP_KERNEL);
924         if (!buffer) {
925                 ret = -ENOMEM;
926                 goto err;
927         }
928         dev->bos->desc = (struct usb_bos_descriptor *)buffer;
929
930         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
931         if (ret < total_len) {
932                 dev_err(ddev, "unable to get BOS descriptor set\n");
933                 if (ret >= 0)
934                         ret = -ENOMSG;
935                 goto err;
936         }
937         total_len -= length;
938
939         for (i = 0; i < num; i++) {
940                 buffer += length;
941                 cap = (struct usb_dev_cap_header *)buffer;
942
943                 if (total_len < sizeof(*cap) || total_len < cap->bLength) {
944                         dev->bos->desc->bNumDeviceCaps = i;
945                         break;
946                 }
947                 cap_type = cap->bDevCapabilityType;
948                 length = cap->bLength;
949                 if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
950                         dev->bos->desc->bNumDeviceCaps = i;
951                         break;
952                 }
953
954                 total_len -= length;
955
956                 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
957                         dev_warn(ddev, "descriptor type invalid, skip\n");
958                         continue;
959                 }
960
961                 switch (cap_type) {
962                 case USB_CAP_TYPE_WIRELESS_USB:
963                         /* Wireless USB cap descriptor is handled by wusb */
964                         break;
965                 case USB_CAP_TYPE_EXT:
966                         dev->bos->ext_cap =
967                                 (struct usb_ext_cap_descriptor *)buffer;
968                         break;
969                 case USB_SS_CAP_TYPE:
970                         dev->bos->ss_cap =
971                                 (struct usb_ss_cap_descriptor *)buffer;
972                         break;
973                 case USB_SSP_CAP_TYPE:
974                         ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
975                         ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
976                                 USB_SSP_SUBLINK_SPEED_ATTRIBS) + 1;
977                         if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
978                                 dev->bos->ssp_cap = ssp_cap;
979                         break;
980                 case CONTAINER_ID_TYPE:
981                         dev->bos->ss_id =
982                                 (struct usb_ss_container_id_descriptor *)buffer;
983                         break;
984                 case USB_PTM_CAP_TYPE:
985                         dev->bos->ptm_cap =
986                                 (struct usb_ptm_cap_descriptor *)buffer;
987                 default:
988                         break;
989                 }
990         }
991
992         return 0;
993
994 err:
995         usb_release_bos_descriptor(dev);
996         return ret;
997 }