Skip to content

Commit f9a5b4f

Browse files
matnymangregkh
authored andcommitted
usb: Avoid use-after-free by flushing endpoints early in usb_set_interface()
The steps taken by usb core to set a new interface is very different from what is done on the xHC host side. xHC hardware will do everything in one go. One command is used to set up new endpoints, free old endpoints, check bandwidth, and run the new endpoints. All this is done by xHC when usb core asks the hcd to check for available bandwidth. At this point usb core has not yet flushed the old endpoints, which will cause use-after-free issues in xhci driver as queued URBs are cancelled on a re-allocated endpoint. To resolve this add a call to usb_disable_interface() which will flush the endpoints before calling usb_hcd_alloc_bandwidth() Additional checks in xhci driver will also be implemented to gracefully handle stale URB cancel on freed and re-allocated endpoints Cc: <[email protected]> Reported-by: Sudip Mukherjee <[email protected]> Signed-off-by: Mathias Nyman <[email protected]> Acked-by: Alan Stern <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d23df2d commit f9a5b4f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

drivers/usb/core/message.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ void usb_enable_interface(struct usb_device *dev,
13411341
* is submitted that needs that bandwidth. Some other operating systems
13421342
* allocate bandwidth early, when a configuration is chosen.
13431343
*
1344+
* xHCI reserves bandwidth and configures the alternate setting in
1345+
* usb_hcd_alloc_bandwidth(). If it fails the original interface altsetting
1346+
* may be disabled. Drivers cannot rely on any particular alternate
1347+
* setting being in effect after a failure.
1348+
*
13441349
* This call is synchronous, and may not be used in an interrupt context.
13451350
* Also, drivers must not change altsettings while urbs are scheduled for
13461351
* endpoints in that interface; all such urbs must first be completed
@@ -1376,6 +1381,12 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
13761381
alternate);
13771382
return -EINVAL;
13781383
}
1384+
/*
1385+
* usb3 hosts configure the interface in usb_hcd_alloc_bandwidth,
1386+
* including freeing dropped endpoint ring buffers.
1387+
* Make sure the interface endpoints are flushed before that
1388+
*/
1389+
usb_disable_interface(dev, iface, false);
13791390

13801391
/* Make sure we have enough bandwidth for this alternate interface.
13811392
* Remove the current alt setting and add the new alt setting.

0 commit comments

Comments
 (0)