diff options
author | Russ Dill <Russ.Dill@gmail.com> | 2009-12-14 23:45:35 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2009-12-15 10:50:28 -0500 |
commit | c2d284ee04ab6f6718de2ddcf1b43160e046c41d (patch) | |
tree | 915ff77064dd9c6c002f416d2b1ead97d1c7c970 /drivers | |
parent | ab7cd8c76cb47f1bda0ad964d309b4efce81b5e9 (diff) |
USB: Close usb_find_interface race v3
USB drivers that create character devices call usb_register_dev in their
probe function. This associates the usb_interface device with that minor
number and creates the character device and announces it to the world.
However, the driver's probe function is called before the new
usb_interface is added to the driver's klist_devices.
This is a problem because userspace will respond to the character device
creation announcement by opening the character device. The driver's open
function will the call usb_find_interface to find the usb_interface
associated with that minor number. usb_find_interface will walk the
driver's list of devices and find the usb_interface with the matching
minor number.
Because the announcement happens before the usb_interface is added to the
driver's klist_devices, a race condition exists. A straightforward fix
is to walk the list of devices on usb_bus_type instead since the device
is added to that list before the announcement occurs.
bus_find_device calls get_device to bump the reference count on the found
device. It is arguable that the reference count should be dropped by the
caller of usb_find_interface instead of usb_find_interface, however,
the current users of usb_find_interface do not expect this.
The original version of this patch only matched against minor number
instead of driver and minor number. This version matches against both.
Signed-off-by: Russ Dill <Russ.Dill@gmail.com>
Cc: stable <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/usb/core/usb.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c index 932f68e6ad19..d86276c639c1 100644 --- a/drivers/usb/core/usb.c +++ b/drivers/usb/core/usb.c | |||
@@ -169,7 +169,7 @@ EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting); | |||
169 | 169 | ||
170 | struct find_interface_arg { | 170 | struct find_interface_arg { |
171 | int minor; | 171 | int minor; |
172 | struct usb_interface *interface; | 172 | struct device_driver *drv; |
173 | }; | 173 | }; |
174 | 174 | ||
175 | static int __find_interface(struct device *dev, void *data) | 175 | static int __find_interface(struct device *dev, void *data) |
@@ -180,12 +180,10 @@ static int __find_interface(struct device *dev, void *data) | |||
180 | if (!is_usb_interface(dev)) | 180 | if (!is_usb_interface(dev)) |
181 | return 0; | 181 | return 0; |
182 | 182 | ||
183 | if (dev->driver != arg->drv) | ||
184 | return 0; | ||
183 | intf = to_usb_interface(dev); | 185 | intf = to_usb_interface(dev); |
184 | if (intf->minor != -1 && intf->minor == arg->minor) { | 186 | return intf->minor == arg->minor; |
185 | arg->interface = intf; | ||
186 | return 1; | ||
187 | } | ||
188 | return 0; | ||
189 | } | 187 | } |
190 | 188 | ||
191 | /** | 189 | /** |
@@ -193,21 +191,24 @@ static int __find_interface(struct device *dev, void *data) | |||
193 | * @drv: the driver whose current configuration is considered | 191 | * @drv: the driver whose current configuration is considered |
194 | * @minor: the minor number of the desired device | 192 | * @minor: the minor number of the desired device |
195 | * | 193 | * |
196 | * This walks the driver device list and returns a pointer to the interface | 194 | * This walks the bus device list and returns a pointer to the interface |
197 | * with the matching minor. Note, this only works for devices that share the | 195 | * with the matching minor and driver. Note, this only works for devices |
198 | * USB major number. | 196 | * that share the USB major number. |
199 | */ | 197 | */ |
200 | struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) | 198 | struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) |
201 | { | 199 | { |
202 | struct find_interface_arg argb; | 200 | struct find_interface_arg argb; |
203 | int retval; | 201 | struct device *dev; |
204 | 202 | ||
205 | argb.minor = minor; | 203 | argb.minor = minor; |
206 | argb.interface = NULL; | 204 | argb.drv = &drv->drvwrap.driver; |
207 | /* eat the error, it will be in argb.interface */ | 205 | |
208 | retval = driver_for_each_device(&drv->drvwrap.driver, NULL, &argb, | 206 | dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface); |
209 | __find_interface); | 207 | |
210 | return argb.interface; | 208 | /* Drop reference count from bus_find_device */ |
209 | put_device(dev); | ||
210 | |||
211 | return dev ? to_usb_interface(dev) : NULL; | ||
211 | } | 212 | } |
212 | EXPORT_SYMBOL_GPL(usb_find_interface); | 213 | EXPORT_SYMBOL_GPL(usb_find_interface); |
213 | 214 | ||