* [PATCH] gpiolib-sysfs: fix use-after-free in error path
@ 2025-06-22 22:02 Antonio Quartulli
2025-06-23 7:16 ` Bartosz Golaszewski
0 siblings, 1 reply; 2+ messages in thread
From: Antonio Quartulli @ 2025-06-22 22:02 UTC (permalink / raw)
To: linux-gpio; +Cc: linus.walleij, brgl, linux-kernel, Antonio Quartulli
When invoking device_create_with_groups() its return
value is stored in `data->cdev_base`.
However, in case of faiure, `data` is first
freed and then derefernced in order to return
`data->cdev_base`.
Fix the use-after-free by extracting the error
code before free'ing `data`.
This issue was reported by Coverity Scan.
Addresses-Coverity-ID: 1644512 ("Memory - illegal accesses (USE_AFTER_FREE)")
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
drivers/gpio/gpiolib-sysfs.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index 956411fc467a..c4c21e25c682 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -741,6 +741,7 @@ int gpiochip_sysfs_register(struct gpio_device *gdev)
struct gpiodev_data *data;
struct gpio_chip *chip;
struct device *parent;
+ int err;
/*
* Many systems add gpio chips for SOC support very early,
@@ -781,8 +782,9 @@ int gpiochip_sysfs_register(struct gpio_device *gdev)
GPIOCHIP_NAME "%d",
chip->base);
if (IS_ERR(data->cdev_base)) {
+ err = PTR_ERR(data->cdev_base);
kfree(data);
- return PTR_ERR(data->cdev_base);
+ return err;
}
return 0;
--
2.49.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] gpiolib-sysfs: fix use-after-free in error path
2025-06-22 22:02 [PATCH] gpiolib-sysfs: fix use-after-free in error path Antonio Quartulli
@ 2025-06-23 7:16 ` Bartosz Golaszewski
0 siblings, 0 replies; 2+ messages in thread
From: Bartosz Golaszewski @ 2025-06-23 7:16 UTC (permalink / raw)
To: linux-gpio, Antonio Quartulli
Cc: Bartosz Golaszewski, linus.walleij, brgl, linux-kernel
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
On Mon, 23 Jun 2025 00:02:21 +0200, Antonio Quartulli wrote:
> When invoking device_create_with_groups() its return
> value is stored in `data->cdev_base`.
> However, in case of faiure, `data` is first
> freed and then derefernced in order to return
> `data->cdev_base`.
>
> Fix the use-after-free by extracting the error
> code before free'ing `data`.
>
> [...]
Thanks for the catch. I tweaked the commit message and added the Fixes: tag.
Applied, thanks!
[1/1] gpiolib-sysfs: fix use-after-free in error path
https://git.kernel.org/brgl/linux/c/e6bb78570f7d531622ec572ef9ddbe6e66ff16ce
Best regards,
--
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-06-23 7:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-22 22:02 [PATCH] gpiolib-sysfs: fix use-after-free in error path Antonio Quartulli
2025-06-23 7:16 ` Bartosz Golaszewski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox