Skip to contentSkip to conversation

gpiolib: potential oops on failure path

PATCH In review Dan Carpenter 3 participants 6 messages Jun 17, 2016 – Jun 18, 2016

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

PATCH 1/1 gpiolib: potential oops on failure path

If anon_inode_getfd() fails then "i" is set to GPIOHANDLES_MAX. It means that we will read beyond the end of the array and dereference an invalid pointer.

Fixes d7c51b47ac11 ('gpio: userspace ABI for reading/writing GPIO lines')
drivers/gpio/gpiolib.c +2 -0
index 8b3db59..8578b7f 100644
@@ -495,6 +495,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	return 0;
 
 out_free_descs:
+	if (i = GPIOHANDLES_MAX)
+		i--;
 	for (; i >= 0; i--)
 		gpiod_free(lh->descs[i]);
 	kfree(lh->label);
#1 lore raw

Am 17.06.2016 11:15, schrieb Dan Carpenter:

Quoted 12 lines

If anon_inode_getfd() fails then "i" is set to GPIOHANDLES_MAX. It means that we will read beyond the end of the array and dereference an invalid pointer.

Fixes d7c51b47ac11 ('gpio: userspace ABI for reading/writing GPIO lines')
drivers/gpio/gpiolib.c +0 -0
index 8b3db59..8578b7f 100644

drivers/gpio/gpiolib.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 502

Quoted 8 lines
drivers/gpio/gpiolib.c +2 -0
@@ -495,6 +495,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	return 0;
 
 out_free_descs:
+	if (i = GPIOHANDLES_MAX)
+		i--;
 	for (; i >= 0; i--)
 		gpiod_free(lh->descs[i]);
 	kfree(lh->label);

Since we have already noticed that programmes are bad at counting backwards is it possible to change the loop into counting up ?

btw: if lh->descs[i] is initialized to NULL it would be more robust just to free everything like:

for(i=0;i< GPIOHANDLES_MAX; i++)
gpiod_free(lh->descs[i]);

just my two cents,

re,
wh

#2 lore raw
Quoted 11 lines · walter harms

drivers/gpio/gpiolib.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 502

Quoted 6 lines

out_free_descs:

drivers/gpio/gpiolib.c +2 -0
+	if (i = GPIOHANDLES_MAX)
+		i--;
 	for (; i >= 0; i--)
 		gpiod_free(lh->descs[i]);
 	kfree(lh->label);

Since we have already noticed that programmes are bad at counting backwards is it possible to change the loop into counting up ?

Quoted 5 lines

btw: if lh->descs[i] is initialized to NULL it would be more robust just to free everything like:

for(i=0;i< GPIOHANDLES_MAX; i++)
gpiod_free(lh->descs[i]);

Depending on the config gpiod_free(NULL) generates a warning. It's possible that it will be generated once with the current code actually but I didn't feel it was worth worrying about that.

Still I wouldn't want to generate 64 warnings.

regards,
dan carpenter

#3 lore raw
In reply to walter harms
Quoted 13 lines · walter harms

Am 17.06.2016 11:15, schrieb Dan Carpenter:

drivers/gpio/gpiolib.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 502

Quoted 6 lines

out_free_descs:

drivers/gpio/gpiolib.c +2 -0
+     if (i = GPIOHANDLES_MAX)
+             i--;
      for (; i >= 0; i--)
              gpiod_free(lh->descs[i]);
      kfree(lh->label);

Since we have already noticed that programmes are bad at counting backwards is it possible to change the loop into counting up ?

Quoted 6 lines

btw: if lh->descs[i] is initialized to NULL it would be more robust just to free everything like:

for(i=0;i< GPIOHANDLES_MAX; i++)
gpiod_free(lh->descs[i]);

just my two cents,

The loop looks like that for a reason: i is indexing upwards while grabbing the descriptors, then it rewinds that way to free them if something went wrong during init.

Yours,
Linus Walleij

#4 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

If anon_inode_getfd() fails then "i" is set to GPIOHANDLES_MAX. It means that we will read beyond the end of the array and dereference an invalid pointer.

Fixes d7c51b47ac11 ('gpio: userspace ABI for reading/writing GPIO lines')

Ouch, yeah that happens when you request max number of handles, thanks.

Patch applied.

Yours,
Linus Walleij

#5 lore raw
Quoted 7 lines · Linus Walleij
Quoted 4 lines · Dan Carpenter

If anon_inode_getfd() fails then "i" is set to GPIOHANDLES_MAX. It means that we will read beyond the end of the array and dereference an invalid pointer.

Quoted 6 lines
Quoted 2 lines
Fixes d7c51b47ac11 ('gpio: userspace ABI for reading/writing GPIO lines')

Ouch, yeah that happens when you request max number of handles, thanks.

Patch applied.

Ah no, I saw the real problem now. Sending another patch.

Yours,
Linus Walleij