From: sashiko-bot@kernel.org
To: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>
Cc: "Bjorn Helgaas" <bhelgaas@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Len Brown" <lenb@kernel.org>,
"Linus Walleij" <linusw@kernel.org>,
"Bartosz Golaszewski" <brgl@kernel.org>,
"Sven Peter" <sven@kernel.org>, "Janne Grunau" <j@jannau.net>,
"Neal Gompa" <neal@gompa.dev>,
"Guenter Roeck" <linux@roeck-us.net>,
"Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>, "Lee Jones" <lee@kernel.org>,
"Pavel Machek" <pavel@kernel.org>,
"Griffin Kroah-Hartman" <griffin@kroah.com>,
"Hans de Goede" <hansg@kernel.org>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Sebastian Reichel" <sre@kernel.org>,
"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
"Wim Van Sebroeck" <wim@linux-watchdog.org>,
"Jarkko Sakkinen" <jarkko@kernel.org>,
"Mimi Zohar" <zohar@linux.ibm.com>,
"Roberto Sassu" <roberto.sassu@huawei.com>,
"Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>,
"Eric Snowberg" <eric.snowberg@oracle.com>,
"Paul Moore" <paul@paul-moore.com>,
"James Morris" <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
linux-pci@vger.kernel.org, linux-gpio@vger.kernel.org,
linux-hwmon@vger.kernel.org, linux-leds@vger.kernel.org,
mfd@lists.linux.dev, linux-rtc@vger.kernel.org,
linux-watchdog@vger.kernel.org, keyrings@vger.kernel.org,
linux-security-module@vger.kernel.org
Subject: Re: [RFC PATCH 12/14] hwmon/macsmc: add more types
Date: Thu, 29 Oct 2026 09:18:58 +0000 [thread overview]
Message-ID: <sashiko-outbox-189021@kernel.org> (raw)
In-Reply-To: <20261029085504.3547-13-atharvatiwarilinuxdev@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hwmon: 32-bit integer overflow when scaling values in macsmc_hwmon_read_key()
--
commit 8d01f24623ebf973c1f3f366376ae2ac53217c82
Author: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
hwmon/macsmc: add more types
This commit adds support for reading common integer (ui8, si8, ui16, si16,
ui32, si32) and fixed-point (fp* and sp*) SMC key data types in
macsmc_hwmon_read_key(), and allows writing fpe2 keys for fan speed control
in macsmc_hwmon_write_key().
> diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c
> index 1500ec2cc9f83..2e74dcd6651b3 100644
> --- a/drivers/hwmon/macsmc-hwmon.c
> +++ b/drivers/hwmon/macsmc-hwmon.c
> @@ -221,6 +221,313 @@ static int macsmc_hwmon_read_key(struct apple_smc *smc,
> *val = (long)ioft;
> break;
> }
[ ... ]
> + /* unsigned 16-bit integer */
> + case __SMC_KEY('u', 'i', '1', '6'): {
> + u16 ui16 = 0;
> +
> + ret = apple_smc_read_u16(smc, sensor->macsmc_key, &ui16);
> + if (ret)
> + return ret;
> +
> + *val = ui16 * scale;
> + break;
> + }
> + /* signed 16-bit integer */
> + case __SMC_KEY('s', 'i', '1', '6'): {
> + s16 si16 = 0;
> +
> + ret = apple_smc_read_s16(smc, sensor->macsmc_key, &si16);
> + if (ret)
> + return ret;
> +
> + *val = si16 * scale;
> + break;
> + }
> + /* unsigned 32-bit integer */
> + case __SMC_KEY('u', 'i', '3', '2'): {
> + u32 ui32 = 0;
> +
> + ret = apple_smc_read_u32(smc, sensor->macsmc_key, &ui32);
> + if (ret)
> + return ret;
> +
> + *val = ui32 * scale;
> + break;
> + }
> + /* signed 32-bit integer */
> + case __SMC_KEY('s', 'i', '3', '2'): {
> + s32 si32;
> +
> + ret = apple_smc_read_s32(smc, sensor->macsmc_key, &si32);
> + if (ret)
> + return ret;
> +
> + *val = si32 * scale;
> + break;
> + }
> + /* 2.14 unsigned fixed point decimal */
> + case __SMC_KEY('f', 'p', '2', 'e'): {
> + u16 fp2e;
> +
> + ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp2e);
> + if (ret)
> + return ret;
> +
> + *val = mult_frac(be16_to_cpu(fp2e), scale, BIT(14));
> + break;
> + }
[ ... ]
> + /* 1.14 signed fixed point decimal */
> + case __SMC_KEY('s', 'p', '1', 'e'): {
> + s16 sp1e;
> +
> + ret = apple_smc_read_s16(smc, sensor->macsmc_key, &sp1e);
> + if (ret)
> + return ret;
> +
> + *val = mult_frac((s16)be16_to_cpu(sp1e), scale, BIT(14));
> + break;
> + }
[ ... ]
> + /* 15.0 signed fixed point decimal */
> + case __SMC_KEY('s', 'p', 'f', '0'): {
> + s16 spf0;
> +
> + ret = apple_smc_read_s16(smc, sensor->macsmc_key, &spf0);
> + if (ret)
> + return ret;
> +
> + *val = (s16)be16_to_cpu(spf0) * scale;
> + break;
> + }
[Severity: Medium]
Can these scaling calculations in macsmc_hwmon_read_key() overflow 32-bit
integers and corrupt sensor readings when called from macsmc_hwmon_read()?
In macsmc_hwmon_read(), scale is 1000000 for hwmon_power and 1000 for
hwmon_temp, hwmon_in, and hwmon_curr, while the scale parameter in
macsmc_hwmon_read_key() has type int:
drivers/hwmon/macsmc-hwmon.c:macsmc_hwmon_read() {
...
case hwmon_power:
/* SMC returns power in Watts with acceptable precision to scale to uW */
ret = macsmc_hwmon_read_key(hwmon->smc,
&hwmon->power.sensors[channel],
1000000, val);
break;
}
First, in the mult_frac() calls for the unsigned (fp2e through fp88) and
signed (sp1e through spd2) fixed-point types, the first argument has a 16-bit
type (u16 from be16_to_cpu() or s16 from (s16)be16_to_cpu()):
include/linux/math.h:
#define mult_frac(x, n, d) \
({ \
typeof(x) x_ = (x); \
typeof(n) n_ = (n); \
typeof(d) d_ = (d); \
\
typeof(x_) q = x_ / d_; \
typeof(x_) r = x_ % d_; \
q * n_ + r * n_ / d_; \
})
Because typeof(x_) is a 16-bit integer, q and r are promoted to 32-bit signed
int, and n_ (scale) is also a 32-bit signed int. Both q * n_ and r * n_ are
evaluated as 32-bit signed int multiplications before dividing by d_
(unsigned long from BIT(n)). When scale is 1000000, any remainder r >= 2148
(possible for fp2e, fp3d, fp4c, sp1e, sp2d, and sp3c) or quotient |q| >= 2148
(possible for spc3 and spd2) overflows 32-bit signed int into a negative
value, which then converts to a huge unsigned long when divided by BIT(n).
Second, for the signed sp* types (sp1e through spd2), passing a signed x_ with
an unsigned long divisor BIT(n) causes x_ / d_ and x_ % d_ inside mult_frac()
to be evaluated as unsigned division and modulo whenever x_ is negative,
corrupting negative readings.
Third, for ui16, si16, ui32, si32, and spf0, the multiplications by scale
(such as ui16 * scale, si32 * scale, and (s16)be16_to_cpu(spf0) * scale) are
all evaluated in 32-bit arithmetic before widening to long *val, which
overflows when scale is 1000000 (for values >= 2148 on signed/16-bit types or
>= 4295 on ui32) or 1000 (for 32-bit types).
Would it make sense to widen scale (or cast the operands and divisors to
signed long / s64) so that the multiplications and mult_frac() divisions are
performed in 64-bit signed arithmetic?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261029085504.3547-1-atharvatiwarilinuxdev@gmail.com?part=12
next prev parent reply other threads:[~2026-10-29 9:19 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 8:51 [RFC PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Atharva Tiwari
2026-10-29 8:51 ` [RFC PATCH 01/14] treewide: Add backend for macsmc Atharva Tiwari
2026-10-29 9:20 ` sashiko-bot
2026-10-29 8:51 ` [RFC PATCH 02/14] platform/x86: add backend for macsmc in intel macs Atharva Tiwari
2026-10-29 9:23 ` sashiko-bot
2026-10-29 12:59 ` Ilpo Järvinen
2026-10-29 8:52 ` [RFC PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-10-29 9:17 ` sashiko-bot
2026-10-29 13:30 ` Andy Shevchenko
2026-10-29 8:52 ` [RFC PATCH 04/14] iio/light: add new driver for macsmc Atharva Tiwari
2026-10-29 9:17 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 05/14] iio/accel: add driver for apple SMS Atharva Tiwari
2026-10-29 9:20 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-10-29 9:16 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc Atharva Tiwari
2026-10-29 9:15 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-10-29 9:11 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-10-29 9:16 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-10-29 9:19 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 11/14] acpi: disable battery and adapter for x86 apple machines Atharva Tiwari
2026-10-29 9:13 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-10-29 9:18 ` sashiko-bot [this message]
2026-10-29 8:52 ` [RFC PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-10-29 9:20 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-10-29 9:22 ` Sasha Finkelstein
2026-10-29 14:39 ` Atharva Tiwari
2026-10-29 14:53 ` Sasha Finkelstein
2026-10-29 9:26 ` 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=sashiko-outbox-189021@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alexandre.belloni@bootlin.com \
--cc=andy@kernel.org \
--cc=atharvatiwarilinuxdev@gmail.com \
--cc=bhelgaas@google.com \
--cc=bp@alien8.de \
--cc=brgl@kernel.org \
--cc=dave.hansen@linux.intel.com \
--cc=dlechner@baylibre.com \
--cc=dmitry.kasatkin@gmail.com \
--cc=eric.snowberg@oracle.com \
--cc=griffin@kroah.com \
--cc=hansg@kernel.org \
--cc=hpa@zytor.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=j@jannau.net \
--cc=jarkko@kernel.org \
--cc=jic23@kernel.org \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=lee@kernel.org \
--cc=lenb@kernel.org \
--cc=linusw@kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux-watchdog@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=mfd@lists.linux.dev \
--cc=mingo@redhat.com \
--cc=neal@gompa.dev \
--cc=nuno.sa@analog.com \
--cc=paul@paul-moore.com \
--cc=pavel@kernel.org \
--cc=rafael@kernel.org \
--cc=roberto.sassu@huawei.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=serge@hallyn.com \
--cc=sre@kernel.org \
--cc=sven@kernel.org \
--cc=tglx@kernel.org \
--cc=wim@linux-watchdog.org \
--cc=x86@kernel.org \
--cc=zohar@linux.ibm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.