Skip to contentSkip to conversation

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

PATCH Superseded Dan Carpenter 2 participants 4 messages Jan 21, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

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..cd474584dc0b 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)
+		return -EINVAL;
+
 	val = FAN_RPM_TO_PERIOD(val);
 	val = clamp_val(val, 1, 65534);
 
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 15 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.

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 +0 -0
index d519aca4a9d6..cd474584dc0b 100644

drivers/hwmon/adt7470.c static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val · line 667

Quoted 6 lines
drivers/hwmon/adt7470.c +3 -0
@@ -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)
+		return -EINVAL;
+

Technically that restores old (pre-fc958a61ff6d) behavior, but it is still bad:
Userspace can provide a value of -1 (or any other negative number), and it will translate to 5400000 RPM. So it should either be

	if (val <= 0)
		return -EINVAL;

or

	if (val <= 0)
		val = 1;

Thanks,
Guenter

#2 lore raw
Quoted 26 lines · Guenter Roeck
Quoted 15 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.

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 +0 -0
index d519aca4a9d6..cd474584dc0b 100644

drivers/hwmon/adt7470.c static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val · line 666

Quoted 5 lines
drivers/hwmon/adt7470.c +3 -0
@@ -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)
+		return -EINVAL;
+

Technically that restores old (pre-fc958a61ff6d) behavior, but it is still bad:
Userspace can provide a value of -1 (or any other negative number), and it will translate to 5400000 RPM. So it should either be

Quoted 6 lines
	if (val <= 0)
		return -EINVAL;

or

	if (val <= 0)
		val = 1;

There is a clamp() which does already turn invalid values into something valid.

	val = FAN_RPM_TO_PERIOD(val);
	val = clamp_val(val, 1, 65534);

But I will make the <= 0 return -EINVAL change and resend.

regards,
dan carpenter

maintainer
#3 lore raw
Quoted 30 lines · Dan Carpenter
Quoted 29 lines · Guenter Roeck
Quoted 15 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.

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 +0 -0
index d519aca4a9d6..cd474584dc0b 100644

drivers/hwmon/adt7470.c static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val · line 666

Quoted 5 lines
drivers/hwmon/adt7470.c +3 -0
@@ -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)
+		return -EINVAL;
+

Technically that restores old (pre-fc958a61ff6d) behavior, but it is still bad:
Userspace can provide a value of -1 (or any other negative number), and it will translate to 5400000 RPM. So it should either be

	if (val <= 0)
		return -EINVAL;
Quoted 7 lines
Quoted 3 lines

or

	if (val <= 0)
		val = 1;

There is a clamp() which does already turn invalid values into something valid.

Yes, but

	-1 -> -5400000 -> 1, which translates to 5400000 rpm.
This is in contrast to
	1 -> 5400000 -> 65534

which translates to a more reasonable 82 rpm.

Quoted 5 lines
	val = FAN_RPM_TO_PERIOD(val);
	val = clamp_val(val, 1, 65534);

But I will make the <= 0 return -EINVAL change and resend.

Thanks,
Guenter