Skip to content

Commit 786de92

Browse files
AlanSterngregkh
authored andcommitted
USB: uas: fix bug in handling of alternate settings
The uas driver has a subtle bug in the way it handles alternate settings. The uas_find_uas_alt_setting() routine returns an altsetting value (the bAlternateSetting number in the descriptor), but uas_use_uas_driver() then treats that value as an index to the intf->altsetting array, which it isn't. Normally this doesn't cause any problems because the various alternate settings have bAlternateSetting values 0, 1, 2, ..., so the value is equal to the index in the array. But this is not guaranteed, and Andrey Konovalov used the syzkaller fuzzer with KASAN to get a slab-out-of-bounds error by violating this assumption. This patch fixes the bug by making uas_find_uas_alt_setting() return a pointer to the altsetting entry rather than either the value or the index. Pointers are less subject to misinterpretation. Signed-off-by: Alan Stern <[email protected]> Reported-by: Andrey Konovalov <[email protected]> Tested-by: Andrey Konovalov <[email protected]> CC: Oliver Neukum <[email protected]> CC: <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 113f6eb commit 786de92

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

drivers/usb/storage/uas-detect.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ static int uas_is_interface(struct usb_host_interface *intf)
99
intf->desc.bInterfaceProtocol == USB_PR_UAS);
1010
}
1111

12-
static int uas_find_uas_alt_setting(struct usb_interface *intf)
12+
static struct usb_host_interface *uas_find_uas_alt_setting(
13+
struct usb_interface *intf)
1314
{
1415
int i;
1516

1617
for (i = 0; i < intf->num_altsetting; i++) {
1718
struct usb_host_interface *alt = &intf->altsetting[i];
1819

1920
if (uas_is_interface(alt))
20-
return alt->desc.bAlternateSetting;
21+
return alt;
2122
}
2223

23-
return -ENODEV;
24+
return NULL;
2425
}
2526

2627
static int uas_find_endpoints(struct usb_host_interface *alt,
@@ -58,14 +59,14 @@ static int uas_use_uas_driver(struct usb_interface *intf,
5859
struct usb_device *udev = interface_to_usbdev(intf);
5960
struct usb_hcd *hcd = bus_to_hcd(udev->bus);
6061
unsigned long flags = id->driver_info;
61-
int r, alt;
62-
62+
struct usb_host_interface *alt;
63+
int r;
6364

6465
alt = uas_find_uas_alt_setting(intf);
65-
if (alt < 0)
66+
if (!alt)
6667
return 0;
6768

68-
r = uas_find_endpoints(&intf->altsetting[alt], eps);
69+
r = uas_find_endpoints(alt, eps);
6970
if (r < 0)
7071
return 0;
7172

drivers/usb/storage/uas.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,14 @@ MODULE_DEVICE_TABLE(usb, uas_usb_ids);
873873
static int uas_switch_interface(struct usb_device *udev,
874874
struct usb_interface *intf)
875875
{
876-
int alt;
876+
struct usb_host_interface *alt;
877877

878878
alt = uas_find_uas_alt_setting(intf);
879-
if (alt < 0)
880-
return alt;
879+
if (!alt)
880+
return -ENODEV;
881881

882-
return usb_set_interface(udev,
883-
intf->altsetting[0].desc.bInterfaceNumber, alt);
882+
return usb_set_interface(udev, alt->desc.bInterfaceNumber,
883+
alt->desc.bAlternateSetting);
884884
}
885885

886886
static int uas_configure_endpoints(struct uas_dev_info *devinfo)

0 commit comments

Comments
 (0)