Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] HID: corsair: do not re-schedule LED worker after it has been cancelled
@ 2026-08-31  7:41 Chen Changcheng
  2026-08-31  7:53 ` sashiko-bot
  0 siblings, 1 reply; 5+ messages in thread
From: Chen Changcheng @ 2026-08-31  7:41 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Jeffin Philip
  Cc: linux-input, linux-kernel, Chen Changcheng,
	syzbot+0a031a76585d1c7e737d, syzbot+0b8bff5929865345b29e

Commit eb51c9f8cb4f0 ("HID: corsair: cancel worker before unregistering
LED to fix use-after-free") moved cancel_work_sync() ahead of
led_classdev_unregister() in k90_cleanup_backlight() and
k90_cleanup_macro_functions().  led_classdev_unregister() internally
calls led_set_brightness(LED_OFF), which reaches the driver's
k90_brightness_set() callback.  Since that callback schedules the worker
unconditionally, the worker was re-queued after cancel_work_sync() had
drained it, and the subsequent kfree() freed a still-active work_struct:

    ODEBUG: free active (active state 0) object type: work_struct
            hint: k90_record_led_work

The removed flag check inside the worker itself only stops it from
dereferencing freed memory once it runs; it cannot prevent the re-queue.

Fix this by making k90_brightness_set() a no-op once removed is set, so
the LED_OFF update issued from led_classdev_unregister() can no longer
re-schedule the worker after it has been cancelled.  Also apply the
cancel-before-unregister ordering to the probe error path
(k90_init_macro_functions() fail_sysfs) for consistency.

Fixes: eb51c9f8cb4f0 ("HID: corsair: cancel worker before unregistering LED to fix use-after-free")
Reported-by: syzbot+0a031a76585d1c7e737d@syzkaller.appspotmail.com
Reported-by: syzbot+0b8bff5929865345b29e@syzkaller.appspotmail.com
https://lore.kernel.org/all/6a937393.1d9ded08.62e62.0113.GAE@google.com/
Signed-off-by: Chen Changcheng <chenchangcheng@kylinos.cn>
---
 drivers/hid/hid-corsair.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-corsair.c b/drivers/hid/hid-corsair.c
index 278c6efb565d..73b3c1ff78c6 100644
--- a/drivers/hid/hid-corsair.c
+++ b/drivers/hid/hid-corsair.c
@@ -194,6 +194,9 @@ static void k90_brightness_set(struct led_classdev *led_cdev,
 {
 	struct k90_led *led = container_of(led_cdev, struct k90_led, cdev);
 
+	if (led->removed)
+		return;
+
 	led->brightness = brightness;
 	schedule_work(&led->work);
 }
@@ -507,8 +510,8 @@ static int k90_init_macro_functions(struct hid_device *dev)
 
 fail_sysfs:
 	k90->record_led.removed = true;
-	led_classdev_unregister(&k90->record_led.cdev);
 	cancel_work_sync(&k90->record_led.work);
+	led_classdev_unregister(&k90->record_led.cdev);
 fail_record_led:
 	kfree(k90->record_led.cdev.name);
 fail_record_led_alloc:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] HID: corsair: fix two use-after-free bugs on device removal
@ 2026-08-17  7:23 Chen Changcheng
  2026-08-17  7:28 ` [PATCH] HID: corsair: do not re-schedule LED worker after it has been cancelled Chen Changcheng
  0 siblings, 1 reply; 5+ messages in thread
From: Chen Changcheng @ 2026-08-17  7:23 UTC (permalink / raw)
  To: jeffinphilip14
  Cc: bentiss, chenchangcheng, jikos, linux-input, linux-kernel,
	sashiko-bot, ccc194101

Hi Jeffin,

Thanks for the review and for the pointer to the syzbot report.

You are right: led_classdev_unregister() internally calls
led_set_brightness(LED_OFF), which reaches k90_brightness_set() and
re-schedules the worker after cancel_work_sync() has returned, so the
work_struct can still be queued when kfree() is called.  This is the
issue syzbot reported (extid=0a031a76585d1c7e737d).

The follow-up patch below makes k90_brightness_set() a no-op once
removed is set, so the LED_OFF update from led_classdev_unregister()
cannot re-schedule the worker after it has been cancelled.  It also
applies the same cancel-before-unregister ordering to the probe error
path in k90_init_macro_functions() for consistency.

Thanks,
Chen Changcheng


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-08-31  7:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31  7:41 [PATCH] HID: corsair: do not re-schedule LED worker after it has been cancelled Chen Changcheng
2026-08-31  7:53 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-08-17  7:23 [PATCH 0/2] HID: corsair: fix two use-after-free bugs on device removal Chen Changcheng
2026-08-17  7:28 ` [PATCH] HID: corsair: do not re-schedule LED worker after it has been cancelled Chen Changcheng
2026-08-17  7:47   ` sashiko-bot
2026-08-31  5:50   ` Jeffin Philip

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox