From: sashiko-bot@kernel.org
To: "Dmitry Antipov" <dmantipov@yandex.ru>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v6 2/2] HID: roccat: use kref to manage device instances
Date: Fri, 16 Oct 2026 11:46:21 +0000 [thread overview]
Message-ID: <sashiko-outbox-174249@kernel.org> (raw)
In-Reply-To: <20261016113321.892452-3-dmantipov@yandex.ru>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] hid: roccat: UAF in roccat_device_available() during disconnect
--
commit bc24653b7f3a809f26552e2e26b0e0b92e805ecd
Author: Dmitry Antipov <dmantipov@yandex.ru>
HID: roccat: use kref to manage device instances
Use kref to manage 'struct roccat_device' instances and fix both memory
leaks and UaF-triggering races between roccat_open()/roccat_release()
and roccat_connect()/roccat_disconnect() pairs. To avoid the scenario
when opened device is disconnected and its slot indexed by minor number
is reused by another device, release the slot in roccat_free_device()
rather than in roccat_disconnect(). This way, there is a time frame when
disconnected device may still occupy the slot; to reject such a device,
add extra roccat_device_available() checks to roccat_open() and
roccat_ioctl(). Add extra WARN_ON() device check to roccat_disconnect()
and a few debugging quirks to roccat_free_device() as well.
Link: https://sashiko.dev/#/patchset/20260902094551.200587-1-dmantipov@yandex.ru?part=2
> diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c
> index 92a9b9f14f591..581dedde6d66e 100644
> --- a/drivers/hid/hid-roccat.c
> +++ b/drivers/hid/hid-roccat.c
[ ... ]
> @@ -370,23 +380,25 @@ EXPORT_SYMBOL_GPL(roccat_connect);
> void roccat_disconnect(int minor)
> {
> struct roccat_device *device;
> - struct device *dev;
>
> mutex_lock(&devices_lock);
> device = devices[minor];
> + if (WARN_ON(!device))
> + goto out;
>
> - dev = xchg(&device->dev, NULL);
> - device_destroy(dev->class, MKDEV(roccat_major, minor));
> + if (!WARN_ON(!roccat_device_available(device))) {
> + struct device *dev = xchg(&device->dev, NULL);
>
> - devices[minor] = NULL;
> + device_destroy(dev->class, MKDEV(roccat_major, minor));
> + }
>
> if (device->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);
> }
[Severity: High]
Can this concurrent disconnect path trigger a use-after-free in readers?
If CPU1 calls roccat_poll() or roccat_read(), they invoke
roccat_device_available() without holding devices_lock. That helper loads the
pointer locklessly:
drivers/hid/hid-roccat.c:roccat_device_available() {
struct device *dev = smp_load_acquire(&device->dev);
return dev ? device_is_registered(dev) : false;
}
If CPU1 is preempted right after smp_load_acquire(), and CPU2 runs
roccat_disconnect(), CPU2 will exchange device->dev to NULL and call
device_destroy(). This drops the final reference and frees the device memory.
When CPU1 resumes, it will pass the now-freed dev pointer to
device_is_registered(), causing a use-after-free when accessing
dev->kobj.state_in_sysfs.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261016113321.892452-1-dmantipov@yandex.ru?part=2
prev parent reply other threads:[~2026-10-16 11:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-29 6:57 [PATCH v5 0/3] Latest Roccat HID fixes Dmitry Antipov
2026-09-29 6:57 ` [PATCH v5 1/3] HID: roccat: use device_is_registered() to check whether device is available Dmitry Antipov
2026-09-29 7:14 ` sashiko-bot
2026-09-29 6:57 ` [PATCH v5 2/3] HID: roccat: fix device access in roccat_release() Dmitry Antipov
2026-09-29 6:57 ` [PATCH v5 3/3] HID: roccat: use kref to manage device instances Dmitry Antipov
2026-10-15 16:43 ` (subset) [PATCH v5 0/3] Latest Roccat HID fixes Benjamin Tissoires
2026-10-15 16:47 ` Benjamin Tissoires
2026-10-16 11:33 ` [PATCH v6 0/2] Last remaining " Dmitry Antipov
2026-10-16 11:33 ` [PATCH v6 1/2] HID: roccat: use device_is_registered() to check whether device is available Dmitry Antipov
2026-10-16 11:45 ` sashiko-bot
2026-10-16 11:33 ` [PATCH v6 2/2] HID: roccat: use kref to manage device instances Dmitry Antipov
2026-10-16 11:46 ` sashiko-bot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-174249@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dmantipov@yandex.ru \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox