Skip to content

Commit 31d04ca

Browse files
Timur Tabigregkh
authored andcommitted
gpioib: do not free unrequested descriptors
commit ab3dbcf upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b8c3208 commit 31d04ca

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

drivers/gpio/gpiolib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
425425
struct gpiohandle_request handlereq;
426426
struct linehandle_state *lh;
427427
struct file *file;
428-
int fd, i, ret;
428+
int fd, i, count = 0, ret;
429429

430430
if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
431431
return -EFAULT;
@@ -471,6 +471,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
471471
if (ret)
472472
goto out_free_descs;
473473
lh->descs[i] = desc;
474+
count = i;
474475

475476
if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
476477
set_bit(FLAG_ACTIVE_LOW, &desc->flags);
@@ -537,7 +538,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
537538
out_put_unused_fd:
538539
put_unused_fd(fd);
539540
out_free_descs:
540-
for (; i >= 0; i--)
541+
for (i = 0; i < count; i++)
541542
gpiod_free(lh->descs[i]);
542543
kfree(lh->label);
543544
out_free_lh:

0 commit comments

Comments
 (0)