[PATCH 3/3] hwmon: (w83792d) Fix incorrect struct cast in show_temp1() and store_temp1()
From: Ziheng Wei
Date: Sun Nov 15 2026 - 03:02:49 EST
On big-endian architectures, accessing the temp1_max and temp1_max_hyst
sysfs attributes in the w83792d hardware monitoring driver uses a huge
out-of-bounds index, because show_temp1() and store_temp1() downcast the
attribute to the wrong struct.
The driver defines the temp1_* attributes with SENSOR_DEVICE_ATTR_2(),
which initializes them as struct sensor_device_attribute_2 (where the
index is an 8-bit u8). The temp1_input attribute has index 0, temp1_max
has index 1 and temp1_max_hyst has index 2. However, show_temp1() and
store_temp1() downcast the attribute to struct sensor_device_attribute
(where the index is a 32-bit int) and use it to index data->temp1[].
Because both structs start with struct device_attribute, container_of()
resolves to the same base address, but the field widths differ. On
big-endian architectures, reading the 32-bit int from the memory layout
of the two u8 fields yields index << 24: 0x01000000 for temp1_max and
0x02000000 for temp1_max_hyst, instead of 1 and 2. data->temp1[] has
only 3 elements, so the access runs off the end of the array by up to
32 MiB.
Fix the type casts in show_temp1() and store_temp1().
Confirmed on a big-endian s390x QEMU guest with the driver bound to an
emulated chip: temp1_max and temp1_max_hyst read 0 instead of the
register values. Not tested on real hardware.
Fixes: 1f8dae31fe8d ("[PATCH] hwmon: w83792d use arrays of attributes")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
Notes:
The changelog was drafted with an AI assistant from my analysis;
I have reviewed the code, the fix and the text.
drivers/hwmon/w83792d.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/w83792d.c b/drivers/hwmon/w83792d.c
index 2de6d7b68..cba907713 100644
--- a/drivers/hwmon/w83792d.c
+++ b/drivers/hwmon/w83792d.c
@@ -487,7 +487,7 @@ store_fan_div(struct device *dev, struct device_attribute *attr,
static ssize_t show_temp1(struct device *dev, struct device_attribute *attr,
char *buf)
{
- struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
+ struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
int nr = sensor_attr->index;
struct w83792d_data *data = w83792d_update_device(dev);
return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[nr]));
@@ -496,7 +496,7 @@ static ssize_t show_temp1(struct device *dev, struct device_attribute *attr,
static ssize_t store_temp1(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
- struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
+ struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
int nr = sensor_attr->index;
struct i2c_client *client = to_i2c_client(dev);
struct w83792d_data *data = i2c_get_clientdata(client);
--
2.43.0