From: Alexandre Courbot <gnurou@gmail.com>
To: Grant Likely <grant.likely@secretlab.ca>,
Linus Walleij <linus.walleij@linaro.org>
Cc: linux-kernel@vger.kernel.org, gnurou@gmail.com,
Alexandre Courbot <acourbot@nvidia.com>
Subject: [PATCH 1/4] gpiolib: check descriptors validity before use
Date: Wed, 13 Feb 2013 16:02:59 +0900 [thread overview]
Message-ID: <1360738983-22436-2-git-send-email-gnurou@gmail.com> (raw)
In-Reply-To: <1360738983-22436-1-git-send-email-gnurou@gmail.com>
From: Alexandre Courbot <acourbot@nvidia.com>
Some functions dereferenced their GPIO descriptor argument without
checking its validity first, potentially leading to an oops when given
an invalid argument.
This patch also makes gpio_get_value() more resilient when given an
invalid GPIO, returning 0 instead of oopsing.
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
drivers/gpio/gpiolib.c | 64 +++++++++++++++++++++++++++-----------------------
1 file changed, 35 insertions(+), 29 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index fff9786..8a2cf9c 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -174,7 +174,7 @@ static int gpio_ensure_requested(struct gpio_desc *desc)
/* caller holds gpio_lock *OR* gpio is marked as requested */
static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
{
- return desc->chip;
+ return desc ? desc->chip : NULL;
}
struct gpio_chip *gpio_to_chip(unsigned gpio)
@@ -653,7 +653,12 @@ static ssize_t export_store(struct class *class,
if (status < 0)
goto done;
+ status = -EINVAL;
+
desc = gpio_to_desc(gpio);
+ /* reject invalid GPIOs */
+ if (!desc)
+ goto done;
/* No extra locking here; FLAG_SYSFS just signifies that the
* request and export were done by on behalf of userspace, so
@@ -867,8 +872,8 @@ static int gpiod_export_link(struct device *dev, const char *name,
done:
if (status)
- pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
- status);
+ pr_debug("%s: gpio%d status %d\n", __func__,
+ desc ? desc_to_gpio(desc) : -1, status);
return status;
}
@@ -916,8 +921,8 @@ unlock:
done:
if (status)
- pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
- status);
+ pr_debug("%s: gpio%d status %d\n", __func__,
+ desc ? desc_to_gpio(desc) : -1, status);
return status;
}
@@ -964,8 +969,8 @@ static void gpiod_unexport(struct gpio_desc *desc)
}
done:
if (status)
- pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
- status);
+ pr_debug("%s: gpio%d status %d\n", __func__,
+ desc ? desc_to_gpio(desc) : -1, status);
}
void gpio_unexport(unsigned gpio)
@@ -1432,8 +1437,7 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)
done:
if (status)
pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
- desc ? desc_to_gpio(desc) : -1,
- label ? : "?", status);
+ desc ? desc_to_gpio(desc) : -1, label ? : "?", status);
spin_unlock_irqrestore(&gpio_lock, flags);
return status;
}
@@ -1655,13 +1659,9 @@ lose:
return status;
fail:
spin_unlock_irqrestore(&gpio_lock, flags);
- if (status) {
- int gpio = -1;
- if (desc)
- gpio = desc_to_gpio(desc);
+ if (status)
pr_debug("%s: gpio-%d status %d\n",
- __func__, gpio, status);
- }
+ __func__, desc ? desc_to_gpio(desc) : -1, status);
return status;
}
@@ -1678,6 +1678,9 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
int status = -EINVAL;
int offset;
+ if (!desc)
+ goto fail_unlocked;
+
/* Open drain pin should not be driven to 1 */
if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
return gpiod_direction_input(desc);
@@ -1688,8 +1691,6 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
spin_lock_irqsave(&gpio_lock, flags);
- if (!desc)
- goto fail;
chip = desc->chip;
if (!chip || !chip->set || !chip->direction_output)
goto fail;
@@ -1725,13 +1726,10 @@ lose:
return status;
fail:
spin_unlock_irqrestore(&gpio_lock, flags);
- if (status) {
- int gpio = -1;
- if (desc)
- gpio = desc_to_gpio(desc);
+fail_unlocked:
+ if (status)
pr_debug("%s: gpio-%d status %d\n",
- __func__, gpio, status);
- }
+ __func__, desc ? desc_to_gpio(desc) : -1, status);
return status;
}
@@ -1776,13 +1774,9 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
fail:
spin_unlock_irqrestore(&gpio_lock, flags);
- if (status) {
- int gpio = -1;
- if (desc)
- gpio = desc_to_gpio(desc);
+ if (status)
pr_debug("%s: gpio-%d status %d\n",
- __func__, gpio, status);
- }
+ __func__, desc ? desc_to_gpio(desc) : -1, status);
return status;
}
@@ -1830,6 +1824,8 @@ static int gpiod_get_value(struct gpio_desc *desc)
int value;
int offset;
+ if (!desc)
+ return 0;
chip = desc->chip;
offset = gpio_chip_hwgpio(desc);
/* Should be using gpio_get_value_cansleep() */
@@ -1912,6 +1908,8 @@ static void gpiod_set_value(struct gpio_desc *desc, int value)
{
struct gpio_chip *chip;
+ if (!desc)
+ return;
chip = desc->chip;
/* Should be using gpio_set_value_cansleep() */
WARN_ON(chip->can_sleep);
@@ -1940,6 +1938,8 @@ EXPORT_SYMBOL_GPL(__gpio_set_value);
*/
static int gpiod_cansleep(struct gpio_desc *desc)
{
+ if (!desc)
+ return 0;
/* only call this on GPIOs that are valid! */
return desc->chip->can_sleep;
}
@@ -1964,6 +1964,8 @@ static int gpiod_to_irq(struct gpio_desc *desc)
struct gpio_chip *chip;
int offset;
+ if (!desc)
+ return -EINVAL;
chip = desc->chip;
offset = gpio_chip_hwgpio(desc);
return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
@@ -1987,6 +1989,8 @@ static int gpiod_get_value_cansleep(struct gpio_desc *desc)
int offset;
might_sleep_if(extra_checks);
+ if (!desc)
+ return 0;
chip = desc->chip;
offset = gpio_chip_hwgpio(desc);
value = chip->get ? chip->get(chip, offset) : 0;
@@ -2005,6 +2009,8 @@ static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
struct gpio_chip *chip;
might_sleep_if(extra_checks);
+ if (!desc)
+ return;
chip = desc->chip;
trace_gpio_value(desc_to_gpio(desc), 0, value);
if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
--
1.8.1.3
next prev parent reply other threads:[~2013-02-13 7:02 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-13 7:02 [PATCH 0/4] gpiolib: some fixup patches Alexandre Courbot
2013-02-13 7:02 ` Alexandre Courbot [this message]
2013-02-13 22:49 ` [PATCH 1/4] gpiolib: check descriptors validity before use Ryan Mallon
2013-02-14 3:00 ` Alexandre Courbot
2013-02-13 7:03 ` [PATCH 2/4] gpiolib: use const parameters when possible Alexandre Courbot
2014-11-17 9:09 ` Uwe Kleine-König
2014-11-19 8:33 ` Alexandre Courbot
2014-11-19 8:44 ` Uwe Kleine-König
2014-11-19 8:57 ` Alexandre Courbot
2014-11-19 9:02 ` Uwe Kleine-König
2014-11-19 9:07 ` Alexandre Courbot
2014-11-19 10:09 ` Uwe Kleine-König
2014-11-25 7:37 ` Alexandre Courbot
2014-11-25 9:36 ` Janusz Użycki
2013-02-13 7:03 ` [PATCH 3/4] gpiolib: move comment to right function Alexandre Courbot
2013-02-13 7:03 ` [PATCH 4/4] gpiolib: rename local offset variables to "hwgpio" Alexandre Courbot
2013-02-13 22:54 ` Ryan Mallon
2013-02-14 3:11 ` Alexandre Courbot
2013-02-13 7:03 ` Alexandre Courbot
-- strict thread matches above, loose matches on Subject: below --
2013-02-15 5:46 [PATCH v2 0/4] gpiolib: some fixup patches Alexandre Courbot
2013-02-15 5:46 ` [PATCH 1/4] gpiolib: check descriptors validity before use Alexandre Courbot
2013-02-26 17:48 ` Grant Likely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1360738983-22436-2-git-send-email-gnurou@gmail.com \
--to=gnurou@gmail.com \
--cc=acourbot@nvidia.com \
--cc=grant.likely@secretlab.ca \
--cc=linus.walleij@linaro.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).