Skip to contentSkip to conversation

gpiolib: of: fix bounds check for 'gpio-reserved-ranges'

PATCH Applied Andrei Lalaev 4 participants 4 messages Apr 15, 2022 – Apr 25, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpiolib: of: fix bounds check for 'gpio-reserved-ranges'

Gpiolib interprets the elements of "gpio-reserved-ranges" as "start,size" because it clears "size" bits starting from the "start" bit in the according bitmap. So it has to use "greater" instead of "greater or equal" when performs bounds check to make sure that GPIOs are in the available range. Previous implementation skipped ranges that include the last GPIO in the range.

Fixes 726cb3ba4969 ("gpiolib: Support 'gpio-reserved-ranges' property")

---
I wrote the mail to the maintainers
(https://lore.kernel.org/linux-gpio/[email protected]/T/#u)
of the questioned DTSes (because I couldn't understand how the maintainers interpreted this property), but I haven't received a response.
Since the questioned DTSes use "gpio-reserved-ranges = <0 4>"
(i.e., the beginning of the range), this patch doesn't affect these DTSes at all. TBH this patch doesn't break any existing DTSes because none of them reserve gpios at the end of range.

---
 drivers/gpio/gpiolib-of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-of.c +1 -1
index ae1ce319cd78..7e5e51d49d09 100644
@@ -910,7 +910,7 @@ static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
 					   i, &start);
 		of_property_read_u32_index(np, "gpio-reserved-ranges",
 					   i + 1, &count);
-		if (start >= chip->ngpio || start + count >= chip->ngpio)
+		if (start >= chip->ngpio || start + count > chip->ngpio)
 			continue;
 
 		bitmap_clear(chip->valid_mask, start, count);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Andrei Lalaev

Gpiolib interprets the elements of "gpio-reserved-ranges" as "start,size" because it clears "size" bits starting from the "start" bit in the according bitmap. So it has to use "greater" instead of "greater or equal" when performs bounds check to make sure that GPIOs are in the available range. Previous implementation skipped ranges that include the last GPIO in the range.

Quoted 31 lines
Fixes 726cb3ba4969 ("gpiolib: Support 'gpio-reserved-ranges' property")

---
I wrote the mail to the maintainers
(https://lore.kernel.org/linux-gpio/[email protected]/T/#u)
of the questioned DTSes (because I couldn't understand how the maintainers interpreted this property), but I haven't received a response.
Since the questioned DTSes use "gpio-reserved-ranges = <0 4>"
(i.e., the beginning of the range), this patch doesn't affect these DTSes at all. TBH this patch doesn't break any existing DTSes because none of them reserve gpios at the end of range.

---
 drivers/gpio/gpiolib-of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-of.c +1 -2
index ae1ce319cd78..7e5e51d49d09 100644
@@ -910,7 +910,7 @@ static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
                                           i, &start);
                of_property_read_u32_index(np, "gpio-reserved-ranges",
                                           i + 1, &count);
-               if (start >= chip->ngpio || start + count >= chip->ngpio)
+               if (start >= chip->ngpio || start + count > chip->ngpio)
                        continue;
 
                bitmap_clear(chip->valid_mask, start, count);
--

2.25.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 1/1
#2 lore raw
In reply to Andrei Lalaev
Quoted 9 lines · Andrei Lalaev

Gpiolib interprets the elements of "gpio-reserved-ranges" as "start,size" because it clears "size" bits starting from the "start" bit in the according bitmap. So it has to use "greater" instead of "greater or equal" when performs bounds check to make sure that GPIOs are in the available range. Previous implementation skipped ranges that include the last GPIO in the range.

Fixes 726cb3ba4969 ("gpiolib: Support 'gpio-reserved-ranges' property")

Nice patch!

Yours,
Linus Walleij

#3 lore raw
In reply to Andrei Lalaev
Quoted 10 lines · Andrei Lalaev

Gpiolib interprets the elements of "gpio-reserved-ranges" as "start,size" because it clears "size" bits starting from the "start" bit in the according bitmap. So it has to use "greater" instead of "greater or equal" when performs bounds check to make sure that GPIOs are in the available range. Previous implementation skipped ranges that include the last GPIO in the range.

Fixes 726cb3ba4969 ("gpiolib: Support 'gpio-reserved-ranges' property")
Quoted 10 lines

---
I wrote the mail to the maintainers
(https://lore.kernel.org/linux-gpio/[email protected]/T/#u)
of the questioned DTSes (because I couldn't understand how the maintainers interpreted this property), but I haven't received a response.
Since the questioned DTSes use "gpio-reserved-ranges = <0 4>"
(i.e., the beginning of the range), this patch doesn't affect these DTSes at all. TBH this patch doesn't break any existing DTSes because none of them reserve gpios at the end of range.
---

Queued for fixes and Cc'ed stable, thanks! I also added the part below the tags to the commit message as it's important so I don't want to drop it from history.

Bart

Quoted 19 lines
 drivers/gpio/gpiolib-of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-of.c +1 -2
index ae1ce319cd78..7e5e51d49d09 100644
@@ -910,7 +910,7 @@ static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
                                           i, &start);
                of_property_read_u32_index(np, "gpio-reserved-ranges",
                                           i + 1, &count);
-               if (start >= chip->ngpio || start + count >= chip->ngpio)
+               if (start >= chip->ngpio || start + count > chip->ngpio)
                        continue;
 
                bitmap_clear(chip->valid_mask, start, count);
--

2.25.1