Skip to contentSkip to conversation

gpiolib: fix a NULL-pointer dereference when setting direction

PATCH Applied Bartosz Golaszewski 3 participants 5 messages Oct 24, 2024 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: fix a NULL-pointer dereference when setting direction

From: Bartosz Golaszewski <[email protected]>

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")
---
 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
index ae758ba6dc3d1..6001ec96693c5 100644
@@ -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))
Signature · 1 line
2.30.2
Tested-by on v1 1/1
#1 lore raw
Quoted 55 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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")
---
  drivers/gpio/gpiolib.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -2
index ae758ba6dc3d1..6001ec96693c5 100644
@@ -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);

drivers/gpio/gpiolib.c int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value) · line 2861

Quoted 8 lines
drivers/gpio/gpiolib.c +0 -2
@@ -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!

#2 lore raw
Quoted 64 lines · Klara Modin
Quoted 62 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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")
---
  drivers/gpio/gpiolib.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
index ae758ba6dc3d1..6001ec96693c5 100644
@@ -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);
 
Quoted 5 lines

drivers/gpio/gpiolib.c int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value) · line 2861

Quoted 1 line
drivers/gpio/gpiolib.c +0 -0
      if (test_bit(FLAG_ACTIVE_LOW, &flags))

This patch fixes the issue for me, thanks!

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

Tested-by on v1 1/1
#3 lore raw
In reply to Bartosz Golaszewski
Quoted 9 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

author Applied
#4 lore raw
In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 9 lines · Bartosz Golaszewski

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,

Signature · 1 line
Bartosz Golaszewski <[email protected]>