Skip to content

Commit ab3dbcf

Browse files
Timur Tabilinusw
authored andcommitted
gpioib: do not free unrequested descriptors
If the main loop in linehandle_create() encounters an error, it unwinds completely by freeing all previously requested GPIO descriptors. However, if the error occurs in the beginning of the loop before that GPIO is requested, then the exit code attempts to free a null descriptor. If extrachecks is enabled, gpiod_free() triggers a WARN_ON. Instead, keep a separate count of legitimate GPIOs so that only those are freed. Cc: stable@vger.kernel.org Fixes: d7c51b4 ("gpio: userspace ABI for reading/writing GPIO lines") Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org> Signed-off-by: Timur Tabi <timur@codeaurora.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent f241632 commit ab3dbcf

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

drivers/gpio/gpiolib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
497497
struct gpiohandle_request handlereq;
498498
struct linehandle_state *lh;
499499
struct file *file;
500-
int fd, i, ret;
500+
int fd, i, count = 0, ret;
501501
u32 lflags;
502502

503503
if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
@@ -558,6 +558,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
558558
if (ret)
559559
goto out_free_descs;
560560
lh->descs[i] = desc;
561+
count = i;
561562

562563
if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
563564
set_bit(FLAG_ACTIVE_LOW, &desc->flags);
@@ -628,7 +629,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
628629
out_put_unused_fd:
629630
put_unused_fd(fd);
630631
out_free_descs:
631-
for (; i >= 0; i--)
632+
for (i = 0; i < count; i++)
632633
gpiod_free(lh->descs[i]);
633634
kfree(lh->label);
634635
out_free_lh:

0 commit comments

Comments
 (0)