interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / osh / host / test_osh_caps.c

434 lines · 21116 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_caps.c -- capability enforcement of the osh Linux host adapter (aien-architecture#158, ABI section 9).
19 * Authority under test: omega's rx_caproot (separate root process) through src/osh/host/osh_caps.c. Everything happens
20 * in a disposable directory under $TMPDIR. The test binary is also its own helper program (`--helper echo|touch|cat`).
21 * Usage: test_osh_caps [path-to-osh-binary] (with the path, the same properties are checked through `osh --caps`)
22 */
23#ifndef _GNU_SOURCE
24#define _GNU_SOURCE
25#endif
26#include "osh_caps.h"
27
28#include <dirent.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <limits.h>
32#include <signal.h>
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39#include <unistd.h>
40
41static int g_pass, g_fail;
42#define CHECK(c, msg) do { if (c) g_pass++; else { g_fail++; printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, msg); } } while (0)
43
44static int helper_main(int argc, char **argv)
45{
46 const char *op = argc > 2 ? argv[2] : "";
47 if (!strcmp(op, "echo")) { for (int i = 3; i < argc; i++) printf("%s%s", i > 3 ? " " : "", argv[i]); printf("\n"); return 0; }
48 if (!strcmp(op, "touch")) { int fd = open(argv[3], O_WRONLY | O_CREAT, 0666); if (fd < 0) return 1; close(fd); return 0; }
49 if (!strcmp(op, "cat")) { char b[4096]; ssize_t n; while ((n = read(0, b, sizeof b)) > 0) if (write(1, b, (size_t)n) != n) return 1; return 0; }
50 return 2;
51}
52
53static char T[PATH_MAX], SELF[PATH_MAX], NOHELP[PATH_MAX], DIAG[PATH_MAX];
54
55static void P(char *out, const char *rel) { snprintf(out, PATH_MAX, "%s/%s", T, rel); }
56static int exists(const char *p) { struct stat st; return lstat(p, &st) == 0; }
57
58static char *slurp(const char *p)
59{
60 static char b[8192];
61 b[0] = 0;
62 int fd = open(p, O_RDONLY);
63 if (fd < 0) return b;
64 ssize_t n = read(fd, b, sizeof b - 1);
65 close(fd);
66 b[n > 0 ? n : 0] = 0;
67 return b;
68}
69
70static void put(const char *p, const char *s)
71{
72 int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC, 0666);
73 if (fd >= 0) { if (write(fd, s, strlen(s)) < 0) {} close(fd); }
74}
75
76static void copy_self(const char *dst)
77{
78 int in = open(SELF, O_RDONLY), out = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0755);
79 char b[65536];
80 ssize_t n;
81 while (in >= 0 && out >= 0 && (n = read(in, b, sizeof b)) > 0) if (write(out, b, (size_t)n) < 0) break;
82 if (in >= 0) close(in);
83 if (out >= 0) close(out);
84}
85
86/* number of direct children of this process (the capability root is one) */
87static int nchildren(void)
88{
89 char p[64], b[1024];
90 snprintf(p, sizeof p, "/proc/self/task/%d/children", (int)getpid());
91 int fd = open(p, O_RDONLY);
92 if (fd < 0) return -1;
93 ssize_t n = read(fd, b, sizeof b - 1);
94 close(fd);
95 if (n <= 0) return 0;
96 b[n] = 0;
97 int c = 0;
98 for (char *s = strtok(b, " \n"); s; s = strtok(NULL, " \n")) c++;
99 return c;
100}
101
102static int run(OshSession *s, const char *const *argv, int n, const OshRedir *rd, int nrd, OshResult *r)
103{
104 return osh_submit_argv(s, argv, n, NULL, 0, rd, nrd, r);
105}
106
107static int run_cd(OshSession *s, const char *dir, OshResult *r)
108{
109 OshBuilder *b = malloc(sizeof *b);
110 osh_rb_init(b, 0, OSH_CONN_NONE);
111 osh_rb_cmd(b, OSH_B_CD);
112 osh_rb_arg(b, "cd");
113 osh_rb_arg(b, dir);
114 osh_rb_seal(b);
115 int st = osh_exec_record(s, b->rec, osh_rb_cells(b), b->out, b->out_used, r);
116 free(b);
117 return st;
118}
119
120static char *diag_text(void) { return slurp(DIAG); }
121static void diag_reset(void) { put(DIAG, ""); }
122
123static int rm_rf(const char *path)
124{
125 struct stat st;
126 if (lstat(path, &st) != 0) return 0;
127 if (S_ISDIR(st.st_mode)) {
128 DIR *d = opendir(path);
129 struct dirent *e;
130 while (d && (e = readdir(d))) {
131 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
132 char c[PATH_MAX];
133 snprintf(c, sizeof c, "%s/%s", path, e->d_name);
134 rm_rf(c);
135 }
136 if (d) closedir(d);
137 return rmdir(path);
138 }
139 return unlink(path);
140}
141
142static void session(OshSession *s, int diagfd)
143{
144 char *env[] = {"PATH=/usr/bin:/bin", NULL};
145 osh_session_init(s, env);
146 s->fd[2] = diagfd;
147}
148
149static int start_caps(OshCaps *c, const char *policy)
150{
151 char err[256];
152 if (osh_caps_start(c) != 0) return -1;
153 if (osh_caps_load(c, policy, err, sizeof err) != 0) { printf("policy error: %s\n", err); return -1; }
154 return 0;
155}
156
157static int spawn_status(pid_t pid)
158{
159 int st = 0;
160 waitpid(pid, &st, 0);
161 return WIFEXITED(st) ? WEXITSTATUS(st) : 128 + WTERMSIG(st);
162}
163
164/* run the osh program with --caps; returns its exit status; stdout/stderr go to /dev/null */
165static int run_osh(const char *osh, const char *policy, const char *cmd, const char *extra_env)
166{
167 pid_t pid = fork();
168 if (pid == 0) {
169 int dn = open("/dev/null", O_RDWR);
170 dup2(dn, 1);
171 dup2(dn, 2);
172 if (extra_env) putenv((char *)extra_env);
173 if (policy) execl(osh, osh, "--caps", policy, "-c", cmd, (char *)NULL);
174 else execl(osh, osh, "-c", cmd, (char *)NULL);
175 _exit(99);
176 }
177 return spawn_status(pid);
178}
179
180int main(int argc, char **argv)
181{
182 if (argc > 2 && !strcmp(argv[1], "--helper")) return helper_main(argc, argv);
183 const char *osh_bin = argc > 1 ? argv[1] : NULL;
184 ssize_t sl = readlink("/proc/self/exe", SELF, sizeof SELF - 1);
185 if (sl <= 0) return 70;
186 SELF[sl] = 0;
187 const char *tmp = getenv("TMPDIR");
188 char tpl[PATH_MAX];
189 snprintf(tpl, sizeof tpl, "%s/osh-caps-XXXXXX", tmp && *tmp ? tmp : "/tmp");
190 if (!mkdtemp(tpl)) return 70;
191 if (!realpath(tpl, T)) return 70;
192 char ok[PATH_MAX], no[PATH_MAX], tmpf[PATH_MAX];
193 P(ok, "ok"); P(no, "no"); P(DIAG, "diag"); P(NOHELP, "no/helper");
194 mkdir(ok, 0777); mkdir(no, 0777);
195 copy_self(NOHELP);
196 char old[PATH_MAX], keep[PATH_MAX];
197 P(old, "no/old"); put(old, "KEEP");
198 P(keep, "ok/in"); put(keep, "READABLE\n");
199 char selfdir[PATH_MAX];
200 strcpy(selfdir, SELF);
201 *strrchr(selfdir, '/') = 0;
202
203 char policy[4 * PATH_MAX];
204 snprintf(policy, sizeof policy, "# test policy\nprincipal 77\nallow spawn %s\nallow read %s\nallow write %s\nallow chdir %s\n",
205 selfdir, ok, ok, ok);
206
207 int dfd = open(DIAG, O_WRONLY | O_CREAT | O_APPEND, 0666);
208 OshCaps caps;
209 OshSession s;
210 OshResult r;
211 CHECK(start_caps(&caps, policy) == 0, "root starts and policy mints");
212 session(&s, dfd);
213 osh_caps_attach(&caps, &s);
214 CHECK(s.binding.cap_generation > 0xFFFFFFFFull, "real generation exceeds 2^32");
215 CHECK(s.binding.cap_generation == caps.principal_ref.generation && s.binding.domain == 2, "binding carries the full 64-bit generation, domain 2");
216 int base_children = nchildren();
217 CHECK(base_children == 1, "only the capability root is a child before the tests");
218
219 /* ---- allowed ---- */
220 char o1[PATH_MAX], a1[PATH_MAX];
221 P(o1, "ok/out"); P(a1, "ok/app");
222 const char *echo[] = {SELF, "--helper", "echo", "hi"};
223 OshRedir w1[] = {{OSH_R_OUT, 1, 0, o1}};
224 CHECK(run(&s, echo, 4, w1, 1, &r) == 0 && r.err == OSH_E_OK, "allowed spawn + allowed write succeeds");
225 CHECK(!strcmp(slurp(o1), "hi\n"), "allowed write produced the bytes");
226 OshRedir ap[] = {{OSH_R_APPEND, 1, 0, o1}};
227 CHECK(run(&s, echo, 4, ap, 1, &r) == 0 && !strcmp(slurp(o1), "hi\nhi\n"), "allowed append works");
228 const char *cat[] = {SELF, "--helper", "cat"};
229 OshRedir rin[] = {{OSH_R_IN, 0, 0, keep}, {OSH_R_OUT, 1, 0, a1}};
230 CHECK(run(&s, cat, 3, rin, 2, &r) == 0 && !strcmp(slurp(a1), "READABLE\n"), "allowed read redirection works");
231 char cwd0[PATH_MAX], cwd1[PATH_MAX];
232 if (!getcwd(cwd0, sizeof cwd0)) cwd0[0] = 0;
233 CHECK(run_cd(&s, ok, &r) == 0 && getcwd(cwd1, sizeof cwd1) && !strcmp(cwd1, ok), "allowed cd works");
234 if (chdir(cwd0) != 0) {}
235
236 /* ---- denied: no effect ---- */
237 diag_reset();
238 char mk[PATH_MAX];
239 P(mk, "no/marker");
240 const char *tch[] = {NOHELP, "--helper", "touch", mk};
241 CHECK(run(&s, tch, 4, NULL, 0, &r) == 126 && r.err == OSH_E_DENIED && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT, "denied spawn: 126, DENIED, FAILED_NO_EFFECT");
242 CHECK(r.cmd[0].pid == 0 && !exists(mk) && nchildren() == base_children, "denied spawn: no pid, marker absent, process table unchanged");
243 CHECK(strstr(diag_text(), "spawn") && strstr(diag_text(), "denied (DENIED)"), "denied spawn names the error");
244
245 char nw[PATH_MAX];
246 P(nw, "no/new");
247 OshRedir wn[] = {{OSH_R_OUT, 1, 0, nw}};
248 CHECK(run(&s, echo, 4, wn, 1, &r) == 1 && r.err == OSH_E_DENIED && r.cmd[0].pid == 0, "denied write: status 1, DENIED, no child");
249 CHECK(!exists(nw), "denied write: file was not created");
250 OshRedir wt[] = {{OSH_R_OUT, 1, 0, old}};
251 CHECK(run(&s, echo, 4, wt, 1, &r) == 1 && !strcmp(slurp(old), "KEEP"), "denied truncate: existing file untouched");
252 OshRedir wa[] = {{OSH_R_APPEND, 1, 0, old}};
253 CHECK(run(&s, echo, 4, wa, 1, &r) == 1 && !strcmp(slurp(old), "KEEP"), "denied append: existing file untouched");
254 OshRedir rn[] = {{OSH_R_IN, 0, 0, old}};
255 CHECK(run(&s, cat, 3, rn, 1, &r) == 1 && r.err == OSH_E_DENIED && r.cmd[0].pid == 0, "denied read: status 1, no child");
256 CHECK(run_cd(&s, no, &r) == 1 && getcwd(cwd1, sizeof cwd1) && !strcmp(cwd1, cwd0), "denied cd: status 1, directory unchanged");
257 CHECK(strstr(diag_text(), "cd ") != NULL, "denied cd named");
258 /* a read-only grant never authorizes a write and the other way round */
259 char readonly[PATH_MAX];
260 P(readonly, "ok");
261 CHECK(osh_caps_effect(&caps, &s.binding, OSH_OP_OPEN_WRITE, "/etc/passwd") == OSH_E_DENIED, "no grant covers /etc/passwd");
262 CHECK(osh_caps_effect(&caps, &s.binding, OSH_OP_SPAWN, keep) == OSH_E_DENIED, "write/read grants do not authorize spawn");
263
264 /* symlink out of an allowed prefix */
265 char lnk[PATH_MAX], viaf[PATH_MAX], dang[PATH_MAX], dtarget[PATH_MAX];
266 P(lnk, "ok/lnk"); P(viaf, "ok/lnk/f"); P(dang, "ok/dang"); P(dtarget, "no/dtarget");
267 if (symlink(no, lnk) != 0) {}
268 OshRedir wl[] = {{OSH_R_OUT, 1, 0, viaf}};
269 CHECK(run(&s, echo, 4, wl, 1, &r) == 1 && !exists(viaf) && !exists("/dev/null/x"), "symlink escape denied");
270 char nof[PATH_MAX];
271 P(nof, "no/f");
272 CHECK(!exists(nof), "symlink escape: nothing created in the target");
273 if (symlink(dtarget, dang) != 0) {}
274 OshRedir wd[] = {{OSH_R_OUT, 1, 0, dang}};
275 CHECK(run(&s, echo, 4, wd, 1, &r) == 1 && !exists(dtarget), "dangling symlink out of the prefix: target not created");
276
277 /* ---- the documented non-claim: an allowed child is not contained ---- */
278 char bychild[PATH_MAX];
279 P(bychild, "no/by_child");
280 const char *tch2[] = {SELF, "--helper", "touch", bychild};
281 CHECK(run(&s, tch2, 4, NULL, 0, &r) == 0 && exists(bychild), "NON-CLAIM: an allowed child can write outside the write grants (no containment)");
282
283 /* ---- forged principal ---- */
284 osh_var_set(&s, "OSH_PRINCIPAL", "77");
285 osh_var_set(&s, "PRINCIPAL", "77");
286 osh_var_export(&s, "CAP_GEN", "1");
287 CHECK(run(&s, tch, 4, NULL, 0, &r) == 126 && !exists(mk), "variables named like a principal grant nothing");
288 OshBinding save = s.binding, f;
289 f = save; f.principal_id[0] = 78;
290 s.binding = f;
291 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_DENIED, "forged principal id (wrong subject) denied");
292 f = save; f.principal_id[9] = 1;
293 s.binding = f;
294 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126, "principal id with nonzero upper bytes denied");
295 f = save; f.cap_index = 200;
296 s.binding = f;
297 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126, "forged capability index denied");
298 f = save; f.cap_generation ^= (1ull << 40); /* same low 32 bits */
299 s.binding = f;
300 CHECK((uint32_t)f.cap_generation == (uint32_t)save.cap_generation, "forgery keeps the low 32 bits");
301 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_STALE, "generation differing only above bit 32 is STALE (never narrowed)");
302 f = save; f.domain = 1;
303 s.binding = f;
304 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_CAP_GEN_NARROW, "domain 1 + 64-bit generation: CAP_GEN_NARROW");
305 f.cap_generation = (uint32_t)save.cap_generation;
306 s.binding = f;
307 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_CAP_DOMAIN_MISMATCH, "domain 1 binding against hosted resources: CAP_DOMAIN_MISMATCH");
308 f = save; f.domain = 3;
309 s.binding = f;
310 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_CAP_DOMAIN_MISMATCH, "unknown domain refused");
311 s.binding = save;
312 CHECK(run(&s, echo, 4, w1, 1, &r) == 0, "restored binding works again");
313
314 /* ---- revocation and stale handles ---- */
315 RxCapRef office = rx_capadmin_office(&caps.admin);
316 int wi = 2; /* grant index of 'allow write' */
317 CHECK(caps.grants[wi].op == OSH_OP_OPEN_WRITE, "grant table order as written");
318 CHECK(rx_capadmin_revoke(&caps.admin, office, caps.grants[wi].ref) == RX_CAP_OK, "operator revokes the write grant");
319 diag_reset();
320 CHECK(run(&s, echo, 4, w1, 1, &r) == 1 && r.err == OSH_E_REVOKED, "same write after revoke: REVOKED");
321 CHECK(strstr(diag_text(), "denied (REVOKED)") != NULL, "revocation named in the diagnostic");
322 char o2[PATH_MAX];
323 P(o2, "ok/out2");
324 CHECK(!exists(o2), "no stray file");
325 OshRedir w2[] = {{OSH_R_OUT, 1, 0, o2}};
326 CHECK(run(&s, echo, 4, w2, 1, &r) == 1 && !exists(o2), "revoked grant: new file not created");
327 CHECK(run(&s, echo, 4, NULL, 0, &r) == 0, "other grants unaffected by the revoke (spawn still works)");
328 /* revoke the principal: everything stops */
329 CHECK(rx_capadmin_revoke(&caps.admin, office, caps.principal_ref) == RX_CAP_OK, "operator revokes the session principal");
330 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_REVOKED && r.cmd[0].pid == 0, "spawn after principal revoke: REVOKED, not started");
331 CHECK(run_cd(&s, ok, &r) == 1, "cd after principal revoke denied");
332 /* reclaim bumps the generation: the old handle is stale, and stays so after the slot is reused */
333 uint64_t g0 = caps.principal_ref.generation;
334 CHECK(rx_capadmin_reclaim(&caps.admin, office, caps.principal_ref.cap_id) == RX_CAP_OK, "slot reclaimed");
335 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_STALE, "old handle after reclaim: STALE");
336 RxCapMint m = {.issuer = 0, .subject = 77, .resource = 0, .rights = RX_RIGHT_READ, .parent = {UINT32_MAX, 0}, .authority = office};
337 RxCapRef again;
338 CHECK(rx_capadmin_mint(&caps.admin, &m, &again) == RX_CAP_OK && again.cap_id == caps.principal_ref.cap_id && again.generation > g0, "slot reused with a larger 64-bit generation");
339 CHECK(run(&s, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_STALE, "old handle still STALE after reuse");
340
341 /* ---- seal: nothing in this process can mint or revoke ---- */
342 OshCaps c2;
343 OshSession s2;
344 CHECK(start_caps(&c2, policy) == 0, "second root starts (above the first root's generations)");
345 CHECK(c2.principal_ref.generation > caps.principal_ref.generation || c2.principal_ref.generation != caps.principal_ref.generation, "second root has its own generations");
346 session(&s2, dfd);
347 osh_caps_attach(&c2, &s2);
348 osh_caps_seal(&c2);
349 CHECK(run(&s2, echo, 4, w1, 1, &r) == 0, "sealed session: allowed effects still work");
350 CHECK(rx_capadmin_mint(&c2.admin, &m, &again) != RX_CAP_OK, "sealed: mint impossible");
351 CHECK(rx_capadmin_revoke(&c2.admin, rx_capadmin_office(&c2.admin), c2.principal_ref) != RX_CAP_OK, "sealed: revoke impossible");
352 CHECK(run(&s2, tch, 4, NULL, 0, &r) == 126 && !exists(mk), "sealed: denied stays denied");
353 osh_caps_stop(&c2);
354 osh_session_free(&s2);
355
356 /* ---- policy errors, empty policy ---- */
357 OshCaps c3;
358 char err[256];
359 CHECK(osh_caps_start(&c3) == 0 && osh_caps_load(&c3, "allow spawn /\n", err, sizeof err) != 0, "grant before principal refused");
360 osh_caps_stop(&c3);
361 CHECK(osh_caps_start(&c3) == 0 && osh_caps_load(&c3, "principal 5\nallow spawn relative/path\n", err, sizeof err) != 0, "relative prefix refused");
362 osh_caps_stop(&c3);
363 CHECK(osh_caps_start(&c3) == 0 && osh_caps_load(&c3, "principal 5\nallow exec /bin\n", err, sizeof err) != 0, "unknown verb refused");
364 osh_caps_stop(&c3);
365 CHECK(osh_caps_start(&c3) == 0 && osh_caps_load(&c3, "", err, sizeof err) != 0, "policy without a principal refused");
366 osh_caps_stop(&c3);
367 OshSession s3;
368 session(&s3, dfd);
369 CHECK(osh_caps_start(&c3) == 0 && osh_caps_load(&c3, "principal 5\n", err, sizeof err) == 0, "principal-only policy loads");
370 osh_caps_attach(&c3, &s3);
371 CHECK(run(&s3, echo, 4, NULL, 0, &r) == 126 && r.err == OSH_E_DENIED, "no grants: default deny");
372 osh_caps_stop(&c3);
373 osh_session_free(&s3);
374
375 /* ---- the osh program itself ---- */
376 if (osh_bin) {
377 char pol[PATH_MAX], cmd[4 * PATH_MAX], e1[PATH_MAX];
378 P(pol, "policy");
379 put(pol, policy);
380 P(e1, "ok/cli");
381 snprintf(cmd, sizeof cmd, "%s --helper echo viaosh > %s", SELF, e1);
382 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 0 && !strcmp(slurp(e1), "viaosh\n"), "osh --caps: allowed command runs");
383 char nfile[PATH_MAX];
384 P(nfile, "no/cli");
385 snprintf(cmd, sizeof cmd, "%s --helper echo x > %s", SELF, nfile);
386 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 1 && !exists(nfile), "osh --caps: denied redirection status 1, file not created");
387 snprintf(cmd, sizeof cmd, "%s --helper touch %s", NOHELP, mk);
388 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 126 && !exists(mk), "osh --caps: denied spawn status 126, no marker");
389 snprintf(cmd, sizeof cmd, "cd %s", no);
390 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 1, "osh --caps: denied cd status 1");
391 snprintf(cmd, sizeof cmd, "cd %s", ok);
392 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 0, "osh --caps: allowed cd status 0");
393 snprintf(cmd, sizeof cmd, "OSH_PRINCIPAL=77 PRINCIPAL=77 %s --helper touch %s", NOHELP, mk);
394 CHECK(run_osh(osh_bin, pol, cmd, "OSH_PRINCIPAL=77") == 126 && !exists(mk), "osh --caps: principal-looking variables grant nothing");
395 snprintf(cmd, sizeof cmd, "/bin/true");
396 CHECK(run_osh(osh_bin, pol, cmd, NULL) == 126, "osh --caps: /bin/true is outside the spawn grant");
397 P(tmpf, "missing-policy");
398 CHECK(run_osh(osh_bin, tmpf, "true", NULL) == 70, "osh --caps with an unreadable policy refuses to start");
399 char bad[PATH_MAX];
400 P(bad, "bad-policy");
401 put(bad, "principal 1\nallow bogus /\n");
402 CHECK(run_osh(osh_bin, bad, "true", NULL) == 70, "osh --caps with a bad policy refuses to start");
403 {
404 char nul[PATH_MAX];
405 P(nul, "nul-policy");
406 FILE *nf = fopen(nul, "wb");
407 static const char body[] = "principal 1\nallow spawn /bin\0\nallow spawn /usr\n";
408 if (nf) { fwrite(body, 1, sizeof body - 1, nf); fclose(nf); }
409 CHECK(nf && run_osh(osh_bin, nul, "true", NULL) == 70, "osh --caps with a NUL byte in the policy refuses to start");
410 }
411 char pol0[PATH_MAX];
412 P(pol0, "empty-policy");
413 put(pol0, "principal 1\n");
414 CHECK(run_osh(osh_bin, pol0, "/bin/true", NULL) == 126, "osh --caps with no grants: everything denied");
415 /* fail closed: no --caps at all is the same as a policy with no grants */
416 char dflt[PATH_MAX];
417 P(dflt, "default-denied");
418 CHECK(run_osh(osh_bin, NULL, "/bin/true", NULL) == 126, "osh without --caps: spawn denied (fail closed)");
419 snprintf(cmd, sizeof cmd, "printf x > %s", dflt);
420 CHECK(run_osh(osh_bin, NULL, cmd, NULL) == 1 && !exists(dflt), "osh without --caps: redirection denied, file not created");
421 CHECK(run_osh(osh_bin, NULL, "cd /", NULL) == 1, "osh without --caps: cd denied");
422 CHECK(run_osh(osh_bin, NULL, "printf ''", NULL) == 0, "osh without --caps: a builtin with no effect still runs");
423 CHECK(run_osh(osh_bin, NULL, "OSH_CAPS=/ PATH=/bin true", "OSH_CAPS=/") == 126, "osh without --caps: variables grant nothing");
424 }
425
426 osh_caps_stop(&caps);
427 osh_session_free(&s);
428 close(dfd);
429 if (g_fail) printf("DIAG: %s\n", slurp(DIAG));
430 rm_rf(T);
431 printf("checks: %d passed, %d failed\n", g_pass, g_fail);
432 printf(g_fail ? "OSH_CAPS_FAIL\n" : "OSH_CAPS_PASS\n");
433 return g_fail ? 1 : 0;
434}