Re: [PATCH v2] HID: corsair: use disable_work_sync() to tear down the LED workers

From: Jiri Kosina

Date: Thu Nov 19 2026 - 15:27:05 EST


On Fri, 4 Sep 2026, Danish Khateeb wrote:

> Tearing down the K90 LEDs races with their brightness workers in two
> different ways, and swapping cancel_work_sync() and
> led_classdev_unregister() around only trades one for the other.
>
> Cancelling before the unregister, as k90_cleanup_backlight() and
> k90_cleanup_macro_functions() have done since
> commit eb51c9f8cb4f ("HID: corsair: cancel worker before
> unregistering LED to fix use-after-free"), leaves
> led_classdev_unregister() free to re-arm the worker: it calls
> led_set_brightness(led_cdev, LED_OFF), which reaches
> k90_brightness_set() and schedule_work()s the item again. The structure
> holding that work is then freed while it is still linked into the
> worklist:
>
> BUG: KASAN: slab-use-after-free in __list_add_valid_or_report+0x186/0x210
> Read of size 8 at addr ffff888102a1ead0 by task kworker/0:1/11
> __queue_work+0xade/0x1350
> queue_work_on+0xb6/0xc0
> led_classdev_unregister+0x26b/0x340
> corsair_remove+0x1c2/0x2d0
> hid_device_remove+0xba/0x1e0
> usbhid_disconnect+0xa0/0xe0
> Allocated by task 129:
> corsair_probe+0x560/0xd50
> Freed by task 11:
> corsair_remove+0xec/0x2d0
>
> Cancelling after the unregister avoids that, but reintroduces precisely
> the use-after-free that commit set out to fix: a worker that has
> already tested led->removed and found it false goes on to dereference
> led->cdev.dev, which led_classdev_unregister() has meanwhile freed via
> device_unregister().
>
> Use disable_work_sync(), which provides both halves at once. It waits
> for a worker that is already executing, so nothing can be sitting
> between the led->removed test and the led->cdev.dev dereference when the
> device goes away, and it makes the subsequent schedule_work() from the
> LED_OFF callback fail rather than queue, so nothing is left on the
> worklist to be freed. The ordering then stops mattering. The removed
> flag is left alone; it is redundant for these paths now but harmless.
>
> The fail_sysfs error path in k90_init_macro_functions() unregisters
> before cancelling, and so still carries the dereference-after-free that
> was fixed in the teardown paths, so convert it too.
>
> Fixes: eb51c9f8cb4f ("HID: corsair: cancel worker before unregistering LED to fix use-after-free")
> Reported-by: syzbot+abcedffc9201f2bb66c2@xxxxxxxxxxxxxxxxxxxxxxxxx
> Link: https://syzkaller.appspot.com/bug?extid=abcedffc9201f2bb66c2
> Assisted-by: LLM
> Signed-off-by: Danish Khateeb <danishkhateeb03@xxxxxxxxx>

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

--
Jiri Kosina
SUSE Labs