Skip to content

nvmem: keep the Apple SPMI PMIC's sysfs file read-only - #3299

Merged
RyanTheTide merged 3 commits into
mainfrom
fix/nvmem-apple-spmi-raw-write
Nov 27, 2026
Merged

RyanTheTide merged 3 commits into
mainfrom
fix/nvmem-apple-spmi-raw-write

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: The Apple SPMI NVMEM provider covers the PMIC's whole 64 KiB register space. Its sysfs file (/sys/bus/nvmem/devices/spmi_nvmem*/nvmem) is now read-only for good: root still reads it, nobody writes it, and force_ro can't change that. The kernel still writes the cells the device tree declares (RTC offset, boot and reboot state).
Tested: no hardware row yet at 9608a26. On arm64 QEMU, the new apple-spmi-nvmem KUnit suite passes 2/2 with the fix. With the provider registered as on main, its sysfs case fails. W=1 and checkpatch --strict are clean.
Risk: two-way door · all Macs (every Apple SoC registers this provider for its primary PMIC). Userspace that wrote the raw file breaks; no tool in our repos does.
Not yet: an RTC and reboot-state row on J714s (M5 Pro) and on a second generation; see Testing.

Problem

apple-spmi-nvmem registers a byte-stride provider over offsets 0x0000 to 0xfffe, with a reg_write callback that forwards to regmap_bulk_write(). Because of that callback, the nvmem core gives it a writable whole-device sysfs file. root_only only makes the file 0600. So root can write any PMIC register through sysfs, not only the few cells the kernel's consumers use: the RTC offset, the boot stage, the boot error and panic counts, and the shutdown flag.

Setting read_only doesn't help. It also stops the kernel's cell writes, and root can clear it through force_ro.

Fix

nvmem: core: a new nvmem_config.sysfs_read_only, copied into struct nvmem_device. On such a device:

  • the whole-device file gets no write permission;
  • bin_attr_nvmem_write() returns -EPERM whatever the file's mode or read_only say, so a chmod or a cleared force_ro doesn't help either;
  • force_ro is shown as 0444 and its store returns -EPERM, because it would only govern the kernel's cell writes.

Cell writes (nvmem_cell_write(), nvmem_device_cell_write()) don't go through sysfs and are unchanged.

nvmem: apple-spmi: the provider registers with sysfs_read_only, through a small helper the test also uses. Root still reads the registers through sysfs. The per-cell sysfs files were already read-only.

ci: drivers/nvmem is compiled in the Apple build job, and the new kunitconfig runs in the KUnit matrix.

I kept CONFIG_NVMEM_SYSFS: other providers' read-only files stay available, and the boundary is enforced even with it on.

Testing

kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/nvmem/apple-spmi-nvmem.kunitconfig uses the provider registered as the driver does, on a register map in memory.

Case With the fix Provider as on main
apple_spmi_nvmem_sysfs_read_only_test: the file is 0400; a write returns -EPERM and the register is unchanged, also with read_only cleared; force_ro is 0444 and refuses a store; root still reads PASS FAIL: the file is 0600, the write returns 6 and changes register 0x2100, force_ro is 0644 and takes 0
apple_spmi_nvmem_cell_write_test: the kernel writes a 6-byte cell (RTC offset) and a 4-bit cell sharing a byte (panic count beside the boot error count) PASS PASS

The "as on main" column ran main's nvmem core and headers, with the provider registered without the flag.

make W=1 drivers/nvmem/core.o drivers/nvmem/apple-spmi-nvmem.o: no warnings. checkpatch --strict: clean.

Hardware row still to run:

  • stat of spmi_nvmem*/nvmem shows 0400, force_ro shows 0444, and echo 0 > force_ro fails. No raw write is attempted on hardware.
  • hwclock --systohc then hwclock -r, and the time is kept across a reboot (the RTC offset cell).
  • A reboot and a shutdown work, with no nvmem or reboot errors in the kernel log (the boot and reboot state cells).

Risk

Two-way: a revert restores the old file. Only the raw whole-device file changes, and it changes on every Mac. Kernel consumers and the per-cell files are untouched.

A provider with a reg_write callback gets a writable whole-device
sysfs file, nvmem. Its kernel consumers may need to write a few cells,
but the file then lets root write anywhere in the device. read_only
does not separate the two: it also stops the kernel's cell writes, and
root can clear it through force_ro.

Add nvmem_config.sysfs_read_only. The whole-device file of such a
device has no write permission, and its write handler refuses writes
whatever the file's mode or read_only say. force_ro is shown but
cannot be stored, as it would only stop the kernel's cell writes.
Kernel consumers write the device's cells as before.

Signed-off-by: DjDeveloperr <[email protected]>
The provider covers the PMIC's whole 64 KiB register space, and its
kernel consumers (the RTC offset, the boot stage, the boot error and
panic counts, the shutdown flag) write the cells the device tree
declares. Its sysfs file was writable by root, which could so write
any PMIC register.

Register it with sysfs_read_only: root still reads the registers
through sysfs, nobody writes them there, and the kernel writes the
cells as before. Registration moves to a helper that the test uses.

Add KUnit cases on a register map in memory, with the provider
registered as the driver does: the sysfs file is 0400, refuses writes,
also once read_only is cleared, and force_ro cannot be stored, while
root still reads; and the kernel still writes a byte cell (the RTC
offset) and a bit-field cell (the panic count beside the boot error
count). With the provider registered as before, the first case fails:
the file is 0600, a write reaches the register, and force_ro is
writable.

Signed-off-by: DjDeveloperr <[email protected]>
Compile drivers/nvmem in the Apple build job, run the apple-spmi-nvmem
suite in the KUnit matrix, and start the workflow on drivers/nvmem
changes.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team and RyanTheTide as code owners November 26, 2026 18:56
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 9608a26: ready

Blockers: none

Checked

  • Every sysfs path to the whole-device file is closed for this provider.

    • nvmem_bin_attr_get_umode() drops the write bit, which covers both the nvmem file and the legacy eeprom/fram compat file. nvmem_sysfs_setup_compat() takes its mode from that same function and its writes go through the same bin_attr_nvmem_write().
    • bin_attr_nvmem_write() refuses before it looks at alignment or read_only, so a chmod of the file doesn't help.
    • force_ro is shown as 0444 and its store refuses, so read_only can't be changed from sysfs.
    • The per-cell files were already 0444 & mode.
    • sysfs_read_only is copied before device_add() and never changes afterwards, so there is no window where the file is created writable.
  • No other userspace path reaches the PMIC's registers.

    • The SPMI bus doesn't enable driver_override and has no character device, so the PMIC can't be rebound to another driver.
    • Regmap debugfs writes are compiled out (REGMAP_ALLOW_WRITE_DEBUGFS is undefined).
    • rtc-apple-pmu, the PMIC's child driver, writes only its own fixed RTC registers over the parent regmap, plus the rtc_offset cell.
    • Nothing in the tree calls nvmem_device_write() on this provider. The RTC and reboot consumers write only their declared cells, and those writes still work (the second KUnit case).
  • The KUnit cases catch each part of the fix (all three mutations below fail). Each one removes one part of the fix:

    • the provider's flag;
    • only the write handler's check (with the mode still 0400);
    • only the force_ro store check.

    The cell case passes in every run.

  • Merged-code rules: the flag is set once at registration. It isn't a runtime switch or a module parameter, adds no magic constant, and has no SoC or board gate.

  • CI hunks: drivers/nvmem/** is added to both path filters, drivers/nvmem/ to the Apple build job, and the suite to the KUnit matrix. The kunitconfig selects everything the test's depends on needs.

  • PR text and commits are neutral and carry no attribution. checkpatch --strict is clean on all three commits.

  • A minor point, not blocking: in apple_spmi_nvmem_sysfs_read_only_test, nvmem->read_only is already false for this provider (it has reg_write, no read-only property, and read_only unset in its config). So the "as if read_only were cleared" half re-checks the same state. The first write already exercises the new check (mutation M2), so coverage doesn't depend on it.

  • Still owed before merge: CI was still pending at this head when I checked, and the J714s and second-generation RTC/reboot rows listed under "Not yet" haven't run.

Evidence

Local runs at 9608a26ff3794c185ca3f55a5f3bbef15c9ac53e, arm64 QEMU, clang. CONFIG_WERROR=n was added only because the host compiler warns in unrelated files.

kunit.py run --arch=arm64 --make_options LLVM=1 --kconfig_add CONFIG_WERROR=n \
  --kunitconfig=drivers/nvmem/apple-spmi-nvmem.kunitconfig

head:  [PASSED] apple_spmi_nvmem_sysfs_read_only_test
       [PASSED] apple_spmi_nvmem_cell_write_test
       Testing complete. Ran 2 tests: passed: 2

M1 (provider without .sysfs_read_only, as on main):
       FAILED sysfs_read_only_test: mode != 0400, write != -EPERM, regs[0x2100] changed,
              force_ro mode != 0444, force_ro store != -EPERM
       PASSED cell_write_test

M2 (flag kept, write-handler check removed):
       FAILED sysfs_read_only_test: write != -EPERM, regs[0x2100] changed
       PASSED cell_write_test

M3 (flag kept, force_ro store check removed):
       FAILED sysfs_read_only_test: force_ro store != -EPERM
       PASSED cell_write_test
make O=<kunit build> ARCH=arm64 LLVM=1 W=1 drivers/nvmem/core.o drivers/nvmem/apple-spmi-nvmem.o
-> no warnings (CONFIG_NVMEM_SYSFS=y, test included)

scripts/checkpatch.pl --strict -g HEAD~3..HEAD
-> 0 errors, 0 warnings, 0 checks (all three commits)

Paths read: drivers/nvmem/core.c (force_ro_store, bin_attr_nvmem_write, nvmem_bin_attr_get_umode, nvmem_attr_is_visible, nvmem_sysfs_setup_compat, nvmem_populate_sysfs_cells, nvmem_register ordering), drivers/spmi/spmi.c (spmi_bus_type), drivers/base/regmap/regmap-debugfs.c, drivers/rtc/rtc-apple-pmu.c, drivers/power/reset/nvmem-reboot-mode.c, and the tree-wide nvmem_device_write() callers.

@github-actions github-actions Bot added area:release Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 9608a26f: ready to merge

Check Detail
CI at head ok 25 checks passed
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head ok comment at 9608a26f
Conflicts ok none
PR text ok Risk: two-way · all Macs (every Apple SoC registers this provider for its primary PMIC). Userspace that wrote the raw file breaks; no tool in our repos does. : a revert restores the old file. Only the raw whole-device file changes, and it changes on every Mac. Kernel consumers and the per-cell files are untouched.

Areas: area:release
Merge: gh pr merge 3299 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J516s (MacBook Pro 16" M3 Pro, T6030) row at 9608a26ff379: PASS. One-shot boot of the exact head (main + this PR, default cmdline, the head's own t6030-j516s.dtb).

  • sysfs, spmi_nvmem0 (364714000.spmi/spmi-0/0-0b): nvmem is 0400 (the baseline kernel has 0600) and force_ro is 0444 (baseline 0644). Root reads the file; a normal user gets Permission denied. echo 0 > force_ro as root fails with Permission denied at open, because the file has no write bit, and force_ro stays 0. No write to nvmem was attempted.
  • RTC: hwclock --systohc rc 0, and hwclock -r matched the system clock. After the reboot, the baseline kernel came up with NTP not yet synced; its clock, read from the RTC, agreed with the host's within 1 s. On this Mac rtc0 is macsmc-rtc.
  • Reboot: the head kernel rebooted cleanly. Its log (journalctl -k -b -1) has 0 nvmem, SPMI, PMIC or reboot error lines.
  • Regressions: 0 failed units, and no new kernel errors beyond this Mac's per-boot MTP HID line.
  • Not tested: shutdown, because there is no remote power-on. Restored to stock and verified.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row at 9608a26ff379: PASS.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 9608a26ff379

Blockers:
None in the fix. Record the required two-Mac full regression coverage before merge.

Checked: Reviewed the provider registration, both sysfs mode and write enforcement, the compatibility file path, and the kernel cell path. The new flag is established before publication and changes only the provider that opts in. Reviewed the two field tests, CI KUnit and the J516s/J714s hardware rows.

Evidence

GitHub lists 26 successful checks, including KUnit apple-spmi-nvmem. J516s and J714s report the expected read-only permissions, working RTC and clean reboots. The J714s run used a combined build. Neither hardware row reports a raw register write. Keep the T8112 auxiliary-PMIC reland separate, with its own J473 row.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 27, 2026
@RyanTheTide
RyanTheTide merged commit fcb1242 into main Nov 27, 2026
27 of 53 checks passed
@RyanTheTide
RyanTheTide deleted the fix/nvmem-apple-spmi-raw-write branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:release Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants