From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796395731; b=slVet1kF/2I5zbrtwO8rTDIyn8hmtHiXpjjBlUxTkl/DsS1FourXANsYwFlJS9Eo/v12 exyNZ7rPlxgSCtEzKnrQ786bKnNSlUqscbvcFiGdI0YEFr3Qo/dj8W6TXUcxXUqv1oaGr wG73+QgjgC44ga5fPEySAPJCa/u7niLfYoprX0xlcZhEFZtZZwaCDDSFyCRXFcUBxXNNt 0RBCJG/M8QVoUeN0BtheL/3rXmTyKg2turqNc4251KaD2F9AsddHQ7ueefsbHnxuIZ5wq aSnKEHxxESapJztoud8msc/BKBAWaTghutnjwUPEKqMoNvIpmgjvO2gTTjRtczqHH4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796395731; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=P45RPifaTZPHinPUf4AW6j6t2I5UebcW1W9Jehatdr4=; b=fq+fSXucz6ory7bgGwjBQON3KK0u0pXTPPoGAPjuugV//lWCngqSwc+XJ/Gkg4CjWQL/ NteeP3EORXx5CmO8H/svZ8IzSgta6bb8Q7VMS9NwN0KOVLPQj8xI/f5hvkNGFq3wLuAAu dszSDvM4m5bsPXV1R3MrmtFuXf3h5OV1CtQeJGkkgmMV+wRF6UYaychii3+WZUfmdtvHo hEs0MRKYYD4oGFNMomNKTlF+Oo4f5mL0oLghzEhkAYZIGfhXu7EdTBr+fQ4PcEjh+7TyW Iy8rDhcnJ/WcbBu/VDssSKtJUV++CBuB+volyZY1Kz18IZIJMNrzxHjyzRApdz1Qo1A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k2Xf3DSS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 737FF606A322; Fri, 4 Dec 2026 14:48:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k2Xf3DSS Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1AAF01F000C2; Fri, 4 Dec 2026 14:48:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796395731; bh=P45RPifaTZPHinPUf4AW6j6t2I5UebcW1W9Jehatdr4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=k2Xf3DSSyJoKMmzxuxHDbHG9iMSECVBoUg3pDIet9BMaw9MV72qVFm9SaWrDkBw2D UZChlXE8SQqdRujHckf5PnTUKnjHnUWwxNfZaMVVthsdqM0IHzZhVqk7r/2O2EOUFG DjOsv57s1ODAA7y939hhigM/aqS3rIXWzk/OYOq3UdAqe1/3c6/b3QydIAVEKqWIym yM/Rt5HiH4nWAtyka7+Bbnxki/i9kFPBhkXrIbp+TUozuaJO2BP6bIJer3tl7jHEVg c03XRbq0jykIZdvx+CBkg5UBa6B6mxk/3i/psXoMWcg0v+VhnwGoXuwAX6x5jdttgg O4eo4ELbHqy0A== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Reply-To: sashiko-reviews@lists.linux.dev To: "Atharva Tiwari" Cc: "Ihor Odainyi" , "Antoine Sidem" , "Aditya Garg" , "Bjorn Helgaas" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , "Len Brown" , "Linus Walleij" , "Bartosz Golaszewski" , "Sven Peter" , "Janne Grunau" , "Neal Gompa" , "Guenter Roeck" , "Jonathan Cameron" , "David Lechner" , =?utf-8?b?TnVubyBTw6E=?= , "Andy Shevchenko" , "Lee Jones" , "Pavel Machek" , "Griffin Kroah-Hartman" , "Hans de Goede" , =?utf-8?b?SWxwbyBKw6RydmluZW4=?= , "Sebastian Reichel" , "Alexandre Belloni" , "Wim Van Sebroeck" , "Jarkko Sakkinen" , "Mimi Zohar" , "Roberto Sassu" , "Dmitry Kasatkin" , "Eric Snowberg" , "Paul Moore" , "James Morris" , "Serge E. Hallyn" , 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 In-Reply-To: <20261204131619.5059-15-atharvatiwarilinuxdev@gmail.com> References: <20261204131619.5059-1-atharvatiwarilinuxdev@gmail.com> <20261204131619.5059-15-atharvatiwarilinuxdev@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 14:48:48 +0000 X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 17 potential issues to consider: - [High] mfd: macsmc: use-after-free in apple_smc_fops_ioctl() after device unbind - [Medium] mfd: macsmc: IOCTL_SMC_WRITE ignores FMODE_WRITE on /dev/smc fd - [Medium] mfd: macsmc: missing APPLE_SMC_WRITABLE check and late allowlist validation - [Medium] mfd: macsmc: raw /dev/smc ioctl UAPI in MFD core with undocumented allowlist - [Medium] mfd: macsmc: commit message mischaracterizes 6-key write allowlist - [Medium] mfd: macsmc: Missing index bounds check in IOCTL_SMC_GET_KEY_INDEX - [Medium] mfd: macsmc: apple_smc_probe() omits OSWD and NATi for macsmc-wdt - [Medium] mfd: macsmc: kernel-only BIT() macro used in UAPI header - [Medium] mfd: macsmc: UAPI ioctl structs have implicit padding and 32-byte key cap - [Medium] mfd: macsmc: unconditional /dev/smc registration and unfiltered key reads - [Low] mfd: macsmc: return -ENOTTY for unknown cmd in apple_smc_fops_ioctl() - [Low] mfd: macsmc: implicit padding holes in new UAPI ioctl structs - [Low] mfd: macsmc: commit subject violates MFD prefix and capitalization rules - [Low] mfd: macsmc: invalid SPDX license expression in UAPI header - [Low] mfd: macsmc: apple_smc_fops_ioctl() returns -EINVAL for unknown commands - [Low] mfd: macsmc: ioctl magic 'S' conflicts and lacks ioctl-number.rst entry - [Low] mfd: macsmc: commit message misdescribes IOCTL_SMC_WRITE key allowlist -- commit 4104c5546b08cdfee61eacef164c47b7d69ce901 Author: Atharva Tiwari mfd/macsmc: add ioctl to access smc keys This commit registers a /dev/smc miscdevice in macsmc-core and adds a UAPI header exposing ioctls to read keys, write a fixed set of keys, enumerate keys by index, and query key metadata. > mfd/macsmc: add ioctl to access smc keys [Severity: Low] This isn't a bug, but MFD commit subjects use the format "mfd: : ", e.g. "mfd: macsmc: Add ioctl to access SMC keys". > Keys already managed by kernel drivers can't be written this > way, to avoid conflicts. [Severity: Medium] Does this accurately describe the write restriction? The changelog describes a denylist of driver-managed keys, whereas apple_smc_fops_ioctl() enforces a strict hardcoded allowlist of six keys (HE0N, HE2N, HI0N, MSTS, MSPC, QENA) and rejects every other key with -EPERM. > diff --git a/drivers/mfd/macsmc-core.c b/drivers/mfd/macsmc-core.c > index bc4200f574bd5..a53df6f6cd3f2 100644 > --- a/drivers/mfd/macsmc-core.c > +++ b/drivers/mfd/macsmc-core.c [ ... ] > @@ -112,6 +115,129 @@ static void apple_smc_disable_notifications(void *d= ata) > smc->be->disable_notifications(smc->be_cookie); > } > =20 > +static long apple_smc_fops_ioctl(struct file *file, unsigned int cmd, un= signed long arg) > +{ [ ... ] > + case IOCTL_SMC_WRITE: { > + struct ioctl_smc_rw_payload payload; > + struct apple_smc_key_info info; > + > + if (copy_from_user(&payload, argp, sizeof(payload))) > + return -EFAULT; [Severity: Medium] Can an O_RDONLY file descriptor write SMC keys here? vfs_ioctl() does not enforce FMODE_WRITE on behalf of drivers, so IOCTL_SMC_WRITE should reject callers whose file->f_mode lacks FMODE_WRITE. > + ret =3D apple_smc_get_key_info(smc, payload.key, &info); > + if (ret < 0) > + return ret; > + > + if (payload.size !=3D info.size) > + return -EINVAL; > + > + if (payload.size > 32) > + return -EINVAL; > + > + switch (payload.key) { > + case __SMC_KEY('H', 'E', '0', 'N'): > + case __SMC_KEY('H', 'E', '2', 'N'): > + case __SMC_KEY('H', 'I', '0', 'N'): > + case __SMC_KEY('M', 'S', 'T', 'S'): > + case __SMC_KEY('M', 'S', 'P', 'C'): > + case __SMC_KEY('Q', 'E', 'N', 'A'): > + break; > + default: > + return -EPERM; > + } [Severity: Medium] Should IOCTL_SMC_WRITE check (info.flags & APPLE_SMC_WRITABLE) before writing, and validate payload.size and the key allowlist before calling apple_smc_get_key_info()? Unlike IOCTL_SMC_READ, IOCTL_SMC_WRITE omits the writability flag check; on the ACPI PIO backend, apple_smc_acpi_port_write() does not read back a firmware error status, so a write to a read-only key can silently succeed. Running apple_smc_get_key_info() before the static size and allowlist checks also acquires smc->mutex and performs hardware IPC for disallowed or oversized requests (and on ACPI PIO, a nonexistent key stalls for 1000 ms and clears smc->interrupt_enabled). [Severity: Low] Neither the commit message nor comments here explain what these six allowlisted keys control or why userspace needs write access to them; the changelog only mentions excluding driver-managed keys. [ ... ] > + case IOCTL_SMC_GET_KEY_INDEX: { > + struct ioctl_smc_index_payload payload; > + > + if (copy_from_user(&payload, argp, sizeof(payload))) > + return -EFAULT; > + > + ret =3D apple_smc_get_key_by_index(smc, payload.index, &payload.key); [Severity: Medium] Does IOCTL_SMC_GET_KEY_INDEX need to check payload.index >=3D smc->key_count before calling apple_smc_get_key_by_index()? Because the UAPI documents sequential enumeration with increasing index values, normal callers will pass payload.index =3D=3D smc->key_count to find the end of the table. On ACPI PIO Intel Macs, an out-of-range query stalls apple_smc_acpi_port_wait() for up to 2 seconds while holding smc->mutex and permanently clears smc->interrupt_enabled =3D 0, reverting subsequent SMC PIO transactions to polling. [ ... ] > + default: > + return -EINVAL; > + } > +} [Severity: Low] Should unrecognized ioctl commands return -ENOTTY instead of -EINVAL? Returning -EINVAL breaks standard userspace ioctl probing such as isatty() / TCGETS on /dev/smc. [Severity: Low] Per Documentation/driver-api/ioctl.rst, unlocked_ioctl handlers should return -ENOTTY (or -ENOIOCTLCMD) for unknown command numbers so userspace can distinguish unsupported ioctls from invalid payload arguments (-EINVAL). [ ... ] > @@ -150,6 +276,20 @@ int apple_smc_probe(struct device *dev, const struct= apple_smc_backend_ops *ops, > if (ret) > return ret; > =20 > + smc->miscdev.minor =3D MISC_DYNAMIC_MINOR; > + smc->miscdev.name =3D "smc"; > + smc->miscdev.fops =3D &apple_smc_fops; > + smc->miscdev.parent =3D dev; > + smc->miscdev.mode =3D 0600; > + > + ret =3D misc_register(&smc->miscdev); > + if (ret) > + return ret; > + > + ret =3D devm_add_action_or_reset(dev, apple_smc_misc_unregister, &smc->= miscdev); > + if (ret) > + return ret; > + > if (smc->is_acpi) { > ret =3D devm_mfd_add_devices(dev, -1, apple_smc_acpi_common, > ARRAY_SIZE(apple_smc_acpi_common), NULL, 0, NULL); [Severity: High] Can apple_smc_fops_ioctl() hit a use-after-free when the SMC device is unbound or a backend module is unloaded while /dev/smc is open? struct apple_smc and its backend context are devm-allocated, apple_smc_fops_open() caches a raw pointer to smc in file->private_data with no reference counting or revocation lock, and apple_smc_fops.owner pins only macsmc-core rather than the loadable macsmc-acpi or macsmc-rtkit backend modules. misc_deregister() does not revoke open file descriptors or wait for in-flight ioctls: CPU0 CPU1 sysfs unbind / rmmod macsmc-acpi open("/dev/smc") devres_release_all() file->private_data =3D smc misc_deregister() kfree(smc) & backend teardown ioctl(fd, IOCTL_SMC_READ, ...) apple_smc_fops_ioctl() mutex_lock(&smc->mutex) <-- UAF smc->be->read(...) The same race also exists if devm_mfd_add_devices() fails after misc_register() exposes /dev/smc. Registering the miscdevice after MFD child registration, refcounting the open-file context independently of devm, and synchronizing deregistration with a lock (such as an rw_semaphore) before tearing down backend resources would prevent this. [Severity: Medium] As reported by Jonathan Cameron on RFC [1], should a raw firmware pass-through miscdevice and custom ioctl UAPI live in drivers/mfd/? Core MFD drivers normally handle only shared resources and child registration, and exposing a permanent opaque ioctl interface for six undocumented keys bypasses standard kernel subsystems (or fwctl) without explaining what those keys do or why standard interfaces cannot be used. [Severity: Medium] As reported by Sasha Finkelstein on RFC [2], why is /dev/smc registered unconditionally here instead of being restricted to T2 Macs, and why does IOCTL_SMC_READ allow reading arbitrary readable keys? Because misc_register() runs before checking smc->is_acpi or has_apple_t2_chip (added in commit "treewide: Add a flag to detect the Apple T2 chip"), /dev/smc is exposed on Apple Silicon RTKit and non-T2 Intel Macs as well. On the read path, IOCTL_SMC_READ only checks APPLE_SMC_READABLE and size <=3D 32, without a read allowlist or filtering read-sensitive keys such as APPLE_SMC_FUNCTION or APPLE_SMC_PRIVATE_READ. [Severity: Medium] In the smc->is_acpi branch right below, apple_smc_probe() gates apple_smc_acpi_wdt registration solely on SMC_KEY(NATJ). macsmc_wdt_probe() in drivers/watchdog/macsmc_wdt.c supports newer SMCs via SMC_KEY(OSWD) and otherwise requires both SMC_KEY(NATJ) and SMC_KEY(NATi). Checking only NATJ leaves the watchdog unregistered on OSWD-only Macs and registers it even when NATi is absent. > diff --git a/include/uapi/linux/macsmc.h b/include/uapi/linux/macsmc.h > new file mode 100644 > index 0000000000000..ef784539d3252 > --- /dev/null > +++ b/include/uapi/linux/macsmc.h > @@ -0,0 +1,119 @@ > +/* SPDX-License-Identifier: GPL-2.0-only OR MIT WITH Linux-syscall-note = */ [Severity: Low] Is this SPDX expression valid? WITH binds more tightly than OR, so this attaches Linux-syscall-note to MIT (which scripts/spdxcheck.py rejects) and leaves GPL-2.0-only without the syscall note. It should be "(GPL-2.0-only WITH Linux-syscall-note) OR MIT". [ ... ] > +#define APPLE_SMC_READABLE BIT(7) > +#define APPLE_SMC_WRITABLE BIT(6) > +#define APPLE_SMC_FUNCTION BIT(4) > +#define APPLE_SMC_CONST BIT(3) > +#define APPLE_SMC_ATOMIC BIT(2) > +#define APPLE_SMC_PRIVATE_READ BIT(1) > +#define APPLE_SMC_PRIVATE_WRITE BIT(0) [Severity: Medium] Does this break userspace compilation when any APPLE_SMC_* flag is referenced? BIT() is a kernel-internal macro not provided by UAPI headers; UAPI constants should use _BITUL() (with ) or plain unsigned shifts such as (1U << 7). [ ... ] > +struct ioctl_smc_rw_payload { > + smc_key key; > + __u32 size; > + __u8 buf[32]; > +}; [Severity: Medium] Does hardcoding __u8 buf[32] permanently prevent accessing SMC keys larger than 32 bytes? struct apple_smc_key_info.size is __u8 and the RTKit backend defines SMC_MAX_SIZE as 255, so baking sizeof(struct ioctl_smc_rw_payload) into the ioctl ABI makes larger keys inaccessible without new ioctls. In addition, struct ioctl_smc_index_payload narrows index to __u16 despite u32 smc->key_count (with a 2-byte internal padding hole), and struct apple_smc_key_info leaves 2 bytes of implicit tail padding when embedded in struct ioctl_smc_info_payload. [ ... ] > +struct ioctl_smc_index_payload { > + __u16 index; > + smc_key key; > +}; [Severity: Low] Both struct ioctl_smc_index_payload (between __u16 index and smc_key key) and struct apple_smc_key_info (trailing 2 bytes) contain implicit compiler padding holes. Widening index to __u32 and adding explicit reserved padding fields avoids unnamed holes in the UAPI structs. [ ... ] > +#define APPLE_SMC_MAGIC 'S' > +#define IOCTL_SMC_READ _IOWR(APPLE_SMC_MAGIC, 1, struct ioctl_smc_rw_= payload) > +#define IOCTL_SMC_WRITE _IOW(APPLE_SMC_MAGIC, 2, struct ioctl_smc_rw_p= ayload) > +#define IOCTL_SMC_GET_KEY_INDEX _IOWR(APPLE_SMC_MAGIC, 3, struct ioctl_= smc_index_payload) > +#define IOCTL_SMC_GET_KEY_INFO _IOWR(APPLE_SMC_MAGIC, 4, struct ioctl_= smc_info_payload) [Severity: Low] Does APPLE_SMC_MAGIC 'S' conflict with existing ioctl numbers? Documentation/userspace-api/ioctl/ioctl-number.rst already lists 'S' as conflicted across cdrom.h, asequencer.h, scsi_ioctl.h, and scsi.h, and this new range is not registered there. [1] https://lore.kernel.org/r/20261101013817.22d67ea7@jic23-hlaptop [2] https://lore.kernel.org/r/525E5B4D-A947-4567-9FCD-5FFF2CA8B161@chaosmai= l.tech --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204131619.5059= -1-atharvatiwarilinuxdev@gmail.com?part=3D14