Skip to contentSkip to conversation

gpiolib: fix stack out-of-bounds access in gpiochip_add_hog()

PATCH Applied Bartosz Golaszewski 3 participants 4 messages Nov 10 – Nov 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpiolib: fix stack out-of-bounds access in gpiochip_add_hog()

In gpiochip_add_hog(), parsing a device tree with an excessively large "#gpio-cells" property causes a stack out-of-bounds write when populating the GPIO specification.

The value read from #gpio-cells is used to copy the values into right places in struct fwnode_reference_args but that struct's args field is an array of size NR_FWNODE_REFERENCE_ARGS so a value larger than it will make the loop write past the end of that array.

If #gpio-cells is 0, we can trigger a divide-by-zero later when calculating the actual number of hogs.

If #gpio-cells is 1, we can also perform an out-of-bounds read of the "gpios" array.

Reject #gpio-cells larger than NR_FWNODE_REFERENCE_ARGS, lower than 1 and make sure #gpio-cells is larger than 1 before checking the active-low flag.

Fixes 802c51a83e9a ("gpiolib: fix hogs with multiple lines")

---
Changes in v2:
- Fix a potential divide-by-zero bug too (sashiko)
- Fix an off-by-one bug for #gpio-cells == 1
- Link to v1: https://patch.msgid.link/20261105-gpiolib-hog-stack-corruption-v1-1-a47af9c51965@oss.qualcomm.com

---
 drivers/gpio/gpiolib.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +5 -2
index 9282d9874e1109b7f030a3e83598ad016c2bf629..a11e65be476142e082ce76ffd2788c3cbe82d08f 100644
@@ -986,6 +986,8 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 	ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells);
 	if (ret && is_of_node(fwnode))
 		return ret;
+	if (cells < 1 || cells > NR_FWNODE_REFERENCE_ARGS)
+		return -EINVAL;
 	if (argc % cells)
 		return -EINVAL;
 
@@ -1041,9 +1043,10 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 		} else {
 			/*
 			 * GPIO_ACTIVE_LOW is currently the only lookup flag
-			 * supported for non-OF firmware nodes.
+			 * supported for non-OF firmware nodes and it must live
+			 * in the second cell.
 			 */
-			if (gpios[i * cells + 1])
+			if (cells > 1 && gpios[i * cells + 1])
 				lflags |= GPIO_ACTIVE_LOW;
 		}
 

---
base-commit: 630059a5a632377b9b7b487baccde92b7ae1dd47
change-id: 20261105-gpiolib-hog-stack-corruption-431b647ea556

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 15 lines · Bartosz Golaszewski

In gpiochip_add_hog(), parsing a device tree with an excessively large "#gpio-cells" property causes a stack out-of-bounds write when populating the GPIO specification.

The value read from #gpio-cells is used to copy the values into right places in struct fwnode_reference_args but that struct's args field is an array of size NR_FWNODE_REFERENCE_ARGS so a value larger than it will make the loop write past the end of that array.

If #gpio-cells is 0, we can trigger a divide-by-zero later when calculating the actual number of hogs.

If #gpio-cells is 1, we can also perform an out-of-bounds read of the "gpios" array.

Quoted 8 lines

Reject #gpio-cells larger than NR_FWNODE_REFERENCE_ARGS, lower than 1 and make sure #gpio-cells is larger than 1 before checking the active-low flag.

Fixes 802c51a83e9a ("gpiolib: fix hogs with multiple lines")

Yours,
Linus Walleij

Reviewed-by on v2 1/1
#2 lore raw
In reply to Bartosz Golaszewski
Quoted 23 lines · Bartosz Golaszewski

In gpiochip_add_hog(), parsing a device tree with an excessively large "#gpio-cells" property causes a stack out-of-bounds write when populating the GPIO specification.

The value read from #gpio-cells is used to copy the values into right places in struct fwnode_reference_args but that struct's args field is an array of size NR_FWNODE_REFERENCE_ARGS so a value larger than it will make the loop write past the end of that array.

If #gpio-cells is 0, we can trigger a divide-by-zero later when calculating the actual number of hogs.

If #gpio-cells is 1, we can also perform an out-of-bounds read of the "gpios" array.

Reject #gpio-cells larger than NR_FWNODE_REFERENCE_ARGS, lower than 1 and make sure #gpio-cells is larger than 1 before checking the active-low flag.

Fixes 802c51a83e9a ("gpiolib: fix hogs with multiple lines")
Quoted 5 lines

---
Changes in v2:
- Fix a potential divide-by-zero bug too (sashiko)
- Fix an off-by-one bug for #gpio-cells == 1
- Link to v1: https://patch.msgid.link/20261105-gpiolib-hog-stack-corruption-v1-1-a47af9c51965@oss.qualcomm.com

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author Applied
#3 lore raw
In reply to Bartosz Golaszewski
Quoted 10 lines · Bartosz Golaszewski

In gpiochip_add_hog(), parsing a device tree with an excessively large "#gpio-cells" property causes a stack out-of-bounds write when populating the GPIO specification.

The value read from #gpio-cells is used to copy the values into right places in struct fwnode_reference_args but that struct's args field is an array of size NR_FWNODE_REFERENCE_ARGS so a value larger than it will make the loop write past the end of that array.

[...]

Applied, thanks!

[1/1] gpiolib: fix stack out-of-bounds access in gpiochip_add_hog()

      https://git.kernel.org/brgl/c/f8a4fdf57cedb9cb3f47d94269ff27fea8e819e8

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>