[PATCH v2 4/4] leds: pca9532: fix use-after-free on unbind with N2100 beeper
[email protected] Wed, 29 Jul 2026 15:43:30 +0800
| Newsgroups | org.kernel.vger.linux-leds,dev.linux.lists.imx,org.kernel.vger.linux-kernel,org.kernel.vger.stable |
|---|---|
| Message-ID | <[email protected]> |
From: Haibo Chen <[email protected]> The input device for the N2100 beeper is allocated with devm_input_allocate_device(), so it stays registered until devres cleanup runs after .remove() returns. pca9532_destroy_devices() only calls cancel_work_sync() and clears data->idev, but leaves the input device registered. In the window between cancel_work_sync() returning and devres unregistering the input device, a userspace EV_SND event can reach pca9532_event(), which calls schedule_work() and re-queues data->work after it was cancelled. Once devres frees the devm-allocated driver data, the work callback dereferences freed memory, resulting in a use-after-free. Explicitly unregister the input device before cancelling the work so that no new events can be delivered after the work has been cancelled. Fixes: 8614fb46637a ("leds: pca9532: Convert to devm_input_allocate_device()") Cc: [email protected] Assisted-by: VeroCoder:claude-sonnet-4 Signed-off-by: Haibo Chen <[email protected]> --- drivers/leds/leds-pca9532.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/leds/leds-pca9532.c b/drivers/leds/leds-pca9532.c index cee398d8275b8953b5dbdb741c36161f34bba029..eeb91b7466419325191200f6cd9a4dc81f8ee20c 100644 --- a/drivers/leds/leds-pca9532.c +++ b/drivers/leds/leds-pca9532.c @@ -385,6 +385,12 @@ static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs) break; case PCA9532_TYPE_N2100_BEEP: if (data->idev != NULL) { + /* + * Unregister the input device first so that no + * new EV_SND events can re-queue the work after + * it has been cancelled. + */ + input_unregister_device(data->idev); cancel_work_sync(&data->work); data->idev = NULL; } -- 2.34.1