diff options
| author | Bartosz Golaszewski <[email protected]> | 2025-02-10 10:52:00 +0000 |
|---|---|---|
| committer | Bartosz Golaszewski <[email protected]> | 2025-02-24 09:02:36 +0000 |
| commit | dfeb70c86d637d49af9313245e6b1f2ead08ce9b (patch) | |
| tree | be849d28df202103923529c763bf779248f1ab2a /drivers/gpio/gpiolib.c | |
| parent | gpiolib: sanitize the return value of gpio_chip::get_multiple() (diff) | |
| download | kernel-dfeb70c86d637d49af9313245e6b1f2ead08ce9b.tar.gz kernel-dfeb70c86d637d49af9313245e6b1f2ead08ce9b.zip | |
gpiolib: sanitize the return value of gpio_chip::direction_output()
The return value of the direction_output() callback may be propagated to
user-space. As per the API contract it can only return 0 or a negative
error number. Add a wrapper around the callback calls that filters out
anything else.
Reviewed-by: Linus Walleij <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Bartosz Golaszewski <[email protected]>
Diffstat (limited to 'drivers/gpio/gpiolib.c')
| -rw-r--r-- | drivers/gpio/gpiolib.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 9ac2dad0ad34..2c8c4183f6f0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2701,6 +2701,23 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) return ret; } +static int gpiochip_direction_output(struct gpio_chip *gc, unsigned int offset, + int value) +{ + int ret; + + lockdep_assert_held(&gc->gpiodev->srcu); + + if (WARN_ON(!gc->direction_output)) + return -EOPNOTSUPP; + + ret = gc->direction_output(gc, offset, value); + if (ret > 0) + ret = -EBADE; + + return ret; +} + /** * gpiod_direction_input - set the GPIO direction to input * @desc: GPIO to set to input @@ -2798,8 +2815,8 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) } if (guard.gc->direction_output) { - ret = guard.gc->direction_output(guard.gc, - gpio_chip_hwgpio(desc), val); + ret = gpiochip_direction_output(guard.gc, + gpio_chip_hwgpio(desc), val); } else { /* Check that we are in output mode if we can */ if (guard.gc->get_direction) { @@ -3460,7 +3477,7 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) if (value) { ret = guard.gc->direction_input(guard.gc, offset); } else { - ret = guard.gc->direction_output(guard.gc, offset, 0); + ret = gpiochip_direction_output(guard.gc, offset, 0); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } @@ -3485,7 +3502,7 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value return; if (value) { - ret = guard.gc->direction_output(guard.gc, offset, 1); + ret = gpiochip_direction_output(guard.gc, offset, 1); if (!ret) set_bit(FLAG_IS_OUT, &desc->flags); } else { |
