[PATCH] power: supply: hwmon: reject writes that do not fit in an int
From: Weidong Wang via B4 Relay
Date: Sat Nov 28 2026 - 22:18:01 EST
From: Weidong Wang <weidongw@xxxxxxxxxxxx>
hwmon_attr_store() parses the user buffer with kstrtol() and passes the
resulting long to power_supply_hwmon_write(), which stores it in the int
member of union power_supply_propval before converting the unit:
pspval.intval = val;
On 64-bit kernels a value outside the int range is silently truncated by
this assignment. For current and voltage attributes the following
check_mul_overflow() only sees the truncated value, and for temperature
attributes there is no check at all. Writing 4294967297 to in0_max, for
example, is accepted and sets VOLTAGE_MAX to 1000 uV instead of failing.
Return -EOVERFLOW, as the scaling overflow check already does, when the
value does not fit in an int.
The affected attributes are only writable by root and only exist for
supplies that mark the property writable.
Found by an automated code scan and confirmed by code inspection.
The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.
Fixes: e67d4dfc9ff1 ("power: supply: Add HWMON compatibility layer")
Signed-off-by: Weidong Wang <weidongw@xxxxxxxxxxxx>
---
The range check is placed before the narrowing assignment so that it
covers all three sensor types. -EOVERFLOW was chosen to match the error
the existing check_mul_overflow() path already returns. Clamping to the
int range, as many hwmon drivers do for limits, would be an alternative;
I kept rejecting because this function already rejects on overflow.
On 32-bit long and int have the same width, so nothing changes there.
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/power/supply/power_supply_hwmon.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/power/supply/power_supply_hwmon.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c
index 95245e6a6baa..f76cbd7a6da1 100644
--- a/drivers/power/supply/power_supply_hwmon.c
+++ b/drivers/power/supply/power_supply_hwmon.c
@@ -279,6 +279,9 @@ power_supply_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
if (prop < 0)
return prop;
+ if (val < INT_MIN || val > INT_MAX)
+ return -EOVERFLOW;
+
pspval.intval = val;
switch (type) {
---
base-commit: f49a9afecd6d55842dab3f9b0ccb702d49db6654
change-id: 20261128-kbug-l100-094ff3585c56
Best regards,
--
Weidong Wang <weidongw@xxxxxxxxxxxx>