Skip to contentSkip to conversation

usb: core: Abort deauthorization if unsetting configuration fails

PATCH In review Julius Werner 2 participants 6 messages Dec 4, 2013 – Dec 11, 2013 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: core: Abort deauthorization if unsetting configuration fails

usb_deauthorize_device() tries to unset the configuration of a USB device and then unconditionally blows away the configuration descriptors with usb_destroy_configuration(). This is bad if the usb_set_configuration() call failed before the configuration could be correctly unset, since pointers like udev->actconfig can keep pointing to the now invalid memory. We have encountered hard to reproduce crashes from devices disconnected during suspend due to this, where khubd's disconnect handling races with userspace tools that change authorization on resume.

This patch ensures that usb_deauthorize_device() aborts when usb_set_configuration() fails, and the underlying status code (such as ENODEV) is returned to userspace.

---
 drivers/usb/core/hub.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/usb/core/hub.c +8 -3
index a7c04e2..ade315f 100644
@@ -2420,13 +2420,17 @@ fail:
  */
 int usb_deauthorize_device(struct usb_device *usb_dev)
 {
+	int result = 0;
+
 	usb_lock_device(usb_dev);
 	if (usb_dev->authorized == 0)
 		goto out_unauthorized;
 
-	usb_dev->authorized = 0;
-	usb_set_configuration(usb_dev, -1);
+	result = usb_set_configuration(usb_dev, -1);
+	if (result)
+		goto error_deconfigure;
 
+	usb_dev->authorized = 0;
 	kfree(usb_dev->product);
 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
 	kfree(usb_dev->manufacturer);
@@ -2437,9 +2441,10 @@ int usb_deauthorize_device(struct usb_device *usb_dev)
 	usb_destroy_configuration(usb_dev);
 	usb_dev->descriptor.bNumConfigurations = 0;
 
+error_deconfigure:
 out_unauthorized:
 	usb_unlock_device(usb_dev);
-	return 0;
+	return result;
 }
 
 
Signature · 1 line
1.7.12.4
#1 lore raw
Quoted 10 lines · Julius Werner

usb_deauthorize_device() tries to unset the configuration of a USB device and then unconditionally blows away the configuration descriptors with usb_destroy_configuration(). This is bad if the usb_set_configuration() call failed before the configuration could be correctly unset, since pointers like udev->actconfig can keep pointing to the now invalid memory. We have encountered hard to reproduce crashes from devices disconnected during suspend due to this, where khubd's disconnect handling races with userspace tools that change authorization on resume.

Quoted 3 lines

This patch ensures that usb_deauthorize_device() aborts when usb_set_configuration() fails, and the underlying status code (such as ENODEV) is returned to userspace.

I don't like this approach at all. It should always be possible to deauthorize a device, no matter what.

Instead, how about changing usb_set_configuration() so that it will never fail when the new config is -1? Except perhaps for -ENODEV errors (the device has been disconnected), which usb_deauthorize_device() could check for.

Alan Stern

#2 lore raw
Quoted 4 lines

Instead, how about changing usb_set_configuration() so that it will never fail when the new config is -1? Except perhaps for -ENODEV errors (the device has been disconnected), which usb_deauthorize_device() could check for.

Yes, that should work as well. It's really just one autoresume and one disable_lpm that can fail in that case so it shouldn't be too intrusive. I would prefer not to special-case ENODEV though, no need to add more complexity than necessary.

I will write up a new version for that tomorrow.

#3 lore raw

PATCH v2 1/1 usb: core: Make sure usb_set_configuration(-1) cannot fail

usb_deauthorize_device() tries to unset the configuration of a USB device and then unconditionally blows away the configuration descriptors with usb_destroy_configuration(). This is bad if the usb_set_configuration() call failed before the configuration could be correctly unset, since pointers like udev->actconfig can keep pointing to the now invalid memory. We have encountered hard to reproduce crashes from devices disconnected during suspend due to this, where khubd's disconnect handling races with userspace tools that change authorization on resume.

It seems desirable that a USB device can always be marked as unconfigured (reclaiming its bandwidth) in the kernel, regardless of communication problems. This patch changes usb_set_configuration() to ignore all failures in the case where no new configuration is being set.

---
 drivers/usb/core/message.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/core/message.c +4 -3
index bb31597..f980b6d 100644
@@ -1706,7 +1706,8 @@ static void __usb_queue_reset_device(struct work_struct *ws)
  * underlying call that failed.  On successful completion, each interface
  * in the original device configuration has been destroyed, and each one
  * in the new configuration has been probed by all relevant usb device
- * drivers currently known to the kernel.
+ * drivers currently known to the kernel. Guaranteed not to fail if the
+ * device is to be unconfigured (@configuration = -1).
  */
 int usb_set_configuration(struct usb_device *dev, int configuration)
 {
@@ -1774,7 +1775,7 @@ free_interfaces:
 
 	/* Wake up the device so we can send it the Set-Config request */
 	ret = usb_autoresume_device(dev);
-	if (ret)
+	if (ret && cp)
 		goto free_interfaces;
 
 	/* if it's already configured, clear out old state first.
@@ -1797,7 +1798,7 @@ free_interfaces:
 	 * installed, so that the xHCI driver can recalculate the U1/U2
 	 * timeouts.
 	 */
-	if (dev->actconfig && usb_disable_lpm(dev)) {
+	if (dev->actconfig && usb_disable_lpm(dev) && cp) {
 		dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
 		mutex_unlock(hcd->bandwidth_mutex);
 		ret = -ENOMEM;
Signature · 1 line
1.7.12.4
#4 lore raw

Re: [PATCH v2] usb: core: Make sure usb_set_configuration(-1) cannot fail

Quoted 10 lines · Julius Werner

usb_deauthorize_device() tries to unset the configuration of a USB device and then unconditionally blows away the configuration descriptors with usb_destroy_configuration(). This is bad if the usb_set_configuration() call failed before the configuration could be correctly unset, since pointers like udev->actconfig can keep pointing to the now invalid memory. We have encountered hard to reproduce crashes from devices disconnected during suspend due to this, where khubd's disconnect handling races with userspace tools that change authorization on resume.

Quoted 6 lines

It seems desirable that a USB device can always be marked as unconfigured (reclaiming its bandwidth) in the kernel, regardless of communication problems. This patch changes usb_set_configuration() to ignore all failures in the case where no new configuration is being set.

Sorry for not getting back to this sooner. Ironically, it looks like this change isn't needed any more, thanks to Thomas Pugliese's patch:

	http://marc.info/?l=linux-usb&m=138661805209999&w=2

drivers/usb/core/message.c free_interfaces: · line 1779

Quoted 10 lines
drivers/usb/core/message.c +1 -1
@@ -1774,7 +1775,7 @@ free_interfaces:
 
 	/* Wake up the device so we can send it the Set-Config request */
 	ret = usb_autoresume_device(dev);
-	if (ret)
+	if (ret && cp)
 		goto free_interfaces;

That isn't quite right. If the autoresume fails then we have to skip the autosuspend call later on.

Alan Stern

#5 lore raw

Re: [PATCH v2] usb: core: Make sure usb_set_configuration(-1) cannot fail

Quoted 4 lines

Sorry for not getting back to this sooner. Ironically, it looks like this change isn't needed any more, thanks to Thomas Pugliese's patch:

        http://marc.info/?l=linux-usb&m=138661805209999&w=2

Yes... thanks for pointing it out, that would make this patch obsolete. I'll wait a few days with this to see if that patch gets accepted as it is.