diff mbox series

[RFC] hwmon: (nct6775-core) Fix overflows seen when writing limit attributes

Message ID b09c4396-1578-45bb-b1fb-23c1f91cabe7@kylinos.cn (mailing list archive)
State Changes Requested
Headers show
Series [RFC] hwmon: (nct6775-core) Fix overflows seen when writing limit attributes | expand

Commit Message

Pei Xiao Nov. 11, 2024, 8:36 a.m. UTC
hi Guenter Roeck,
   Excuse me, may I ask a question?
   When I see this commit (Fixes: 0403e10bf082 ("hwmon: (nct6775-core) 
Fix underflows seen when writing limit attributes")), I found an issue,
DIV_ROUND_CLOSEST() after kstrtoul() results in an overflow if a large
number such as 18446744073709551615 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.

If the issue described above does indeed exist, I will send an formal patch.


         mutex_lock(&data->update_lock);
         data->target_temp[nr] = val;
@@ -2959,7 +2958,7 @@ store_temp_tolerance(struct device *dev, struct 
device_attribute *attr,
                 return err;

         /* Limit tolerance as needed */
-       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 
data->tolerance_mask);
+       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, data->tolerance_mask * 
1000), 1000);

         mutex_lock(&data->update_lock);
         data->temp_tolerance[index][nr] = val;
@@ -3085,7 +3084,7 @@ store_weight_temp(struct device *dev, struct 
device_attribute *attr,
         if (err < 0)
                 return err;

-       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
+       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 25500), 1000);

         mutex_lock(&data->update_lock);
         data->weight_temp[index][nr] = val;

Thanks!

Comments

Guenter Roeck Nov. 11, 2024, 2:14 p.m. UTC | #1
On 11/11/24 00:36, Pei Xiao wrote:
> hi Guenter Roeck,
>    Excuse me, may I ask a question?
>    When I see this commit (Fixes: 0403e10bf082 ("hwmon: (nct6775-core) Fix underflows seen when writing limit attributes")), I found an issue,
> DIV_ROUND_CLOSEST() after kstrtoul() results in an overflow if a large
> number such as 18446744073709551615 is provided by the user.
> Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
> 
> If the issue described above does indeed exist, I will send an formal patch.
> 

Yes, and good find, but please fix the calculations.

Thanks,
Guenter

> 
> diff --git a/drivers/hwmon/nct6775-core.c b/drivers/hwmon/nct6775-core.c
> index 934fed3dd586..12ee2ca4a6d6 100644
> --- a/drivers/hwmon/nct6775-core.c
> +++ b/drivers/hwmon/nct6775-core.c
> @@ -2878,8 +2878,7 @@ store_target_temp(struct device *dev, struct device_attribute *attr,
>          if (err < 0)
>                  return err;
> 
> -       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
> -                       data->target_temp_mask);
> +       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, data->tolerance_mask * 1000), 1000);
> 
                                                     data->target_temp_mask
>          mutex_lock(&data->update_lock);
>          data->target_temp[nr] = val;
> @@ -2959,7 +2958,7 @@ store_temp_tolerance(struct device *dev, struct device_attribute *attr,
>                  return err;
> 
>          /* Limit tolerance as needed */
> -       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
> +       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, data->tolerance_mask * 1000), 1000);
> 
>          mutex_lock(&data->update_lock);
>          data->temp_tolerance[index][nr] = val;
> @@ -3085,7 +3084,7 @@ store_weight_temp(struct device *dev, struct device_attribute *attr,
>          if (err < 0)
>                  return err;
> 
> -       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
> +       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 25500), 1000);
                                                     255000
> 
>          mutex_lock(&data->update_lock);
>          data->weight_temp[index][nr] = val;
> 
> Thanks!
diff mbox series

Patch

diff --git a/drivers/hwmon/nct6775-core.c b/drivers/hwmon/nct6775-core.c
index 934fed3dd586..12ee2ca4a6d6 100644
--- a/drivers/hwmon/nct6775-core.c
+++ b/drivers/hwmon/nct6775-core.c
@@ -2878,8 +2878,7 @@  store_target_temp(struct device *dev, struct 
device_attribute *attr,
         if (err < 0)
                 return err;

-       val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
-                       data->target_temp_mask);
+       val = DIV_ROUND_CLOSEST(clamp_val(val, 0, data->tolerance_mask * 
1000), 1000);