aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMasami Hiramatsu <mhiramat@kernel.org>2017-08-02 00:47:42 -0400
committerLinus Walleij <linus.walleij@linaro.org>2017-08-14 09:00:43 -0400
commit90b05b0598c698252d5f7dcdce335c5a546e9047 (patch)
tree1ab8cbb4a65de9ec2ac18cc96e8c2976969cffec
parentef954844c7ace62f773f4f23e28d2d915adc419f (diff)
gpio: reject invalid gpio before getting gpio_desc
Check user-given gpio number and reject it before calling gpio_to_desc() because gpio_to_desc() is for kernel driver and it expects given gpio number is valid (means 0 to 511). If given number is invalid, gpio_to_desc() calls WARN() and dump registers and stack for debug. This means user can easily kick WARN() just by writing invalid gpio number (e.g. 512) to /sys/class/gpio/export. Fixes: 0e9a5edf5d01 ("gpio: fix deferred probe detection for legacy API") Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/gpio/gpiolib-sysfs.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c
index 16fe9742597b..fc80add5fedb 100644
--- a/drivers/gpio/gpiolib-sysfs.c
+++ b/drivers/gpio/gpiolib-sysfs.c
@@ -2,6 +2,7 @@
2#include <linux/mutex.h> 2#include <linux/mutex.h>
3#include <linux/device.h> 3#include <linux/device.h>
4#include <linux/sysfs.h> 4#include <linux/sysfs.h>
5#include <linux/gpio.h>
5#include <linux/gpio/consumer.h> 6#include <linux/gpio/consumer.h>
6#include <linux/gpio/driver.h> 7#include <linux/gpio/driver.h>
7#include <linux/interrupt.h> 8#include <linux/interrupt.h>
@@ -432,6 +433,11 @@ static struct attribute *gpiochip_attrs[] = {
432}; 433};
433ATTRIBUTE_GROUPS(gpiochip); 434ATTRIBUTE_GROUPS(gpiochip);
434 435
436static struct gpio_desc *gpio_to_valid_desc(int gpio)
437{
438 return gpio_is_valid(gpio) ? gpio_to_desc(gpio) : NULL;
439}
440
435/* 441/*
436 * /sys/class/gpio/export ... write-only 442 * /sys/class/gpio/export ... write-only
437 * integer N ... number of GPIO to export (full access) 443 * integer N ... number of GPIO to export (full access)
@@ -450,7 +456,7 @@ static ssize_t export_store(struct class *class,
450 if (status < 0) 456 if (status < 0)
451 goto done; 457 goto done;
452 458
453 desc = gpio_to_desc(gpio); 459 desc = gpio_to_valid_desc(gpio);
454 /* reject invalid GPIOs */ 460 /* reject invalid GPIOs */
455 if (!desc) { 461 if (!desc) {
456 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 462 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
@@ -493,7 +499,7 @@ static ssize_t unexport_store(struct class *class,
493 if (status < 0) 499 if (status < 0)
494 goto done; 500 goto done;
495 501
496 desc = gpio_to_desc(gpio); 502 desc = gpio_to_valid_desc(gpio);
497 /* reject bogus commands (gpio_unexport ignores them) */ 503 /* reject bogus commands (gpio_unexport ignores them) */
498 if (!desc) { 504 if (!desc) {
499 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); 505 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);