All of lore.kernel.org
 help / color / mirror / Atom feed
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 13/14] hwmon: add support for x86 macs in macsmc-hwmon
Date: Thu, 29 Oct 2026 09:20:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-189027@kernel.org> (raw)
In-Reply-To: <20261029085504.3547-14-atharvatiwarilinuxdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] hwmon: macsmc: NULL pointer dereference in DT sensor label handling
- [High] hwmon: macsmc: macsmc_hwmon_create_fan_acpi() treats positive read return as error
- [High] hwmon: macsmc: unchecked fan setup in macsmc_hwmon_populate_sensors_acpi()
- [High] hwmon: macsmc: endianness bug in macsmc_hwmon_write_old_fan_manual()
- [Medium] hwmon: macsmc: macsmc_hwmon_write_fan() swallows target write errors with > 0
--

commit 4bcde7b8b0bda2dbb66c2bfcaa1b0806af093c48
Author: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>

hwmon: add support for x86 macs in macsmc-hwmon

This commit adds support for Intel Macs to macsmc-hwmon, exposing readable
sensor names for temperature, power, current, and voltage keys, as well as
fan speed reporting and manual control on ACPI platforms.

> diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c
> index 2e74dcd6651b3..29e43ff4b4feb 100644
> --- a/drivers/hwmon/macsmc-hwmon.c
> +++ b/drivers/hwmon/macsmc-hwmon.c
[ ... ]
> @@ -47,7 +52,7 @@ MODULE_PARM_DESC(fan_control,
>  struct macsmc_hwmon_sensor {
>  	struct apple_smc_key_info info;
>  	smc_key macsmc_key;
> -	char label[MAX_LABEL_LENGTH];
> +	char *label;
>  	u32 attrs;
>  };

[Severity: High]
Will changing sensor->label from an inline char array to a pointer cause a
null pointer dereference on Device Tree platforms during
macsmc_hwmon_probe() -> macsmc_hwmon_populate_sensors()?

In macsmc_hwmon_populate_sensors(), the sensor arrays are allocated with
devm_kcalloc(), leaving sensor->label initialized to NULL. When
macsmc_hwmon_create_sensor() runs for a Device Tree node with a label
property, it still writes directly to sensor->label using sizeof(char *):

drivers/hwmon/macsmc-hwmon.c:macsmc_hwmon_create_sensor() {
	...
	ret = of_property_read_string(sensor_node, "label", &label);
	if (ret)
		dev_dbg(dev, "No label found for sensor %s\n", key);
	else
		strscpy_pad(sensor->label, label, sizeof(sensor->label));
}

And if a Device Tree sensor node does not have a label property,
sensor->label remains NULL when macsmc_hwmon_populate_sensors() immediately
dereferences it across the current, power, temperature, and voltage loops:

drivers/hwmon/macsmc-hwmon.c:macsmc_hwmon_populate_sensors() {
	...
	if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
		sensor->attrs = HWMON_C_INPUT;

		if (*sensor->label)
			sensor->attrs |= HWMON_C_LABEL;
	...
}

[ ... ]
> @@ -599,6 +605,23 @@ static int macsmc_hwmon_read_fan(struct macsmc_hwmon *hwmon, u32 attr, int chan,
>  	}
>  }
>  
> +static int macsmc_hwmon_write_old_fan_manual(struct macsmc_hwmon *hwmon, u8 val, int channel)
> +{
> +	int ret;
> +	u16 buf;
> +
> +	ret = apple_smc_read_u16(hwmon->smc, __SMC_KEY('F', 'S', '!', ' '), &buf);
> +	if (ret)
> +		return ret;
> +
> +	if (val)
> +		buf |= BIT(channel);
> +	else
> +		buf &= ~BIT(channel);
> +
> +	return apple_smc_write_u16(hwmon->smc, __SMC_KEY('F', 'S', '!', ' '), cpu_to_be16(buf));
> +}

[Severity: High]
Should buf be converted with be16_to_cpu() after apple_smc_read_u16() in
macsmc_hwmon_write_old_fan_manual()?

Because apple_smc_read_u16() reads raw big-endian bytes into buf without
byte-swapping, modifying BIT(channel) on the unswapped value and then
passing cpu_to_be16(buf) to apple_smc_write_u16() swaps the high and low
bytes of the manual fan bitmask on little-endian systems. For example, once
fan 0 is placed in manual mode (0x0001 big-endian, read into buf as 0x0100),
clearing BIT(0) on buf is a no-op and writes 0x0001 big-endian back to the
SMC, leaving fan 0 stuck in manual mode.

>  static int macsmc_hwmon_write_fan(struct device *dev, u32 attr, int channel,
>  				  long val)
>  {
[ ... ]
> @@ -625,7 +649,10 @@ static int macsmc_hwmon_write_fan(struct device *dev, u32 attr, int channel,
>  	if (val >= min && val <= max) {
>  		if (!hwmon->fan.fans[channel].manual) {
>  			/* Write 1 to mode key for manual control */
> -			ret = macsmc_hwmon_write_key(hwmon->smc,
> +			if (hwmon->fan.has_old_manual)
> +				ret = macsmc_hwmon_write_old_fan_manual(hwmon, 1, channel);
> +			else
> +				ret = macsmc_hwmon_write_key(hwmon->smc,
>  						     &hwmon->fan.fans[channel].mode, 1);
>  			if (ret < 0)
>  				return ret;
>  
>  			hwmon->fan.fans[channel].manual = true;
>  		}
>  		return macsmc_hwmon_write_key(hwmon->smc,
> -					      &hwmon->fan.fans[channel].set, val);
> +					      &hwmon->fan.fans[channel].set, val) > 0;

[Severity: Medium]
Why is the return value of macsmc_hwmon_write_key() compared with > 0 here
in macsmc_hwmon_write_fan()?

The relational comparison turns any negative error code returned by
macsmc_hwmon_write_key() into 0, causing hwmon_attr_store() to treat failed
writes to the fan target key as successful.

[ ... ]
> @@ -871,6 +1475,57 @@ static int macsmc_hwmon_create_fan(struct device *dev, struct apple_smc *smc,
>  	return 0;
>  }
>  
> +static int macsmc_hwmon_create_fan_acpi(struct apple_smc *smc,
> +					struct macsmc_hwmon_fan *fan, int num)
> +{
> +	const char num_char = num + '0';
> +	int ret;
> +
> +	ret = macsmc_hwmon_create_sensor_acpi(smc, &fan->now,
> +			__SMC_KEY('F', num_char, 'A', 'c'), NULL, 0);
> +	if (ret)
> +		return ret;
> +
> +	if (apple_smc_key_exists(smc, __SMC_KEY('F', num_char, 'I', 'D'))) {
> +		char buf[17];
> +
> +		ret = apple_smc_read(smc, __SMC_KEY('F', num_char, 'I', 'D'), buf, 16);
> +		if (ret)
> +			return ret;

[Severity: High]
Does checking if (ret) after apple_smc_read() cause
macsmc_hwmon_create_fan_acpi() to abort on success whenever the F%dID key is
present?

On ACPI platforms, apple_smc_read() delegates to apple_smc_acpi_read(),
which returns the positive number of bytes read (16) on success:

drivers/platform/x86/macsmc-acpi.c:apple_smc_acpi_read() {
	...
	if (ret < 0)
		return ret;

	return size;
}

When F%dID exists and is read, ret is 16, so macsmc_hwmon_create_fan_acpi()
returns 16 early before initializing fan->label, the min/max/target/mode
sensors, and fan->attrs.

[ ... ]
> @@ -990,6 +1645,101 @@ static int macsmc_hwmon_populate_sensors(struct macsmc_hwmon *hwmon,
[ ... ]
> +static int macsmc_hwmon_populate_sensors_acpi(struct macsmc_hwmon *hwmon)
> +{
> +	int ret, i;
> +
> +	ret = apple_smc_read_u8(hwmon->smc, SMC_KEY(FNum), &hwmon->fan.count);
> +	if (hwmon->fan.count && !ret) {
> +		hwmon->fan.fans = devm_kcalloc(hwmon->dev, hwmon->fan.count,
> +				sizeof(struct macsmc_hwmon_fan), GFP_KERNEL);
> +		if (!hwmon->fan.fans)
> +			return -ENOMEM;
> +
> +		hwmon->fan.has_old_manual = apple_smc_key_exists(hwmon->smc,
> +				__SMC_KEY('F', 'S', '!', ' '));
> +
> +		for (i = 0; i < hwmon->fan.count; i++) {
> +			macsmc_hwmon_create_fan_acpi(hwmon->smc, &hwmon->fan.fans[i], i);
> +			if (ret)
> +				return ret;
> +		}
> +	}

[Severity: High]
Was ret meant to capture the return value of macsmc_hwmon_create_fan_acpi()
in macsmc_hwmon_populate_sensors_acpi()?

Because ret is not assigned when calling macsmc_hwmon_create_fan_acpi(),
if (ret) always checks the stale 0 from apple_smc_read_u8(), ignoring fan
initialization failures and leaving hwmon->fan.fans[i].attrs as 0.

Also, if apple_smc_read_u8() fails after modifying hwmon->fan.count,
hwmon->fan.count can remain non-zero while hwmon->fan.fans is still NULL,
which would later dereference NULL in macsmc_hwmon_create_infos() ->
macsmc_hwmon_populate_fan_configs().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261029085504.3547-1-atharvatiwarilinuxdev@gmail.com?part=13

  reply	other threads:[~2026-10-29  9:20 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
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 [this message]
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-189027@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.