mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] HID: corsair: cancel LED work again after unregistering the LED
@ 2026-11-09 19:55 Andreas Claesson via B4 Relay
  2026-11-19 20:30 ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Claesson via B4 Relay @ 2026-11-09 19:55 UTC (permalink / raw)
  To: Benjamin Tissoires, Chen Changcheng, Jiri Kosina
  Cc: linux-input, linux-kernel

From: Andreas Claesson <andreas@aclaesson.com>

Commit eb51c9f8cb4f ("HID: corsair: cancel worker before unregistering
LED to fix use-after-free") moved cancel_work_sync() in front of
led_classdev_unregister() in k90_cleanup_backlight() and
k90_cleanup_macro_functions().

led_classdev_unregister() turns the LED off unless
LED_RETAIN_AT_SHUTDOWN is set. That calls k90_brightness_set(), which
unconditionally does schedule_work() on the same work item. The work is
therefore queued again after it was cancelled, and the structure that
embeds it is freed right afterwards:

  k90_cleanup_macro_functions()
    cancel_work_sync(&k90->record_led.work)
    led_classdev_unregister(&k90->record_led.cdev)
      led_set_brightness(LED_OFF)
        k90_brightness_set()
          schedule_work(&led->work)        <-- re-queued
    kfree(k90)                             <-- frees the queued work_struct

When the work later runs, the workqueue dereferences the freed
work_struct and k90_record_led_work() reads led->removed from freed
memory. A USB device that enumerates as a K90 and is then unplugged
triggers this reliably. With CONFIG_DEBUG_OBJECTS_WORK it shows up as:

  ODEBUG: free active (active state 0) object: ffff88803b8772c8 object type: work_struct hint: k90_record_led_work+0x0/0x1e0
  WARNING: lib/debugobjects.c:632 at debug_print_object+0xec/0x230
  Workqueue: usb_hub_wq hub_event
  Call Trace:
   debug_check_no_obj_freed+0x2e3/0x450 lib/debugobjects.c:1201
   kfree+0x13e/0x650 mm/slub.c:6923
   k90_cleanup_macro_functions drivers/hid/hid-corsair.c:547 [inline]
   corsair_remove+0xfd/0x260 drivers/hid/hid-corsair.c:602
   hid_device_remove+0xc5/0x1c0 drivers/hid/hid-core.c:2897
   ...
   usbhid_disconnect+0x9f/0xc0 drivers/hid/usbhid/hid-core.c:1486

(with ODEBUG enabled, work_fixup_free() cancels the work and hides the
use-after-free. Without it, the freed work item is executed.)

Keep the existing cancel_work_sync() before unregistering, which waits
for a worker that may already be past the 'removed' check, and cancel
the work once more after led_classdev_unregister() so the work queued
by the LED-off request is gone before the memory is freed. A re-queued
work that manages to run before the second cancel still sees
'removed == true' while the structure is alive and returns early.

Fixes: eb51c9f8cb4f ("HID: corsair: cancel worker before unregistering LED to fix use-after-free")
Cc: stable@vger.kernel.org
Signed-off-by: Andreas Claesson <andreas@aclaesson.com>
---
Found with syzkaller (USB emulation, KASAN + ODEBUG) on mainline
f66469d9905b. Both the record LED and the backlight paths reproduce
(hint: k90_record_led_work / k90_backlight_work); a minimised
3-call syzkaller reproducer is available on request. With this patch,
200 probe/removal cycles per path gave no ODEBUG or KASAN reports,
against ODEBUG on roughly half of removals without it.
---
 drivers/hid/hid-corsair.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/hid/hid-corsair.c b/drivers/hid/hid-corsair.c
index 278c6efb565d..75c862f5f783 100644
--- a/drivers/hid/hid-corsair.c
+++ b/drivers/hid/hid-corsair.c
@@ -526,6 +526,8 @@ static void k90_cleanup_backlight(struct hid_device *dev)
 		drvdata->backlight->removed = true;
 		cancel_work_sync(&drvdata->backlight->work);
 		led_classdev_unregister(&drvdata->backlight->cdev);
+		/* unregister turns the LED off, which re-queues the work */
+		cancel_work_sync(&drvdata->backlight->work);
 		kfree(drvdata->backlight->cdev.name);
 		kfree(drvdata->backlight);
 	}
@@ -542,6 +544,8 @@ static void k90_cleanup_macro_functions(struct hid_device *dev)
 		k90->record_led.removed = true;
 		cancel_work_sync(&k90->record_led.work);
 		led_classdev_unregister(&k90->record_led.cdev);
+		/* unregister turns the LED off, which re-queues the work */
+		cancel_work_sync(&k90->record_led.work);
 		kfree(k90->record_led.cdev.name);
 
 		kfree(k90);

---
base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1
change-id: 20261109-hid-corsair-k90-uaf-0c711197ce3e

Best regards,
--  
Andreas Claesson <andreas@aclaesson.com>



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

* Re: [PATCH] HID: corsair: cancel LED work again after unregistering the LED
  2026-11-09 19:55 [PATCH] HID: corsair: cancel LED work again after unregistering the LED Andreas Claesson via B4 Relay
@ 2026-11-19 20:30 ` Jiri Kosina
  2026-11-20 12:32   ` Andreas Claesson
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Kosina @ 2026-11-19 20:30 UTC (permalink / raw)
  To: andreas; +Cc: Benjamin Tissoires, Chen Changcheng, linux-input, linux-kernel

On Mon, 9 Nov 2026, Andreas Claesson via B4 Relay wrote:

> From: Andreas Claesson <andreas@aclaesson.com>
> 
> Commit eb51c9f8cb4f ("HID: corsair: cancel worker before unregistering
> LED to fix use-after-free") moved cancel_work_sync() in front of
> led_classdev_unregister() in k90_cleanup_backlight() and
> k90_cleanup_macro_functions().
> 
> led_classdev_unregister() turns the LED off unless
> LED_RETAIN_AT_SHUTDOWN is set. That calls k90_brightness_set(), which
> unconditionally does schedule_work() on the same work item. The work is
> therefore queued again after it was cancelled, and the structure that
> embeds it is freed right afterwards:
> 
>   k90_cleanup_macro_functions()
>     cancel_work_sync(&k90->record_led.work)
>     led_classdev_unregister(&k90->record_led.cdev)
>       led_set_brightness(LED_OFF)
>         k90_brightness_set()
>           schedule_work(&led->work)        <-- re-queued
>     kfree(k90)                             <-- frees the queued work_struct
> 
> When the work later runs, the workqueue dereferences the freed
> work_struct and k90_record_led_work() reads led->removed from freed
> memory. A USB device that enumerates as a K90 and is then unplugged
> triggers this reliably. With CONFIG_DEBUG_OBJECTS_WORK it shows up as:
> 
>   ODEBUG: free active (active state 0) object: ffff88803b8772c8 object type: work_struct hint: k90_record_led_work+0x0/0x1e0
>   WARNING: lib/debugobjects.c:632 at debug_print_object+0xec/0x230
>   Workqueue: usb_hub_wq hub_event
>   Call Trace:
>    debug_check_no_obj_freed+0x2e3/0x450 lib/debugobjects.c:1201
>    kfree+0x13e/0x650 mm/slub.c:6923
>    k90_cleanup_macro_functions drivers/hid/hid-corsair.c:547 [inline]
>    corsair_remove+0xfd/0x260 drivers/hid/hid-corsair.c:602
>    hid_device_remove+0xc5/0x1c0 drivers/hid/hid-core.c:2897
>    ...
>    usbhid_disconnect+0x9f/0xc0 drivers/hid/usbhid/hid-core.c:1486
> 
> (with ODEBUG enabled, work_fixup_free() cancels the work and hides the
> use-after-free. Without it, the freed work item is executed.)
> 
> Keep the existing cancel_work_sync() before unregistering, which waits
> for a worker that may already be past the 'removed' check, and cancel
> the work once more after led_classdev_unregister() so the work queued
> by the LED-off request is gone before the memory is freed. A re-queued
> work that manages to run before the second cancel still sees
> 'removed == true' while the structure is alive and returns early.
> 
> Fixes: eb51c9f8cb4f ("HID: corsair: cancel worker before unregistering LED to fix use-after-free")
> Cc: stable@vger.kernel.org
> Signed-off-by: Andreas Claesson <andreas@aclaesson.com>
> ---
> Found with syzkaller (USB emulation, KASAN + ODEBUG) on mainline
> f66469d9905b. Both the record LED and the backlight paths reproduce
> (hint: k90_record_led_work / k90_backlight_work); a minimised
> 3-call syzkaller reproducer is available on request. With this patch,
> 200 probe/removal cycles per path gave no ODEBUG or KASAN reports,
> against ODEBUG on roughly half of removals without it.

Applied to hid.git#for-7.4/upstream-fixes, thanks.

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH] HID: corsair: cancel LED work again after unregistering the LED
  2026-11-19 20:30 ` Jiri Kosina
@ 2026-11-20 12:32   ` Andreas Claesson
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Claesson @ 2026-11-20 12:32 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Benjamin Tissoires, Chen Changcheng, Danish Khateeb, linux-input,
	linux-kernel

On Thu, 19 Nov 2026, Jiri Kosina wrote:
> Applied to hid.git#for-7.4/upstream-fixes, thanks.

Thanks, but I think this one should be dropped again, sorry for the
noise.

for-7.4/upstream-fixes also has cc8d022e8679 ("HID: corsair: use
disable_work_sync() to tear down the LED workers") from Danish, which
fixes the same bug and which I had missed when I sent mine. With the
work disabled before led_classdev_unregister(), the LED-off request can
no longer queue it, so the cancel_work_sync() calls my patch adds on
top (e624a56578e5) do nothing, and the comment it adds is no longer
true.

It is harmless, so if dropping it is inconvenient I can send a revert
instead, whichever you prefer.

Thanks,
Andreas

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

end of thread, other threads:[~2026-11-20 12:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 19:55 [PATCH] HID: corsair: cancel LED work again after unregistering the LED Andreas Claesson via B4 Relay
2026-11-19 20:30 ` Jiri Kosina
2026-11-20 12:32   ` Andreas Claesson

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

all inboxes | Powered by JetHome®