[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250425200051.2410-1-a.safin@rosa.ru>
Date: Fri, 25 Apr 2025 23:00:51 +0300
From: Alexei Safin <a.safin@...a.ru>
To: Eugene Shalygin <eugene.shalygin@...il.com>
Cc: Alexei Safin <a.safin@...a.ru>,
Jean Delvare <jdelvare@...e.com>,
Guenter Roeck <linux@...ck-us.net>,
linux-hwmon@...r.kernel.org,
linux-kernel@...r.kernel.org,
lvc-project@...uxtesting.org
Subject: [PATCH v2] hwmon: (asus-ec-sensors) add WARN_ONCE() on invalid sensor index
Prevent undefined behavior by adding WARN_ONCE() when find_ec_sensor_index()
returns a negative value.
Even though unsupported attributes are filtered out by asus_ec_hwmon_is_visible(),
a programming error could still cause an invalid sensor access.
Instead of silently returning an error, log a warning to highlight the problem,
and propagate the original error code.
Update both asus_ec_hwmon_read() and asus_ec_hwmon_read_string() accordingly.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes: d0ddfd241e57 ("hwmon: (asus-ec-sensors) add driver for ASUS EC")
Signed-off-by: Alexei Safin <a.safin@...a.ru>
---
v2: Use WARN_ONCE() instead of returning -EINVAL, and update
both asus_ec_hwmon_read() and asus_ec_hwmon_read_string()
as suggested by Eugene Shalygin.
drivers/hwmon/asus-ec-sensors.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/asus-ec-sensors.c b/drivers/hwmon/asus-ec-sensors.c
index d893cfd1cb82..a563d7acef2e 100644
--- a/drivers/hwmon/asus-ec-sensors.c
+++ b/drivers/hwmon/asus-ec-sensors.c
@@ -820,9 +820,8 @@ static int asus_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
struct ec_sensors_data *state = dev_get_drvdata(dev);
int sidx = find_ec_sensor_index(state, type, channel);
- if (sidx < 0) {
+ if (WARN_ONCE(sidx < 0, "asus-ec-sensors: sensor not found\n"))
return sidx;
- }
ret = get_cached_value_or_update(dev, sidx, state, &value);
if (!ret) {
@@ -839,6 +838,10 @@ static int asus_ec_hwmon_read_string(struct device *dev,
{
struct ec_sensors_data *state = dev_get_drvdata(dev);
int sensor_index = find_ec_sensor_index(state, type, channel);
+
+ if (WARN_ONCE(sensor_index < 0, "asus-ec-sensors: sensor not found\n"))
+ return sensor_index;
+
*str = get_sensor_info(state, sensor_index)->label;
return 0;
--
2.39.5 (Apple Git-154)
Powered by blists - more mailing lists