Skip to contentSkip to conversation

hwmon: (adt7470) Prevent divide by zero in adt7470_fan_write()

PATCH Applied Dan Carpenter 2 participants 2 messages Jan 21, 2022

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

PATCH v2 1/1 hwmon: (adt7470) Prevent divide by zero in adt7470_fan_write()

The "val" variable is controlled by the user and comes from hwmon_attr_store(). The FAN_RPM_TO_PERIOD() macro divides by "val" so a zero will crash the system. Check for that and return -EINVAL. Negatives are also invalid so return -EINVAL for those too.

Fixes fc958a61ff6d ("hwmon: (adt7470) Convert to devm_hwmon_device_register_with_info API")
---
 drivers/hwmon/adt7470.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/hwmon/adt7470.c +3 -0
index d519aca4a9d6..fb6d14d213a1 100644
@@ -662,6 +662,9 @@ static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val
 	struct adt7470_data *data = dev_get_drvdata(dev);
 	int err;
 
+	if (val <= 0)
+		return -EINVAL;
+
 	val = FAN_RPM_TO_PERIOD(val);
 	val = clamp_val(val, 1, 65534);
 
Signature · 1 line
2.20.1
maintainer Applied
#1 lore raw
Quoted 7 lines · Dan Carpenter

The "val" variable is controlled by the user and comes from hwmon_attr_store(). The FAN_RPM_TO_PERIOD() macro divides by "val" so a zero will crash the system. Check for that and return -EINVAL. Negatives are also invalid so return -EINVAL for those too.

Fixes fc958a61ff6d ("hwmon: (adt7470) Convert to devm_hwmon_device_register_with_info API")

Applied.

Thanks,
Guenter

Quoted 21 lines
---
 drivers/hwmon/adt7470.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/hwmon/adt7470.c +3 -0
index d519aca4a9d6..fb6d14d213a1 100644
@@ -662,6 +662,9 @@ static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val
 	struct adt7470_data *data = dev_get_drvdata(dev);
 	int err;
 
+	if (val <= 0)
+		return -EINVAL;
+
 	val = FAN_RPM_TO_PERIOD(val);
 	val = clamp_val(val, 1, 65534);
 
Signature · 1 line
2.20.1