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/runtime/rx_r8_aegis.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 / runtime / rx_r8_aegis.c` 726 lines · 34707 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 * R8: AEGIS resident authority (ADR 0016 §41, scope note 2026-09-28). 19 * 20 * A client principal ("worker", subject 51) holds its authority in 21 * capability slots in the world. Its work reaction reads its reference from a 22 * slot. The test only publishes outside stimuli and reads the world: 23 * 24 * intent (outside) -> worker.ask -> request -> aegis.decide -> decision 25 * -> root.install (Aienux mint) -> slot -> worker.tick wakes and runs 26 * 27 * and afterwards, with the slot live, every input runs worker.tick with no 28 * AEGIS activation at all. Attacks on the slow path must be refused by the 29 * root whatever AEGIS or a hostile writer publishes. 30 * 31 * Authority is the native Aienux capability authority. 32 */ 33 #include "runtime/aienos_cap.h" 34 #include "runtime/rx_aegis.h" 35 #include "runtime/rx_world.h" 36 #include "omega_evidence.h" 37 #include "sha256.h" 38 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 49 extern char **environ; 50 51 enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_WORKER = 51, SUBJ_OTHER = 52, SUBJ_ROGUE = 60 }; 52 53 #define RES_WORK 0x9000001ull /* rule 1: R|W, no lease, no approval */ 54 #define RES_INPUT 0x9000002ull /* the worker's input, static read */ 55 #define RES_INTENT 0x9000003ull /* outside stimulus: what the worker should now ask for */ 56 #define RES_LOG 0x9000005ull /* the worker's own log, static read and write */ 57 #define RES_EFFECT 0x9000011ull /* rule 2: R|W|E, needs a human */ 58 #define RES_LEASED 0x9000021ull /* rule 3: R, lease 100 */ 59 #define RES_OUTSIDE 0x9100000ull /* rule 4: a policy mistake outside the worker's domain */ 60 #define RES_NORULE 0x9000040ull /* in the domain, no rule */ 61 #define RES_ROGUE 0x9800000ull 62 63 static int g_checks; 64 static int g_fail; 65 66 #define CHECK(cond, ...) do { \ 67 g_checks++; \ 68 if (!(cond)) { \ 69 g_fail++; \ 70 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ 71 fprintf(stderr, __VA_ARGS__); \ 72 fputc('\n', stderr); \ 73 } \ 74 } while (0) 75 76 static uint64_t now_ns(void) { 77 struct timespec ts; 78 clock_gettime(CLOCK_MONOTONIC, &ts); 79 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 80 } 81 82 typedef struct { 83 AienosCapAdmin *admin; 84 AienosCapView *view; 85 RxWorld w; 86 RxAegisFaculty a; 87 RxObjRef input, intent, work, leased_out, lease_log; 88 RxCapRef ext_input, ext_intent; 89 uint32_t r_tick, r_ask, r_lease; 90 uint64_t inputs; 91 } Env; 92 93 static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { 94 AienosCapRef office; 95 aienos_cap_office(e->admin, &office); 96 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; 97 AienosCapRef r = { UINT32_MAX, 0 }; 98 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; 99 return (RxCapRef){ r.cap_id, r.generation }; 100 } 101 102 static uint64_t fld(Env *e, RxObjRef r, uint32_t i) { 103 RxObject o; 104 if (rx_world_read(&e->w, r, &o) != RX_OK) return UINT64_MAX; 105 return o.field[i]; 106 } 107 108 static int settle(Env *e) { return rx_world_wait_quiescent(&e->w, 20000); } 109 110 static RxObjRef slot(Env *e, uint32_t j) { return e->a.o[0].slot[j]; } 111 112 /* ---- the worker's reactions ---- */ 113 114 static const RxSnapshotDep *in_of(const RxCtx *c, RxObjRef r) { 115 for (uint32_t i = 0; i < c->n_in; i++) 116 if (c->in[i].obj.id == r.id) return &c->in[i]; 117 return NULL; 118 } 119 120 /* worker.tick: count each input into `work`. Needs R|W on RES_WORK from slot 0. */ 121 static int fn_tick(RxCtx *c) { 122 Env *e = c->user; 123 const RxSnapshotDep *in = in_of(c, e->input), *wk = in_of(c, e->work); 124 if (!in || !wk) return -1; 125 if (in->field[0] == wk->field[1]) return 0; /* this input already counted */ 126 c->out[c->n_out++] = (RxMutation){ e->work, 0, wk->field[0] + 1 }; 127 c->out[c->n_out++] = (RxMutation){ e->work, 1, in->field[0] }; 128 return 0; 129 } 130 131 /* worker.lease: read the leased object and log the input it saw. Needs READ on 132 * RES_LEASED from slot 2; the log is the worker's own. */ 133 static int fn_lease(RxCtx *c) { 134 Env *e = c->user; 135 const RxSnapshotDep *in = in_of(c, e->input), *lo = in_of(c, e->leased_out); 136 if (!in || !lo) return -1; 137 c->out[c->n_out++] = (RxMutation){ e->lease_log, 0, in->field[0] }; 138 return 0; 139 } 140 141 /* worker.ask: the worker turns an intent into its own authority request. 142 * intent: 0 seq, 1 resource, 2 rights, 3 lease, 4 slot, 5 op */ 143 static int fn_ask(RxCtx *c) { 144 Env *e = c->user; 145 const RxSnapshotDep *it = in_of(c, e->intent), *rq = in_of(c, e->a.o[0].request); 146 if (!it || !rq) return -1; 147 if (it->field[0] == 0 || it->field[0] == rq->field[0]) return 0; 148 for (uint32_t i = 0; i < 6; i++) 149 c->out[c->n_out++] = (RxMutation){ e->a.o[0].request, i, it->field[i] }; 150 return 0; 151 } 152 153 static int env_start(Env *e) { 154 memset(e, 0, sizeof(*e)); 155 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; 156 if (rx_world_init_native(&e->w, e->view, 4, 1u << 18) != RX_OK) return -1; 157 e->w.external_subject = SUBJ_EXTERNAL; 158 const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE; 159 160 RxAegisPolicy pol; 161 memset(&pol, 0, sizeof pol); 162 pol.n_rules = 4; 163 pol.rules[0] = (RxAegisRule){ 1, SUBJ_WORKER, 0x9000000, 0x900000f, RW, 0, 0 }; 164 pol.rules[1] = (RxAegisRule){ 2, SUBJ_WORKER, 0x9000010, 0x900001f, RW | RX_RIGHT_EFFECT, 0, 1 }; 165 pol.rules[2] = (RxAegisRule){ 3, SUBJ_WORKER, 0x9000020, 0x900002f, R, 100, 0 }; 166 pol.rules[3] = (RxAegisRule){ 4, SUBJ_WORKER, RES_OUTSIDE, RES_OUTSIDE, RW, 0, 0 }; 167 RxAegisClient cl = { SUBJ_WORKER, 0x9000000, 0x90000ff, RW | RX_RIGHT_EFFECT }; 168 if (rx_aegis_create(&e->a, &e->w, e->admin, &pol, &cl, 1) != RX_OK) return -1; 169 170 RxAegisCaps caps; 171 caps.aegis_request = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); 172 caps.aegis_approval = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_APPROVAL), R); 173 caps.aegis_decision = mint(e, RX_AEGIS_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RW); 174 caps.root_request = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R); 175 caps.root_decision = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_DECISION), R); 176 for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) 177 caps.root_slot[j] = mint(e, RX_AEGIS_ROOT_SUBJ, rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + j), RW); 178 if (rx_aegis_register(&e->a, &caps) != RX_OK) return -1; 179 180 uint64_t z[RX_MAX_FIELDS] = { 0 }; 181 if (rx_world_create(&e->w, 0x9001, RX_PERSIST_RESIDENT, RES_INPUT, z, &e->input) != RX_OK || 182 rx_world_create(&e->w, 0x9002, RX_PERSIST_RESIDENT, RES_INTENT, z, &e->intent) != RX_OK || 183 rx_world_create(&e->w, 0x9003, RX_PERSIST_RESIDENT, RES_WORK, z, &e->work) != RX_OK || 184 rx_world_create(&e->w, 0x9004, RX_PERSIST_RESIDENT, RES_LEASED, z, &e->leased_out) != RX_OK || 185 rx_world_create(&e->w, 0x9005, RX_PERSIST_RESIDENT, RES_LOG, z, &e->lease_log) != RX_OK) 186 return -1; 187 e->ext_input = mint(e, SUBJ_EXTERNAL, RES_INPUT, RX_RIGHT_WRITE); 188 e->ext_intent = mint(e, SUBJ_EXTERNAL, RES_INTENT, RX_RIGHT_WRITE); 189 190 /* Static authority the worker is born with: read its input and its intent, 191 * read its own slots, write its own request. Nothing on RES_WORK. */ 192 RxCapRef w_input = mint(e, SUBJ_WORKER, RES_INPUT, R); 193 RxCapRef w_intent = mint(e, SUBJ_WORKER, RES_INTENT, R); 194 RxCapRef w_request = mint(e, SUBJ_WORKER, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), RW); 195 RxCapRef w_slot[RX_AEGIS_SLOTS]; 196 for (uint32_t j = 0; j < RX_AEGIS_SLOTS; j++) 197 w_slot[j] = mint(e, SUBJ_WORKER, rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + j), R); 198 199 RxReactionDesc d; 200 memset(&d, 0, sizeof d); 201 d.name = "worker.tick"; 202 d.faculty = RX_FACULTY_EXTERNAL; 203 d.subject = SUBJ_WORKER; 204 d.priority = RX_PRIO_INTERACTIVE; 205 d.fn = fn_tick; 206 d.user = e; 207 d.n_triggers = 1; 208 d.triggers[0] = (RxDep){ e->input, RX_ALL_FIELDS }; 209 d.n_reads = 1; 210 d.reads[0] = (RxDep){ e->work, RX_ALL_FIELDS }; 211 d.n_writes = 1; 212 d.writes[0] = (RxDep){ e->work, RX_ALL_FIELDS }; 213 d.n_caps = 3; 214 d.caps[0] = (RxCapNeed){ w_input, RES_INPUT, R }; 215 d.caps[1] = (RxCapNeed){ { UINT32_MAX, 0 }, RES_WORK, RW }; 216 d.caps[2] = (RxCapNeed){ w_slot[0], rx_aegis_res(0, RX_AEGIS_RES_SLOT0), R }; 217 rx_aegis_use_slot(&d, 1, slot(e, 0)); 218 if (rx_world_add_reaction(&e->w, &d, &e->r_tick) != RX_OK) return -1; 219 220 memset(&d, 0, sizeof d); 221 d.name = "worker.lease"; 222 d.faculty = RX_FACULTY_EXTERNAL; 223 d.subject = SUBJ_WORKER; 224 d.priority = RX_PRIO_INTERACTIVE; 225 d.fn = fn_lease; 226 d.user = e; 227 d.n_triggers = 1; 228 d.triggers[0] = (RxDep){ e->input, RX_ALL_FIELDS }; 229 d.n_reads = 1; 230 d.reads[0] = (RxDep){ e->leased_out, RX_ALL_FIELDS }; 231 d.n_writes = 1; 232 d.writes[0] = (RxDep){ e->lease_log, RX_ALL_FIELDS }; 233 d.n_caps = 4; 234 d.caps[0] = (RxCapNeed){ w_input, RES_INPUT, R }; 235 d.caps[1] = (RxCapNeed){ { UINT32_MAX, 0 }, RES_LEASED, R }; 236 d.caps[2] = (RxCapNeed){ w_slot[2], rx_aegis_res(0, RX_AEGIS_RES_SLOT0 + 2), R }; 237 d.caps[3] = (RxCapNeed){ mint(e, SUBJ_WORKER, RES_LOG, RW), RES_LOG, RW }; 238 rx_aegis_use_slot(&d, 1, slot(e, 2)); 239 if (rx_world_add_reaction(&e->w, &d, &e->r_lease) != RX_OK) return -1; 240 241 memset(&d, 0, sizeof d); 242 d.name = "worker.ask"; 243 d.faculty = RX_FACULTY_EXTERNAL; 244 d.subject = SUBJ_WORKER; 245 d.priority = RX_PRIO_INTERACTIVE; 246 d.fn = fn_ask; 247 d.user = e; 248 d.stamp_proposed = true; 249 d.n_triggers = 1; 250 d.triggers[0] = (RxDep){ e->intent, RX_ALL_FIELDS }; 251 d.n_reads = 1; 252 d.reads[0] = (RxDep){ e->a.o[0].request, RX_ALL_FIELDS }; 253 d.n_writes = 1; 254 d.writes[0] = (RxDep){ e->a.o[0].request, RX_ALL_FIELDS }; 255 d.n_caps = 2; 256 d.caps[0] = (RxCapNeed){ w_intent, RES_INTENT, R }; 257 d.caps[1] = (RxCapNeed){ w_request, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), RW }; 258 if (rx_world_add_reaction(&e->w, &d, &e->r_ask) != RX_OK) return -1; 259 return 0; 260 } 261 262 static void env_stop(Env *e) { 263 rx_world_wait_quiescent(&e->w, 20000); 264 rx_world_destroy(&e->w); 265 rx_aegis_destroy(&e->a); 266 aienos_cap_stop(e->admin, e->view); 267 } 268 269 static int64_t input(Env *e) { 270 RxMutation m = { e->input, 0, ++e->inputs }; 271 int64_t id = rx_world_publish_external(&e->w, e->ext_input, &m, 1); 272 CHECK(id > 0, "input refused"); 273 CHECK(settle(e) == RX_OK, "settle"); 274 return id; 275 } 276 277 static uint64_t intent(Env *e, uint64_t res, uint64_t rights, uint64_t lease, uint64_t sl, 278 uint64_t op) { 279 /* Past anything the request has carried, including a forged one. */ 280 uint64_t s = fld(e, e->intent, 0), r = fld(e, e->a.o[0].request, 0); 281 s = (s > r ? s : r) + 1; 282 RxMutation m[6] = { { e->intent, 0, s }, { e->intent, 1, res }, { e->intent, 2, rights }, 283 { e->intent, 3, lease }, { e->intent, 4, sl }, { e->intent, 5, op } }; 284 CHECK(rx_world_publish_external(&e->w, e->ext_intent, m, 6) > 0, "intent refused"); 285 CHECK(settle(e) == RX_OK, "settle"); 286 return s; 287 } 288 289 static void approve(Env *e, uint64_t seq, uint64_t verdict) { 290 RxCapRef c = mint(e, SUBJ_EXTERNAL, rx_aegis_res(0, RX_AEGIS_RES_APPROVAL), RX_RIGHT_WRITE); 291 RxMutation m[2] = { { e->a.o[0].approval, 0, seq }, { e->a.o[0].approval, 1, verdict } }; 292 CHECK(rx_world_publish_external(&e->w, c, m, 2) > 0, "approval refused"); 293 CHECK(settle(e) == RX_OK, "settle"); 294 } 295 296 static uint64_t acts(Env *e, uint32_t rid) { return e->w.reactions[rid].activations; } 297 298 static int caused_by(Env *e, uint64_t from, uint64_t ancestor) { 299 uint64_t n = e->w.n_crumbs; 300 uint8_t *seen = calloc(n + 1, 1); 301 uint64_t *stack = malloc((n + 1) * sizeof(uint64_t)); 302 uint64_t sp = 0; 303 int found = 0; 304 stack[sp++] = from; 305 while (sp && !found) { 306 uint64_t id = stack[--sp]; 307 if (id == 0 || id > n || seen[id]) continue; 308 seen[id] = 1; 309 if (id == ancestor) found = 1; 310 const RxCrumb *c = rx_world_crumb(&e->w, id); 311 if (!c) continue; 312 for (uint32_t i = 0; i < c->n_parents && sp < n; i++) stack[sp++] = c->parents[i]; 313 if (c->wake_cause && sp < n) stack[sp++] = c->wake_cause; 314 } 315 free(seen); 316 free(stack); 317 return found; 318 } 319 320 /* Receipt data. */ 321 static uint64_t g_fast_n, g_fast_decide, g_fast_install, g_slow_ns, g_check_ns, g_eval_ns; 322 static uint64_t g_spawn_ns, g_mints, g_revokes; 323 324 /* ---- tests ---- */ 325 326 static void t_slow_then_fast(void) { 327 printf("[*] no authority: blocked, AEGIS untouched; the worker asks; then the fast path\n"); 328 Env e; 329 CHECK(env_start(&e) == 0, "setup"); 330 RxAegisFaculty *a = &e.a; 331 332 input(&e); 333 CHECK(e.w.reactions[e.r_tick].commits == 0 && fld(&e, e.work, 0) == 0, "ran without authority"); 334 CHECK(e.w.stats.blocked_authority > 0, "no block recorded"); 335 CHECK(acts(&e, a->r_decide[0]) == 0 && acts(&e, a->r_install[0]) == 0, 336 "a block by itself woke AEGIS or the root"); 337 338 /* The worker is told what it will need. It asks; nobody waits. */ 339 uint64_t t0 = now_ns(); 340 uint64_t seq = intent(&e, RES_WORK, RX_RIGHT_READ | RX_RIGHT_WRITE, 0, 0, RX_AEGIS_OP_ACQUIRE); 341 g_slow_ns = now_ns() - t0; 342 CHECK(fld(&e, a->o[0].request, 0) == seq, "request not published"); 343 CHECK(fld(&e, a->o[0].decision, 0) == seq && fld(&e, a->o[0].decision, 1) == RX_AEGIS_GRANT && 344 fld(&e, a->o[0].decision, 6) == 1, "decision %llu rule %llu", 345 (unsigned long long)fld(&e, a->o[0].decision, 1), (unsigned long long)fld(&e, a->o[0].decision, 6)); 346 CHECK(fld(&e, slot(&e, 0), 2) == RX_AEGIS_SLOT_LIVE && fld(&e, slot(&e, 0), 5) == seq && 347 fld(&e, slot(&e, 0), 3) == RES_WORK, "slot 0 state %llu why %llu", 348 (unsigned long long)fld(&e, slot(&e, 0), 2), (unsigned long long)fld(&e, slot(&e, 0), 7)); 349 /* The pending input ran when the slot filled. */ 350 CHECK(fld(&e, e.work, 0) == 1 && fld(&e, e.work, 1) == 1, "blocked input did not run after the grant"); 351 CHECK(a->mints == 1, "mints %llu", (unsigned long long)a->mints); 352 353 /* The minted reference is the worker's, from AEGIS, bounded as decided. */ 354 RxCapEntry ce; 355 RxCapRef ref = { (uint32_t)fld(&e, slot(&e, 0), 0), fld(&e, slot(&e, 0), 1) }; 356 CHECK(rx_world_inspect_cap(&e.w, ref, &ce) == RX_CAP_OK && ce.subject == SUBJ_WORKER && 357 ce.resource == RES_WORK && ce.rights == (RX_RIGHT_READ | RX_RIGHT_WRITE) && 358 ce.issuer == RX_AEGIS_SUBJ, "minted entry"); 359 360 /* Causal chain: work <- slot <- decision <- request <- intent. */ 361 uint64_t c_work = rx_world_explain(&e.w, e.work, 0); 362 uint64_t c_slot = rx_world_explain(&e.w, slot(&e, 0), 0); 363 uint64_t c_dec = rx_world_explain(&e.w, a->o[0].decision, 1); 364 uint64_t c_req = rx_world_explain(&e.w, a->o[0].request, 0); 365 uint64_t c_int = rx_world_explain(&e.w, e.intent, 0); 366 CHECK(caused_by(&e, c_work, c_slot) && caused_by(&e, c_slot, c_dec) && 367 caused_by(&e, c_dec, c_req) && caused_by(&e, c_req, c_int), "authority chain broken"); 368 369 /* Fast path: many inputs, no AEGIS or root activation. */ 370 uint64_t d0 = acts(&e, a->r_decide[0]), i0 = acts(&e, a->r_install[0]); 371 uint64_t w0 = fld(&e, e.work, 0); 372 g_fast_n = 2000; 373 for (uint64_t i = 0; i < g_fast_n; i++) { 374 RxMutation m = { e.input, 0, ++e.inputs }; 375 rx_world_publish_external(&e.w, e.ext_input, &m, 1); 376 if ((i & 63) == 63) settle(&e); 377 } 378 CHECK(settle(&e) == RX_OK, "settle"); 379 CHECK(fld(&e, e.work, 1) == e.inputs && fld(&e, e.work, 0) > w0, "fast path lost the last input"); 380 g_fast_decide = acts(&e, a->r_decide[0]) - d0; 381 g_fast_install = acts(&e, a->r_install[0]) - i0; 382 CHECK(g_fast_decide == 0 && g_fast_install == 0, "fast path woke AEGIS %llu / root %llu times", 383 (unsigned long long)g_fast_decide, (unsigned long long)g_fast_install); 384 385 /* Cost of one fast-path authority check against one synchronous policy decision. */ 386 const int N = 200000; 387 uint64_t t = now_ns(); 388 int bad = 0; 389 for (int i = 0; i < N; i++) 390 bad |= rx_world_validate_cap(&e.w, ref, SUBJ_WORKER, RES_WORK, RX_RIGHT_WRITE, NULL); 391 g_check_ns = (now_ns() - t) / N; 392 CHECK(bad == 0, "fast check failed"); 393 uint64_t l; 394 uint32_t why, na; 395 volatile uint32_t sink = 0; 396 t = now_ns(); 397 for (int i = 0; i < N; i++) 398 sink += rx_aegis_evaluate(&a->policy, SUBJ_WORKER, RES_WORK, RX_RIGHT_WRITE, 0, &l, &why, &na); 399 g_eval_ns = (now_ns() - t) / N; 400 (void)sink; 401 402 /* Release: AEGIS revokes through the root; the worker stops; the world does not. */ 403 intent(&e, RES_WORK, 0, 0, 0, RX_AEGIS_OP_RELEASE); 404 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_REVOKE, "release decision"); 405 CHECK(fld(&e, slot(&e, 0), 2) == RX_AEGIS_SLOT_REVOKED, "slot after release"); 406 CHECK(rx_world_validate_cap(&e.w, ref, SUBJ_WORKER, RES_WORK, RX_RIGHT_WRITE, NULL) != RX_CAP_OK, 407 "released reference still valid"); 408 uint64_t before = fld(&e, e.work, 0); 409 input(&e); 410 CHECK(fld(&e, e.work, 0) == before, "worked after release"); 411 CHECK(fld(&e, e.input, 0) == e.inputs, "the world stopped with the worker"); 412 g_mints = a->mints; 413 g_revokes = a->revokes; 414 env_stop(&e); 415 } 416 417 static void t_escalate_deny(void) { 418 printf("[*] a human approves or rejects; privileged and unruled requests are denied\n"); 419 Env e; 420 CHECK(env_start(&e) == 0, "setup"); 421 RxAegisFaculty *a = &e.a; 422 const uint64_t RWE = RX_RIGHT_READ | RX_RIGHT_WRITE | RX_RIGHT_EFFECT; 423 424 uint64_t s1 = intent(&e, RES_EFFECT, RWE, 0, 1, RX_AEGIS_OP_ACQUIRE); 425 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_ESCALATE, "not escalated"); 426 CHECK(fld(&e, slot(&e, 1), 2) == RX_AEGIS_SLOT_EMPTY && a->mints == 0, "minted before approval"); 427 approve(&e, s1, RX_AEGIS_APPROVE); 428 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_GRANT && fld(&e, slot(&e, 1), 2) == RX_AEGIS_SLOT_LIVE, 429 "approved request not installed"); 430 431 uint64_t s2 = intent(&e, RES_EFFECT + 1, RWE, 0, 3, RX_AEGIS_OP_ACQUIRE); 432 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_ESCALATE, "second not escalated"); 433 approve(&e, s2, RX_AEGIS_REJECT); 434 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_DENY && fld(&e, a->o[0].decision, 6) == RX_AEGIS_WHY_HUMAN, 435 "rejection"); 436 CHECK(fld(&e, slot(&e, 3), 2) == RX_AEGIS_SLOT_EMPTY, "rejected request installed"); 437 438 intent(&e, RES_WORK, RX_RIGHT_READ | RX_RIGHT_MINT, 0, 3, RX_AEGIS_OP_ACQUIRE); 439 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_DENY && 440 fld(&e, a->o[0].decision, 6) == RX_AEGIS_WHY_PRIVILEGED, "privileged request"); 441 intent(&e, RES_NORULE, RX_RIGHT_READ, 0, 3, RX_AEGIS_OP_ACQUIRE); 442 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_DENY && 443 fld(&e, a->o[0].decision, 6) == RX_AEGIS_WHY_NO_RULE, "unruled request"); 444 intent(&e, RES_WORK, RWE, 0, 3, RX_AEGIS_OP_ACQUIRE); 445 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_DENY, "rights beyond the rule"); 446 intent(&e, RES_WORK, RX_RIGHT_READ, 0, 9, RX_AEGIS_OP_ACQUIRE); 447 CHECK(fld(&e, a->o[0].decision, 6) == RX_AEGIS_WHY_BAD_REQUEST, "slot out of range"); 448 CHECK(fld(&e, slot(&e, 3), 2) == RX_AEGIS_SLOT_EMPTY && a->mints == 1, "a denial minted"); 449 env_stop(&e); 450 } 451 452 static void t_lease(void) { 453 printf("[*] a leased grant expires on the AIENOS clock; renewal replaces it\n"); 454 Env e; 455 CHECK(env_start(&e) == 0, "setup"); 456 RxAegisFaculty *a = &e.a; 457 intent(&e, RES_LEASED, RX_RIGHT_READ, 5000, 2, RX_AEGIS_OP_ACQUIRE); 458 CHECK(fld(&e, a->o[0].decision, 4) == 100, "policy did not cap the lease: %llu", 459 (unsigned long long)fld(&e, a->o[0].decision, 4)); 460 CHECK(fld(&e, slot(&e, 2), 2) == RX_AEGIS_SLOT_LIVE && fld(&e, slot(&e, 2), 6) != 0, "leased slot"); 461 RxCapRef old = { (uint32_t)fld(&e, slot(&e, 2), 0), fld(&e, slot(&e, 2), 1) }; 462 input(&e); 463 CHECK(fld(&e, e.lease_log, 0) == e.inputs, "leased reader did not run inside its lease"); 464 465 AienosCapRef office; 466 aienos_cap_office(e.admin, &office); 467 CHECK(rx_world_validate_cap(&e.w, old, SUBJ_WORKER, RES_LEASED, RX_RIGHT_READ, NULL) == RX_CAP_OK, 468 "lease valid before expiry"); 469 CHECK(aienos_cap_advance_clock(e.admin, office, 101) == 0, "clock"); 470 CHECK(rx_world_validate_cap(&e.w, old, SUBJ_WORKER, RES_LEASED, RX_RIGHT_READ, NULL) == 471 RX_CAP_ERR_EXPIRED, "lease did not expire"); 472 uint64_t seen = fld(&e, e.lease_log, 0); 473 input(&e); 474 CHECK(fld(&e, e.lease_log, 0) == seen, "leased reader ran after expiry"); 475 intent(&e, RES_LEASED, RX_RIGHT_READ, 0, 2, RX_AEGIS_OP_ACQUIRE); 476 input(&e); 477 CHECK(fld(&e, e.lease_log, 0) == e.inputs, "leased reader did not resume after renewal"); 478 RxCapRef neu = { (uint32_t)fld(&e, slot(&e, 2), 0), fld(&e, slot(&e, 2), 1) }; 479 CHECK(neu.cap_id != old.cap_id || neu.generation != old.generation, "renewal reused the old grant"); 480 CHECK(rx_world_validate_cap(&e.w, neu, SUBJ_WORKER, RES_LEASED, RX_RIGHT_READ, NULL) == RX_CAP_OK, 481 "renewed grant invalid"); 482 CHECK(a->mints == 2 && a->revokes == 1, "mints %llu revokes %llu", (unsigned long long)a->mints, 483 (unsigned long long)a->revokes); 484 env_stop(&e); 485 } 486 487 /* rogue.forge: a non-AEGIS reaction that was wrongly given WRITE on the decision. */ 488 static struct { RxObjRef decision, request; uint64_t seq; } g_forge; 489 static int fn_forge(RxCtx *c) { 490 const RxSnapshotDep *rq = NULL; 491 for (uint32_t i = 0; i < c->n_in; i++) 492 if (c->in[i].obj.id == g_forge.request.id) rq = &c->in[i]; 493 if (!rq || rq->field[0] == 0) return 0; 494 uint64_t v[8] = { rq->field[0], RX_AEGIS_GRANT, rq->field[1], rq->field[2], 0, rq->field[4], 1, 495 rq->field[5] }; 496 for (uint32_t i = 0; i < 8; i++) c->out[c->n_out++] = (RxMutation){ g_forge.decision, i, v[i] }; 497 return 0; 498 } 499 500 static void t_attacks(void) { 501 printf("[*] forged decisions, requests written for the worker, slot stuffing, and a policy " 502 "mistake are refused by the root\n"); 503 Env e; 504 CHECK(env_start(&e) == 0, "setup"); 505 RxAegisFaculty *a = &e.a; 506 const uint32_t R = RX_RIGHT_READ, RW = RX_RIGHT_READ | RX_RIGHT_WRITE; 507 508 /* 1. Policy mistake: rule 4 grants outside the worker's domain. */ 509 intent(&e, RES_OUTSIDE, RW, 0, 3, RX_AEGIS_OP_ACQUIRE); 510 CHECK(fld(&e, a->o[0].decision, 1) == RX_AEGIS_GRANT, "rule 4 should grant (the mistake)"); 511 CHECK(fld(&e, slot(&e, 3), 2) == RX_AEGIS_SLOT_EMPTY && fld(&e, slot(&e, 3), 7) == RX_AEGIS_WHY_DOMAIN, 512 "root installed outside the domain: state %llu why %llu", 513 (unsigned long long)fld(&e, slot(&e, 3), 2), (unsigned long long)fld(&e, slot(&e, 3), 7)); 514 CHECK(a->mints == 0, "minted outside the domain"); 515 516 /* 2. A request written for the worker by someone else (an outside writer 517 * that was wrongly given WRITE on the worker's request). AEGIS judges the 518 * worker's policy and grants; the root sees who wrote it. */ 519 RxCapRef ext_req = mint(&e, SUBJ_EXTERNAL, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), RX_RIGHT_WRITE); 520 uint64_t s = fld(&e, a->o[0].request, 0) + 1; 521 RxMutation m[6] = { { a->o[0].request, 0, s }, { a->o[0].request, 1, RES_WORK }, 522 { a->o[0].request, 2, RW }, { a->o[0].request, 3, 0 }, 523 { a->o[0].request, 4, 2 }, { a->o[0].request, 5, RX_AEGIS_OP_ACQUIRE } }; 524 CHECK(rx_world_publish_external(&e.w, ext_req, m, 6) > 0, "outside request"); 525 CHECK(settle(&e) == RX_OK, "settle"); 526 CHECK(fld(&e, a->o[0].decision, 0) == s && fld(&e, a->o[0].decision, 1) == RX_AEGIS_GRANT, 527 "AEGIS decided the forged request"); 528 CHECK(fld(&e, slot(&e, 2), 2) == RX_AEGIS_SLOT_EMPTY && 529 fld(&e, slot(&e, 2), 7) == RX_AEGIS_WHY_REQUEST_ORIGIN, "root installed a forged request: why %llu", 530 (unsigned long long)fld(&e, slot(&e, 2), 7)); 531 CHECK(a->mints == 0, "minted for a forged request"); 532 533 /* 3. A forged decision from a non-AEGIS reaction, and from outside. */ 534 g_forge.decision = a->o[0].decision; 535 g_forge.request = a->o[0].request; 536 RxReactionDesc d; 537 memset(&d, 0, sizeof d); 538 d.name = "rogue.forge"; 539 d.faculty = RX_FACULTY_EXTERNAL; 540 d.subject = SUBJ_ROGUE; 541 d.priority = RX_PRIO_FOREGROUND; 542 d.fn = fn_forge; 543 d.n_triggers = 1; 544 d.triggers[0] = (RxDep){ a->o[0].request, RX_ALL_FIELDS }; 545 d.n_writes = 1; 546 d.writes[0] = (RxDep){ a->o[0].decision, RX_ALL_FIELDS }; 547 d.n_caps = 2; 548 d.caps[0] = (RxCapNeed){ mint(&e, SUBJ_ROGUE, rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R), 549 rx_aegis_res(0, RX_AEGIS_RES_REQUEST), R }; 550 RxCapRef rogue_dec = mint(&e, SUBJ_ROGUE, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RW); 551 d.caps[1] = (RxCapNeed){ rogue_dec, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RW }; 552 uint32_t rid; 553 CHECK(rx_world_add_reaction(&e.w, &d, &rid) == RX_OK, "rogue register"); 554 /* The worker asks for something AEGIS denies; the rogue overwrites it with a grant. */ 555 intent(&e, RES_NORULE, R, 0, 1, RX_AEGIS_OP_ACQUIRE); 556 CHECK(e.w.reactions[rid].commits > 0, "rogue never wrote (activations %llu, state %s)", 557 (unsigned long long)e.w.reactions[rid].activations, rx_state_name(e.w.reactions[rid].state)); 558 for (uint64_t id = 1; id <= e.w.n_crumbs && e.w.reactions[rid].commits == 0; id++) { 559 const RxCrumb *c = rx_world_crumb(&e.w, id); 560 if (c && c->reaction == rid) 561 fprintf(stderr, " rogue crumb %s reason %d\n", rx_crumb_kind_name(c->kind), c->reason); 562 } 563 CHECK(fld(&e, slot(&e, 1), 2) == RX_AEGIS_SLOT_EMPTY, "root installed a forged decision"); 564 CHECK(fld(&e, slot(&e, 1), 7) == RX_AEGIS_WHY_DECISION_ORIGIN || fld(&e, slot(&e, 1), 5) == 0, 565 "refusal reason %llu", (unsigned long long)fld(&e, slot(&e, 1), 7)); 566 CHECK(a->mints == 0, "minted for a forged decision"); 567 568 RxCapRef ext_dec = mint(&e, SUBJ_EXTERNAL, rx_aegis_res(0, RX_AEGIS_RES_DECISION), RX_RIGHT_WRITE); 569 uint64_t rs = fld(&e, a->o[0].request, 0); 570 RxMutation fd[8] = { { a->o[0].decision, 0, rs }, { a->o[0].decision, 1, RX_AEGIS_GRANT }, 571 { a->o[0].decision, 2, RES_NORULE }, { a->o[0].decision, 3, R }, 572 { a->o[0].decision, 4, 0 }, { a->o[0].decision, 5, 0 }, 573 { a->o[0].decision, 6, 1 }, { a->o[0].decision, 7, RX_AEGIS_OP_ACQUIRE } }; 574 CHECK(rx_world_publish_external(&e.w, ext_dec, fd, 8) > 0, "outside decision"); 575 CHECK(settle(&e) == RX_OK, "settle"); 576 CHECK(fld(&e, slot(&e, 0), 2) == RX_AEGIS_SLOT_EMPTY && a->mints == 0, 577 "root installed an outside decision"); 578 579 /* 4. Slot stuffing: a hostile writer puts another subject's valid reference 580 * in the worker's slot. The authority still says no. */ 581 RxCapRef other = mint(&e, SUBJ_OTHER, RES_WORK, RW); 582 RxCapRef ext_slot = mint(&e, SUBJ_EXTERNAL, rx_aegis_res(0, RX_AEGIS_RES_SLOT0), RX_RIGHT_WRITE); 583 RxMutation st[3] = { { slot(&e, 0), 0, other.cap_id }, { slot(&e, 0), 1, other.generation }, 584 { slot(&e, 0), 2, RX_AEGIS_SLOT_LIVE } }; 585 CHECK(rx_world_publish_external(&e.w, ext_slot, st, 3) > 0, "stuffed slot"); 586 CHECK(settle(&e) == RX_OK, "settle"); 587 uint64_t w0 = fld(&e, e.work, 0); 588 input(&e); 589 CHECK(fld(&e, e.work, 0) == w0, "ran on another subject's reference"); 590 CHECK(e.w.reactions[e.r_tick].state != RX_RUNNING, "state"); 591 592 /* A principal wrongly holding WRITE on the decision can jam it (it cannot 593 * get anything minted). The remedy is to revoke that grant. Then the 594 * honest path works again. */ 595 AienosCapRef office; 596 aienos_cap_office(e.admin, &office); 597 CHECK(aienos_cap_revoke(e.admin, office, (AienosCapRef){ rogue_dec.cap_id, rogue_dec.generation }) == 0, 598 "revoke rogue"); 599 CHECK(a->mints == 0, "the rogue got something minted"); 600 intent(&e, RES_WORK, RW, 0, 0, RX_AEGIS_OP_ACQUIRE); 601 CHECK(fld(&e, slot(&e, 0), 2) == RX_AEGIS_SLOT_LIVE && a->mints == 1, "honest grant after attacks"); 602 CHECK(fld(&e, e.work, 0) == w0 + 1, "worker did not resume"); 603 CHECK(rx_world_validate_cap(&e.w, other, SUBJ_OTHER, RES_WORK, RX_RIGHT_WRITE, NULL) == RX_CAP_OK, 604 "the root revoked a reference it never minted because it sat in a slot"); 605 CHECK(a->revokes == 0, "revokes %llu", (unsigned long long)a->revokes); 606 607 /* No AEGIS or root activation happened without a waking cause. */ 608 uint64_t uncaused = 0, n = 0; 609 for (uint64_t id = 1; id <= e.w.n_crumbs; id++) { 610 const RxCrumb *c = rx_world_crumb(&e.w, id); 611 if (!c || c->reaction == UINT32_MAX) continue; 612 if (c->faculty != RX_FACULTY_AEGIS && c->faculty != RX_FACULTY_ROOT) continue; 613 n++; 614 if (!c->wake_cause) uncaused++; 615 } 616 CHECK(n > 0 && uncaused == 0, "authority crumbs %llu uncaused %llu", (unsigned long long)n, 617 (unsigned long long)uncaused); 618 uint64_t checked = 0; 619 CHECK(rx_world_verify_crumbs(&e.w, &checked) == 0 && checked > 0, "crumb chain"); 620 env_stop(&e); 621 } 622 623 /* The floor of any shell-out policy path (the current spark-aegis route): 624 * spawn and reap one process that does nothing. */ 625 static void t_spawn_floor(void) { 626 char *argv[] = { "/bin/true", NULL }; 627 const int N = 50; 628 uint64_t t = now_ns(); 629 int ok = 0; 630 for (int i = 0; i < N; i++) { 631 pid_t pid; 632 if (posix_spawn(&pid, "/bin/true", NULL, NULL, argv, environ) != 0) continue; 633 int st; 634 if (waitpid(pid, &st, 0) == pid && WIFEXITED(st) && WEXITSTATUS(st) == 0) ok++; 635 } 636 g_spawn_ns = ok ? (now_ns() - t) / (uint64_t)ok : 0; 637 CHECK(ok == N, "spawn floor %d/%d", ok, N); 638 } 639 640 /* ---- receipt ---- */ 641 642 static void binary_digest(char out[65]) { 643 strcpy(out, "unavailable"); 644 FILE *fp = fopen("/proc/self/exe", "rb"); 645 if (!fp) return; 646 sha256_ctx c; 647 sha256_init(&c); 648 uint8_t buf[65536]; 649 size_t n; 650 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); 651 fclose(fp); 652 uint8_t d[32]; 653 sha256_final(&c, d); 654 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]); 655 } 656 657 static void write_receipt(void) { 658 char path[512]; 659 if (omega_evidence_path("R8/rx_aegis_resident_receipt.json", path, sizeof path) != 0) return; 660 FILE *fp = fopen(path, "w"); 661 if (!fp) return; 662 char commit[41]; 663 memset(commit, 0, sizeof commit); 664 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); 665 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); 666 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && 667 !omega_evidence_tree_dirty(); 668 const char *aienos = getenv("AIENOS_COMMIT"); 669 char digest[65]; 670 binary_digest(digest); 671 struct utsname u; 672 memset(&u, 0, sizeof u); 673 uname(&u); 674 fprintf(fp, 675 "{\n" 676 " \"schema\": \"AIEN_RX_R8_AEGIS_RESIDENT_V1\",\n" 677 " \"run_id\": \"%s\",\n" 678 " \"candidate_commit\": %s%s%s,\n" 679 " \"candidate_bound\": %s,\n" 680 " \"run_commit\": \"%s\",\n" 681 " \"tree_dirty\": %s,\n" 682 " \"aienos_commit\": %s%s%s,\n" 683 " \"checks\": %d,\n" 684 " \"failures\": %d,\n" 685 " \"test_binary_sha256\": \"%s\",\n" 686 " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\"},\n" 687 " \"scope\": \"host processor; the native AIENOS authority library is the root\",\n" 688 " \"fast_path\": {\"actions\": %llu, \"aegis_activations\": %llu, \"root_activations\": %llu,\n" 689 " \"synchronous_policy_round_trips_saved\": %llu},\n" 690 " \"cost_ns\": {\"fast_path_authority_check\": %llu, \"in_process_policy_evaluation\": %llu,\n" 691 " \"process_spawn_floor_for_shell_out\": %llu,\n" 692 " \"slow_path_intent_to_work\": %llu},\n" 693 " \"root\": {\"mints\": %llu, \"revokes\": %llu},\n" 694 " \"gates\": {\n" 695 " \"R8_CONSTITUTIONAL_AEGIS\": \"%s\",\n" 696 " \"not_claimed\": [\"AIENOS kernel (the authority runs as a host library)\", " 697 "\"policy learned or changed at run time\", \"R13\"]\n" 698 " }\n" 699 "}\n", 700 omega_evidence_run_id(), candidate ? "\"" : "", candidate ? candidate : "null", 701 candidate ? "\"" : "", bound ? "true" : "false", commit, 702 omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", 703 aienos ? aienos : "null", aienos ? "\"" : "", g_checks, g_fail, digest, u.sysname, 704 u.release, u.machine, (unsigned long long)g_fast_n, (unsigned long long)g_fast_decide, 705 (unsigned long long)g_fast_install, (unsigned long long)g_fast_n, 706 (unsigned long long)g_check_ns, (unsigned long long)g_eval_ns, 707 (unsigned long long)g_spawn_ns, (unsigned long long)g_slow_ns, 708 (unsigned long long)g_mints, (unsigned long long)g_revokes, g_fail ? "FAIL" : "PASS"); 709 fclose(fp); 710 printf("receipt: %s\n", path); 711 } 712 713 int main(void) { 714 signal(SIGPIPE, SIG_IGN); 715 t_slow_then_fast(); 716 t_escalate_deny(); 717 t_lease(); 718 t_attacks(); 719 t_spawn_floor(); 720 printf(" fast check %llu ns, policy evaluation %llu ns, process spawn %llu ns, " 721 "slow path %llu ns\n", (unsigned long long)g_check_ns, (unsigned long long)g_eval_ns, 722 (unsigned long long)g_spawn_ns, (unsigned long long)g_slow_ns); 723 printf("checks %d failures %d\n", g_checks, g_fail); 724 write_receipt(); 725 return g_fail ? 1 : 0; 726 }