Skip to content

Commit 80ba43e

Browse files
AlanSterngregkh
authored andcommitted
USB: core: Fix deadlock in usb_deauthorize_interface()
Among the attribute file callback routines in drivers/usb/core/sysfs.c, the interface_authorized_store() function is the only one which acquires a device lock on an ancestor device: It calls usb_deauthorize_interface(), which locks the interface's parent USB device. The will lead to deadlock if another process already owns that lock and tries to remove the interface, whether through a configuration change or because the device has been disconnected. As part of the removal procedure, device_del() waits for all ongoing sysfs attribute callbacks to complete. But usb_deauthorize_interface() can't complete until the device lock has been released, and the lock won't be released until the removal has finished. The mechanism provided by sysfs to prevent this kind of deadlock is to use the sysfs_break_active_protection() function, which tells sysfs not to wait for the attribute callback. Reported-and-tested by: Yue Sun <samsun1006219@gmail.com> Reported by: xingwei lee <xrivendell7@gmail.com> Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Link: https://lore.kernel.org/linux-usb/CAEkJfYO6jRVC8Tfrd_R=cjO0hguhrV31fDPrLrNOOHocDkPoAA@mail.gmail.com/#r Fixes: 310d2b4 ("usb: interface authorization: SysFS part of USB interface authorization") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/1c37eea1-9f56-4534-b9d8-b443438dc869@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 17af505 commit 80ba43e

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

drivers/usb/core/sysfs.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,14 +1217,24 @@ static ssize_t interface_authorized_store(struct device *dev,
12171217
{
12181218
struct usb_interface *intf = to_usb_interface(dev);
12191219
bool val;
1220+
struct kernfs_node *kn;
12201221

12211222
if (kstrtobool(buf, &val) != 0)
12221223
return -EINVAL;
12231224

1224-
if (val)
1225+
if (val) {
12251226
usb_authorize_interface(intf);
1226-
else
1227-
usb_deauthorize_interface(intf);
1227+
} else {
1228+
/*
1229+
* Prevent deadlock if another process is concurrently
1230+
* trying to unregister intf.
1231+
*/
1232+
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
1233+
if (kn) {
1234+
usb_deauthorize_interface(intf);
1235+
sysfs_unbreak_active_protection(kn);
1236+
}
1237+
}
12281238

12291239
return count;
12301240
}

0 commit comments

Comments
 (0)