Skip to content

Commit 8052ee5

Browse files
Julia LawallJiri Kosina
authored andcommitted
HID: drivers/hid/hid-roccat.c: eliminate a null pointer dereference
It is not possible to take the lock in device if device is NULL. The mutex_lock is thus moved after the NULL test. New error handling labels are added at the end to differentiate between the cases where different sets of locks should be unlocks, and between whether or not reader should be freed (only on error). The semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // <smpl> @r@ expression E, E1; identifier f; statement S1,S2,S3; @@ if (E == NULL) { ... when != if (E == NULL || ...) S1 else S2 when != E = E1 *E->f ... when any return ...; } else S3 // </smpl> Signed-off-by: Julia Lawall <julia@diku.dk> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent ad734bc commit 8052ee5

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

‎drivers/hid/hid-roccat.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,27 +162,27 @@ static int roccat_open(struct inode *inode, struct file *file)
162162

163163
device = devices[minor];
164164

165-
mutex_lock(&device->readers_lock);
166-
167165
if (!device) {
168166
pr_emerg("roccat device with minor %d doesn't exist\n", minor);
169167
error = -ENODEV;
170-
goto exit_err;
168+
goto exit_err_devices;
171169
}
172170

171+
mutex_lock(&device->readers_lock);
172+
173173
if (!device->open++) {
174174
/* power on device on adding first reader */
175175
error = hid_hw_power(device->hid, PM_HINT_FULLON);
176176
if (error < 0) {
177177
--device->open;
178-
goto exit_err;
178+
goto exit_err_readers;
179179
}
180180

181181
error = hid_hw_open(device->hid);
182182
if (error < 0) {
183183
hid_hw_power(device->hid, PM_HINT_NORMAL);
184184
--device->open;
185-
goto exit_err;
185+
goto exit_err_readers;
186186
}
187187
}
188188

@@ -193,13 +193,13 @@ static int roccat_open(struct inode *inode, struct file *file)
193193
list_add_tail(&reader->node, &device->readers);
194194
file->private_data = reader;
195195

196-
exit_unlock:
196+
exit_err_readers:
197197
mutex_unlock(&device->readers_lock);
198+
exit_err_devices:
198199
mutex_unlock(&devices_lock);
200+
if (error)
201+
kfree(reader);
199202
return error;
200-
exit_err:
201-
kfree(reader);
202-
goto exit_unlock;
203203
}
204204

205205
static int roccat_release(struct inode *inode, struct file *file)

0 commit comments

Comments
 (0)