Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/osh/host/test_osh_journal.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / osh / host / test_osh_journal.c` 1214 lines ยท 50046 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * test_osh_journal.c -- durable intent/outcome journal of the osh Linux host adapter (OSH_PLATFORM_ABI.md section 10, 19 * aien-architecture#158). Records are hand-built; everything happens in a disposable temp dir under $TMPDIR. 20 * The binary is its own helper program: `test_osh_journal --helper append FILE` (create + append one byte) and 21 * `--helper sleep MS`. Scenarios that kill, interrupt or crash the shell run it in a forked child. 22 */ 23 #ifndef _GNU_SOURCE 24 #define _GNU_SOURCE 25 #endif 26 #include "osh_host.h" 27 #include "osh_journal.h" 28 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 42 static int g_fail, g_checks; 43 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; printf("FAIL line %d: %s -- ", __LINE__, #c); printf(__VA_ARGS__); printf("\n"); } } while (0) 44 45 static char g_self[1024], g_dir[512]; 46 static int g_null; 47 48 static void msleep(int ms) { struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L}; nanosleep(&ts, NULL); } 49 50 static int helper_main(int argc, char **argv) 51 { 52 if (argc >= 4 && !strcmp(argv[2], "append")) { 53 int fd = open(argv[3], O_WRONLY | O_CREAT | O_APPEND, 0666); 54 if (fd < 0 || write(fd, "x", 1) != 1) return 1; 55 return 0; 56 } 57 if (argc >= 4 && !strcmp(argv[2], "sleep")) { msleep(atoi(argv[3])); return 0; } 58 if (argc >= 3 && !strcmp(argv[2], "quiet")) return 0; 59 return 2; 60 } 61 62 /* ---------------- plumbing ---------------- */ 63 64 static int count_fds(void) 65 { 66 int n = 0; 67 DIR *d = opendir("/proc/self/fd"); 68 int dfd = dirfd(d); 69 struct dirent *e; 70 while ((e = readdir(d))) if (e->d_name[0] != '.' && atoi(e->d_name) != dfd) n++; 71 closedir(d); 72 return n; 73 } 74 75 static char *slurp(const char *path, size_t *n) 76 { 77 FILE *f = fopen(path, "rb"); 78 char *b = calloc(1, 1 << 16); 79 size_t len = f ? fread(b, 1, (1 << 16) - 1, f) : 0; 80 if (f) fclose(f); 81 if (n) *n = len; 82 return b; 83 } 84 85 static int count_sub(const char *hay, const char *needle) 86 { 87 int n = 0; 88 for (const char *p = hay; (p = strstr(p, needle)); p += strlen(needle)) n++; 89 return n; 90 } 91 92 static long fsize(const char *path) { struct stat st; return stat(path, &st) == 0 ? (long)st.st_size : -1; } 93 94 typedef struct { 95 OshSession s; 96 OshJournal j; 97 char jpath[4400]; 98 char dir[4200]; 99 } T; 100 101 static int g_tn; 102 103 static void t_begin(T *t, int with_journal) 104 { 105 memset(t, 0, sizeof *t); 106 snprintf(t->dir, sizeof t->dir, "%s/t%d", g_dir, ++g_tn); 107 mkdir(t->dir, 0700); 108 snprintf(t->jpath, sizeof t->jpath, "%s/journal.log", t->dir); 109 char *envp[] = {"PATH=/usr/bin:/bin", "HOME=/tmp", NULL}; 110 osh_session_init(&t->s, envp); 111 for (int i = 0; i < 3; i++) t->s.fd[i] = g_null; 112 t->s.binding.valid = 1; 113 t->s.binding.domain = 2; 114 t->s.binding.cap_generation = 1; 115 if (with_journal) { 116 int rc = osh_journal_open(&t->j, t->jpath); 117 CHECK(rc == 0, "journal open: %d", rc); 118 t->s.journal = &t->j; 119 } 120 } 121 122 static void t_end(T *t) 123 { 124 if (t->s.journal) osh_journal_close(&t->j); 125 osh_session_free(&t->s); 126 } 127 128 static void mpath(const T *t, const char *name, char *out, size_t cap) { snprintf(out, cap, "%s/%s", t->dir, name); } 129 130 static OshBuilder *nb(void) 131 { 132 OshBuilder *b = malloc(sizeof *b); 133 osh_rb_init(b, 0, 0); 134 return b; 135 } 136 137 static void helper(OshBuilder *b, const char *op, const char *arg) 138 { 139 osh_rb_cmd(b, 0); 140 osh_rb_arg(b, g_self); 141 osh_rb_arg(b, "--helper"); 142 osh_rb_arg(b, op); 143 if (arg) osh_rb_arg(b, arg); 144 } 145 146 static int run(T *t, OshBuilder *b, OshResult *r) 147 { 148 osh_rb_seal(b); 149 return osh_exec_record(&t->s, b->rec, osh_rb_cells(b), b->out, b->out_used, r); 150 } 151 152 static char *jtext(const T *t) { return slurp(t->jpath, NULL); } 153 154 /* the Nth line (0-based) of a text blob, copied */ 155 static int line_has(const char *text, int n, const char *needle) 156 { 157 const char *p = text; 158 for (int i = 0; i < n && p; i++) { p = strchr(p, '\n'); if (p) p++; } 159 if (!p) return 0; 160 const char *e = strchr(p, '\n'); 161 size_t len = e ? (size_t)(e - p) : strlen(p); 162 char *ln = strndup(p, len); 163 int ok = strstr(ln, needle) != NULL; 164 free(ln); 165 return ok; 166 } 167 168 /* ONE ACCOUNT: for every command of the request the LAST outcome record in the journal names the same outcome and status as 169 * the in-memory result. Returns the number of commands that disagree (or have no outcome record). */ 170 static int same_account(const T *t, int ncmds, const OshResult *r) 171 { 172 static const char *const nm[] = {"NOT_STARTED", "COMPLETED", "FAILED_NO_EFFECT", "CANCELLED", "OUTCOME_UNKNOWN"}; 173 char *j = jtext(t); 174 int bad = 0; 175 for (int i = 0; i < ncmds; i++) { 176 char key[32], want[96], *hit = NULL; 177 snprintf(key, sizeof key, " %d/%d ", i, ncmds); 178 snprintf(want, sizeof want, "%s st=%d err=%d", nm[r->cmd[i].outcome], r->cmd[i].status, r->cmd[i].err); 179 for (char *p = j; p && *p;) { 180 char *e = strchr(p, '\n'); 181 if (strstr(p, " O ") && strstr(p, key) && (!e || strstr(p, key) < e)) hit = p; 182 p = e ? e + 1 : NULL; 183 } 184 char *e = hit ? strchr(hit, '\n') : NULL; 185 if (!hit || !(e ? (size_t)(e - hit) : strlen(hit)) || !memmem(hit, e ? (size_t)(e - hit) : strlen(hit), want, strlen(want))) { 186 bad++; 187 printf(" account mismatch cmd %d: want '%s' in: %.*s\n", i, want, hit ? (int)(e ? e - hit : (long)strlen(hit)) : 4, hit ? hit : "none"); 188 } 189 } 190 free(j); 191 return bad; 192 } 193 194 static int wait_file(const char *path, long size, int ms) 195 { 196 for (int i = 0; i < ms / 20; i++) { if (fsize(path) >= size) return 1; msleep(20); } 197 return fsize(path) >= size; 198 } 199 200 /* run fn in a forked child (own process group when grp); returns the child's wait status */ 201 static int in_child(void (*fn)(T *, void *), T *t, void *arg, int grp) 202 { 203 fflush(stdout); 204 pid_t pid = fork(); 205 if (pid == 0) { 206 if (grp) setpgid(0, 0); 207 fn(t, arg); 208 _exit(0); 209 } 210 int st = 0; 211 waitpid(pid, &st, 0); 212 if (grp) kill(-pid, SIGKILL); 213 return st; 214 } 215 216 /* ---------------- tests ---------------- */ 217 218 static void test_secret_names(void) 219 { 220 CHECK(osh_secret_name("DB_PASSWORD") && osh_secret_name("api_token") && osh_secret_name("AWS_SECRET_ACCESS_KEY") && 221 osh_secret_name("Authorization"), "secret-class names are caught"); 222 CHECK(!osh_secret_name("PATH") && !osh_secret_name("HOME") && !osh_secret_name("LANG") && !osh_secret_name("PLAINVAR"), 223 "ordinary names are not (negative control)"); 224 } 225 226 static void test_normal_and_redaction(void) 227 { 228 T t; 229 t_begin(&t, 1); 230 char marker[4400]; 231 mpath(&t, "m1", marker, sizeof marker); 232 int fds0 = count_fds(); 233 OshBuilder *b = nb(); 234 helper(b, "append", marker); 235 osh_rb_arg(b, "--password=hunter2XYZ"); 236 osh_rb_assign(b, "API_TOKEN", "hunter2XYZ"); 237 osh_rb_assign(b, "PLAINVAR", "visibleval"); 238 char outf[4400]; 239 mpath(&t, "out-hunter2XYZ.txt", outf, sizeof outf); 240 osh_rb_redir(b, OSH_R_OUT, 1, outf, 0); 241 OshResult r; 242 int st = run(&t, b, &r); 243 free(b); 244 CHECK(st == 0 && r.cmd[0].outcome == OSH_OUT_COMPLETED && fsize(marker) == 1, "ran once (st %d)", st); 245 CHECK(same_account(&t, 1, &r) == 0, "normal: record == result"); 246 char *j = jtext(&t); 247 CHECK(count_sub(j, " I ") == 1 && count_sub(j, " O ") == 1, "one intent and one outcome:\n%s", j); 248 CHECK(line_has(j, 0, " I ") && line_has(j, 1, " O 1 ") && line_has(j, 1, "COMPLETED st=0 err=0"), "order and ref:\n%s", j); 249 CHECK(line_has(j, 0, "argc=") && line_has(j, 0, "wr=1"), "write-class open counted:\n%s", j); 250 CHECK(!strstr(j, "hunter2XYZ"), "secret value absent from record bytes"); 251 CHECK(!strstr(j, "visibleval") && !strstr(j, "PLAINVAR") && !strstr(j, "API_TOKEN") && !strstr(j, "password"), 252 "no assignment, argument or target text at all"); 253 CHECK(strstr(j, "a0=") != NULL, "argv[0] is recorded"); 254 free(j); 255 struct stat sb; 256 CHECK(stat(t.jpath, &sb) == 0 && (sb.st_mode & 0777) == 0600, "journal mode 0600"); 257 CHECK(count_fds() == fds0, "no descriptor leaked by a run (%d vs %d)", count_fds(), fds0); 258 t_end(&t); 259 260 /* digest: a secret-named assignment's VALUE does not change it; an ordinary value and an argument do (control) */ 261 char kpa[700]; 262 snprintf(kpa, sizeof kpa, "%s/digest-a.log", g_dir); 263 OshJournal ja; 264 CHECK(osh_journal_open(&ja, kpa) == 0, "open journal A: %s", ja.why); 265 static OshRequest ra, rb, rc, rd; 266 OshBuilder *x[4]; 267 OshRequest *rq[4] = {&ra, &rb, &rc, &rd}; 268 const char *tok[4] = {"aaa", "bbb", "aaa", "aaa"}, *pv[4] = {"v", "v", "w", "v"}, *arg[4] = {"p", "p", "p", "q"}; 269 uint8_t dg[4][32]; 270 for (int i = 0; i < 4; i++) { 271 x[i] = nb(); 272 osh_rb_cmd(x[i], 0); 273 osh_rb_arg(x[i], "prog"); 274 osh_rb_arg(x[i], arg[i]); 275 osh_rb_assign(x[i], "MY_TOKEN", tok[i]); 276 osh_rb_assign(x[i], "PLAINVAR", pv[i]); 277 osh_rb_seal(x[i]); 278 CHECK(osh_req_decode(x[i]->rec, osh_rb_cells(x[i]), x[i]->out, x[i]->out_used, rq[i]) == 0, "decode"); 279 osh_req_digest(&ja, rq[i], dg[i]); 280 free(x[i]); 281 } 282 CHECK(!memcmp(dg[0], dg[1], 32), "secret value masked in the digest"); 283 CHECK(memcmp(dg[0], dg[2], 32) && memcmp(dg[0], dg[3], 32), "ordinary value and argument change the digest"); 284 osh_journal_close(&ja); 285 } 286 287 static void hexs(const uint8_t d[32], char o[65]) 288 { 289 for (int i = 0; i < 32; i++) snprintf(o + 2 * i, 3, "%02x", d[i]); 290 } 291 292 /* The digest is keyed: two journals give two digests for one request, and a low-entropy secret passed as a plain argument 293 * cannot be confirmed by hashing guesses (the plain SHA-256 of every PIN does not match the recorded digest). */ 294 static void test_keyed_digest(void) 295 { 296 char pa[700], pb[700]; 297 snprintf(pa, sizeof pa, "%s/keyed-a.log", g_dir); 298 snprintf(pb, sizeof pb, "%s/keyed-b.log", g_dir); 299 OshJournal a, b2, a2; 300 CHECK(osh_journal_open(&a, pa) == 0 && osh_journal_open(&b2, pb) == 0, "two journals, two keys"); 301 CHECK(memcmp(a.key, b2.key, 32) != 0, "keys differ between journals"); 302 OshBuilder *x = nb(); 303 osh_rb_cmd(x, 0); 304 osh_rb_arg(x, "login"); 305 osh_rb_arg(x, "--pin"); 306 osh_rb_arg(x, "1234"); 307 osh_rb_seal(x); 308 static OshRequest rq; 309 CHECK(osh_req_decode(x->rec, osh_rb_cells(x), x->out, x->out_used, &rq) == 0, "decode"); 310 free(x); 311 uint8_t da[32], db[32], da2[32], pl[32]; 312 osh_req_digest(&a, &rq, da); 313 osh_req_digest(&b2, &rq, db); 314 CHECK(memcmp(da, db, 32) != 0, "same request under two journal keys: different digests"); 315 osh_journal_close(&a); 316 CHECK(osh_journal_open(&a2, pa) == 0, "reopen: %s", a2.why); 317 osh_req_digest(&a2, &rq, da2); 318 CHECK(!memcmp(da, da2, 32), "the key is reused on reopen: same digest"); 319 struct stat sb; 320 char kp[720]; 321 snprintf(kp, sizeof kp, "%s.key", pa); 322 CHECK(stat(kp, &sb) == 0 && sb.st_size == 32 && (sb.st_mode & 0777) == 0600, "key file: 32 bytes, mode 0600"); 323 /* an attacker who knows the encoding and the argument shape but not the key */ 324 osh_req_digest_unkeyed(&rq, pl); 325 CHECK(memcmp(da, pl, 32) != 0, "recorded digest is not the plain SHA-256 of the request"); 326 char want[65], plain[65], got[65]; 327 hexs(da, want); 328 hexs(pl, plain); 329 int hit = 0, ctl = 0; 330 for (int pin = 0; pin < 10000; pin++) { 331 char s[16]; 332 snprintf(s, sizeof s, "%04d", pin); 333 OshBuilder *y = nb(); 334 osh_rb_cmd(y, 0); 335 osh_rb_arg(y, "login"); 336 osh_rb_arg(y, "--pin"); 337 osh_rb_arg(y, s); 338 osh_rb_seal(y); 339 static OshRequest rg; 340 osh_req_decode(y->rec, osh_rb_cells(y), y->out, y->out_used, &rg); 341 free(y); 342 uint8_t g[32]; 343 osh_req_digest_unkeyed(&rg, g); 344 hexs(g, got); 345 hit += !strcmp(got, want); 346 ctl += !strcmp(got, plain); 347 } 348 CHECK(hit == 0, "none of the 10000 PIN guesses hashed with plain SHA-256 matches the recorded digest (control: see next check)"); 349 CHECK(ctl == 1, "control: the same loop does find the PIN against the PLAIN digest of the request (%d)", ctl); 350 osh_journal_close(&a2); 351 osh_journal_close(&b2); 352 } 353 354 static int deny_all(void *c, const OshBinding *bd, int op, const char *p) { (void)c; (void)bd; (void)op; (void)p; return OSH_E_DENIED; } 355 356 static void test_denial(void) 357 { 358 T t; 359 t_begin(&t, 1); 360 char marker[4400]; 361 mpath(&t, "m3", marker, sizeof marker); 362 t.s.binding.valid = 0; /* no authority */ 363 OshBuilder *b = nb(); 364 helper(b, "append", marker); 365 OshResult r; 366 int st = run(&t, b, &r); 367 free(b); 368 msleep(150); 369 CHECK(st == 126 && r.err == OSH_E_DENIED && r.cmd[0].pid == 0, "denied (st %d err %d)", st, r.err); 370 CHECK(fsize(marker) < 0, "negative control: no child ran, marker absent"); 371 char *j = jtext(&t); 372 CHECK(count_sub(j, " I ") == 0 && count_sub(j, " O 0 ") == 1 && strstr(j, "FAILED_NO_EFFECT st=126 err=1"), "denial record, no intent:\n%s", j); 373 free(j); 374 375 /* control: the same request with authority runs */ 376 t.s.binding.valid = 1; 377 b = nb(); 378 helper(b, "append", marker); 379 st = run(&t, b, &r); 380 free(b); 381 CHECK(st == 0 && fsize(marker) == 1, "with authority the marker appears"); 382 t_end(&t); 383 384 /* per-effect denial (ABI 9.2) at the moment of the spawn */ 385 T u; 386 t_begin(&u, 1); 387 mpath(&u, "m3b", marker, sizeof marker); 388 u.s.effect_hook = deny_all; 389 b = nb(); 390 helper(b, "append", marker); 391 st = run(&u, b, &r); 392 free(b); 393 msleep(150); 394 char *j2 = jtext(&u); 395 CHECK(st == 126 && fsize(marker) < 0 && count_sub(j2, " I ") == 0 && strstr(j2, "FAILED_NO_EFFECT"), "effect-hook denial:\n%s", j2); 396 free(j2); 397 t_end(&u); 398 } 399 400 static void raise_int_group(void *ctx) { (void)ctx; kill(0, SIGINT); } 401 402 static void child_sigint(T *t, void *arg) 403 { 404 (void)arg; 405 /* A background job of a non-interactive shell (`make ... &`, nohup) inherits SIGINT and SIGQUIT ignored, and a 406 * non-interactive osh keeps an inherited ignore for its children (POSIX, child_signals). The test then never kills 407 * the sleepers and reads COMPLETED st=0 (#388). Start from default dispositions so the test measures osh, not the 408 * caller's environment. */ 409 signal(SIGINT, SIG_DFL); 410 signal(SIGQUIT, SIG_DFL); 411 OshBuilder *b = nb(); 412 helper(b, "sleep", "20000"); 413 helper(b, "sleep", "20000"); 414 t->s.after_launch_hook = raise_int_group; 415 OshResult r; 416 run(t, b, &r); 417 _exit(same_account(t, 2, &r) == 0 && r.cmd[0].outcome == OSH_OUT_COMPLETED && r.cmd[1].outcome == OSH_OUT_COMPLETED ? 0 : 3); 418 } 419 420 static void test_interrupt(void) 421 { 422 T t; 423 t_begin(&t, 1); 424 int st = in_child(child_sigint, &t, NULL, 1); 425 CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0, "SIGINT: record == result for both commands, both COMPLETED (child status %d)", st); 426 char *j = jtext(&t); 427 CHECK(count_sub(j, " I ") == 2 && count_sub(j, " O ") == 2 && count_sub(j, "COMPLETED st=130") == 2, "both ended by the signal: COMPLETED st=130 (ABI 8.4 convention):\n%s", j); 428 CHECK(line_has(j, 2, " O 1 ") && line_has(j, 3, " O 2 "), "outcomes close their intents:\n%s", j); 429 free(j); 430 osh_journal_close(&t.j); 431 OshJournal j2; 432 CHECK(osh_journal_open(&j2, t.jpath) == 0, "reopen"); 433 int bad = 0, n = osh_journal_recover(&j2, t.jpath, NULL, 0, &bad); 434 CHECK(n == 0 && bad == 0, "nothing unresolved after an interrupt (%d)", n); 435 osh_journal_close(&j2); 436 t.s.journal = NULL; 437 t_end(&t); 438 } 439 440 static void test_partial_launch(void) 441 { 442 T t; 443 t_begin(&t, 1); 444 OshBuilder *b = nb(); 445 helper(b, "sleep", "30000"); 446 helper(b, "sleep", "30000"); 447 helper(b, "sleep", "30000"); 448 t.s.fail_fork_at = 2; 449 OshResult r; 450 int st = run(&t, b, &r); 451 free(b); 452 CHECK(st == 1 && r.err == OSH_E_PARTIAL_LAUNCH, "partial launch (st %d err %d)", st, r.err); 453 CHECK(same_account(&t, 3, &r) == 0, "partial launch: record == result"); 454 char *j = jtext(&t); 455 CHECK(count_sub(j, " I ") == 2, "first and second have intents, third never reached:\n%s", j); 456 CHECK(count_sub(j, " O ") == 3, "three outcomes"); 457 CHECK(strstr(j, " 0/3 CANCELLED") && strstr(j, " 1/3 NOT_STARTED") && strstr(j, " 2/3 NOT_STARTED"), "first CANCELLED, rest NOT_STARTED:\n%s", j); 458 CHECK(strstr(j, " O 0 ") != NULL, "the never-reached command has an outcome with no intent"); 459 free(j); 460 t_end(&t); 461 } 462 463 static void steal_child(void *ctx) { (void)ctx; msleep(300); waitpid(-1, NULL, 0); } 464 465 static void test_echild_unknown(void) 466 { 467 T t; 468 t_begin(&t, 1); 469 OshBuilder *b = nb(); 470 helper(b, "quiet", NULL); 471 t.s.after_launch_hook = steal_child; /* somebody else reaps our child: its fate is unknowable */ 472 OshResult r; 473 run(&t, b, &r); 474 free(b); 475 CHECK(r.cmd[0].outcome == OSH_OUT_UNKNOWN && r.err == OSH_E_OUTCOME_UNKNOWN, "result says unknown"); 476 CHECK(same_account(&t, 1, &r) == 0, "ECHILD: record == result"); 477 char *j = jtext(&t); 478 CHECK(strstr(j, " O 1 ") && strstr(j, "OUTCOME_UNKNOWN st=1 err=11"), "durably mirrored:\n%s", j); 479 free(j); 480 t_end(&t); 481 } 482 483 static void child_kill_after_launch_hook(void *ctx) { (void)ctx; kill(getpid(), SIGKILL); } 484 static void child_kill_between(T *t, void *arg) 485 { 486 OshBuilder *b = nb(); 487 helper(b, "append", (const char *)arg); 488 t->s.after_launch_hook = child_kill_after_launch_hook; /* the shell dies after the spawn, before any outcome */ 489 OshResult r; 490 run(t, b, &r); 491 } 492 493 static void test_kill_between_and_recovery(void) 494 { 495 T t; 496 t_begin(&t, 1); 497 char marker[4400]; 498 mpath(&t, "m7", marker, sizeof marker); 499 int st = in_child(child_kill_between, &t, marker, 0); 500 CHECK(WIFSIGNALED(st) && WTERMSIG(st) == SIGKILL, "shell died by SIGKILL between intent and outcome"); 501 CHECK(wait_file(marker, 1, 3000), "the already launched child still ran once"); 502 char *j = jtext(&t); 503 CHECK(count_sub(j, " I ") == 1 && count_sub(j, " O ") == 0, "intent only:\n%s", j); 504 free(j); 505 osh_journal_close(&t.j); 506 507 OshJournal j2; 508 CHECK(osh_journal_open(&j2, t.jpath) == 0, "reopen after the crash"); 509 OshJournalUnknown u[4]; 510 int bad = -1, n = osh_journal_recover(&j2, t.jpath, u, 4, &bad); 511 CHECK(n == 1 && bad == 0 && u[0].idx == 0 && u[0].ncmds == 1 && strlen(u[0].digest) == 64, "one UNKNOWN reported (n %d)", n); 512 CHECK(strstr(u[0].argv0, "test_osh_journal") != NULL, "with argv0 (%s)", u[0].argv0); 513 { 514 OshBuilder *kb = nb(); 515 helper(kb, "append", marker); 516 osh_rb_seal(kb); 517 static OshRequest kr; 518 uint8_t kd[32]; 519 char kh[65]; 520 CHECK(osh_req_decode(kb->rec, osh_rb_cells(kb), kb->out, kb->out_used, &kr) == 0, "decode"); 521 free(kb); 522 osh_req_digest(&j2, &kr, kd); 523 hexs(kd, kh); 524 CHECK(!strcmp(kh, u[0].digest), "recovery reports the KEYED digest of the request (recomputed with the journal key)"); 525 osh_req_digest_unkeyed(&kr, kd); 526 hexs(kd, kh); 527 CHECK(strcmp(kh, u[0].digest) != 0, "and it is not the plain SHA-256"); 528 } 529 msleep(700); 530 CHECK(fsize(marker) == 1, "recovery did not replay: marker still 1 byte (%ld)", fsize(marker)); 531 j = jtext(&t); 532 CHECK(strstr(j, "OUTCOME_UNKNOWN") && strstr(j, "recovered=1"), "recovery closes it durably as UNKNOWN:\n%s", j); 533 free(j); 534 n = osh_journal_recover(&j2, t.jpath, u, 4, &bad); 535 CHECK(n == 0, "second recovery reports nothing new (%d)", n); 536 osh_journal_close(&j2); 537 t.s.journal = NULL; 538 t_end(&t); 539 } 540 541 static void child_die_after_intent(T *t, void *arg) 542 { 543 t->j.die_after_intent = 1; /* crash right after the intent is durable, before fork */ 544 OshBuilder *b = nb(); 545 helper(b, "append", (const char *)arg); 546 OshResult r; 547 run(t, b, &r); 548 } 549 550 static void test_crash_before_launch(void) 551 { 552 T t; 553 t_begin(&t, 1); 554 char marker[4400]; 555 mpath(&t, "m8", marker, sizeof marker); 556 int st = in_child(child_die_after_intent, &t, marker, 0); 557 CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 77, "crashed after the intent (status %d)", st); 558 msleep(300); 559 CHECK(fsize(marker) < 0, "nothing was launched"); 560 osh_journal_close(&t.j); 561 OshJournal j2; 562 CHECK(osh_journal_open(&j2, t.jpath) == 0, "reopen"); 563 OshJournalUnknown u[2]; 564 int bad = 0, n = osh_journal_recover(&j2, t.jpath, u, 2, &bad); 565 CHECK(n == 1, "intent without outcome is UNKNOWN (%d)", n); 566 msleep(300); 567 CHECK(fsize(marker) < 0, "recovery launched nothing"); 568 osh_journal_close(&j2); 569 t.s.journal = NULL; 570 t_end(&t); 571 } 572 573 static void test_fail_closed(void) 574 { 575 T t; 576 t_begin(&t, 1); 577 char marker[4400]; 578 mpath(&t, "m9", marker, sizeof marker); 579 int full = open("/dev/full", O_WRONLY); 580 dup2(full, t.j.fd); /* the journal's descriptor now fails every write with ENOSPC */ 581 close(full); 582 OshBuilder *b = nb(); 583 helper(b, "append", marker); 584 OshResult r; 585 int st = run(&t, b, &r); 586 free(b); 587 msleep(150); 588 CHECK(st == 1 && r.err == OSH_E_IO && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT && fsize(marker) < 0, 589 "no durable intent -> command not launched (st %d err %d)", st, r.err); 590 t_end(&t); 591 } 592 593 static void test_non_effects_and_direct(void) 594 { 595 T t; 596 t_begin(&t, 1); 597 long before = fsize(t.jpath); 598 OshBuilder *b = nb(); 599 osh_rb_cmd(b, OSH_B_PWD); 600 osh_rb_arg(b, "pwd"); /* builtin, no write-class open: not an effect */ 601 OshResult r; 602 run(&t, b, &r); 603 free(b); 604 CHECK(fsize(t.jpath) == before, "a builtin with no write open writes no record"); 605 char outf[4400]; 606 mpath(&t, "o10", outf, sizeof outf); 607 b = nb(); 608 osh_rb_cmd(b, OSH_B_PWD); 609 osh_rb_arg(b, "pwd"); 610 osh_rb_redir(b, OSH_R_OUT, 1, outf, 0); /* write-class open IS an effect, even for a builtin */ 611 run(&t, b, &r); 612 free(b); 613 char *j = jtext(&t); 614 CHECK(count_sub(j, " I ") == 1 && count_sub(j, "COMPLETED") == 1 && line_has(j, 0, "b=2"), "builtin with output redirection recorded:\n%s", j); 615 free(j); 616 617 char marker[4400]; 618 mpath(&t, "m10", marker, sizeof marker); 619 const char *argv[] = {g_self, "--helper", "append", marker}; 620 int st = osh_submit_argv(&t.s, argv, 4, NULL, 0, NULL, 0, &r); /* ABI section 11: same path, same records */ 621 j = jtext(&t); 622 CHECK(st == 0 && count_sub(j, " I ") == 2 && count_sub(j, " O ") == 2, "direct-argv submission is journaled:\n%s", j); 623 free(j); 624 t_end(&t); 625 626 /* control: a session without a journal behaves as before and writes nothing */ 627 T u; 628 t_begin(&u, 0); 629 b = nb(); 630 helper(b, "append", marker); 631 st = run(&u, b, &r); 632 free(b); 633 CHECK(st == 0 && fsize(u.jpath) < 0, "no journal -> no file"); 634 t_end(&u); 635 } 636 637 static void test_torn_tail_and_cleanup(void) 638 { 639 T t; 640 t_begin(&t, 1); 641 OshBuilder *b = nb(); 642 helper(b, "quiet", NULL); 643 OshResult r; 644 run(&t, b, &r); 645 free(b); 646 osh_journal_close(&t.j); 647 t.s.journal = NULL; 648 FILE *f = fopen(t.jpath, "ab"); 649 fputs("OSHJ1 99 I 00ff", f); /* a write cut short by a crash: no newline */ 650 fclose(f); 651 int fds0 = count_fds(); 652 OshJournal j2; 653 CHECK(osh_journal_open(&j2, t.jpath) == 0, "reopen over a torn tail"); 654 CHECK(j2.next_rec == 3, "record counter continues (%llu)", (unsigned long long)j2.next_rec); 655 int bad = 0, n = osh_journal_recover(&j2, t.jpath, NULL, 0, &bad); 656 CHECK(n == 0 && bad == 1, "torn line is ignored, not an intent (n %d bad %d)", n, bad); 657 osh_journal_close(&j2); 658 CHECK(count_fds() == fds0, "open/recover/close leaks no descriptor"); 659 int files = 0; 660 DIR *d = opendir(t.dir); 661 struct dirent *e; 662 while ((e = readdir(d))) if (e->d_name[0] != '.' && strcmp(e->d_name, "journal.log") && strcmp(e->d_name, "journal.log.key")) files++; 663 closedir(d); 664 CHECK(files == 0, "only the journal and its key exist, no temp files (%d stray)", files); 665 t_end(&t); 666 } 667 668 /* the real program. jpath NULL = no --journal (default location from xdg/home); xdg/home NULL = variable unset */ 669 static int run_osh(const char *osh, const char *caps, const char *jpath, const char *script, const char *errf, const char *xdg, 670 const char *home) 671 { 672 pid_t pid = fork(); 673 if (pid == 0) { 674 int fd = open(errf, O_WRONLY | O_CREAT | O_TRUNC, 0600); 675 dup2(fd, 2); 676 dup2(g_null, 1); 677 if (xdg) setenv("XDG_STATE_HOME", xdg, 1); else unsetenv("XDG_STATE_HOME"); 678 if (home) setenv("HOME", home, 1); else unsetenv("HOME"); 679 const char *av[12]; 680 int n = 0; 681 av[n++] = osh; 682 if (caps) { av[n++] = "--caps"; av[n++] = caps; } 683 if (jpath) { av[n++] = "--journal"; av[n++] = jpath; } 684 av[n++] = "-c"; 685 av[n++] = script; 686 av[n] = NULL; 687 execv(osh, (char *const *)av); 688 _exit(127); 689 } 690 int st = 0; 691 waitpid(pid, &st, 0); 692 return WIFEXITED(st) ? WEXITSTATUS(st) : -1; 693 } 694 695 static void test_cli(const char *osh) 696 { 697 char jp[700], ef[700], pol[700], xdg[700], home[700], jd[1400], mk[700], sc[1500]; 698 snprintf(jp, sizeof jp, "%s/cli.log", g_dir); 699 snprintf(ef, sizeof ef, "%s/cli.err", g_dir); 700 snprintf(pol, sizeof pol, "%s/cli.policy", g_dir); 701 snprintf(xdg, sizeof xdg, "%s/xdg", g_dir); 702 snprintf(home, sizeof home, "%s/home", g_dir); 703 snprintf(mk, sizeof mk, "%s/cli.marker", g_dir); 704 FILE *pf = fopen(pol, "w"); 705 fputs("principal 77\nallow spawn /bin\nallow spawn /usr/bin\nallow spawn /home\nallow write /home\n", pf); 706 fclose(pf); 707 /* default policy (no grants): the spawns are denied; denials are recorded, no intents, nothing ran */ 708 CHECK(run_osh(osh, NULL, jp, "/bin/true", ef, xdg, home) == 126, "denied by the default policy"); 709 char *d0 = slurp(jp, NULL); 710 CHECK(count_sub(d0, " I ") == 0 && count_sub(d0, "FAILED_NO_EFFECT st=126 err=1") == 1, "program records the denial:\n%s", d0); 711 free(d0); 712 unlink(jp); 713 CHECK(run_osh(osh, pol, jp, "/bin/true; /bin/false", ef, xdg, home) == 1, "osh --journal runs the script"); 714 char *j = slurp(jp, NULL); 715 CHECK(count_sub(j, " I ") == 2 && count_sub(j, " O ") == 2 && strstr(j, "COMPLETED st=0") && strstr(j, "COMPLETED st=1"), "program writes intents and outcomes:\n%s", j); 716 free(j); 717 FILE *f = fopen(jp, "ab"); /* a previous run died after its intent */ 718 fprintf(f, "OSHJ1 50 I %064d 0/1 b=0 argc=2 wr=0 a0=/bin/touch lens=10,3\n", 7); 719 fclose(f); 720 CHECK(run_osh(osh, pol, jp, "/bin/echo second-run", ef, xdg, home) == 0, "second start"); 721 char *e = slurp(ef, NULL); 722 CHECK(strstr(e, "has outcome UNKNOWN; it is not re-run") && strstr(e, "/bin/touch"), "UNKNOWN reported at start:\n%s", e); 723 free(e); 724 CHECK(run_osh(osh, pol, jp, "/bin/true", ef, xdg, home) == 0, "third start"); 725 e = slurp(ef, NULL); 726 CHECK(!strstr(e, "UNKNOWN"), "reported once only"); 727 free(e); 728 729 /* MANDATORY: no --journal -> the default location is used ($XDG_STATE_HOME/osh/effects.journal), 0700 dirs, 0600 file */ 730 snprintf(jd, sizeof jd, "%s/osh/effects.journal", xdg); 731 CHECK(run_osh(osh, pol, NULL, "/bin/true", ef, xdg, home) == 0, "no --journal still runs"); 732 struct stat sb; 733 char xo[1000]; 734 snprintf(xo, sizeof xo, "%s/osh", xdg); 735 CHECK(stat(jd, &sb) == 0 && (sb.st_mode & 0777) == 0600, "default journal exists with mode 0600"); 736 CHECK(stat(xo, &sb) == 0 && (sb.st_mode & 0777) == 0700, "its directory is 0700"); 737 j = slurp(jd, NULL); 738 CHECK(count_sub(j, " I ") == 1 && count_sub(j, " O ") == 1, "the effect was recorded there:\n%s", j); 739 free(j); 740 /* fallback: no XDG_STATE_HOME -> $HOME/.local/state/osh/effects.journal */ 741 snprintf(jd, sizeof jd, "%s/.local/state/osh/effects.journal", home); 742 mkdir(home, 0700); 743 CHECK(run_osh(osh, pol, NULL, "/bin/true", ef, NULL, home) == 0 && stat(jd, &sb) == 0 && (sb.st_mode & 0777) == 0600, 744 "HOME fallback location used"); 745 char dd[1200]; 746 snprintf(dd, sizeof dd, "%s/.local/state/osh", home); 747 CHECK(stat(dd, &sb) == 0 && (sb.st_mode & 0777) == 0700, "fallback directory is 0700"); 748 749 /* unwritable journal: effects refused by name, nothing launched, non-effect builtins still run */ 750 snprintf(jd, sizeof jd, "%s/not-a-dir", g_dir); 751 FILE *nf = fopen(jd, "w"); /* a regular file where the state directory must be */ 752 fclose(nf); 753 unlink(mk); 754 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 755 CHECK(run_osh(osh, pol, NULL, sc, ef, jd, home) == 1, "effect refused when the journal cannot be opened"); 756 e = slurp(ef, NULL); 757 CHECK(strstr(e, "effect journal unavailable") && strstr(e, "effect refused, nothing was run"), "named error:\n%s", e); 758 free(e); 759 msleep(150); 760 CHECK(fsize(mk) < 0, "no child was launched (marker absent)"); 761 CHECK(run_osh(osh, pol, NULL, "printf x", ef, jd, home) == 0, "a builtin without a write redirection is unaffected"); 762 snprintf(sc, sizeof sc, "printf x > %s", mk); 763 CHECK(run_osh(osh, pol, NULL, sc, ef, jd, home) == 1 && fsize(mk) < 0, "a write-class redirection is an effect: refused, no file"); 764 /* an explicit --journal that cannot be opened is the same refusal (no silent fallback) */ 765 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 766 CHECK(run_osh(osh, pol, "/nonexistent-dir/j.log", sc, ef, xdg, home) == 1 && fsize(mk) < 0, "explicit unopenable --journal: effect refused"); 767 /* the key file sits beside the default journal: 32 bytes, 0600 */ 768 snprintf(jd, sizeof jd, "%s/.local/state/osh/effects.journal.key", home); 769 CHECK(stat(jd, &sb) == 0 && sb.st_size == 32 && (sb.st_mode & 0777) == 0600, "default key file: 32 bytes, 0600"); 770 /* wrong-mode key, wrong-mode journal, wrong-mode state dir: refused by name, nothing launched; fixing them restores service */ 771 char kd[3000], jf[2900], sd[1500]; 772 snprintf(sd, sizeof sd, "%s/.local/state/osh", home); 773 snprintf(jf, sizeof jf, "%s/effects.journal", sd); 774 snprintf(kd, sizeof kd, "%s.key", jf); 775 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 776 unlink(mk); 777 chmod(kd, 0644); 778 CHECK(run_osh(osh, pol, NULL, sc, ef, NULL, home) == 1 && fsize(mk) < 0, "key with mode 0644: effect refused, nothing launched"); 779 e = slurp(ef, NULL); 780 CHECK(strstr(e, "effect journal unavailable") && strstr(e, "key file"), "named: %s", e); 781 free(e); 782 chmod(kd, 0600); 783 chmod(jf, 0644); 784 CHECK(run_osh(osh, pol, NULL, sc, ef, NULL, home) == 1 && fsize(mk) < 0, "journal with mode 0644: effect refused"); 785 chmod(jf, 0600); 786 chmod(sd, 0755); 787 CHECK(run_osh(osh, pol, NULL, sc, ef, NULL, home) == 1 && fsize(mk) < 0, "state directory with mode 0755: effect refused"); 788 e = slurp(ef, NULL); 789 CHECK(strstr(e, "state directory") && strstr(e, "0700"), "named: %s", e); 790 free(e); 791 chmod(sd, 0700); 792 unlink(kd); 793 CHECK(run_osh(osh, pol, NULL, sc, ef, NULL, home) == 1 && fsize(mk) < 0, "key deleted beside a journal with records: effect refused"); 794 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 795 /* control: the same command with a working journal does run */ 796 CHECK(run_osh(osh, pol, jp, sc, ef, xdg, home) == 0 && fsize(mk) == 0, "control: with a working journal the marker is created"); 797 } 798 799 static void test_key_and_modes(void) 800 { 801 char p[1400], k[1500], dir[700]; 802 snprintf(dir, sizeof dir, "%s/km", g_dir); 803 mkdir(dir, 0700); 804 snprintf(p, sizeof p, "%s/j.log", dir); 805 snprintf(k, sizeof k, "%s.key", p); 806 OshJournal j; 807 CHECK(osh_journal_open(&j, p) == 0, "fresh journal and key: %s", j.why); 808 osh_journal_close(&j); 809 chmod(k, 0644); 810 CHECK(osh_journal_open(&j, p) != 0 && strstr(j.why, "mode 0600"), "key with mode 0644 refused: %s", j.why); 811 chmod(k, 0600); 812 CHECK(truncate(k, 16) == 0 && osh_journal_open(&j, p) != 0, "key of the wrong size refused"); 813 unlink(k); 814 mkdir(k, 0700); 815 CHECK(osh_journal_open(&j, p) != 0, "unreadable key (a directory) refused"); 816 rmdir(k); 817 /* a missing key beside a journal with records: refused, no silent new key */ 818 FILE *f = fopen(p, "ab"); 819 fputs("OSHJ1 1 O 0 00 0/1 NOT_STARTED st=0 err=0\n", f); 820 fclose(f); 821 CHECK(osh_journal_open(&j, p) != 0 && strstr(j.why, "missing") && access(k, F_OK) != 0, "missing key beside a journal with records refused, no key invented: %s", j.why); 822 /* a pre-existing journal with a wrong mode is REFUSED (not silently fixed), and nothing is changed */ 823 unlink(p); 824 f = fopen(p, "w"); 825 fclose(f); 826 chmod(p, 0644); 827 CHECK(osh_journal_open(&j, p) != 0 && strstr(j.why, "mode 0600"), "journal with mode 0644 refused: %s", j.why); 828 struct stat sb; 829 CHECK(stat(p, &sb) == 0 && (sb.st_mode & 0777) == 0644, "and its mode was left alone"); 830 CHECK(osh_journal_check_dir(&j, dir) == 0, "private directory accepted"); 831 chmod(dir, 0755); 832 CHECK(osh_journal_check_dir(&j, dir) != 0 && strstr(j.why, "0700"), "directory with mode 0755 refused: %s", j.why); 833 chmod(dir, 0700); 834 } 835 836 /* library level: journal_required with no journal open refuses every effect and launches nothing */ 837 static void test_required_without_journal(void) 838 { 839 T t; 840 t_begin(&t, 0); 841 t.s.journal_required = 1; 842 t.s.journal_error = "test: unwritable"; 843 char marker[4400]; 844 mpath(&t, "mreq", marker, sizeof marker); 845 OshBuilder *b = nb(); 846 helper(b, "append", marker); 847 OshResult r; 848 int st = run(&t, b, &r); 849 free(b); 850 msleep(150); 851 CHECK(st == 1 && r.err == OSH_E_IO && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT && r.cmd[0].pid == 0 && fsize(marker) < 0, 852 "required journal missing -> effect refused (st %d)", st); 853 b = nb(); 854 osh_rb_cmd(b, OSH_B_PWD); 855 osh_rb_arg(b, "pwd"); 856 st = run(&t, b, &r); 857 free(b); 858 CHECK(st == 0 && r.cmd[0].outcome == OSH_OUT_COMPLETED, "non-effect builtin unaffected"); 859 t_end(&t); 860 } 861 862 /* ---------------- review round: partial launch on a failed later intent, sticky failure, fsync, modes, hooks ---------------- */ 863 864 void osh_hmac_for_test(const uint8_t key[32], const uint8_t *msg, size_t n, uint8_t out[32]); 865 866 static void test_hmac_vectors(void) 867 { 868 /* RFC 4231 test cases 1 and 2 (keys shorter than 32 bytes are zero-extended, which HMAC treats as the same key) */ 869 uint8_t key[32] = {0}, out[32]; 870 char h[65]; 871 memset(key, 0x0b, 20); 872 osh_hmac_for_test(key, (const uint8_t *)"Hi There", 8, out); 873 hexs(out, h); 874 CHECK(!strcmp(h, "`b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7`"), "RFC 4231 case 1: %s", h); 875 memset(key, 0, sizeof key); 876 memcpy(key, "Jefe", 4); 877 osh_hmac_for_test(key, (const uint8_t *)"what do ya want for nothing?", 28, out); 878 hexs(out, h); 879 CHECK(!strcmp(h, "`5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843`"), "RFC 4231 case 2: %s", h); 880 } 881 882 /* The test program supplies its own fsync(2); calls on the journal's descriptor are counted, then really performed. If the 883 * journal stopped calling fsync, the count below would fall short and this test fails. */ 884 static int g_fsync_fd = -1, g_fsync_n; 885 int fsync(int fd) 886 { 887 if (fd == g_fsync_fd) g_fsync_n++; 888 return (int)syscall(SYS_fsync, fd); 889 } 890 891 static void test_fsync_counted(void) 892 { 893 T t; 894 t_begin(&t, 1); 895 g_fsync_fd = t.j.fd; 896 g_fsync_n = 0; 897 OshBuilder *b = nb(); 898 helper(b, "quiet", NULL); 899 OshResult r; 900 run(&t, b, &r); 901 free(b); 902 CHECK(t.j.n_commit == 2 && g_fsync_n == 2, "one intent + one outcome = 2 commits, each followed by a real fsync on the journal (commits %d, fsyncs %d)", t.j.n_commit, g_fsync_n); 903 g_fsync_fd = -1; 904 t_end(&t); 905 } 906 907 static int zombies(void) 908 { 909 int n = 0; 910 while (waitpid(-1, NULL, WNOHANG) > 0) n++; 911 return n; 912 } 913 914 static void test_pipeline_later_intent_fails(void) 915 { 916 T t; 917 t_begin(&t, 1); 918 char m1[4400], m2[4400], m3[4400]; 919 mpath(&t, "pl1", m1, sizeof m1); 920 mpath(&t, "pl2", m2, sizeof m2); 921 mpath(&t, "pl3", m3, sizeof m3); 922 OshBuilder *b = nb(); 923 helper(b, "sleep", "30000"); /* a: starts */ 924 helper(b, "append", m2); /* b: its intent write fails */ 925 helper(b, "append", m3); /* c: never reached */ 926 t.j.fail_write_at = 2; /* commit 1 = a's intent, commit 2 = b's intent */ 927 OshResult r; 928 int st = run(&t, b, &r); 929 free(b); 930 msleep(200); 931 CHECK(st == 1 && r.err == OSH_E_PARTIAL_LAUNCH, "journal failure on member 2 of 3: PARTIAL_LAUNCH (st %d err %d)", st, r.err); 932 CHECK(r.cmd[0].outcome == OSH_OUT_CANCELLED && r.cmd[0].pid > 0, "member 1 was started and is CANCELLED (%d)", r.cmd[0].outcome); 933 CHECK(kill(r.cmd[0].pid, 0) != 0 && errno == ESRCH, "member 1 is gone, not running"); 934 CHECK(zombies() == 0, "and reaped: no zombie left"); 935 CHECK(r.cmd[1].outcome == OSH_OUT_FAILED_NO_EFFECT && r.cmd[1].pid == 0 && r.cmd[2].outcome == OSH_OUT_NOT_STARTED && r.cmd[2].pid == 0, 936 "members 2 and 3 never launched (%d, %d)", r.cmd[1].outcome, r.cmd[2].outcome); 937 CHECK(fsize(m2) < 0 && fsize(m3) < 0, "their marker files do not exist"); 938 CHECK(same_account(&t, 3, &r) == 0, "records match the result"); 939 char *j = jtext(&t); 940 CHECK(count_sub(j, " I ") == 1 && strstr(j, " 0/3 CANCELLED") && strstr(j, " 1/3 FAILED_NO_EFFECT"), "journal: one intent, member 1 cancelled:\n%s", j); 941 free(j); 942 t_end(&t); 943 } 944 945 static int rec_numbers_unique(const char *text) 946 { 947 unsigned long long seen[512]; 948 int n = 0, bad = 0; 949 for (const char *p = text; p && *p;) { 950 unsigned long long v; 951 if (!strncmp(p, "OSHJ1 ", 6) && sscanf(p + 6, "%llu", &v) == 1) { 952 for (int i = 0; i < n; i++) bad += seen[i] == v; 953 if (n < 512) seen[n++] = v; 954 } 955 p = strchr(p, '\n'); 956 if (p) p++; 957 } 958 return bad; 959 } 960 961 static void sticky_case(int write_fail) 962 { 963 T t; 964 t_begin(&t, 1); 965 char m1[4400], m2[4400]; 966 mpath(&t, write_fail ? "sk1" : "sk3", m1, sizeof m1); 967 mpath(&t, write_fail ? "sk2" : "sk4", m2, sizeof m2); 968 if (write_fail) t.j.fail_write_at = 1; else t.j.fail_fsync_at = 1; 969 OshBuilder *b = nb(); 970 helper(b, "append", m1); 971 OshResult r; 972 int st = run(&t, b, &r); 973 free(b); 974 msleep(150); 975 CHECK(st == 1 && fsize(m1) < 0, "%s: first command refused (st %d), nothing launched", write_fail ? "short write" : "fsync failure", st); 976 CHECK(t.j.poisoned, "journal is poisoned"); 977 long size1 = fsize(t.jpath); 978 char *before = slurp(t.jpath, NULL); 979 int intents_before = count_sub(before, " I "); 980 free(before); 981 int commits = t.j.n_commit; 982 /* the injected fault is one-shot; a second command must STILL be refused, by name, and write nothing */ 983 int err = dup(2), ef = open("/dev/null", O_WRONLY); 984 char errf[4400]; 985 mpath(&t, "sk.err", errf, sizeof errf); 986 int efd = open(errf, O_WRONLY | O_CREAT | O_TRUNC, 0600); 987 t.s.fd[2] = efd; 988 b = nb(); 989 helper(b, "append", m2); 990 st = run(&t, b, &r); 991 free(b); 992 close(efd); close(ef); close(err); 993 msleep(150); 994 char *e = slurp(errf, NULL); 995 CHECK(st == 1 && r.err == OSH_E_IO && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT && fsize(m2) < 0, "second command refused, nothing launched (st %d)", st); 996 CHECK(e && strstr(e, "effect journal failed") && strstr(e, "nothing was run"), "refusal is named:\n%s", e ? e : "(none)"); 997 free(e); 998 char *after = slurp(t.jpath, NULL); 999 CHECK(count_sub(after, " I ") == intents_before, "no new intent was written after the poison"); 1000 CHECK(write_fail || fsize(t.jpath) == size1, "after a failed fsync nothing at all is written (%ld vs %ld)", fsize(t.jpath), size1); 1001 CHECK(t.j.n_commit <= commits + 1, "at most an intent-less outcome line was attempted (%d vs %d)", t.j.n_commit, commits); 1002 free(after); 1003 /* recovery reads the file sanely: reopen, then run a command; record numbers never repeat and every line parses */ 1004 osh_journal_close(&t.j); 1005 OshJournal j2; 1006 CHECK(osh_journal_open(&j2, t.jpath) == 0, "reopen after the failure: %s", j2.why); 1007 OshJournalUnknown u[8]; 1008 int bad = 0; 1009 int nu = osh_journal_recover(&j2, t.jpath, u, 8, &bad); 1010 CHECK(nu >= 0 && bad == 0, "recovery reads the file sanely (unknown %d, unreadable lines %d)", nu, bad); 1011 t.s.journal = &j2; 1012 t.s.fd[2] = g_null; 1013 b = nb(); 1014 helper(b, "quiet", NULL); 1015 st = run(&t, b, &r); 1016 free(b); 1017 char *txt = slurp(t.jpath, NULL); 1018 CHECK(st == 0 && rec_numbers_unique(txt) == 0, "after reopening, a new command runs and no record number repeats:\n%s", txt); 1019 free(txt); 1020 osh_journal_close(&j2); 1021 t.s.journal = NULL; 1022 osh_session_free(&t.s); 1023 } 1024 1025 static void test_sticky_failure(void) 1026 { 1027 sticky_case(1); 1028 sticky_case(0); 1029 } 1030 1031 static void test_explicit_journal_modes(void) 1032 { 1033 char dir[700], p[1400], k[1500]; 1034 snprintf(dir, sizeof dir, "%s/em", g_dir); 1035 mkdir(dir, 0700); 1036 snprintf(p, sizeof p, "%s/j.log", dir); 1037 snprintf(k, sizeof k, "%s.key", p); 1038 OshJournal j; 1039 CHECK(osh_journal_open(&j, p) == 0, "fresh"); 1040 osh_journal_close(&j); 1041 chmod(p, 0400); 1042 CHECK(osh_journal_open(&j, p) != 0 && strstr(j.why, "0600"), "journal mode 0400 refused: %s", j.why); 1043 chmod(p, 0700); 1044 CHECK(osh_journal_open(&j, p) != 0 && strstr(j.why, "0600"), "journal mode 0700 refused: %s", j.why); 1045 chmod(p, 0600); 1046 CHECK(osh_journal_open(&j, p) == 0, "exactly 0600 accepted"); 1047 osh_journal_close(&j); 1048 /* mkparents creates every missing level at 0700 */ 1049 char deep[1800]; 1050 snprintf(deep, sizeof deep, "%s/mp/a/b/effects.journal", g_dir); 1051 CHECK(osh_journal_mkparents(deep) == 0, "mkparents"); 1052 struct stat sb; 1053 char d1[1800]; 1054 snprintf(d1, sizeof d1, "%s/mp/a/b", g_dir); 1055 CHECK(stat(d1, &sb) == 0 && (sb.st_mode & 0777) == 0700, "created directory is 0700"); 1056 CHECK(osh_journal_mkparents(deep) == 0, "and is idempotent"); 1057 } 1058 1059 static void test_cli_explicit_dir(const char *osh) 1060 { 1061 char pol[700], ef[700], sd[700], jp[900], mk[700], sc[1500]; 1062 snprintf(pol, sizeof pol, "%s/cli.policy", g_dir); 1063 snprintf(ef, sizeof ef, "%s/cli2.err", g_dir); 1064 snprintf(sd, sizeof sd, "%s/ed", g_dir); 1065 snprintf(jp, sizeof jp, "%s/j.log", sd); 1066 snprintf(mk, sizeof mk, "%s/cli2.marker", g_dir); 1067 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 1068 mkdir(sd, 0755); 1069 CHECK(run_osh(osh, pol, jp, sc, ef, NULL, "/tmp") == 1 && fsize(mk) < 0, "explicit --journal in a 0755 directory: effect refused, nothing launched"); 1070 char *e = slurp(ef, NULL); 1071 CHECK(strstr(e, "state directory") && strstr(e, "0700"), "named: %s", e); 1072 free(e); 1073 chmod(sd, 0700); 1074 CHECK(run_osh(osh, pol, jp, sc, ef, NULL, "/tmp") == 0 && fsize(mk) == 0, "control: with a private directory it runs"); 1075 } 1076 1077 /* ONE SESSION PER JOURNAL (flock). Start osh in its own process group without waiting. */ 1078 static pid_t start_osh(const char *osh, const char *caps, const char *jpath, const char *script, const char *errf) 1079 { 1080 pid_t pid = fork(); 1081 if (pid == 0) { 1082 setpgid(0, 0); 1083 int fd = open(errf, O_WRONLY | O_CREAT | O_TRUNC, 0600); 1084 dup2(fd, 2); 1085 dup2(g_null, 1); 1086 const char *av[8] = {osh, "--caps", caps, "--journal", jpath, "-c", script, NULL}; 1087 execv(osh, (char *const *)av); 1088 _exit(127); 1089 } 1090 setpgid(pid, pid); 1091 return pid; 1092 } 1093 1094 static void test_cli_one_session(const char *osh) 1095 { 1096 char pol[700], jp[900], ef1[700], ef2[700], ef3[700], mk[700], sc[1500], key[1000]; 1097 snprintf(pol, sizeof pol, "%s/cli.policy", g_dir); 1098 snprintf(jp, sizeof jp, "%s/lock.log", g_dir); 1099 snprintf(key, sizeof key, "%s.key", jp); 1100 snprintf(ef1, sizeof ef1, "%s/lk1.err", g_dir); 1101 snprintf(ef2, sizeof ef2, "%s/lk2.err", g_dir); 1102 snprintf(ef3, sizeof ef3, "%s/lk3.err", g_dir); 1103 snprintf(mk, sizeof mk, "%s/lk.marker", g_dir); 1104 pid_t a = start_osh(osh, pol, jp, "/bin/sleep 2; /bin/true", ef1); 1105 for (int i = 0; i < 200 && !(fsize(jp) > 0); i++) msleep(25); 1106 CHECK(fsize(jp) > 0, "first session holds the journal and has written its intent"); 1107 /* second session on the SAME journal: effects refused by name, nothing launched, builtins still work */ 1108 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 1109 CHECK(run_osh(osh, pol, jp, sc, ef2, NULL, "/tmp") == 1 && fsize(mk) < 0, "second session: effect refused, nothing launched"); 1110 char *e = slurp(ef2, NULL); 1111 CHECK(strstr(e, "in use by another osh session") && strstr(e, "--journal") && strstr(e, "use a separate journal);") && !strstr(e, "journal0"), "named, says to use --journal, exact ending (no stray digit):\n%s", e); 1112 free(e); 1113 CHECK(run_osh(osh, pol, jp, "printf x", ef2, NULL, "/tmp") == 0, "second session: a builtin without a write still runs"); 1114 int st = 0; 1115 waitpid(a, &st, 0); 1116 CHECK(WIFEXITED(st) && WEXITSTATUS(st) == 0, "first session unaffected (exit %d)", WIFEXITED(st) ? WEXITSTATUS(st) : -1); 1117 char *j = slurp(jp, NULL); 1118 CHECK(count_sub(j, " I ") == 2 && count_sub(j, " O ") == 2 && rec_numbers_unique(j) == 0, "no duplicate record numbers, nothing from the refused session:\n%s", j); 1119 free(j); 1120 /* the lock dies with the process: SIGKILL a session mid-command, a new session takes the lock and recovery reports the open intent once */ 1121 unlink(jp); 1122 unlink(key); 1123 pid_t b = start_osh(osh, pol, jp, "/bin/sleep 30", ef1); 1124 for (int i = 0; i < 200 && !(fsize(jp) > 0); i++) msleep(25); 1125 CHECK(fsize(jp) > 0, "second run: intent written"); 1126 kill(-b, SIGKILL); 1127 waitpid(b, NULL, 0); 1128 msleep(100); 1129 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 1130 CHECK(run_osh(osh, pol, jp, sc, ef3, NULL, "/tmp") == 0 && fsize(mk) == 0, "after the holder was killed, a new session gets the lock and runs"); 1131 e = slurp(ef3, NULL); 1132 CHECK(count_sub(e, "has outcome UNKNOWN") == 1, "recovery marks the killed session's open intent UNKNOWN exactly once:\n%s", e); 1133 free(e); 1134 CHECK(run_osh(osh, pol, jp, "/bin/true", ef3, NULL, "/tmp") == 0, "next start"); 1135 e = slurp(ef3, NULL); 1136 CHECK(count_sub(e, "UNKNOWN") == 0, "and not again"); 1137 free(e); 1138 j = slurp(jp, NULL); 1139 CHECK(rec_numbers_unique(j) == 0, "record numbers unique across sessions:\n%s", j); 1140 free(j); 1141 } 1142 1143 /* a forked builtin never execs: it must not keep the journal descriptor (and so the flock) after the shell is SIGKILLed */ 1144 static void test_cli_orphan_builtin(const char *osh) 1145 { 1146 char pol[700], jp[900], ef[700], mk[700], fifo[700], sc[1500]; 1147 snprintf(pol, sizeof pol, "%s/orphan.policy", g_dir); 1148 snprintf(jp, sizeof jp, "%s/orphan.log", g_dir); 1149 snprintf(ef, sizeof ef, "%s/orphan.err", g_dir); 1150 snprintf(mk, sizeof mk, "%s/orphan.marker", g_dir); 1151 snprintf(fifo, sizeof fifo, "%s/orphan.fifo", g_dir); 1152 FILE *pf = fopen(pol, "w"); 1153 fprintf(pf, "principal 77\nallow spawn /bin\nallow spawn /usr/bin\nallow write %s\n", g_dir); 1154 fclose(pf); 1155 CHECK(mkfifo(fifo, 0600) == 0, "fifo"); 1156 snprintf(sc, sizeof sc, "pwd > %s | /bin/true", fifo); 1157 pid_t a = start_osh(osh, pol, jp, sc, ef); 1158 for (int i = 0; i < 200 && !(fsize(jp) > 0); i++) msleep(25); 1159 msleep(400); /* the forked builtin is now blocked opening the fifo */ 1160 kill(a, SIGKILL); /* only the shell: the blocked builtin child is orphaned and still alive */ 1161 waitpid(a, NULL, 0); 1162 msleep(100); 1163 snprintf(sc, sizeof sc, "/bin/touch %s", mk); 1164 CHECK(run_osh(osh, pol, jp, sc, ef, NULL, "/tmp") == 0 && fsize(mk) == 0, "new session gets the lock at once while the orphan still holds the fifo"); 1165 char *e = slurp(ef, NULL); 1166 CHECK(!strstr(e, "in use by another"), "no 'in use' refusal:\n%s", e); 1167 free(e); 1168 int rd = open(fifo, O_RDONLY | O_NONBLOCK); /* let the orphan go */ 1169 char buf[4400]; 1170 for (int i = 0; i < 40; i++) { if (read(rd, buf, sizeof buf) > 0) break; msleep(25); } 1171 close(rd); 1172 msleep(100); 1173 unlink(fifo); 1174 } 1175 1176 int main(int argc, char **argv) 1177 { 1178 if (argc >= 2 && !strcmp(argv[1], "--helper")) return helper_main(argc, argv); 1179 const char *osh_bin = argc >= 2 ? argv[1] : NULL; 1180 ssize_t n = readlink("/proc/self/exe", g_self, sizeof g_self - 1); 1181 if (n <= 0) return 1; 1182 g_self[n] = 0; 1183 const char *tmp = getenv("TMPDIR"); 1184 snprintf(g_dir, sizeof g_dir, "%s/osh-journal-XXXXXX", tmp && *tmp ? tmp : "/tmp"); 1185 if (!mkdtemp(g_dir)) return 1; 1186 g_null = open("/dev/null", O_RDWR); 1187 test_secret_names(); 1188 test_keyed_digest(); 1189 test_normal_and_redaction(); 1190 test_denial(); 1191 test_interrupt(); 1192 test_partial_launch(); 1193 test_echild_unknown(); 1194 test_kill_between_and_recovery(); 1195 test_crash_before_launch(); 1196 test_fail_closed(); 1197 test_non_effects_and_direct(); 1198 test_torn_tail_and_cleanup(); 1199 test_required_without_journal(); 1200 test_key_and_modes(); 1201 test_hmac_vectors(); 1202 test_fsync_counted(); 1203 test_pipeline_later_intent_fails(); 1204 test_sticky_failure(); 1205 test_explicit_journal_modes(); 1206 if (osh_bin) { test_cli(osh_bin); test_cli_explicit_dir(osh_bin); test_cli_one_session(osh_bin); test_cli_orphan_builtin(osh_bin); } 1207 close(g_null); 1208 char cmd[4400]; 1209 snprintf(cmd, sizeof cmd, "rm -rf '%s'", g_dir); 1210 if (system(cmd)) {} 1211 printf("%d checks, %d failed\n", g_checks, g_fail); 1212 printf(g_fail ? "OSH_JOURNAL_FAIL\n" : "OSH_JOURNAL_PASS\n"); 1213 return g_fail ? 1 : 0; 1214 }