gpio: exar: initialize the ID before registering its cleanup
PATCH 1/1 gpio: exar: initialize the ID before registering its cleanup
devm_add_action_or_reset() invokes the cleanup action immediately when registering the action fails. gpio_exar_probe() currently stores the allocated ID in exar_gpio only after registering the action, so that failure makes exar_devm_ida_free() release ID 0. If the allocated ID is nonzero, this leaks that ID and can free ID 0 belonging to another device.
Store the ID before registering the cleanup action so both the immediate failure path and normal device teardown release the correct allocation.
---
Compile-tested with x86_64 allmodconfig and W=1.
I have not tested the devres allocation failure with fault injection.
drivers/gpio/gpio-exar.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-exar.c +1 -1
@@ -199,6 +199,7 @@ static int gpio_exar_probe(struct platform_device *pdev) index =ida_alloc (&ida_index,GFP_KERNEL );if (index <0 )return index; + exar_gpio->index = index; ret =devm_add_action_or_reset (dev, exar_devm_ida_free, exar_gpio);if (ret)
@@ -214,7 +215,6 @@ static int gpio_exar_probe(struct platform_device *pdev) exar_gpio->gpio_chip.set = exar_set_value; exar_gpio->gpio_chip.base =-1 ; exar_gpio->gpio_chip.ngpio = ngpios; - exar_gpio->index = index; exar_gpio->first_pin = first_pin; ret =devm_gpiochip_add_data (dev, &exar_gpio->gpio_chip, exar_gpio);
Signature · 1 line
2.43.0
Quoted 6 lines · Sang-Hoon Choi
devm_add_action_or_reset()invokes the cleanup action immediately when registering the action fails.gpio_exar_probe()currently stores the allocated ID inexar_gpioonly after registering the action, so that failure makesexar_devm_ida_free()release ID 0. If the allocated ID is nonzero, this leaks that ID and can free ID 0 belonging to another device.
Quoted 7 lines
Store the ID before registering the cleanup action so both the immediate failure path and normal device teardown release the correct allocation.
Fixes 5300ebb695fa ("gpio: exar: use devm action for freeing the IDA and drop remove()")Reported-by Changyul Lee <[email protected]>Assisted-by LLMSigned-off-by Sang-Hoon Choi <[email protected]>
This looks right.
Yours,
Linus Walleij
Bartosz Golaszewski
AppliedQuoted 10 lines · Sang-Hoon Choi
devm_add_action_or_reset()invokes the cleanup action immediately when registering the action fails.gpio_exar_probe()currently stores the allocated ID inexar_gpioonly after registering the action, so that failure makesexar_devm_ida_free()release ID 0. If the allocated ID is nonzero, this leaks that ID and can free ID 0 belonging to another device.Store the ID before registering the cleanup action so both the immediate failure path and normal device teardown release the correct allocation.
[...]
Applied, thanks!
[1/1] gpio: exar: initialize the ID before registering its cleanup
https://git.kernel.org/brgl/c/4d69e74034b208588636ecc177e0dd43a7696af3
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>