Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>,
	Ed Brindley <kernel@maidavale.org>,
	Denis Pauk <pauk.denis@gmail.com>
Subject: [PATCH 02/11] hwmon: (asus_wmi_sensors) Avoid global struct mutation
Date: Fri, 30 Oct 2026 11:52:47 -0700	[thread overview]
Message-ID: <20261030185256.976255-3-linux@roeck-us.net> (raw)
In-Reply-To: <20261030185256.976255-1-linux@roeck-us.net>

In the asus_wmi_sensors driver, device-managed memory allocated in
asus_wmi_configure_sensor_setup() is assigned to a global static structure,
leading to a potential use-after-free when the device is unbound.
The driver defines a single global asus_wmi_chip_info structure to register
with the hwmon core. During probe, it dynamically allocates the .info
channel array using devm_kcalloc() and assigns it directly to this global
structure. When a device is unbound, its device-managed memory is
automatically freed, leaving asus_wmi_chip_info.info as a dangling pointer.
Furthermore, if multiple WMI devices are probed sequentially, each probe
overwrites the same global pointer, forcing all registered hwmon instances
to reference the channel information of the most recently probed device.

While it is unlikely if not impossible that multiple WMI devices of the same
kind exist, modifying a global static data structure is undesirable. Remove
the static data structure and allocate it as part of struct asus_wmi_sensors
to avoid the potential problem.

Fixes: 548820e21ce1 ("hwmon: (asus_wmi_sensors) Support X370 Asus WMI.")
Cc: Ed Brindley <kernel@maidavale.org>
Cc: Denis Pauk <pauk.denis@gmail.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/asus_wmi_sensors.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/hwmon/asus_wmi_sensors.c b/drivers/hwmon/asus_wmi_sensors.c
index 621e3506972d..0feab3f62496 100644
--- a/drivers/hwmon/asus_wmi_sensors.c
+++ b/drivers/hwmon/asus_wmi_sensors.c
@@ -164,6 +164,7 @@ struct asus_wmi_wmi_info {
 
 struct asus_wmi_sensors {
 	struct asus_wmi_wmi_info wmi;
+	struct hwmon_chip_info chip_info;
 	/* lock access to internal cache */
 	struct mutex lock;
 };
@@ -496,11 +497,6 @@ static const struct hwmon_ops asus_wmi_hwmon_ops = {
 	.read_string = asus_wmi_hwmon_read_string,
 };
 
-static struct hwmon_chip_info asus_wmi_chip_info = {
-	.ops = &asus_wmi_hwmon_ops,
-	.info = NULL,
-};
-
 static int asus_wmi_configure_sensor_setup(struct device *dev,
 					   struct asus_wmi_sensors *sensor_data)
 {
@@ -508,7 +504,7 @@ static int asus_wmi_configure_sensor_setup(struct device *dev,
 	struct hwmon_channel_info *asus_wmi_hwmon_chan;
 	int nr_count[hwmon_max] = {}, nr_types = 0;
 	struct asus_wmi_sensor_info *temp_sensor;
-	const struct hwmon_chip_info *chip_info;
+	struct hwmon_chip_info *chip_info;
 	enum hwmon_sensor_types type;
 	struct device *hwdev;
 	int i, idx;
@@ -549,8 +545,9 @@ static int asus_wmi_configure_sensor_setup(struct device *dev,
 	if (!ptr_asus_wmi_ci)
 		return -ENOMEM;
 
-	asus_wmi_chip_info.info = ptr_asus_wmi_ci;
-	chip_info = &asus_wmi_chip_info;
+	chip_info = &sensor_data->chip_info;
+	chip_info->ops = &asus_wmi_hwmon_ops;
+	chip_info->info = ptr_asus_wmi_ci;
 
 	sensor_data->wmi.info_by_id = devm_kcalloc(dev, sensor_data->wmi.sensor_count,
 						   sizeof(*sensor_data->wmi.info_by_id),
-- 
2.45.2


  parent reply	other threads:[~2026-10-30 18:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 18:52 [PATCH 0/11] hwmon: Various fixes Guenter Roeck
2026-10-30 18:52 ` [PATCH 01/11] hwmon: (aquacomputer_d5next) Check maxcollection when probing AQUAERO devices Guenter Roeck
2026-10-30 18:52 ` Guenter Roeck [this message]
2026-10-30 18:52 ` [PATCH 03/11] hwmon: (hp-wmi-sensors) Avoid global struct mutation Guenter Roeck
2026-10-30 18:52 ` [PATCH 04/11] hwmon: (hp-wmi-sensors) Fix unbounded loops in scale_numeric_sensor() Guenter Roeck
2026-10-30 18:52 ` [PATCH 05/11] hwmon: (lm87) Fix drvdata type confusion in vrm_show() and vrm_store() Guenter Roeck
2026-10-30 18:52 ` [PATCH 06/11] hwmon: (nct6683) Fix incorrect struct cast in show_pwm() and store_pwm() Guenter Roeck
2026-10-30 18:52 ` [PATCH 07/11] hwmon: (peci/dimmtemp) Fix devres leak and UAF on concurrent unbind Guenter Roeck
2026-10-30 18:52 ` [PATCH 08/11] hwmon: (pmbus/q54sj108a2) Add missing NULL pointer check Guenter Roeck
2026-10-30 18:52 ` [PATCH 09/11] hwmon: (pmbus) Check return value from pmbus_thermal_add_sensor() Guenter Roeck
2026-10-30 18:52 ` [PATCH 10/11] hwmon: (pmbus) Release devres allocated memory after devm_thermal_of_zone_register error Guenter Roeck
2026-10-30 18:52 ` [PATCH 11/11] hwmon: (pmbus) Fix return value check from thermal zone registration Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261030185256.976255-3-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=kernel@maidavale.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=pauk.denis@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox