Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@mandelbit.com>
To: linux-gpio@vger.kernel.org
Cc: linus.walleij@linaro.org, brgl@bgdev.pl,
	linux-kernel@vger.kernel.org,
	Antonio Quartulli <antonio@mandelbit.com>
Subject: [PATCH] gpiolib-sysfs: fix use-after-free in error path
Date: Mon, 23 Jun 2025 00:02:21 +0200	[thread overview]
Message-ID: <20250622220221.28025-1-antonio@mandelbit.com> (raw)

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


             reply	other threads:[~2025-06-22 22:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-22 22:02 Antonio Quartulli [this message]
2025-06-23  7:16 ` [PATCH] gpiolib-sysfs: fix use-after-free in error path Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250622220221.28025-1-antonio@mandelbit.com \
    --to=antonio@mandelbit.com \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox