Skip to contentSkip to conversation

gpio: ep93xx: fix incorrect array element size check

PATCH In review Colin King 3 participants 7 messages Sep 6, 2018

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 gpio: ep93xx: fix incorrect array element size check

From: Colin Ian King <[email protected]>

Currently the while loop checks for the end of the array using the size of egp->gc rather that the number of elements in the array, so fix this. Also, perform the array size check first as stylistically it is always good to bounds check on an array first before referencing the array (in this case, we're just computing the address of an element in an array so this is a moot point).

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")
---
 drivers/gpio/gpio-ep93xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-ep93xx.c +1 -1
index 68a416fc3141..dd22ea19c3ed 100644
@@ -76,7 +76,7 @@ static int ep93xx_gpio_port(struct gpio_chip *gc)
 	struct ep93xx_gpio *epg = gpiochip_get_data(gc);
 	int port = 0;
 
-	while (gc != &epg->gc[port] && port < sizeof(epg->gc))
+	while (port < ARRAY_SIZE(epg->gc) && gc != &epg->gc[port])
 		port++;
 
 	/* This should not happen but is there as a last safeguard */
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Colin King

From: Colin Ian King <[email protected]>

Currently the while loop checks for the end of the array using the size of egp->gc rather that the number of elements in the array, so fix this. Also, perform the array size check first as stylistically it is always good to bounds check on an array first before referencing the array (in this case, we're just computing the address of an element in an array so this is a moot point).

Quoted 3 lines
Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")

Oh that was really neat code! Thanks a lot.

Patch applied.

Yours,
Linus Walleij

#2 lore raw

PATCH 1/1 gpio: ep93xx: fix test for end of loop

The problem is that if port = ARRAY_SIZE() and "gc = &epg->gc[port]" then that should be treated as invalid.

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")
drivers/gpio/gpio-ep93xx.c +1 -1
index 68a416fc3141..b0699f57ddf5 100644
@@ -80,7 +80,7 @@ static int ep93xx_gpio_port(struct gpio_chip *gc)
 		port++;
 
 	/* This should not happen but is there as a last safeguard */
-	if (gc != &epg->gc[port]) {
+	if (port = ARRAY_SIZE(epg->gc)) {
 		pr_crit("can't find the GPIO port\n");
 		return 0;
 	}
#3 lore raw

Re: [PATCH] gpio: ep93xx: fix test for end of loop

Quoted 12 lines · Dan Carpenter

The problem is that if port = ARRAY_SIZE() and "gc = &epg->gc[port]" then that should be treated as invalid.

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")
drivers/gpio/gpio-ep93xx.c +0 -0
index 68a416fc3141..b0699f57ddf5 100644

drivers/gpio/gpio-ep93xx.c static int ep93xx_gpio_port(struct gpio_chip *gc) · line 86

Quoted 8 lines
drivers/gpio/gpio-ep93xx.c +1 -1
@@ -80,7 +80,7 @@ static int ep93xx_gpio_port(struct gpio_chip *gc)
 		port++;
 
 	/* This should not happen but is there as a last safeguard */
-	if (gc != &epg->gc[port]) {
+	if (port = ARRAY_SIZE(epg->gc)) {
 		pr_crit("can't find the GPIO port\n");
 		return 0;
 	}

Good catch! I overlooked that one.

#6 lore raw

Re: [PATCH] gpio: ep93xx: fix test for end of loop

In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

The problem is that if port = ARRAY_SIZE() and "gc = &epg->gc[port]" then that should be treated as invalid.

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")

Patch applied!

Yours,
Linus Walleij

#4 lore raw

Re: [PATCH] gpio: ep93xx: fix test for end of loop

In reply to Colin Ian King
Quoted 14 lines · Colin Ian King
Quoted 13 lines · Dan Carpenter

The problem is that if port = ARRAY_SIZE() and "gc = &epg->gc[port]" then that should be treated as invalid.

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")
drivers/gpio/gpio-ep93xx.c +0 -0
index 68a416fc3141..b0699f57ddf5 100644
@@ -80,7 +80,7 @@ static int ep93xx_gpio_port(struct gpio_chip *gc)
 		port++;
 
Quoted 9 lines

drivers/gpio/gpio-ep93xx.c static int ep93xx_gpio_port(struct gpio_chip *gc) · line 86

Quoted 7 lines
drivers/gpio/gpio-ep93xx.c +1 -1
 	/* This should not happen but is there as a last safeguard */
-	if (gc != &epg->gc[port]) {
+	if (port = ARRAY_SIZE(epg->gc)) {
 		pr_crit("can't find the GPIO port\n");
 		return 0;
 	}

Good catch! I overlooked that one.

It's unfortunate but I don't think any of our static checkers would have caught it. You found this bug because of cppcheck, right? I know it warns about the bounds test after use. Does it also warn about the out of bounds?

Smatch doesn't warn about the out of bounds read because Smatch has bad handling of loops. Smatch is supposed to warn about the test after use but that code is buggy. I will investigate what's going on.

I have an unpublished test so Smatch does warn that the port < sizeof() means that port is in terms of bytes but we're using it as an array offset. That's how I noticed this code, but it only warns about the first use, so it warns about the loop but not the post-loop test.

I should fix Smatch's handling of loops so that we know this function originally could return 0-8 and you maybe get a warning in the caller. Unfortunately, I'm not sure I would have paid very much attention to a warning like that because they tend to be prone to false positives. We have a lot of loops that do:

	for (i = 0; i < ARRAY_SIZE(array); i++) {
		if (foo <= array[i])
			break;
	}

And the last element of the array[] is UINT_MAX so we always break. It's a lot of work but not necessarily difficult work.

regards,
dan carpenter

#5 lore raw

Re: [PATCH] gpio: ep93xx: fix test for end of loop

Quoted 25 lines · Dan Carpenter
Quoted 14 lines · Colin Ian King
Quoted 13 lines · Dan Carpenter

The problem is that if port = ARRAY_SIZE() and "gc = &epg->gc[port]" then that should be treated as invalid.

Fixes fd935fc421e7 ("gpio: ep93xx: Do not pingpong irq numbers")
drivers/gpio/gpio-ep93xx.c +0 -0
index 68a416fc3141..b0699f57ddf5 100644
@@ -80,7 +80,7 @@ static int ep93xx_gpio_port(struct gpio_chip *gc)
 		port++;
 
Quoted 9 lines

drivers/gpio/gpio-ep93xx.c static int ep93xx_gpio_port(struct gpio_chip *gc) · line 86

Quoted 7 lines
drivers/gpio/gpio-ep93xx.c +1 -1
 	/* This should not happen but is there as a last safeguard */
-	if (gc != &epg->gc[port]) {
+	if (port = ARRAY_SIZE(epg->gc)) {
 		pr_crit("can't find the GPIO port\n");
 		return 0;
 	}

Good catch! I overlooked that one.

Quoted 4 lines

It's unfortunate but I don't think any of our static checkers would have caught it. You found this bug because of cppcheck, right? I know it warns about the bounds test after use. Does it also warn about the out of bounds?

It can catch these, not sure how well it compares to the other tools I use.

Quoted 16 lines

Smatch doesn't warn about the out of bounds read because Smatch has bad handling of loops. Smatch is supposed to warn about the test after use but that code is buggy. I will investigate what's going on.

I have an unpublished test so Smatch does warn that the port < sizeof() means that port is in terms of bytes but we're using it as an array offset. That's how I noticed this code, but it only warns about the first use, so it warns about the loop but not the post-loop test.

I should fix Smatch's handling of loops so that we know this function originally could return 0-8 and you maybe get a warning in the caller. Unfortunately, I'm not sure I would have paid very much attention to a warning like that because they tend to be prone to false positives. We have a lot of loops that do:

Quoted 4 lines
	for (i = 0; i < ARRAY_SIZE(array); i++) {
		if (foo <= array[i])
			break;
	}

..yep, the are a lot of these to fix up for sure.

Quoted 7 lines

And the last element of the array[] is UINT_MAX so we always break. It's a lot of work but not necessarily difficult work.

regards,
dan carpenter