Skip to contentSkip to conversation

power: supply: ds2782: fix possible use-after-free on remove

PATCH Superseded Sven Van Asbroeck 1 participant 2 messages Feb 12, 2019 Power supply

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: ds2782: fix possible use-after-free on remove

In remove(), use cancel_delayed_work_sync() to cancel the delayed work. Otherwise there's a chance that this work will continue to run until after the device has been removed.

While we're here, fix the deallocation order in remove(), to correspond to the inverse of the probe() allocation order. This guarantees that any remaining work can run to completion with all driver structures still intact.

This issue was detected with the help of Coccinelle.

---
 drivers/power/supply/ds2782_battery.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/power/supply/ds2782_battery.c +2 -3
index 019c58493e3d..5853a66ff246 100644
@@ -321,15 +321,14 @@ static int ds278x_battery_remove(struct i2c_client *client)
 	struct ds278x_info *info = i2c_get_clientdata(client);
 
 	power_supply_unregister(info->battery);
+	cancel_delayed_work_sync(&info->bat_work);
 	kfree(info->battery_desc.name);
+	kfree(info);
 
 	mutex_lock(&battery_lock);
 	idr_remove(&battery_id, info->id);
 	mutex_unlock(&battery_lock);
 
-	cancel_delayed_work(&info->bat_work);
-
-	kfree(info);
 	return 0;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 22 lines · Sven Van Asbroeck

In remove(), use cancel_delayed_work_sync() to cancel the delayed work. Otherwise there's a chance that this work will continue to run until after the device has been removed.

While we're here, fix the deallocation order in remove(), to correspond to the inverse of the probe() allocation order. This guarantees that any remaining work can run to completion with all driver structures still intact.

This issue was detected with the help of Coccinelle.

---
 drivers/power/supply/ds2782_battery.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/power/supply/ds2782_battery.c +0 -0
index 019c58493e3d..5853a66ff246 100644

drivers/power/supply/ds2782_battery.c static int ds278x_battery_remove(struct i2c_client *client) · line 329

Quoted 9 lines
drivers/power/supply/ds2782_battery.c +2 -0
@@ -321,15 +321,14 @@ static int ds278x_battery_remove(struct i2c_client *client)
        struct ds278x_info *info = i2c_get_clientdata(client);
 
        power_supply_unregister(info->battery);
+       cancel_delayed_work_sync(&info->bat_work);
        kfree(info->battery_desc.name);
+       kfree(info);
 
        mutex_lock(&battery_lock);
        idr_remove(&battery_id, info->id);

argh, just a minute !

Quoted 11 lines
drivers/power/supply/ds2782_battery.c +0 -4
        mutex_unlock(&battery_lock);
 
-       cancel_delayed_work(&info->bat_work);
-
-       kfree(info);
        return 0;
 }
 
--

2.17.1