Jiri Slaby 1d32c9
From: Alan Stern <stern@rowland.harvard.edu>
Jiri Slaby 1d32c9
Date: Tue, 31 Jan 2023 15:49:04 -0500
Jiri Slaby 1d32c9
Subject: [PATCH] USB: core: Don't hold device lock while reading the
Jiri Slaby 1d32c9
 "descriptors" sysfs file
Jiri Slaby 1d32c9
References: bsc#1012628
Jiri Slaby 1d32c9
Patch-mainline: 6.2.2
Jiri Slaby 1d32c9
Git-commit: 45bf39f8df7f05efb83b302c65ae3b9bc92b7065
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
commit 45bf39f8df7f05efb83b302c65ae3b9bc92b7065 upstream.
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
Ever since commit 83e83ecb79a8 ("usb: core: get config and string
Jiri Slaby 1d32c9
descriptors for unauthorized devices") was merged in 2013, there has
Jiri Slaby 1d32c9
been no mechanism for reallocating the rawdescriptors buffers in
Jiri Slaby 1d32c9
struct usb_device after the initial enumeration.  Before that commit,
Jiri Slaby 1d32c9
the buffers would be deallocated when a device was deauthorized and
Jiri Slaby 1d32c9
reallocated when it was authorized and enumerated.
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
This means that the locking in the read_descriptors() routine is not
Jiri Slaby 1d32c9
needed, since the buffers it reads will never be reallocated while the
Jiri Slaby 1d32c9
routine is running.  This locking can interfere with user programs
Jiri Slaby 1d32c9
trying to read a hub's descriptors via sysfs while new child devices
Jiri Slaby 1d32c9
of the hub are being initialized, since the hub is locked during this
Jiri Slaby 1d32c9
procedure.
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
Since the locking in read_descriptors() hasn't been needed for over
Jiri Slaby 1d32c9
nine years, we can remove it.
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
Reported-and-tested-by: Troels Liebe Bentsen <troels@connectedcars.dk>
Jiri Slaby 1d32c9
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Jiri Slaby 1d32c9
CC: stable@vger.kernel.org
Jiri Slaby 1d32c9
Link: https://lore.kernel.org/r/Y9l+wDTRbuZABzsE@rowland.harvard.edu
Jiri Slaby 1d32c9
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Jiri Slaby 1d32c9
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Jiri Slaby 1d32c9
---
Jiri Slaby 1d32c9
 drivers/usb/core/hub.c   | 5 ++---
Jiri Slaby 1d32c9
 drivers/usb/core/sysfs.c | 5 -----
Jiri Slaby 1d32c9
 2 files changed, 2 insertions(+), 8 deletions(-)
Jiri Slaby 1d32c9
Jiri Slaby 1d32c9
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
Jiri Slaby 1d32c9
index 9eca403a..97a0f8fa 100644
Jiri Slaby 1d32c9
--- a/drivers/usb/core/hub.c
Jiri Slaby 1d32c9
+++ b/drivers/usb/core/hub.c
Jiri Slaby 1d32c9
@@ -2389,9 +2389,8 @@ static int usb_enumerate_device_otg(struct usb_device *udev)
Jiri Slaby 1d32c9
  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Jiri Slaby 1d32c9
  * @udev: newly addressed device (in ADDRESS state)
Jiri Slaby 1d32c9
  *
Jiri Slaby 1d32c9
- * This is only called by usb_new_device() and usb_authorize_device()
Jiri Slaby 1d32c9
- * and FIXME -- all comments that apply to them apply here wrt to
Jiri Slaby 1d32c9
- * environment.
Jiri Slaby 1d32c9
+ * This is only called by usb_new_device() -- all comments that apply there
Jiri Slaby 1d32c9
+ * apply here wrt to environment.
Jiri Slaby 1d32c9
  *
Jiri Slaby 1d32c9
  * If the device is WUSB and not authorized, we don't attempt to read
Jiri Slaby 1d32c9
  * the string descriptors, as they will be errored out by the device
Jiri Slaby 1d32c9
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
Jiri Slaby 1d32c9
index 8217032d..b63f78e4 100644
Jiri Slaby 1d32c9
--- a/drivers/usb/core/sysfs.c
Jiri Slaby 1d32c9
+++ b/drivers/usb/core/sysfs.c
Jiri Slaby 1d32c9
@@ -869,11 +869,7 @@ read_descriptors(struct file *filp, struct kobject *kobj,
Jiri Slaby 1d32c9
 	size_t srclen, n;
Jiri Slaby 1d32c9
 	int cfgno;
Jiri Slaby 1d32c9
 	void *src;
Jiri Slaby 1d32c9
-	int retval;
Jiri Slaby 1d32c9
 
Jiri Slaby 1d32c9
-	retval = usb_lock_device_interruptible(udev);
Jiri Slaby 1d32c9
-	if (retval < 0)
Jiri Slaby 1d32c9
-		return -EINTR;
Jiri Slaby 1d32c9
 	/* The binary attribute begins with the device descriptor.
Jiri Slaby 1d32c9
 	 * Following that are the raw descriptor entries for all the
Jiri Slaby 1d32c9
 	 * configurations (config plus subsidiary descriptors).
Jiri Slaby 1d32c9
@@ -898,7 +894,6 @@ read_descriptors(struct file *filp, struct kobject *kobj,
Jiri Slaby 1d32c9
 			off -= srclen;
Jiri Slaby 1d32c9
 		}
Jiri Slaby 1d32c9
 	}
Jiri Slaby 1d32c9
-	usb_unlock_device(udev);
Jiri Slaby 1d32c9
 	return count - nleft;
Jiri Slaby 1d32c9
 }
Jiri Slaby 1d32c9
 
Jiri Slaby 1d32c9
-- 
Jiri Slaby 1d32c9
2.35.3
Jiri Slaby 1d32c9