public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpio: Avoid NULL dereference in linehandle_create()
@ 2026-02-13 17:29 Douglas Anderson
  2026-02-15 11:32 ` Kent Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: Douglas Anderson @ 2026-02-13 17:29 UTC (permalink / raw)
  To: Linus Walleij, Christian Brauner, Bartosz Golaszewski,
	Kent Gibson
  Cc: Douglas Anderson, linux-gpio, linux-kernel

In linehandle_create(), there is a statement like this:
  retain_and_null_ptr(lh);

Soon after, there is a debug printout that dereferences "lh", which
will crash things.

Avoid the crash by reading value needed by the printout before making
"lh" NULL.

Fixes: da7e394bf58f ("gpio: convert linehandle_create() to FD_PREPARE()")
Signed-off-by: Douglas Anderson <dianders@chromium.org>
---

 drivers/gpio/gpiolib-cdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 2adc3c070908..13ab6a5ebb76 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -304,6 +304,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 {
 	struct gpiohandle_request handlereq;
 	struct linehandle_state *lh __free(linehandle_free) = NULL;
+	u32 num_descs;
 	int i, ret;
 	u32 lflags;
 
@@ -379,6 +380,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 				      lh, O_RDONLY | O_CLOEXEC));
 	if (fdf.err)
 		return fdf.err;
+	num_descs = lh->num_descs;
 	retain_and_null_ptr(lh);
 
 	handlereq.fd = fd_prepare_fd(fdf);
@@ -388,7 +390,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	fd_publish(fdf);
 
 	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
-		lh->num_descs);
+		num_descs);
 
 	return 0;
 }
-- 
2.53.0.273.g2a3d683680-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] gpio: Avoid NULL dereference in linehandle_create()
  2026-02-13 17:29 [PATCH] gpio: Avoid NULL dereference in linehandle_create() Douglas Anderson
@ 2026-02-15 11:32 ` Kent Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: Kent Gibson @ 2026-02-15 11:32 UTC (permalink / raw)
  To: Douglas Anderson
  Cc: Linus Walleij, Christian Brauner, Bartosz Golaszewski, linux-gpio,
	linux-kernel

On Fri, Feb 13, 2026 at 09:29:31AM -0800, Douglas Anderson wrote:
> In linehandle_create(), there is a statement like this:
>   retain_and_null_ptr(lh);
> 
> Soon after, there is a debug printout that dereferences "lh", which
> will crash things.
> 
> Avoid the crash by reading value needed by the printout before making
> "lh" NULL.
> 
> Fixes: da7e394bf58f ("gpio: convert linehandle_create() to FD_PREPARE()")
> Signed-off-by: Douglas Anderson <dianders@chromium.org>
> ---
> 
>  drivers/gpio/gpiolib-cdev.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
> index 2adc3c070908..13ab6a5ebb76 100644
> --- a/drivers/gpio/gpiolib-cdev.c
> +++ b/drivers/gpio/gpiolib-cdev.c
> @@ -304,6 +304,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
>  {
>  	struct gpiohandle_request handlereq;
>  	struct linehandle_state *lh __free(linehandle_free) = NULL;
> +	u32 num_descs;
>  	int i, ret;
>  	u32 lflags;
>  
> @@ -379,6 +380,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
>  				      lh, O_RDONLY | O_CLOEXEC));
>  	if (fdf.err)
>  		return fdf.err;
> +	num_descs = lh->num_descs;
>  	retain_and_null_ptr(lh);
>  
>  	handlereq.fd = fd_prepare_fd(fdf);
> @@ -388,7 +390,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
>  	fd_publish(fdf);
>  
>  	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
> -		lh->num_descs);
> +		num_descs);
>  

Use handlereq.lines rather than creating another local shadow.

Cheers,
Kent.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-15 11:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-13 17:29 [PATCH] gpio: Avoid NULL dereference in linehandle_create() Douglas Anderson
2026-02-15 11:32 ` Kent Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox