[lkml]   [2019]   [Feb]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH v2] 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.

Signed-off-by: Sven Van Asbroeck <>

v2: fix silly use-after-free in remove()

drivers/power/supply/ds2782_battery.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/power/supply/ds2782_battery.c b/drivers/power/supply/ds2782_battery.c
index 019c58493e3d..04b0fe7d7d62 100644
--- a/drivers/power/supply/ds2782_battery.c
+++ b/drivers/power/supply/ds2782_battery.c
@@ -319,17 +319,17 @@ static void ds278x_power_supply_init(struct power_supply_desc *battery)
static int ds278x_battery_remove(struct i2c_client *client)
struct ds278x_info *info = i2c_get_clientdata(client);
+ int id = info->id;

+ cancel_delayed_work_sync(&info->bat_work);
+ kfree(info);

- idr_remove(&battery_id, info->id);
+ idr_remove(&battery_id, id);

- cancel_delayed_work(&info->bat_work);
- kfree(info);
return 0;

 \ /
  Last update: 2019-02-12 17:22    [W:0.024 / U:26.872 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site