From cfeb91726aa1fca20529c44b24c668eee7ca0cd6 Mon Sep 17 00:00:00 2001 From: Mark Godwin Date: Mon, 16 Nov 2026 15:53:09 -0700 Subject: [PATCH 1/2] Keep /run/t2-touchid root-private on Linux-native installs `install -d -m 0700 /run/t2-touchid/workers` applies the mode only to the leaf, so a fresh Linux-native install created the parent as 0755. The only other creator of the parent with 0700 is t2-keybag-load.sh, which is disabled in Linux-native mode, and fprintd.service's RuntimeDirectory=t2-touchid/workers also creates the parent as 0755 at boot. t2-native-first-run then stopped with "replacement runtime directory is not root-private". Create the parent explicitly as 0700 in the installer and add a tmpfiles.d entry so it is root-private before fprintd starts on every boot. Observed on MacBookPro15,1, linux-t2 7.3.4, fresh Linux-only install. Co-Authored-By: Claude Opus 5.5 --- install.sh | 3 +++ systemd/tmpfiles.d/t2-touchid.conf | 3 +++ 2 files changed, 6 insertions(+) diff --git a/install.sh b/install.sh index 7378551..a319cfb 100755 --- a/install.sh +++ b/install.sh @@ -410,6 +410,9 @@ install -d -o root -g root -m 0700 \ /var/lib/t2-touchid /var/lib/t2-touchid/users \ /var/lib/t2-touchid/mutations /var/lib/t2-touchid/recovery-anchors \ /var/lib/t2-touchid/external-reconciliation-backups +# install -d applies -m only to the leaf; the parent must be root-private too. +install -d -o root -g root -m 0700 /run/t2-touchid +chmod 0700 /run/t2-touchid install -d -o root -g root -m 0700 /run/t2-touchid/workers install -o root -g root -m 0755 "$source_dir/src/"*.py "$target_dir/src/" install -o root -g root -m 0755 "$source_dir/src/t2_touchid_cli.py" /usr/local/bin/t2touch diff --git a/systemd/tmpfiles.d/t2-touchid.conf b/systemd/tmpfiles.d/t2-touchid.conf index 33fe498..e46de92 100644 --- a/systemd/tmpfiles.d/t2-touchid.conf +++ b/systemd/tmpfiles.d/t2-touchid.conf @@ -2,3 +2,6 @@ # Create the activation journal directory. Age deletion is not used: the # Python pruner removes only proved-terminal histories. d /var/lib/t2-touchid/activation 0700 root root - +# Root-private runtime state. Create before fprintd's RuntimeDirectory= would +# create the parent with the default 0755 mode. +d /run/t2-touchid 0700 root root - From ec22e05a56fd383cfabc92e5967478b8627096bb Mon Sep 17 00:00:00 2001 From: Mark Godwin Date: Mon, 16 Nov 2026 15:53:18 -0700 Subject: [PATCH 2/2] Publish the T2 SEP boot state through macsmc on Linux 7.3+ Linux 7.3 removed struct acpi_driver, and T2 kernels now bind the SMC (APP0001) to the macsmc MFD stack (macsmc-acpi, -hwmon, -power, -rtc, -wdt). The bundled applesmc-t2touch prerequisite no longer compiles, and even a ported applesmc would contend with macsmc for the same device. The macsmc core exposes /dev/smc with key read/write/info ioctls, so the one-shot EFMV/EFMS/EFBS boot-state transaction can run from userspace: - src/t2-smc-boot-state.c performs the same transaction as the applesmc patch (pristine EFMV check, bridgeOS version from DMI, EFMS clear, EFBS commit, 40 s EFMS poll) and records the result in the existing vocabulary at /run/t2touch/t2_sep_boot_state. --probe is read-only. Runs at most once per boot; commit-uncertain is never retried. - t2-smc-boot-state.service runs it sandboxed to /dev/smc, ordered before t2-sep-transport.service. udev starts it when /dev/smc appears, because misc devices are not visible to systemd and a device dependency would time out (and delay boot on applesmc kernels). - The installer detects macsmc (character /dev/smc plus macsmc-acpi bound to APP0001), installs the helper, unit and udev rule instead of staging applesmc DKMS, skips the applesmc modprobe options/softdep, and reads the userspace result alongside the sysfs attribute. applesmc kernels are unchanged. uninstall.sh removes the new files. - Tests stub macsmc detection so they stay hermetic on macsmc hosts, and cover the macsmc gate, detection and result reading. Verified on MacBookPro15,1 (J680, bridgeOS 23.16.15067.0.0,0), linux-t2 7.3.4, Linux-only: response-received:1, first-run identity created, finger enrolled, sudo authenticated by Touch ID. Co-Authored-By: Claude Opus 5.5 --- install.sh | 21 +- src/Makefile | 7 +- src/t2-smc-boot-state.c | 422 +++++++++++++++++++++++ systemd/system/t2-smc-boot-state.service | 45 +++ tests/test_installer_kernel.py | 63 +++- tools/installer-kernel.sh | 41 ++- udev/90-t2-smc-boot-state.rules | 4 + uninstall.sh | 4 + 8 files changed, 593 insertions(+), 14 deletions(-) create mode 100644 src/t2-smc-boot-state.c create mode 100644 systemd/system/t2-smc-boot-state.service create mode 100644 udev/90-t2-smc-boot-state.rules diff --git a/install.sh b/install.sh index a319cfb..4ea678b 100755 --- a/install.sh +++ b/install.sh @@ -599,12 +599,21 @@ if [[ $identity_replacement == 1 ]]; then fi printf '%s\n' "$module_options" >/etc/modprobe.d/t2-sep-transport.conf chmod 0644 /etc/modprobe.d/t2-sep-transport.conf -{ - printf 'options applesmc t2_sep_boot_state=1 t2_sep_epoch_major=%s t2_sep_epoch_minor=%s\n' \ - "$sep_epoch_major" "$sep_epoch_minor" - printf 'softdep t2_sep_transport pre: applesmc\n' -} >/etc/modprobe.d/t2-sep-boot-state.conf -chmod 0644 /etc/modprobe.d/t2-sep-boot-state.conf +if macsmc_active; then + # macsmc kernels publish boot state via t2-smc-boot-state.service, which + # reads the epoch from /etc/t2-touchid.conf. + rm -f -- /etc/modprobe.d/t2-sep-boot-state.conf + install -o root -g root -m 0644 \ + "$source_dir/systemd/system/t2-smc-boot-state.service" \ + /etc/systemd/system/t2-smc-boot-state.service +else + { + printf 'options applesmc t2_sep_boot_state=1 t2_sep_epoch_major=%s t2_sep_epoch_minor=%s\n' \ + "$sep_epoch_major" "$sep_epoch_minor" + printf 'softdep t2_sep_transport pre: applesmc\n' + } >/etc/modprobe.d/t2-sep-boot-state.conf + chmod 0644 /etc/modprobe.d/t2-sep-boot-state.conf +fi install -d -o "$target_user" -g "$target_gid" -m 0755 "$target_home/.config/systemd/user" install -o "$target_user" -g "$target_gid" -m 0644 \ diff --git a/src/Makefile b/src/Makefile index 0f87bf0..537eba3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,7 +8,7 @@ LDHARDEN := -pie -Wl,-z,relro,-z,now,-z,noexecstack .PHONY: all module recovery-tools clean -all: module t2-aks-tool t2-pam-fingerprint-prompt pam_t2touch_action_prompt.so t2-recovery-usb-observer +all: module t2-aks-tool t2-pam-fingerprint-prompt pam_t2touch_action_prompt.so t2-recovery-usb-observer t2-smc-boot-state module: $(MAKE) -C $(KDIR) M=$(CURDIR) modules @@ -25,6 +25,9 @@ pam_t2touch_action_prompt.so: pam_t2touch_action_prompt.c t2-recovery-usb-observer: t2-recovery-usb-observer.c $(CC) -O2 -Wall -Wextra -Werror $(HARDEN) $(LDHARDEN) -o t2-recovery-usb-observer t2-recovery-usb-observer.c +t2-smc-boot-state: t2-smc-boot-state.c + $(CC) -O2 -Wall -Wextra -Werror $(HARDEN) $(LDHARDEN) -o t2-smc-boot-state t2-smc-boot-state.c + clean: $(MAKE) -C $(KDIR) M=$(CURDIR) clean - $(RM) t2-aks-tool t2-pam-fingerprint-prompt pam_t2touch_action_prompt.so t2-recovery-usb-observer + $(RM) t2-aks-tool t2-pam-fingerprint-prompt pam_t2touch_action_prompt.so t2-recovery-usb-observer t2-smc-boot-state diff --git a/src/t2-smc-boot-state.c b/src/t2-smc-boot-state.c new file mode 100644 index 0000000..ce2853b --- /dev/null +++ b/src/t2-smc-boot-state.c @@ -0,0 +1,422 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Publish the typed T2 SEP versioned-app boot state through the macsmc + * character device (/dev/smc). + * + * Linux 7.3 replaced applesmc with the macsmc MFD stack on T2 Macs, so the + * applesmc boot-state publisher (linux_native/patches/ + * applesmc-t2-sep-boot-state.patch) cannot load there. This helper performs + * the same one-shot EFMV/EFMS/EFBS transaction from userspace and records the + * result in the same vocabulary the applesmc sysfs attribute used. + * + * t2-smc-boot-state --probe read-only: report key info and EFMV state + * t2-smc-boot-state --publish perform the one-shot transaction + * + * The transaction must run at most once per boot. A commit-uncertain result + * must not be retried before the next reboot. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Mirror of include/linux/mfd/macsmc.h (not exported as UAPI). */ +typedef uint32_t smc_key; + +struct apple_smc_key_info { + uint32_t type_code; + uint8_t size; + uint8_t flags; +}; + +struct ioctl_smc_rw_payload { + smc_key key; + size_t size; + uint8_t buf[32]; +}; + +struct ioctl_smc_info_payload { + smc_key key; + struct apple_smc_key_info info; +}; + +#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_payload) +#define IOCTL_SMC_GET_KEY_INFO _IOWR(APPLE_SMC_MAGIC, 4, struct ioctl_smc_info_payload) + +#define APPLE_SMC_READABLE 0x80 +#define APPLE_SMC_WRITABLE 0x40 + +#define KEY(a, b, c, d) (((uint32_t)(a) << 24) | ((uint32_t)(b) << 16) | \ + ((uint32_t)(c) << 8) | (uint32_t)(d)) +#define KEY_EFMV KEY('E', 'F', 'M', 'V') +#define KEY_EFMS KEY('E', 'F', 'M', 'S') +#define KEY_EFBS KEY('E', 'F', 'B', 'S') + +#define EFMV_LENGTH 32 +#define EFM_STATE_LENGTH 1 +#define EFBS_COMMIT 0x11 +#define EFMS_TIMEOUT_MSECS 40000 +#define EFMS_POLL_MSECS 10 + +#define SMC_DEVICE "/dev/smc" +#define RESULT_DIR "/run/t2touch" +#define RESULT_PATH RESULT_DIR "/t2_sep_boot_state" +#define LOCK_PATH RESULT_DIR "/t2_sep_boot_state.lock" +#define DMI_BIOS "/sys/class/dmi/id/bios_version" + +static int smc_fd = -1; + +static int smc_info(smc_key key, struct apple_smc_key_info *info) +{ + struct ioctl_smc_info_payload p = { .key = key }; + + if (ioctl(smc_fd, IOCTL_SMC_GET_KEY_INFO, &p) < 0) + return -errno; + *info = p.info; + return 0; +} + +static int smc_read(smc_key key, void *buf, size_t size) +{ + struct ioctl_smc_rw_payload p = { .key = key, .size = size }; + + if (size > sizeof(p.buf)) + return -EINVAL; + if (ioctl(smc_fd, IOCTL_SMC_READ, &p) < 0) + return -errno; + memcpy(buf, p.buf, size); + return 0; +} + +static int smc_write(smc_key key, const void *buf, size_t size) +{ + struct ioctl_smc_rw_payload p = { .key = key, .size = size }; + + if (size > sizeof(p.buf)) + return -EINVAL; + memcpy(p.buf, buf, size); + if (ioctl(smc_fd, IOCTL_SMC_WRITE, &p) < 0) + return -errno; + return 0; +} + +/* Same parser as the applesmc patch: "... (iBridge: a.b.c.d.e,f)" */ +static int parse_bridge_version(uint32_t v[6]) +{ + char bios[256] = { 0 }; + const char *ibridge; + char trailing; + FILE *f = fopen(DMI_BIOS, "re"); + + if (!f) + return -ENODEV; + if (!fgets(bios, sizeof(bios), f)) { + fclose(f); + return -ENODEV; + } + fclose(f); + bios[strcspn(bios, "\n")] = '\0'; + + ibridge = strstr(bios, "(iBridge: "); + if (!ibridge) + return -ENODEV; + if (sscanf(ibridge, "(iBridge: %u.%u.%u.%u.%u,%u)%c", + &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &trailing) != 6) + return -EINVAL; + return 0; +} + +static void put_le32(uint8_t *p, uint32_t v) +{ + p[0] = v & 0xff; + p[1] = (v >> 8) & 0xff; + p[2] = (v >> 16) & 0xff; + p[3] = (v >> 24) & 0xff; +} + +static int record(const char *fmt, ...) __attribute__((format(printf, 1, 2))); +static int record(const char *fmt, ...) +{ + char tmp[] = RESULT_DIR "/.t2_sep_boot_state.XXXXXX"; + va_list ap; + FILE *f; + int fd; + + if (mkdir(RESULT_DIR, 0755) < 0 && errno != EEXIST) + return -errno; + fd = mkstemp(tmp); + if (fd < 0) + return -errno; + fchmod(fd, 0644); + f = fdopen(fd, "w"); + if (!f) { + close(fd); + unlink(tmp); + return -errno; + } + va_start(ap, fmt); + vfprintf(f, fmt, ap); + va_end(ap); + fputc('\n', f); + if (fclose(f) != 0 || rename(tmp, RESULT_PATH) < 0) { + unlink(tmp); + return -errno; + } + return 0; +} + +static void dump(const char *label, const uint8_t *b, size_t n) +{ + printf("%s:", label); + for (size_t i = 0; i < n; i++) + printf(" %02x", b[i]); + printf("\n"); +} + +static int efmv_is_pristine(const uint8_t *efmv) +{ + if (efmv[0] != 0xfe) + return 0; + for (size_t i = 1; i < EFMV_LENGTH; i++) + if (efmv[i]) + return 0; + return 1; +} + +static int check_keys(int verbose) +{ + static const struct { smc_key key; const char *name; uint8_t size; } want[] = { + { KEY_EFMV, "EFMV", EFMV_LENGTH }, + { KEY_EFMS, "EFMS", EFM_STATE_LENGTH }, + { KEY_EFBS, "EFBS", EFM_STATE_LENGTH }, + }; + + for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) { + struct apple_smc_key_info info = { 0 }; + int ret = smc_info(want[i].key, &info); + + if (ret) { + fprintf(stderr, "%s: key info failed: %s\n", want[i].name, strerror(-ret)); + return ret; + } + if (verbose) + printf("%s: size=%u flags=0x%02x type=%c%c%c%c\n", want[i].name, + info.size, info.flags, + (info.type_code >> 24) & 0xff, (info.type_code >> 16) & 0xff, + (info.type_code >> 8) & 0xff, info.type_code & 0xff); + if (info.size != want[i].size) { + fprintf(stderr, "%s: unexpected size %u (want %u)\n", + want[i].name, info.size, want[i].size); + return -EPROTO; + } + } + return 0; +} + +static int do_probe(void) +{ + uint8_t efmv[EFMV_LENGTH], efms = 0; + uint32_t v[6]; + int ret; + + ret = parse_bridge_version(v); + if (ret) { + fprintf(stderr, "bridgeOS version from DMI: %s\n", strerror(-ret)); + return 1; + } + printf("bridgeOS version: %u.%u.%u.%u.%u,%u\n", v[0], v[1], v[2], v[3], v[4], v[5]); + + if (check_keys(1)) + return 1; + ret = smc_read(KEY_EFMV, efmv, sizeof(efmv)); + if (ret) { + fprintf(stderr, "EFMV read failed: %s\n", strerror(-ret)); + return 1; + } + dump("EFMV", efmv, sizeof(efmv)); + ret = smc_read(KEY_EFMS, &efms, 1); + if (ret) { + fprintf(stderr, "EFMS read failed: %s\n", strerror(-ret)); + return 1; + } + printf("EFMS: %02x\n", efms); + printf("state: %s\n", efmv_is_pristine(efmv) ? + "pristine (boot state not yet published this boot)" : + "already published this boot"); + return 0; +} + +static int do_publish(unsigned int epoch_major, unsigned int epoch_minor) +{ + uint8_t efmv[EFMV_LENGTH] = { 0 }; + uint8_t efms = 0; + const uint8_t efbs = EFBS_COMMIT; + uint32_t v[6]; + struct timespec start, now, nap = { 0, EFMS_POLL_MSECS * 1000000L }; + int lock_fd, ret; + + if (mkdir(RESULT_DIR, 0755) < 0 && errno != EEXIST) { + perror(RESULT_DIR); + return 1; + } + /* Once per boot: /run is tmpfs, so an existing result means we ran. */ + lock_fd = open(LOCK_PATH, O_RDWR | O_CREAT | O_CLOEXEC, 0600); + if (lock_fd < 0 || flock(lock_fd, LOCK_EX | LOCK_NB) < 0) { + fprintf(stderr, "another publisher is running\n"); + return 1; + } + if (access(RESULT_PATH, F_OK) == 0) { + fprintf(stderr, "boot state already attempted this boot; see %s\n", RESULT_PATH); + return 0; + } + + if (epoch_major > 0xff || epoch_minor > 0xff) { + record("rejected"); + return 1; + } + ret = parse_bridge_version(v); + if (ret) { + record("rejected"); + fprintf(stderr, "bridgeOS version from DMI: %s\n", strerror(-ret)); + return 1; + } + ret = check_keys(0); + if (ret) { + record(ret == -EPROTO ? "rejected" : "failed"); + return 1; + } + ret = smc_read(KEY_EFMV, efmv, sizeof(efmv)); + if (ret) { + record("failed"); + fprintf(stderr, "EFMV read failed: %s\n", strerror(-ret)); + return 1; + } + if (!efmv_is_pristine(efmv)) { + record("rejected"); + fprintf(stderr, "EFMV is not pristine; boot state was already published\n"); + return 1; + } + + memset(efmv, 0, sizeof(efmv)); + efmv[0] = 1; + efmv[1] = epoch_major; + efmv[2] = epoch_minor; + for (int i = 0; i < 6; i++) + put_le32(&efmv[4 + i * 4], v[i]); + + /* From here on, any failure leaves the outcome uncertain. */ + if ((ret = smc_write(KEY_EFMV, efmv, sizeof(efmv))) || + (ret = smc_write(KEY_EFMS, &efms, EFM_STATE_LENGTH)) || + (ret = smc_write(KEY_EFBS, &efbs, EFM_STATE_LENGTH))) + goto uncertain; + + clock_gettime(CLOCK_MONOTONIC, &start); + for (;;) { + ret = smc_read(KEY_EFMS, &efms, EFM_STATE_LENGTH); + if (ret) + goto uncertain; + if (efms) + break; + clock_gettime(CLOCK_MONOTONIC, &now); + if ((now.tv_sec - start.tv_sec) * 1000 + + (now.tv_nsec - start.tv_nsec) / 1000000 >= EFMS_TIMEOUT_MSECS) { + ret = -ETIMEDOUT; + goto uncertain; + } + nanosleep(&nap, NULL); + } + + record("response-received:%u", efms); + printf("received T2 SEP boot-policy response %u for bridgeOS %u.%u.%u.%u.%u,%u epoch %u.%u\n", + efms, v[0], v[1], v[2], v[3], v[4], v[5], epoch_major, epoch_minor); + return 0; + +uncertain: + record("commit-uncertain"); + fprintf(stderr, "T2 SEP boot-state transaction outcome is uncertain (%s); do not retry before reboot\n", + strerror(-ret)); + return 1; +} + +static void usage(FILE *out) +{ + fprintf(out, "Usage: t2-smc-boot-state --probe\n" + " t2-smc-boot-state --publish [--epoch-major N] [--epoch-minor N]\n"); +} + +int main(int argc, char **argv) +{ + static const struct option opts[] = { + { "probe", no_argument, NULL, 'p' }, + { "publish", no_argument, NULL, 'P' }, + { "epoch-major", required_argument, NULL, 'M' }, + { "epoch-minor", required_argument, NULL, 'm' }, + { "help", no_argument, NULL, 'h' }, + { 0 } + }; + unsigned long major = 1, minor = 0; + int mode = 0, c; + char *end; + const char *env; + + /* Defaults match the applesmc modprobe options; /etc/t2-touchid.conf + * (loaded by the unit as EnvironmentFile) may override them. */ + if ((env = getenv("T2_TOUCHID_SEP_EPOCH_MAJOR")) && *env) { + errno = 0; + major = strtoul(env, &end, 0); + if (errno || *end) { fprintf(stderr, "bad T2_TOUCHID_SEP_EPOCH_MAJOR\n"); return 2; } + } + if ((env = getenv("T2_TOUCHID_SEP_EPOCH_MINOR")) && *env) { + errno = 0; + minor = strtoul(env, &end, 0); + if (errno || *end) { fprintf(stderr, "bad T2_TOUCHID_SEP_EPOCH_MINOR\n"); return 2; } + } + + while ((c = getopt_long(argc, argv, "", opts, NULL)) != -1) { + switch (c) { + case 'p': case 'P': + if (mode) { usage(stderr); return 2; } + mode = c; + break; + case 'M': case 'm': + errno = 0; + if (c == 'M') + major = strtoul(optarg, &end, 0); + else + minor = strtoul(optarg, &end, 0); + if (errno || *end || !*optarg) { usage(stderr); return 2; } + break; + case 'h': + usage(stdout); + return 0; + default: + usage(stderr); + return 2; + } + } + if (!mode || optind != argc) { + usage(stderr); + return 2; + } + + smc_fd = open(SMC_DEVICE, O_RDWR | O_CLOEXEC); + if (smc_fd < 0) { + fprintf(stderr, "%s: %s\n", SMC_DEVICE, strerror(errno)); + if (mode == 'P') + record("not-applicable"); + return 1; + } + return mode == 'p' ? do_probe() : do_publish(major, minor); +} diff --git a/systemd/system/t2-smc-boot-state.service b/systemd/system/t2-smc-boot-state.service new file mode 100644 index 0000000..70d5a32 --- /dev/null +++ b/systemd/system/t2-smc-boot-state.service @@ -0,0 +1,45 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Publish the typed T2 SEP boot state through macsmc (/dev/smc) on kernels +# where applesmc is replaced by the macsmc MFD stack (Linux 7.3+). Started +# once per boot by udev (90-t2-smc-boot-state.rules) when /dev/smc appears, +# and ordered before the SEP transport. +[Unit] +Description=Apple T2 SEP boot-state publisher (macsmc) +DefaultDependencies=no +After=dev-smc.device systemd-tmpfiles-setup.service +Before=t2-sep-transport.service t2-biometric-port-refresh.service +ConditionPathExists=/dev/smc +ConditionPathExists=!/run/t2touch/t2_sep_boot_state + +[Service] +Type=oneshot +RemainAfterExit=yes +EnvironmentFile=-/etc/t2-touchid.conf +ExecStart=/usr/local/sbin/t2-smc-boot-state --publish +TimeoutStartSec=60 +DevicePolicy=closed +DeviceAllow=/dev/smc rw +CapabilityBoundingSet= +NoNewPrivileges=true +ProtectSystem=strict +# systemd creates /run/t2touch before namespacing and keeps the result file +# after the oneshot exits; ReadWritePaths= would fail on a fresh /run. +RuntimeDirectory=t2touch +RuntimeDirectoryMode=0755 +RuntimeDirectoryPreserve=yes +ProtectHome=true +PrivateTmp=true +PrivateNetwork=true +ProtectKernelTunables=true +ProtectKernelModules=true +ProtectKernelLogs=true +ProtectControlGroups=true +ProtectClock=true +ProtectHostname=true +RestrictRealtime=true +RestrictNamespaces=true +RestrictAddressFamilies=AF_UNIX +LockPersonality=true +MemoryDenyWriteExecute=true +SystemCallArchitectures=native +SystemCallFilter=@system-service diff --git a/tests/test_installer_kernel.py b/tests/test_installer_kernel.py index d3b9546..a8f7df6 100644 --- a/tests/test_installer_kernel.py +++ b/tests/test_installer_kernel.py @@ -38,7 +38,8 @@ def test_boot_rebuild_selects_limine_and_propagates_failure(self): self.assertEqual(result.returncode, status, result.stderr) self.assertEqual(result.stdout.strip(), expected) - def run_gate(self, result, disk=True, fail_stage=False, active_upgrade=False): + def run_gate(self, result, disk=True, fail_stage=False, active_upgrade=False, + macsmc=False): return subprocess.run( ["bash", "-c", ''' set -euo pipefail @@ -47,15 +48,53 @@ def run_gate(self, result, disk=True, fail_stage=False, active_upgrade=False): modinfo() { [[ $DISK == yes ]] && printf 't2_sep_boot_state: publisher\n'; } stage_applesmc_prerequisite() { echo staged; [[ $FAIL_STAGE == no ]]; } enable_applesmc_next_boot() { echo boot-options; } +macsmc_active() { [[ $MACSMC == yes ]]; } +ensure_macsmc_publisher() { echo macsmc-publisher; [[ $FAIL_STAGE == no ]]; } check_applesmc_prerequisite "$ACTIVE_UPGRADE" || exit $? echo product-ready ''', "fixture", str(HELPER)], env={**os.environ, "RESULT": result, "DISK": "yes" if disk else "no", "FAIL_STAGE": "yes" if fail_stage else "no", - "ACTIVE_UPGRADE": "1" if active_upgrade else "0"}, + "ACTIVE_UPGRADE": "1" if active_upgrade else "0", + "MACSMC": "yes" if macsmc else "no"}, capture_output=True, text=True, check=False, ) + def test_macsmc_absent_publisher_installs_service_then_stops(self): + result = self.run_gate("absent", disk=False, macsmc=True) + self.assertEqual(result.returncode, 3, result.stderr) + lines = result.stdout.splitlines() + self.assertIn("macsmc-publisher", lines) + self.assertNotIn("staged", lines) + self.assertNotIn("boot-options", lines) + self.assertNotIn("product-ready", lines) + + def test_macsmc_response_continues_without_applesmc_staging(self): + result = self.run_gate("response-received:1", disk=False, macsmc=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.splitlines(), ["macsmc-publisher", "product-ready"]) + + def test_macsmc_publisher_install_failure_cannot_report_ready(self): + result = self.run_gate("response-received:1", macsmc=True, fail_stage=True) + self.assertEqual(result.returncode, 2) + self.assertNotIn("product-ready", result.stdout) + + def test_macsmc_detection_requires_device_and_bound_driver(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + def active(): + return subprocess.run( + ["bash", "-c", 'source "$1"; macsmc_active "$2/sys" "$2/dev"', + "fixture", str(HELPER), directory], + capture_output=True, text=True, check=False, + ).returncode == 0 + self.assertFalse(active()) + (root / "sys/bus/platform/drivers/macsmc-acpi/APP0001:00").mkdir(parents=True) + # A regular file is not a character device. + (root / "dev").mkdir() + (root / "dev/smc").write_text("") + self.assertFalse(active()) + def test_successful_live_and_disk_driver_need_no_staging(self): result = self.run_gate("response-received:1") self.assertEqual(result.returncode, 0, result.stderr) @@ -288,7 +327,7 @@ def test_reads_device_result_instead_of_enabled_parameter(self): root / "bus/platform/devices/applesmc.0/t2_sep_boot_state"] def read(): result = subprocess.run( - ["bash", "-c", 'source "$1"; applesmc_boot_result "$2"', + ["bash", "-c", 'source "$1"; applesmc_boot_result "$2" "$2/run"', "fixture", str(HELPER), directory], capture_output=True, text=True, check=True, ) @@ -303,6 +342,24 @@ def read(): paths[1].write_text("response-received:1\n") self.assertEqual(read(), "ambiguous") + def test_reads_macsmc_userspace_publisher_result(self): + with tempfile.TemporaryDirectory() as directory: + result_file = Path(directory) / "run/t2touch/t2_sep_boot_state" + def read(): + return subprocess.run( + ["bash", "-c", 'source "$1"; applesmc_boot_result "$2" "$2/run"', + "fixture", str(HELPER), directory], + capture_output=True, text=True, check=True, + ).stdout.strip() + self.assertEqual(read(), "absent") + result_file.parent.mkdir(parents=True) + result_file.write_text("response-received:1\n") + self.assertEqual(read(), "response-received:1") + sysfs = Path(directory) / "bus/acpi/devices/APP0001:00/t2_sep_boot_state" + sysfs.parent.mkdir(parents=True) + sysfs.write_text("response-received:1\n") + self.assertEqual(read(), "ambiguous") + def test_update_preparation_records_recovery_before_retirement(self): with tempfile.TemporaryDirectory() as directory: uninstall = Path(directory) / "uninstall.sh" diff --git a/tools/installer-kernel.sh b/tools/installer-kernel.sh index c3332e8..0bdb55e 100644 --- a/tools/installer-kernel.sh +++ b/tools/installer-kernel.sh @@ -147,10 +147,11 @@ enable_applesmc_next_boot() { # the ACPI SMC identity; older driver layouts expose a platform device. # shellcheck disable=SC2120 # The optional root argument is for tests. applesmc_boot_result() { - local sys_root=${1:-/sys} path + local sys_root=${1:-/sys} run_root=${2:-/run} path local -a results=() for path in "$sys_root"/bus/acpi/devices/APP0001:*/t2_sep_boot_state \ - "$sys_root"/bus/platform/devices/applesmc*/t2_sep_boot_state; do + "$sys_root"/bus/platform/devices/applesmc*/t2_sep_boot_state \ + "$run_root"/t2touch/t2_sep_boot_state; do [[ -e $path ]] || continue [[ -r $path ]] || { printf '%s\n' unavailable; return; } local value @@ -166,7 +167,41 @@ applesmc_boot_result() { fi } +# Linux 7.3+ drives the T2 SMC through the macsmc MFD stack (macsmc-acpi bound +# to APP0001, /dev/smc). applesmc cannot bind there, so the boot state is +# published from userspace by t2-smc-boot-state.service instead. +# shellcheck disable=SC2120 # The optional root arguments are for tests. +macsmc_active() { + local sys_root=${1:-/sys} dev_root=${2:-/dev} + [[ -c $dev_root/smc ]] && + compgen -G "$sys_root/bus/platform/drivers/macsmc-acpi/APP0001:*" >/dev/null +} + +ensure_macsmc_publisher() { + install -o root -g root -m 0755 "$source_dir/src/t2-smc-boot-state" \ + /usr/local/sbin/t2-smc-boot-state || return 1 + install -o root -g root -m 0644 \ + "$source_dir/systemd/system/t2-smc-boot-state.service" \ + /etc/systemd/system/t2-smc-boot-state.service || return 1 + # The applesmc options and softdep do not apply under macsmc, and the + # transport must not pull the in-tree applesmc in next to macsmc. + rm -f -- /etc/modprobe.d/t2-sep-boot-state.conf + # udev starts the unit when /dev/smc appears; misc devices are otherwise not + # visible to systemd, so a unit-level device dependency would time out. + install -o root -g root -m 0644 \ + "$source_dir/udev/90-t2-smc-boot-state.rules" \ + /etc/udev/rules.d/90-t2-smc-boot-state.rules || return 1 + udevadm control --reload || return 1 + # Earlier revisions enabled the unit into sysinit.target; drop that link. + rm -f -- /etc/systemd/system/sysinit.target.wants/t2-smc-boot-state.service + systemctl daemon-reload || return 1 +} + ensure_applesmc_on_disk() { + if macsmc_active; then + ensure_macsmc_publisher || return 2 + return 0 + fi # A resident replacement can outlive DKMS removal. Restore the next-boot # module even if this session already published boot state successfully. if ! modinfo -k "$(uname -r)" -p applesmc 2>/dev/null | @@ -207,7 +242,7 @@ check_applesmc_prerequisite() { ;; absent|disabled) ensure_applesmc_on_disk || return 2 - enable_applesmc_next_boot || return 2 + macsmc_active || enable_applesmc_next_boot || return 2 echo "Restart when convenient, then rerun ./install-omarchy.sh." >&2 emit_next_step "Restart when convenient, then rerun ./install-omarchy.sh." return 3 diff --git a/udev/90-t2-smc-boot-state.rules b/udev/90-t2-smc-boot-state.rules new file mode 100644 index 0000000..32fcb48 --- /dev/null +++ b/udev/90-t2-smc-boot-state.rules @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +# Start the one-shot T2 SEP boot-state publisher when the macsmc character +# device appears (Linux 7.3+ T2 kernels). Absent on applesmc kernels. +ACTION=="add", SUBSYSTEM=="misc", KERNEL=="smc", TAG+="systemd", ENV{SYSTEMD_WANTS}+="t2-smc-boot-state.service" diff --git a/uninstall.sh b/uninstall.sh index c904081..99b37e5 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -67,6 +67,10 @@ if [[ -d $applesmc_dkms_state ]]; then dkms remove -m applesmc-t2touch -v 0.1.0 --all fi rm -rf -- "$applesmc_dkms_source" +rm -f -- /etc/systemd/system/t2-smc-boot-state.service /usr/local/sbin/t2-smc-boot-state \ + /etc/systemd/system/sysinit.target.wants/t2-smc-boot-state.service \ + /etc/udev/rules.d/90-t2-smc-boot-state.rules +udevadm control --reload 2>/dev/null || true # Apply the same ownership-checked retirement as upgrades. Unknown old copies # remain visible in the report; do not delete them in the generic loop below. python3 "$source_dir/tools/retire-path-tools.py" --apply