diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index aa4a7bd38..783635dac 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -7,6 +7,7 @@ The current development method is [Human-Directed Agentic Engineering (HDAE)](RE | Model | What it did | |---|---| +| OpenAI Codex | Descriptor-retained persistence roots, component-wise symlink refusal, exclusive temporary files, owner/type/link validation, and static regression controls for machine identity, composition, Cortex, J-Space, generation and emergency-halt stores. | | Grok 4.7 (via Cursor Agent) | Merged main `fd3751b31` (#1020, carrying #1045 and #1050) into #1052. The only conflict was the generated root `.crumb` (child `last_commit` stamps, the `tests` digest, and the root digest). Recompiled with the crumb.lock tool (`d0f0b95d`); `crumb verify .` current. The #525/#962 pins were left as committed: `src/checker2/c2_check.c`, `src/compiler/osc_ir.c` and `src/compiler/osc_ir.h` still hash to the SHA-256s in `ACCEPTED`. | | Claude Opus 5.5 (via omp) | #1020 (actions/cache-4.3.0 ->6.1.0): merged main `d3d1b1bc` and recompiled the root crumb with the crumb.lock tool (`d0f0b95d`). The root crumb's source tree covers `.github/workflows/`, so the bump alone left it stale and `AIEN Checks` failed at `crumb verify .` (`1 of 188 crumbs stale`). | | Claude Opus 5.5 (via omp) | #1018 (actions/upload-artifact-4 ->7): merged main `d3d1b1bc` and recompiled the root crumb with the crumb.lock tool (`d0f0b95d`). The root crumb's source tree covers `.github/workflows/`, so the bump alone left it stale and `AIEN Checks` failed at `crumb verify .` (`1 of 188 crumbs stale`). | diff --git a/src/omega_fs.h b/src/omega_fs.h new file mode 100644 index 000000000..a6348d799 --- /dev/null +++ b/src/omega_fs.h @@ -0,0 +1,276 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Descriptor-relative filesystem confinement for runtime persistence. + */ +#ifndef OMEGA_FS_H +#define OMEGA_FS_H + +#include "omega_path_check.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define OMEGA_FS_PATH_MAX 4096u +#define OMEGA_FS_NAME_MAX 255u + +static inline int omega_fs_dupfd(int fd) { + return fcntl(fd, F_DUPFD_CLOEXEC, 0); +} + +/* Walk a directory path one component at a time. O_NOFOLLOW therefore applies + * to every component, not only the final one. A created directory is always + * reopened and validated through the same path. */ +static inline int omega_fs_dir_open_at(int base_fd, const char *path, int create, + mode_t mode, int secure) { + char checked[OMEGA_FS_PATH_MAX]; + if (omega_path_copy(checked, sizeof checked, path) != 0) { + errno = EINVAL; + return -1; + } + int absolute = checked[0] == '/'; + if (absolute && base_fd != AT_FDCWD) { + errno = EINVAL; + return -1; + } + int fd = absolute ? open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC) + : base_fd == AT_FDCWD + ? open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC) + : omega_fs_dupfd(base_fd); + if (fd < 0) return -1; + + const char *p = checked; + while (*p) { + while (*p == '/') p++; + if (!*p) break; + const char *q = p; + while (*q && *q != '/') q++; + size_t n = (size_t)(q - p); + if (n > OMEGA_FS_NAME_MAX) { + close(fd); + errno = ENAMETOOLONG; + return -1; + } + char name[OMEGA_FS_NAME_MAX + 1]; + memcpy(name, p, n); + name[n] = 0; + const char *after = q; + while (*after == '/') after++; + int last = *after == 0; + if (strcmp(name, ".") != 0) { + int next = openat(fd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (next < 0 && errno == ENOENT && create && last) { + if (mkdirat(fd, name, mode) != 0 && errno != EEXIST) { + close(fd); + return -1; + } + next = openat(fd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + } + if (next < 0) { + close(fd); + return -1; + } + close(fd); + fd = next; + } + p = q; + } + + struct stat st; + if (fstat(fd, &st) != 0) { + int e = errno; + close(fd); + errno = e; + return -1; + } + if (!S_ISDIR(st.st_mode) || + (secure && (st.st_uid != geteuid() || (st.st_mode & 022) != 0))) { + close(fd); + errno = EPERM; + return -1; + } + return fd; +} + +static inline int omega_fs_root_open(const char *path, int create, mode_t mode) { + return omega_fs_dir_open_at(AT_FDCWD, path, create, mode, 1); +} + +static inline int omega_fs_subdir_open(int root_fd, const char *path, int create, mode_t mode) { + if (!path || path[0] == '/') { + errno = EINVAL; + return -1; + } + return omega_fs_dir_open_at(root_fd, path, create, mode, 1); +} + +/* Resolve a file's parent without following any directory symlink. */ +static inline int omega_fs_parent_open_at(int base_fd, const char *path, char *leaf, + size_t leaf_cap) { + char checked[OMEGA_FS_PATH_MAX]; + if (!leaf || leaf_cap == 0 || omega_path_copy(checked, sizeof checked, path) != 0 || + (base_fd != AT_FDCWD && checked[0] == '/')) { + errno = EINVAL; + return -1; + } + size_t len = strlen(checked); + if (!len || checked[len - 1] == '/') { + errno = EINVAL; + return -1; + } + char *slash = strrchr(checked, '/'); + const char *name = slash ? slash + 1 : checked; + size_t n = strlen(name); + if (!n || n > OMEGA_FS_NAME_MAX || n + 1 > leaf_cap || strcmp(name, ".") == 0) { + errno = n > OMEGA_FS_NAME_MAX ? ENAMETOOLONG : EINVAL; + return -1; + } + memcpy(leaf, name, n + 1); + if (!slash) return omega_fs_dir_open_at(base_fd, ".", 0, 0, 0); + if (slash == checked) slash[1] = 0; + else *slash = 0; + return omega_fs_dir_open_at(base_fd, checked, 0, 0, 0); +} + +static inline int omega_fs_open_regular_at(int root_fd, const char *path, int flags, + mode_t mode) { + char leaf[OMEGA_FS_NAME_MAX + 1]; + int pfd = omega_fs_parent_open_at(root_fd, path, leaf, sizeof leaf); + if (pfd < 0) return -1; + int actual = (flags & ~O_TRUNC) | O_NOFOLLOW | O_CLOEXEC; + int fd = openat(pfd, leaf, actual, mode); + int saved = errno; + close(pfd); + if (fd < 0) { + errno = saved; + return -1; + } + struct stat st; + if (fstat(fd, &st) != 0) { + int e = errno; + close(fd); + errno = e; + return -1; + } + if (!S_ISREG(st.st_mode) || st.st_uid != geteuid() || st.st_nlink != 1 || + (st.st_mode & 022) != 0) { + close(fd); + errno = EPERM; + return -1; + } + /* Validate type, ownership, link count and mode before destructive truncation. */ + if ((flags & O_TRUNC) && ftruncate(fd, 0) != 0) { + saved = errno; + close(fd); + errno = saved; + return -1; + } + return fd; +} + +static inline int omega_fs_open_regular(const char *path, int flags, mode_t mode) { + return omega_fs_open_regular_at(AT_FDCWD, path, flags, mode); +} + +static inline int omega_fs_unlink_at(int root_fd, const char *path, int flags) { + char leaf[OMEGA_FS_NAME_MAX + 1]; + int pfd = omega_fs_parent_open_at(root_fd, path, leaf, sizeof leaf); + if (pfd < 0) return -1; + int rc = unlinkat(pfd, leaf, flags); + int saved = errno; + close(pfd); + errno = saved; + return rc; +} + +static inline int omega_fs_rename_at(int root_fd, const char *old_path, const char *new_path) { + char old_leaf[OMEGA_FS_NAME_MAX + 1], new_leaf[OMEGA_FS_NAME_MAX + 1]; + int old_fd = omega_fs_parent_open_at(root_fd, old_path, old_leaf, sizeof old_leaf); + if (old_fd < 0) return -1; + int new_fd = omega_fs_parent_open_at(root_fd, new_path, new_leaf, sizeof new_leaf); + if (new_fd < 0) { + close(old_fd); + return -1; + } + int rc = renameat(old_fd, old_leaf, new_fd, new_leaf); + int saved = errno; + close(new_fd); + close(old_fd); + errno = saved; + return rc; +} + +static inline int omega_fs_link_at(int root_fd, const char *old_path, const char *new_path) { + char old_leaf[OMEGA_FS_NAME_MAX + 1], new_leaf[OMEGA_FS_NAME_MAX + 1]; + int old_fd = omega_fs_parent_open_at(root_fd, old_path, old_leaf, sizeof old_leaf); + if (old_fd < 0) return -1; + int new_fd = omega_fs_parent_open_at(root_fd, new_path, new_leaf, sizeof new_leaf); + if (new_fd < 0) { + close(old_fd); + return -1; + } + int rc = linkat(old_fd, old_leaf, new_fd, new_leaf, 0); + int saved = errno; + close(new_fd); + close(old_fd); + errno = saved; + return rc; +} + +static inline int omega_fs_lstat_at(int root_fd, const char *path, struct stat *st) { + char leaf[OMEGA_FS_NAME_MAX + 1]; + int pfd = omega_fs_parent_open_at(root_fd, path, leaf, sizeof leaf); + if (pfd < 0) return -1; + int rc = fstatat(pfd, leaf, st, AT_SYMLINK_NOFOLLOW); + int saved = errno; + close(pfd); + errno = saved; + return rc; +} + +/* A replaceable destination is either absent or an already validated regular + * file. Symlinks, hard links, devices and files writable by another uid are + * unsafe state and must not be silently replaced. */ +static inline int omega_fs_replace_ok_at(int root_fd, const char *path) { + struct stat st; + if (omega_fs_lstat_at(root_fd, path, &st) != 0) return errno == ENOENT ? 0 : -1; + if (!S_ISREG(st.st_mode)) { + errno = EPERM; + return -1; + } + int fd = omega_fs_open_regular_at(root_fd, path, O_RDONLY, 0); + if (fd < 0) return -1; + close(fd); + return 0; +} + +static inline int omega_fs_fsync_dir_at(int root_fd, const char *path) { + int fd = omega_fs_subdir_open(root_fd, path, 0, 0); + if (fd < 0) return -1; + int rc = fsync(fd); + int saved = errno; + close(fd); + errno = saved; + return rc; +} + +/* O_EXCL makes a guessed name a refusal, never a target to truncate. PID and + * a process-local counter keep normal writers from sharing temporary names. */ +static inline int omega_fs_temp_name(char *out, size_t cap, const char *stem) { + static unsigned long long sequence; + unsigned long long n = __atomic_add_fetch(&sequence, 1, __ATOMIC_RELAXED); + int wrote = snprintf(out, cap, "%s.%ld.%llu", stem, (long)getpid(), n); + if (wrote < 0 || (size_t)wrote >= cap) { + errno = ENAMETOOLONG; + return -1; + } + return 0; +} + +#endif /* OMEGA_FS_H */ diff --git a/src/runtime/aien_machine_id.c b/src/runtime/aien_machine_id.c index bd1686f89..46eaae3e3 100644 --- a/src/runtime/aien_machine_id.c +++ b/src/runtime/aien_machine_id.c @@ -19,7 +19,7 @@ * canonical machine identity. The codec is in aien_machine_id.h. */ #include "aien_machine_id.h" -#include "omega_path_check.h" +#include "omega_fs.h" #include #include @@ -35,41 +35,32 @@ static int write_all(int fd, const uint8_t *p, size_t n) { return 0; } -int aien_mid_store(const char *path, const AienMachineId *m) { - if (!path || !m) return AIEN_MID_E_ARG; +int aien_mid_store_at(int dir_fd, const char *name, const AienMachineId *m) { + if (dir_fd < 0 || !name || !m || strchr(name, '/')) return AIEN_MID_E_ARG; uint8_t rec[AIEN_MID_RECORD_BYTES]; int rc = aien_mid_encode(m, rec); if (rc != AIEN_MID_OK) return rc; + if (omega_fs_replace_ok_at(dir_fd, name) != 0) return AIEN_MID_E_IO; - /* The caller's path, checked (src/omega_path_check.h), is the only path opened or renamed. */ - char p[4091]; - if (omega_path_copy(p, sizeof p, path) != 0) return AIEN_MID_E_ARG; - char tmp[4096]; - if ((size_t)snprintf(tmp, sizeof tmp, "%s.tmp", p) >= sizeof tmp) return AIEN_MID_E_ARG; - int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + char stem[OMEGA_FS_NAME_MAX + 1], tmp[OMEGA_FS_NAME_MAX + 1]; + if ((size_t)snprintf(stem, sizeof stem, "%s.tmp", name) >= sizeof stem || + omega_fs_temp_name(tmp, sizeof tmp, stem) != 0) + return AIEN_MID_E_ARG; + int fd = omega_fs_open_regular_at(dir_fd, tmp, O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd < 0) return AIEN_MID_E_IO; if (write_all(fd, rec, sizeof rec) != 0 || fsync(fd) != 0) { - close(fd); unlink(tmp); return AIEN_MID_E_IO; + close(fd); omega_fs_unlink_at(dir_fd, tmp, 0); return AIEN_MID_E_IO; + } + if (close(fd) != 0 || omega_fs_rename_at(dir_fd, tmp, name) != 0 || fsync(dir_fd) != 0) { + omega_fs_unlink_at(dir_fd, tmp, 0); + return AIEN_MID_E_IO; } - if (close(fd) != 0 || rename(tmp, p) != 0) { unlink(tmp); return AIEN_MID_E_IO; } - - /* fsync the directory so the rename survives a crash */ - char dir[4096]; - snprintf(dir, sizeof dir, "%s", p); - char *slash = strrchr(dir, '/'); - if (slash == dir) dir[1] = 0; - else if (slash) *slash = 0; - else { dir[0] = '.'; dir[1] = 0; } - int dfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); - if (dfd >= 0) { fsync(dfd); close(dfd); } return AIEN_MID_OK; } -int aien_mid_load(const char *path, AienMachineId *out) { - if (!path || !out) return AIEN_MID_E_ARG; - char p[4096]; - if (omega_path_copy(p, sizeof p, path) != 0) return AIEN_MID_E_ARG; - int fd = open(p, O_RDONLY | O_CLOEXEC); +int aien_mid_load_at(int dir_fd, const char *name, AienMachineId *out) { + if (dir_fd < 0 || !name || !out || strchr(name, '/')) return AIEN_MID_E_ARG; + int fd = omega_fs_open_regular_at(dir_fd, name, O_RDONLY, 0); if (fd < 0) return AIEN_MID_E_IO; uint8_t buf[AIEN_MID_RECORD_BYTES + 1]; size_t got = 0; @@ -84,6 +75,30 @@ int aien_mid_load(const char *path, AienMachineId *out) { return aien_mid_decode(buf, got, out); } +int aien_mid_store(const char *path, const AienMachineId *m) { + if (!path || !m) return AIEN_MID_E_ARG; + char checked[OMEGA_FS_PATH_MAX]; + if (omega_path_copy(checked, sizeof checked, path) != 0) return AIEN_MID_E_ARG; + char name[OMEGA_FS_NAME_MAX + 1]; + int dir_fd = omega_fs_parent_open_at(AT_FDCWD, checked, name, sizeof name); + if (dir_fd < 0) return AIEN_MID_E_IO; + int rc = aien_mid_store_at(dir_fd, name, m); + close(dir_fd); + return rc; +} + +int aien_mid_load(const char *path, AienMachineId *out) { + if (!path || !out) return AIEN_MID_E_ARG; + char checked[OMEGA_FS_PATH_MAX]; + if (omega_path_copy(checked, sizeof checked, path) != 0) return AIEN_MID_E_ARG; + char name[OMEGA_FS_NAME_MAX + 1]; + int dir_fd = omega_fs_parent_open_at(AT_FDCWD, checked, name, sizeof name); + if (dir_fd < 0) return AIEN_MID_E_IO; + int rc = aien_mid_load_at(dir_fd, name, out); + close(dir_fd); + return rc; +} + void aien_mid_index_init(AienMachineIndex *t, AienMachineId *slots, uint32_t capacity) { t->capacity = slots ? capacity : 0; t->count = 0; diff --git a/src/runtime/aien_machine_id.h b/src/runtime/aien_machine_id.h index 7494642ab..a54cee89b 100644 --- a/src/runtime/aien_machine_id.h +++ b/src/runtime/aien_machine_id.h @@ -247,12 +247,15 @@ static inline int aien_mid_from_text(const char *s, AienMachineId *out) { /* ---- aien_machine_id.c -------------------------------------------------- */ -/* Persist atomically (temp file, fsync, rename, fsync directory). A path that - * src/omega_path_check.h refuses (a ".." component, a byte outside its set, too - * long) is AIEN_MID_E_ARG for both calls, before any file is touched. */ +/* Persist atomically (exclusive temp file, fsync, descriptor-relative rename, + * fsync directory). Every path component and final file refuses symlinks; a + * path that src/omega_path_check.h refuses is AIEN_MID_E_ARG before any file + * is touched. The _at forms use one checked component in a held directory. */ int aien_mid_store(const char *path, const AienMachineId *m); +int aien_mid_store_at(int dir_fd, const char *name, const AienMachineId *m); /* Load; any file that is not exactly one valid record is rejected. */ int aien_mid_load(const char *path, AienMachineId *out); +int aien_mid_load_at(int dir_fd, const char *name, AienMachineId *out); /* Local runtime index <-> canonical identity. Index 0 means "none" (as in * rx_capq); indexes are dense from 1 in bind order and live only in this diff --git a/src/runtime/rx_compose.c b/src/runtime/rx_compose.c index 4d4242701..1d657b4ac 100644 --- a/src/runtime/rx_compose.c +++ b/src/runtime/rx_compose.c @@ -25,7 +25,7 @@ #include "rx_compose.h" #include "rx_cortex_record.h" #include "sha256.h" -#include "omega_path_check.h" +#include "omega_fs.h" #include #include @@ -59,10 +59,6 @@ const JsRealizer RXC_REALIZER = { JS_REAL_WORLD_PROJECTION, "rxc_state", RXC_UNI /* ---- small helpers -------------------------------------------------------- */ -static void path_in(char *out, size_t n, const char *dir, const char *name) { - snprintf(out, n, "%s/%s", dir, name); -} - static uint64_t ref_pack(JsBranchRef r) { return js_branch_ref_pack(r); } static JsBranchRef ref_unpack(uint64_t v) { return js_branch_ref_unpack(v); } static int ref_eq(JsBranchRef a, JsBranchRef b) { return a.id == b.id && a.gen == b.gen; } @@ -865,6 +861,7 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u hooks.fault_hit = 0; hooks.held = hooks.release = hooks.hold_done = 0; memset(c, 0, sizeof *c); + c->dir_fd = -1; c->test = hooks; c->wait_ms = RXC_WAIT_MS_DEFAULT; snprintf(c->dir, sizeof c->dir, "%s", dir); @@ -873,24 +870,33 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u c->router = router; c->contract = contract; c->admin = admin; - /* Check before mkdir (src/omega_path_check.h): a refused dir is an argument error and creates nothing. */ + /* Check and retain the root before any child is inspected or created. */ char checked[sizeof c->dir]; if (omega_path_copy(checked, sizeof checked, dir) != 0) return RX_ERR_ARG; - if (mkdir(checked, 0700) && errno != EEXIST) return RX_ERR_ARG; + c->dir_fd = omega_fs_root_open(checked, 1, 0700); + if (c->dir_fd < 0) return RX_ERR_ARG; - char p[256]; - path_in(p, sizeof p, dir, "machine.id"); - if (access(p, F_OK) == 0) { + struct stat st; + if (omega_fs_lstat_at(c->dir_fd, "machine.id", &st) == 0) { AienMachineId stored; - if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, self)) + if (aien_mid_load_at(c->dir_fd, "machine.id", &stored) != AIEN_MID_OK || + !aien_mid_equal(&stored, self)) { + close(c->dir_fd); + c->dir_fd = -1; return RX_ERR_IDENTITY; - } else if (aien_mid_store(p, self) != AIEN_MID_OK) { + } + } else if (errno != ENOENT || aien_mid_store_at(c->dir_fd, "machine.id", self) != AIEN_MID_OK) { + close(c->dir_fd); + c->dir_fd = -1; return RX_ERR_IDENTITY; } - path_in(p, sizeof p, dir, "cortex.cx"); - if (cx_open(&c->cx, p, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) + if (cx_open_at(&c->cx, c->dir_fd, "cortex.cx", RX_CORTEX_SUBJECTS, + CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) { + close(c->dir_fd); + c->dir_fd = -1; return RX_ERR_REPLAY; + } /* cx_open already verified every record and rebuilt the chain (see rx_cortex.h). */ JsHome home; @@ -898,9 +904,10 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u aien_mid_to_slot(self, home.machine); home.locality = JS_HOME_LOCAL; const JsRealizer *rz[1] = { &RXC_REALIZER }; - path_in(p, sizeof p, dir, "jspace"); - if (js_space_open(&c->js, p, rz, 1, NULL, &home) != JS_OK) { + if (js_space_open_at(&c->js, c->dir_fd, "jspace", rz, 1, NULL, &home) != JS_OK) { cx_close(&c->cx); + close(c->dir_fd); + c->dir_fd = -1; return RX_ERR_REPLAY; } /* A Cortex journal cut exactly at a record boundary opens cleanly in cx_open @@ -921,6 +928,8 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u if (c->cx.n < cnt || (cnt > 0 && (!head || memcmp(head->digest, an + 8, 32) != 0))) { js_space_destroy(&c->js); cx_close(&c->cx); + close(c->dir_fd); + c->dir_fd = -1; return RX_ERR_REPLAY; } } else { @@ -930,6 +939,8 @@ static int open_home(RxCompose *c, const char *dir, const AienMachineId *self, u if (rc != RX_OK) { js_space_destroy(&c->js); cx_close(&c->cx); + close(c->dir_fd); + c->dir_fd = -1; } return rc; } @@ -1025,6 +1036,8 @@ int rx_compose_open(RxCompose *c, const char *dir, const AienMachineId *self, ui c->world = NULL; js_space_destroy(&c->js); cx_close(&c->cx); + close(c->dir_fd); + c->dir_fd = -1; return rc; } return RX_OK; @@ -1125,6 +1138,8 @@ int rx_compose_attach(RxCompose *c, RxWorld *w, const RxCallerKeyring *keys, con c->world = NULL; js_space_destroy(&c->js); cx_close(&c->cx); + close(c->dir_fd); + c->dir_fd = -1; } pthread_mutex_unlock(&g_attach_mu); return rc; @@ -1294,6 +1309,8 @@ void rx_compose_close(RxCompose *c) { c->world = NULL; js_space_destroy(&c->js); /* never commits: durable state is what settle made durable */ cx_close(&c->cx); + if (c->dir_fd >= 0) close(c->dir_fd); + c->dir_fd = -1; } void rx_compose_record_digest(const CxStore *s, uint8_t out[32]) { diff --git a/src/runtime/rx_compose.h b/src/runtime/rx_compose.h index 24da74372..c5f5c5cac 100644 --- a/src/runtime/rx_compose.h +++ b/src/runtime/rx_compose.h @@ -184,6 +184,7 @@ enum { typedef struct RxCompose { char dir[200]; + int dir_fd; /* retained validated composition root */ AienMachineId self; RxWorld w; /* storage of the World rx_compose_open owns */ RxWorld *world; /* the World it runs in: &w (open) or the caller's (attach) */ @@ -261,7 +262,9 @@ typedef struct RxCompose { * World is built with `n_workers` (the composition owns it; see * rx_compose_attach to run inside an existing World), state is recovered * OLD-or-NEW. `dir` must pass src/omega_path_check.h (omega_path_copy) and - * be under 168 bytes; otherwise RX_ERR_ARG and nothing is created. */ + * be under 168 bytes. An existing root must be owned by this uid, not + * group/other-writable, and have no symlink component; its descriptor is + * retained until close. Otherwise RX_ERR_ARG and nothing is created. */ int rx_compose_open(RxCompose *c, const char *dir, const AienMachineId *self, uint64_t session, const SrRouter *router, RxcContract contract, AienosCapAdmin *admin, AienosCapView *view, uint32_t n_workers); diff --git a/src/runtime/rx_cortex.c b/src/runtime/rx_cortex.c index a5445a438..7b9c2a4df 100644 --- a/src/runtime/rx_cortex.c +++ b/src/runtime/rx_cortex.c @@ -36,6 +36,7 @@ #endif #ifndef CX_NO_POSIX +#include "omega_fs.h" #include #include #include @@ -386,8 +387,9 @@ static int write_all(int fd, const uint8_t *b, size_t n) { } static int jp_open(void *ctx, const char *name, int ro, void **h) { - (void)ctx; - int fd = open(name, ro ? O_RDONLY | O_CLOEXEC : O_RDWR | O_CREAT | O_CLOEXEC, 0644); + int flags = ro ? O_RDONLY : O_RDWR | O_CREAT; + int fd = ctx ? omega_fs_open_regular_at((int)(intptr_t)ctx - 1, name, flags, 0644) + : omega_fs_open_regular(name, flags, 0644); if (fd < 0) return CX_ERR_IO; if (!ro && flock(fd, LOCK_EX | LOCK_NB) != 0) { int e = errno; @@ -448,6 +450,12 @@ const CxJournalOps cx_journal_posix = { jp_open, jp_size, jp_read, jp_append, jp int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags) { return cx_open_with(s, &cx_journal_posix, NULL, path, n_subjects, flags); } + +int cx_open_at(CxStore *s, int dir_fd, const char *name, uint64_t n_subjects, uint32_t flags) { + if (dir_fd < 0 || !name || strchr(name, '/')) return CX_ERR_ARG; + return cx_open_with(s, &cx_journal_posix, (void *)(intptr_t)(dir_fd + 1), name, + n_subjects, flags); +} #endif /* !CX_NO_POSIX */ /* ---- read ---------------------------------------------------------------- */ diff --git a/src/runtime/rx_cortex.h b/src/runtime/rx_cortex.h index 55502f6a3..04de067ed 100644 --- a/src/runtime/rx_cortex.h +++ b/src/runtime/rx_cortex.h @@ -279,6 +279,8 @@ int cx_open_with(CxStore *s, const CxJournalOps *ops, void *ctx, const char *na * together, nothing local detects it (an external anchor problem). */ #ifndef CX_NO_POSIX int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags); +/* Descriptor-relative form for a single journal name in a retained directory. */ +int cx_open_at(CxStore *s, int dir_fd, const char *name, uint64_t n_subjects, uint32_t flags); #endif void cx_close(CxStore *s); /* = cx_free */ diff --git a/src/runtime/rx_generation.c b/src/runtime/rx_generation.c index dff8c965e..6fc8366a0 100644 --- a/src/runtime/rx_generation.c +++ b/src/runtime/rx_generation.c @@ -22,7 +22,7 @@ #include "runtime/rx_generation.h" #include "runtime/rx_argus.h" #include "sha256.h" -#include "omega_path_check.h" +#include "omega_fs.h" #include #include @@ -102,6 +102,7 @@ typedef struct { struct RxGenStore { char dir[384]; + int dir_fd; /* retained validated store root */ uint64_t active_id; uint64_t active_lineage; uint64_t next_id; @@ -206,8 +207,16 @@ static int write_full(RxGenStore *store, int fd, const uint8_t *p, size_t n) { return 0; } -static int read_full(const char *path, uint8_t *buf, size_t n) { - int fd = open(path, O_RDONLY); +static const char *store_rel(const RxGenStore *store, const char *path) { + size_t n = strlen(store->dir); + if (strcmp(path, store->dir) == 0) return "."; + return strncmp(path, store->dir, n) == 0 && path[n] == '/' ? path + n + 1 : NULL; +} + +static int read_full(const RxGenStore *store, const char *path, uint8_t *buf, size_t n) { + const char *rel = store_rel(store, path); + if (!rel || strcmp(rel, ".") == 0) return -1; + int fd = omega_fs_open_regular_at(store->dir_fd, rel, O_RDONLY, 0); if (fd < 0) return -1; size_t off = 0; while (off < n) { @@ -234,16 +243,24 @@ static int fsync_fd(RxGenStore *store, int fd) { } static int fsync_path(RxGenStore *store, const char *path) { - int fd = open(path, O_RDONLY); + const char *rel = store_rel(store, path); + if (!rel) return -1; + int fd = strcmp(rel, ".") == 0 ? omega_fs_dupfd(store->dir_fd) + : omega_fs_subdir_open(store->dir_fd, rel, 0, 0); if (fd < 0) return -1; int rc = fsync_fd(store, fd); close(fd); return rc; } -static int ensure_dir(const char *path) { - if (mkdir(path, 0755) == 0 || errno == EEXIST) return 0; - return -1; +static int ensure_dir(RxGenStore *store, const char *path) { + const char *rel = store_rel(store, path); + if (!rel) return -1; + int fd = strcmp(rel, ".") == 0 ? omega_fs_dupfd(store->dir_fd) + : omega_fs_subdir_open(store->dir_fd, rel, 1, 0755); + if (fd < 0) return -1; + close(fd); + return 0; } static void crash_now(void) { _exit(86); } @@ -381,10 +398,14 @@ static int decode_journal(const uint8_t in[JRN_BYTES], uint32_t *phase, uint64_t static int commit_file(RxGenStore *store, const char *final_path, const uint8_t *buf, size_t n, int during_step) { - char tmp[512]; - if (snprintf(tmp, sizeof tmp, "%s/tmp/write.partial", store->dir) >= (int)sizeof tmp) + const char *final_rel = store_rel(store, final_path); + char tmp[OMEGA_FS_NAME_MAX + 1]; + if (!final_rel || strcmp(final_rel, ".") == 0 || + omega_fs_temp_name(tmp, sizeof tmp, "tmp/write.partial") != 0) return RX_GEN_ERR_IO; - int fd = open(tmp, O_CREAT | O_TRUNC | O_WRONLY, 0644); + if (omega_fs_replace_ok_at(store->dir_fd, final_rel) != 0) return RX_GEN_ERR_IO; + int fd = omega_fs_open_regular_at(store->dir_fd, tmp, + O_CREAT | O_EXCL | O_WRONLY, 0644); if (fd < 0) return RX_GEN_ERR_IO; if (during_step != RX_CRASH_NONE && store->crash_step == during_step) { size_t half = n / 2; @@ -392,6 +413,7 @@ static int commit_file(RxGenStore *store, const char *final_path, const uint8_t if (half > n) half = n; if (write_full(store, fd, buf, half) != 0) { close(fd); + omega_fs_unlink_at(store->dir_fd, tmp, 0); return RX_GEN_ERR_IO; } fsync_fd(store, fd); @@ -400,10 +422,13 @@ static int commit_file(RxGenStore *store, const char *final_path, const uint8_t } if (write_full(store, fd, buf, n) != 0 || fsync_fd(store, fd) != 0) { close(fd); + omega_fs_unlink_at(store->dir_fd, tmp, 0); + return RX_GEN_ERR_IO; + } + if (close(fd) != 0 || omega_fs_rename_at(store->dir_fd, tmp, final_rel) != 0) { + omega_fs_unlink_at(store->dir_fd, tmp, 0); return RX_GEN_ERR_IO; } - close(fd); - if (rename(tmp, final_path) != 0) return RX_GEN_ERR_IO; char parent[512]; snprintf(parent, sizeof parent, "%s", final_path); char *slash = strrchr(parent, '/'); @@ -419,20 +444,23 @@ static int gen_dir(const char *dir, uint64_t id, char *out, size_t n) { return 0; } -static int load_root_file(const char *dir, uint64_t id, RootView *view, uint8_t digest[32]) { +static int load_root_file(const RxGenStore *store, uint64_t id, RootView *view, uint8_t digest[32]) { char folder[512], path[512]; - if (gen_dir(dir, id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; + if (gen_dir(store->dir, id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; if (path_join(path, sizeof path, folder, "root") != 0) return RX_GEN_ERR_IO; uint8_t raw[ROOT_BYTES]; - if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; + if (read_full(store, path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; int rc = decode_root(raw, view); if (rc != RX_GEN_OK) return rc; checksum(raw, ROOT_BYTES, digest); return RX_GEN_OK; } -static int file_matches(const char *path, const uint8_t expect[32], uint64_t expect_len) { - int fd = open(path, O_RDONLY); +static int file_matches(const RxGenStore *store, const char *path, const uint8_t expect[32], + uint64_t expect_len) { + const char *rel = store_rel(store, path); + if (!rel || strcmp(rel, ".") == 0) return RX_GEN_ERR_IO; + int fd = omega_fs_open_regular_at(store->dir_fd, rel, O_RDONLY, 0); if (fd < 0) return RX_GEN_ERR_MISSING; sha256_ctx ctx; sha256_init(&ctx); @@ -460,38 +488,43 @@ static const char *BLOB_NAME[DIG_COUNT] = { "objects", "evidence", "model", "realization", "config", "provenance" }; -static int root_reachable(const char *dir, const RootView *view) { +static int root_reachable(const RxGenStore *store, const RootView *view) { char folder[512], path[512]; - if (gen_dir(dir, view->id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; + if (gen_dir(store->dir, view->id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; for (int i = 0; i < DIG_COUNT; i++) { if (path_join(path, sizeof path, folder, BLOB_NAME[i]) != 0) return RX_GEN_ERR_IO; - int rc = file_matches(path, view->digest[i], view->lengths[i]); + int rc = file_matches(store, path, view->digest[i], view->lengths[i]); if (rc != RX_GEN_OK) return rc; } return RX_GEN_OK; } -static int read_pointer(const char *dir, uint64_t *id, uint64_t *lineage, uint8_t digest[32]) { +static int read_pointer(const RxGenStore *store, uint64_t *id, uint64_t *lineage, + uint8_t digest[32]) { char path[512]; - if (path_join(path, sizeof path, dir, "active") != 0) return RX_GEN_ERR_IO; + if (path_join(path, sizeof path, store->dir, "active") != 0) return RX_GEN_ERR_IO; uint8_t raw[PTR_BYTES]; - if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; + if (read_full(store, path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; return decode_pointer(raw, id, lineage, digest); } -static int read_journal(const char *dir, uint32_t *phase, uint64_t *parent, uint64_t *candidate, +static int read_journal(const RxGenStore *store, uint32_t *phase, uint64_t *parent, + uint64_t *candidate, uint64_t *parent_lineage, uint8_t digest[32]) { char path[512]; - if (path_join(path, sizeof path, dir, "journal") != 0) return RX_GEN_ERR_IO; + if (path_join(path, sizeof path, store->dir, "journal") != 0) return RX_GEN_ERR_IO; uint8_t raw[JRN_BYTES]; - if (read_full(path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; + if (read_full(store, path, raw, sizeof raw) != 0) return RX_GEN_ERR_MISSING; return decode_journal(raw, phase, parent, candidate, parent_lineage, digest); } -static int event_has(const char *dir, uint64_t id) { +static int event_has(const RxGenStore *store, uint64_t id) { char path[512]; - if (path_join(path, sizeof path, dir, "events") != 0) return 0; - FILE *f = fopen(path, "r"); + if (path_join(path, sizeof path, store->dir, "events") != 0) return 0; + const char *rel = store_rel(store, path); + int fd = rel ? omega_fs_open_regular_at(store->dir_fd, rel, O_RDONLY, 0) : -1; + FILE *f = fd >= 0 ? fdopen(fd, "r") : NULL; + if (!f && fd >= 0) close(fd); if (!f) return 0; unsigned long long seen = 0, lineage = 0; int found = 0; @@ -503,10 +536,12 @@ static int event_has(const char *dir, uint64_t id) { } static int append_event(RxGenStore *store, uint64_t id, uint64_t lineage) { - if (event_has(store->dir, id)) return RX_GEN_OK; + if (event_has(store, id)) return RX_GEN_OK; char path[512]; if (path_join(path, sizeof path, store->dir, "events") != 0) return RX_GEN_ERR_IO; - int fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0644); + const char *rel = store_rel(store, path); + int fd = rel ? omega_fs_open_regular_at(store->dir_fd, rel, + O_CREAT | O_WRONLY | O_APPEND, 0644) : -1; if (fd < 0) return RX_GEN_ERR_IO; char line[64]; int n = snprintf(line, sizeof line, "%llu %llu\n", (unsigned long long)id, @@ -533,11 +568,15 @@ static void fill_record(const RootView *view, int receipt, int event, RxRecovery memcpy(out->excluded_ids, view->excluded_ids, sizeof view->excluded_ids); } -static int receipt_exists(const char *dir, uint64_t id) { +static int receipt_exists(const RxGenStore *store, uint64_t id) { char folder[512], path[512]; - if (gen_dir(dir, id, folder, sizeof folder) != 0) return 0; + if (gen_dir(store->dir, id, folder, sizeof folder) != 0) return 0; if (path_join(path, sizeof path, folder, "receipt") != 0) return 0; - return access(path, R_OK) == 0; + const char *rel = store_rel(store, path); + int fd = rel ? omega_fs_open_regular_at(store->dir_fd, rel, O_RDONLY, 0) : -1; + if (fd < 0) return 0; + close(fd); + return 1; } static int write_receipt_file(RxGenStore *store, const RootView *view, uint32_t subject, @@ -553,7 +592,7 @@ static int write_receipt_file(RxGenStore *store, const RootView *view, uint32_t if (gen_dir(store->dir, view->id, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; if (path_join(root_path, sizeof root_path, folder, "root") != 0) return RX_GEN_ERR_IO; uint8_t root_raw[ROOT_BYTES]; - if (read_full(root_path, root_raw, sizeof root_raw) != 0) return RX_GEN_ERR_MISSING; + if (read_full(store, root_path, root_raw, sizeof root_raw) != 0) return RX_GEN_ERR_MISSING; checksum(root_raw, ROOT_BYTES, root_digest); memcpy(raw + 32, root_digest, 32); put_u32(raw + 64, subject); @@ -606,16 +645,14 @@ static int release_lock(RxGenStore *store) { close(store->lock_fd); store->lock_fd = -1; } - char path[512]; - if (path_join(path, sizeof path, store->dir, "barrier.lock") != 0) return RX_GEN_ERR_IO; - if (unlink(path) != 0 && errno != ENOENT) return RX_GEN_ERR_IO; + if (omega_fs_unlink_at(store->dir_fd, "barrier.lock", 0) != 0 && errno != ENOENT) + return RX_GEN_ERR_IO; return RX_GEN_OK; } static int take_lock(RxGenStore *store) { - char path[512]; - if (path_join(path, sizeof path, store->dir, "barrier.lock") != 0) return RX_GEN_ERR_IO; - int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0644); + int fd = omega_fs_open_regular_at(store->dir_fd, "barrier.lock", + O_CREAT | O_EXCL | O_WRONLY, 0644); if (fd < 0) return RX_GEN_ERR_BUSY; char pid[32]; int n = snprintf(pid, sizeof pid, "%ld\n", (long)getpid()); @@ -649,7 +686,7 @@ static int install_genesis(RxGenStore *store) { } char folder[512]; if (gen_dir(store->dir, 1, folder, sizeof folder) != 0) return RX_GEN_ERR_IO; - if (ensure_dir(folder) != 0) return RX_GEN_ERR_IO; + if (ensure_dir(store, folder) != 0) return RX_GEN_ERR_IO; RootView view; memset(&view, 0, sizeof view); view.id = 1; @@ -692,53 +729,58 @@ static int install_genesis(RxGenStore *store) { int rx_gen_open(const char *dir, RxGenStore **out) { if (!dir || !out) return RX_GEN_ERR_ARG; + *out = NULL; RxGenStore *store = calloc(1, sizeof(*store)); if (!store) return RX_GEN_ERR_IO; pthread_mutex_init(&store->active_mu, NULL); - /* The caller's directory, checked (src/omega_path_check.h), is the only one this store opens. */ + store->dir_fd = -1; + int rc = RX_GEN_ERR_IO; + /* Retain one validated root; every child operation is relative to it. */ if (omega_path_copy(store->dir, sizeof store->dir, dir) != 0) { - free(store); - return RX_GEN_ERR_ARG; + rc = RX_GEN_ERR_ARG; + goto fail; } + store->dir_fd = omega_fs_root_open(store->dir, 1, 0700); + if (store->dir_fd < 0) goto fail; store->lock_fd = -1; char gdir[512], tmp[512]; if (path_join(gdir, sizeof gdir, store->dir, "g") != 0 || path_join(tmp, sizeof tmp, store->dir, "tmp") != 0) { - free(store); - return RX_GEN_ERR_IO; - } - if (ensure_dir(store->dir) != 0 || ensure_dir(gdir) != 0 || ensure_dir(tmp) != 0) { - free(store); - return RX_GEN_ERR_IO; + goto fail; } - char active[512]; - path_join(active, sizeof active, store->dir, "active"); - if (access(active, R_OK) != 0) { - int rc = install_genesis(store); + if (ensure_dir(store, gdir) != 0 || ensure_dir(store, tmp) != 0) goto fail; + struct stat st; + if (omega_fs_lstat_at(store->dir_fd, "active", &st) != 0) { + if (errno != ENOENT) goto fail; + rc = install_genesis(store); if (rc != RX_GEN_OK) { - free(store); - return rc; + goto fail; } } else { uint8_t digest[32]; - int rc = read_pointer(store->dir, &store->active_id, &store->active_lineage, digest); - if (rc != RX_GEN_OK) { - free(store); - return rc; - } + rc = read_pointer(store, &store->active_id, &store->active_lineage, digest); + if (rc != RX_GEN_OK) goto fail; FILE *f = NULL; - char nextp[512]; - path_join(nextp, sizeof nextp, store->dir, "nextid"); - f = fopen(nextp, "r"); unsigned long long next = store->active_id + 1; - if (f) { + if (omega_fs_lstat_at(store->dir_fd, "nextid", &st) == 0) { + int fd = omega_fs_open_regular_at(store->dir_fd, "nextid", O_RDONLY, 0); + if (fd < 0 || !(f = fdopen(fd, "r"))) { + if (fd >= 0) close(fd); + goto fail; + } if (fscanf(f, "%llu", &next) != 1) next = store->active_id + 1; fclose(f); - } + } else if (errno != ENOENT) goto fail; store->next_id = (uint64_t)next; } *out = store; return RX_GEN_OK; +fail: + if (rc == RX_GEN_OK) rc = RX_GEN_ERR_IO; + if (store->dir_fd >= 0) close(store->dir_fd); + pthread_mutex_destroy(&store->active_mu); + free(store); + return rc; } static void exec_stop(RxGenStore *store); @@ -750,6 +792,7 @@ void rx_gen_close(RxGenStore *store) { for (int b = 0; b < DIG_COUNT; b++) free(store->cand[i].blob[b]); } if (store->lock_fd >= 0) close(store->lock_fd); + if (store->dir_fd >= 0) close(store->dir_fd); pthread_mutex_destroy(&store->active_mu); free(store); } @@ -773,7 +816,7 @@ int rx_gen_read_blob(const RxGenStore *store, uint64_t id, const char *name, uin if (which < 0) return RX_GEN_ERR_ARG; RootView view; uint8_t root_digest[32]; - int rc = load_root_file(store->dir, id, &view, root_digest); + int rc = load_root_file(store, id, &view, root_digest); if (rc != RX_GEN_OK) return rc; if (view.id != id) return RX_GEN_ERR_TORN; if (view.lengths[which] > cap) return RX_GEN_ERR_ARG; @@ -781,9 +824,10 @@ int rx_gen_read_blob(const RxGenStore *store, uint64_t id, const char *name, uin /* BLOB_NAME[which], not the caller's string: the name opened is the table's, never the input's */ if (gen_dir(store->dir, id, folder, sizeof folder) != 0 || path_join(path, sizeof path, folder, BLOB_NAME[which]) != 0) return RX_GEN_ERR_IO; - rc = file_matches(path, view.digest[which], view.lengths[which]); + rc = file_matches(store, path, view.digest[which], view.lengths[which]); if (rc != RX_GEN_OK) return rc; - if (view.lengths[which] && read_full(path, buf, (size_t)view.lengths[which]) != 0) + if (view.lengths[which] && + read_full(store, path, buf, (size_t)view.lengths[which]) != 0) return RX_GEN_ERR_IO; /* The bytes read must be the bytes the root names, not whatever the file * became after the check above. */ @@ -803,11 +847,9 @@ static int caller_refusal(int crc) { /* R16 G6: the durable mark of an operator stop in the store directory. 0 none, * RX_GEN_ERR_HALTED present, RX_GEN_ERR_IO when it cannot be told (refuse). */ static int halt_marked(const RxGenStore *store) { - char p[512]; - if (snprintf(p, sizeof p, "%s/%s", store->dir, RX_GEN_HALT_MARK) >= (int)sizeof p) - return RX_GEN_ERR_IO; struct stat st; - if (lstat(p, &st) == 0) return RX_GEN_ERR_HALTED; + if (omega_fs_lstat_at(store->dir_fd, RX_GEN_HALT_MARK, &st) == 0) + return RX_GEN_ERR_HALTED; return errno == ENOENT ? 0 : RX_GEN_ERR_IO; } @@ -1084,7 +1126,7 @@ int rx_gen_promote(RxGenStore *store, const RxPromotionRequest *request, RxGenAu int rc = RX_GEN_OK; uint64_t disk_id = 0, disk_lineage = 0; uint8_t disk_digest[32]; - rc = read_pointer(store->dir, &disk_id, &disk_lineage, disk_digest); + rc = read_pointer(store, &disk_id, &disk_lineage, disk_digest); if (rc != RX_GEN_OK) goto done; set_active(store, disk_id, disk_lineage); if (c->parent_id != store->active_id) { @@ -1139,7 +1181,7 @@ int rx_gen_promote(RxGenStore *store, const RxPromotionRequest *request, RxGenAu rc = RX_GEN_ERR_IO; goto done; } - if (ensure_dir(folder) != 0) { + if (ensure_dir(store, folder) != 0) { rc = RX_GEN_ERR_IO; goto done; } @@ -1182,7 +1224,7 @@ int rx_gen_promote(RxGenStore *store, const RxPromotionRequest *request, RxGenAu if (store->crash_step == RX_CRASH_AFTER_CANDIDATE_WRITE) crash_now(); store->phases.candidate_ns = monotonic_ns(); if (store->disk_hook) store->disk_hook(folder, store->disk_ctx); - rc = root_reachable(store->dir, &view); + rc = root_reachable(store, &view); if (rc != RX_GEN_OK) goto done; store->phases.reachable_ns = monotonic_ns(); if (store->crash_step == RX_CRASH_BEFORE_ROOT_FLIP) crash_now(); @@ -1258,78 +1300,84 @@ done: int rx_gen_recover(const char *dir, RxRecoveryRecord *out) { if (!dir || !out) return RX_GEN_ERR_ARG; memset(out, 0, sizeof(*out)); - /* From here dir is the checked copy (src/omega_path_check.h): no other path is opened. */ - char checked[sizeof ((RxGenStore *)0)->dir]; - if (omega_path_copy(checked, sizeof checked, dir) != 0) return RX_GEN_ERR_ARG; - dir = checked; - char lock[512]; - if (path_join(lock, sizeof lock, dir, "barrier.lock") == 0) unlink(lock); + RxGenStore scratch; + memset(&scratch, 0, sizeof scratch); + scratch.lock_fd = -1; + scratch.dir_fd = -1; + if (omega_path_copy(scratch.dir, sizeof scratch.dir, dir) != 0) return RX_GEN_ERR_ARG; + scratch.dir_fd = omega_fs_root_open(scratch.dir, 0, 0); + if (scratch.dir_fd < 0) return RX_GEN_ERR_IO; + if (omega_fs_unlink_at(scratch.dir_fd, "barrier.lock", 0) != 0 && errno != ENOENT) { + close(scratch.dir_fd); + return RX_GEN_ERR_IO; + } uint64_t id = 0, lineage = 0; uint8_t pointer_digest[32]; - int pointer_rc = read_pointer(dir, &id, &lineage, pointer_digest); + int pointer_rc = read_pointer(&scratch, &id, &lineage, pointer_digest); uint32_t phase = PHASE_NONE; uint64_t parent = 0, candidate = 0, parent_lineage = 0; uint8_t journal_digest[32]; - int journal_rc = - read_journal(dir, &phase, &parent, &candidate, &parent_lineage, journal_digest); + int journal_rc = read_journal(&scratch, &phase, &parent, &candidate, &parent_lineage, + journal_digest); + int rc = RX_GEN_OK; if (pointer_rc != RX_GEN_OK && journal_rc == RX_GEN_OK && phase >= PHASE_FLIPPED) { RootView repaired; uint8_t got[32]; - int rc = load_root_file(dir, candidate, &repaired, got); - if (rc != RX_GEN_OK) return rc; - if (memcmp(got, journal_digest, 32) != 0) return RX_GEN_ERR_TORN; - if (root_reachable(dir, &repaired) != RX_GEN_OK) return RX_GEN_ERR_TORN; - RxGenStore scratch; - memset(&scratch, 0, sizeof scratch); - scratch.lock_fd = -1; - snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); + rc = load_root_file(&scratch, candidate, &repaired, got); + if (rc != RX_GEN_OK) goto done; + if (memcmp(got, journal_digest, 32) != 0 || + root_reachable(&scratch, &repaired) != RX_GEN_OK) { + rc = RX_GEN_ERR_TORN; + goto done; + } uint8_t pointer[PTR_BYTES]; encode_pointer(repaired.id, repaired.lineage, got, pointer); char active[512]; - if (path_join(active, sizeof active, dir, "active") != 0) return RX_GEN_ERR_IO; + if (path_join(active, sizeof active, scratch.dir, "active") != 0) { + rc = RX_GEN_ERR_IO; + goto done; + } rc = commit_file(&scratch, active, pointer, sizeof pointer, RX_CRASH_NONE); - if (rc != RX_GEN_OK) return rc; + if (rc != RX_GEN_OK) goto done; id = repaired.id; lineage = repaired.lineage; memcpy(pointer_digest, got, 32); pointer_rc = RX_GEN_OK; } - if (pointer_rc != RX_GEN_OK) return pointer_rc; + if (pointer_rc != RX_GEN_OK) { rc = pointer_rc; goto done; } RootView view; uint8_t root_digest[32]; - int rc = load_root_file(dir, id, &view, root_digest); - if (rc != RX_GEN_OK) return rc; - if (view.id != id || view.lineage != lineage || memcmp(root_digest, pointer_digest, 32) != 0) - return RX_GEN_ERR_TORN; - rc = root_reachable(dir, &view); - if (rc != RX_GEN_OK) return rc; + rc = load_root_file(&scratch, id, &view, root_digest); + if (rc != RX_GEN_OK) goto done; + if (view.id != id || view.lineage != lineage || memcmp(root_digest, pointer_digest, 32) != 0) { + rc = RX_GEN_ERR_TORN; + goto done; + } + rc = root_reachable(&scratch, &view); + if (rc != RX_GEN_OK) goto done; - int receipt = receipt_exists(dir, id); + int receipt = receipt_exists(&scratch, id); if (view.parent != 0 && journal_rc == RX_GEN_OK && candidate == id && !receipt) { - RxGenStore scratch; - memset(&scratch, 0, sizeof scratch); - scratch.lock_fd = -1; - scratch.crash_step = RX_CRASH_NONE; - snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); rc = write_receipt_file(&scratch, &view, 0, 0, 0); - if (rc != RX_GEN_OK) return rc; + if (rc != RX_GEN_OK) goto done; rc = append_event(&scratch, view.id, view.lineage); - if (rc != RX_GEN_OK) return rc; + if (rc != RX_GEN_OK) goto done; receipt = 1; } - fill_record(&view, receipt, event_has(dir, id) || view.parent == 0, out); + fill_record(&view, receipt, event_has(&scratch, id) || view.parent == 0, out); if (view.parent != 0 && !out->event_present) { - RxGenStore scratch; - memset(&scratch, 0, sizeof scratch); - scratch.lock_fd = -1; - snprintf(scratch.dir, sizeof scratch.dir, "%s", dir); - if (append_event(&scratch, view.id, view.lineage) != RX_GEN_OK) return RX_GEN_ERR_IO; + if (append_event(&scratch, view.id, view.lineage) != RX_GEN_OK) { + rc = RX_GEN_ERR_IO; + goto done; + } out->event_present = 1; } - return RX_GEN_OK; +done: + close(scratch.dir_fd); + return rc; } int rx_gen_reject_replay(const char *dir, uint64_t effect_id) { diff --git a/src/runtime/rx_generation.h b/src/runtime/rx_generation.h index 9cdd7adab..54df260bf 100644 --- a/src/runtime/rx_generation.h +++ b/src/runtime/rx_generation.h @@ -173,8 +173,9 @@ typedef struct { } RxGenPhases; /* rx_gen_open, rx_gen_recover and rx_gen_reject_replay answer RX_GEN_ERR_ARG, - * before touching any file, for a dir that src/omega_path_check.h refuses - * (a ".." component, a byte outside its set, or too long for RxGenStore). */ + * before touching any file, for a dir that src/omega_path_check.h refuses. + * The root is retained as a descriptor and child operations refuse symlink + * components, non-regular files, foreign ownership and hard links. */ int rx_gen_open(const char *dir, RxGenStore **out); void rx_gen_close(RxGenStore *store); /* R15: process-wide bytes written and fsync calls made by every store. diff --git a/src/runtime/rx_jspace.c b/src/runtime/rx_jspace.c index df5a34e62..d2907b7a6 100644 --- a/src/runtime/rx_jspace.c +++ b/src/runtime/rx_jspace.c @@ -28,6 +28,7 @@ * full SHA-256 digests outside the store. */ #include "rx_jspace.h" +#include "omega_fs.h" #include "sha256.h" #include "aien_machine_id.h" @@ -621,6 +622,7 @@ void js_limits_default(JsLimits *lim) { static int space_setup(JsSpace *s, const JsLimits *lim) { memset(s, 0, sizeof *s); s->spill_fd = -1; + s->dir_fd = -1; if (lim) s->limits = *lim; else js_limits_default(&s->limits); if (!s->limits.max_reals || !s->limits.max_branches || s->limits.max_branches > JS_MAX_BRANCHES) return JS_ERR_ARG; @@ -641,9 +643,9 @@ static int space_setup(JsSpace *s, const JsLimits *lim) { int js_space_init_limits(JsSpace *s, const char *spill_path, const JsLimits *lim) { int rc = space_setup(s, lim); if (rc) return rc; - s->spill_fd = open(spill_path, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + s->spill_fd = omega_fs_open_regular(spill_path, O_RDWR | O_CREAT | O_TRUNC, 0600); if (s->spill_fd < 0) { js_space_destroy(s); return JS_ERR_IO; } - unlink(spill_path); /* anonymous from here on */ + omega_fs_unlink_at(AT_FDCWD, spill_path, 0); /* anonymous from here on */ return JS_OK; } @@ -657,6 +659,8 @@ void js_space_destroy(JsSpace *s) { if (s->branches[i]) release_u(s, i); if (s->spill_fd >= 0) close(s->spill_fd); s->spill_fd = -1; + if (s->dir_fd >= 0) close(s->dir_fd); + s->dir_fd = -1; free(s->cache_bytes); s->cache_bytes = NULL; s->cache_r = NULL; @@ -1533,13 +1537,6 @@ static uint64_t rd64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; static void put32(Buf *b, uint32_t v) { uint8_t o[4]; le32(o, v); buf_put(b, o, 4); } static void put64(Buf *b, uint64_t v) { uint8_t o[8]; le64(o, v); buf_put(b, o, 8); } -static char *path_in(const char *dir, const char *name) { - size_t n = strlen(dir) + strlen(name) + 2; - char *p = malloc(n); - if (p) snprintf(p, n, "%s/%s", dir, name); - return p; -} - static int write_all(int fd, const uint8_t *p, size_t n) { while (n) { ssize_t w = write(fd, p, n); @@ -1663,21 +1660,23 @@ static int commit_u(JsSpace *s) { sha256_hash(h, 96, h + 96); /* 4. Temporary file, fsync, atomic rename, directory fsync. */ - char *tmp = path_in(s->dir, "jspace.meta.tmp"), *fin = path_in(s->dir, "jspace.meta"); - int fd = tmp && fin ? open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600) : -1; + if (omega_fs_replace_ok_at(s->dir_fd, "jspace.meta") != 0) { + free(body.p); + return JS_ERR_IO; + } + char tmp[OMEGA_FS_NAME_MAX + 1] = { 0 }; + int fd = omega_fs_temp_name(tmp, sizeof tmp, "jspace.meta.tmp") == 0 + ? omega_fs_open_regular_at(s->dir_fd, tmp, O_WRONLY | O_CREAT | O_EXCL, 0600) + : -1; rc = fd < 0 ? JS_ERR_IO : JS_OK; if (rc == JS_OK) rc = write_all(fd, h, sizeof h); if (rc == JS_OK && body.n) rc = write_all(fd, body.p, body.n); if (rc == JS_OK && fsync(fd)) rc = JS_ERR_IO; if (fd >= 0) close(fd); - if (rc == JS_OK && rename(tmp, fin)) rc = JS_ERR_IO; - if (rc == JS_OK) { - int dfd = open(s->dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); - if (dfd < 0 || fsync(dfd)) rc = JS_ERR_IO; - if (dfd >= 0) close(dfd); - } - if (rc != JS_OK && tmp) unlink(tmp); - free(tmp); free(fin); free(body.p); + if (rc == JS_OK && omega_fs_rename_at(s->dir_fd, tmp, "jspace.meta")) rc = JS_ERR_IO; + if (rc == JS_OK && fsync(s->dir_fd)) rc = JS_ERR_IO; + if (rc != JS_OK && tmp[0]) omega_fs_unlink_at(s->dir_fd, tmp, 0); + free(body.p); if (rc != JS_OK) return rc; /* 5. The new checkpoint names none of the quarantined extents. */ @@ -1915,23 +1914,26 @@ static int load_u(JsSpace *s, const uint8_t *f, size_t fn, return rc; } -int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizers, - uint32_t n_realizers, const JsLimits *lim, const JsHome *local_home) { - if (!dir) return JS_ERR_ARG; +static int space_open_fd(JsSpace *s, int dir_fd, const char *dir, + const JsRealizer *const *realizers, uint32_t n_realizers, + const JsLimits *lim, const JsHome *local_home) { int rc = space_setup(s, lim); - if (rc) return rc; + if (rc) { close(dir_fd); return rc; } + s->dir_fd = dir_fd; if (local_home) s->local_home = *local_home; s->local_home.locality = JS_HOME_LOCAL; s->durable = true; - if (mkdir(dir, 0700) && errno != EEXIST) { js_space_destroy(s); return JS_ERR_IO; } s->dir = strdup(dir); - char *data = path_in(dir, "jspace.data"), *meta = path_in(dir, "jspace.meta"); - char *tmp = path_in(dir, "jspace.meta.tmp"); - if (!s->dir || !data || !meta || !tmp) { rc = JS_ERR_NOMEM; goto out; } - unlink(tmp); /* a torn temporary checkpoint is never authoritative */ - s->spill_fd = open(data, O_RDWR | O_CREAT | O_CLOEXEC, 0600); + if (!s->dir) { rc = JS_ERR_NOMEM; goto out; } + /* Legacy fixed-name temporary checkpoints are non-authoritative. Removing + * the directory entry cannot follow a planted symlink. */ + if (omega_fs_unlink_at(s->dir_fd, "jspace.meta.tmp", 0) != 0 && errno != ENOENT) { + rc = JS_ERR_IO; + goto out; + } + s->spill_fd = omega_fs_open_regular_at(s->dir_fd, "jspace.data", O_RDWR | O_CREAT, 0600); if (s->spill_fd < 0) { rc = JS_ERR_IO; goto out; } - int mfd = open(meta, O_RDONLY | O_CLOEXEC); + int mfd = omega_fs_open_regular_at(s->dir_fd, "jspace.meta", O_RDONLY, 0); if (mfd < 0) { if (errno != ENOENT) { rc = JS_ERR_IO; goto out; } /* No checkpoint yet: nothing in the data file is named. */ @@ -1955,11 +1957,27 @@ int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizer free(f); close(mfd); out: - free(data); free(meta); free(tmp); if (rc) js_space_destroy(s); return rc; } +int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizers, + uint32_t n_realizers, const JsLimits *lim, const JsHome *local_home) { + if (!dir) return JS_ERR_ARG; + int dir_fd = omega_fs_root_open(dir, 1, 0700); + if (dir_fd < 0) return JS_ERR_IO; + return space_open_fd(s, dir_fd, dir, realizers, n_realizers, lim, local_home); +} + +int js_space_open_at(JsSpace *s, int root_fd, const char *name, + const JsRealizer *const *realizers, uint32_t n_realizers, + const JsLimits *lim, const JsHome *local_home) { + if (root_fd < 0 || !name || strchr(name, '/')) return JS_ERR_ARG; + int dir_fd = omega_fs_subdir_open(root_fd, name, 1, 0700); + if (dir_fd < 0) return JS_ERR_IO; + return space_open_fd(s, dir_fd, name, realizers, n_realizers, lim, local_home); +} + int js_branch_info(JsSpace *s, JsBranchRef ref, JsBranchInfo *out) { if (!out) return JS_ERR_ARG; LOCK(s); diff --git a/src/runtime/rx_jspace.h b/src/runtime/rx_jspace.h index 28bada054..27625bfee 100644 --- a/src/runtime/rx_jspace.h +++ b/src/runtime/rx_jspace.h @@ -299,6 +299,7 @@ typedef struct { JsStats stats; uint64_t clock; int spill_fd; + int dir_fd; /* retained validated durable directory, or -1 */ uint64_t spill_end; bool reuse_enabled; /* Bytes of the last realization rebuilt, so a sequential read of a @@ -358,6 +359,10 @@ void js_limits_default(JsLimits *lim); * (js_space_anchor returns 0). Commits write version 3. */ int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizers, uint32_t n_realizers, const JsLimits *lim, const JsHome *local_home); +/* Open one durable subdirectory relative to a retained validated directory. */ +int js_space_open_at(JsSpace *s, int root_fd, const char *name, + const JsRealizer *const *realizers, uint32_t n_realizers, + const JsLimits *lim, const JsHome *local_home); /* Make the current non-staged state the durable checkpoint: data first * (fdatasync), then metadata to a temporary file, fsync, atomic rename, * directory fsync. Only then are quarantined extents reusable. */ diff --git a/src/runtime/rx_world.c b/src/runtime/rx_world.c index 379e41c85..7cc4563ab 100644 --- a/src/runtime/rx_world.c +++ b/src/runtime/rx_world.c @@ -21,7 +21,7 @@ #include "rx_argus.h" #include "sha256.h" #include "rx_generation.h" /* RX_GEN_HALT_MARK */ -#include "omega_path_check.h" +#include "omega_fs.h" #include #include @@ -959,72 +959,77 @@ static int seal_append(char *buf, size_t len, size_t cap) { return (int)len + 72; } -static int fsync_dir(const char *dir) { - int fd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); - if (fd < 0) return -errno; - int rc = fsync(fd) == 0 ? 0 : -errno; - close(fd); - return rc; -} - /* Write `buf` to dir/name durably (temporary file, fsync, rename, fsync dir). * With excl, an existing dir/name is never replaced (link, not rename). The * name is one checked component (src/omega_path_check.h, no '/'); dir is - * w->halt_dir, checked when it was set. */ -static int durable_write(const char *dir, const char *name, const char *buf, size_t len, bool excl) { - char nm[160], tmp[512], dst[512]; + * w->halt_fd, validated and retained when it was set. */ +static int durable_write(int dir_fd, const char *name, const char *buf, size_t len, bool excl) { + char nm[160], stem[200], tmp[OMEGA_FS_NAME_MAX + 1]; if (omega_path_copy(nm, sizeof nm, name) != 0 || strchr(nm, '/') || strcmp(nm, ".") == 0) return -EINVAL; - if (snprintf(tmp, sizeof tmp, "%s/%s.partial", dir, nm) >= (int)sizeof tmp || - snprintf(dst, sizeof dst, "%s/%s", dir, nm) >= (int)sizeof dst) + if (snprintf(stem, sizeof stem, "%s.partial", nm) >= (int)sizeof stem || + omega_fs_temp_name(tmp, sizeof tmp, stem) != 0) return -ENAMETOOLONG; - int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (!excl && omega_fs_replace_ok_at(dir_fd, nm) != 0) return -errno; + int fd = omega_fs_open_regular_at(dir_fd, tmp, O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd < 0) return -errno; size_t off = 0; while (off < len) { ssize_t n = write(fd, buf + off, len - off); if (n < 0 && errno == EINTR) continue; - if (n <= 0) { int e = n < 0 ? errno : EIO; close(fd); unlink(tmp); return -e; } + if (n <= 0) { + int e = n < 0 ? errno : EIO; + close(fd); + omega_fs_unlink_at(dir_fd, tmp, 0); + return -e; + } off += (size_t)n; } - if (fsync(fd) != 0) { int e = errno; close(fd); unlink(tmp); return -e; } + if (fsync(fd) != 0) { + int e = errno; + close(fd); + omega_fs_unlink_at(dir_fd, tmp, 0); + return -e; + } close(fd); if (excl) { - if (link(tmp, dst) != 0) { int e = errno; unlink(tmp); return -e; } - unlink(tmp); - } else if (rename(tmp, dst) != 0) { - int e = errno; unlink(tmp); return -e; + if (omega_fs_link_at(dir_fd, tmp, nm) != 0) { + int e = errno; + omega_fs_unlink_at(dir_fd, tmp, 0); + return -e; + } + omega_fs_unlink_at(dir_fd, tmp, 0); + } else if (omega_fs_rename_at(dir_fd, tmp, nm) != 0) { + int e = errno; + omega_fs_unlink_at(dir_fd, tmp, 0); + return -e; } - return fsync_dir(dir); + return fsync(dir_fd) == 0 ? 0 : -errno; } static int halt_mark_write(const RxWorld *w) { - if (!w->halt_dir[0]) return 0; + if (w->halt_fd < 0) return 0; char buf[512]; int n = halt_record(&w->halt, buf, sizeof buf); if (n < 0 || (n = seal_append(buf, (size_t)n, sizeof buf)) < 0) return -EOVERFLOW; - int rc = durable_write(w->halt_dir, RX_GEN_HALT_MARK, buf, (size_t)n, false); + int rc = durable_write(w->halt_fd, RX_GEN_HALT_MARK, buf, (size_t)n, false); return rc == 0 ? 1 : rc; } /* Read dir/OPERATOR_HALT. 0 absent, 1 read and its seal holds (h filled), * RX_ERR_TORN present but unreadable as a sealed record, RX_ERR_IO cannot tell. */ -static int halt_mark_read(const char *dir, RxHaltStatus *h, char *raw, size_t cap, size_t *raw_len) { - char path[512]; - if (snprintf(path, sizeof path, "%s/%s", dir, RX_GEN_HALT_MARK) >= (int)sizeof path) - return RX_ERR_IO; +static int halt_mark_read(int dir_fd, RxHaltStatus *h, char *raw, size_t cap, size_t *raw_len) { raw[0] = 0; *raw_len = 0; /* Never follow a link or block on a FIFO under the world locks: a mark that * is not a regular file is damaged (TORN: the world stays stopped, and resume * keeps a record and removes it, so the store is never blocked for good). */ - int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK); - if (fd < 0) return errno == ENOENT ? 0 : errno == ELOOP ? RX_ERR_TORN : RX_ERR_IO; struct stat st; - if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { - close(fd); - return RX_ERR_TORN; - } + if (omega_fs_lstat_at(dir_fd, RX_GEN_HALT_MARK, &st) != 0) + return errno == ENOENT ? 0 : RX_ERR_IO; + if (!S_ISREG(st.st_mode)) return RX_ERR_TORN; + int fd = omega_fs_open_regular_at(dir_fd, RX_GEN_HALT_MARK, O_RDONLY | O_NONBLOCK, 0); + if (fd < 0) return RX_ERR_TORN; ssize_t n = read(fd, raw, cap - 1); close(fd); if (n < 0) return RX_ERR_IO; @@ -1067,16 +1072,20 @@ static uint64_t halt_crumb(RxWorld *w, RxCrumbKind kind, RxCapRef cap, int32_t r } int rx_world_set_halt_dir(RxWorld *w, const char *dir) { - /* Only the checked copy (src/omega_path_check.h) is kept and opened. */ + /* Retain the checked directory so every later operation stays in it. */ char checked[sizeof w->halt_dir]; if (!w || omega_path_copy(checked, sizeof checked, dir) != 0) return RX_ERR_ARG; + int dir_fd = omega_fs_root_open(checked, 0, 0); + if (dir_fd < 0) return RX_ERR_ARG; pthread_mutex_lock(&w->mu); pthread_mutex_lock(&w->callers_mu); + if (w->halt_fd >= 0) close(w->halt_fd); + w->halt_fd = dir_fd; memcpy(w->halt_dir, checked, strlen(checked) + 1); RxHaltStatus h; char raw[600]; size_t raw_len = 0; - int rc = halt_mark_read(w->halt_dir, &h, raw, sizeof raw, &raw_len); + int rc = halt_mark_read(w->halt_fd, &h, raw, sizeof raw, &raw_len); if (rc == 0 && w->halted) { w->halt.durable = halt_mark_write(w); /* a stop taken before the directory was set */ } else if (rc != 0 && !w->halted) { @@ -1139,14 +1148,14 @@ int rx_world_emergency_resume(RxWorld *w, uint32_t subject, const RxCallerCred * pthread_mutex_lock(&w->callers_mu); int rc = halt_authorize(w, subject, cred, cap); if (rc == RX_OK && !w->halted) rc = RX_HALT_NOT_STOPPED; - if (rc == RX_OK && w->halt_dir[0]) { + if (rc == RX_OK && w->halt_fd >= 0) { /* The stop record is kept, never deleted: its bytes, then the resume, * sealed together under a name no later resume can take; only then is * the mark removed. Any failure leaves the world stopped. */ RxHaltStatus disk; char raw[600]; size_t raw_len = 0; - int mr = halt_mark_read(w->halt_dir, &disk, raw, sizeof raw, &raw_len); + int mr = halt_mark_read(w->halt_fd, &disk, raw, sizeof raw, &raw_len); if (mr == RX_ERR_IO) rc = RX_ERR_IO; else if (mr != 0) { char buf[1200], name[160]; @@ -1158,11 +1167,12 @@ int rx_world_emergency_resume(RxWorld *w, uint32_t subject, const RxCallerCred * rc = RX_ERR_IO; snprintf(name, sizeof name, "%s.resumed.%llu.%llu", RX_GEN_HALT_MARK, (unsigned long long)w->halt.seq, (unsigned long long)t); - if (rc == RX_OK && durable_write(w->halt_dir, name, buf, (size_t)n, true) != 0) + if (rc == RX_OK && durable_write(w->halt_fd, name, buf, (size_t)n, true) != 0) + rc = RX_ERR_IO; + if (rc == RX_OK && + (omega_fs_unlink_at(w->halt_fd, RX_GEN_HALT_MARK, 0) != 0 || + fsync(w->halt_fd) != 0)) rc = RX_ERR_IO; - char mark[512]; - snprintf(mark, sizeof mark, "%s/%s", w->halt_dir, RX_GEN_HALT_MARK); - if (rc == RX_OK && (unlink(mark) != 0 || fsync_dir(w->halt_dir) != 0)) rc = RX_ERR_IO; } } if (rc == RX_OK) { @@ -2117,6 +2127,7 @@ static int init_common(RxWorld *w, RxCapRoot *root, const void *auth_ctx, return RX_ERR_ARG; if (!root && !validate) return RX_ERR_ARG; memset(w, 0, sizeof(*w)); + w->halt_fd = -1; w->root = root; pthread_mutex_init(&w->callers_mu, NULL); w->sequential = sequential; @@ -2189,6 +2200,8 @@ void rx_world_destroy(RxWorld *w) { w->recorder = NULL; w->recorder_release = NULL; w->recorder_ctx = NULL; + if (w->halt_fd >= 0) close(w->halt_fd); + w->halt_fd = -1; for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) free(w->subs[i]); rx_coherent_free(w); free(w->deferred); diff --git a/src/runtime/rx_world.h b/src/runtime/rx_world.h index f9f2603d3..24b125169 100644 --- a/src/runtime/rx_world.h +++ b/src/runtime/rx_world.h @@ -686,6 +686,7 @@ typedef struct RxWorld { bool halted; RxHaltStatus halt; char halt_dir[384]; /* durable mark directory; "" = none */ + int halt_fd; /* retained validated halt directory, or -1 */ /* M20 durable recorder (rx_world_set_recorder); at most one. */ RxRecordFn recorder; @@ -836,8 +837,9 @@ int rx_world_caller_check_fn(void *world, uint32_t subject, const RxCallerCred capability may also pause the world */ #define RX_HALT_ALREADY 1 /* stop: already stopped, nothing changed */ #define RX_HALT_NOT_STOPPED 2 /* resume: not stopped, nothing changed */ -/* A dir that src/omega_path_check.h refuses (a ".." component, a byte outside - * its set, too long) is RX_ERR_ARG, and the world's halt directory is unchanged. */ +/* A dir that src/omega_path_check.h refuses, has a symlink component, is not + * owned by this uid, or is group/other-writable is RX_ERR_ARG, and the world's + * halt directory is unchanged. A valid directory descriptor is retained. */ int rx_world_set_halt_dir(RxWorld *w, const char *dir); int rx_world_emergency_stop(RxWorld *w, uint32_t subject, const RxCallerCred *cred, RxCapRef cap, int32_t reason, RxHaltStatus *out); diff --git a/src/runtime/rxc_host_abi.c b/src/runtime/rxc_host_abi.c index 9de7b4717..6c67cc04c 100644 --- a/src/runtime/rxc_host_abi.c +++ b/src/runtime/rxc_host_abi.c @@ -35,7 +35,7 @@ #include "runtime/rx_graph.h" #include "runtime/rx_skillroute.h" #include "sha256.h" -#include "omega_path_check.h" +#include "omega_fs.h" #include #include @@ -264,28 +264,33 @@ int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size memset(&inf, 0, sizeof inf); inf.abi_version = RXC_HOST_ABI_VERSION; fill_identity(&inf, &self); - /* Check before mkdir (src/omega_path_check.h): a refused dir is an argument error and creates nothing. */ + /* Check and retain the root while probing identity and journal state. */ char checked[200]; if (omega_path_copy(checked, sizeof checked, dir) != 0) return RXC_HOST_E_ARG; - if (mkdir(checked, 0700) && errno != EEXIST) return RXC_HOST_E_ARG; + int dir_fd = omega_fs_root_open(checked, 1, 0700); + if (dir_fd < 0) return RXC_HOST_E_ARG; /* Identity first (rx_compose_open checks it again). */ - char p[256]; - snprintf(p, sizeof p, "%s/machine.id", dir); - if (access(p, F_OK) == 0) { + struct stat st; + if (omega_fs_lstat_at(dir_fd, "machine.id", &st) == 0) { AienMachineId stored; - if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) { + if (aien_mid_load_at(dir_fd, "machine.id", &stored) != AIEN_MID_OK || + !aien_mid_equal(&stored, &self)) { + close(dir_fd); if (info) *info = inf; return RXC_HOST_E_IDENTITY; } inf.machine_id_was_stored = 1; + } else if (errno != ENOENT) { + close(dir_fd); + return RXC_HOST_E_ARG; } /* Torn-tail probe: read-only, no lock, no repair. */ - snprintf(p, sizeof p, "%s/cortex.cx", dir); - if (access(p, F_OK) == 0) { + if (omega_fs_lstat_at(dir_fd, "cortex.cx", &st) == 0) { CxStore probe; memset(&probe, 0, sizeof probe); - int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); + int prc = cx_open_at(&probe, dir_fd, "cortex.cx", RX_CORTEX_SUBJECTS, + CX_OPEN_READONLY); if (prc == CX_OK) { inf.records = probe.n; cx_close(&probe); @@ -293,10 +298,15 @@ int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size inf.tail_torn = 1; } else { inf.open_rc = prc; + close(dir_fd); if (info) *info = inf; return RXC_HOST_E_OPEN; } + } else if (errno != ENOENT) { + close(dir_fd); + return RXC_HOST_E_OPEN; } + close(dir_fd); /* Strict: a torn tail is refused, never cut here (rxc_host_recover). */ if (inf.tail_torn) { if (info) *info = inf; @@ -319,7 +329,7 @@ int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size return RXC_HOST_E_FULL; } h->slot = slot; - snprintf(h->dir, sizeof h->dir, "%s", dir); + snprintf(h->dir, sizeof h->dir, "%s", checked); h->session = session; h->self = self; h->info = inf; @@ -557,46 +567,65 @@ int rxc_host_recover(const char *dir, uint32_t root_kind, const uint8_t *root, s AienMachineId self; if (aien_mid_derive((uint8_t)root_kind, root, root_len, &self) != AIEN_MID_OK) return RXC_HOST_E_IDENTITY; - char p[256]; - snprintf(p, sizeof p, "%s/machine.id", dir); + char checked[200]; + if (omega_path_copy(checked, sizeof checked, dir) != 0) return RXC_HOST_E_ARG; + int dir_fd = omega_fs_root_open(checked, 0, 0); + if (dir_fd < 0) return RXC_HOST_E_ARG; AienMachineId stored; - if (aien_mid_load(p, &stored) != AIEN_MID_OK || !aien_mid_equal(&stored, &self)) + if (aien_mid_load_at(dir_fd, "machine.id", &stored) != AIEN_MID_OK || + !aien_mid_equal(&stored, &self)) { + close(dir_fd); return RXC_HOST_E_IDENTITY; /* only the home's own machine repairs it */ - snprintf(p, sizeof p, "%s/cortex.cx", dir); + } struct stat st; - if (stat(p, &st) != 0) return RXC_HOST_OK; /* no journal: nothing to repair */ + if (omega_fs_lstat_at(dir_fd, "cortex.cx", &st) != 0) { + int absent = errno == ENOENT; + close(dir_fd); + return absent ? RXC_HOST_OK : RXC_HOST_E_OPEN; + } const uint64_t old_size = (uint64_t)st.st_size; /* Keep the tail bytes before anything is cut (the cut goes into the * repair record, so nothing leaves the journal unrecorded). */ uint64_t keep_n = old_size < RXC_HOST_REPAIR_KEEP_MAX ? old_size : RXC_HOST_REPAIR_KEEP_MAX; uint8_t *tail = malloc(keep_n ? keep_n : 1); - if (!tail) return RXC_HOST_E_NOMEM; - int fd = open(p, O_RDONLY | O_CLOEXEC); + if (!tail) { close(dir_fd); return RXC_HOST_E_NOMEM; } + int fd = omega_fs_open_regular_at(dir_fd, "cortex.cx", O_RDONLY, 0); if (fd < 0 || pread(fd, tail, keep_n, (off_t)(old_size - keep_n)) != (ssize_t)keep_n) { if (fd >= 0) close(fd); free(tail); + close(dir_fd); return RXC_HOST_E_OPEN; } close(fd); CxStore probe; memset(&probe, 0, sizeof probe); - int prc = cx_open(&probe, p, RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); + int prc = cx_open_at(&probe, dir_fd, "cortex.cx", RX_CORTEX_SUBJECTS, CX_OPEN_READONLY); if (prc == CX_OK) cx_close(&probe); else if (prc == CX_ERR_TORN) out->tail_torn = 1; - else { free(tail); return RXC_HOST_E_OPEN; } + else { free(tail); close(dir_fd); return RXC_HOST_E_OPEN; } /* The writer open: takes the journal lock (a live handle on this home * makes it fail: RXC_HOST_E_OPEN) and, if torn, truncates the one * incomplete trailing record. */ CxStore cx; memset(&cx, 0, sizeof cx); - if (cx_open(&cx, p, RX_CORTEX_SUBJECTS, CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) { + if (cx_open_at(&cx, dir_fd, "cortex.cx", RX_CORTEX_SUBJECTS, + CX_OPEN_SYNC | CX_OPEN_REPAIR_TAIL) != CX_OK) { free(tail); + close(dir_fd); return RXC_HOST_E_OPEN; } - if (stat(p, &st) != 0) { cx_close(&cx); free(tail); return RXC_HOST_E_OPEN; } + fd = omega_fs_open_regular_at(dir_fd, "cortex.cx", O_RDONLY, 0); + if (fd < 0 || fstat(fd, &st) != 0) { + if (fd >= 0) close(fd); + cx_close(&cx); + free(tail); + close(dir_fd); + return RXC_HOST_E_OPEN; + } + close(fd); const uint64_t new_size = (uint64_t)st.st_size; const uint64_t kept = cx.n; out->cut_lo = new_size; @@ -611,10 +640,10 @@ int rxc_host_recover(const char *dir, uint32_t root_kind, const uint8_t *root, s const JsRealizer *rz[1] = { &RXC_REALIZER }; JsSpace js; memset(&js, 0, sizeof js); - snprintf(p, sizeof p, "%s/jspace", dir); - if (js_space_open(&js, p, rz, 1, NULL, &home) != JS_OK) { + if (js_space_open_at(&js, dir_fd, "jspace", rz, 1, NULL, &home) != JS_OK) { cx_close(&cx); free(tail); + close(dir_fd); return RXC_HOST_E_OPEN; } uint8_t an[48]; @@ -691,6 +720,7 @@ done: js_space_destroy(&js); cx_close(&cx); free(tail); + close(dir_fd); if (rc == RXC_HOST_OK && out->repaired) { /* Trial open: does the composition accept the repaired home? */ RxcHost *t = NULL; diff --git a/src/runtime/rxc_host_abi.h b/src/runtime/rxc_host_abi.h index a08b8a319..36609f056 100644 --- a/src/runtime/rxc_host_abi.h +++ b/src/runtime/rxc_host_abi.h @@ -237,8 +237,9 @@ typedef struct { * that already holds machine.id must hold this machine (RXC_HOST_E_IDENTITY * otherwise). `info` (may be NULL) is filled on success and on * RXC_HOST_E_TORN / RXC_HOST_E_IDENTITY. `flags` must be 0. `dir` must - * pass src/omega_path_check.h (omega_path_copy) and be under 184 bytes; - * otherwise RXC_HOST_E_ARG and nothing is created. */ + * pass src/omega_path_check.h (omega_path_copy) and be under 184 bytes. An + * existing root must be owned by this uid, not group/other-writable, and have + * no symlink component; otherwise RXC_HOST_E_ARG and nothing is created. */ int rxc_host_open(const char *dir, uint32_t root_kind, const uint8_t *root, size_t root_len, uint64_t session, uint32_t flags, RxcHost **out, RxcHostInfo *info); diff --git a/tests/runtime/aien_machine_id_test.c b/tests/runtime/aien_machine_id_test.c index 452401c01..ca897272d 100644 --- a/tests/runtime/aien_machine_id_test.c +++ b/tests/runtime/aien_machine_id_test.c @@ -92,6 +92,39 @@ static void test_round_trip(void) { unlink(path); } +static void test_symlink_refusal(void) { + AienMachineId m; + aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, (const uint8_t *)"symlink-root", 12, &m); + char dir[] = "/tmp/aien_mid_symlink_XXXXXX"; + CHECK(mkdtemp(dir) != NULL); + char victim[160], path[160]; + snprintf(victim, sizeof victim, "%s/victim", dir); + snprintf(path, sizeof path, "%s/machine.id", dir); + int fd = open(victim, O_WRONLY | O_CREAT | O_EXCL, 0600); + CHECK(fd >= 0 && write(fd, "SAFE", 4) == 4); + if (fd >= 0) close(fd); + CHECK(symlink(victim, path) == 0); + CHECK(aien_mid_store(path, &m) == AIEN_MID_E_IO); + char got[5] = { 0 }; + fd = open(victim, O_RDONLY); + CHECK(fd >= 0 && read(fd, got, 4) == 4 && memcmp(got, "SAFE", 4) == 0); + if (fd >= 0) close(fd); + unlink(path); + unlink(victim); + rmdir(dir); + + char real[] = "/tmp/aien_mid_real_XXXXXX"; + char link[] = "/tmp/aien_mid_link_XXXXXX"; + CHECK(mkdtemp(real) != NULL && mkdtemp(link) != NULL); + CHECK(rmdir(link) == 0 && symlink(real, link) == 0); + snprintf(path, sizeof path, "%s/machine.id", link); + CHECK(aien_mid_store(path, &m) == AIEN_MID_E_IO); + snprintf(path, sizeof path, "%s/machine.id", real); + CHECK(access(path, F_OK) != 0); + unlink(link); + rmdir(real); +} + static void test_equality_hash(void) { AienMachineId a, b, c; uint8_t s[32]; @@ -208,6 +241,7 @@ int main(int argc, char **argv) { } test_derive_and_aienos_compat(); test_round_trip(); + test_symlink_refusal(); test_equality_hash(); test_conversions(); test_malformed(); diff --git a/tests/runtime/rx_compose_test.c b/tests/runtime/rx_compose_test.c index b9491923f..24eaef508 100644 --- a/tests/runtime/rx_compose_test.c +++ b/tests/runtime/rx_compose_test.c @@ -669,6 +669,36 @@ static void t_refused_dir(void) { CHECK(access(bad, F_OK) != 0, "refused dir not created"); } +static void t_symlink_confinement(void) { + printf("[*] symlink confinement: roots and identity files are refused\n"); + char real[240], link[240], identity[280], victim[280], got[5] = { 0 }; + dir_for(real, sizeof real, "symlink_real"); + dir_for(link, sizeof link, "symlink_root"); + CHECK(mkdir(real, 0700) == 0 && symlink(real, link) == 0, "symlink root setup"); + reset_c(); + CHECK(fx_open(&g_fx, &g_c, link, 1) == RX_ERR_ARG, "symlink root was accepted"); + snprintf(identity, sizeof identity, "%s/machine.id", real); + CHECK(access(identity, F_OK) != 0, "symlink root target was modified"); + unlink(link); + rmtree(real); + + dir_for(real, sizeof real, "symlink_identity"); + CHECK(mkdir(real, 0700) == 0, "identity directory setup"); + snprintf(victim, sizeof victim, "%s/victim", real); + FILE *f = fopen(victim, "wb"); + CHECK(f && fwrite("SAFE", 1, 4, f) == 4, "identity victim setup"); + if (f) fclose(f); + snprintf(identity, sizeof identity, "%s/machine.id", real); + CHECK(symlink(victim, identity) == 0, "identity symlink setup"); + reset_c(); + CHECK(fx_open(&g_fx, &g_c, real, 1) == RX_ERR_IDENTITY, "symlink identity was accepted"); + f = fopen(victim, "rb"); + CHECK(f && fread(got, 1, 4, f) == 4 && memcmp(got, "SAFE", 4) == 0, + "identity symlink target was modified"); + if (f) fclose(f); + rmtree(real); +} + int main(void) { snprintf(g_base, sizeof g_base, "/tmp/rx_compose_test.XXXXXX"); if (!mkdtemp(g_base)) { perror("mkdtemp"); return 1; } @@ -680,6 +710,7 @@ int main(void) { t_cortex(); t_pending(); t_refused_dir(); + t_symlink_confinement(); fx_free(&g_fx); rmtree(g_base); printf("%s: %d checks, %d failed\n", g_fail ? "FAIL" : "PASS", g_checks, g_fail); diff --git a/tests/runtime/rx_emergency.c b/tests/runtime/rx_emergency.c index b6ac43058..9ab51624b 100644 --- a/tests/runtime/rx_emergency.c +++ b/tests/runtime/rx_emergency.c @@ -629,9 +629,19 @@ static void e6_durable(void) { Env e; check(setup(&e, 1, 1u << 16) == 0, "E6 setup: world"); check(rx_world_set_halt_dir(&e.w, dir) == 0, "E6 no mark: set_halt_dir answers 0, world runs"); + char partial[200], victim[200]; + snprintf(partial, sizeof partial, "%s/%s.partial", dir, RX_GEN_HALT_MARK); + snprintf(victim, sizeof victim, "%s/halt-victim", dir); + FILE *vf = fopen(victim, "wb"); + check(vf && fwrite("SAFE", 1, 4, vf) == 4, "E6 setup: halt partial victim"); + if (vf) fclose(vf); + check(symlink(victim, partial) == 0, "E6 setup: planted predictable halt partial symlink"); RxHaltStatus h; check(rx_world_emergency_stop(&e.w, OP, &e.cr_op, e.c_op, 606, &h) == RX_OK && h.durable == 1, "E6 the stop wrote its mark durably (durable = 1)"); + char safe[8]; + check(read_file(victim, safe, sizeof safe) == 4 && memcmp(safe, "SAFE", 4) == 0, + "E6 halt partial symlink target was not modified"); int n = read_file(mark, raw, sizeof raw); char want_head[200]; snprintf(want_head, sizeof want_head, "aien-operator-halt v1\nseq 1\nsubject %u\ncap %u %llu\nreason 606\n", diff --git a/tests/runtime/rx_emergency_mutants.sh b/tests/runtime/rx_emergency_mutants.sh index 333487bae..4e3762a45 100755 --- a/tests/runtime/rx_emergency_mutants.sh +++ b/tests/runtime/rx_emergency_mutants.sh @@ -33,12 +33,12 @@ M4 caller check ignores the stop|src/runtime/rx_world.c|s/if \(w->halted\) \{\n M5 a reaction subject may stop the world|src/runtime/rx_world.c|s/if \(w->reactions\[i\]\.desc\.subject == subject\) return RX_ERR_AUTHORITY;/(void)i;/ M6 store ignores the durable mark|src/runtime/rx_generation.c|s/(static int halt_marked\(const RxGenStore \*store\) \{\n)/$1 if (store) return 0;\n/ M7 a restarted world ignores the mark|src/runtime/rx_world.c|s/\} else if \(rc != 0 && !w->halted\) \{/} else if (0) {/ -M8 resume deletes the mark without keeping the record|src/runtime/rx_world.c|s/if \(rc == RX_OK && durable_write\(w->halt_dir, name, buf, \(size_t\)n, true\) != 0\)/if (0)/ +M8 resume deletes the mark without keeping the record|src/runtime/rx_world.c|s/if \(rc == RX_OK && durable_write\(w->halt_fd, name, buf, \(size_t\)n, true\) != 0\)/if (0)/ M9 the stop is not in force until the disk write returns|src/runtime/rx_world.c|s/( w->halted = true; \/\* in force before any disk work \*\/\n)( w->halt\.durable = halt_mark_write\(w\);\n)/$2 if (w->halt.durable != 1) w->halted = false; else w->halted = true;\n/ M10 a refused activation spends the episode budget|src/runtime/rx_world.c|s/ if \(r->episode_activations\) r->episode_activations--;\n r->rearm = true;\n end_activation\(w, rid\);/ r->rearm = true;\n end_activation(w, rid);/ M11 object create under a stop|src/runtime/rx_world.c|s/(RxObjRef \*out\) \{\n pthread_mutex_lock\(&w->mu\);\n )if \(w->halted\)/${1}if (0)/ M12 object retire under a stop|src/runtime/rx_world.c|s/(int rx_world_retire\(RxWorld \*w, RxObjRef ref\) \{\n pthread_mutex_lock\(&w->mu\);\n )if \(w->halted\)/${1}if (0)/ -M13 a symlink mark reads as absent|src/runtime/rx_world.c|s/errno == ELOOP \? RX_ERR_TORN : RX_ERR_IO/errno == ELOOP ? 0 : RX_ERR_IO/' +M13 a symlink mark reads as absent|src/runtime/rx_world.c|s/if \(!S_ISREG\(st\.st_mode\)\) return RX_ERR_TORN;/if (!S_ISREG(st.st_mode)) return 0;/' build_run() { # dir -> exit status of the test (cd "$1" && $CC -std=gnu11 -Wall -Wextra -D_GNU_SOURCE -O2 -Isrc -pthread -o t $SRCS) >"$1/build.log" 2>&1 || { echo "build failed:"; tail -5 "$1/build.log"; return 99; } diff --git a/tests/runtime/rx_jspace_prod.c b/tests/runtime/rx_jspace_prod.c index b3b5a0b3b..80bfdba6f 100644 --- a/tests/runtime/rx_jspace_prod.c +++ b/tests/runtime/rx_jspace_prod.c @@ -520,6 +520,24 @@ static void test_durable(void) { verify_reopen(&sv, "leftover temp file"); CHECK(access(tmp, F_OK) != 0, "temp checkpoint not removed"); + /* A planted durable data symlink is refused without touching its target. */ + char bad[] = "/tmp/jspace_symlink_XXXXXX"; + CHECK(mkdtemp(bad) != NULL, "symlink directory"); + char bad_data[160], victim[160], safe[5] = { 0 }; + snprintf(bad_data, sizeof bad_data, "%s/jspace.data", bad); + snprintf(victim, sizeof victim, "%s/victim", bad); + fd = open(victim, O_WRONLY | O_CREAT | O_EXCL, 0600); + CHECK(fd >= 0 && write(fd, "SAFE", 4) == 4, "symlink victim"); + if (fd >= 0) close(fd); + CHECK(symlink(victim, bad_data) == 0, "data symlink"); + CHECK(js_space_open(&s, bad, RZS, 2, NULL, NULL) == JS_ERR_IO, + "symlinked durable data file accepted"); + fd = open(victim, O_RDONLY); + CHECK(fd >= 0 && read(fd, safe, 4) == 4 && memcmp(safe, "SAFE", 4) == 0, + "durable data symlink target modified"); + if (fd >= 0) close(fd); + unlink(bad_data); unlink(victim); rmdir(bad); + /* Commit, reopen, commit again: sequence advances, state holds; released * extents are reused only after the commit that stops naming them. */ CHECK(js_space_open(&s, g_dir, RZS, 2, NULL, NULL) == JS_OK, "open"); diff --git a/tests/runtime/rx_r9_barrier.c b/tests/runtime/rx_r9_barrier.c index e324e35f8..565a952e1 100644 --- a/tests/runtime/rx_r9_barrier.c +++ b/tests/runtime/rx_r9_barrier.c @@ -861,6 +861,39 @@ static void read_blob_checked(void) { rm_tree(dir); } +static void symlink_persistence_refused(void) { + char dir[256], tmp[320], legacy[384], victim[320], got[5] = { 0 }; + CHECK(fresh_dir(dir, sizeof dir) == 0, "symlink temp directory"); + snprintf(tmp, sizeof tmp, "%s/tmp", dir); + snprintf(legacy, sizeof legacy, "%s/write.partial", tmp); + snprintf(victim, sizeof victim, "%s/victim", dir); + CHECK(mkdir(tmp, 0755) == 0, "symlink tmp setup"); + FILE *f = fopen(victim, "wb"); + CHECK(f && fwrite("SAFE", 1, 4, f) == 4, "symlink victim setup"); + if (f) fclose(f); + CHECK(symlink(victim, legacy) == 0, "legacy write.partial symlink setup"); + RxGenStore *s = NULL; + CHECK(rx_gen_open(dir, &s) == RX_GEN_OK && s, + "a planted predictable temporary redirected the exclusive temporary path"); + if (s) rx_gen_close(s); + f = fopen(victim, "rb"); + CHECK(f && fread(got, 1, 4, f) == 4 && memcmp(got, "SAFE", 4) == 0, + "generation temporary symlink target was modified"); + if (f) fclose(f); + rm_tree(dir); + + char real[256], link[256]; + CHECK(fresh_dir(real, sizeof real) == 0 && fresh_dir(link, sizeof link) == 0, + "symlink root directories"); + CHECK(rmdir(link) == 0 && symlink(real, link) == 0, "generation root symlink setup"); + s = NULL; + CHECK(rx_gen_open(link, &s) != RX_GEN_OK && !s, "generation symlink root was accepted"); + snprintf(victim, sizeof victim, "%s/active", real); + CHECK(access(victim, F_OK) != 0, "generation symlink root target was modified"); + unlink(link); + rm_tree(real); +} + int main(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); if (argc >= 4 && strcmp(argv[1], "--crash") == 0) return child_crash(argv[2], atoi(argv[3])); @@ -885,6 +918,7 @@ int main(int argc, char **argv) { check_crash(exe, steps[i], want[i], text[i], receipt_already[i]); negative_and_live(); read_blob_checked(); + symlink_persistence_refused(); drafts_are_released(); if (g_fail) { fprintf(stderr, "%d generation-barrier failures\n", g_fail);