Skip to content

soc: apple: sep: publish catacomb files by atomic replacement - #2966

Draft
mmdisbrow wants to merge 4 commits into
aurora-silicon:mainfrom
mmdisbrow:sep-catacomb-atomic
Draft

mmdisbrow wants to merge 4 commits into
aurora-silicon:mainfrom
mmdisbrow:sep-catacomb-atomic

Conversation

@mmdisbrow

@mmdisbrow mmdisbrow commented Nov 23, 2026 •

Copy link
Copy Markdown

catacomb::write() truncates the saved catacomb and rewrites it in place. An error or crash between the truncate and the final sync leaves a short or empty file where the last valid generation was, and the enclave cannot restore it at the next start. Write the complete file under a temporary name, sync and close it, then rename it over the catacomb and sync the parent directory. The driver sends CONFIRM_SAVE only after both sync calls succeed. Durability depends on the filesystem and storage honoring those calls.

Port of omacom/linux-aurora#250, reworked after review. The original remains open. Three enrollments on combined J313 build 39e8f218d716 produced user/master inode changes and empty post-enrollment temporary-file listings. The report did not record rename cookies or listing exit codes. That run used the omacom base, random temporary names, the old filesystem root and no replaceability check, so it does not qualify this series. Hardware report

Upgrade order (§1)

aurora-sep-selinux 1-14 (module 1.4.0) must reach hosts before booting a kernel with atomic replacement, because under 1.3.0 temporary files inherit var_lib_t and kernel_t lacks module-scoped setattr/rename/unlink rules on aurora_sep_state_t. Commit 1 (41a02cefc) is split out with zero driver dependencies and opened as standalone precursor PR #2978 so aurora-sep-selinux 1-14 can land and ship ahead of this PR. Once #2978 merges, this PR rebases cleanly to commits 2–4.

Changes

  1. aurora-sep: selinux: allow catacomb publication by atomic replacement (41a02cefc, standalone precursor aurora-sep: selinux: allow catacomb publication by atomic replacement #2978)
    • aurora_sep.te / .fc: module 1.4.0, aurora_sep_state_t for the three .apple-sep-catacomb-{master,owner,user}.bin.new names, and setattr_file_perms rename_file_perms delete_file_perms for kernel_t on aurora_sep_state_t:file.
    • aurora-sep.spec: bump to 1-14 with upgrade-order changelog note.
    • test_sep_selinux_state.py: verifies the temporary contexts, transitions, and allow rule without depending on store_shim.c.
  2. soc: apple: sep: publish catacomb files by atomic replacement (47346f3dc)
    • store_shim.c, sep_store_replace(): resolves parent from userspace init's root under kernel credentials, checks parent fsync (-EINVAL) and rename (-EPERM) via store_dir_capable(), serializes with store_replace_lock, creates .<name>.new exclusively, retries once after removing a stale regular file (treating -ENOENT from start_removing() as already gone), normalizes mode to 0600 via notify_change() only when caller current_umask() or a default ACL cleared or altered bits, writes all bytes, runs vfs_fsync(temp, 0), pins temp_dentry = dget(temp->f_path.dentry), and closes temp with filp_close(temp, NULL) before rename so any close-time ->flush error aborts before publication and leaves the previous generation intact. Non-regular destinations are refused. A failed post-rename parent fsync returns an error although the replacement is already visible, so CONFIRM_SAVE is not sent.
    • Admission. sep_store_replaceable() and sep_store_publishable() check store_dir_capable(), mnt_want_write(), fsuidgid_has_mapping(), destination replaceability under start_removing() (or parent MAY_WRITE|MAY_EXEC, !IS_APPEND, !IS_DEADDIR under inode_lock_shared(file_inode(dir)) when an absent destination is allowed), and any reserved .<name>.new entry without mutating the directory.
    • catacomb.rs: catacomb::writable() requires shim::replaceable() and open_writable_existing(). catacomb::publishable() checks shim::publishable() plus open_writable_existing().is_ok() || open_readonly() == Err(ENOENT), avoiding a full-file read, KVec allocation, and CRC16 on existing optional catacombs. prepare_missing_owner() checks shim::publishable() before open_host().
    • test_sep_selinux_state.py: adds test_temporary_names_match_the_driver cross-checking ".%s.new", base + 1 in store_shim.c.
  3. aurora-sep: add host fault tests for catacomb replacement (cc4b5bca3)
    • Compiles production store_shim.c (store_write_all, store_remove_temp, store_remove_stale, store_dir_capable, store_ensure_mode, store_replace, store_check_reserved, store_check_publish, sep_store_sync_parent), Rust StoreFile, catacomb.rs, and SepData::{save_catacomb,preflight_enrol,delete_identity,bio_prepare_enrol} against a synthetic per-file VFS and SEP transport.
  4. ci: sep: run the catacomb replacement tests (2b6bcebc8)
    • Runs persistence-tests.py --suite atomic and test_sep_selinux_state.py in sep-source.yml, adding rust/kernel/error.rs to the sparse checkout.

No storage format, enclave protocol, reader or xART policy changes.

Metadata: creation requests mode 0600 under kernel credentials and normalizes back to 0600 via notify_change() if the caller's umask stripped bits. Ownership, ACLs and SELinux label follow creation-time rules. Replacement discards the previous inode's metadata.

Local host validation (2b6bcebc8, Rust 1.85.0)

  • Atomic fault suite (sparse checkout matching sep-source.yml):
    • 38/38 Rust cases pass, adding coverage for filp_close flush failure skipping CONFIRM_SAVE, zero-read catacomb::publishable() on existing/absent/unwritable/unreadable files, and bio_prepare_enrol() unwinding cleanly before SELECT_CONTEXT when prepare_missing_owner() fails parent fsync.
    • 89/89 ASan/UBSan C boundary cases pass, adding coverage for stale .new vanishing (-ENOENT race -> retry succeeds), filp_close() before rename (FLRD ordering and flush-error preservation), umasks 0022/0277/0777 + notify_change() failure unwind, locked IS_APPEND/IS_DEADDIR checks, and sep_store_sync_parent() parent-fsync refusal and unwind (-EINVAL, -EIO, -EACCES, -ENOMEM).
    • Negative control: reverting catacomb::write() to truncate-in-place fails 15/38 Rust cases.
  • CI-selected Python tests: all 16 commands in sep-source.yml (including all 4 -O runs) pass in sparse checkouts at Commit 1 (41a02cefc, 14/14 SELinux + 4/4 packaging, zero drivers/ diff), Commit 2 (47346f3dc, 15/15 SELinux), and HEAD (2b6bcebc8).
  • ABI check, compile, hygiene: sep-abi _Static_assert check passes; store_shim.o compiles with 0 warnings under W=1 against this tree's Linux 7.3 headers (using a throwaway defconfig build outside the repo) with all undefined symbols verified exported (dput, filp_close, notify_change, security_path_rename, security_path_unlink); checkpatch.pl reports 0 errors across all 4 commits (0/0/1/0 warnings, only the MAINTAINERS reminder on Commit 3); git diff --check is clean; git merge-tree --write-tree 2b6bcebc8 471593d0f is clean (9ed76d00d).

Exact-head hardware, SELinux Enforcing, and CI matrix

The host suite and policy checks cover mocked paths only. They do not establish live SELinux labels, CONFIG_APPLE_SEP=m module linkage, KUnit, or power-loss recovery on the shipped filesystem.

Row Requirement Status at 2b6bcebc8
Precursor policy (§1) Ship aurora-sep-selinux 1-14 (#2978) ahead of this kernel change OPEN (#2978)
CI workflows (§5.1, §6) Green SEP source checks (source, rust-build, arm64 QEMU kunit apple_sep*, dwc3-atc-compile) and Host tests on final head PENDING (awaiting maintainer workflow approval)
Enforcing upgrade (RyanTheTide, §5.4) Start with aurora-sep-selinux 1-13 state files on disk, install aurora-sep-selinux 1-14 (module 1.4.0), boot kernel at final head under SELinux Enforcing, enrol + reboot; verify ls -Z /var/lib/apple-sep-catacomb-*.bin is aurora_sep_state_t, clean cold-restore, 0 AVCs, 0 failed units, 0 new kernel errors PENDING (requires Touch ID Mac under SELinux Enforcing)
Lifecycle + S2R cold-restore (§5.2) Boot + enrol + verify + delete + reboot + S2R cold-restore confirming inode replacement, clean restore, and no leaked .apple-sep-catacomb-*.bin.new files PENDING (requires Touch ID Mac)
Stale-temp recovery (§5.3) Pre-existing regular .new cleaned up and replaced; non-regular .new refused without touching destination or mutating enclave PENDING on live hardware (covered in host C/Rust fixtures)
Parent fsync refusal unwind (RyanTheTide) Clean unwind (error propagated, 0 enclave mutations / 0 CONFIRM_SAVE, 0 leaked refs/locks/temp files) on parent fsync refusal Host PASS (store_replace, store_check_publish, sep_store_sync_parent, delete_identity, bio_prepare_enrol, save_catacomb); live filesystem row PENDING

Follow-ups

Still truncate-in-place: the keybag slots, and the host failed-attempt record written by write_lockout_record(). That record is distinct from the enclave lockout blob that save_lockout() stores after a master save. The xART guard intentionally empties its one-time allowance.

Assisted-by: OpenAI Codex (original change and cross-family review), Claude Code and Gemini (rework, port, and verification)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR checklist audit at head 014b29bedf33a7250c4470adb8e3a3479f2e4ac6.

The core VFS replacement design in drivers/soc/apple/store_shim.c is thoughtfully engineered (full-write loop, vfs_fsync(temp, 0) before start_renaming_dentry()/vfs_rename(), d_is_reg() target guard, vfs_fsync(dir, 0) gating CONFIRM_SAVE, start_removing_dentry() cleanup that won't unlink a reused name, mnt_want_write() kept outside kernel_write(), and verified Linux 7.3 symbol exports for CONFIG_APPLE_SEP=m). However, there are blocking §1/§5/§6 gaps plus concrete store_shim.c edge cases to address before this can enter MERGETRAIN-LINUX4:


§1. No model gating & chicken-and-egg — BLOCKED (upgrade-order flag day)

  • Model gating: PASS — no chip/SoC/board/firmware-version gates introduced.
  • Chicken-and-egg / SELinux upgrade-order hazard (Blocker): The PR requires aurora-sep-selinux 1-14 (module 1.4.0) to be installed before booting a kernel with this change. On any Fedora Aurora host still running aurora-sep-selinux 1-13 (module 1.3.0) when the kernel updates first:
    1. type_transition does not cover .apple-sep-catacomb-{master,owner,user}.bin.new, so newly created temporaries inherit var_lib_t instead of aurora_sep_state_t.
    2. kernel_t lacks rename_file_perms / delete_file_perms on aurora_sep_state_t:file in 1.3.0.
    3. Because SELinux enforces inode_unlink/inode_rename (unexported hooks) rather than security_path_unlink (which is a no-op under SELinux), sep_store_replaceable()/sep_store_publishable() cannot detect an outdated 1.3.0 policy during preflight. That means delete_identity() (0x57) or bio_prepare_enrol() (SELECT_CONTEXT) can pass preflight, mutate enclave state, and then fail SAVE_CATACOMB with -EACCES — or on first creation publish a var_lib_t catacomb that fprintd_t/kmod_t/init_t cannot read under Enforcing.
    • Per §1's chicken-and-egg rule, ship aurora-sep-selinux 1-14 first (or split the policy update so it lands and ships ahead of the kernel consumer) before merging the kernel atomic-replacement switch.

§2. Production-ready, no hacks — CHANGES REQUESTED

  • No new kernel cmdline/module parameters or race sleeps.
  • Concrete bugs / edge cases in store_shim.c & catacomb.rs:
    1. Stale-cleanup race returns -ENOENT instead of retrying (drivers/soc/apple/store_shim.c, store_replace() / store_remove_stale()): When file_open_root(..., O_CREAT | O_EXCL) returns -EEXIST and the stale .<name>.new disappears before start_removing() takes the parent lock, store_remove_stale() returns -ENOENT, which breaks out of the 2-attempt loop and fails store_replace() with -ENOENT instead of treating -ENOENT as already-removed and retrying exclusive creation.
    2. fput(temp) bypasses filp_close() / ->flush (drivers/soc/apple/store_shim.c, store_replace()): temp is released with fput(temp) after rename rather than filp_close(temp, NULL), skipping f_op->flush and any close-time flush error (inconsistent with sep_store_close() and relevant on non-local/9P/FUSE filesystems).
    3. Caller current_umask() stripped on every replacement (drivers/soc/apple/store_shim.c, store_replace()): file_open_root(&dir->f_path, temporary, ..., 0600) strips current->fs->umask via mode_strip_umask() / posix_acl_create() when /var/lib has no default POSIX ACL. Previously open_trunc() preserved an existing catacomb's 0600 mode across writes; now every replacement creates a new inode in caller context (bio_prepare_enrol, delete_identity/persist_deletion), so a caller with a restrictive umask masking owner bits produces a non-0600 catacomb. Consider ensuring the created inode has explicit 0600 (or creating under a neutral umask / notify_change) if 0600 is an invariant.
    4. Minor — unnecessary full-file read + CRC in catacomb::publishable() (drivers/soc/apple/catacomb.rs): publishable() calls is_absent(kind), which runs catacomb::read() (allocating a KVec up to 1 MiB, reading the full file, and computing CRC16) on every preflight whenever an optional catacomb already exists, just to decide whether to call open_writable_existing().
    5. Minor — unlocked IS_APPEND/IS_DEADDIR read in store_check_publish() (drivers/soc/apple/store_shim.c): On -ENOENT from start_removing() (which has already dropped dir->i_rwsem), IS_APPEND(file_inode(dir)) and IS_DEADDIR(file_inode(dir)) are inspected unlocked.

§3. Safety — CONDITIONAL ON §1 & §5

  • No hardcoded hardware limits/tables; host-side VFS safety checks (d_is_reg(), d_mountpoint(), store_dir_capable(), fsuidgid_has_mapping(), may_delete_dentry(), CONFIRM_SAVE strictly after both vfs_fsync calls, non-regular .new preserved) are sound.
  • However, SEP catacomb durability and SELinux Enforcing behavior cannot be considered production-safe until §1 upgrade ordering and §5 live hardware + Enforcing validation are proven.

§4. Clean-room and attribution — PASS

  • All 3 commits (1baf9f068f3a, f3923c3db19a, 014b29bedf33) carry human author/committer (Matt <mmdisbro@gmail.com>) and human Signed-off-by: only, with zero AI trailers in commits.
  • AI assistance is disclosed at the end of the PR body. No Apple code, strings, disassembly, or firmware values in the diff.

§5. Evidence — BLOCKED (no exact-head hardware / SELinux Enforcing proof)

  • As candidly noted in the PR body, full CONFIG_APPLE_SEP=m module linkage, KUnit execution, SELinux Enforcing operation, and exact-head SEP hardware behavior remain untested, and the earlier J313 report (omacom/linux-aurora#250) does not qualify this reworked series.
  • Before this can leave draft / be approved for merge, please provide exact-head evidence on an Apple Silicon Mac with SEP/Touch ID:
    1. Full arm64 kernel + CONFIG_APPLE_SEP=m module build/link and kunit.py apple_sep* pass.
    2. Real Mac SEP catacomb boot + enrol/verify/delete + reboot and S2R cold-restore confirming inode replacement, clean restore across reboot/S2R, and no leaked .apple-sep-catacomb-*.bin.new files.
    3. Stale-temp recovery check (pre-existing regular .new cleaned up and replaced; non-regular .new refused without touching destination or mutating enclave).
    4. SELinux Enforcing regression run with aurora-sep-selinux 1-14 installed: ls -Z /var/lib/apple-sep-catacomb-*.bin showing aurora_sep_state_t post-replacement, 0 SELinux AVCs, 0 failed systemd units, 0 new kernel errors.
  • Until hardware + CI evidence is ready, consider marking the PR as Draft per the pre-PR checklist.

§6. Naming & CI — BLOCKED (CI not yet run)

  • Naming: PASS — no prohibited/stale naming in the diff.
  • CI: Fork PR with no executed checks yet (statusCheckRollup empty; SEP source checks and Host tests are both action_required awaiting maintainer workflow approval). Needs green SEP source checks (including rust-build, kunit, and the new atomic host suite) and Host tests on the final head.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review at 014b29b. The replacement path fences the write and checks directory fsync before sending CONFIRM_SAVE. Deployment still needs to carry the matching SELinux policy before this becomes the default. Please add an enforcing upgrade row with the old release's state files followed by an enrollment and reboot on the new policy, and an unwind row for a parent fsync refusal. The host matrix and policy compile cover their mocked paths. They cannot establish working labels or recovery after a power loss on the shipped filesystem.

A kernel that publishes a catacomb by atomic replacement writes the
complete file under a fixed temporary name, ".<name>.new", in /var/lib,
and renames it over the catacomb. SELinux labels a file when it is
created and rename keeps that label, so the temporary must be created
with the state type, not var_lib_t.

Give the three temporary names aurora_sep_state_t with type_transition
rules and file contexts, and let the kernel set attributes on (to reset
a temporary's mode to 0600 after a restrictive caller umask), rename and
unlink state files. Bump the module to 1.4.0 and the package to 1-14.

This policy has to be installed before a kernel with atomic catacomb
replacement, so it lands and ships on its own first. It only adds
labels and permissions; current kernels are unaffected.

Signed-off-by: Matt <mmdisbro@gmail.com>
catacomb::write() truncates the saved catacomb and rewrites it in place.
An error or crash between the truncate and the final sync leaves a short
or empty file where the last valid generation was, and the enclave
cannot restore it at the next start.

Write the complete file under a fixed temporary name, ".<name>.new",
fsync it, close it, rename it over the catacomb under the directory
lock, and fsync the parent before reporting success. Closing before
rename runs ->flush, so a close-time flush error leaves the previous
generation in place. A failed parent fsync is reported as an error even
though the replacement is already visible, so SAVE_CATACOMB is not
confirmed. Non-regular destinations are refused. The parent is resolved
from userspace init's root, as open_as_kernel() does; kernel threads
start in nullfs. Both the writer (before creating a temporary file) and
admission require the parent directory to support directory fsync and
rename.

O_CREAT applies the calling task's umask to the temporary. When that
umask cleared bits from 0600, set the mode back with notify_change()
before writing, so the published file is 0600 regardless of the caller.
A failure there unlinks the temporary and keeps the previous catacomb.

The ".<name>.new" names are reserved for the driver, and a driver lock
serializes its replacements. After a collision, the driver removes a
regular file at its reserved temporary name and retries exclusive
creation once; a leftover that is already gone counts as removed. A
non-regular entry is left in place.

Rename, temporary creation, or parent fsync can fail on a destination
that still opens for writing (for example a file bind mount, an idmapped
mount whose mapping excludes the kernel creator IDs, a filesystem
lacking directory fsync or rename, an append-only parent directory, or a
non-regular entry occupying the reserved temporary name). Add
sep_store_replaceable() and sep_store_publishable() to inspect parent
directory fsync/rename callbacks, the parent mount, creator ID mapping
via fsuidgid_has_mapping(), parent directory permissions, any existing
destination via may_delete_dentry() and d_mountpoint(), and any entry at
the reserved temporary name (including parent unlink support) without
modifying the directory. For an absent destination, the parent's
permission and append-only and dead flags are read under its shared
inode lock. Rejecting d_mountpoint() catches mounts on the dentry
through any alias in this namespace and conservatively refuses mounts
present only in another mount namespace; bind-mounting the parent
directory itself remains allowed. catacomb::writable() now requires
sep_store_replaceable() alongside opening for write;
catacomb::publishable() requires sep_store_publishable() and a file
that either opens for writing or is provably missing, without reading
or checksumming it; enrollment preflight checks publishability for
not-yet-existing catacombs; and owner preparation checks MASTER and
OWNER publishability before creating the owner placeholder or sending
SYSTEM SELECT_CONTEXT, retaining the post-save validation. Later races,
I/O errors, or create/unlink/rename filesystem and LSM denials remain
possible.

Requires aurora-sep-selinux 1.4.0 (aurora-sep 1-14) from the previous
commit, which labels the temporary names and lets the kernel set the
mode of, rename and unlink state files. The new file takes its owner,
ACLs and label from creation, not from the file it replaces.

Signed-off-by: Matt <mmdisbro@gmail.com>
Compile the production store_shim.c replacement, mode, publishability
and parent-sync functions, the Rust StoreFile wrapper,
catacomb::write(), catacomb::writable(), catacomb::publishable(),
catacomb::prepare_missing_owner(), SepData::save_catacomb(),
SepData::preflight_enrol(), SepData::delete_identity() and
SepData::bio_prepare_enrol() against a synthetic per-file VFS and SEP
transport.

With Rust 1.85.0, 38 Rust cases and 89 C boundary cases (ASan and
UBSan) pass. They cover short writes; open, security, chmod, close,
rename and fsync failures; the filesystem root; leftover temporary
files, including non-regular ones, denied or failing removal, and one
that disappears before removal; lock ownership from creation through
cleanup; the temporary closed through filp_close() before rename and
unlinked through its pinned dentry with no leaked references; mode 0600
published under umasks 0022, 0277 and 0777, with notify_change() only
when the umask cleared bits; per-slot replaceability and
publishability refusals (bind mounts, symlinks, directories, unmapped
creator IDs, parents lacking directory fsync/rename/unlink callbacks,
append-only/dead/unwritable parents, read-only mounts, and non-regular
or unremovable reserved temporary entries), with the parent's flags
read only under its lock; publishability decided without reading an
existing catacomb; sep_store_sync_parent() refusals and unwinding;
zero delete commands on each modeled static refusal; enrollment
preflight distinguishing absent from unwritable optional catacombs;
owner preparation refusing before creating a placeholder or selecting a
context, and unwinding on a failed parent fsync; and, for the master
catacomb, that CONFIRM_SAVE follows a durable replacement and the
lockout record is refreshed only after it. The suite also compiles
catacomb::write() reverted to truncate-in-place and requires the
previous-generation preservation case to fail; 15 cases fail.

Signed-off-by: Matt <mmdisbro@gmail.com>
Run the catacomb replacement suite and the SELinux module test in the
SEP source checks. Both run under the job's pinned Rust 1.85.0
toolchain. The suite reads rust/kernel/error.rs, so add it to the job's
sparse checkout.

Signed-off-by: Matt <mmdisbro@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants