hwmon: (asus-ec-sensors) add WARN_ONCE() on invalid sensor index
Subsystems (2)
PATCH v2 1/1 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.
---
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(-)
drivers/hwmon/asus-ec-sensors.c +5 -2
@@ -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 ;
Signature · 1 line
2.39.5 (Apple Git-154)
Eugene Shalygin
maintainerQuoted 3 lines · Alexei Safin
Prevent undefined behavior by adding
WARN_ONCE()whenfind_ec_sensor_index()returns a negative value.
I'm not sure about WARN_ONCE, does it bring anything useful? Clients see the error in the return value, sensor reading is absent, do we need to duplicate that in the log? Guenter, may I leave it up to you, please?
Best regards,
Eugene
Guenter Roeck
maintainerQuoted 10 lines · Eugene Shalygin
Quoted 3 lines · Alexei Safin
Prevent undefined behavior by adding
WARN_ONCE()whenfind_ec_sensor_index()returns a negative value.I'm not sure about
WARN_ONCE, does it bring anything useful? Clients see the error in the return value, sensor reading is absent, do we need to duplicate that in the log? Guenter, may I leave it up to you, please?
If this is seen, it is an implementation error which needs to be fixed. Returning an error to userspace will leave users annoyed but will not result in a fix. The warning backtrace is warranted in this situation.
Guenter
Quoted 3 lines
If this is seen, it is an implementation error which needs to be fixed. Returning an error to userspace will leave users annoyed but will not result in a fix. The warning backtrace is warranted in this situation.
Thanks for the explanation! I'm still not convinced that such a generic error message (without the type and channel) can be of great help. Something serious needs to happen to trigger this error, like a hardware change, or a RAM failure... If the purpose of the message is only to grab attention, it could say that explicitly. But anyway,
Eugene
Guenter Roeck
maintainerQuoted 6 lines · Eugene Shalygin
Quoted 3 lines
If this is seen, it is an implementation error which needs to be fixed. Returning an error to userspace will leave users annoyed but will not result in a fix. The warning backtrace is warranted in this situation.
Thanks for the explanation! I'm still not convinced that such a generic error message (without the type and channel) can be of great
Feel free to suggest a better one. Maybe I misunderstood your earlier concerns, but it seemed to me that you objected to having a message in the first place, not to its contents.
Thanks,
Guenter
Quoted 7 lines
help. Something serious needs to happen to trigger this error, like a hardware change, or a RAM failure... If the purpose of the message is only to grab attention, it could say that explicitly. But anyway,
Reviewed-by Eugene Shalygin <[email protected]>Eugene
Eugene Shalygin
maintainerQuoted 6 lines · Guenter Roeck
Quoted 2 lines
Thanks for the explanation! I'm still not convinced that such a generic error message (without the type and channel) can be of great
Feel free to suggest a better one. Maybe I misunderstood your earlier concerns, but it seemed to me that you objected to having a message in the first place, not to its contents.
The only two conditions I can imaging to trigger the log message are hardware and/or firmware change and RAM instability. In both cases the message is not helpful: for the hardware change case I would need sensor type and channel, for the RAM-related cases I would need to know how often the problem repeats itself. Neither is possible with this log message, and therefore I'd rather log every time and with sensor type and channel.
Best regards,
Eugene
Guenter Roeck
maintainerQuoted 9 lines · Eugene Shalygin
Quoted 6 lines · Guenter Roeck
Quoted 2 lines
Thanks for the explanation! I'm still not convinced that such a generic error message (without the type and channel) can be of great
Feel free to suggest a better one. Maybe I misunderstood your earlier concerns, but it seemed to me that you objected to having a message in the first place, not to its contents.
Quoted 8 lines
The only two conditions I can imaging to trigger the log message are hardware and/or firmware change and RAM instability. In both cases the message is not helpful: for the hardware change case I would need sensor type and channel, for the RAM-related cases I would need to know how often the problem repeats itself. Neither is possible with this log message, and therefore I'd rather log every time and with sensor type and channel.
The message would only be expected if there is some programming error, where is_visible does not correctly disable non-existing sensors. That is the situation the messages try to catch.
You do have an excellent point, though: The messages should display the sensor type and channel. As for logging it every time: No, because _if_ there is a programming error it would clog the log, so once is enough to tell "something is wrong with the code, fix it". The same is actually true if the hardware changes "under the hood".
Regarding RAM errors: Those won't be caught by checks like this. You'd have to litter the whole kernel with checks at almost every line of code to even have a remote chance to catch problems like bit flips. Even then you'd still not catch cases where the code itself is changed. Even trying to catch that would be futile.
Thanks,
Guenter
Eugene Shalygin
maintainerQuoted 5 lines · Guenter Roeck
You do have an excellent point, though: The messages should display the sensor type and channel. As for logging it every time: No, because _if_ there is a programming error it would clog the log, so once is enough to tell "something is wrong with the code, fix it". The same is actually true if the hardware changes "under the hood".
But WARM_ONCE() can not behave the way we want, as far as I understand, printing a single warning for each sensor type and channel, can it?
Quoted 5 lines
Regarding RAM errors: Those won't be caught by checks like this. You'd have to litter the whole kernel with checks at almost every line of code to even have a remote chance to catch problems like bit flips. Even then you'd still not catch cases where the code itself is changed. Even trying to catch that would be futile.
I already had a bug report (race condition in this driver, another log message in the code) that kept me puzzled until the user reported that it was caused by a malfunctioning RAM module.
Eugene