diff options
author | Alan Stern <stern@rowland.harvard.edu> | 2007-05-22 11:46:41 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2007-07-12 16:29:48 -0700 |
commit | d4ead16f50f9ad30bdc7276ec8fee7a24c72f294 (patch) | |
tree | e1905abbc393cc4d73180dd7b9e1cf860378b590 /drivers/usb/misc/ldusb.c | |
parent | 55e5fdfa541ec7bf1b1613624ed4dd8cdacaa841 (diff) |
USB: prevent char device open/deregister race
This patch (as908) adds central protection in usbcore for the
prototypical race between opening and unregistering a char device.
The spinlock used to protect the minor-numbers array is replaced with
an rwsem, which can remain locked across a call to a driver's open()
method. This guarantees that open() and deregister() will be mutually
exclusive.
The private locks currently used in several individual drivers for
this purpose are no longer necessary, and the patch removes them. The
following USB drivers are affected: usblcd, idmouse, auerswald,
legousbtower, sisusbvga/sisusb, ldusb, adutux, iowarrior, and
usb-skeleton.
As a side effect of this change, usb_deregister_dev() must not be
called while holding a lock that is acquired by open(). Unfortunately
a number of drivers do this, but luckily the solution is simple: call
usb_deregister_dev() before acquiring the lock.
In addition to these changes (and their consequent code
simplifications), the patch fixes a use-after-free bug in adutux and a
race between open() and release() in iowarrior.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/misc/ldusb.c')
-rw-r--r-- | drivers/usb/misc/ldusb.c | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c index 7bad4940476..5e950b90c54 100644 --- a/drivers/usb/misc/ldusb.c +++ b/drivers/usb/misc/ldusb.c @@ -176,9 +176,6 @@ struct ld_usb { int interrupt_out_busy; }; -/* prevent races between open() and disconnect() */ -static DEFINE_MUTEX(disconnect_mutex); - static struct usb_driver ld_usb_driver; /** @@ -298,35 +295,28 @@ static int ld_usb_open(struct inode *inode, struct file *file) { struct ld_usb *dev; int subminor; - int retval = 0; + int retval; struct usb_interface *interface; nonseekable_open(inode, file); subminor = iminor(inode); - mutex_lock(&disconnect_mutex); - interface = usb_find_interface(&ld_usb_driver, subminor); if (!interface) { err("%s - error, can't find device for minor %d\n", __FUNCTION__, subminor); - retval = -ENODEV; - goto unlock_disconnect_exit; + return -ENODEV; } dev = usb_get_intfdata(interface); - if (!dev) { - retval = -ENODEV; - goto unlock_disconnect_exit; - } + if (!dev) + return -ENODEV; /* lock this device */ - if (down_interruptible(&dev->sem)) { - retval = -ERESTARTSYS; - goto unlock_disconnect_exit; - } + if (down_interruptible(&dev->sem)) + return -ERESTARTSYS; /* allow opening only once */ if (dev->open_count) { @@ -366,9 +356,6 @@ static int ld_usb_open(struct inode *inode, struct file *file) unlock_exit: up(&dev->sem); -unlock_disconnect_exit: - mutex_unlock(&disconnect_mutex); - return retval; } @@ -766,18 +753,16 @@ static void ld_usb_disconnect(struct usb_interface *intf) struct ld_usb *dev; int minor; - mutex_lock(&disconnect_mutex); - dev = usb_get_intfdata(intf); usb_set_intfdata(intf, NULL); - down(&dev->sem); - minor = intf->minor; /* give back our minor */ usb_deregister_dev(intf, &ld_usb_class); + down(&dev->sem); + /* if the device is not opened, then we clean up right now */ if (!dev->open_count) { up(&dev->sem); @@ -787,8 +772,6 @@ static void ld_usb_disconnect(struct usb_interface *intf) up(&dev->sem); } - mutex_unlock(&disconnect_mutex); - dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", (minor - USB_LD_MINOR_BASE)); } |