Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions drivers/soc/apple/catacomb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KVec<u8>> {
Expand Down
5 changes: 5 additions & 0 deletions drivers/soc/apple/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions drivers/soc/apple/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
233 changes: 233 additions & 0 deletions drivers/soc/apple/store_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fs_struct.h>
#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/random.h>
#include <linux/sched.h>
#include <linux/security.h>
#include <linux/slab.h>
#include <linux/types.h>

#include "shim.h"
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions tools/aurora-ci/host-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tools/aurora-sep/tests/persistence/README.md
Original file line number Diff line number Diff line change
@@ -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.
Loading