From 06902b339438dbf4e14437895caacde4d602198c Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 27 Oct 2026 21:58:06 -0400 Subject: [PATCH 1/2] soc: apple: sep: publish catacomb files by atomic replacement Prepare a complete file under an exclusive temporary name, fsync it, rename under the directory lock and fsync the parent before reporting success. Refuse non-regular destinations and keep visibility distinct from proven durability after rename. Shared primitives also support later exclusive-file and host-store header publication. Compile the production C and Rust paths in synthetic VFS fault tests with Rust 1.85.0. Fifteen catacomb cases and 33 C boundary cases pass; reverting the writer to truncate-in-place fails the named preservation assertions. No hardware or power-loss qualification is claimed. Signed-off-by: Matt --- drivers/soc/apple/catacomb.rs | 7 +- drivers/soc/apple/shim.h | 5 + drivers/soc/apple/shim.rs | 14 + drivers/soc/apple/store_shim.c | 233 ++++++++ tools/aurora-ci/host-tests.json | 15 + tools/aurora-sep/tests/persistence/README.md | 5 + .../tests/persistence/fixtures/atomic-vfs.h | 550 ++++++++++++++++++ .../tests/persistence/fixtures/atomic.c | 167 ++++++ .../tests/persistence/fixtures/atomic.rs | 254 ++++++++ .../tests/persistence/fixtures/common.rs | 207 +++++++ .../tests/persistence/persistence-tests.py | 224 +++++++ 11 files changed, 1675 insertions(+), 6 deletions(-) create mode 100644 tools/aurora-sep/tests/persistence/README.md create mode 100644 tools/aurora-sep/tests/persistence/fixtures/atomic-vfs.h create mode 100644 tools/aurora-sep/tests/persistence/fixtures/atomic.c create mode 100644 tools/aurora-sep/tests/persistence/fixtures/atomic.rs create mode 100644 tools/aurora-sep/tests/persistence/fixtures/common.rs create mode 100644 tools/aurora-sep/tests/persistence/persistence-tests.py diff --git a/drivers/soc/apple/catacomb.rs b/drivers/soc/apple/catacomb.rs index 8b18efb4ead770..a6e4d790d9d8d2 100644 --- a/drivers/soc/apple/catacomb.rs +++ b/drivers/soc/apple/catacomb.rs @@ -38,12 +38,7 @@ pub(crate) fn write(kind: u8, blob: &[u8]) -> Result<()> { head[8..12].copy_from_slice(&(blob.len() as u32).to_le_bytes()); head[12..14].copy_from_slice(&crc16_ccitt_false(blob).to_le_bytes()); - let file = shim::StoreFile::open_trunc(path)?; - file.write_all(0, &head)?; - if !blob.is_empty() { - file.write_all(HEADER as u64, blob)?; - } - file.sync() + shim::replace_file(path, &head, blob) } pub(crate) fn read(kind: u8) -> Option> { diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 0425bf2cdaa6ed..d30e2140b0a7ee 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -44,6 +44,11 @@ void sep_hwrng_unregister(void *mem); void *sep_store_open(const char *path); void *sep_store_open_host(const char *path, bool *created); void *sep_store_open_trunc(const char *path); +int sep_store_replace_header(void **handle, const char *path, + const void *head, size_t head_len, u64 expected_size); +int sep_store_replace(const char *path, const void *head, size_t head_len, + const void *body, size_t body_len); +int sep_store_create(const char *path, const void *body, size_t body_len); void *sep_store_open_ro(const char *path); void *sep_store_open_block(const char *path, int writable); void sep_store_close(void *handle); diff --git a/drivers/soc/apple/shim.rs b/drivers/soc/apple/shim.rs index 8e30c3f54b8177..fff26a12838a19 100644 --- a/drivers/soc/apple/shim.rs +++ b/drivers/soc/apple/shim.rs @@ -9,6 +9,8 @@ extern "C" { fn sep_store_open(path: *const c_char) -> *mut c_void; fn sep_store_open_host(path: *const c_char, created: *mut bool) -> *mut c_void; fn sep_store_open_trunc(path: *const c_char) -> *mut c_void; + fn sep_store_replace(path: *const c_char, head: *const c_void, head_len: usize, + body: *const c_void, body_len: usize) -> c_int; fn sep_store_open_ro(path: *const c_char) -> *mut c_void; fn sep_store_open_block(path: *const c_char, writable: c_int) -> *mut c_void; fn sep_store_close(handle: *mut c_void); @@ -28,6 +30,18 @@ pub(crate) fn random_bytes(buf: &mut [u8]) -> Result<()> { kernel::error::to_result(ret) } +/// Atomically replaces a complete file and synchronises its parent directory. +/// On an error after rename the complete new file can already be visible. +pub(crate) fn replace_file(path: &CStr, head: &[u8], body: &[u8]) -> Result<()> { + // SAFETY: path is NUL-terminated and both slices remain valid for the whole + // call. The shim consumes these bytes synchronously and retains no pointer. + let ret = unsafe { + sep_store_replace(path.as_char_ptr(), head.as_ptr().cast(), head.len(), + body.as_ptr().cast(), body.len()) + }; + kernel::error::to_result(ret) +} + /// Backing-store file handle. /// /// # Invariants diff --git a/drivers/soc/apple/store_shim.c b/drivers/soc/apple/store_shim.c index 0eabe7305b0497..caf50211546349 100644 --- a/drivers/soc/apple/store_shim.c +++ b/drivers/soc/apple/store_shim.c @@ -16,7 +16,12 @@ #include #include #include +#include +#include +#include #include +#include +#include #include #include "shim.h" @@ -80,6 +85,234 @@ void *sep_store_open_trunc(const char *path) return IS_ERR(f) ? NULL : f; } +static int store_write_all(struct file *file, const void *buf, size_t len, + loff_t *pos) +{ + while (len) { + ssize_t n = kernel_write(file, buf, len, pos); + + if (n < 0) + return n; + if (!n) + return -EIO; + buf = (const char *)buf + n; + len -= n; + } + return 0; +} + +/* Stream the unchanged part of a host store without a multi-megabyte allocation. */ +static int store_copy_tail(struct file *to, struct file *from, loff_t offset, + loff_t size) +{ + loff_t out = offset; + void *buf; + int err = 0; + + if (size < offset) + return -EINVAL; + if (i_size_read(file_inode(from)) != size) + return -ESTALE; + buf = kmalloc(32768, GFP_KERNEL); + if (!buf) + return -ENOMEM; + while (offset < size) { + ssize_t n = kernel_read(from, buf, min_t(loff_t, size - offset, 32768), &offset); + + if (n < 0) { + err = n; + break; + } + if (!n) { + err = -EIO; + break; + } + err = store_write_all(to, buf, n, &out); + if (err) + break; + } + if (!err && i_size_read(file_inode(from)) != size) + err = -ESTALE; + kfree(buf); + return err; +} + +/* Remove only the temporary dentry we opened, never a name reused by another task. */ +static int store_remove_temp(struct file *dir, struct file *temp) +{ + struct dentry *dentry; + int err; + + err = mnt_want_write(dir->f_path.mnt); + if (err) + return err; + dentry = start_removing_dentry(dir->f_path.dentry, temp->f_path.dentry); + if (IS_ERR(dentry)) { + err = PTR_ERR(dentry); + } else { + err = security_path_unlink(&dir->f_path, dentry); + if (!err) + err = vfs_unlink(file_mnt_idmap(dir), file_inode(dir), dentry, NULL); + end_removing(dentry); + } + mnt_drop_write(dir->f_path.mnt); + return err; +} + +/* + * Durable full-file replacement, without truncating the previous generation. + * The caller owns both byte slices until return. Only the final pathname is + * replaced; symlinks and non-regular destinations are refused under rename's + * directory lock. A successful rename is the visibility commit point: a later + * directory fsync failure returns an error but cannot safely undo that rename. + */ +static int store_replace(const char *path, const void *head, size_t head_len, + const void *body, size_t body_len, void **handle, + loff_t expected_size, bool exclusive) +{ + const char *base = strrchr(path, '/'); + const struct cred *old; + struct cred *kern; + struct file *dir, *temp = NULL; + struct path root; + struct renamedata rd = {}; + struct qstr name; + char *parent; + char temporary[48]; + u64 nonce[2]; + loff_t pos = 0; + bool renamed = false; + int err, cleanup, attempt; + + if (!base || base == path || !base[1]) + return -EINVAL; + parent = kstrndup(path, base - path, GFP_KERNEL); + if (!parent) + return -ENOMEM; + kern = prepare_kernel_cred(&init_task); + if (!kern) { + kfree(parent); + return -ENOMEM; + } + task_lock(&init_task); + get_fs_root(init_task.fs, &root); + task_unlock(&init_task); + old = override_creds(kern); + dir = file_open_root(&root, parent, O_RDONLY | O_DIRECTORY | O_LARGEFILE, 0); + path_put(&root); + kfree(parent); + if (IS_ERR(dir)) { + err = PTR_ERR(dir); + goto out_creds; + } + + /* Exclusive creation never adopts or truncates a previous temporary file. */ + for (attempt = 0; attempt < 4; attempt++) { + get_random_bytes(nonce, sizeof(nonce)); + snprintf(temporary, sizeof(temporary), ".sep-%016llx%016llx.tmp", + (unsigned long long)nonce[0], (unsigned long long)nonce[1]); + temp = file_open_root(&dir->f_path, temporary, + (handle ? O_RDWR : O_WRONLY) | O_CREAT | O_EXCL | O_NOFOLLOW | O_LARGEFILE, 0600); + if (PTR_ERR_OR_ZERO(temp) != -EEXIST) + break; + } + if (IS_ERR(temp)) { + err = PTR_ERR(temp); + temp = NULL; + goto out_dir; + } + err = store_write_all(temp, head, head_len, &pos); + if (!err) { + if (handle) + err = store_copy_tail(temp, *handle, head_len, expected_size); + else + err = store_write_all(temp, body, body_len, &pos); + } + if (!err) + err = vfs_fsync(temp, 0); + if (err) + goto out_temp; + + /* Do not hold a mount write claim across kernel_write's own freeze lock. */ + err = mnt_want_write(dir->f_path.mnt); + if (err) + goto out_temp; + rd.mnt_idmap = file_mnt_idmap(dir); + rd.old_parent = dir->f_path.dentry; + rd.new_parent = dir->f_path.dentry; + name = (struct qstr)QSTR_INIT(base + 1, strlen(base + 1)); + err = start_renaming_dentry(&rd, 0, temp->f_path.dentry, &name); + if (!err) { + if (exclusive && d_is_positive(rd.new_dentry)) + err = -EEXIST; + else if (handle && (!d_is_positive(rd.new_dentry) || + d_inode(rd.new_dentry) != file_inode((struct file *)*handle))) + err = -ESTALE; + else if (d_is_positive(rd.new_dentry) && !d_is_reg(rd.new_dentry)) + err = -EINVAL; + else + err = security_path_rename(&dir->f_path, rd.old_dentry, + &dir->f_path, rd.new_dentry, 0); + if (!err) + err = vfs_rename(&rd); + end_renaming(&rd); + } + mnt_drop_write(dir->f_path.mnt); + if (!err) { + renamed = true; + err = vfs_fsync(dir, 0); + if (err) + pr_err("apple-sep: replacement is visible but parent directory sync failed (%d)\n", err); + } +out_temp: + if (!renamed) { + cleanup = store_remove_temp(dir, temp); + if (cleanup) + pr_warn("apple-sep: temporary store cleanup failed (%d)\n", cleanup); + } + /* Rename committed the new inode even when the parent fsync failed. Keep + * future reads/writes on that inode; never continue on an unlinked old one. + */ + if (renamed && handle) { + struct file *previous = *handle; + + *handle = temp; + temp = NULL; + fput(previous); + } + if (temp) + fput(temp); +out_dir: + fput(dir); +out_creds: + put_cred(revert_creds(old)); + return err; +} + +int sep_store_replace(const char *path, const void *head, size_t head_len, + const void *body, size_t body_len) +{ + return store_replace(path, head, head_len, body, body_len, NULL, 0, false); +} + +/* Initial key material must not replace a file published by another caller + * after the absence check. The destination check shares rename's lock. */ +int sep_store_create(const char *path, const void *body, size_t body_len) +{ + return store_replace(path, NULL, 0, body, body_len, NULL, 0, true); +} + +/* Caller exclusively owns *handle; on a committed rename its reference moves + * to the replacement inode, including when directory fsync reports an error. + */ +int sep_store_replace_header(void **handle, const char *path, + const void *head, size_t head_len, u64 expected_size) +{ + if (expected_size > LLONG_MAX) + return -EINVAL; + return store_replace(path, head, head_len, NULL, 0, handle, expected_size, false); +} + /* * Opens an existing file read-only, NULL if absent. O_RDONLY and no O_CREAT so * the seed cannot be created, truncated or written by mistake. diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 1310ec2bb60146..931443ae336e88 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -1101,6 +1101,21 @@ ], "rust": "1.85.0" }, +{ + "name": "tools/aurora-sep/tests/persistence/persistence-tests.py:atomic", + "group": "aurora-sep", + "argv": [ + "python3", + "-I", + "tools/aurora-sep/tests/persistence/persistence-tests.py", + "--suite", + "atomic", + "--output", + "{output}" + ], + "rust": "1.85.0", + "timeout": 300 + }, { "name": "tools/aurora-sep/tests/test_adt_extract.py", "group": "aurora-sep", diff --git a/tools/aurora-sep/tests/persistence/README.md b/tools/aurora-sep/tests/persistence/README.md new file mode 100644 index 00000000000000..8665ff82fd74f4 --- /dev/null +++ b/tools/aurora-sep/tests/persistence/README.md @@ -0,0 +1,5 @@ +# SEP host-file publication fault tests + +The atomic suite compiles the production C replacement primitive, Rust StoreFile wrapper and catacomb writer against a synthetic VFS. It checks short writes, allocation/open/security/rename/fsync failures, preservation of the old generation before rename, and a complete visible generation after a failed directory fsync. The C boundary tests reject symlinks and non-regular destinations, exclusive-publication races, and unsafe cleanup. The suite also compiles a deliberate regression and requires its named behavioral assertion to fail. No real files, SEP state or hardware are opened. + +Run `python3 -I tools/aurora-sep/tests/persistence/persistence-tests.py --suite atomic --output /tmp/sep-atomic`. CI pins Rust 1.85.0. These host fixtures do not establish power-loss durability on physical storage or SEP anti-replay safety. diff --git a/tools/aurora-sep/tests/persistence/fixtures/atomic-vfs.h b/tools/aurora-sep/tests/persistence/fixtures/atomic-vfs.h new file mode 100644 index 00000000000000..8c478e9d1a0f06 --- /dev/null +++ b/tools/aurora-sep/tests/persistence/fixtures/atomic-vfs.h @@ -0,0 +1,550 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Offline VFS boundary fixture. Production replacement function is appended unchanged. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define GFP_KERNEL 0 +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif +#define IS_ERR(p) ((uintptr_t)(p) >= (uintptr_t)(-4095)) +#define PTR_ERR(p) ((long)(intptr_t)(p)) +#define PTR_ERR_OR_ZERO(p) (IS_ERR(p) ? PTR_ERR(p) : 0) +#define ERR_PTR(n) ((void *)(intptr_t)(n)) +#define QSTR_INIT(n, l) { .name = (n), .len = (l) } +#define CAP ((1U << 20) + 32) +typedef uint64_t u64; +/* Supply the existing kernel ABI name to the host-compiled production code. */ +typedef unsigned int umode_t; +struct inode { + int type; +}; +struct dentry { + struct inode inode; + bool linked; +}; +struct path { + struct dentry *dentry; + void *mnt; +}; +struct file { + struct path f_path; + unsigned char bytes[CAP]; + size_t len; + int refs; +}; +struct cred { + int unused; +}; +struct qstr { + const char *name; + size_t len; +}; +struct renamedata { + void *mnt_idmap; + struct dentry *old_parent, *old_dentry, *new_parent, *new_dentry; + void *delegated_inode; + unsigned int flags; +}; +static struct { + void *fs; +} init_task; +static struct cred credential; +static struct file directory, temporary_file, canonical; +static struct dentry root_dentry; +static int mount_token, idmap_token; +static int stages[20], mounts, locks, credentials, roots, allocations; +static int attempts, unlinks, renames, temp_syncs, dir_syncs, temp_present, + collision_count; +static int cleanup_warning, commit_warning, write_error, zero_write, + write_calls; +static size_t write_at, short_limit; +static bool exists; +static bool publish_at_rename; +static char events[1024]; +static void event(char c) +{ + size_t n = strlen(events); + + assert(n + 1 < sizeof(events)); + events[n] = c; + events[n + 1] = 0; +} +enum { + ALLOC = 1, + CRED, + PARENT_OPEN, + TEMP_OPEN, + MOUNT, + RENAME_LOCK, + SECURITY_RENAME, + RENAME, + TEMP_SYNC, + DIR_SYNC, + CLEANUP_LOCK, + SECURITY_UNLINK, + UNLINK, + TARGET_TYPE, + STALE_SOURCE +}; +static bool fixture_path(const char *name) +{ + return strstr(name, "apple-sep-catacomb-") || + strstr(name, "aurora-sep-keybag.bin") || + strstr(name, "aurora-sep-refkey.bin") || + strstr(name, "aurora-sep-refkey-v2.bin"); +} +void fixture_reset(void) +{ + memset(&directory, 0, sizeof(directory)); + memset(&temporary_file, 0, sizeof(temporary_file)); + memset(&canonical, 0, sizeof(canonical)); + directory.f_path.mnt = &mount_token; + directory.f_path.dentry = calloc(1, sizeof(struct dentry)); + assert(directory.f_path.dentry); + directory.f_path.dentry->inode.type = 2; + temporary_file.f_path.dentry = calloc(1, sizeof(struct dentry)); + assert(temporary_file.f_path.dentry); + temporary_file.f_path.dentry->inode.type = 1; + canonical.f_path.dentry = calloc(1, sizeof(struct dentry)); + assert(canonical.f_path.dentry); + canonical.f_path.dentry->inode.type = 1; + temporary_file.f_path.mnt = canonical.f_path.mnt = &mount_token; + memset(stages, 0, sizeof(stages)); + mounts = locks = credentials = roots = allocations = 0; + attempts = unlinks = renames = temp_syncs = dir_syncs = temp_present = + collision_count = 0; + cleanup_warning = commit_warning = write_error = zero_write = + write_calls = 0; + write_at = short_limit = 0; + exists = false; + publish_at_rename = false; + events[0] = 0; +} +void fixture_done(void) +{ + assert(!mounts && !locks && !credentials && !roots && !allocations); + assert(!directory.refs && !temporary_file.refs && !canonical.refs); + free(directory.f_path.dentry); + free(temporary_file.f_path.dentry); + free(canonical.f_path.dentry); +} +void fixture_set(const void *p, size_t n) +{ + assert(n <= CAP); + memcpy(canonical.bytes, p, n); + canonical.len = n; + exists = true; +} +void fixture_race_publication(void) +{ + publish_at_rename = true; +} +size_t fixture_get(void *p, size_t n) +{ + if (!exists) + return 0; + assert(n >= canonical.len); + memcpy(p, canonical.bytes, canonical.len); + return canonical.len; +} +void fixture_fault(int stage, int error) +{ + assert(stage > 0 && stage < 20); + stages[stage] = error; +} +void fixture_write_fault(size_t at, int error, int zero) +{ + write_at = at; + write_error = error; + zero_write = zero; +} +void fixture_short(size_t n) +{ + short_limit = n; +} +void fixture_collisions(int n) +{ + collision_count = n; +} +int fixture_count(int n) +{ + switch (n) { + case 0: + return renames; + case 1: + return temp_syncs; + case 2: + return dir_syncs; + case 3: + return temp_present; + case 4: + return unlinks; + case 5: + return commit_warning; + case 6: + return cleanup_warning; + case 7: + return attempts; + default: + abort(); + } +} +const char *fixture_events(void) +{ + return events; +} +static void *kstrndup(const char *p, size_t n, int g) +{ + assert(g == GFP_KERNEL); + if (stages[ALLOC]) + return NULL; + char *s = malloc(n + 1); + + assert(s); + memcpy(s, p, n); + s[n] = 0; + allocations++; + return s; +} +static void kfree(void *p) +{ + free(p); + allocations--; + assert(allocations >= 0); +} +static struct cred *prepare_kernel_cred(void *task) +{ + assert(task == &init_task); + return stages[CRED] ? NULL : &credential; +} +static void task_lock(void *p) +{ + assert(p == &init_task); +} +static void task_unlock(void *p) +{ + assert(p == &init_task); +} +static void get_fs_root(void *fs, struct path *p) +{ + (void)fs; + *p = (struct path){ &root_dentry, &mount_token }; + roots++; +} +static void path_put(struct path *p) +{ + assert(p->dentry == &root_dentry); + roots--; +} +static const struct cred *override_creds(struct cred *p) +{ + assert(p == &credential); + credentials++; + return &credential; +} +static struct cred *revert_creds(const struct cred *p) +{ + assert(p == &credential); + credentials--; + return &credential; +} +static void put_cred(struct cred *p) +{ + assert(p == &credential); +} +static struct file *file_open_root(const struct path *p, const char *name, + int flags, umode_t mode) +{ + assert(credentials == 1); + if (p->dentry == &root_dentry) { + assert(!strcmp(name, "/var/lib")); + assert(flags == (O_RDONLY | O_DIRECTORY | O_LARGEFILE) && + mode == 0); + if (stages[PARENT_OPEN]) + return ERR_PTR(-stages[PARENT_OPEN]); + directory.refs++; + return &directory; + } + assert(p == &directory.f_path); + assert(flags == + (O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_LARGEFILE)); + assert(mode == 0600); + assert(strlen(name) == 41 && !strncmp(name, ".sep-", 5)); + attempts++; + if (collision_count-- > 0) + return ERR_PTR(-EEXIST); + if (stages[TEMP_OPEN]) + return ERR_PTR(-stages[TEMP_OPEN]); + assert(!temp_present); + temp_present = 1; + temporary_file.refs++; + temporary_file.f_path.dentry->linked = true; + temporary_file.len = 0; + event('C'); + return &temporary_file; +} +static void get_random_bytes(void *p, size_t n) +{ + memset(p, attempts + 1, n); +} +static ssize_t kernel_write(struct file *f, const void *buf, size_t n, + loff_t *pos) +{ + assert(f == &temporary_file || f == &canonical); + assert(!mounts); + assert(*pos >= 0 && (size_t)*pos + n <= CAP); + write_calls++; + if ((write_error || zero_write) && (size_t)*pos >= write_at) + return zero_write ? 0 : -write_error; + if (short_limit && n > short_limit) + n = short_limit; + if ((write_error || zero_write) && (size_t)*pos < write_at && + n > write_at - (size_t)*pos) + n = write_at - (size_t)*pos; + memcpy(f->bytes + *pos, buf, n); + *pos += n; + if ((size_t)*pos > f->len) + f->len = *pos; + event('W'); + return n; +} +static int vfs_fsync(struct file *f, int data) +{ + assert(!data && !locks && !mounts); + if (f == &directory) { + dir_syncs++; + event('D'); + return -stages[DIR_SYNC]; + } + assert(f == &temporary_file || f == &canonical); + temp_syncs++; + event('F'); + return -stages[TEMP_SYNC]; +} +static int mnt_want_write(void *m) +{ + assert(m == &mount_token); + if (stages[MOUNT]) + return -stages[MOUNT]; + assert(!mounts); + mounts++; + return 0; +} +static void mnt_drop_write(void *m) +{ + assert(m == &mount_token && mounts == 1 && !locks); + mounts--; +} +static void *file_mnt_idmap(struct file *f) +{ + assert(f == &directory); + return &idmap_token; +} +static struct inode *file_inode(struct file *f) +{ + return &f->f_path.dentry->inode; +} +static int start_renaming_dentry(struct renamedata *rd, int flags, + struct dentry *old, struct qstr *name) +{ + assert(credentials == 1 && mounts == 1 && !locks && flags == 0); + assert(rd->mnt_idmap == &idmap_token); + assert(rd->old_parent == directory.f_path.dentry && + rd->new_parent == directory.f_path.dentry); + assert(old == temporary_file.f_path.dentry); + assert(name->len == strlen(name->name) && fixture_path(name->name)); + if (stages[RENAME_LOCK]) + return -stages[RENAME_LOCK]; + if (stages[STALE_SOURCE]) + return -EINVAL; + locks++; + if (publish_at_rename) { + fixture_set("competing", 9); + publish_at_rename = false; + } + rd->old_dentry = old; + rd->new_dentry = canonical.f_path.dentry; + rd->new_dentry->linked = exists; + rd->new_dentry->inode.type = stages[TARGET_TYPE] ? stages[TARGET_TYPE] : + 1; + return 0; +} +static bool d_is_positive(struct dentry *d) +{ + return d->linked; +} +static bool d_is_reg(struct dentry *d) +{ + return d->inode.type == 1; +} +static int security_path_rename(const struct path *a, struct dentry *b, + const struct path *c, struct dentry *d, + unsigned int flags) +{ + assert(a == &directory.f_path && c == a && + b == temporary_file.f_path.dentry && + d == canonical.f_path.dentry && !flags && locks); + return -stages[SECURITY_RENAME]; +} +static int vfs_rename(struct renamedata *rd) +{ + assert(locks && mounts && credentials); + assert(strchr(events, 'F')); + assert(rd->delegated_inode == NULL && rd->flags == 0); + if (stages[RENAME]) + return -stages[RENAME]; + canonical.len = temporary_file.len; + memcpy(canonical.bytes, temporary_file.bytes, canonical.len); + exists = true; + temp_present = 0; + renames++; + event('R'); + return 0; +} +static void end_renaming(struct renamedata *rd) +{ + assert(rd->old_parent == directory.f_path.dentry && locks == 1); + locks--; +} +static struct dentry *start_removing_dentry(struct dentry *parent, + struct dentry *child) +{ + assert(parent == directory.f_path.dentry && + child == temporary_file.f_path.dentry && mounts && !locks); + if (stages[CLEANUP_LOCK]) + return ERR_PTR(-stages[CLEANUP_LOCK]); + if (stages[STALE_SOURCE]) + return ERR_PTR(-EINVAL); + locks++; + return child; +} +static int security_path_unlink(const struct path *p, struct dentry *d) +{ + assert(p == &directory.f_path && d == temporary_file.f_path.dentry && + locks); + return -stages[SECURITY_UNLINK]; +} +static int vfs_unlink(void *idmap, struct inode *inode, struct dentry *d, + void *delegated) +{ + assert(idmap == &idmap_token && inode == file_inode(&directory) && + d == temporary_file.f_path.dentry && !delegated && locks && + mounts); + if (stages[UNLINK]) + return -stages[UNLINK]; + assert(temp_present); + temp_present = 0; + unlinks++; + event('U'); + return 0; +} +static void end_removing(struct dentry *d) +{ + assert(d == temporary_file.f_path.dentry && locks == 1); + locks--; +} +static void fput(struct file *f) +{ + assert(f->refs > 0); + f->refs--; +} +/* Leaves for the existing production parent-sync operation used on retry. */ +static struct dentry *dget_parent(struct dentry *d) +{ + assert(d == canonical.f_path.dentry); + roots++; + return directory.f_path.dentry; +} +static void dput(struct dentry *d) +{ + assert(d == directory.f_path.dentry && roots > 0); + roots--; +} +static struct file *dentry_open(const struct path *p, int flags, + const struct cred *cred) +{ + assert(p->mnt == &mount_token && p->dentry == directory.f_path.dentry); + assert(flags == (O_RDONLY | O_DIRECTORY) && cred == &credential); + if (stages[PARENT_OPEN]) + return ERR_PTR(-stages[PARENT_OPEN]); + directory.refs++; + return &directory; +} +#define pr_err(...) (++commit_warning) +#define pr_warn(...) (++cleanup_warning) +/* Existing C/Rust I/O ABI for testing the actual Catacomb reader and old writer. */ +void *sep_store_open_ro(const char *p) +{ + assert(fixture_path(p)); + if (stages[16]) + return ERR_PTR(-stages[16]); + if (!exists) + return ERR_PTR(-ENOENT); + canonical.refs++; + return &canonical; +} +void *sep_store_open_trunc(const char *p) +{ + (void)p; + canonical.len = 0; + exists = true; + canonical.refs++; + return &canonical; +} +void *sep_store_open(const char *p) +{ + return sep_store_open_trunc(p); +} +void *sep_store_open_host(const char *p, bool *created) +{ + *created = !exists; + return sep_store_open_trunc(p); +} +void *sep_store_open_block(const char *p, int w) +{ + (void)p; + (void)w; + abort(); +} +void sep_store_close(void *p) +{ + fput(p); +} +long long sep_store_size(void *p) +{ + return ((struct file *)p)->len; +} +long sep_store_read(void *p, long long off, void *buf, size_t n) +{ + struct file *f = p; + + if ((size_t)off >= f->len) + return 0; + if (n > f->len - off) + n = f->len - off; + memcpy(buf, f->bytes + off, n); + return n; +} +long sep_store_write(void *p, long long off, const void *buf, size_t n) +{ + loff_t pos = off; + + return kernel_write(p, buf, n, &pos); +} +int sep_store_sync(void *p) +{ + return vfs_fsync(p, 0); +} +int sep_random_bytes(void *p, size_t n) +{ + memset(p, 1, n); + return 0; +} diff --git a/tools/aurora-sep/tests/persistence/fixtures/atomic.c b/tools/aurora-sep/tests/persistence/fixtures/atomic.c new file mode 100644 index 00000000000000..05a76f54eec5d1 --- /dev/null +++ b/tools/aurora-sep/tests/persistence/fixtures/atomic.c @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +static const char original[] = "previous validated generation"; +static const char header[] = "0123456789abcdef"; +static const char body[] = "replacement opaque biometric generation"; +static const char path[] = "/var/lib/apple-sep-catacomb-user.bin"; +static int passed; +static void setup(void) +{ + fixture_reset(); + fixture_set(original, sizeof(original)); +} +static void unchanged(void) +{ + assert(exists && canonical.len == sizeof(original) && + !memcmp(canonical.bytes, original, sizeof(original))); + assert(!renames && !dir_syncs); +} +static void complete(void) +{ + assert(exists && canonical.len == sizeof(header) - 1 + sizeof(body)); + assert(!memcmp(canonical.bytes, header, sizeof(header) - 1)); + assert(!memcmp(canonical.bytes + sizeof(header) - 1, body, + sizeof(body))); +} +static int replace(void) +{ + return sep_store_replace(path, header, sizeof(header) - 1, body, + sizeof(body)); +} +static void done(const char *name) +{ + fixture_done(); + passed++; + printf("PASS %s\n", name); +} +int main(void) +{ + struct { + int stage, error; + const char *name; + } failures[] = { + { ALLOC, ENOMEM, "parent allocation" }, + { CRED, ENOMEM, "kernel credential allocation" }, + { PARENT_OPEN, EACCES, "parent open denied" }, + { PARENT_OPEN, ENOENT, "parent missing" }, + { TEMP_OPEN, ENOSPC, "temporary create ENOSPC" }, + { TEMP_OPEN, EROFS, "temporary create EROFS" }, + { TEMP_SYNC, EIO, "temporary fsync EIO" }, + { TEMP_SYNC, ENOSPC, "temporary fsync ENOSPC" }, + { MOUNT, EROFS, "mount write denied" }, + { RENAME_LOCK, ENOMEM, "rename lookup allocation" }, + { SECURITY_RENAME, EPERM, "path security rejects rename" }, + { RENAME, EIO, "vfs rename EIO" }, + { RENAME, EWOULDBLOCK, "vfs rename delegation" }, + { RENAME, EBUSY, "vfs rename mountpoint" }, + }; + for (size_t i = 0; i < sizeof(failures) / sizeof(*failures); i++) { + setup(); + fixture_fault(failures[i].stage, failures[i].error); + assert(replace() == -failures[i].error); + unchanged(); + done(failures[i].name); + } + setup(); + fixture_write_fault(0, EIO, 0); + assert(replace() == -EIO); + unchanged(); + assert(!temp_present); + done("header write EIO"); + setup(); + fixture_write_fault(7, ENOSPC, 0); + assert(replace() == -ENOSPC); + unchanged(); + done("partial header then ENOSPC"); + setup(); + fixture_write_fault(16, EIO, 0); + assert(replace() == -EIO); + unchanged(); + done("payload write EIO"); + setup(); + fixture_write_fault(22, EIO, 0); + assert(replace() == -EIO); + unchanged(); + done("partial payload then EIO"); + setup(); + fixture_write_fault(0, 0, 1); + assert(replace() == -EIO); + unchanged(); + done("zero-progress header"); + setup(); + fixture_write_fault(18, 0, 1); + assert(replace() == -EIO); + unchanged(); + done("zero-progress payload"); + setup(); + fixture_fault(TARGET_TYPE, 2); + assert(replace() == -EINVAL); + unchanged(); + done("directory target refused"); + setup(); + fixture_fault(TARGET_TYPE, 3); + assert(replace() == -EINVAL); + unchanged(); + done("symlink target refused"); + setup(); + fixture_fault(STALE_SOURCE, 1); + assert(replace() == -EINVAL); + unchanged(); + assert(temp_present && cleanup_warning); + done("source moved; cleanup does not remove reused name"); + for (int i = CLEANUP_LOCK; i <= UNLINK; i++) { + setup(); + fixture_write_fault(16, EIO, 0); + fixture_fault(i, EPERM); + assert(replace() == -EIO); + unchanged(); + assert(temp_present && cleanup_warning); + done(i == CLEANUP_LOCK ? "cleanup lookup failure" : + i == SECURITY_UNLINK ? "cleanup security failure" : + "cleanup unlink failure"); + } + setup(); + fixture_collisions(4); + assert(replace() == -EEXIST); + unchanged(); + assert(attempts == 4 && !unlinks && !temp_present); + done("exclusive-create collision exhaustion"); + setup(); + fixture_collisions(2); + assert(!replace()); + complete(); + assert(attempts == 3); + done("exclusive-create collision retry"); + setup(); + fixture_short(3); + assert(!replace()); + complete(); + assert(write_calls > 2 && !temp_present && temp_syncs == 1 && + dir_syncs == 1); + assert(strstr(events, "FRD")); + done("short writes loop to durable replacement"); + setup(); + fixture_fault(DIR_SYNC, EIO); + assert(replace() == -EIO); + complete(); + assert(renames == 1 && !unlinks && commit_warning); + done("directory fsync failure leaves complete new generation visible"); + setup(); + fixture_fault(DIR_SYNC, EIO); + assert(replace() == -EIO); + fixture_fault(DIR_SYNC, 0); + assert(!replace()); + complete(); + assert(renames == 2 && !temp_present); + done("retry after ambiguous directory fsync"); + fixture_reset(); + assert(!replace()); + complete(); + done("first creation durable"); + setup(); + assert(sep_store_replace("invalid", header, 16, body, sizeof(body)) == + -EINVAL); + unchanged(); + done("invalid path rejected before writes"); + printf("C cases: %d passed, 0 failed\n", passed); + return 0; +} diff --git a/tools/aurora-sep/tests/persistence/fixtures/atomic.rs b/tools/aurora-sep/tests/persistence/fixtures/atomic.rs new file mode 100644 index 00000000000000..78510ab34fc5da --- /dev/null +++ b/tools/aurora-sep/tests/persistence/fixtures/atomic.rs @@ -0,0 +1,254 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +const PRIVATE_TYPE_CATACOMB_MASTER: u8 = 0xf2; +const PRIVATE_TYPE_CATACOMB_OWNER: u8 = 0xf3; +const PRIVATE_TYPE_CATACOMB_USER: u8 = 0xf4; +extern "C" { + fn fixture_reset(); + fn fixture_done(); + fn fixture_fault(stage: i32, error: i32); + fn fixture_write_fault(at: usize, error: i32, zero: i32); + fn fixture_short(n: usize); + fn fixture_count(n: i32) -> i32; +} +struct Fixture; +impl Fixture { + fn new() -> Self { + unsafe { fixture_reset() }; + Self + } +} +impl Drop for Fixture { + fn drop(&mut self) { + unsafe { fixture_done() }; + } +} +fn fault(stage: i32, error: i32) { + unsafe { fixture_fault(stage, error) } +} +fn count(n: i32) -> i32 { + unsafe { fixture_count(n) } +} +fn user_read() -> Result>> { + catacomb::read_checked(PRIVATE_TYPE_CATACOMB_USER) +} +fn current() -> Vec { + user_read().unwrap().unwrap().to_vec() +} +fn original() { + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"original valid catacomb").unwrap(); +} +struct Status; +impl Status { + fn is_ok(&self) -> bool { + true + } +} +impl std::fmt::Display for Status { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ok") + } +} +struct Done { + status: Status, + payload: KVec, +} +struct SepData { + dev: (), + confirms: Cell, +} +impl SepData { + fn new() -> Self { + Self { + dev: (), + confirms: Cell::new(0), + } + } + fn sbio_transfer(&self, op: &sbio::Operation) -> Result { + let mut blob = vec![0xab; sbio::SBIO_SAVED_MIN]; + blob[8..12].copy_from_slice(&501i32.to_le_bytes()); + if op.0 { + self.confirms.set(self.confirms.get() + 1); + } + Ok(Done { + status: Status, + payload: KVec(blob), + }) + } +} +mod sbio { + #[derive(Clone, Copy)] + pub struct CatacombUser; + impl CatacombUser { + pub const OWNER: Self = Self; + pub fn value(&self) -> i32 { + 501 + } + } + pub struct SaveSelector; + impl SaveSelector { + pub fn new(_: CatacombUser) -> Self { + Self + } + } + pub struct Operation(pub bool); + pub fn sbio_save_catacomb(_: &SaveSelector) -> Operation { + Operation(false) + } + pub fn sbio_confirm_save(_: &SaveSelector) -> Operation { + Operation(true) + } + pub const SBIO_SAVED_MIN: usize = 0x22; + pub const SBIO_SAVED_MAX: usize = 0x4b000; + pub const SBIO_SAVED_USER_ID_AT: usize = 8; +} +fn save(s: &SepData) -> bool { + s.save_catacomb( + sbio::CatacombUser::OWNER, + PRIVATE_TYPE_CATACOMB_USER, + c"test user", + ) +} +#[test] +fn success_serialization_and_crc_round_trip() { + let _f = Fixture::new(); + original(); + let b = vec![0x55; 10001]; + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, &b).unwrap(); + assert_eq!(current(), b); +} +#[test] +fn maximum_size_round_trip() { + let _f = Fixture::new(); + let b = vec![0x89; 1 << 20]; + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, &b).unwrap(); + assert_eq!(current(), b); +} +#[test] +fn oversized_blob_preserves_previous() { + let _f = Fixture::new(); + original(); + let old = current(); + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, &vec![0; (1 << 20) + 1]), + Err(ENOSPC) + ); + assert_eq!(current(), old); +} +#[test] +fn invalid_kind_preserves_previous() { + let _f = Fixture::new(); + original(); + assert_eq!(catacomb::write(0, &[1]), Err(EINVAL)); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn header_error_preserves_valid_previous() { + let _f = Fixture::new(); + original(); + unsafe { fixture_write_fault(0, 5, 0) }; + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement"), + Err(EIO) + ); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn partial_payload_error_preserves_valid_previous() { + let _f = Fixture::new(); + original(); + unsafe { fixture_write_fault(18, 5, 0) }; + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement"), + Err(EIO) + ); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn temporary_fsync_error_preserves_valid_previous() { + let _f = Fixture::new(); + original(); + fault(9, 5); + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement"), + Err(EIO) + ); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn rename_error_preserves_valid_previous() { + let _f = Fixture::new(); + original(); + fault(8, 5); + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement"), + Err(EIO) + ); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn directory_fsync_error_keeps_complete_visible_replacement() { + let _f = Fixture::new(); + original(); + fault(10, 5); + assert_eq!( + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement"), + Err(EIO) + ); + assert_eq!(current(), b"replacement"); +} +#[test] +fn short_write_round_trip() { + let _f = Fixture::new(); + unsafe { fixture_short(3) }; + catacomb::write(PRIVATE_TYPE_CATACOMB_USER, b"replacement").unwrap(); + assert_eq!(current(), b"replacement"); +} +#[test] +fn save_confirms_only_after_durable_replacement() { + let _f = Fixture::new(); + original(); + let s = SepData::new(); + assert!(save(&s)); + assert_eq!(s.confirms.get(), 1); + assert_eq!(count(2), 2); +} +#[test] +fn save_with_payload_error_keeps_old_and_never_confirms() { + let _f = Fixture::new(); + original(); + unsafe { fixture_write_fault(18, 5, 0) }; + let s = SepData::new(); + assert!(!save(&s)); + assert_eq!(s.confirms.get(), 0); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn save_with_file_fsync_error_keeps_old_and_never_confirms() { + let _f = Fixture::new(); + original(); + fault(9, 5); + let s = SepData::new(); + assert!(!save(&s)); + assert_eq!(s.confirms.get(), 0); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn save_with_rename_error_keeps_old_and_never_confirms() { + let _f = Fixture::new(); + original(); + fault(8, 5); + let s = SepData::new(); + assert!(!save(&s)); + assert_eq!(s.confirms.get(), 0); + assert_eq!(current(), b"original valid catacomb"); +} +#[test] +fn save_with_directory_fsync_error_never_confirms() { + let _f = Fixture::new(); + original(); + fault(10, 5); + let s = SepData::new(); + assert!(!save(&s)); + assert_eq!(s.confirms.get(), 0); + assert_eq!(current().len(), sbio::SBIO_SAVED_MIN); +} diff --git a/tools/aurora-sep/tests/persistence/fixtures/common.rs b/tools/aurora-sep/tests/persistence/fixtures/common.rs new file mode 100644 index 00000000000000..a79c394eee48ff --- /dev/null +++ b/tools/aurora-sep/tests/persistence/fixtures/common.rs @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +// Host-only dependencies for unchanged production methods. No hardware or files. +#![allow(dead_code, non_upper_case_globals, non_snake_case, unused_imports)] +extern crate self as kernel; +use std::cell::{Cell, RefCell, RefMut}; +use std::ffi::CStr; +use std::ops::{Deref, DerefMut}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Error(i32); +impl Error { + pub fn from_errno(n: i32) -> Self { + assert!((-4095..0).contains(&n)); + Self(n) + } + pub unsafe fn from_errno_unchecked(n: i32) -> Self { + Self(n) + } +} +pub type Result = std::result::Result; +pub const EPERM: Error = Error(-1); +pub const ENOENT: Error = Error(-2); +pub const EIO: Error = Error(-5); +pub const ENOMEM: Error = Error(-12); +pub const EACCES: Error = Error(-13); +pub const EEXIST: Error = Error(-17); +pub const ENOTDIR: Error = Error(-20); +pub const EINVAL: Error = Error(-22); +pub const EMFILE: Error = Error(-24); +pub const ENOSPC: Error = Error(-28); +pub const EROFS: Error = Error(-30); +pub const ENODATA: Error = Error(-61); +pub const ELOOP: Error = Error(-40); +pub const ENOTSYNC: Error = Error(-522); +pub const ENODEV: Error = Error(-19); +pub const GFP_KERNEL: u32 = 0; +pub trait AsCharPtr { + fn as_char_ptr(&self) -> *const std::ffi::c_char; +} +impl AsCharPtr for CStr { + fn as_char_ptr(&self) -> *const std::ffi::c_char { + self.as_ptr() + } +} +pub mod ffi { + pub use std::ffi::*; +} +pub mod bindings { + pub unsafe fn IS_ERR(p: *const std::ffi::c_void) -> bool { + (p as usize) >= usize::MAX - 4094 + } + pub unsafe fn PTR_ERR(p: *const std::ffi::c_void) -> isize { + p as isize + } +} +pub mod prelude { + pub use crate::{ + AsCharPtr, Error, KVec, Result, EACCES, EEXIST, EINVAL, EIO, ELOOP, EMFILE, ENODATA, + ENODEV, ENOENT, ENOMEM, ENOSPC, ENOTDIR, ENOTSYNC, EPERM, EROFS, GFP_KERNEL, + }; + pub use std::ffi::{c_char, c_int, c_long, c_void, CStr}; +} +pub struct KVec(Vec); +impl KVec { + pub const fn new() -> Self { + Self(Vec::new()) + } + pub fn push(&mut self, value: T, _: u32) -> Result { + self.0.push(value); + Ok(()) + } + pub fn with_capacity(n: usize, _: u32) -> Result { + let mut v = Vec::new(); + v.try_reserve(n).map_err(|_| ENOMEM)?; + Ok(Self(v)) + } +} +impl KVec { + pub fn resize(&mut self, n: usize, v: T, _: u32) -> Result { + self.0 + .try_reserve(n.saturating_sub(self.0.len())) + .map_err(|_| ENOMEM)?; + self.0.resize(n, v); + Ok(()) + } + pub fn extend_from_slice(&mut self, s: &[T], _: u32) -> Result { + self.0.try_reserve(s.len()).map_err(|_| ENOMEM)?; + self.0.extend_from_slice(s); + Ok(()) + } +} +impl Deref for KVec { + type Target = [T]; + fn deref(&self) -> &[T] { + &self.0 + } +} +impl DerefMut for KVec { + fn deref_mut(&mut self) -> &mut [T] { + &mut self.0 + } +} +pub struct Secret(pub KVec); +impl Deref for Secret { + type Target = [u8]; + fn deref(&self) -> &[u8] { + &self.0 + } +} +impl Drop for Secret { + fn drop(&mut self) { + self.0.fill(0); + } +} +macro_rules! static_assert { + ($v:expr) => { + const _: () = assert!($v); + }; +} +macro_rules! dev_err { ($dev:expr, $($arg:tt)*) => { let _ = (&$dev, format_args!($($arg)*)); }; } +macro_rules! dev_warn { ($dev:expr, $($arg:tt)*) => { let _ = (&$dev, format_args!($($arg)*)); }; } + +pub struct FakeMutex(RefCell); +impl FakeMutex { + fn lock(&self) -> RefMut<'_, T> { + self.0.borrow_mut() + } +} +pub mod sks { + #[derive(Clone, Copy)] + pub struct KeyBagHandle(pub i32); +} +pub mod module_parameters { + pub struct Parameter(u8); + impl Parameter { + pub fn value(&self) -> &u8 { + &self.0 + } + } + pub static refkey_v2: Parameter = Parameter(0); +} +pub struct SepData { + dev: (), + machine_refkey: FakeMutex>, + creates: Cell, + caches: Cell, + valid_blob: Cell, + create_ok: Cell, +} +impl SepData { + fn new() -> Self { + Self { + dev: (), + machine_refkey: FakeMutex(RefCell::new(None)), + creates: Cell::new(0), + caches: Cell::new(0), + valid_blob: Cell::new(true), + create_ok: Cell::new(true), + } + } + fn sks_create_refkey(&self, _: sks::KeyBagHandle, _: &[u8]) -> Option> { + self.creates.set(self.creates.get() + 1); + self.create_ok.get().then(|| KVec(vec![1, 2, 3])) + } + fn cache_machine_refkey(&self, _: KVec) -> bool { + self.caches.set(self.caches.get() + 1); + if self.valid_blob.get() { + *self.machine_refkey.lock() = Some(()); + true + } else { + false + } + } +} +extern "C" { + fn test_reset(); + fn test_error(n: i32); + fn test_read_error(n: i32); + fn test_bytes(p: *const u8, n: usize); + fn test_flip(off: usize); + fn test_set32(off: usize, n: u32); + fn test_clear_counts(); + fn test_writes() -> usize; + fn test_read_opens() -> usize; + fn test_closes() -> usize; +} +fn reset() { + unsafe { + test_reset(); + } +} +fn error(n: i32) { + unsafe { + test_error(n); + } +} +fn bytes(v: &[u8]) { + unsafe { + test_bytes(v.as_ptr(), v.len()); + } +} +fn writes() -> usize { + unsafe { test_writes() } +} + +// Production error::from_err_ptr, StoreFile methods, keybag parser, and ref-key +// method are inserted by test_faults.py, with provenance hashes in the report. diff --git a/tools/aurora-sep/tests/persistence/persistence-tests.py b/tools/aurora-sep/tests/persistence/persistence-tests.py new file mode 100644 index 00000000000000..436233b9df7501 --- /dev/null +++ b/tools/aurora-sep/tests/persistence/persistence-tests.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Fault tests of current SEP production sources; no hardware, root or network.""" + + +import argparse + + +import hashlib + + +import json + + +import os + + +from pathlib import Path + + +import re + + +import shutil + + +import subprocess + + +HERE = Path(__file__).resolve().parent + + +TREE = HERE.parents[3] + + +APPLE = TREE / 'drivers/soc/apple' + + +FIXTURES = HERE / 'fixtures' + + +def function(text, signature): + start = text.index(signature) + end = text.index('{', start) + 1 + depth = 1 + while depth: + depth += (text[end] == '{') - (text[end] == '}') + end += 1 + return text[start:end] + + +def replace_once(text, old, new): + if text.count(old) != 1: + raise RuntimeError('Mutation no longer targets exactly one production site: ' + old) + return text.replace(old, new) + + +def fixture(name): + return (FIXTURES / name).read_text() + + +def command(argv, log, env=None): + result = subprocess.run(list(map(str, argv)), capture_output=True, text=True, + timeout=120, env=env) + log.write_text(result.stdout + result.stderr) + return result + + +def require_compile(result, log): + if result.returncode: + raise RuntimeError('Compilation failed; never credited as a caught mutant: ' + str(log)) + + +def c_build(source, output, binary=False): + c = output.with_suffix('.c') + c.write_text(source) + # Host fixture only: ASan/UBSan provide instrumentation without a distro + # stack-protector ABI dependency when linking with an older Rust compiler. + flags = [os.environ.get('CC', 'cc'), '-std=gnu11', '-Wall', '-Wextra', '-Werror', + '-Wno-misleading-indentation', '-fsanitize=address,undefined', + '-fno-omit-frame-pointer', '-fno-stack-protector', '-g'] + log = output.with_suffix('.compile.log') + result = command(flags + ([] if binary else ['-c']) + [c, '-o', output], log) + require_compile(result, log) + + +def runtime_environment(linked_c): + env = os.environ.copy() + env['ASAN_OPTIONS'] = 'detect_leaks=0:abort_on_error=1' + env['UBSAN_OPTIONS'] = 'halt_on_error=1:print_stacktrace=1' + if linked_c: + cc = os.environ.get('CC', 'cc') + env['LD_PRELOAD'] = subprocess.check_output( + [cc, '-print-file-name=libasan.so'], text=True).strip() + return env + + +def rust_run(code, destination, expected, test_count, obj=None, required_failure=None): + source = destination / 'harness.rs' + source.write_text(code) + binary = destination / 'tests' + log = destination / 'compile-rust.log' + args = [os.environ.get('RUSTC', 'rustc'), '--edition=2021', '--test', source, '-o', binary] + if obj: + args += ['-C', 'link-arg=' + str(obj), '-C', 'link-arg=-lasan', '-C', 'link-arg=-lubsan'] + require_compile(command(args, log), log) + result = command([binary, '--test-threads=1', '--nocapture'], destination / 'tests.log', + runtime_environment(bool(obj))) + match = re.search(r'test result: (\w+)\. (\d+) passed; (\d+) failed; (\d+) ignored; (\d+) measured; (\d+) filtered out;', result.stdout) + if not match: + raise RuntimeError('Runtime failure without test summary; not a caught mutant: ' + str(destination)) + passed, failed = int(match[2]), int(match[3]) + if passed + failed != test_count or any(int(match[i]) for i in (4, 5, 6)): + raise RuntimeError('Missing, ignored or filtered tests: ' + str(destination)) + failures = re.findall(r'^test (\S+) \.\.\. FAILED$', result.stdout, re.M) + if expected == 'pass': + if failed or result.returncode or passed == 0: + raise RuntimeError('Production regression failed: ' + str(destination)) + elif result.returncode != 101 or not failed or not any(required_failure in n for n in failures): + raise RuntimeError('Mutation did not produce the required semantic failure: ' + str(destination)) + return {'passed': passed, 'failed': failed, 'failed_tests': failures, + 'expected': expected, 'returncode': result.returncode, + 'compiled': True, 'harness_sha256': hashlib.sha256(code.encode()).hexdigest()} + + +def error_functions(): + error = (TREE / 'rust/kernel/error.rs').read_text() + return ('pub mod error { use crate::prelude::*; use crate::bindings;\n' + + function(error, 'pub fn from_err_ptr') + '\n' + + function(error, 'pub fn to_result(') + '\n}\n') + + +def shim_module(apple): + text = (apple / 'shim.rs').read_text() + return 'mod shim {\n' + text[:text.index('extern "C" {', text.index('impl Drop for StoreFile'))] + '\n}\n' + + +def base_common(): + return fixture('common.rs').split('pub struct FakeMutex')[0] + + +def crc_module(apple): + return ('mod store { pub const MAX_VALUE:usize=4096;\n' + + function((apple / 'store.rs').read_text(), 'pub(crate) const fn crc16_ccitt_false(') + '\n}\n') + + +def source_module(name, path): + return '#[path=' + json.dumps(str(path)) + '] mod ' + name + ';\n' + + +def atomic_c(apple): + c = (apple / 'store_shim.c').read_text() + support = ''' +#define min_t(t,a,b) ((t)(a)<(t)(b)?(t)(a):(t)(b)) +static struct inode *d_inode(struct dentry *d) { return &d->inode; } +static loff_t i_size_read(struct inode *i) { (void)i;abort(); } +static void *kmalloc(size_t n,int f) { (void)n;(void)f;abort(); } +static ssize_t kernel_read(struct file*f,void*b,size_t n,loff_t*p) { (void)f;(void)b;(void)n;(void)p;abort(); } +''' + signatures = ['static int store_write_all(', 'static int store_copy_tail(', + 'static int store_remove_temp(', 'static int store_replace(', 'int sep_store_replace(', 'int sep_store_create(', 'int sep_store_sync_parent('] + c = fixture('atomic-vfs.h') + support + '\n'.join(function(c, s) for s in signatures) + return c + + +def atomic_suite(apple, output): + c = atomic_c(apple) + obj = output / 'vfs.o' + c_build(c, obj) + if output.name == 'production': + binary = output / 'boundaries' + c_build(c + fixture('atomic.c'), binary, binary=True) + result = command([binary], output / 'boundaries.log', runtime_environment(False)) + if result.returncode or 'C cases: 33 passed, 0 failed' not in result.stdout: + raise RuntimeError('Atomic C boundary regression failed') + common = base_common().replace('let _ = (&$dev, format_args!($($arg)*));', 'let _ = &$dev;') + code = common + error_functions() + shim_module(apple) + crc_module(apple) + reader = (apple / 'catacomb.rs').read_text() + atomic = fixture('atomic.rs').replace('catacomb::read_checked(PRIVATE_TYPE_CATACOMB_USER)', + 'catacomb::read(PRIVATE_TYPE_CATACOMB_USER)' if 'fn read(kind: u8) -> Result<' in reader + else 'Ok(catacomb::read(PRIVATE_TYPE_CATACOMB_USER))') + code += source_module('catacomb', apple / 'catacomb.rs') + atomic + code += '\nimpl SepData{\n' + function((apple / 'sbio.rs').read_text(), 'fn save_catacomb(') + '\n}\n' + return code, obj + + +SUITES = {'atomic': atomic_suite} + +TEST_COUNTS = {'atomic': 15} + +MUTATIONS = {'atomic': ('catacomb.rs', 'shim::replace_file(path, &head, blob)', 'let file = shim::StoreFile::open_trunc(path)?;\nfile.write_all(0, &head)?;\nfile.write_all(HEADER as u64, blob)?;\nfile.sync()', 'partial_payload_error_preserves')} + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--suite', choices=SUITES, required=True) + parser.add_argument('--output', type=Path, required=True) + args = parser.parse_args() + args.output = args.output.resolve() + args.output.mkdir(parents=True, exist_ok=False) + names = [p for p in APPLE.iterdir() if p.is_file() and p.suffix in ('.rs', '.c', '.h')] + hashes = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in names} + report = {'suite': args.suite, 'hardware_access': False, 'production_source_hashes': hashes, 'variants': {}} + for variant in ['production', 'negative-control']: + output = args.output / variant + output.mkdir() + source = output / 'source' + source.mkdir() + for p in names: shutil.copy2(p, source / p.name) + if variant == 'negative-control': + name, old, new, failure = MUTATIONS[args.suite] + p = source / name + p.write_text(replace_once(p.read_text(), old, new)) + code, obj = SUITES[args.suite](source, output) + report['variants'][variant] = rust_run(code, output, 'pass' if variant == 'production' else 'caught', + TEST_COUNTS[args.suite], obj, None if variant == 'production' else failure) + if hashes != {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in names}: + raise RuntimeError('Production source tree changed during the test') + report['status'] = 'PASS' + (args.output / 'validation.json').write_text(json.dumps(report, indent=2) + '\n') + print(json.dumps({'suite': args.suite, 'status': 'PASS', 'variants': report['variants']}, indent=2)) + + +if __name__ == '__main__': + main() From c47d11b2e46c41333245307dce506ed65a1a3303 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 28 Oct 2026 00:50:44 -0400 Subject: [PATCH 2/2] soc: apple: sep: defer unused publication paths to their callers Keep the catacomb replacement primitive limited to full-file writes. Move header-tail copying and exclusive destination creation out of this change, trim their fixture stubs, and document temporary files left by a crash before rename. Signed-off-by: Matt --- drivers/soc/apple/shim.h | 3 - drivers/soc/apple/store_shim.c | 89 ++----------------- tools/aurora-sep/tests/persistence/README.md | 6 +- .../tests/persistence/persistence-tests.py | 14 +-- 4 files changed, 16 insertions(+), 96 deletions(-) diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index d30e2140b0a7ee..51b7cfa3a48014 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -44,11 +44,8 @@ void sep_hwrng_unregister(void *mem); void *sep_store_open(const char *path); void *sep_store_open_host(const char *path, bool *created); void *sep_store_open_trunc(const char *path); -int sep_store_replace_header(void **handle, const char *path, - const void *head, size_t head_len, u64 expected_size); int sep_store_replace(const char *path, const void *head, size_t head_len, const void *body, size_t body_len); -int sep_store_create(const char *path, const void *body, size_t body_len); void *sep_store_open_ro(const char *path); void *sep_store_open_block(const char *path, int writable); void sep_store_close(void *handle); diff --git a/drivers/soc/apple/store_shim.c b/drivers/soc/apple/store_shim.c index caf50211546349..73b776f7cdbbc9 100644 --- a/drivers/soc/apple/store_shim.c +++ b/drivers/soc/apple/store_shim.c @@ -101,42 +101,6 @@ static int store_write_all(struct file *file, const void *buf, size_t len, return 0; } -/* Stream the unchanged part of a host store without a multi-megabyte allocation. */ -static int store_copy_tail(struct file *to, struct file *from, loff_t offset, - loff_t size) -{ - loff_t out = offset; - void *buf; - int err = 0; - - if (size < offset) - return -EINVAL; - if (i_size_read(file_inode(from)) != size) - return -ESTALE; - buf = kmalloc(32768, GFP_KERNEL); - if (!buf) - return -ENOMEM; - while (offset < size) { - ssize_t n = kernel_read(from, buf, min_t(loff_t, size - offset, 32768), &offset); - - if (n < 0) { - err = n; - break; - } - if (!n) { - err = -EIO; - break; - } - err = store_write_all(to, buf, n, &out); - if (err) - break; - } - if (!err && i_size_read(file_inode(from)) != size) - err = -ESTALE; - kfree(buf); - return err; -} - /* Remove only the temporary dentry we opened, never a name reused by another task. */ static int store_remove_temp(struct file *dir, struct file *temp) { @@ -167,8 +131,7 @@ static int store_remove_temp(struct file *dir, struct file *temp) * directory fsync failure returns an error but cannot safely undo that rename. */ static int store_replace(const char *path, const void *head, size_t head_len, - const void *body, size_t body_len, void **handle, - loff_t expected_size, bool exclusive) + const void *body, size_t body_len) { const char *base = strrchr(path, '/'); const struct cred *old; @@ -212,7 +175,7 @@ static int store_replace(const char *path, const void *head, size_t head_len, snprintf(temporary, sizeof(temporary), ".sep-%016llx%016llx.tmp", (unsigned long long)nonce[0], (unsigned long long)nonce[1]); temp = file_open_root(&dir->f_path, temporary, - (handle ? O_RDWR : O_WRONLY) | O_CREAT | O_EXCL | O_NOFOLLOW | O_LARGEFILE, 0600); + O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_LARGEFILE, 0600); if (PTR_ERR_OR_ZERO(temp) != -EEXIST) break; } @@ -222,12 +185,8 @@ static int store_replace(const char *path, const void *head, size_t head_len, goto out_dir; } err = store_write_all(temp, head, head_len, &pos); - if (!err) { - if (handle) - err = store_copy_tail(temp, *handle, head_len, expected_size); - else - err = store_write_all(temp, body, body_len, &pos); - } + if (!err) + err = store_write_all(temp, body, body_len, &pos); if (!err) err = vfs_fsync(temp, 0); if (err) @@ -243,12 +202,7 @@ static int store_replace(const char *path, const void *head, size_t head_len, name = (struct qstr)QSTR_INIT(base + 1, strlen(base + 1)); err = start_renaming_dentry(&rd, 0, temp->f_path.dentry, &name); if (!err) { - if (exclusive && d_is_positive(rd.new_dentry)) - err = -EEXIST; - else if (handle && (!d_is_positive(rd.new_dentry) || - d_inode(rd.new_dentry) != file_inode((struct file *)*handle))) - err = -ESTALE; - else if (d_is_positive(rd.new_dentry) && !d_is_reg(rd.new_dentry)) + if (d_is_positive(rd.new_dentry) && !d_is_reg(rd.new_dentry)) err = -EINVAL; else err = security_path_rename(&dir->f_path, rd.old_dentry, @@ -270,18 +224,7 @@ static int store_replace(const char *path, const void *head, size_t head_len, if (cleanup) pr_warn("apple-sep: temporary store cleanup failed (%d)\n", cleanup); } - /* Rename committed the new inode even when the parent fsync failed. Keep - * future reads/writes on that inode; never continue on an unlinked old one. - */ - if (renamed && handle) { - struct file *previous = *handle; - - *handle = temp; - temp = NULL; - fput(previous); - } - if (temp) - fput(temp); + fput(temp); out_dir: fput(dir); out_creds: @@ -292,25 +235,7 @@ static int store_replace(const char *path, const void *head, size_t head_len, int sep_store_replace(const char *path, const void *head, size_t head_len, const void *body, size_t body_len) { - return store_replace(path, head, head_len, body, body_len, NULL, 0, false); -} - -/* Initial key material must not replace a file published by another caller - * after the absence check. The destination check shares rename's lock. */ -int sep_store_create(const char *path, const void *body, size_t body_len) -{ - return store_replace(path, NULL, 0, body, body_len, NULL, 0, true); -} - -/* Caller exclusively owns *handle; on a committed rename its reference moves - * to the replacement inode, including when directory fsync reports an error. - */ -int sep_store_replace_header(void **handle, const char *path, - const void *head, size_t head_len, u64 expected_size) -{ - if (expected_size > LLONG_MAX) - return -EINVAL; - return store_replace(path, head, head_len, NULL, 0, handle, expected_size, false); + return store_replace(path, head, head_len, body, body_len); } /* diff --git a/tools/aurora-sep/tests/persistence/README.md b/tools/aurora-sep/tests/persistence/README.md index 8665ff82fd74f4..23a490b5ded84d 100644 --- a/tools/aurora-sep/tests/persistence/README.md +++ b/tools/aurora-sep/tests/persistence/README.md @@ -1,5 +1,9 @@ # SEP host-file publication fault tests -The atomic suite compiles the production C replacement primitive, Rust StoreFile wrapper and catacomb writer against a synthetic VFS. It checks short writes, allocation/open/security/rename/fsync failures, preservation of the old generation before rename, and a complete visible generation after a failed directory fsync. The C boundary tests reject symlinks and non-regular destinations, exclusive-publication races, and unsafe cleanup. The suite also compiles a deliberate regression and requires its named behavioral assertion to fail. No real files, SEP state or hardware are opened. +The atomic suite compiles the production C replacement primitive, Rust StoreFile wrapper and catacomb writer against a synthetic VFS. It checks short writes, allocation/open/security/rename/fsync failures, preservation of the old generation before rename, and a complete visible generation after a failed directory fsync. The C boundary tests reject symlinks and non-regular destinations, temporary-name collisions, and unsafe cleanup. The suite also compiles a deliberate regression and requires its named behavioral assertion to fail. No real files, SEP state or hardware are opened. Run `python3 -I tools/aurora-sep/tests/persistence/persistence-tests.py --suite atomic --output /tmp/sep-atomic`. CI pins Rust 1.85.0. These host fixtures do not establish power-loss durability on physical storage or SEP anti-replay safety. + +A crash after temporary-file creation and before rename can leave a `.sep-.tmp` file in `/var/lib`, up to 1 MiB per interrupted catacomb write. Ordinary error paths attempt to unlink their own temporary file; there is no automatic cleanup after a crash. A later write uses a fresh exclusive temporary name and never adopts the leftover. These files can contain biometric material: inspect and remove confirmed stale temporary files only while SEP persistence is inactive. Successful publication leaves no temporary file. + +Only full-file replacement is implemented here. Header-tail copying and exclusive destination creation belong with their later callers and tests; this suite does not claim to exercise those operations. diff --git a/tools/aurora-sep/tests/persistence/persistence-tests.py b/tools/aurora-sep/tests/persistence/persistence-tests.py index 436233b9df7501..da39968cd00fd6 100644 --- a/tools/aurora-sep/tests/persistence/persistence-tests.py +++ b/tools/aurora-sep/tests/persistence/persistence-tests.py @@ -150,16 +150,10 @@ def source_module(name, path): def atomic_c(apple): c = (apple / 'store_shim.c').read_text() - support = ''' -#define min_t(t,a,b) ((t)(a)<(t)(b)?(t)(a):(t)(b)) -static struct inode *d_inode(struct dentry *d) { return &d->inode; } -static loff_t i_size_read(struct inode *i) { (void)i;abort(); } -static void *kmalloc(size_t n,int f) { (void)n;(void)f;abort(); } -static ssize_t kernel_read(struct file*f,void*b,size_t n,loff_t*p) { (void)f;(void)b;(void)n;(void)p;abort(); } -''' - signatures = ['static int store_write_all(', 'static int store_copy_tail(', - 'static int store_remove_temp(', 'static int store_replace(', 'int sep_store_replace(', 'int sep_store_create(', 'int sep_store_sync_parent('] - c = fixture('atomic-vfs.h') + support + '\n'.join(function(c, s) for s in signatures) + signatures = ['static int store_write_all(', 'static int store_remove_temp(', + 'static int store_replace(', 'int sep_store_replace(', + 'int sep_store_sync_parent('] + c = fixture('atomic-vfs.h') + '\n'.join(function(c, s) for s in signatures) return c