Skip to content

Commit 9c6d778

Browse files
AlanSterngregkh
authored andcommitted
USB: core: Prevent nested device-reset calls
Automatic kernel fuzzing revealed a recursive locking violation in usb-storage: ============================================ WARNING: possible recursive locking detected 5.18.0 #3 Not tainted -------------------------------------------- kworker/1:3/1205 is trying to acquire lock: ffff888018638db8 (&us_interface_key[i]){+.+.}-{3:3}, at: usb_stor_pre_reset+0x35/0x40 drivers/usb/storage/usb.c:230 but task is already holding lock: ffff888018638db8 (&us_interface_key[i]){+.+.}-{3:3}, at: usb_stor_pre_reset+0x35/0x40 drivers/usb/storage/usb.c:230 ... stack backtrace: CPU: 1 PID: 1205 Comm: kworker/1:3 Not tainted 5.18.0 #3 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Workqueue: usb_hub_wq hub_event Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_deadlock_bug kernel/locking/lockdep.c:2988 [inline] check_deadlock kernel/locking/lockdep.c:3031 [inline] validate_chain kernel/locking/lockdep.c:3816 [inline] __lock_acquire.cold+0x152/0x3ca kernel/locking/lockdep.c:5053 lock_acquire kernel/locking/lockdep.c:5665 [inline] lock_acquire+0x1ab/0x520 kernel/locking/lockdep.c:5630 __mutex_lock_common kernel/locking/mutex.c:603 [inline] __mutex_lock+0x14f/0x1610 kernel/locking/mutex.c:747 usb_stor_pre_reset+0x35/0x40 drivers/usb/storage/usb.c:230 usb_reset_device+0x37d/0x9a0 drivers/usb/core/hub.c:6109 r871xu_dev_remove+0x21a/0x270 drivers/staging/rtl8712/usb_intf.c:622 usb_unbind_interface+0x1bd/0x890 drivers/usb/core/driver.c:458 device_remove drivers/base/dd.c:545 [inline] device_remove+0x11f/0x170 drivers/base/dd.c:537 __device_release_driver drivers/base/dd.c:1222 [inline] device_release_driver_internal+0x1a7/0x2f0 drivers/base/dd.c:1248 usb_driver_release_interface+0x102/0x180 drivers/usb/core/driver.c:627 usb_forced_unbind_intf+0x4d/0xa0 drivers/usb/core/driver.c:1118 usb_reset_device+0x39b/0x9a0 drivers/usb/core/hub.c:6114 This turned out not to be an error in usb-storage but rather a nested device reset attempt. That is, as the rtl8712 driver was being unbound from a composite device in preparation for an unrelated USB reset (that driver does not have pre_reset or post_reset callbacks), its ->remove routine called usb_reset_device() -- thus nesting one reset call within another. Performing a reset as part of disconnect processing is a questionable practice at best. However, the bug report points out that the USB core does not have any protection against nested resets. Adding a reset_in_progress flag and testing it will prevent such errors in the future. Link: https://lore.kernel.org/all/CAB7eexKUpvX-JNiLzhXBDWgfg2T9e9_0Tw4HQ6keN==voRbP0g@mail.gmail.com/ Cc: stable@vger.kernel.org Reported-and-tested-by: Rondreis <linhaoguo86@gmail.com> Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Link: https://lore.kernel.org/r/YwkflDxvg0KWqyZK@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1016fc0 commit 9c6d778

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

drivers/usb/core/hub.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6038,6 +6038,11 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
60386038
* the reset is over (using their post_reset method).
60396039
*
60406040
* Return: The same as for usb_reset_and_verify_device().
6041+
* However, if a reset is already in progress (for instance, if a
6042+
* driver doesn't have pre_ or post_reset() callbacks, and while
6043+
* being unbound or re-bound during the ongoing reset its disconnect()
6044+
* or probe() routine tries to perform a second, nested reset), the
6045+
* routine returns -EINPROGRESS.
60416046
*
60426047
* Note:
60436048
* The caller must own the device lock. For example, it's safe to use
@@ -6071,6 +6076,10 @@ int usb_reset_device(struct usb_device *udev)
60716076
return -EISDIR;
60726077
}
60736078

6079+
if (udev->reset_in_progress)
6080+
return -EINPROGRESS;
6081+
udev->reset_in_progress = 1;
6082+
60746083
port_dev = hub->ports[udev->portnum - 1];
60756084

60766085
/*
@@ -6135,6 +6144,7 @@ int usb_reset_device(struct usb_device *udev)
61356144

61366145
usb_autosuspend_device(udev);
61376146
memalloc_noio_restore(noio_flag);
6147+
udev->reset_in_progress = 0;
61386148
return ret;
61396149
}
61406150
EXPORT_SYMBOL_GPL(usb_reset_device);

include/linux/usb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ struct usb3_lpm_parameters {
575575
* @devaddr: device address, XHCI: assigned by HW, others: same as devnum
576576
* @can_submit: URBs may be submitted
577577
* @persist_enabled: USB_PERSIST enabled for this device
578+
* @reset_in_progress: the device is being reset
578579
* @have_langid: whether string_langid is valid
579580
* @authorized: policy has said we can use it;
580581
* (user space) policy determines if we authorize this device to be
@@ -662,6 +663,7 @@ struct usb_device {
662663

663664
unsigned can_submit:1;
664665
unsigned persist_enabled:1;
666+
unsigned reset_in_progress:1;
665667
unsigned have_langid:1;
666668
unsigned authorized:1;
667669
unsigned authenticated:1;

0 commit comments

Comments
 (0)