linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction
@ 2024-10-24 13:38 Bartosz Golaszewski
  2024-10-24 13:44 ` Klara Modin
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2024-10-24 13:38 UTC (permalink / raw)
  To: Linus Walleij, Kent Gibson, Mark Brown, Klara Modin
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

For optional GPIOs we may pass NULL to gpiod_direction_(input|output)().
With the call to the notifier chain added by commit 07c61d4da43f
("gpiolib: notify user-space about in-kernel line state changes") we
will now dereference a NULL pointer in this case. The reason for that is
the fact that the expansion of the VALIDATE_DESC() macro (which returns
0 for NULL descriptors) was moved into the nonotify variants of the
direction setters.

Move them back to the top-level interfaces as the nonotify ones are only
ever called from inside the GPIO core and are always passed valid GPIO
descriptors. This way we'll never call the line_state notifier chain
with non-valid descs.

Fixes: 07c61d4da43f ("gpiolib: notify user-space about in-kernel line state changes")
Reported-by: Mark Brown <broonie@kernel.org>
Closes: https://lore.kernel.org/all/d6601a31-7685-4b21-9271-1b76116cc483@sirena.org.uk/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index ae758ba6dc3d1..6001ec96693c5 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -2695,6 +2695,8 @@ int gpiod_direction_input(struct gpio_desc *desc)
 {
 	int ret;
 
+	VALIDATE_DESC(desc);
+
 	ret = gpiod_direction_input_nonotify(desc);
 	if (ret == 0)
 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
@@ -2707,8 +2709,6 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
 {
 	int ret = 0;
 
-	VALIDATE_DESC(desc);
-
 	CLASS(gpio_chip_guard, guard)(desc);
 	if (!guard.gc)
 		return -ENODEV;
@@ -2841,6 +2841,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 {
 	int ret;
 
+	VALIDATE_DESC(desc);
+
 	ret = gpiod_direction_output_nonotify(desc, value);
 	if (ret == 0)
 		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
@@ -2854,8 +2856,6 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
 	unsigned long flags;
 	int ret;
 
-	VALIDATE_DESC(desc);
-
 	flags = READ_ONCE(desc->flags);
 
 	if (test_bit(FLAG_ACTIVE_LOW, &flags))
-- 
2.30.2


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

* Re: [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction
  2024-10-24 13:38 [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction Bartosz Golaszewski
@ 2024-10-24 13:44 ` Klara Modin
  2024-10-24 16:25   ` Bartosz Golaszewski
  2024-10-24 16:52 ` Mark Brown
  2024-10-24 19:04 ` Bartosz Golaszewski
  2 siblings, 1 reply; 5+ messages in thread
From: Klara Modin @ 2024-10-24 13:44 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij, Kent Gibson, Mark Brown
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

On 2024-10-24 15:38, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> For optional GPIOs we may pass NULL to gpiod_direction_(input|output)().
> With the call to the notifier chain added by commit 07c61d4da43f
> ("gpiolib: notify user-space about in-kernel line state changes") we
> will now dereference a NULL pointer in this case. The reason for that is
> the fact that the expansion of the VALIDATE_DESC() macro (which returns
> 0 for NULL descriptors) was moved into the nonotify variants of the
> direction setters.
> 
> Move them back to the top-level interfaces as the nonotify ones are only
> ever called from inside the GPIO core and are always passed valid GPIO
> descriptors. This way we'll never call the line_state notifier chain
> with non-valid descs.
> 
> Fixes: 07c61d4da43f ("gpiolib: notify user-space about in-kernel line state changes")
> Reported-by: Mark Brown <broonie@kernel.org>
> Closes: https://lore.kernel.org/all/d6601a31-7685-4b21-9271-1b76116cc483@sirena.org.uk/
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
>   drivers/gpio/gpiolib.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index ae758ba6dc3d1..6001ec96693c5 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -2695,6 +2695,8 @@ int gpiod_direction_input(struct gpio_desc *desc)
>   {
>   	int ret;
>   
> +	VALIDATE_DESC(desc);
> +
>   	ret = gpiod_direction_input_nonotify(desc);
>   	if (ret == 0)
>   		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
> @@ -2707,8 +2709,6 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
>   {
>   	int ret = 0;
>   
> -	VALIDATE_DESC(desc);
> -
>   	CLASS(gpio_chip_guard, guard)(desc);
>   	if (!guard.gc)
>   		return -ENODEV;
> @@ -2841,6 +2841,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
>   {
>   	int ret;
>   
> +	VALIDATE_DESC(desc);
> +
>   	ret = gpiod_direction_output_nonotify(desc, value);
>   	if (ret == 0)
>   		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
> @@ -2854,8 +2856,6 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
>   	unsigned long flags;
>   	int ret;
>   
> -	VALIDATE_DESC(desc);
> -
>   	flags = READ_ONCE(desc->flags);
>   
>   	if (test_bit(FLAG_ACTIVE_LOW, &flags))

This patch fixes the issue for me, thanks!

Tested-by: Klara Modin <klarasmodin@gmail.com>

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

* Re: [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction
  2024-10-24 13:44 ` Klara Modin
@ 2024-10-24 16:25   ` Bartosz Golaszewski
  0 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2024-10-24 16:25 UTC (permalink / raw)
  To: Mark Brown
  Cc: Linus Walleij, Kent Gibson, linux-gpio, linux-kernel,
	Bartosz Golaszewski, Klara Modin

On Thu, Oct 24, 2024 at 3:44 PM Klara Modin <klarasmodin@gmail.com> wrote:
>
> On 2024-10-24 15:38, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > For optional GPIOs we may pass NULL to gpiod_direction_(input|output)().
> > With the call to the notifier chain added by commit 07c61d4da43f
> > ("gpiolib: notify user-space about in-kernel line state changes") we
> > will now dereference a NULL pointer in this case. The reason for that is
> > the fact that the expansion of the VALIDATE_DESC() macro (which returns
> > 0 for NULL descriptors) was moved into the nonotify variants of the
> > direction setters.
> >
> > Move them back to the top-level interfaces as the nonotify ones are only
> > ever called from inside the GPIO core and are always passed valid GPIO
> > descriptors. This way we'll never call the line_state notifier chain
> > with non-valid descs.
> >
> > Fixes: 07c61d4da43f ("gpiolib: notify user-space about in-kernel line state changes")
> > Reported-by: Mark Brown <broonie@kernel.org>
> > Closes: https://lore.kernel.org/all/d6601a31-7685-4b21-9271-1b76116cc483@sirena.org.uk/
> > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > ---
> >   drivers/gpio/gpiolib.c | 8 ++++----
> >   1 file changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> > index ae758ba6dc3d1..6001ec96693c5 100644
> > --- a/drivers/gpio/gpiolib.c
> > +++ b/drivers/gpio/gpiolib.c
> > @@ -2695,6 +2695,8 @@ int gpiod_direction_input(struct gpio_desc *desc)
> >   {
> >       int ret;
> >
> > +     VALIDATE_DESC(desc);
> > +
> >       ret = gpiod_direction_input_nonotify(desc);
> >       if (ret == 0)
> >               gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
> > @@ -2707,8 +2709,6 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
> >   {
> >       int ret = 0;
> >
> > -     VALIDATE_DESC(desc);
> > -
> >       CLASS(gpio_chip_guard, guard)(desc);
> >       if (!guard.gc)
> >               return -ENODEV;
> > @@ -2841,6 +2841,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
> >   {
> >       int ret;
> >
> > +     VALIDATE_DESC(desc);
> > +
> >       ret = gpiod_direction_output_nonotify(desc, value);
> >       if (ret == 0)
> >               gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
> > @@ -2854,8 +2856,6 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
> >       unsigned long flags;
> >       int ret;
> >
> > -     VALIDATE_DESC(desc);
> > -
> >       flags = READ_ONCE(desc->flags);
> >
> >       if (test_bit(FLAG_ACTIVE_LOW, &flags))
>
> This patch fixes the issue for me, thanks!
>
> Tested-by: Klara Modin <klarasmodin@gmail.com>

Mark: if that fixes the issue for you as well, I'd like to pick up
quickly and fix linux-next ASAP. Let me know.

Thanks in advance,
Bartosz

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

* Re: [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction
  2024-10-24 13:38 [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction Bartosz Golaszewski
  2024-10-24 13:44 ` Klara Modin
@ 2024-10-24 16:52 ` Mark Brown
  2024-10-24 19:04 ` Bartosz Golaszewski
  2 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2024-10-24 16:52 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, Kent Gibson, Klara Modin, linux-gpio, linux-kernel,
	Bartosz Golaszewski

[-- Attachment #1: Type: text/plain, Size: 720 bytes --]

On Thu, Oct 24, 2024 at 03:38:34PM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> For optional GPIOs we may pass NULL to gpiod_direction_(input|output)().
> With the call to the notifier chain added by commit 07c61d4da43f
> ("gpiolib: notify user-space about in-kernel line state changes") we
> will now dereference a NULL pointer in this case. The reason for that is
> the fact that the expansion of the VALIDATE_DESC() macro (which returns
> 0 for NULL descriptors) was moved into the nonotify variants of the
> direction setters.

I didn't test all the boards but this does get several of them working
so

Tested-by: Mark Brown <broonie@kernel.org>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction
  2024-10-24 13:38 [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction Bartosz Golaszewski
  2024-10-24 13:44 ` Klara Modin
  2024-10-24 16:52 ` Mark Brown
@ 2024-10-24 19:04 ` Bartosz Golaszewski
  2 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2024-10-24 19:04 UTC (permalink / raw)
  To: Linus Walleij, Kent Gibson, Mark Brown, Klara Modin,
	Bartosz Golaszewski
  Cc: Bartosz Golaszewski, linux-gpio, linux-kernel

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


On Thu, 24 Oct 2024 15:38:34 +0200, Bartosz Golaszewski wrote:
> For optional GPIOs we may pass NULL to gpiod_direction_(input|output)().
> With the call to the notifier chain added by commit 07c61d4da43f
> ("gpiolib: notify user-space about in-kernel line state changes") we
> will now dereference a NULL pointer in this case. The reason for that is
> the fact that the expansion of the VALIDATE_DESC() macro (which returns
> 0 for NULL descriptors) was moved into the nonotify variants of the
> direction setters.
> 
> [...]

Applied, thanks!

[1/1] gpiolib: fix a NULL-pointer dereference when setting direction
      commit: 1f4a640e9ac7f450752365541ad9c064b13ef8bf

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

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

end of thread, other threads:[~2024-10-24 19:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-24 13:38 [PATCH] gpiolib: fix a NULL-pointer dereference when setting direction Bartosz Golaszewski
2024-10-24 13:44 ` Klara Modin
2024-10-24 16:25   ` Bartosz Golaszewski
2024-10-24 16:52 ` Mark Brown
2024-10-24 19:04 ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).