Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

USB: core: Fix race by not overwriting udev->descriptor in hub_port_init()

Syzbot reported an out-of-bounds read in sysfs.c:read_descriptors():

BUG: KASAN: slab-out-of-bounds in read_descriptors+0x263/0x280 drivers/usb/core/sysfs.c:883
Read of size 8 at addr ffff88801e78b8c8 by task udevd/5011

CPU: 0 PID: 5011 Comm: udevd Not tainted 6.4.0-rc6-syzkaller-00195-g40f71e7cd3c6 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/27/2023
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106
print_address_description.constprop.0+0x2c/0x3c0 mm/kasan/report.c:351
print_report mm/kasan/report.c:462 [inline]
kasan_report+0x11c/0x130 mm/kasan/report.c:572
read_descriptors+0x263/0x280 drivers/usb/core/sysfs.c:883
...
Allocated by task 758:
...
__do_kmalloc_node mm/slab_common.c:966 [inline]
__kmalloc+0x5e/0x190 mm/slab_common.c:979
kmalloc include/linux/slab.h:563 [inline]
kzalloc include/linux/slab.h:680 [inline]
usb_get_configuration+0x1f7/0x5170 drivers/usb/core/config.c:887
usb_enumerate_device drivers/usb/core/hub.c:2407 [inline]
usb_new_device+0x12b0/0x19d0 drivers/usb/core/hub.c:2545

As analyzed by Khazhy Kumykov, the cause of this bug is a race between
read_descriptors() and hub_port_init(): The first routine uses a field
in udev->descriptor, not expecting it to change, while the second
overwrites it.

Prior to commit 45bf39f8df7f ("USB: core: Don't hold device lock while
reading the "descriptors" sysfs file") this race couldn't occur,
because the routines were mutually exclusive thanks to the device
locking. Removing that locking from read_descriptors() exposed it to
the race.

The best way to fix the bug is to keep hub_port_init() from changing
udev->descriptor once udev has been initialized and registered.
Drivers expect the descriptors stored in the kernel to be immutable;
we should not undermine this expectation. In fact, this change should
have been made long ago.

So now hub_port_init() will take an additional argument, specifying a
buffer in which to store the device descriptor it reads. (If udev has
not yet been initialized, the buffer pointer will be NULL and then
hub_port_init() will store the device descriptor in udev as before.)
This eliminates the data race responsible for the out-of-bounds read.

The changes to hub_port_init() appear more extensive than they really
are, because of indentation changes resulting from an attempt to avoid
writing to other parts of the usb_device structure after it has been
initialized. Similar changes should be made to the code that reads
the BOS descriptor, but that can be handled in a separate patch later
on. This patch is sufficient to fix the bug found by syzbot.

Reported-and-tested-by: syzbot+18996170f8096c6174d0@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/linux-usb/000000000000c0ffe505fe86c9ca@google.com/#r
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Cc: Khazhy Kumykov <khazhy@google.com>
Fixes: 45bf39f8df7f ("USB: core: Don't hold device lock while reading the "descriptors" sysfs file")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/b958b47a-9a46-4c22-a9f9-e42e42c31251@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
ff33299e de28e469

+70 -44
+70 -44
drivers/usb/core/hub.c
··· 4816 4816 * the port lock. For a newly detected device that is not accessible 4817 4817 * through any global pointers, it's not necessary to lock the device, 4818 4818 * but it is still necessary to lock the port. 4819 + * 4820 + * For a newly detected device, @dev_descr must be NULL. The device 4821 + * descriptor retrieved from the device will then be stored in 4822 + * @udev->descriptor. For an already existing device, @dev_descr 4823 + * must be non-NULL. The device descriptor will be stored there, 4824 + * not in @udev->descriptor, because descriptors for registered 4825 + * devices are meant to be immutable. 4819 4826 */ 4820 4827 static int 4821 4828 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 4822 - int retry_counter) 4829 + int retry_counter, struct usb_device_descriptor *dev_descr) 4823 4830 { 4824 4831 struct usb_device *hdev = hub->hdev; 4825 4832 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); ··· 4838 4831 int devnum = udev->devnum; 4839 4832 const char *driver_name; 4840 4833 bool do_new_scheme; 4834 + const bool initial = !dev_descr; 4841 4835 int maxp0; 4842 4836 struct usb_device_descriptor *buf, *descr; 4843 4837 ··· 4877 4869 } 4878 4870 oldspeed = udev->speed; 4879 4871 4880 - /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 4881 - * it's fixed size except for full speed devices. 4882 - * For Wireless USB devices, ep0 max packet is always 512 (tho 4883 - * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 4884 - */ 4885 - switch (udev->speed) { 4886 - case USB_SPEED_SUPER_PLUS: 4887 - case USB_SPEED_SUPER: 4888 - case USB_SPEED_WIRELESS: /* fixed at 512 */ 4889 - udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 4890 - break; 4891 - case USB_SPEED_HIGH: /* fixed at 64 */ 4892 - udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4893 - break; 4894 - case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 4895 - /* to determine the ep0 maxpacket size, try to read 4896 - * the device descriptor to get bMaxPacketSize0 and 4897 - * then correct our initial guess. 4872 + if (initial) { 4873 + /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 4874 + * it's fixed size except for full speed devices. 4875 + * For Wireless USB devices, ep0 max packet is always 512 (tho 4876 + * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 4898 4877 */ 4899 - udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4900 - break; 4901 - case USB_SPEED_LOW: /* fixed at 8 */ 4902 - udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 4903 - break; 4904 - default: 4905 - goto fail; 4878 + switch (udev->speed) { 4879 + case USB_SPEED_SUPER_PLUS: 4880 + case USB_SPEED_SUPER: 4881 + case USB_SPEED_WIRELESS: /* fixed at 512 */ 4882 + udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 4883 + break; 4884 + case USB_SPEED_HIGH: /* fixed at 64 */ 4885 + udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4886 + break; 4887 + case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 4888 + /* to determine the ep0 maxpacket size, try to read 4889 + * the device descriptor to get bMaxPacketSize0 and 4890 + * then correct our initial guess. 4891 + */ 4892 + udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4893 + break; 4894 + case USB_SPEED_LOW: /* fixed at 8 */ 4895 + udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 4896 + break; 4897 + default: 4898 + goto fail; 4899 + } 4906 4900 } 4907 4901 4908 4902 if (udev->speed == USB_SPEED_WIRELESS) ··· 4927 4917 if (udev->speed < USB_SPEED_SUPER) 4928 4918 dev_info(&udev->dev, 4929 4919 "%s %s USB device number %d using %s\n", 4930 - (udev->config) ? "reset" : "new", speed, 4920 + (initial ? "new" : "reset"), speed, 4931 4921 devnum, driver_name); 4932 4922 4933 - /* Set up TT records, if needed */ 4934 - if (hdev->tt) { 4935 - udev->tt = hdev->tt; 4936 - udev->ttport = hdev->ttport; 4937 - } else if (udev->speed != USB_SPEED_HIGH 4938 - && hdev->speed == USB_SPEED_HIGH) { 4939 - if (!hub->tt.hub) { 4940 - dev_err(&udev->dev, "parent hub has no TT\n"); 4941 - retval = -EINVAL; 4942 - goto fail; 4923 + if (initial) { 4924 + /* Set up TT records, if needed */ 4925 + if (hdev->tt) { 4926 + udev->tt = hdev->tt; 4927 + udev->ttport = hdev->ttport; 4928 + } else if (udev->speed != USB_SPEED_HIGH 4929 + && hdev->speed == USB_SPEED_HIGH) { 4930 + if (!hub->tt.hub) { 4931 + dev_err(&udev->dev, "parent hub has no TT\n"); 4932 + retval = -EINVAL; 4933 + goto fail; 4934 + } 4935 + udev->tt = &hub->tt; 4936 + udev->ttport = port1; 4943 4937 } 4944 - udev->tt = &hub->tt; 4945 - udev->ttport = port1; 4946 4938 } 4947 4939 4948 4940 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? ··· 4978 4966 4979 4967 maxp0 = get_bMaxPacketSize0(udev, buf, 4980 4968 GET_DESCRIPTOR_BUFSIZE, retries == 0); 4969 + if (maxp0 > 0 && !initial && 4970 + maxp0 != udev->descriptor.bMaxPacketSize0) { 4971 + dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 4972 + retval = -ENODEV; 4973 + goto fail; 4974 + } 4981 4975 4982 4976 retval = hub_port_reset(hub, port1, udev, delay, false); 4983 4977 if (retval < 0) /* error or disconnect */ ··· 5057 5039 } else { 5058 5040 u32 delay; 5059 5041 5042 + if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) { 5043 + dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 5044 + retval = -ENODEV; 5045 + goto fail; 5046 + } 5047 + 5060 5048 delay = udev->parent->hub_delay; 5061 5049 udev->hub_delay = min_t(u32, delay, 5062 5050 USB_TP_TRANSMISSION_DELAY_MAX); ··· 5106 5082 retval); 5107 5083 goto fail; 5108 5084 } 5109 - udev->descriptor = *descr; 5085 + if (initial) 5086 + udev->descriptor = *descr; 5087 + else 5088 + *dev_descr = *descr; 5110 5089 kfree(descr); 5111 5090 5112 5091 /* ··· 5419 5392 } 5420 5393 5421 5394 /* reset (non-USB 3.0 devices) and get descriptor */ 5422 - status = hub_port_init(hub, udev, port1, i); 5395 + status = hub_port_init(hub, udev, port1, i, NULL); 5423 5396 if (status < 0) 5424 5397 goto loop; 5425 5398 ··· 6049 6022 struct usb_device *parent_hdev = udev->parent; 6050 6023 struct usb_hub *parent_hub; 6051 6024 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 6052 - struct usb_device_descriptor descriptor = udev->descriptor; 6025 + struct usb_device_descriptor descriptor; 6053 6026 struct usb_host_bos *bos; 6054 6027 int i, j, ret = 0; 6055 6028 int port1 = udev->portnum; ··· 6085 6058 /* ep0 maxpacket size may change; let the HCD know about it. 6086 6059 * Other endpoints will be handled by re-enumeration. */ 6087 6060 usb_ep0_reinit(udev); 6088 - ret = hub_port_init(parent_hub, udev, port1, i); 6061 + ret = hub_port_init(parent_hub, udev, port1, i, &descriptor); 6089 6062 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 6090 6063 break; 6091 6064 } ··· 6097 6070 /* Device might have changed firmware (DFU or similar) */ 6098 6071 if (descriptors_changed(udev, &descriptor, bos)) { 6099 6072 dev_info(&udev->dev, "device firmware changed\n"); 6100 - udev->descriptor = descriptor; /* for disconnect() calls */ 6101 6073 goto re_enumerate; 6102 6074 } 6103 6075