[lvc-project] [PATCH v2 4/4] HID: roccat: use kref to manage device instances
Dmitry Antipov
dmantipov at yandex.ru
Mon Sep 14 15:02:40 MSK 2026
Use kref to manage 'struct roccat_device' instances and fix
UaF-triggering race between roccat_open()/roccat_release()
and roccat_connect()/roccat_disconnect() pairs.
Reported-by: syzbot+d632e93ffcd1452bc61e at syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d632e93ffcd1452bc61e
Link: https://sashiko.dev/#/patchset/20260902094551.200587-1-dmantipov@yandex.ru?part=2
Signed-off-by: Dmitry Antipov <dmantipov at yandex.ru>
---
v2: unconditionally get/put device reference during
first open and last close, respectively (Sashiko)
---
drivers/hid/hid-roccat.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c
index d1df4ec34a1e..32927b20bcfe 100644
--- a/drivers/hid/hid-roccat.c
+++ b/drivers/hid/hid-roccat.c
@@ -40,6 +40,7 @@ struct roccat_device {
unsigned int minor;
int report_size;
int exist;
+ struct kref ref;
wait_queue_head_t wait;
struct device *dev;
struct hid_device *hid;
@@ -69,10 +70,12 @@ static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
/* protects modifications of devices array */
static DEFINE_MUTEX(devices_lock);
-static void roccat_free_device(struct roccat_device *device)
+static void roccat_free_device(struct kref *ref)
{
+ struct roccat_device *device;
int i;
+ device = container_of(ref, struct roccat_device, ref);
for (i = 0; i < ROCCAT_CBUF_SIZE; i++)
kfree(device->cbuf[i].value);
kfree(device);
@@ -187,6 +190,7 @@ static int roccat_open(struct inode *inode, struct file *file)
hid_hw_power(device->hid, PM_HINT_NORMAL);
goto exit_err_readers;
}
+ kref_get(&device->ref);
}
reader->device = device;
@@ -227,9 +231,8 @@ static int roccat_release(struct inode *inode, struct file *file)
if (device->exist) {
hid_hw_power(device->hid, PM_HINT_NORMAL);
hid_hw_close(device->hid);
- } else {
- roccat_free_device(device);
}
+ kref_put(&device->ref, roccat_free_device);
}
mutex_unlock(&devices_lock);
@@ -341,6 +344,7 @@ int roccat_connect(const struct class *klass, struct hid_device *hid, int report
INIT_LIST_HEAD(&device->readers);
mutex_init(&device->readers_lock);
mutex_init(&device->cbuf_lock);
+ kref_init(&device->ref);
device->minor = minor;
device->hid = hid;
device->exist = 1;
@@ -362,6 +366,8 @@ void roccat_disconnect(int minor)
mutex_lock(&devices_lock);
device = devices[minor];
+ if (!device)
+ goto out;
device->exist = 0; /* TODO exist maybe not needed */
@@ -376,10 +382,10 @@ void roccat_disconnect(int minor)
if (open) {
hid_hw_close(device->hid);
wake_up_interruptible(&device->wait);
- } else {
- roccat_free_device(device);
}
+ kref_put(&device->ref, roccat_free_device);
+out:
mutex_unlock(&devices_lock);
}
EXPORT_SYMBOL_GPL(roccat_disconnect);
--
2.55.0
More information about the lvc-project
mailing list