Skip to contentSkip to conversation

hwmon: (dell-smm) Validate pwm and fan_max values

PATCH Applied Rosen Penev 4 participants 12 messages Nov 14 – Nov 20

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover hwmon: (dell-smm) Validate pwm and fan_max values

Two input validation fixes for dell-smm-hwmon: a huge value written to pwmN overflowed the scaling and turned the fan off, and an out-of-range fan_max module parameter could wrap an allocation size to zero.

These patches were developed with the help of an AI coding assistant (Claude Opus 5.5): it reviewed the drivers, found the issues and drafted the fixes and changelogs. I reviewed every change and I am responsible for them.

All patches build cleanly with W=1.

Tested on a Dell Precision M4800 running v7.2.15, using the equivalent backports built as out-of-tree modules:
fan_max=256 and fan_max=4294967295 are rejected with the new message, fan_max=3 and the default still load. The pwm clamp could not be
observed: this machine's BIOS ignores manual pwm writes.

Rosen Penev (2):
hwmon: (dell-smm) Clamp the pwm value before scaling it
hwmon: (dell-smm) Reject a fan_max that the BIOS cannot take

 drivers/hwmon/dell-smm-hwmon.c | 7 +++++++
 1 file changed, 7 insertions(+)

base-commit: 2800868f63227c8237d70c41e70d441c919620d6

Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/2 hwmon: (dell-smm) Clamp the pwm value before scaling it

dell_smm_write() scales the value written to pwmN with

	DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult)

before clamping the result to the valid fan states. The rounding adds i8k_pwm_mult / 2 to val first, which overflows for values near LONG_MAX and wraps to a large negative number. That is then clamped to 0, so writing a huge value turns the fan off instead of running it at the highest speed. With the default fan_max of 2, writing any value from LONG_MAX - 63 up to LONG_MAX selects fan state 0.

Clamp val to the pwm range of 0 to 255 first.

Fixes e7f5f275c62a ("i8k: Implement hwmon based fan speed control")
---
 drivers/hwmon/dell-smm-hwmon.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hwmon/dell-smm-hwmon.c +1 -0
index 5ec487843faa..e8b4e8a29e8b 100644
@@ -1030,6 +1030,7 @@ static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32
 	case hwmon_pwm:
 		switch (attr) {
 		case hwmon_pwm_input:
+			val = clamp_val(val, 0, 255);
 			pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0,
 					data->i8k_fan_max);
 
Signature · 1 line
2.56.0
#4 lore raw

PATCH 2/2 hwmon: (dell-smm) Reject a fan_max that the BIOS cannot take

In reply to Rosen Penev

The fan_max module parameter is used without any range check, unlike fan_mult. dell_smm_init_hwmon() allocates the nominal speed table with

	devm_kmalloc_array(dev, data->i8k_fan_max + 1, ...)

and fan_max=4294967295 wraps the count to 0. devm_kmalloc() then returns ZERO_SIZE_PTR, which the first successful nominal speed call writes through.

Values above 255 are also meaningless: the fan state is passed to the BIOS as fan | (speed << 8), so the higher bits end up in other parts of the request. Large values that do not wrap still cost one SMM call per state and fan while probing.

Fail probing if the maximum fan state does not fit in a byte, like the existing check for fan_mult.

Fixes 81474fc2fae9 ("i8k: Add support for configurable maximum fan speed value")
---
 drivers/hwmon/dell-smm-hwmon.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/hwmon/dell-smm-hwmon.c +6 -0
index e8b4e8a29e8b..24a9172b7fed 100644
@@ -1249,6 +1249,12 @@ static int dell_smm_init_data(struct device *dev, const struct dell_smm_ops *ops
 		return -EINVAL;
 	}
 	data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH;
+	/* The BIOS takes the fan state in a single byte */
+	if (data->i8k_fan_max > U8_MAX) {
+		dev_err(dev, "maximum fan state %u is too large (max %u)\n",
+			data->i8k_fan_max, U8_MAX);
+		return -EINVAL;
+	}
 	data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max);
 
 	return 0;
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/2] hwmon: (dell-smm) Clamp the pwm value before scaling it

In reply to Rosen Penev
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

dell_smm_write() scales the value written to pwmN with

	DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult)

before clamping the result to the valid fan states. The rounding adds i8k_pwm_mult / 2 to val first, which overflows for values near LONG_MAX and wraps to a large negative number. That is then clamped to 0, so writing a huge value turns the fan off instead of running it at the highest speed. With the default fan_max of 2, writing any value from LONG_MAX - 63 up to LONG_MAX selects fan state 0.

Quoted 4 lines

Clamp val to the pwm range of 0 to 255 first.

Fixes e7f5f275c62a ("i8k: Implement hwmon based fan speed control")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

Re: [PATCH 2/2] hwmon: (dell-smm) Reject a fan_max that the BIOS cannot take

In reply to Rosen Penev
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

The fan_max module parameter is used without any range check, unlike fan_mult. dell_smm_init_hwmon() allocates the nominal speed table with

	devm_kmalloc_array(dev, data->i8k_fan_max + 1, ...)

and fan_max=4294967295 wraps the count to 0. devm_kmalloc() then returns ZERO_SIZE_PTR, which the first successful nominal speed call writes through.

Values above 255 are also meaningless: the fan state is passed to the BIOS as fan | (speed << 8), so the higher bits end up in other parts of the request. Large values that do not wrap still cost one SMM call per state and fan while probing.

Quoted 5 lines

Fail probing if the maximum fan state does not fit in a byte, like the existing check for fan_mult.

Fixes 81474fc2fae9 ("i8k: Add support for configurable maximum fan speed value")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

PATCH 1/3 platform/x86: dell-laptop: Reject keyboard backlight timeouts that overflow

In reply to Rosen Penev

kbd_led_timeout_store() converts a timeout that is too large for its unit to seconds with plain int multiplication. A large enough value wraps, and if the wrapped number happens to be a valid timeout it is accepted. On a Precision M4800, writing "4424214d" (over 12000 years) to stop_timeout sets the timeout to 4 minutes, and "18989323d" sets it to 2 minutes.

Convert with check_mul_overflow() and return -EINVAL on overflow.

Fixes 02b2aaaa57ab ("platform: x86: dell-laptop: Add support for keyboard backlight")
---
 drivers/platform/x86/dell/dell-laptop.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +15 -6
index 89e85c7f7132..bee347e1d60c 100644
@@ -25,6 +25,7 @@
 #include <linux/sysfs.h>
 #include <linux/acpi.h>
 #include <linux/mm.h>
+#include <linux/overflow.h>
 #include <linux/i8042.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
@@ -1554,6 +1555,7 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 	struct kbd_state state;
 	bool convert;
 	int value;
+	int mult;
 	int ret;
 	char ch;
 	u8 unit;
@@ -1602,16 +1604,23 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 		/* Convert value from current units to seconds */
 		switch (unit) {
 		case KBD_TIMEOUT_DAYS:
-			value *= 24;
-			fallthrough;
+			mult = 24 * 60 * 60;
+			break;
 		case KBD_TIMEOUT_HOURS:
-			value *= 60;
-			fallthrough;
+			mult = 60 * 60;
+			break;
 		case KBD_TIMEOUT_MINUTES:
-			value *= 60;
-			unit = KBD_TIMEOUT_SECONDS;
+			mult = 60;
+			break;
+		default:
+			mult = 1;
+			break;
 		}
 
+		if (check_mul_overflow(value, mult, &value))
+			return -EINVAL;
+		unit = KBD_TIMEOUT_SECONDS;
+
 		if (quirks && quirks->needs_kbd_timeouts) {
 			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
 				if (value <= quirks->kbd_timeouts[i]) {
Signature · 1 line
2.56.0
#9 lore raw

PATCH 2/3 platform/x86: dell-laptop: Don't use an unread keyboard state at init

In reply to Rosen Penev

kbd_init_info() ignores the return value of kbd_get_state(). If the call fails, the uninitialized state on the stack is used to set kbd_previous_level and kbd_previous_mode_bit, which are later sent to the BIOS when triggers are enabled or the backlight is turned back on.

Return the error instead. kbd_get_info() has already filled in kbd_info at that point, so clear it as well: level control through kbd_get_state() cannot work, and kbd_init() then still registers the LED if at least two keyboard backlight tokens are available, which works without it.

Fixes 02b2aaaa57ab ("platform: x86: dell-laptop: Add support for keyboard backlight")
---
 drivers/platform/x86/dell/dell-laptop.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/platform/x86/dell/dell-laptop.c +6 -1
index bee347e1d60c..171728d63c81 100644
@@ -1455,7 +1455,12 @@ static inline int kbd_init_info(void)
 	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
 		kbd_timeout_ac_supported = true;
 
-	kbd_get_state(&state);
+	ret = kbd_get_state(&state);
+	if (ret) {
+		/* Leave level control to the SMBIOS tokens, if there are any */
+		memset(&kbd_info, 0, sizeof(kbd_info));
+		return ret;
+	}
 
 	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
 	if (kbd_info.seconds > 63)
Signature · 1 line
2.56.0
#11 lore raw

PATCH 3/3 platform/x86: dell-laptop: Parse the charge end threshold as an int

In reply to Rosen Penev

charge_control_end_threshold_store() passes a pointer to an int to kstrtouint(), which only builds because the kernel disables -Wpointer-sign, and makes the following "end < 0" check dead code. Use kstrtoint() like charge_control_start_threshold_store(). Values outside 0 to 100 are rejected either way.

---
 drivers/platform/x86/dell/dell-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/dell/dell-laptop.c +1 -1
index 171728d63c81..2ebfef8e5bbe 100644
@@ -2385,7 +2385,7 @@ static ssize_t charge_control_end_threshold_store(struct device *dev,
 {
 	int ret, end;
 
-	ret = kstrtouint(buf, 10, &end);
+	ret = kstrtoint(buf, 10, &end);
 	if (ret)
 		return ret;
 	if (end < 0 || end > 100)
Signature · 1 line
2.56.0
maintainer
#3 lore raw

Re: [PATCH 1/2] hwmon: (dell-smm) Clamp the pwm value before scaling it

In reply to Rosen Penev
Quoted 24 lines · Rosen Penev

dell_smm_write() scales the value written to pwmN with

	DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult)

before clamping the result to the valid fan states. The rounding adds i8k_pwm_mult / 2 to val first, which overflows for values near LONG_MAX and wraps to a large negative number. That is then clamped to 0, so writing a huge value turns the fan off instead of running it at the highest speed. With the default fan_max of 2, writing any value from LONG_MAX - 63 up to LONG_MAX selects fan state 0.

Clamp val to the pwm range of 0 to 255 first.

Fixes e7f5f275c62a ("i8k: Implement hwmon based fan speed control")
---
 drivers/hwmon/dell-smm-hwmon.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hwmon/dell-smm-hwmon.c +0 -0
index 5ec487843faa..e8b4e8a29e8b 100644

drivers/hwmon/dell-smm-hwmon.c static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 · line 1033

Quoted 4 lines
drivers/hwmon/dell-smm-hwmon.c +1 -0
@@ -1030,6 +1030,7 @@ static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32
 	case hwmon_pwm:
 		switch (attr) {
 		case hwmon_pwm_input:
+			val = clamp_val(val, 0, 255);

Per convention, invalid pwm value writes should return -EINVAL (because the range is well defined, unlike other limits which are chip dependent).

Thanks,
Guenter

maintainer Applied
#6 lore raw

Re: [PATCH 2/2] hwmon: (dell-smm) Reject a fan_max that the BIOS cannot take

In reply to Rosen Penev
Quoted 14 lines · Rosen Penev

The fan_max module parameter is used without any range check, unlike fan_mult. dell_smm_init_hwmon() allocates the nominal speed table with

	devm_kmalloc_array(dev, data->i8k_fan_max + 1, ...)

and fan_max=4294967295 wraps the count to 0. devm_kmalloc() then returns ZERO_SIZE_PTR, which the first successful nominal speed call writes through.

Values above 255 are also meaningless: the fan state is passed to the BIOS as fan | (speed << 8), so the higher bits end up in other parts of the request. Large values that do not wrap still cost one SMM call per state and fan while probing.

Quoted 5 lines

Fail probing if the maximum fan state does not fit in a byte, like the existing check for fan_mult.

Fixes 81474fc2fae9 ("i8k: Add support for configurable maximum fan speed value")

Applied.

Thanks,
Guenter

maintainer
#8 lore raw

Re: [PATCH 1/3] platform/x86: dell-laptop: Reject keyboard backlight timeouts that overflow

In reply to Rosen Penev
Quoted 68 lines · On Friday 13 November 2026 22:29:25 Rosen Penev wrote:

kbd_led_timeout_store() converts a timeout that is too large for its unit to seconds with plain int multiplication. A large enough value wraps, and if the wrapped number happens to be a valid timeout it is accepted. On a Precision M4800, writing "4424214d" (over 12000 years) to stop_timeout sets the timeout to 4 minutes, and "18989323d" sets it to 2 minutes.

Convert with check_mul_overflow() and return -EINVAL on overflow.

Fixes 02b2aaaa57ab ("platform: x86: dell-laptop: Add support for keyboard backlight")
---
 drivers/platform/x86/dell/dell-laptop.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +15 -6
index 89e85c7f7132..bee347e1d60c 100644
@@ -25,6 +25,7 @@
 #include <linux/sysfs.h>
 #include <linux/acpi.h>
 #include <linux/mm.h>
+#include <linux/overflow.h>
 #include <linux/i8042.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
@@ -1554,6 +1555,7 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 	struct kbd_state state;
 	bool convert;
 	int value;
+	int mult;
 	int ret;
 	char ch;
 	u8 unit;
@@ -1602,16 +1604,23 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 		/* Convert value from current units to seconds */
 		switch (unit) {
 		case KBD_TIMEOUT_DAYS:
-			value *= 24;
-			fallthrough;
+			mult = 24 * 60 * 60;
+			break;
 		case KBD_TIMEOUT_HOURS:
-			value *= 60;
-			fallthrough;
+			mult = 60 * 60;
+			break;
 		case KBD_TIMEOUT_MINUTES:
-			value *= 60;
-			unit = KBD_TIMEOUT_SECONDS;
+			mult = 60;
+			break;
+		default:
+			mult = 1;
+			break;
 		}
 
+		if (check_mul_overflow(value, mult, &value))
+			return -EINVAL;
+		unit = KBD_TIMEOUT_SECONDS;
+
 		if (quirks && quirks->needs_kbd_timeouts) {
 			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
 				if (value <= quirks->kbd_timeouts[i]) {
Signature · 1 line
2.56.0

This change does not work correctly. The code below the quoted change is then dividing the value by (60 * 60 * 24) again (after being multiplied above). So the newly adding check_mul_overflow() will reject valid inputs, just because they do not fit into the "value" variable.

maintainer
#10 lore raw

Re: [PATCH 2/3] platform/x86: dell-laptop: Don't use an unread keyboard state at init

In reply to Rosen Penev
Quoted 10 lines · On Friday 13 November 2026 22:29:26 Rosen Penev wrote:

kbd_init_info() ignores the return value of kbd_get_state(). If the call fails, the uninitialized state on the stack is used to set kbd_previous_level and kbd_previous_mode_bit, which are later sent to the BIOS when triggers are enabled or the backlight is turned back on.

Return the error instead. kbd_get_info() has already filled in kbd_info at that point, so clear it as well: level control through kbd_get_state() cannot work, and kbd_init() then still registers the LED if at least two keyboard backlight tokens are available, which works without it.

Why returning the hard error here? At this stage we know that the LED tokens are available, kbd_get_info() returned success for us. The kbd_get_state() could have temp issues.

The only problem is with the uninitialized kbd_previous_level and kbd_previous_mode_bit variables.

What about this?

if (kbd_get_state(&state) == 0) {
	kbd_previous_level = kbd_get_level(&state);
	kbd_previous_mode_bit = state.mode_bit;
} else {
	kbd_previous_level = 0;
	kbd_previous_mode_bit = 0;
}
Quoted 27 lines
Fixes 02b2aaaa57ab ("platform: x86: dell-laptop: Add support for keyboard backlight")
---
 drivers/platform/x86/dell/dell-laptop.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/platform/x86/dell/dell-laptop.c +6 -1
index bee347e1d60c..171728d63c81 100644
@@ -1455,7 +1455,12 @@ static inline int kbd_init_info(void)
 	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
 		kbd_timeout_ac_supported = true;
 
-	kbd_get_state(&state);
+	ret = kbd_get_state(&state);
+	if (ret) {
+		/* Leave level control to the SMBIOS tokens, if there are any */
+		memset(&kbd_info, 0, sizeof(kbd_info));
+		return ret;
+	}
 
 	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
 	if (kbd_info.seconds > 63)
Signature · 1 line
2.56.0