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/src/osh/host/osh_caps.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 / src / osh / host / osh_caps.c` 231 lines ยท 9257 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 /* osh_caps.c -- see osh_caps.h. Policy/mapping/prefix logic in src/osh/osh_core.om. */ 18 #include "osh_caps.h" 19 #include "omega_native.h" 20 21 #include 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 30 int osh_caps_start(OshCaps *c) 31 { 32 memset(c, 0, sizeof *c); 33 int rc = rx_caproot_start(&c->root, &c->admin); 34 if (rc == RX_CAP_OK) c->started = 1; 35 return rc; 36 } 37 38 static void set_err(char *err, size_t n, const char *fmt, int line, const char *a) 39 { 40 if (err && n) snprintf(err, n, fmt, line, a ? a : ""); 41 } 42 43 static int op_right(int op) 44 { 45 uint64_t r = 0; 46 if (omega2_osh_om_op_right(&r, (uint64_t)op) != OMEGA_OK) return 0; 47 return (int)r; 48 } 49 50 /* Canonical absolute form of a target: realpath(), or realpath(parent) + "/" + base when the target does not exist yet. 51 * A dangling symlink is refused (open(O_CREAT) would create its target somewhere else). 0 ok, -1 refuse. */ 52 static int canon_target(const char *path, char *out, size_t cap) 53 { 54 if (!path || !*path) return -1; 55 char rp[PATH_MAX]; 56 if (realpath(path, rp)) { 57 if (strlen(rp) >= cap) return -1; 58 strcpy(out, rp); 59 return 0; 60 } 61 if (errno != ENOENT) return -1; 62 struct stat st; 63 if (lstat(path, &st) == 0) return -1; /* dangling symlink */ 64 char dir[PATH_MAX]; 65 const char *slash = strrchr(path, '/'); 66 const char *base; 67 if (!slash) { strcpy(dir, "."); base = path; } 68 else if (slash == path) { strcpy(dir, "/"); base = path + 1; } 69 else { 70 if ((size_t)(slash - path) >= sizeof dir) return -1; 71 memcpy(dir, path, (size_t)(slash - path)); 72 dir[slash - path] = 0; 73 base = slash + 1; 74 } 75 if (!*base || !strcmp(base, ".") || !strcmp(base, "..")) return -1; 76 char rd[PATH_MAX]; 77 if (!realpath(dir, rd)) return -1; 78 size_t n = strlen(rd); 79 if (n + 1 + strlen(base) + 1 > cap) return -1; 80 memcpy(out, rd, n); 81 if (n == 1 && rd[0] == '/') n = 0; 82 out[n] = '/'; 83 strcpy(out + n + 1, base); 84 return 0; 85 } 86 87 static int under(const char *prefix, const char *p) 88 { 89 size_t np = strlen(prefix), npath = strlen(p); 90 uint64_t ok = 0; 91 if (omega2_osh_om_path_under(&ok, (const uint8_t *)prefix, np, (uint64_t)np, 92 (const uint8_t *)p, npath, (uint64_t)npath) != OMEGA_OK) 93 return 0; 94 return ok != 0; 95 } 96 97 int osh_caps_load(OshCaps *c, const char *text, char *err, size_t errsz) 98 { 99 if (!c->started || c->sealed) { set_err(err, errsz, "line %d: capability root not available%s", 0, ""); return -1; } 100 int have_principal = 0, line = 0; 101 const char *p = text; 102 while (*p) { 103 const char *e = strchr(p, '\n'); 104 size_t len = e ? (size_t)(e - p) : strlen(p); 105 char buf[PATH_MAX + 64]; 106 line++; 107 if (len >= sizeof buf) { set_err(err, errsz, "line %d: too long%s", line, ""); return -1; } 108 memcpy(buf, p, len); 109 buf[len] = 0; 110 p = e ? e + 1 : p + len; 111 char *h = strchr(buf, '#'); 112 if (h) *h = 0; 113 char verb[16], kind[16], path[PATH_MAX], junk[2]; 114 unsigned long pr; 115 int n; 116 if ((n = sscanf(buf, " %15s", verb)) != 1) continue; /* blank */ 117 if (!strcmp(verb, "principal")) { 118 if (have_principal || sscanf(buf, " principal %lu %1s", &pr, junk) != 1 || pr > 0xFFFFFFFFul) { 119 set_err(err, errsz, "line %d: bad or repeated principal%s", line, ""); 120 return -1; 121 } 122 c->principal = (uint32_t)pr; 123 RxCapMint m = {.issuer = 0, .subject = c->principal, .resource = 0, .rights = RX_RIGHT_READ, 124 .parent = {UINT32_MAX, 0}, .authority = c->admin.office}; 125 int rc = rx_capadmin_mint(&c->admin, &m, &c->principal_ref); 126 if (rc != RX_CAP_OK) { set_err(err, errsz, "line %d: mint refused: %s", line, rx_cap_strerror(rc)); return -1; } 127 have_principal = 1; 128 } else if (!strcmp(verb, "allow")) { 129 if (!have_principal || sscanf(buf, " allow %15s %4095s %1s", kind, path, junk) != 2) { 130 set_err(err, errsz, "line %d: expected 'allow spawn|read|write|chdir ' after principal%s", line, ""); 131 return -1; 132 } 133 int op = !strcmp(kind, "spawn") ? OSH_OP_SPAWN : !strcmp(kind, "read") ? OSH_OP_OPEN_READ 134 : !strcmp(kind, "write") ? OSH_OP_OPEN_WRITE : !strcmp(kind, "chdir") ? OSH_OP_CHDIR : 0; 135 char canon[PATH_MAX]; 136 if (!op || path[0] != '/' || !realpath(path, canon) || c->ngrants >= OSH_CAPS_MAX_GRANTS) { 137 set_err(err, errsz, "line %d: bad grant (verb, absolute existing path, or table full): %s", line, path); 138 return -1; 139 } 140 OshGrant *g = &c->grants[c->ngrants]; 141 RxCapMint m = {.issuer = 0, .subject = c->principal, .resource = (uint64_t)c->ngrants + 1, 142 .rights = (uint32_t)op_right(op), .parent = {UINT32_MAX, 0}, .authority = c->admin.office}; 143 int rc = rx_capadmin_mint(&c->admin, &m, &g->ref); 144 if (rc != RX_CAP_OK) { set_err(err, errsz, "line %d: mint refused: %s", line, rx_cap_strerror(rc)); return -1; } 145 g->op = op; 146 g->prefix = strdup(canon); 147 g->resource = m.resource; 148 if (!g->prefix) { set_err(err, errsz, "line %d: out of memory%s", line, ""); return -1; } 149 c->ngrants++; 150 } else { 151 set_err(err, errsz, "line %d: unknown directive: %s", line, verb); 152 return -1; 153 } 154 (void)n; 155 } 156 if (!have_principal) { set_err(err, errsz, "line %d: no principal line%s", line, ""); return -1; } 157 return 0; 158 } 159 160 void osh_caps_attach(OshCaps *c, OshSession *s) 161 { 162 memset(&s->binding, 0, sizeof s->binding); 163 s->binding.principal_id[0] = (uint8_t)c->principal; 164 s->binding.principal_id[1] = (uint8_t)(c->principal >> 8); 165 s->binding.principal_id[2] = (uint8_t)(c->principal >> 16); 166 s->binding.principal_id[3] = (uint8_t)(c->principal >> 24); 167 s->binding.domain = 2; /* hosted capability authority: 64-bit generation, never narrowed */ 168 s->binding.cap_index = c->principal_ref.cap_id; 169 s->binding.cap_generation = c->principal_ref.generation; 170 s->binding.valid = 1; 171 s->effect_hook = osh_caps_effect; 172 s->effect_ctx = c; 173 } 174 175 static int map_rc(int rc) 176 { 177 uint64_t r = OSH_E_DENIED; 178 if (omega2_osh_om_map_cap_rc(&r, (uint64_t)(int64_t)rc) != OMEGA_OK) 179 return OSH_E_DENIED; 180 return (int)(int64_t)r; 181 } 182 183 int osh_caps_effect(void *ctx, const OshBinding *b, int op, const char *path) 184 { 185 OshCaps *c = ctx; 186 if (!c || !c->started || !b || !b->valid) return OSH_E_DENIED; 187 uint64_t chk = 0; 188 if (omega2_osh_om_check_binding(&chk, (uint64_t)b->domain, b->principal_id, 32) != OMEGA_OK) 189 return OSH_E_DENIED; 190 if ((int64_t)chk < 0) return (int)(-(int64_t)chk); 191 uint32_t subject = (uint32_t)chk; 192 /* 1. the session's own principal capability, with the generation exactly as bound (64 bits) */ 193 RxCapRef pref = {b->cap_index, b->cap_generation}; 194 int rc = rx_caproot_validate(&c->root, pref, subject, 0, RX_RIGHT_READ, NULL); 195 if (rc != RX_CAP_OK) return map_rc(rc); 196 /* 2. a grant for this operation whose prefix contains the canonical target, validated at this moment */ 197 uint32_t need_right = (uint32_t)op_right(op); 198 if (need_right == 0) return OSH_E_DENIED; 199 char canon[PATH_MAX]; 200 if (canon_target(path, canon, sizeof canon) != 0) return OSH_E_DENIED; 201 int result = OSH_E_DENIED; 202 for (int i = 0; i < c->ngrants; i++) { 203 const OshGrant *g = &c->grants[i]; 204 if (g->op != op || !under(g->prefix, canon)) continue; 205 int e = map_rc(rx_caproot_validate(&c->root, g->ref, subject, g->resource, need_right, NULL)); 206 if (e == OSH_E_OK) return OSH_E_OK; 207 result = e; 208 } 209 return result; 210 } 211 212 void osh_caps_seal(OshCaps *c) 213 { 214 if (!c->started || c->sealed) return; 215 memset(c->admin.token, 0, sizeof c->admin.token); 216 if (c->admin.ctl_fd >= 0) close(c->admin.ctl_fd); /* the root sees EOF and exits */ 217 c->admin.ctl_fd = -1; 218 c->admin.running = false; 219 if (c->admin.root_pid > 0) waitpid(c->admin.root_pid, NULL, 0); 220 c->admin.root_pid = 0; 221 c->sealed = 1; 222 } 223 224 void osh_caps_stop(OshCaps *c) 225 { 226 if (!c->started) return; 227 rx_caproot_stop(&c->root, &c->admin); 228 for (int i = 0; i < c->ngrants; i++) free(c->grants[i].prefix); 229 c->ngrants = 0; 230 c->started = 0; 231 }