Skip to contentSkip to conversation

max17042_battery: fix potential use-after-free on device remove

PATCH In review Sven Van Asbroeck 2 participants 2 messages Feb 15, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 max17042_battery: fix potential use-after-free on device remove

The work which is scheduled on a POR boot is potentially left pending or running until after the device module is removed, which could result in a use-after-free.

Fix by registering a cancel/sync callback, which gets executed as part of standard resource unwinding.

This issue was detected with the help of Coccinelle.

---
 drivers/power/supply/max17042_battery.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/power/supply/max17042_battery.c +10 -0
index 2a8d75e5e930..581c6bd23388 100644
@@ -995,6 +995,13 @@ static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
 	.num_properties	= ARRAY_SIZE(max17042_battery_props) - 2,
 };
 
+static void max17042_stop_work(void *data)
+{
+	struct max17042_chip *chip = data;
+
+	cancel_work_sync(&chip->work);
+}
+
 static int max17042_probe(struct i2c_client *client,
 			const struct i2c_device_id *id)
 {
@@ -1101,6 +1108,9 @@ static int max17042_probe(struct i2c_client *client,
 	regmap_read(chip->regmap, MAX17042_STATUS, &val);
 	if (val & STATUS_POR_BIT) {
 		INIT_WORK(&chip->work, max17042_init_worker);
+		ret = devm_add_action(&client->dev, max17042_stop_work, chip);
+		if (ret)
+			return ret;
 		schedule_work(&chip->work);
 	} else {
 		chip->init_complete = 1;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 11 lines · Sven Van Asbroeck

The work which is scheduled on a POR boot is potentially left pending or running until after the device module is removed, which could result in a use-after-free.

Fix by registering a cancel/sync callback, which gets executed as part of standard resource unwinding.

This issue was detected with the help of Coccinelle.

Looks good to me:

Regards,

Hans

Quoted 33 lines
---
  drivers/power/supply/max17042_battery.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/power/supply/max17042_battery.c +10 -0
index 2a8d75e5e930..581c6bd23388 100644
@@ -995,6 +995,13 @@ static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
  	.num_properties	= ARRAY_SIZE(max17042_battery_props) - 2,
  };
 
+static void max17042_stop_work(void *data)
+{
+	struct max17042_chip *chip = data;
+
+	cancel_work_sync(&chip->work);
+}
+
  static int max17042_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
@@ -1101,6 +1108,9 @@ static int max17042_probe(struct i2c_client *client,
  	regmap_read(chip->regmap, MAX17042_STATUS, &val);
  	if (val & STATUS_POR_BIT) {
  		INIT_WORK(&chip->work, max17042_init_worker);
+		ret = devm_add_action(&client->dev, max17042_stop_work, chip);
+		if (ret)
+			return ret;
  		schedule_work(&chip->work);
  	} else {
  		chip->init_complete = 1;