Skip to content

Commit 297b8b0

Browse files
groeckgregkh
authored andcommitted
hwmon: (gl520sm) Fix overflows and crash seen when writing into limit attributes
[ Upstream commit 87cdfa9d60f4f40e6d71b04b10b36d9df3c89282 ] Writes into limit attributes can overflow due to multplications and additions with unbound input values. Writing into fan limit attributes can result in a crash with a division by zero if very large values are written and the fan divider is larger than 1. Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Sasha Levin <alexander.levin@verizon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d89f41c commit 297b8b0

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

drivers/hwmon/gl520sm.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
208208
}
209209
static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
210210

211-
#define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
212-
#define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
211+
#define VDD_FROM_REG(val) DIV_ROUND_CLOSEST((val) * 95, 4)
212+
#define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4)
213+
#define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
213214

214-
#define IN_FROM_REG(val) ((val) * 19)
215-
#define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
215+
#define IN_FROM_REG(val) ((val) * 19)
216+
#define IN_CLAMP(val) clamp_val(val, 0, 255 * 19)
217+
#define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
216218

217219
static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
218220
char *buf)
@@ -349,8 +351,13 @@ static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
349351

350352
#define DIV_FROM_REG(val) (1 << (val))
351353
#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div))))
352-
#define FAN_TO_REG(val, div) ((val) <= 0 ? 0 : \
353-
clamp_val((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255))
354+
355+
#define FAN_BASE(div) (480000 >> (div))
356+
#define FAN_CLAMP(val, div) clamp_val(val, FAN_BASE(div) / 255, \
357+
FAN_BASE(div))
358+
#define FAN_TO_REG(val, div) ((val) == 0 ? 0 : \
359+
DIV_ROUND_CLOSEST(480000, \
360+
FAN_CLAMP(val, div) << (div)))
354361

355362
static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr,
356363
char *buf)
@@ -513,9 +520,9 @@ static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
513520
static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
514521
get_fan_off, set_fan_off);
515522

516-
#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
517-
#define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
518-
(val) - 500 : (val) + 500) / 1000) + 130), 0, 255)
523+
#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
524+
#define TEMP_CLAMP(val) clamp_val(val, -130000, 125000)
525+
#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 130)
519526

520527
static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
521528
char *buf)

0 commit comments

Comments
 (0)