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>,
	Sashiko <sashiko-bot@kernel.org>,
	Billy Tsai <billy_tsai@aspeedtech.com>
Subject: [PATCH] hwmon: (aspeed-g6-pwm-tach) Validate the number of tach channels
Date: Mon, 29 Jun 2026 16:55:03 -0700	[thread overview]
Message-ID: <20260629235503.2019362-1-linux@roeck-us.net> (raw)

Sashiko reports:

In aspeed_create_fan_monitor(), the driver reads raw channel numbers:
ret = of_property_read_u8_array(child, "tach-ch", tach_ch, count);
Then in aspeed_present_fan_tach(), it uses the unvalidated channel value
to index into the statically-sized tach_present array:
priv->tach_present[ch] = true;
It also uses the unvalidated channel value to calculate MMIO offsets:
val = readl(priv->base + TACH_ASPEED_CTRL(ch));
If the device tree provides a channel number that exceeds the maximum
allowed channels, could this corrupt adjacent memory and trigger
out-of-bounds MMIO accesses?

Fix the problem by validating the number of tachometer channels reported
from of_property_count_u8_elems() and return an error if the number of
tachometer channels exceeds the number of supported channels. Also, in
aspeed_present_fan_tach(), validate the actual channel number in
aspeed_present_fan_tach() and return an error if it exceeds the number
of channels supported by the driver.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260629232305.2013406-1-linux%40roeck-us.net
Fixes: 7e1449cd15d10 ("hwmon: (aspeed-g6-pwm-tacho): Support for ASPEED g6 PWM/Fan tach")
Cc: Billy Tsai <billy_tsai@aspeedtech.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/aspeed-g6-pwm-tach.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/hwmon/aspeed-g6-pwm-tach.c b/drivers/hwmon/aspeed-g6-pwm-tach.c
index 5d611a8e5269..2cb3f2e1aeb2 100644
--- a/drivers/hwmon/aspeed-g6-pwm-tach.c
+++ b/drivers/hwmon/aspeed-g6-pwm-tach.c
@@ -402,13 +402,16 @@ static const struct hwmon_chip_info aspeed_tach_chip_info = {
 	.info = aspeed_tach_info,
 };
 
-static void aspeed_present_fan_tach(struct aspeed_pwm_tach_data *priv, u8 *tach_ch, int count)
+static int aspeed_present_fan_tach(struct aspeed_pwm_tach_data *priv, u8 *tach_ch, int count)
 {
 	u8 ch, index;
 	u32 val;
 
 	for (index = 0; index < count; index++) {
 		ch = tach_ch[index];
+		if (ch >= TACH_ASPEED_NR_TACHS)
+			return -EINVAL;
+
 		priv->tach_present[ch] = true;
 		priv->tach_divisor = DEFAULT_TACH_DIV;
 
@@ -424,6 +427,7 @@ static void aspeed_present_fan_tach(struct aspeed_pwm_tach_data *priv, u8 *tach_
 
 		aspeed_tach_ch_enable(priv, ch, true);
 	}
+	return 0;
 }
 
 static int aspeed_create_fan_monitor(struct device *dev,
@@ -434,7 +438,7 @@ static int aspeed_create_fan_monitor(struct device *dev,
 	u8 *tach_ch;
 
 	count = of_property_count_u8_elems(child, "tach-ch");
-	if (count < 1)
+	if (count < 1 || count > TACH_ASPEED_NR_TACHS)
 		return -EINVAL;
 	tach_ch = devm_kcalloc(dev, count, sizeof(*tach_ch), GFP_KERNEL);
 	if (!tach_ch)
@@ -443,9 +447,7 @@ static int aspeed_create_fan_monitor(struct device *dev,
 	if (ret)
 		return ret;
 
-	aspeed_present_fan_tach(priv, tach_ch, count);
-
-	return 0;
+	return aspeed_present_fan_tach(priv, tach_ch, count);
 }
 
 static void aspeed_pwm_tach_reset_assert(void *data)
-- 
2.45.2


             reply	other threads:[~2026-06-29 23:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-29 23:55 Guenter Roeck [this message]
2026-06-30  0:06 ` [PATCH] hwmon: (aspeed-g6-pwm-tach) Validate the number of tach channels sashiko-bot

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=20260629235503.2019362-1-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=billy_tsai@aspeedtech.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    /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