Skip to contentSkip to conversation

gpio: exar: initialize the ID before registering its cleanup

PATCH Applied Sang-Hoon Choi 3 participants 3 messages Sep 29 – Oct 1 GPIO

lore
Share & tools
lore
#0 lore raw

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.

Fixes 5300ebb695fa ("gpio: exar: use devm action for freeing the IDA and drop remove()")
Assisted-by LLM

---
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
index 9053662f18177f735db2cb33fd690b39dcf3fb0b..f629cc526f3ee71ae20e88ec1979f93bc04841e5 100644
@@ -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
maintainer Reviewed-by on v1 1/1
#1 lore raw
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 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.

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()")
Assisted-by LLM

This looks right.

Yours,
Linus Walleij

#2 lore raw
In reply to Sang-Hoon Choi
Quoted 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 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.

[...]

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]>