hwmon: prevent some divide by zeros in FAN_TO_REG()
Subsystems (4)
PATCH 1/1 hwmon: prevent some divide by zeros in FAN_TO_REG()
It's not enough to just test if "rpm" is zero, the "rpm * div" operation could overflow and that could also lead to a divide by zero.
drivers/hwmon/vt8231.c +1 -1
@@ -145,7 +145,7 @@ static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };*/ static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm =0 ) +if (rpm * div =0 )return 0 ;return clamp_val (1310720 / (rpm * div),1 ,255 ); }
drivers/hwmon/lm78.c +1 -1
@@ -92,7 +92,7 @@ static inline u8 IN_TO_REG(unsigned long val)static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm <=0 ) +if (rpm <=0 || rpm * div =0 )return 255 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
drivers/hwmon/sis5595.c +1 -1
@@ -139,7 +139,7 @@ static inline u8 IN_TO_REG(unsigned long val)static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm <=0 ) +if (rpm <=0 || rpm * div =0 )return 255 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
Hi Dan,
Quoted 2 lines · Dan Carpenter
It's not enough to just test if "rpm" is zero, the "rpm * div" operation could overflow and that could also lead to a divide by zero.
If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.
In practice, this means for the vt8231 driver:
if (rpm = 0 || rpm > 1310720) return 0;
and for the lm78 and sis5595 drivers:
if (rpm <= 0) return 255; if (rpm > 1350000) return 0;
That way you're certain to never overflow (the maximum value for div is 8), and insanely large values are handled properly instead of resulting in random register values.
Thanks,
Jean
Quoted 42 lines
Signed-off-by Dan Carpenter <[email protected]>drivers/hwmon/vt8231.c +1
-1@@ -145,7 +145,7 @@ static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };*/ static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm =0 ) +if (rpm * div =0 )return 0 ;return clamp_val (1310720 / (rpm * div),1 ,255 ); }drivers/hwmon/lm78.c +1
-1@@ -92,7 +92,7 @@ static inline u8 IN_TO_REG(unsigned long val)static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm <=0 ) +if (rpm <=0 || rpm * div =0 )return 255 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }drivers/hwmon/sis5595.c +1
-1@@ -139,7 +139,7 @@ static inline u8 IN_TO_REG(unsigned long val)static inline u8 FAN_TO_REG (long rpm,int div) { -if (rpm <=0 ) +if (rpm <=0 || rpm * div =0 )return 255 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
Quoted 17 lines · Jean Delvare
Hi Dan,
Quoted 2 lines · Dan Carpenter
It's not enough to just test if "rpm" is zero, the "rpm * div" operation could overflow and that could also lead to a divide by zero.
If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.
In practice, this means for the vt8231 driver:
if (rpm = 0 || rpm > 1310720) return 0;and for the lm78 and sis5595 drivers:
Quoted 8 lines
if (rpm <= 0) return 255; if (rpm > 1350000) return 0;That way you're certain to never overflow (the maximum value for div is 8), and insanely large values are handled properly instead of resulting in random register values.
Yes. I will resend.
regards,
dan carpenter
Quoted 15 lines · Jean Delvare
Hi Dan,
Quoted 2 lines · Dan Carpenter
It's not enough to just test if "rpm" is zero, the "rpm * div" operation could overflow and that could also lead to a divide by zero.
If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.
In practice, this means for the vt8231 driver:
if (rpm = 0 || rpm > 1310720) return 0;
Quoted 6 lines
and for the lm78 and sis5595 drivers:
if (rpm <= 0) return 255; if (rpm > 1350000) return 0;
For these two are you sure the return value shouldn't be 1?
regards,
dan carpenter
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Jean Delvare
Hi Dan,
Quoted 2 lines · Dan Carpenter
It's not enough to just test if "rpm" is zero, the "rpm * div" operation could overflow and that could also lead to a divide by zero.
If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.
In practice, this means for the vt8231 driver:
if (rpm = 0 || rpm > 1310720) return 0;
Quoted 8 lines
Quoted 6 lines
and for the lm78 and sis5595 drivers:
if (rpm <= 0) return 255; if (rpm > 1350000) return 0;For these two are you sure the return value shouldn't be 1?
Hmm, yes, you're right. That won't make a big difference in practice but returning 1 would be better.
Thanks,
Signature · 1 line
Jean Delvare