Skip to content

Commit

Permalink
USB: core: Fix deadlock in usb_deauthorize_interface()
Browse files Browse the repository at this point in the history
commit 80ba43e9f799cbdd83842fc27db667289b3150f5 upstream.

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 <[email protected]>
Reported by: xingwei lee <[email protected]>

Signed-off-by: Alan Stern <[email protected]>
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: [email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
AlanStern authored and JeevakaPrabu committed Jun 20, 2024
1 parent 3ae6ac4 commit e3ef8bc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions drivers/usb/core/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,14 +1166,24 @@ static ssize_t interface_authorized_store(struct device *dev,
{
struct usb_interface *intf = to_usb_interface(dev);
bool val;
struct kernfs_node *kn;

if (strtobool(buf, &val) != 0)
return -EINVAL;

if (val)
if (val) {
usb_authorize_interface(intf);
else
usb_deauthorize_interface(intf);
} else {
/*
* Prevent deadlock if another process is concurrently
* trying to unregister intf.
*/
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
if (kn) {
usb_deauthorize_interface(intf);
sysfs_unbreak_active_protection(kn);
}
}

return count;
}
Expand Down

0 comments on commit e3ef8bc

Please sign in to comment.