Skip to contentSkip to conversation

hwmon (occ): Fix division by zero issue

PATCH Applied Lei YU 3 participants 4 messages Jul 8, 2019 – Jul 11, 2019 Hardware monitoring

lore
Share & tools
lore

Lei YU

author
#0 lore raw

PATCH 1/1 hwmon (occ): Fix division by zero issue

The code in occ_get_powr_avg() invokes div64_u64() without checking the divisor. In case the divisor is zero, kernel gets an "Division by zero in kernel" error.

Check the divisor and make it return 0 if the divisor is 0.

---
 drivers/hwmon/occ/common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/occ/common.c +4 -2
index e9d7167..15c5d43 100644
@@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
 
 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
 {
-	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
-			 get_unaligned_be32(samples));
+	u64 divisor = get_unaligned_be32(samples);
+
+	return (divisor == 0) ? 0 :
+		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
 }
 
 static ssize_t occ_show_power_2(struct device *dev,
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Lei YU

The code in occ_get_powr_avg() invokes div64_u64() without checking the divisor. In case the divisor is zero, kernel gets an "Division by zero in kernel" error.

Check the divisor and make it return 0 if the divisor is 0.

Thanks!

Quoted 23 lines
---
  drivers/hwmon/occ/common.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/occ/common.c +4 -2
index e9d7167..15c5d43 100644
@@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
 
  static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
  {
-	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
-			 get_unaligned_be32(samples));
+	u64 divisor = get_unaligned_be32(samples);
+
+	return (divisor == 0) ? 0 :
+		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
  }
 
  static ssize_t occ_show_power_2(struct device *dev,
Applied
#2 lore raw
Quoted 7 lines · Eddie James
Quoted 4 lines · Lei YU

The code in occ_get_powr_avg() invokes div64_u64() without checking the divisor. In case the divisor is zero, kernel gets an "Division by zero in kernel" error.

Quoted 6 lines
Quoted 1 line

Check the divisor and make it return 0 if the divisor is 0.

Thanks!

Merged into dev-5.2.

Lei, can you please submit this to upstream? Please add Eddie's r-b to the commit message.

Cheers,

Joel

Quoted 26 lines
Quoted 25 lines
---
  drivers/hwmon/occ/common.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/occ/common.c +4 -2
index e9d7167..15c5d43 100644
@@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
 
  static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
  {
-     return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
-                      get_unaligned_be32(samples));
+     u64 divisor = get_unaligned_be32(samples);
+
+     return (divisor == 0) ? 0 :
+             div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
  }
 
  static ssize_t occ_show_power_2(struct device *dev,
author
#3 lore raw

Sure, will do.

Quoted 51 lines · Joel Stanley
Quoted 7 lines · Eddie James
Quoted 4 lines · Lei YU

The code in occ_get_powr_avg() invokes div64_u64() without checking the divisor. In case the divisor is zero, kernel gets an "Division by zero in kernel" error.

Quoted 6 lines
Quoted 1 line

Check the divisor and make it return 0 if the divisor is 0.

Thanks!

Merged into dev-5.2.

Lei, can you please submit this to upstream? Please add Eddie's r-b to the commit message.

Cheers,

Joel

Quoted 26 lines
Quoted 25 lines
---
  drivers/hwmon/occ/common.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/occ/common.c +4 -2
index e9d7167..15c5d43 100644
@@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
 
  static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
  {
-     return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
-                      get_unaligned_be32(samples));
+     u64 divisor = get_unaligned_be32(samples);
+
+     return (divisor == 0) ? 0 :
+             div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
  }
 
  static ssize_t occ_show_power_2(struct device *dev,