leds: tca6507: Fix use-after-free on unbind
Subsystems (2)
PATCH v2 1/1 leds: tca6507: Fix use-after-free on unbind
Since the LEDs are registered with devm_led_classdev_register(), they are unregistered only after tca6507_remove() has cancelled the work. Unregistering an LED that is on turns it off, which queues the work again from led_assign(), and the work then runs after devres has freed the chip:
BUG: KASAN: slab-use-after-free in assign_work+0x414/0x5b0 Read of size 8 at addr ffff888010d5c0c0 by task kworker/1:2/65 ... Last potentially related work creation: ... queue_work_on+0xb6/0xc0 led_assign+0x223/0x340 [leds_tca6507] led_classdev_unregister+0x26b/0x340 release_nodes+0xb3/0x140 devres_release_group+0x247/0x470 i2c_device_remove+0xbf/0x1b0
Set up the work with devm_work_autocancel() before the LEDs are registered, so that devres cancels it after they are gone, and drop tca6507_remove().
---
Notes:
Changes in v2:
- Move the new include to the top of the list, where it sorts
alphabetically (Lee)
- Drop the comment above devm_work_autocancel() (Lee)
- Add Andrew's Acked-by
- Link to v1: https://lore.kernel.org/all/[email protected]/
Tested in QEMU on a next-20260925 KASAN kernel, with the driver built as a module and a test-only module that adds a fake I2C adapter and a software node for a TCA6507 with two LEDs. With the LEDs on, unbinding hits the report above (38 reports in 20 unbind/bind cycles), and so does unloading the driver in 2 of 3 runs. With this patch there are no reports in 20 cycles or 3 unloads, and the LEDs come back after each rebind. A W=1 build with GPIOLIB is clean.
v2 was retested the same way on next-20260930: 39 reports in 20 cycles without the patch, none with it in 20 cycles or 3 unloads.
drivers/leds/leds-tca6507.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/leds/leds-tca6507.c +5 -10
@@ -71,6 +71,7 @@* not recognised when matching. */ +#include <linux/devm-helpers.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/leds.h>
@@ -735,9 +736,11 @@ static int tca6507_probe(struct i2c_client *client)return -ENOMEM ; tca->client = client; -INIT_WORK (&tca->work, tca6507_work);spin_lock_init (&tca->lock); -i2c_set_clientdata (client, tca); + + err =devm_work_autocancel (dev, &tca->work, tca6507_work); +if (err) +return err;for (i =0 ; i <NUM_LEDS ; i++) {struct tca6507_led *l = tca->leds + i;
@@ -766,20 +769,12 @@ static int tca6507_probe(struct i2c_client *client)return 0 ; } -static void tca6507_remove (struct i2c_client *client) -{ -struct tca6507_chip *tca =i2c_get_clientdata (client); - -cancel_work_sync (&tca->work); -} -static struct i2c_driver tca6507_driver = { .driver = { .name ="leds-tca6507" , .of_match_table =of_match_ptr (of_tca6507_leds_match), }, .probe = tca6507_probe, - .remove = tca6507_remove, .id_table = tca6507_id, };
base-commit: 05b4738b0078f7d6f154f68068a11c8a0635e9df
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] leds: tca6507: devres cancels work before LED off commands are sent --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] leds: tca6507: devres cancels work before LED off commands are sent --
commit 26c3a76d710837c5d81cb4e74292b31880a4a3d5
Author: Danish Khateeb <[email protected]>
leds: tca6507: Fix use-after-free on unbind
Set up the work with devm_work_autocancel() before the LEDs are registered, so that devres cancels it after they are gone, and drop tca6507_remove(). This resolves a previous use-after-free.
Quoted 4 lines
drivers/leds/leds-tca6507.c +0
-0
[ ... ]
drivers/leds/leds-tca6507.c static int tca6507_probe(struct i2c_client *client) · line 743
Quoted 11 lines
drivers/leds/leds-tca6507.c +4
-2@@ -735,9 +736,11 @@ static int tca6507_probe(struct i2c_client *client)return -ENOMEM ; tca->client = client; -INIT_WORK (&tca->work, tca6507_work);spin_lock_init (&tca->lock); -i2c_set_clientdata (client, tca); + + err =devm_work_autocancel (dev, &tca->work, tca6507_work); +if (err) +return err;
[Severity: Medium]
Since this devres action is registered before the LEDs are registered later in tca6507_probe(), doesn't it run after them during unbind?
When the driver is unbound, devres will first unregister the LEDs, which sets their brightness to LED_OFF. The driver's brightness_set callback will queue an I2C transaction via schedule_work(&tca->work).
Immediately after, devres invokes the devm_work_autocancel action, which calls cancel_work_sync(&tca->work). Since the work is still pending, it gets cancelled before it can run, meaning the I2C write to turn off the LEDs never occurs.
Although this resolves the previous use-after-free, does it prevent the LEDs from correctly turning off on teardown, causing the hardware LEDs to physically stay on after driver unbind?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Lee Jones
maintainerOn Thu, 15 Oct 2026, Danish Khateeb wrote:
Please review, explain or fix the Sashiko review(s).
Quoted 103 lines
Since the LEDs are registered with
devm_led_classdev_register(), they are unregistered only aftertca6507_remove()has cancelled the work. Unregistering an LED that is on turns it off, which queues the work again fromled_assign(), and the work then runs after devres has freed the chip:BUG: KASAN: slab-use-after-free in assign_work+0x414/0x5b0 Read of size 8 at addr ffff888010d5c0c0 by task kworker/1:2/65 ... Last potentially related work creation: ... queue_work_on+0xb6/0xc0 led_assign+0x223/0x340 [leds_tca6507] led_classdev_unregister+0x26b/0x340 release_nodes+0xb3/0x140 devres_release_group+0x247/0x470 i2c_device_remove+0xbf/0x1b0Set up the work with
devm_work_autocancel()before the LEDs are registered, so that devres cancels it after they are gone, and droptca6507_remove().Fixes 1b5c2fa7081c ("leds: tca6507: Use devm_led_classdev_register() to simplify remove path")Acked-by Andrew Davis <[email protected]>Assisted-by LLMSigned-off-by Danish Khateeb <[email protected]>---
Notes:
Changes in v2:
- Move the new include to the top of the list, where it sorts
alphabetically (Lee)
- Drop the comment abovedevm_work_autocancel()(Lee)
- Add Andrew's Acked-by
- Link to v1: https://lore.kernel.org/all/[email protected]/Tested in QEMU on a next-20260925 KASAN kernel, with the driver built as a module and a test-only module that adds a fake I2C adapter and a software node for a TCA6507 with two LEDs. With the LEDs on, unbinding hits the report above (38 reports in 20 unbind/bind cycles), and so does unloading the driver in 2 of 3 runs. With this patch there are no reports in 20 cycles or 3 unloads, and the LEDs come back after each rebind. A W=1 build with GPIOLIB is clean.
v2 was retested the same way on next-20260930: 39 reports in 20 cycles without the patch, none with it in 20 cycles or 3 unloads.
drivers/leds/leds-tca6507.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)drivers/leds/leds-tca6507.c +5
-10@@ -71,6 +71,7 @@* not recognised when matching. */ +#include <linux/devm-helpers.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/leds.h> @@ -735,9 +736,11 @@ static int tca6507_probe(struct i2c_client *client)return -ENOMEM ; tca->client = client; -INIT_WORK (&tca->work, tca6507_work);spin_lock_init (&tca->lock); -i2c_set_clientdata (client, tca); + + err =devm_work_autocancel (dev, &tca->work, tca6507_work); +if (err) +return err;for (i =0 ; i <NUM_LEDS ; i++) {struct tca6507_led *l = tca->leds + i;@@ -766,20 +769,12 @@ static int tca6507_probe(struct i2c_client *client)return 0 ; } -static void tca6507_remove (struct i2c_client *client) -{ -struct tca6507_chip *tca =i2c_get_clientdata (client); - -cancel_work_sync (&tca->work); -} -static struct i2c_driver tca6507_driver = { .driver = { .name ="leds-tca6507" , .of_match_table =of_match_ptr (of_tca6507_leds_match), }, .probe = tca6507_probe, - .remove = tca6507_remove, .id_table = tca6507_id, };base-commit:
05b4738b0078f7d6f154f68068a11c8a0635e9dfSignature · 1 line
2.56.0
Signature · 1 line
Lee Jones
Quoted 1 line · Lee Jones
Please review, explain or fix the Sashiko review(s).
Sashiko is right that the "LED off" write queued on unbind gets cancelled, but that predates 1b5c2fa7081c: remove() used to call led_classdev_unregister() and then cancel_work_sync(), the same order this patch restores. Since 1b5c2fa7081c that write only ran on freed memory. If you'd like the LEDs turned off on unbind, I can send a separate patch that flushes the work instead of cancelling it.
Thanks,
Danish
Lee Jones
maintainerQuoted 9 lines · Danish Khateeb
Quoted 1 line · Lee Jones
Please review, explain or fix the Sashiko review(s).
Sashiko is right that the "LED off" write queued on unbind gets cancelled, but that predates
1b5c2fa7081c:remove()used to callled_classdev_unregister()and thencancel_work_sync(), the same order this patch restores. Since1b5c2fa7081cthat write only ran on freed memory. If you'd like the LEDs turned off on unbind, I can send a separate patch that flushes the work instead of cancelling it.
Submit them as a set.
Signature · 1 line
Lee Jones