Skip to contentSkip to conversation

hwmon: (ftsteutates) Fix potential memory access error

PATCH New Guenter Roeck 2 participants 5 messages Jul 26, 2016 – Jul 27, 2016

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (ftsteutates) Fix potential memory access error

Using set_bit() to set a bit in an integer is not a good idea, since the function expects an unsigned long as argument, which can be 64 bit wide. Coverity reports this problem as

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

CID 1364488: Memory - illegal accesses (INCOMPATIBLE_CAST) Pointer "&ret" points to an object whose effective type is "int"
(32 bits, signed) but is dereferenced as a wider "unsigned

+long" (64 bits, unsigned). This may lead to memory corruption.
245 set_bit(1, (unsigned long *)&ret);

Just use BIT instead.

Cc Thilo Cestonaro <[email protected]>
---
 drivers/hwmon/ftsteutates.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/ftsteutates.c +1 -1
index 2b2ff67026be..48633e541dc3 100644
@@ -242,7 +242,7 @@ static int fts_wd_set_resolution(struct fts_data *data,
 	}
 
 	if (resolution == seconds)
-		set_bit(1, (unsigned long *)&ret);
+		ret |= BIT(1);
 	else
 		ret &= ~BIT(1);
 
Signature · 1 line
2.5.0
author maintainer
#1 lore raw

PATCH 1/1 hwmon: (tmp102) Improve error handling

Use devm_add_action_or_reset() instead of devm_add_action(), and check its return code.

---
 drivers/hwmon/tmp102.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwmon/tmp102.c +3 -1
index a942a2574a4d..8479ac5eb853 100644
@@ -227,7 +227,9 @@ static int tmp102_probe(struct i2c_client *client,
 
 	tmp102->config_orig = regval;
 
-	devm_add_action(dev, tmp102_restore_config, tmp102);
+	err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102);
+	if (err)
+		return err;
 
 	regval &= ~TMP102_CONFIG_CLEAR;
 	regval |= TMP102_CONFIG_SET;
Signature · 1 line
2.5.0
author maintainer
#2 lore raw

PATCH 1/1 hwmon: (lm75) Improve error handling

In reply to Guenter Roeck

Use devm_add_action_or_reset() instead of devm_add_action(), and check its return value.

---
 drivers/hwmon/lm75.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/lm75.c +4 -2
index 547a9c87c68c..92f9d4bbf597 100644
@@ -220,7 +220,7 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
 	struct device *dev = &client->dev;
 	struct device *hwmon_dev;
 	struct lm75_data *data;
-	int status;
+	int status, err;
 	u8 set_mask, clr_mask;
 	int new;
 	enum lm75_type kind = id->driver_data;
@@ -331,7 +331,9 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
 	if (status != new)
 		i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
 
-	devm_add_action(dev, lm75_remove, data);
+	err = devm_add_action_or_reset(dev, lm75_remove, data);
+	if (err)
+		return err;
 
 	dev_dbg(dev, "Config %02x\n", new);
 
Signature · 1 line
2.5.0
author maintainer
#3 lore raw

PATCH 1/1 hwmon: (lm90) Improve error handling

In reply to Guenter Roeck

Replace devm_add_action() with devm_add_action_or_reset(), and check its return value.

---
 drivers/hwmon/lm90.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hwmon/lm90.c +7 -5
index 7b3eedf76c62..fa15f7238a28 100644
@@ -1551,9 +1551,7 @@ static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
 	if (config != data->config_orig) /* Only write if changed */
 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
 
-	devm_add_action(&client->dev, lm90_restore_conf, data);
-
-	return 0;
+	return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
 }
 
 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
@@ -1640,7 +1638,9 @@ static int lm90_probe(struct i2c_client *client,
 		return err;
 	}
 
-	devm_add_action(dev, lm90_regulator_disable, regulator);
+	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
+	if (err)
+		return err;
 
 	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
 	if (!data)
@@ -1696,7 +1696,9 @@ static int lm90_probe(struct i2c_client *client,
 		err = device_create_file(dev, &dev_attr_pec);
 		if (err)
 			return err;
-		devm_add_action(dev, lm90_remove_pec, dev);
+		err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
+		if (err)
+			return err;
 	}
 
 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH] hwmon: (lm90) Improve error handling

Hi Guenter,

Quoted 35 lines · Guenter Roeck

Replace devm_add_action() with devm_add_action_or_reset(), and check its return value.

---
 drivers/hwmon/lm90.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hwmon/lm90.c +4 -4
index 7b3eedf76c62..fa15f7238a28 100644
@@ -1551,9 +1551,7 @@ static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
 	if (config != data->config_orig) /* Only write if changed */
 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
 
-	devm_add_action(&client->dev, lm90_restore_conf, data);
-
-	return 0;
+	return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
 }
 
 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
@@ -1640,7 +1638,9 @@ static int lm90_probe(struct i2c_client *client,
 		return err;
 	}
 
-	devm_add_action(dev, lm90_regulator_disable, regulator);
+	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
+	if (err)
+		return err;
 
 	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
 	if (!data)

drivers/hwmon/lm90.c static int lm90_probe(struct i2c_client *client, · line 1704

Quoted 10 lines
drivers/hwmon/lm90.c +3 -1
@@ -1696,7 +1696,9 @@ static int lm90_probe(struct i2c_client *client,
 		err = device_create_file(dev, &dev_attr_pec);
 		if (err)
 			return err;
-		devm_add_action(dev, lm90_remove_pec, dev);
+		err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
+		if (err)
+			return err;
 	}
 
 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,

I'm not yet familiar with that API but it looks like the right thing to do.

Signature · 2 lines
Jean Delvare
SUSE L3 Support