[PATCH] gpio: cdev: fix memory leak in lineinfo_changed_notify
From: l1za0 . sec
Date: Thu Nov 26 2026 - 07:48:26 EST
From: Haocheng Yu <l1za0.sec@xxxxxxxxx>
Commit 1feb5d39b05a ("gpio: cdev: fix kernel stack leak to
user-space in error path") made gpio_desc_to_lineinfo() return an error
when the GPIO chip guard cannot be acquired, introducing a new error
path in lineinfo_changed_notify().
This error path can be reached when a watched line changes state while
its GPIO chip is being removed. The state-change path may decide to
emit a notification while the chip is still present, but before
gpio_desc_to_lineinfo() enters its own SRCU read-side critical section,
gpiochip_remove() may clear gdev->chip. The newly acquired chip guard
then observes a NULL chip and gpio_desc_to_lineinfo() returns -ENODEV.
Driver unbind, hot-unplug, or device-tree overlay removal can create
this race with line request, release, or configuration changes.
At this point lineinfo_changed_notify() drops the file reference and
returns, but it neither frees the newly allocated notification context
nor queues the work that normally takes ownership of and frees it.
Free the context before returning to avoid leaking it whenever the race
is hit.
Fixes: 1feb5d39b05a ("gpio: cdev: fix kernel stack leak to user-space in error path")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Haocheng Yu <l1za0.sec@xxxxxxxxx>
---
drivers/gpio/gpiolib-cdev.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 5d53bfcdf726..1146198415f8 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -2535,6 +2535,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb,
ret = gpio_desc_to_lineinfo(desc, &ctx->chg.info, true);
if (ret) {
fput(fp);
+ kfree(ctx);
return NOTIFY_DONE;
}
--
2.43.0