Skip to content

Commit

Permalink
gpio: fix gpio-chip list corruption
Browse files Browse the repository at this point in the history
Fix potential corruption of gpio-chip list due to failure to remove the
chip from the list before returning in gpiochip_add error path.

The chip could be long gone when the global list is next traversed,
something which could lead to a null-pointer dereference. In the best
case (chip not deallocated) we are just leaking the gpio range.

Fixes: 14e85c0 ("gpio: remove gpio_descs global array")
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
  • Loading branch information
Johan Hovold authored and Linus Walleij committed Jan 14, 2015
1 parent 5539b3c commit 225fce8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ int gpiochip_add(struct gpio_chip *chip)
base = gpiochip_find_base(chip->ngpio);
if (base < 0) {
status = base;
goto unlock;
spin_unlock_irqrestore(&gpio_lock, flags);
goto err_free_descs;
}
chip->base = base;
}
Expand Down Expand Up @@ -288,21 +289,23 @@ int gpiochip_add(struct gpio_chip *chip)
acpi_gpiochip_add(chip);

status = gpiochip_export(chip);
if (status) {
acpi_gpiochip_remove(chip);
of_gpiochip_remove(chip);
goto fail;
}
if (status)
goto err_remove_chip;

pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
chip->base, chip->base + chip->ngpio - 1,
chip->label ? : "generic");

return 0;

unlock:
err_remove_chip:
acpi_gpiochip_remove(chip);
of_gpiochip_remove(chip);
spin_lock_irqsave(&gpio_lock, flags);
list_del(&chip->list);
spin_unlock_irqrestore(&gpio_lock, flags);
fail:
err_free_descs:
kfree(descs);
chip->desc = NULL;

Expand Down

0 comments on commit 225fce8

Please sign in to comment.