HID: hidraw: fix window in hidraw_release
authorJiri Slaby <jslaby@suse.cz>
Tue, 19 Oct 2010 09:29:55 +0000 (11:29 +0200)
committerJiri Kosina <jkosina@suse.cz>
Wed, 20 Oct 2010 14:54:04 +0000 (16:54 +0200)
There is a window between hidraw_table check and its dereference.
In that window, the device may be unplugged and removed form the
system and we will then dereference NULL.

Lock that place properly so that either we get NULL and jump out or we
can work with real pointer.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
drivers/hid/hidraw.c

index 47d70c5..5a6f99b 100644 (file)
@@ -212,9 +212,13 @@ static int hidraw_release(struct inode * inode, struct file * file)
        unsigned int minor = iminor(inode);
        struct hidraw *dev;
        struct hidraw_list *list = file->private_data;
+       int ret;
 
-       if (!hidraw_table[minor])
-               return -ENODEV;
+       mutex_lock(&minors_lock);
+       if (!hidraw_table[minor]) {
+               ret = -ENODEV;
+               goto unlock;
+       }
 
        list_del(&list->node);
        dev = hidraw_table[minor];
@@ -227,10 +231,12 @@ static int hidraw_release(struct inode * inode, struct file * file)
                        kfree(list->hidraw);
                }
        }
-
        kfree(list);
+       ret = 0;
+unlock:
+       mutex_unlock(&minors_lock);
 
-       return 0;
+       return ret;
 }
 
 static long hidraw_ioctl(struct file *file, unsigned int cmd,