Skip to content

Commit 64407f4

Browse files
Dan CarpenterBartosz Golaszewski
authored andcommitted
gpiolib: Fix Oops in gpiod_direction_input_nonotify()
The gpiod_direction_input_nonotify() function is supposed to return zero if the direction for the pin is input. But instead it accidentally returns GPIO_LINE_DIRECTION_IN (1) which will be cast into an ERR_PTR() in gpiochip_request_own_desc(). The callers dereference it and it leads to a crash. I changed gpiod_direction_output_raw_commit() just for consistency but returning GPIO_LINE_DIRECTION_OUT (0) is fine. Cc: [email protected] Fixes: 9d846b1 ("gpiolib: check the return value of gpio_chip::get_direction()") Signed-off-by: Dan Carpenter <[email protected]> Link: https://lore.kernel.org/r/[email protected] [Bartosz: moved the variable declarations to the top of the functions] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent d082ecb commit 64407f4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

drivers/gpio/gpiolib.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ EXPORT_SYMBOL_GPL(gpiod_direction_input);
27122712

27132713
int gpiod_direction_input_nonotify(struct gpio_desc *desc)
27142714
{
2715-
int ret = 0;
2715+
int ret = 0, dir;
27162716

27172717
CLASS(gpio_chip_guard, guard)(desc);
27182718
if (!guard.gc)
@@ -2740,12 +2740,12 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
27402740
ret = guard.gc->direction_input(guard.gc,
27412741
gpio_chip_hwgpio(desc));
27422742
} else if (guard.gc->get_direction) {
2743-
ret = guard.gc->get_direction(guard.gc,
2743+
dir = guard.gc->get_direction(guard.gc,
27442744
gpio_chip_hwgpio(desc));
2745-
if (ret < 0)
2746-
return ret;
2745+
if (dir < 0)
2746+
return dir;
27472747

2748-
if (ret != GPIO_LINE_DIRECTION_IN) {
2748+
if (dir != GPIO_LINE_DIRECTION_IN) {
27492749
gpiod_warn(desc,
27502750
"%s: missing direction_input() operation and line is output\n",
27512751
__func__);
@@ -2764,7 +2764,7 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
27642764

27652765
static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
27662766
{
2767-
int val = !!value, ret = 0;
2767+
int val = !!value, ret = 0, dir;
27682768

27692769
CLASS(gpio_chip_guard, guard)(desc);
27702770
if (!guard.gc)
@@ -2788,12 +2788,12 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
27882788
} else {
27892789
/* Check that we are in output mode if we can */
27902790
if (guard.gc->get_direction) {
2791-
ret = guard.gc->get_direction(guard.gc,
2791+
dir = guard.gc->get_direction(guard.gc,
27922792
gpio_chip_hwgpio(desc));
2793-
if (ret < 0)
2794-
return ret;
2793+
if (dir < 0)
2794+
return dir;
27952795

2796-
if (ret != GPIO_LINE_DIRECTION_OUT) {
2796+
if (dir != GPIO_LINE_DIRECTION_OUT) {
27972797
gpiod_warn(desc,
27982798
"%s: missing direction_output() operation\n",
27992799
__func__);

0 commit comments

Comments
 (0)