Commit 6a5f25e3 authored by Vladimir Zapolskiy's avatar Vladimir Zapolskiy Committed by Greg Kroah-Hartman

gpio: don't free unallocated ida on gpiochip_add_data_with_key() error path

commit a05a1404 upstream.

The change corrects the error path in gpiochip_add_data_with_key()
by avoiding to call ida_simple_remove(), if ida_simple_get() returns
an error.

Note that ida_simple_remove()/ida_free() throws a BUG(), if id argument
is negative, it allows to easily check the correctness of the fix by
fuzzing the return value from ida_simple_get().

Fixes: ff2b1359 ("gpio: make the gpiochip a real device")
Cc: stable@vger.kernel.org # v4.6+
Signed-off-by: default avatarVladimir Zapolskiy <vz@mleia.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 7c9f55c5
...@@ -1166,7 +1166,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data) ...@@ -1166,7 +1166,7 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
if (!gdev->descs) { if (!gdev->descs) {
status = -ENOMEM; status = -ENOMEM;
goto err_free_gdev; goto err_free_ida;
} }
if (chip->ngpio == 0) { if (chip->ngpio == 0) {
...@@ -1298,8 +1298,9 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data) ...@@ -1298,8 +1298,9 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
kfree(gdev->label); kfree(gdev->label);
err_free_descs: err_free_descs:
kfree(gdev->descs); kfree(gdev->descs);
err_free_gdev: err_free_ida:
ida_simple_remove(&gpio_ida, gdev->id); ida_simple_remove(&gpio_ida, gdev->id);
err_free_gdev:
/* failures here can mean systems won't boot... */ /* failures here can mean systems won't boot... */
pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
gdev->base, gdev->base + gdev->ngpio - 1, gdev->base, gdev->base + gdev->ngpio - 1,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment