Skip to contentSkip to conversation

power: supply: fix work-vs-devres teardown use-after-free

PATCH Applied hyunjungg 2 participants 5 messages Nov 9 – Nov 16

Subsystems (4)
lore
Share & tools
lore

hyunjungg

author
#0 lore raw

v2 cover power: supply: fix work-vs-devres teardown use-after-free

From: Hyunjung Ko <[email protected]>

Three power-supply drivers register a poll/detect work for devm cancellation before devm_power_supply_register(). devres unwinds in reverse, so on unbind the power supply is freed before the work is cancelled, and the work's power_supply_changed() then dereferences the freed supply. Each patch moves the devm work-cancel registration to after devm_power_supply_register() so teardown drains the work first. No functional change.

v2 (thanks Sebastian Reichel):
- trim the commit messages down to the bug and the fix
- drop the inline comments
- no code change vs v1 other than that comment removal

Hyunjung Ko (3):
power: supply: ltc2941: drain poll work before unregistering the supply
power: supply: axp20x_usb_power: drain vbus_detect work before
unregistering the supply
power: supply: mt6360_charger: drain chrdet_work before unregistering
the supply

 drivers/power/supply/axp20x_usb_power.c      | 10 +++++-----
 drivers/power/supply/ltc2941-battery-gauge.c | 10 +++++-----
 drivers/power/supply/mt6360_charger.c        |  7 ++++---
 3 files changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 power: supply: ltc2941: drain poll work before unregistering the supply

From: Hyunjung Ko <[email protected]>

ltc294x_i2c_probe() registers the poll work for devm cancellation before devm_power_supply_register(). devres unwinds in reverse, so on unbind the power supply is freed first and the work cancelled last. ltc294x_work() re-queues itself and calls power_supply_changed(info->supply), so a poll instance running in that window dereferences the freed supply.

Register the work cancellation after the power supply so devres drains it first. Nothing queues the work before the schedule_delayed_work() that follows, so the move is safe.

Fixes 6d0c5de2fd84 ("power: supply: Clean-up few drivers by using managed work init")

---

drivers/power/supply/ltc2941-battery-gauge.c +5 -5
index 6ebb555..8f8a8f6 100644
@@ -531,11 +531,6 @@ static int ltc294x_i2c_probe(struct i2c_client *client)
 
 	psy_cfg.drv_data = info;
 
-	ret = devm_delayed_work_autocancel(&client->dev, &info->work,
-					   ltc294x_work);
-	if (ret)
-		return ret;
-
 	ret = ltc294x_reset(info, prescaler_exp);
 	if (ret < 0)
 		return dev_err_probe(&client->dev, ret,
@@ -547,6 +542,11 @@ static int ltc294x_i2c_probe(struct i2c_client *client)
 		return dev_err_probe(&client->dev, PTR_ERR(info->supply),
 			"failed to register ltc2941\n");
 
+	ret = devm_delayed_work_autocancel(&client->dev, &info->work,
+					   ltc294x_work);
+	if (ret)
+		return ret;
+
 	schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
 
 	return 0;
Signature · 4 lines
2.43.0

-- 
2.43.0
author
#2 lore raw

PATCH v2 2/3 power: supply: axp20x_usb_power: drain vbus_detect work before unregistering the supply

In reply to hyunjungg

From: Hyunjung Ko <[email protected]>

axp20x_usb_power_probe() registers the vbus_detect poll work for devm cancellation before devm_power_supply_register(). devres unwinds in reverse, so on unbind the power supply is freed first and the work cancelled last. The handler calls power_supply_changed(power->supply) and re-queues itself (and is also queued from the USB power IRQ), so an instance running in that window dereferences the freed supply.

Register the work cancellation after the power supply, matching the "request irqs after registering" ordering already used below.

Fixes 6d0c5de2fd84 ("power: supply: Clean-up few drivers by using managed work init")

---

drivers/power/supply/axp20x_usb_power.c +5 -5
index 7625366104bc..f6a7dcbd5a01 100644
@@ -979,11 +979,6 @@ static int axp20x_usb_power_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = devm_delayed_work_autocancel(dev, &power->vbus_detect,
-					   axp_data->axp20x_read_vbus);
-	if (ret)
-		return ret;
-
 	if (power->vbus_mon_bit) {
 		/* Enable vbus valid checking */
 		ret = regmap_field_write(power->vbus_mon_bit, 1);
@@ -1018,6 +1013,11 @@ static int axp20x_usb_power_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(power->supply),
 				     "Failed to register power supply\n");
 
+	ret = devm_delayed_work_autocancel(dev, &power->vbus_detect,
+					   axp_data->axp20x_read_vbus);
+	if (ret)
+		return ret;
+
 	/* Request irqs after registering, as irqs may trigger immediately */
 	for (i = 0; i < axp_data->num_irq_names; i++) {
 		irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/3 power: supply: mt6360_charger: drain chrdet_work before unregistering the supply

In reply to hyunjungg

From: Hyunjung Ko <[email protected]>

mt6360_charger_probe() registers chrdet_work for devm cancellation before devm_power_supply_register(). devres unwinds in reverse, so on unbind the power supply is freed first and the work cancelled last. mt6360_chrdet_work() calls power_supply_changed(mci->psy), so the work running in that window dereferences the freed supply.

Register the work cancellation after the power supply so devres drains it first. chrdet_work is only scheduled later in probe, so the move is safe.

Fixes 0402e8ebb8b8 ("power: supply: mt6360_charger: add MT6360 charger support")

---

drivers/power/supply/mt6360_charger.c +4 -3
index b4d462343151..71c60ba012df 100644
@@ -790,9 +790,6 @@ static int mt6360_charger_probe(struct platform_device *pdev)
 	mci->vinovp = 6500000;
 	mutex_init(&mci->chgdet_lock);
 	platform_set_drvdata(pdev, mci);
-	ret = devm_work_autocancel(&pdev->dev, &mci->chrdet_work, mt6360_chrdet_work);
-	if (ret)
-		return dev_err_probe(&pdev->dev, ret, "Failed to set delayed work\n");
 
 	ret = device_property_read_u32(&pdev->dev, "richtek,vinovp-microvolt", &mci->vinovp);
 	if (ret)
@@ -828,6 +825,10 @@ static int mt6360_charger_probe(struct platform_device *pdev)
 	if (IS_ERR(mci->otg_rdev))
 		return PTR_ERR(mci->otg_rdev);
 
+	ret = devm_work_autocancel(&pdev->dev, &mci->chrdet_work, mt6360_chrdet_work);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "Failed to set delayed work\n");
+
 	schedule_work(&mci->chrdet_work);
 
 	return 0;
Signature · 1 line
2.43.0
#4 lore raw
In reply to hyunjungg
Quoted 11 lines · hyunjungg

From: Hyunjung Ko <[email protected]>

Three power-supply drivers register a poll/detect work for devm cancellation before devm_power_supply_register(). devres unwinds in reverse, so on unbind the power supply is freed before the work is cancelled, and the work's power_supply_changed() then dereferences the freed supply. Each patch moves the devm work-cancel registration to after devm_power_supply_register() so teardown drains the work first. No functional change.

[...]

Applied, thanks!

[1/3] power: supply: ltc2941: drain poll work before unregistering the supply

      commit: 001fc38da1802cfc4b54fd1141594e5c8dc83893
[2/3] power: supply: axp20x_usb_power: drain vbus_detect work before unregistering the supply
      commit: 4dbc2b7a52a6023508cd424e83487588ed5138f3
[3/3] power: supply: mt6360_charger: drain chrdet_work before unregistering the supply
      commit: 6a40d8459e2385b5d0bdb8832c0bce7f87a55823

Best regards,

Signature · 1 line
Sebastian Reichel <[email protected]>