diff options
author | David Herrmann <dh.herrmann@googlemail.com> | 2011-08-10 08:02:07 -0400 |
---|---|---|
committer | Jiri Kosina <jkosina@suse.cz> | 2011-08-10 08:02:07 -0400 |
commit | 4ea5454203d991ec85264f64f89ca8855fce69b0 (patch) | |
tree | e0bd6d2f01894f09c7b3d72fd8852afbc8c66d73 /include/linux/hid.h | |
parent | 00b15628b0aa73199f74bd6c988b8d2b55f51190 (diff) |
HID: Fix race condition between driver core and ll-driver
HID low level drivers register new devices with the HID core which then
adds the devices to the HID bus. The HID bus normally immediately probes
an appropriate driver which then handles HID input for this device.
The ll driver now uses the hid_input_report() function to report input
events for a specific device. However, if the HID bus unloads the driver
at the same time (for instance via a call to
/sys/bus/hid/devices/<dev>/unbind) then the hdev->driver pointer may be
used by hid_input_report() and hid_device_remove() at the same time
which may cause hdev->driver to point to invalid memory.
This fix adds a semaphore to every hid device which protects
hdev->driver from asynchronous access. This semaphore is locked during
driver *_probe and *_remove and also inside hid_input_report(). The
*_probe and *_remove functions may sleep so the semaphore is good here,
however, hid_input_report() is in atomic context and hence only uses
down_trylock(). If it cannot acquire the lock it simply drops the input
package.
The low-level drivers report input events synchronously so
hid_input_report() should never be entered twice at the same time on the
same device. Hence, the lock should always be available. But if the
driver is currently probed/removed then the lock is not available and
dropping the package should be safe because this is what would have
happened if the package arrived some milliseconds earlier/later.
This also fixes another race condition while probing drivers:
First the *_probe function of the driver is called and only if that
succeeds, the related input device of hidinput is registered. If the low
level driver reports input events after the *_probe function returned
but before the input device is registered, then a NULL pointer
dereference will occur. (Equivalently on driver remove function).
This is not possible anymore, since the semaphore lock drops all
incoming packages until the driver/device is fully initialized.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'include/linux/hid.h')
-rw-r--r-- | include/linux/hid.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/include/linux/hid.h b/include/linux/hid.h index 9cf8e7ae7450..9c02d07af0d1 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h | |||
@@ -71,6 +71,7 @@ | |||
71 | #include <linux/timer.h> | 71 | #include <linux/timer.h> |
72 | #include <linux/workqueue.h> | 72 | #include <linux/workqueue.h> |
73 | #include <linux/input.h> | 73 | #include <linux/input.h> |
74 | #include <linux/semaphore.h> | ||
74 | 75 | ||
75 | /* | 76 | /* |
76 | * We parse each description item into this structure. Short items data | 77 | * We parse each description item into this structure. Short items data |
@@ -475,6 +476,7 @@ struct hid_device { /* device report descriptor */ | |||
475 | unsigned country; /* HID country */ | 476 | unsigned country; /* HID country */ |
476 | struct hid_report_enum report_enum[HID_REPORT_TYPES]; | 477 | struct hid_report_enum report_enum[HID_REPORT_TYPES]; |
477 | 478 | ||
479 | struct semaphore driver_lock; /* protects the current driver */ | ||
478 | struct device dev; /* device */ | 480 | struct device dev; /* device */ |
479 | struct hid_driver *driver; | 481 | struct hid_driver *driver; |
480 | struct hid_ll_driver *ll_driver; | 482 | struct hid_ll_driver *ll_driver; |