Repository navigation
nvmem: keep the Apple SPMI PMIC's sysfs file read-only - #3299
Conversation
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]>
|
Verdict at 9608a26: ready Blockers: none Checked
EvidenceLocal runs at Paths read: |
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
J516s (MacBook Pro 16" M3 Pro, T6030) row at
|
|
J714s (MacBook Pro 14" M5 Pro, T6050) row at
|
RyanTheTide
left a comment
There was a problem hiding this comment.
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.
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, andforce_rocan'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-nvmemregisters a byte-stride provider over offsets 0x0000 to 0xfffe, with areg_writecallback that forwards toregmap_bulk_write(). Because of that callback, the nvmem core gives it a writable whole-device sysfs file.root_onlyonly 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_onlydoesn't help. It also stops the kernel's cell writes, and root can clear it throughforce_ro.Fix
nvmem: core: a new
nvmem_config.sysfs_read_only, copied intostruct nvmem_device. On such a device:bin_attr_nvmem_write()returns-EPERMwhatever the file's mode orread_onlysay, so achmodor a clearedforce_rodoesn't help either;force_rois 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/nvmemis 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.kunitconfiguses the provider registered as the driver does, on a register map in memory.apple_spmi_nvmem_sysfs_read_only_test: the file is 0400; a write returns-EPERMand the register is unchanged, also withread_onlycleared;force_rois 0444 and refuses a store; root still readsforce_rois 0644 and takes0apple_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)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:
statofspmi_nvmem*/nvmemshows 0400,force_roshows 0444, andecho 0 > force_rofails. No raw write is attempted on hardware.hwclock --systohcthenhwclock -r, and the time is kept across a reboot (the RTC offset cell).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.