Skip to contentSkip to conversation

hwmon: (acpi_power_meter) Fix unintentional integer overflow

PATCH Superseded Guenter Roeck 2 participants 7 messages Jun 21, 2012 – Jul 8, 2012 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/3 hwmon: (acpi_power_meter) Fix unintentional integer overflow

Expression with two integer variables is calculated as integer before it is converted to u64. This may result in an integer overflow. Fix by declaring the constant as ULL.

This patch addresses Coverity #200596: Unintentional integer overflow.

---
 drivers/hwmon/acpi_power_meter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/acpi_power_meter.c +1 -1
index 9a0821f1..c8ac1c6 100644
@@ -434,7 +434,7 @@ static ssize_t show_val(struct device *dev,
 		if (resource->trip[attr->index - 7] < 0)
 			return sprintf(buf, "unknown\n");
 
-		val = resource->trip[attr->index - 7] * 1000;
+		val = resource->trip[attr->index - 7] * 1000ULL;
 		break;
 	default:
 		BUG();
Signature · 7 lines
1.7.9.7


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Guenter,

Quoted 16 lines · Guenter Roeck

Expression with two integer variables is calculated as integer before it is converted to u64. This may result in an integer overflow. Fix by declaring the constant as ULL.

This patch addresses Coverity #200596: Unintentional integer overflow.

---
 drivers/hwmon/acpi_power_meter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/acpi_power_meter.c +0 -0
index 9a0821f1..c8ac1c6 100644

drivers/hwmon/acpi_power_meter.c static ssize_t show_val(struct device *dev, · line 440

Quoted 8 lines
drivers/hwmon/acpi_power_meter.c +1 -1
@@ -434,7 +434,7 @@ static ssize_t show_val(struct device *dev,
 		if (resource->trip[attr->index - 7] < 0)
 			return sprintf(buf, "unknown\n");
 
-		val = resource->trip[attr->index - 7] * 1000;
+		val = resource->trip[attr->index - 7] * 1000ULL;
 		break;
 	default:
 		BUG();

That's one way to fix it... Me, I wonder why resource->trip is an arrays of ints, when every other power value handled by the driver is a u64. The trip values end up feeding acpi objects in set_acpi_trip, where 64-bit values would be handled properly. So I don't know much about this driver and I'm not using it, but maybe making trip values 64-bit would be a better fix. Then you still have the problem of signed vs. unsigned, as currently trip = -1 means unset, but this is probably easy to solve.

Also note that the trip values are gathered from sysfs using kstrtoul, so a value between 0x80000000 and 0xffffffff would be silently stored as a negative number. Maybe worth fixing...

And I also see that kstrtoul is used for cap and avg_interval, while they are stored as u64 in struct acpi_power_meter_resource. No overflow here, but an arbitrary limitation on what values the user can set.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#2 lore raw
In reply to Guenter Roeck

On Sat, Jul 07, 2012 at 04:48:34PM +0200, Jean Delvare wrote:

Hi Jean,

Quoted 22 lines

Hi Guenter,

Quoted 19 lines · Guenter Roeck

Expression with two integer variables is calculated as integer before it is converted to u64. This may result in an integer overflow. Fix by declaring the constant as ULL.

This patch addresses Coverity #200596: Unintentional integer overflow.

---
 drivers/hwmon/acpi_power_meter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/acpi_power_meter.c +0 -0
index 9a0821f1..c8ac1c6 100644
@@ -434,7 +434,7 @@ static ssize_t show_val(struct device *dev,
 		if (resource->trip[attr->index - 7] < 0)
 			return sprintf(buf, "unknown\n");
 
Quoted 15 lines

drivers/hwmon/acpi_power_meter.c static ssize_t show_val(struct device *dev, · line 440

Quoted 5 lines
drivers/hwmon/acpi_power_meter.c +1 -1
-		val = resource->trip[attr->index - 7] * 1000;
+		val = resource->trip[attr->index - 7] * 1000ULL;
 		break;
 	default:
 		BUG();

That's one way to fix it... Me, I wonder why resource->trip is an arrays of ints, when every other power value handled by the driver is a u64. The trip values end up feeding acpi objects in set_acpi_trip, where 64-bit values would be handled properly. So I don't know much about this driver and I'm not using it, but maybe making trip values 64-bit would be a better fix. Then you still have the problem of signed vs. unsigned, as currently trip = -1 means unset, but this is probably easy to solve.

It is not needed and would just consume memory, and having to add even more code and maybe variables to handle the not-set case does not sound very compelling to me either.

Quoted 4 lines

Also note that the trip values are gathered from sysfs using kstrtoul, so a value between 0x80000000 and 0xffffffff would be silently stored as a negative number. Maybe worth fixing...

The code in set_trip ensures that stored values are not larger than INT_MAX. Am I missing something ?

Quoted 4 lines

And I also see that kstrtoul is used for cap and avg_interval, while they are stored as u64 in struct acpi_power_meter_resource. No overflow here, but an arbitrary limitation on what values the user can set.

My understanding is that acpi_evaluate_integer() actually enforces "real" limits. Maybe I am wrong, though. Either case, that would be a separate patch.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#3 lore raw
In reply to Guenter Roeck

Hi Guenter,

Quoted 36 lines · Guenter Roeck
Quoted 20 lines · Jean Delvare
Quoted 19 lines · Guenter Roeck

Expression with two integer variables is calculated as integer before it is converted to u64. This may result in an integer overflow. Fix by declaring the constant as ULL.

This patch addresses Coverity #200596: Unintentional integer overflow.

---
 drivers/hwmon/acpi_power_meter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/acpi_power_meter.c +0 -0
index 9a0821f1..c8ac1c6 100644
@@ -434,7 +434,7 @@ static ssize_t show_val(struct device *dev,
 		if (resource->trip[attr->index - 7] < 0)
 			return sprintf(buf, "unknown\n");
 
Quoted 14 lines

drivers/hwmon/acpi_power_meter.c static ssize_t show_val(struct device *dev, · line 440

Quoted 5 lines
drivers/hwmon/acpi_power_meter.c +1 -1
-		val = resource->trip[attr->index - 7] * 1000;
+		val = resource->trip[attr->index - 7] * 1000ULL;
 		break;
 	default:
 		BUG();

That's one way to fix it... Me, I wonder why resource->trip is an arrays of ints, when every other power value handled by the driver is a u64. The trip values end up feeding acpi objects in set_acpi_trip, where 64-bit values would be handled properly. So I don't know much about this driver and I'm not using it, but maybe making trip values 64-bit would be a better fix. Then you still have the problem of signed vs. unsigned, as currently trip = -1 means unset, but this is probably easy to solve.

Quoted 1 line

It is not needed and would just consume memory,

I don't know how you can be sure of that, but if this is the case then I agree your patch is good to go.

Quoted 3 lines

and having to add even more code
and maybe variables to handle the not-set case does not sound very compelling to me either.

You could use s64 and keep -1 for unset, or u64 and 0 or ULLONG_MAX for unset; no need for extra variables. But if you're sure it's not needed, we can keep the code as it is.

Quoted 6 lines
Quoted 3 lines

Also note that the trip values are gathered from sysfs using kstrtoul, so a value between 0x80000000 and 0xffffffff would be silently stored as a negative number. Maybe worth fixing...

The code in set_trip ensures that stored values are not larger than INT_MAX. Am I missing something ?

No, I was. I hadn't seen the patch "hwmon: (acpi_power_meter) Cleanup and optimizations" in your hwmon-staging tree. This indeed solves my above concern.

Quoted 6 lines
Quoted 3 lines

And I also see that kstrtoul is used for cap and avg_interval, while they are stored as u64 in struct acpi_power_meter_resource. No overflow here, but an arbitrary limitation on what values the user can set.

My understanding is that acpi_evaluate_integer() actually enforces "real" limits. Maybe I am wrong, though. Either case, that would be a separate patch.

Not sure what you call "real" limits in this context, but anyway, if 64-bit values are supposed to be possible, then kstrtoull should be used instead of kstrtoul. There's no way acpi_evaluate_integer() can work around this afterward. Still, I agree it would be a separate patch, if anyone cares.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#4 lore raw
In reply to Guenter Roeck
Quoted 39 lines · Jean Delvare

Hi Guenter,

Quoted 36 lines · Guenter Roeck
Quoted 34 lines · Jean Delvare
Quoted 16 lines · Guenter Roeck

Expression with two integer variables is calculated as integer before it is converted to u64. This may result in an integer overflow. Fix by declaring the constant as ULL.

This patch addresses Coverity #200596: Unintentional integer overflow.

---
 drivers/hwmon/acpi_power_meter.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/acpi_power_meter.c +0 -0
index 9a0821f1..c8ac1c6 100644

drivers/hwmon/acpi_power_meter.c static ssize_t show_val(struct device *dev, · line 440

Quoted 8 lines
drivers/hwmon/acpi_power_meter.c +1 -1
@@ -434,7 +434,7 @@ static ssize_t show_val(struct device *dev,
 		if (resource->trip[attr->index - 7] < 0)
 			return sprintf(buf, "unknown\n");
 
-		val = resource->trip[attr->index - 7] * 1000;
+		val = resource->trip[attr->index - 7] * 1000ULL;
 		break;
 	default:
 		BUG();

That's one way to fix it... Me, I wonder why resource->trip is an arrays of ints, when every other power value handled by the driver is a u64. The trip values end up feeding acpi objects in set_acpi_trip, where 64-bit values would be handled properly. So I don't know much about this driver and I'm not using it, but maybe making trip values 64-bit would be a better fix. Then you still have the problem of signed vs. unsigned, as currently trip = -1 means unset, but this is probably easy to solve.

Quoted 5 lines
Quoted 1 line

It is not needed and would just consume memory,

I don't know how you can be sure of that, but if this is the case then I agree your patch is good to go.

Trip point values are stored in mW, and reported/set in uW. I just thought it unlikely that we ever have to deal with trip points above 4 Billion uW or 40 Million Watt.

On the other side, everything else is kept and stored in u64, so from that perspective it makes sense to use the same units for trip points. So I'll change the code to use s64 instead of int for trip[], which means that I can drop the check for INT_MAX in set_trip().

Quoted 12 lines
Quoted 3 lines

and having to add even more code
and maybe variables to handle the not-set case does not sound very compelling to me either.

You could use s64 and keep -1 for unset, or u64 and 0 or ULLONG_MAX for unset; no need for extra variables. But if you're sure it's not needed, we can keep the code as it is.

Quoted 4 lines
Quoted 3 lines

Also note that the trip values are gathered from sysfs using kstrtoul, so a value between 0x80000000 and 0xffffffff would be silently stored as a negative number. Maybe worth fixing...

Quoted 7 lines
Quoted 2 lines

The code in set_trip ensures that stored values are not larger than INT_MAX. Am I missing something ?

No, I was. I hadn't seen the patch "hwmon: (acpi_power_meter) Cleanup and optimizations" in your hwmon-staging tree. This indeed solves my above concern.

Uh, sorry, I forgot about it too. On the other side I missed your Ack for it ...

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

And I also see that kstrtoul is used for cap and avg_interval, while they are stored as u64 in struct acpi_power_meter_resource. No overflow here, but an arbitrary limitation on what values the user can set.

Quoted 9 lines
Quoted 2 lines

My understanding is that acpi_evaluate_integer() actually enforces "real" limits. Maybe I am wrong, though. Either case, that would be a separate patch.

Not sure what you call "real" limits in this context, but anyway, if 64-bit values are supposed to be possible, then kstrtoull should be used instead of kstrtoul. There's no way acpi_evaluate_integer() can work around this afterward. Still, I agree it would be a separate patch, if anyone cares.

"real" as in actual limits as provided and/or determined by ACPI. Anyway, I may have been wrong with that, as apparently acpi_evaluate_integer() is not used to validate a value, but to set it (or both ?).

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#5 lore raw
In reply to Guenter Roeck
Quoted 6 lines · Guenter Roeck
Quoted 3 lines · Jean Delvare

No, I was. I hadn't seen the patch "hwmon: (acpi_power_meter) Cleanup and optimizations" in your hwmon-staging tree. This indeed solves my above concern.

Uh, sorry, I forgot about it too. On the other side I missed your Ack for it ...

Did you?

http://lists.lm-sensors.org/pipermail/lm-sensors/2012-June/036556.html

Quoted 4 lines

(...)
"real" as in actual limits as provided and/or determined by ACPI. Anyway, I may have been wrong with that, as apparently acpi_evaluate_integer() is not used to validate a value, but to set it (or both ?).

I think it can be used to both retrieve or set an integer value using appropriate ACPI methods.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#6 lore raw
In reply to Guenter Roeck
Quoted 6 lines · Jean Delvare
Quoted 5 lines · Guenter Roeck
Quoted 3 lines · Jean Delvare

No, I was. I hadn't seen the patch "hwmon: (acpi_power_meter) Cleanup and optimizations" in your hwmon-staging tree. This indeed solves my above concern.

Quoted 6 lines
Quoted 1 line

Uh, sorry, I forgot about it too. On the other side I missed your Ack for it ...

Did you?

http://lists.lm-sensors.org/pipermail/lm-sensors/2012-June/036556.html

Hi Jean,

when I said "missed", I did not mean "I did not get your Ack", I meant "I overlooked it". Which is why the patch was still in my -staging tree and not in -next.

Thanks,
Guenter

Quoted 5 lines
Quoted 4 lines

(...)
"real" as in actual limits as provided and/or determined by ACPI. Anyway, I may have been wrong with that, as apparently acpi_evaluate_integer() is not used to validate a value, but to set it (or both ?).

Quoted 7 lines

I think it can be used to both retrieve or set an integer value using appropriate ACPI methods.

Signature · 1 line
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors