Skip to content

Commit a368ecd

Browse files
AlanSterngregkh
authored andcommitted
USB: core: Fix duplicate endpoint bug by clearing reserved bits in the descriptor
Syzbot has identified a bug in usbcore (see the Closes: tag below) caused by our assumption that the reserved bits in an endpoint descriptor's bEndpointAddress field will always be 0. As a result of the bug, the endpoint_is_duplicate() routine in config.c (and possibly other routines as well) may believe that two descriptors are for distinct endpoints, even though they have the same direction and endpoint number. This can lead to confusion, including the bug identified by syzbot (two descriptors with matching endpoint numbers and directions, where one was interrupt and the other was bulk). To fix the bug, we will clear the reserved bits in bEndpointAddress when we parse the descriptor. (Note that both the USB-2.0 and USB-3.1 specs say these bits are "Reserved, reset to zero".) This requires us to make a copy of the descriptor earlier in usb_parse_endpoint() and use the copy instead of the original when checking for duplicates. Signed-off-by: Alan Stern <[email protected]> Reported-and-tested-by: [email protected] Closes: https://lore.kernel.org/linux-usb/[email protected]/ Fixes: 0a8fd13 ("USB: fix problems with duplicate endpoint addresses") CC: Oliver Neukum <[email protected]> CC: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 79989bd commit a368ecd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

drivers/usb/core/config.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno,
291291
if (ifp->desc.bNumEndpoints >= num_ep)
292292
goto skip_to_next_endpoint_or_interface_descriptor;
293293

294+
/* Save a copy of the descriptor and use it instead of the original */
295+
endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
296+
memcpy(&endpoint->desc, d, n);
297+
d = &endpoint->desc;
298+
299+
/* Clear the reserved bits in bEndpointAddress */
300+
i = d->bEndpointAddress &
301+
(USB_ENDPOINT_DIR_MASK | USB_ENDPOINT_NUMBER_MASK);
302+
if (i != d->bEndpointAddress) {
303+
dev_notice(ddev, "config %d interface %d altsetting %d has an endpoint descriptor with address 0x%X, changing to 0x%X\n",
304+
cfgno, inum, asnum, d->bEndpointAddress, i);
305+
endpoint->desc.bEndpointAddress = i;
306+
}
307+
294308
/* Check for duplicate endpoint addresses */
295309
if (config_endpoint_is_duplicate(config, inum, asnum, d)) {
296310
dev_notice(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
@@ -308,10 +322,8 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno,
308322
}
309323
}
310324

311-
endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
325+
/* Accept this endpoint */
312326
++ifp->desc.bNumEndpoints;
313-
314-
memcpy(&endpoint->desc, d, n);
315327
INIT_LIST_HEAD(&endpoint->urb_list);
316328

317329
/*

0 commit comments

Comments
 (0)