Skip to content

Commit 879e79c

Browse files
LDVGgregkh
authored andcommitted
HID: hidraw: fix data race on device refcount
commit 944ee77 upstream. The hidraw_open() function increments the hidraw device reference counter. The counter has no dedicated synchronization mechanism, resulting in a potential data race when concurrently opening a device. The race is a regression introduced by commit 8590222 ("HID: hidraw: Replace hidraw device table mutex with a rwsem"). While minors_rwsem is intended to protect the hidraw_table itself, by instead acquiring the lock for writing, the reference counter is also protected. This is symmetrical to hidraw_release(). Link: systemd/systemd#27947 Fixes: 8590222 ("HID: hidraw: Replace hidraw device table mutex with a rwsem") Cc: [email protected] Signed-off-by: Ludvig Michaelsson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cae8542 commit 879e79c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

drivers/hid/hidraw.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@ static int hidraw_open(struct inode *inode, struct file *file)
272272
goto out;
273273
}
274274

275-
down_read(&minors_rwsem);
275+
/*
276+
* Technically not writing to the hidraw_table but a write lock is
277+
* required to protect the device refcount. This is symmetrical to
278+
* hidraw_release().
279+
*/
280+
down_write(&minors_rwsem);
276281
if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
277282
err = -ENODEV;
278283
goto out_unlock;
@@ -301,7 +306,7 @@ static int hidraw_open(struct inode *inode, struct file *file)
301306
spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
302307
file->private_data = list;
303308
out_unlock:
304-
up_read(&minors_rwsem);
309+
up_write(&minors_rwsem);
305310
out:
306311
if (err < 0)
307312
kfree(list);

0 commit comments

Comments
 (0)