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
omega-mv / src / osh / host / osh_caps.c
| 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 <errno.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <unistd.h> |
| 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 | (void)omega2_osh_om_op_right(&r, (uint64_t)op); |
| 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 | (void)omega2_osh_om_path_under(&ok, (const uint8_t *)prefix, np, (uint64_t)np, |
| 92 | (const uint8_t *)p, npath, (uint64_t)npath); |
| 93 | return ok != 0; |
| 94 | } |
| 95 | |
| 96 | int osh_caps_load(OshCaps *c, const char *text, char *err, size_t errsz) |
| 97 | { |
| 98 | if (!c->started || c->sealed) { set_err(err, errsz, "line %d: capability root not available%s", 0, ""); return -1; } |
| 99 | int have_principal = 0, line = 0; |
| 100 | const char *p = text; |
| 101 | while (*p) { |
| 102 | const char *e = strchr(p, '\n'); |
| 103 | size_t len = e ? (size_t)(e - p) : strlen(p); |
| 104 | char buf[PATH_MAX + 64]; |
| 105 | line++; |
| 106 | if (len >= sizeof buf) { set_err(err, errsz, "line %d: too long%s", line, ""); return -1; } |
| 107 | memcpy(buf, p, len); |
| 108 | buf[len] = 0; |
| 109 | p = e ? e + 1 : p + len; |
| 110 | char *h = strchr(buf, '#'); |
| 111 | if (h) *h = 0; |
| 112 | char verb[16], kind[16], path[PATH_MAX], junk[2]; |
| 113 | unsigned long pr; |
| 114 | int n; |
| 115 | if ((n = sscanf(buf, " %15s", verb)) != 1) continue; /* blank */ |
| 116 | if (!strcmp(verb, "principal")) { |
| 117 | if (have_principal || sscanf(buf, " principal %lu %1s", &pr, junk) != 1 || pr > 0xFFFFFFFFul) { |
| 118 | set_err(err, errsz, "line %d: bad or repeated principal%s", line, ""); |
| 119 | return -1; |
| 120 | } |
| 121 | c->principal = (uint32_t)pr; |
| 122 | RxCapMint m = {.issuer = 0, .subject = c->principal, .resource = 0, .rights = RX_RIGHT_READ, |
| 123 | .parent = {UINT32_MAX, 0}, .authority = c->admin.office}; |
| 124 | int rc = rx_capadmin_mint(&c->admin, &m, &c->principal_ref); |
| 125 | if (rc != RX_CAP_OK) { set_err(err, errsz, "line %d: mint refused: %s", line, rx_cap_strerror(rc)); return -1; } |
| 126 | have_principal = 1; |
| 127 | } else if (!strcmp(verb, "allow")) { |
| 128 | if (!have_principal || sscanf(buf, " allow %15s %4095s %1s", kind, path, junk) != 2) { |
| 129 | set_err(err, errsz, "line %d: expected 'allow spawn|read|write|chdir <path>' after principal%s", line, ""); |
| 130 | return -1; |
| 131 | } |
| 132 | int op = !strcmp(kind, "spawn") ? OSH_OP_SPAWN : !strcmp(kind, "read") ? OSH_OP_OPEN_READ |
| 133 | : !strcmp(kind, "write") ? OSH_OP_OPEN_WRITE : !strcmp(kind, "chdir") ? OSH_OP_CHDIR : 0; |
| 134 | char canon[PATH_MAX]; |
| 135 | if (!op || path[0] != '/' || !realpath(path, canon) || c->ngrants >= OSH_CAPS_MAX_GRANTS) { |
| 136 | set_err(err, errsz, "line %d: bad grant (verb, absolute existing path, or table full): %s", line, path); |
| 137 | return -1; |
| 138 | } |
| 139 | OshGrant *g = &c->grants[c->ngrants]; |
| 140 | RxCapMint m = {.issuer = 0, .subject = c->principal, .resource = (uint64_t)c->ngrants + 1, |
| 141 | .rights = (uint32_t)op_right(op), .parent = {UINT32_MAX, 0}, .authority = c->admin.office}; |
| 142 | int rc = rx_capadmin_mint(&c->admin, &m, &g->ref); |
| 143 | if (rc != RX_CAP_OK) { set_err(err, errsz, "line %d: mint refused: %s", line, rx_cap_strerror(rc)); return -1; } |
| 144 | g->op = op; |
| 145 | g->prefix = strdup(canon); |
| 146 | g->resource = m.resource; |
| 147 | if (!g->prefix) { set_err(err, errsz, "line %d: out of memory%s", line, ""); return -1; } |
| 148 | c->ngrants++; |
| 149 | } else { |
| 150 | set_err(err, errsz, "line %d: unknown directive: %s", line, verb); |
| 151 | return -1; |
| 152 | } |
| 153 | (void)n; |
| 154 | } |
| 155 | if (!have_principal) { set_err(err, errsz, "line %d: no principal line%s", line, ""); return -1; } |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void osh_caps_attach(OshCaps *c, OshSession *s) |
| 160 | { |
| 161 | memset(&s->binding, 0, sizeof s->binding); |
| 162 | s->binding.principal_id[0] = (uint8_t)c->principal; |
| 163 | s->binding.principal_id[1] = (uint8_t)(c->principal >> 8); |
| 164 | s->binding.principal_id[2] = (uint8_t)(c->principal >> 16); |
| 165 | s->binding.principal_id[3] = (uint8_t)(c->principal >> 24); |
| 166 | s->binding.domain = 2; /* hosted capability authority: 64-bit generation, never narrowed */ |
| 167 | s->binding.cap_index = c->principal_ref.cap_id; |
| 168 | s->binding.cap_generation = c->principal_ref.generation; |
| 169 | s->binding.valid = 1; |
| 170 | s->effect_hook = osh_caps_effect; |
| 171 | s->effect_ctx = c; |
| 172 | } |
| 173 | |
| 174 | static int map_rc(int rc) |
| 175 | { |
| 176 | uint64_t r = 0; |
| 177 | (void)omega2_osh_om_map_cap_rc(&r, (uint64_t)(int64_t)rc); |
| 178 | return (int)(int64_t)r; |
| 179 | } |
| 180 | |
| 181 | int osh_caps_effect(void *ctx, const OshBinding *b, int op, const char *path) |
| 182 | { |
| 183 | OshCaps *c = ctx; |
| 184 | if (!c || !c->started || !b || !b->valid) return OSH_E_DENIED; |
| 185 | uint64_t chk = 0; |
| 186 | (void)omega2_osh_om_check_binding(&chk, (uint64_t)b->domain, b->principal_id, 32); |
| 187 | if ((int64_t)chk < 0) return (int)(-(int64_t)chk); |
| 188 | uint32_t subject = (uint32_t)chk; |
| 189 | /* 1. the session's own principal capability, with the generation exactly as bound (64 bits) */ |
| 190 | RxCapRef pref = {b->cap_index, b->cap_generation}; |
| 191 | int rc = rx_caproot_validate(&c->root, pref, subject, 0, RX_RIGHT_READ, NULL); |
| 192 | if (rc != RX_CAP_OK) return map_rc(rc); |
| 193 | /* 2. a grant for this operation whose prefix contains the canonical target, validated at this moment */ |
| 194 | char canon[PATH_MAX]; |
| 195 | if (canon_target(path, canon, sizeof canon) != 0) return OSH_E_DENIED; |
| 196 | int result = OSH_E_DENIED; |
| 197 | for (int i = 0; i < c->ngrants; i++) { |
| 198 | const OshGrant *g = &c->grants[i]; |
| 199 | if (g->op != op || !under(g->prefix, canon)) continue; |
| 200 | int e = map_rc(rx_caproot_validate(&c->root, g->ref, subject, g->resource, (uint32_t)op_right(op), NULL)); |
| 201 | if (e == OSH_E_OK) return OSH_E_OK; |
| 202 | result = e; |
| 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | void osh_caps_seal(OshCaps *c) |
| 208 | { |
| 209 | if (!c->started || c->sealed) return; |
| 210 | memset(c->admin.token, 0, sizeof c->admin.token); |
| 211 | if (c->admin.ctl_fd >= 0) close(c->admin.ctl_fd); /* the root sees EOF and exits */ |
| 212 | c->admin.ctl_fd = -1; |
| 213 | c->admin.running = false; |
| 214 | if (c->admin.root_pid > 0) waitpid(c->admin.root_pid, NULL, 0); |
| 215 | c->admin.root_pid = 0; |
| 216 | c->sealed = 1; |
| 217 | } |
| 218 | |
| 219 | void osh_caps_stop(OshCaps *c) |
| 220 | { |
| 221 | if (!c->started) return; |
| 222 | rx_caproot_stop(&c->root, &c->admin); |
| 223 | for (int i = 0; i < c->ngrants; i++) free(c->grants[i].prefix); |
| 224 | c->ngrants = 0; |
| 225 | c->started = 0; |
| 226 | } |