linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: (ftsteutates) Fix TOCTOU race in fts_read()
@ 2025-06-06  7:16 Gui-Dong Han
  2025-06-06 13:28 ` Guenter Roeck
  0 siblings, 1 reply; 2+ messages in thread
From: Gui-Dong Han @ 2025-06-06  7:16 UTC (permalink / raw)
  To: jdelvare, linux
  Cc: linux-hwmon, linux-kernel, baijiaju1990, Gui-Dong Han, stable

In the fts_read() function, when handling hwmon_pwm_auto_channels_temp,
the code accesses the shared variable data->fan_source[channel] twice
without holding any locks. It is first checked against
FTS_FAN_SOURCE_INVALID, and if the check passes, it is read again
when used as an argument to the BIT() macro.

This creates a Time-of-Check to Time-of-Use (TOCTOU) race condition.
Another thread executing fts_update_device() can modify the value of
data->fan_source[channel] between the check and its use. If the value
is changed to FTS_FAN_SOURCE_INVALID (0xff) during this window, the
BIT() macro will be called with a large shift value (BIT(255)).
A bit shift by a value greater than or equal to the type width is
undefined behavior and can lead to a crash or incorrect values being
returned to userspace.

Fix this by reading data->fan_source[channel] into a local variable
once, eliminating the race condition. Additionally, add a bounds check
to ensure the value is less than BITS_PER_LONG before passing it to
the BIT() macro, making the code more robust against undefined behavior.

This possible bug was found by an experimental static analysis tool
developed by our team.

Fixes: 1c5759d8ce05 ("hwmon: (ftsteutates) Replace fanX_source with pwmX_auto_channels_temp")
Cc: stable@vger.kernel.org
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
---
 drivers/hwmon/ftsteutates.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/ftsteutates.c b/drivers/hwmon/ftsteutates.c
index a3a07662e491..8aeec16a7a90 100644
--- a/drivers/hwmon/ftsteutates.c
+++ b/drivers/hwmon/ftsteutates.c
@@ -423,13 +423,16 @@ static int fts_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 		break;
 	case hwmon_pwm:
 		switch (attr) {
-		case hwmon_pwm_auto_channels_temp:
-			if (data->fan_source[channel] == FTS_FAN_SOURCE_INVALID)
+		case hwmon_pwm_auto_channels_temp: {
+			u8 fan_source = data->fan_source[channel];
+
+			if (fan_source == FTS_FAN_SOURCE_INVALID || fan_source >= BITS_PER_LONG)
 				*val = 0;
 			else
-				*val = BIT(data->fan_source[channel]);
+				*val = BIT(fan_source);
 
 			return 0;
+		}
 		default:
 			break;
 		}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] hwmon: (ftsteutates) Fix TOCTOU race in fts_read()
  2025-06-06  7:16 [PATCH] hwmon: (ftsteutates) Fix TOCTOU race in fts_read() Gui-Dong Han
@ 2025-06-06 13:28 ` Guenter Roeck
  0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2025-06-06 13:28 UTC (permalink / raw)
  To: Gui-Dong Han; +Cc: jdelvare, linux-hwmon, linux-kernel, baijiaju1990, stable

On Fri, Jun 06, 2025 at 07:16:40AM +0000, Gui-Dong Han wrote:
> In the fts_read() function, when handling hwmon_pwm_auto_channels_temp,
> the code accesses the shared variable data->fan_source[channel] twice
> without holding any locks. It is first checked against
> FTS_FAN_SOURCE_INVALID, and if the check passes, it is read again
> when used as an argument to the BIT() macro.
> 
> This creates a Time-of-Check to Time-of-Use (TOCTOU) race condition.
> Another thread executing fts_update_device() can modify the value of
> data->fan_source[channel] between the check and its use. If the value
> is changed to FTS_FAN_SOURCE_INVALID (0xff) during this window, the
> BIT() macro will be called with a large shift value (BIT(255)).
> A bit shift by a value greater than or equal to the type width is
> undefined behavior and can lead to a crash or incorrect values being
> returned to userspace.
> 
> Fix this by reading data->fan_source[channel] into a local variable
> once, eliminating the race condition. Additionally, add a bounds check
> to ensure the value is less than BITS_PER_LONG before passing it to
> the BIT() macro, making the code more robust against undefined behavior.
> 
> This possible bug was found by an experimental static analysis tool
> developed by our team.
> 
> Fixes: 1c5759d8ce05 ("hwmon: (ftsteutates) Replace fanX_source with pwmX_auto_channels_temp")
> Cc: stable@vger.kernel.org
> Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>

Applied.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-06-06 13:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-06  7:16 [PATCH] hwmon: (ftsteutates) Fix TOCTOU race in fts_read() Gui-Dong Han
2025-06-06 13:28 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).