Skip to contentSkip to conversation

platform/x86: lenovo: wmi-other: Fix uninitialized variable in lwmi_om_hwmon_write()

PATCH Superseded Yufei CHENG 4 participants 9 messages Apr 26 – Apr 30

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

PATCH v2 1/1 platform/x86: lenovo: wmi-other: Fix uninitialized variable in lwmi_om_hwmon_write()

When the flag relax_fan_constraint is set, local variable 'raw' is never assigned, and lwmi_om_hwmon_write() will pass uninitialized value to lwmi_om_fan_get_set() resulting in undefined behavior.

This flag allows user to bypass minimum fan RPM divisor rounding, but assignment to 'raw' only happens in the non-relaxed path. Fix by defaulting 'raw' to user provided 'val' in the else branch.

Fixes 51ed34282f63fab5b3996477cc56135eb4de5284

---
v2: rewrite the commit title to be more specific

 drivers/platform/x86/lenovo/wmi-other.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/x86/lenovo/wmi-other.c +2 -0
index 6040f45aa..6c2febe1a 100644
@@ -349,6 +349,8 @@ static int lwmi_om_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
 			 */
 			if (!relax_fan_constraint)
 				raw = val / LWMI_FAN_DIV * LWMI_FAN_DIV;
+			else
+				raw = val;
 
 			err = lwmi_om_fan_get_set(priv, channel, &raw, true);
 			if (err)
Signature · 1 line
2.43.0
#1 lore raw

Hi Yufei,

Quoted 9 lines · Yufei CHENG

When the flag relax_fan_constraint is set, local variable 'raw' is never assigned, and lwmi_om_hwmon_write() will pass uninitialized value to lwmi_om_fan_get_set() resulting in undefined behavior.

This flag allows user to bypass minimum fan RPM divisor rounding, but assignment to 'raw' only happens in the non-relaxed path. Fix by defaulting 'raw' to user provided 'val' in the else branch.

Fixes 51ed34282f63fab5b3996477cc56135eb4de5284

You didn't run scripts/checkpatch.pl before submitting patches, as I've reminded in the previous reply to v1.

Had you run it, you would see:

WARNING: Please use correct Fixes: style 'Fixes: <12+ chars of sha1> ("<title line>")' - ie: 'Fixes: 51ed34282f63 ("platform/x86: lenovo-wmi-
other: Add HWMON for fan reporting/tuning")'

Quoted 1 line

My Reviewed-by was dropped. You should've included it in v2.

Please read https://docs.kernel.org/process/submitting-patches.html

Thanks,
Rong

Quoted 19 lines

---
v2: rewrite the commit title to be more specific

 drivers/platform/x86/lenovo/wmi-other.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/x86/lenovo/wmi-other.c +2 -0
index 6040f45aa..6c2febe1a 100644
@@ -349,6 +349,8 @@ static int lwmi_om_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
 			 */
 			if (!relax_fan_constraint)
 				raw = val / LWMI_FAN_DIV * LWMI_FAN_DIV;
+			else
+				raw = val;
 
 			err = lwmi_om_fan_get_set(priv, channel, &raw, true);
 			if (err)
#2 lore raw

Re: [PATCH v2] platform/x86: lenovo: wmi-other: Fix uninitialized

Hi Rong,

Thanks for your feedback, I've corrected the format and resend the patch. However I think I have to point out some other issues in this file.

First, we should not complexify simple things. You use lwmi_om_fan_get_set() to combine get and set into a single function with a boolean flag to determine the action.
This makes the control flow harder to follow and audit. Furthermore, this layer of abstraction is not applied constantly throughout the whole file. In some places, values are set through lwmi_om_fan_get_set() and in other places, they're called manually through lwmi_dev_evaluate_int()
This inconsistency makes the abstraction pointless and is the direct cause of a second uninitialized bug

In line 855, args.arg0 is set to attribute_id, but arg1 is never set. Though arg1 is omitted in read action, you're still passing stack garbage to the lwmi_dev_evaluate_int(). And I think it's better to zero it out if not used.

Additionally, I doubt whether bypassing the minimum RPM divisor has any practical use at all, not to mention it's not guranteed to work and purely depends on whether EC accepts that sub-division value.

I may do some refactor recently, and I'm happy to hear your suggestions.

Thanks,
Yufei

maintainer
#3 lore raw

Re: [PATCH v2] platform/x86: lenovo: wmi-other: Fix uninitialized

Quoted 15 lines · Yufei CHENG

Hi Rong,

Thanks for your feedback, I've corrected the format and resend the patch. However I think I have to point out some other issues in this file.

First, we should not complexify simple things. You use lwmi_om_fan_get_set() to combine get and set into a single function with a boolean flag to determine the action.
This makes the control flow harder to follow and audit. Furthermore, this layer of abstraction is not applied constantly throughout the whole file. In some places, values are set through lwmi_om_fan_get_set() and in other places, they're called manually through lwmi_dev_evaluate_int()
This inconsistency makes the abstraction pointless and is the direct cause of a second uninitialized bug

Quoted 3 lines

In line 855, args.arg0 is set to attribute_id, but arg1 is never set. Though arg1 is omitted in read action, you're still passing stack garbage to the lwmi_dev_evaluate_int(). And I think it's better to zero it out if not used.

Hi Yufei,

This bug is already addressed in a patch series that has been submitted and is awaiting review from the subsystem maintainers.

Quoted 5 lines

Additionally, I doubt whether bypassing the minimum RPM divisor has any practical use at all, not to mention it's not guranteed to work and purely depends on whether EC accepts that sub-division value.

I may do some refactor recently, and I'm happy to hear your suggestions.

Rong has a large refactor series queued up to simplify much of the driver already, pending the acceptance of the previously mentioned series. It would be best to avoid doing any additional large refactoring until that series has landed.

Thanks,
Derek

Quoted 3 lines

Thanks,
Yufei

author
#4 lore raw

Re: [PATCH v2] platform/x86: lenovo: wmi-other: Fix uninitialized

Hi, Derek,
Thanks for your reply!

Quoted 4 lines · Derek J. Clark

Hi Yufei,

This bug is already addressed in a patch series that has been submitted and is awaiting review from the subsystem maintainers.

Which bug do you mean specifically? The args.arg1 uninitialized bug, or the original uninitialized 'raw' variable, or the inappropriate encapsulation?

Quoted 4 lines
Quoted 1 line

I may do some refactor recently, and I'm happy to hear your suggestions.

Rong has a large refactor series queued up to simplify much of the driver already, pending the acceptance of the previously mentioned series. It would be best to avoid doing any additional large refactoring until that series has landed.

OK, that sounds good. I'll wait until the refactor is done. I hope my suggestions can be adopted or we can discuss further about the appropriate architecture. I'm happy to hear your suggestions.

Thanks,
Yufei

maintainer
#5 lore raw

Re: [PATCH v2] platform/x86: lenovo: wmi-other: Fix uninitialized

Quoted 5 lines · no name

Hi, Derek,
Thanks for your reply!

On Mon, Apr 27, 2026 at 2:22 AM Derek J. Clark
<[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines

Hi Yufei,

This bug is already addressed in a patch series that has been submitted and is awaiting review from the subsystem maintainers.

Which bug do you mean specifically? The args.arg1 uninitialized bug, or the original uninitialized 'raw' variable, or the inappropriate encapsulation?

I was replying to that specific part of your email, regarding the args initializing.

See <https://lore.kernel.org/platform-driver-x86/[email protected]/T/#mc7f4ab5f74cd2fa21eedcb6d91488b76d3ba3f31>

Thanks,
Derek

Quoted 10 lines
Quoted 4 lines
Quoted 1 line

I may do some refactor recently, and I'm happy to hear your suggestions.

Rong has a large refactor series queued up to simplify much of the driver already, pending the acceptance of the previously mentioned series. It would be best to avoid doing any additional large refactoring until that series has landed.

OK, that sounds good. I'll wait until the refactor is done. I hope my suggestions can be adopted or we can discuss further about the appropriate architecture. I'm happy to hear your suggestions.

Thanks,
Yufei

author
#6 lore raw

Re: [PATCH v2] platform/x86: lenovo: wmi-other: Fix uninitialized

Thanks for your reply! I appreciate your work on the patch series. If you or Rong have any suggestions, I'm happy to hear. Thanks,
Yufei

#7 lore raw

PATCH v3 1/1 platform/x86: lenovo: wmi-other: Fix uninitialized variable in lwmi_om_hwmon_write()

In reply to Yufei CHENG

When the flag relax_fan_constraint is set, local variable 'raw' is never assigned, and lwmi_om_hwmon_write() will pass uninitialized value to lwmi_om_fan_get_set() resulting in undefined behavior.

This flag allows user to bypass minimum fan RPM divisor rounding, but assignment to 'raw' only happens in the non-relaxed path. Fix by defaulting 'raw' to user provided 'val' in the else branch.

Fixes 51ed34282f63 ("platform/x86: lenovo-wmi-other: Add HWMON for fan reporting/tuning")

---
v3: fix broken format
v2: rewrite the title to be more specific

 drivers/platform/x86/lenovo/wmi-other.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/x86/lenovo/wmi-other.c +2 -0
index 6040f45aa2b..6c2febe1a59 100644
@@ -349,6 +349,8 @@ static int lwmi_om_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
 			 */
 			if (!relax_fan_constraint)
 				raw = val / LWMI_FAN_DIV * LWMI_FAN_DIV;
+			else
+				raw = val;
 
 			err = lwmi_om_fan_get_set(priv, channel, &raw, true);
 			if (err)
Signature · 1 line
2.43.0
maintainer
#8 lore raw
Quoted 9 lines · Yufei CHENG

When the flag relax_fan_constraint is set, local variable 'raw' is never assigned, and lwmi_om_hwmon_write() will pass uninitialized value to lwmi_om_fan_get_set() resulting in undefined behavior.

This flag allows user to bypass minimum fan RPM divisor rounding, but assignment to 'raw' only happens in the non-relaxed path. Fix by defaulting 'raw' to user provided 'val' in the else branch.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-fixes branch. Note it will show up in the public platform-drivers-x86/review-ilpo-fixes branch only once I've pushed my local branch there, which might take a while.

The list of commits applied:
[1/1] platform/x86: lenovo: wmi-other: Fix uninitialized variable in lwmi_om_hwmon_write()

      commit: 863810d4985ad214f70c1623f24384ccc850f2a2

--
i.