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/aienux/blob/main/native/capability/tests/contain_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / capability / tests / contain_test.c` 875 lines ยท 43209 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 * contain_test.c -- AEGIS containment gate (ARGUS-1 lane G): positive path, 19 * hostile negatives for invariants I1-I5, receipts, determinism. 20 * 21 * The test plays the bridge: it sets the authority observer and forwards 22 * every call to aienos_contain_lineage_observe, and it records receipts from 23 * the gate sink. argus_abi.h is included only to prove the layout twin and 24 * the mirrored constants; nothing from libargus is linked. 25 */ 26 #include "../aienos_capability.h" 27 #include "../aienos_contain.h" 28 #include "../../argus/argus_abi.h" 29 30 #include 31 #include 32 #include 33 #include 34 35 /* ---- Layout twin and mirrors (compile time) ---- */ 36 #define SAME_OFF(f) _Static_assert(offsetof(AienosContainRequest, f) == offsetof(ArgusContainmentRequest, f), "twin offset " #f) 37 SAME_OFF(incident_id); SAME_OFF(containment); SAME_OFF(severity); SAME_OFF(finding_code); 38 SAME_OFF(principal); SAME_OFF(target); SAME_OFF(machine_id); SAME_OFF(finding_digest); 39 SAME_OFF(request_id); SAME_OFF(finding_sequence); SAME_OFF(target_object); 40 SAME_OFF(target_rights); SAME_OFF(target_digest); SAME_OFF(flags); SAME_OFF(version); 41 SAME_OFF(reserved); 42 _Static_assert(sizeof(AienosContainRequest) == sizeof(ArgusContainmentRequest), "twin size"); 43 _Static_assert(offsetof(AienosCapRef, generation) == offsetof(ArgusCapRef, generation), "capref"); 44 _Static_assert(sizeof(AienosCapRef) == sizeof(ArgusCapRef), "capref size"); 45 _Static_assert(AIENOS_CONTAIN_REQUEST_SIZE == ARGUS_CONTAIN_REQUEST_SIZE, "size"); 46 _Static_assert(AIENOS_CONTAIN_REQUEST_VERSION == ARGUS_CONTAIN_REQUEST_VERSION, "version"); 47 _Static_assert(AIENOS_CREQ_SYNTHETIC == ARGUS_CREQ_SYNTHETIC && AIENOS_CREQ_SATURATION == ARGUS_CREQ_SATURATION && 48 AIENOS_CREQ_KNOWN == ARGUS_CREQ_KNOWN, "flags"); 49 _Static_assert((int)AIENOS_CONTAIN_GRANT == (int)ARGUS_CSTATUS_GRANT && (int)AIENOS_CONTAIN_DENY == (int)ARGUS_CSTATUS_DENY && 50 (int)AIENOS_CONTAIN_ESCALATE == (int)ARGUS_CSTATUS_ESCALATE && (int)AIENOS_CONTAIN_DONE == (int)ARGUS_CSTATUS_DONE && 51 (int)AIENOS_CONTAIN_PARTIAL == (int)ARGUS_CSTATUS_PARTIAL && (int)AIENOS_CONTAIN_FAILED == (int)ARGUS_CSTATUS_FAILED && 52 (int)AIENOS_CONTAIN_UNAVAILABLE == (int)ARGUS_CSTATUS_UNAVAILABLE && 53 (int)AIENOS_CONTAIN_FAILED_SCOPE == (int)ARGUS_CSTATUS_FAILED_SCOPE, "status"); 54 _Static_assert((int)AIENOS_CONTAIN_KIND_DECIDED == (int)ARGUS_EV_CONTAINMENT_DECIDED && 55 (int)AIENOS_CONTAIN_KIND_EXECUTED == (int)ARGUS_EV_CONTAINMENT_EXECUTED, "kinds"); 56 _Static_assert((int)AIENOS_CONTAIN_REVOKE_CAPABILITY == (int)ARGUS_CONTAIN_REVOKE_CAPABILITY && 57 (int)AIENOS_CONTAIN_FREEZE_PRINCIPAL == (int)ARGUS_CONTAIN_FREEZE_PRINCIPAL && /* stand-in */ 58 (int)AIENOS_CONTAIN_RESTRICT_PRINCIPAL == (int)ARGUS_CONTAIN_RESTRICT_PRINCIPAL && 59 (int)AIENOS_CONTAIN_TYPE_MAX == (int)ARGUS_CONTAIN_MAX, "types"); 60 _Static_assert(ARGUS_CAP_RIGHT_REVOKE == AIENOS_CAP_RIGHT_REVOKE && 61 ARGUS_CAP_RIGHT_PRIVILEGED == AIENOS_CAP_RIGHT_PRIVILEGED, "rights mirrors"); 62 _Static_assert(ARGUS_SUBJ_AEGIS == AIENOS_CONTAIN_SUBJ_AEGIS && ARGUS_SUBJ_AEGIS_ROOT == AIENOS_CONTAIN_SUBJ_AEGIS_ROOT && 63 AIENOS_CONTAIN_SUBJ != ARGUS_SUBJ_AEGIS && AIENOS_CONTAIN_SUBJ != ARGUS_SUBJ_AEGIS_ROOT, "subjects"); 64 _Static_assert(AIENOS_CONTAIN_CAP_NONE == ARGUS_CAP_NONE, "cap none"); 65 66 /* ---- Check plumbing ---- */ 67 static int failures; 68 static int checks; 69 #define CHECK(c) \ 70 do { \ 71 checks++; \ 72 if (!(c)) { \ 73 failures++; \ 74 fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ 75 } \ 76 } while (0) 77 static int inv_fail_mark; 78 static void inv_begin(void) { inv_fail_mark = failures; } 79 static void inv_end(const char *name) { 80 printf("INV %s %s\n", name, failures == inv_fail_mark ? "PASS" : "FAIL"); 81 } 82 83 /* ---- A bridge stand-in: observer log + gate feed + receipt log ---- */ 84 #define LOG_MAX 4096 85 typedef struct { 86 uint32_t op; 87 int rc; 88 int has; 89 AienosCapEntry e; 90 } ObsRec; 91 92 typedef struct { 93 AienosCapAdmin *admin; 94 AienosCapView *view; 95 AienosCapRef office; 96 AienosContain *gate; /* set by aienos_contain_create before its mint */ 97 int feed; /* forward observer calls to the gate */ 98 uint32_t nobs; 99 ObsRec obs[LOG_MAX]; 100 uint32_t nrec; 101 AienosContainReceipt rec[LOG_MAX]; 102 AienosContainTablePolicy policy; 103 _Alignas(16) unsigned char mem[32 * 1024]; 104 } Rig; 105 106 static void rig_observer(void *ctx, uint32_t op, const AienosCapEntry *e, int rc) { 107 Rig *r = ctx; 108 if (r->nobs < LOG_MAX) { 109 ObsRec *o = &r->obs[r->nobs++]; 110 o->op = op; 111 o->rc = rc; 112 o->has = e != NULL; 113 if (e) o->e = *e; 114 } 115 if (r->feed && r->gate) aienos_contain_lineage_observe(r->gate, op, e, rc); 116 } 117 118 static void rig_sink(void *ctx, const AienosContainReceipt *rc) { 119 Rig *r = ctx; 120 if (r->nrec < LOG_MAX) r->rec[r->nrec++] = *rc; 121 } 122 123 static void policy_all(Rig *r, uint32_t v) { 124 for (uint32_t t = 0; t <= AIENOS_CONTAIN_TYPE_MAX; t++) r->policy.verdict[t] = v; 125 } 126 127 /* Starts an authority; optionally mints `pre` READ caps before the gate exists. */ 128 static Rig *rig_new(int feed, int create_gate) { 129 Rig *r = calloc(1, sizeof *r); 130 if (!r || aienos_cap_start(&r->admin, &r->view) != AIENOS_CAP_OK) abort(); 131 aienos_cap_office(r->admin, &r->office); 132 r->feed = feed; 133 aienos_cap_set_observer(r->admin, rig_observer, r); 134 policy_all(r, AIENOS_CONTAIN_GRANT); 135 if (create_gate) { 136 AienosContainAuthorizer az = {aienos_contain_table_decide, &r->policy}; 137 int rc = aienos_contain_create(&r->gate, r->mem, sizeof r->mem, r->admin, r->view, 138 r->office, &az); 139 if (rc != AIENOS_CONTAIN_OK) { 140 fprintf(stderr, "create failed %d\n", rc); 141 abort(); 142 } 143 aienos_contain_set_sink(r->gate, rig_sink, r); 144 } 145 return r; 146 } 147 148 static void rig_free(Rig *r) { 149 aienos_cap_set_observer(r->admin, NULL, NULL); 150 if (r->gate) aienos_contain_destroy(r->gate); 151 aienos_cap_stop(r->admin, r->view); 152 free(r); 153 } 154 155 static AienosCapRef mint_root(Rig *r, uint32_t subject, uint32_t rights) { 156 AienosCapMint m = {0, subject, 0x1000u + subject, rights, 0, {AIENOS_CAP_PARENT_NONE, 0}, r->office}; 157 AienosCapRef out = {AIENOS_CAP_PARENT_NONE, 0}; 158 int rc = aienos_cap_mint(r->admin, &m, &out); 159 if (rc != AIENOS_CAP_OK) fprintf(stderr, "mint_root rc %d\n", rc); 160 return out; 161 } 162 163 static AienosCapRef mint_child(Rig *r, AienosCapRef parent, uint32_t subject) { 164 AienosCapEntry pe; 165 aienos_cap_inspect(r->view, parent, &pe); 166 AienosCapMint m = {pe.subject, subject, pe.resource, AIENOS_CAP_RIGHT_READ, 0, parent, parent}; 167 AienosCapRef out = {AIENOS_CAP_PARENT_NONE, 0}; 168 int rc = aienos_cap_mint(r->admin, &m, &out); 169 if (rc != AIENOS_CAP_OK) fprintf(stderr, "mint_child rc %d\n", rc); 170 return out; 171 } 172 173 static uint64_t next_request_id = 1; 174 175 static AienosContainRequest req_make(uint8_t type, uint32_t principal, AienosCapRef target) { 176 AienosContainRequest q; 177 memset(&q, 0, sizeof q); 178 q.incident_id = 77; 179 q.containment = type; 180 q.severity = 5; 181 q.finding_code = 2; /* STALE_GENERATION */ 182 q.principal = principal; 183 q.target = target; 184 for (int i = 0; i < 32; i++) q.finding_digest[i] = (uint8_t)(0xA0 + i); 185 q.request_id = next_request_id++; 186 q.finding_sequence = 1000 + q.request_id; 187 q.version = AIENOS_CONTAIN_REQUEST_VERSION; 188 return q; 189 } 190 191 static AienosContainDecision submit(Rig *r, const AienosContainRequest *q) { 192 uint8_t d[32]; 193 aienos_contain_request_digest(q, d); 194 AienosContainDecision out; 195 memset(&out, 0, sizeof out); 196 CHECK(aienos_contain_submit(r->gate, q, d, &out) == AIENOS_CONTAIN_OK); 197 return out; 198 } 199 200 static uint32_t count_obs(const Rig *r, uint32_t from, uint32_t op, int with_entry) { 201 uint32_t n = 0; 202 for (uint32_t i = from; i < r->nobs; i++) 203 if (r->obs[i].op == op && (!with_entry || r->obs[i].has)) n++; 204 return n; 205 } 206 207 static uint32_t count_subject_mints(const Rig *r, uint32_t from, uint32_t subject) { 208 uint32_t n = 0; 209 for (uint32_t i = from; i < r->nobs; i++) 210 if (r->obs[i].op == AIENOS_CAP_OBS_MINT && r->obs[i].has && r->obs[i].e.subject == subject) n++; 211 return n; 212 } 213 214 static int executor_intact(Rig *r, AienosCapRef ex0) { 215 AienosCapRef ex; 216 aienos_contain_executor(r->gate, &ex); 217 AienosCapEntry e; 218 return ex.cap_id == ex0.cap_id && ex.generation == ex0.generation && 219 aienos_cap_inspect(r->view, ex, &e) == AIENOS_CAP_OK && e.rights == AIENOS_CAP_RIGHT_REVOKE && 220 e.subject == AIENOS_CONTAIN_SUBJ && e.parent_id == AIENOS_CAP_PARENT_NONE; 221 } 222 223 static int is_live(Rig *r, AienosCapRef c) { 224 AienosCapEntry e; 225 return aienos_cap_inspect(r->view, c, &e) == AIENOS_CAP_OK && e.state == AIENOS_CAP_STATE_LIVE; 226 } 227 228 /* Receipt log is complete and ordered: every returned receipt reached the sink, seq +1 each. */ 229 static int receipts_ordered(const Rig *r) { 230 for (uint32_t i = 0; i < r->nrec; i++) 231 if (r->rec[i].seq != (uint64_t)i + 1) return 0; 232 return 1; 233 } 234 235 /* ---- SHA-256 and encoding ---- */ 236 static int hex_eq(const uint8_t d[32], const char *hex) { 237 char buf[65]; 238 for (int i = 0; i < 32; i++) snprintf(buf + 2 * i, 3, "%02x", d[i]); 239 return strcmp(buf, hex) == 0; 240 } 241 242 static void test_sha_and_encoding(void) { 243 uint8_t d[32]; 244 aienos_contain_sha256((const uint8_t *)"", 0, d); 245 CHECK(hex_eq(d, "`e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`")); 246 aienos_contain_sha256((const uint8_t *)"abc", 3, d); 247 CHECK(hex_eq(d, "`ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad`")); 248 const char *m2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 249 aienos_contain_sha256((const uint8_t *)m2, strlen(m2), d); 250 CHECK(hex_eq(d, "`248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1`")); 251 252 /* Wire offsets (spec 2): each field lands where the spec says. */ 253 AienosContainRequest q; 254 memset(&q, 0, sizeof q); 255 q.incident_id = 0x0102030405060708ull; 256 q.containment = 1; 257 q.severity = 4; 258 q.finding_code = 0x0002; 259 q.principal = 0x21222324; 260 q.target.cap_id = 0x31; 261 q.target.generation = 0x4142434445464748ull; 262 for (int i = 0; i < 32; i++) { 263 q.machine_id[i] = (uint8_t)(0xA0 + i); 264 q.finding_digest[i] = (uint8_t)(0xC0 + i); 265 q.target_digest[i] = (uint8_t)(0xE0 - i); 266 } 267 q.request_id = 0x5152535455565758ull; 268 q.finding_sequence = 0x6162636465666768ull; 269 q.target_object = 0x71727374; 270 q.target_rights = 0x81828384; 271 q.flags = 0x0003; 272 q.version = 2; 273 uint8_t b[AIENOS_CONTAIN_REQUEST_SIZE]; 274 aienos_contain_request_encode(&q, b); 275 CHECK(b[0] == 0x08 && b[7] == 0x01 && b[8] == 1 && b[9] == 4 && b[10] == 2 && b[11] == 0); 276 CHECK(b[12] == 0x24 && b[15] == 0x21 && b[16] == 0x31 && b[20] == 0x48 && b[27] == 0x41); 277 CHECK(b[28] == 0xA0 && b[59] == 0xBF && b[60] == 0xC0 && b[91] == 0xDF); 278 CHECK(b[92] == 0x58 && b[99] == 0x51 && b[100] == 0x68 && b[107] == 0x61); 279 CHECK(b[108] == 0x74 && b[111] == 0x71 && b[112] == 0x84 && b[115] == 0x81); 280 CHECK(b[116] == 0xE0 && b[147] == 0xC1 && b[148] == 3 && b[150] == 2 && b[151] == 0); 281 aienos_contain_request_digest(&q, d); 282 CHECK(hex_eq(d, "`e7b9720638cb215683fe4a5944efb630fa96a023d1afb7dd575ceebbed1204a6`")); 283 284 /* 10,000 random round trips, byte identical. */ 285 uint64_t s = 0x9E3779B97F4A7C15ull; 286 int ok = 1; 287 for (int n = 0; n < 10000; n++) { 288 uint8_t in[AIENOS_CONTAIN_REQUEST_SIZE], again[AIENOS_CONTAIN_REQUEST_SIZE]; 289 for (size_t i = 0; i < sizeof in; i++) { 290 s ^= s << 13; s ^= s >> 7; s ^= s << 17; 291 in[i] = (uint8_t)s; 292 } 293 in[8] = (uint8_t)(1 + in[8] % 10); 294 in[9] = (uint8_t)(1 + in[9] % 5); 295 in[148] &= 0x03; in[149] = 0; in[150] = 2; in[151] = 0; 296 in[92] |= 1; in[100] |= 1; 297 AienosContainRequest r; 298 if (aienos_contain_request_decode(in, &r) != AIENOS_CONTAIN_OK) ok = 0; 299 aienos_contain_request_encode(&r, again); 300 if (memcmp(in, again, sizeof in) != 0) ok = 0; 301 } 302 CHECK(ok); 303 printf("GATE CONTAIN_ENCODING %s sha256 KAT 3/3, round trips 10000\n", ok ? "PASS" : "FAIL"); 304 } 305 306 /* ---- Positive path (I1 accepted leaf revoke, receipts, I1.f recovery) ---- */ 307 static void test_positive_path(void) { 308 Rig *r = rig_new(1, 1); 309 AienosCapRef ex0; 310 aienos_contain_executor(r->gate, &ex0); 311 CHECK(ex0.cap_id != AIENOS_CONTAIN_CAP_NONE); 312 CHECK(count_subject_mints(r, 0, AIENOS_CONTAIN_SUBJ) == 1); /* the executor, at create */ 313 uint32_t after_create = r->nobs; 314 315 AienosCapRef keep = mint_root(r, 8, AIENOS_CAP_RIGHT_READ); 316 AienosCapRef target = mint_root(r, 7, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE); 317 AienosContainRequest q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, target); 318 uint8_t dig[32]; 319 aienos_contain_request_digest(&q, dig); 320 uint32_t obs0 = r->nobs; 321 AienosContainDecision d = submit(r, &q); 322 inv_begin(); 323 CHECK(d.kind == AIENOS_CONTAIN_KIND_DECIDED && d.status == AIENOS_CONTAIN_GRANT); 324 CHECK(d.why == AIENOS_CONTAIN_WHY_POLICY_GRANT && d.decision_id == 1 && d.resource == 1); 325 CHECK(d.seq == 1 && d.request_id == q.request_id && d.finding_sequence == q.finding_sequence); 326 CHECK(d.target.cap_id == target.cap_id && d.target.generation == target.generation); 327 CHECK(d.principal == 7 && d.actor == AIENOS_CONTAIN_ACTOR_GATE && d.type == AIENOS_CONTAIN_REVOKE_CAPABILITY); 328 CHECK(d.executor.cap_id == ex0.cap_id && d.executor.generation == ex0.generation); 329 CHECK(memcmp(d.request_digest, dig, 32) == 0); 330 CHECK(count_obs(r, obs0, AIENOS_CAP_OBS_REVOKE, 0) == 0); /* deciding changes nothing */ 331 332 AienosContainResult x; 333 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_OK); 334 CHECK(x.kind == AIENOS_CONTAIN_KIND_EXECUTED && x.status == AIENOS_CONTAIN_DONE); 335 CHECK(x.resource == 1 && x.rc == 0 && x.seq == 2 && x.decision_id == d.decision_id); 336 CHECK(memcmp(x.request_digest, dig, 32) == 0 && x.target.generation == target.generation); 337 CHECK(count_obs(r, obs0, AIENOS_CAP_OBS_REVOKE, 1) == 1); 338 uint32_t last = r->nobs - 1; 339 CHECK(r->obs[last].op == AIENOS_CAP_OBS_REVOKE && r->obs[last].e.cap_id == target.cap_id && 340 r->obs[last].e.generation == target.generation); 341 CHECK(aienos_cap_validate(r->view, target, 7, 0x1007u, AIENOS_CAP_RIGHT_READ, NULL) == AIENOS_CAP_ERR_REVOKED); 342 CHECK(aienos_cap_validate(r->view, keep, 8, 0x1008u, AIENOS_CAP_RIGHT_READ, NULL) == AIENOS_CAP_OK); 343 CHECK(is_live(r, r->office) && executor_intact(r, ex0)); 344 CHECK(r->nrec == 2 && receipts_ordered(r) && memcmp(&r->rec[0], &d, sizeof d) == 0 && 345 memcmp(&r->rec[1], &x, sizeof x) == 0); 346 inv_end("I1.a accepted-leaf-revoke"); 347 348 /* I1.f recoverable: the operator re-mints through the office. */ 349 inv_begin(); 350 AienosCapRef again = mint_root(r, 7, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_WRITE); 351 CHECK(aienos_cap_validate(r->view, again, 7, 0x1007u, AIENOS_CAP_RIGHT_READ, NULL) == AIENOS_CAP_OK); 352 CHECK(count_subject_mints(r, after_create, AIENOS_CONTAIN_SUBJ) == 0); 353 inv_end("I1.f re-mint-recovers"); 354 355 /* Second execute of the same GRANT: refused and announced, still one revoke. */ 356 inv_begin(); 357 uint32_t o1 = r->nobs; 358 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_ERR_REFUSED); 359 CHECK(x.status == AIENOS_CONTAIN_FAILED && x.why == AIENOS_CONTAIN_WHY_DECISION_REPLAY && 360 (x.gate_flags & AIENOS_CONTAIN_RF_REFUSED_CALL) && r->nrec == 3); 361 CHECK(count_obs(r, o1, AIENOS_CAP_OBS_REVOKE, 0) == 0); 362 CHECK(aienos_contain_execute(r->gate, 999, &x) == AIENOS_CONTAIN_ERR_UNKNOWN && r->nrec == 4); 363 inv_end("I1.e double-execute-refused"); 364 printf("GATE CONTAIN_POSITIVE %s footprint %zu bytes\n", failures ? "FAIL" : "PASS", 365 aienos_contain_footprint()); 366 rig_free(r); 367 } 368 369 /* ---- I1 hostile ---- */ 370 static void test_i1_hostile(void) { 371 /* (a) cascade attempt: target has a live descendant -> ESCALATE 1114, nothing revoked. */ 372 inv_begin(); 373 Rig *r = rig_new(1, 1); 374 AienosCapRef parent = mint_root(r, 7, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE); 375 AienosCapRef child = mint_child(r, parent, 7); 376 CHECK(child.cap_id != AIENOS_CAP_PARENT_NONE); 377 uint32_t o0 = r->nobs; 378 AienosContainDecision d = submit(r, &(AienosContainRequest){0}); 379 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 380 AienosContainRequest q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, parent); 381 d = submit(r, &q); 382 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NOT_LEAF); 383 AienosContainResult x; 384 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_ERR_REFUSED); 385 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && is_live(r, parent) && is_live(r, child)); 386 inv_end("I1.a cascade-target-escalates"); 387 388 /* (b) descendant minted between decide and execute -> re-check refuses, zero revokes. */ 389 inv_begin(); 390 AienosCapRef leaf = mint_root(r, 9, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE); 391 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 9, leaf); 392 d = submit(r, &q); 393 CHECK(d.status == AIENOS_CONTAIN_GRANT); 394 mint_child(r, leaf, 9); 395 o0 = r->nobs; 396 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_OK); 397 CHECK(x.status == AIENOS_CONTAIN_FAILED && x.why == AIENOS_CONTAIN_WHY_NOT_LEAF && x.resource == 0); 398 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && is_live(r, leaf)); 399 inv_end("I1.a recheck-before-revoke"); 400 401 /* (d) generation-bound: wrong or stale generation -> DENY 1104, no revoke. */ 402 inv_begin(); 403 AienosCapRef t = mint_root(r, 11, AIENOS_CAP_RIGHT_READ); 404 AienosCapRef wrong = {t.cap_id, t.generation + 1}; 405 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 11, wrong); 406 d = submit(r, &q); 407 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_TARGET_NOT_LIVE); 408 CHECK(d.target.generation == wrong.generation); 409 /* stale: revoke + reclaim + re-mint the slot, then ask for the old generation. */ 410 CHECK(aienos_cap_revoke(r->admin, r->office, t) == AIENOS_CAP_OK); 411 CHECK(aienos_cap_reclaim(r->admin, r->office, t.cap_id) == AIENOS_CAP_OK); 412 AienosCapRef t2 = mint_root(r, 11, AIENOS_CAP_RIGHT_READ); 413 CHECK(t2.cap_id == t.cap_id && t2.generation > t.generation); 414 o0 = r->nobs; 415 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 11, t); 416 d = submit(r, &q); 417 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_TARGET_NOT_LIVE); 418 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && is_live(r, t2)); 419 /* generation checked again at execute: granted at g, slot moved on before execute. */ 420 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 11, t2); 421 d = submit(r, &q); 422 CHECK(d.status == AIENOS_CONTAIN_GRANT); 423 CHECK(aienos_cap_revoke(r->admin, r->office, t2) == AIENOS_CAP_OK); 424 CHECK(aienos_cap_reclaim(r->admin, r->office, t2.cap_id) == AIENOS_CAP_OK); 425 AienosCapRef t3 = mint_root(r, 11, AIENOS_CAP_RIGHT_READ); 426 o0 = r->nobs; 427 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_OK); 428 CHECK(x.status == AIENOS_CONTAIN_FAILED && x.why == AIENOS_CONTAIN_WHY_TARGET_NOT_LIVE); 429 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && is_live(r, t3)); 430 inv_end("I1.d generation-bound"); 431 432 /* (e) not auto-eligible: SATURATION or SYNTHETIC -> ESCALATE 1116 even if policy grants. */ 433 inv_begin(); 434 AienosCapRef s1 = mint_root(r, 12, AIENOS_CAP_RIGHT_READ); 435 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 12, s1); 436 q.flags = AIENOS_CREQ_SATURATION; 437 d = submit(r, &q); 438 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE); 439 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 12, s1); 440 q.flags = AIENOS_CREQ_SYNTHETIC; 441 d = submit(r, &q); 442 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE); 443 /* subject mismatch -> DENY 1105 */ 444 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 13, s1); 445 d = submit(r, &q); 446 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_SUBJECT_MISMATCH); 447 /* privileged target, and ordinary cap of a subject that holds a privileged cap -> DENY 1103 */ 448 AienosCapRef priv = mint_root(r, 14, AIENOS_CAP_RIGHT_REVOKE); 449 AienosCapRef ord = mint_root(r, 14, AIENOS_CAP_RIGHT_READ); 450 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 14, priv); 451 d = submit(r, &q); 452 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 453 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 14, ord); 454 d = submit(r, &q); 455 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 456 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 0, ord); /* principal unset: still protected */ 457 d = submit(r, &q); 458 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 459 /* policy DENY / ESCALATE are honoured */ 460 policy_all(r, AIENOS_CONTAIN_DENY); 461 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 12, s1); 462 d = submit(r, &q); 463 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_POLICY_DENY); 464 policy_all(r, AIENOS_CONTAIN_ESCALATE); 465 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 12, s1); 466 d = submit(r, &q); 467 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_POLICY_ESCALATE); 468 policy_all(r, AIENOS_CONTAIN_GRANT); 469 CHECK(is_live(r, s1) && is_live(r, priv) && is_live(r, ord)); 470 inv_end("I1.a sweep-not-auto"); 471 rig_free(r); 472 473 /* (c) forced cascade via seam: a child appears after the re-check -> FAILED_SCOPE, inert. */ 474 inv_begin(); 475 r = rig_new(1, 1); 476 AienosCapRef c = mint_root(r, 7, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE); 477 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 478 d = submit(r, &q); 479 CHECK(d.status == AIENOS_CONTAIN_GRANT); 480 struct { Rig *r; AienosCapRef p; } hctx = {r, c}; 481 void hook(void *ctx); 482 AienosContainSeams seams = {0, hook, &hctx, NULL, NULL}; 483 CHECK(aienos_contain_set_seams(r->gate, &seams, NULL, r->office) == AIENOS_CONTAIN_ERR_REFUSED); 484 CHECK(aienos_contain_set_seams(r->gate, &seams, r->admin, r->office) == AIENOS_CONTAIN_OK); 485 o0 = r->nobs; 486 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_OK); 487 CHECK(x.status == AIENOS_CONTAIN_FAILED_SCOPE && (x.resource & 0xFFFFFFFFu) == 2 && x.rc == 0); 488 CHECK((x.gate_flags & AIENOS_CONTAIN_RF_INERT) && (x.gate_flags & AIENOS_CONTAIN_RF_SEAM)); 489 CHECK(aienos_contain_inert(r->gate) == 1 && count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 1) == 2); 490 aienos_contain_set_seams(r->gate, NULL, r->admin, r->office); 491 AienosCapRef n1 = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 492 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, n1); 493 d = submit(r, &q); 494 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED && 495 (d.gate_flags & AIENOS_CONTAIN_RF_INERT)); 496 inv_end("I1.b forced-cascade-goes-inert"); 497 rig_free(r); 498 499 /* Lineage honesty: a target minted before the gate, an unwired feed, an epoch bump. */ 500 inv_begin(); 501 r = rig_new(1, 0); 502 AienosCapRef early = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 503 AienosContainAuthorizer az = {aienos_contain_table_decide, &r->policy}; 504 CHECK(aienos_contain_create(&r->gate, r->mem, sizeof r->mem, r->admin, r->view, r->office, &az) == AIENOS_CONTAIN_OK); 505 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, early); 506 d = submit(r, &q); 507 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED); 508 AienosCapRef late = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 509 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, late); 510 d = submit(r, &q); 511 CHECK(d.status == AIENOS_CONTAIN_GRANT); 512 CHECK(aienos_cap_bump_epoch(r->admin, r->office) == AIENOS_CAP_OK); 513 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, late); 514 d = submit(r, &q); 515 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED); 516 rig_free(r); 517 r = rig_new(0, 1); /* feed not wired: the gate never saw its own executor mint */ 518 AienosCapRef nf = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 519 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, nf); 520 d = submit(r, &q); 521 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED); 522 CHECK(is_live(r, nf)); 523 inv_end("I1.a lineage-must-be-seen"); 524 rig_free(r); 525 } 526 527 void hook(void *ctx) { 528 struct { Rig *r; AienosCapRef p; } *h = ctx; 529 mint_child(h->r, h->p, 7); 530 } 531 532 /* ---- I5.b / root and admin targets, protection layers ---- */ 533 static void test_protected(void) { 534 inv_begin(); 535 Rig *r = rig_new(1, 1); 536 AienosCapRef ex0; 537 aienos_contain_executor(r->gate, &ex0); 538 uint32_t o0 = r->nobs; 539 AienosContainRequest q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 0, r->office); 540 AienosContainDecision d = submit(r, &q); 541 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 542 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, AIENOS_CONTAIN_SUBJ, ex0); 543 d = submit(r, &q); 544 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 545 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 0, ex0); 546 d = submit(r, &q); 547 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 548 uint32_t subs[3] = {AIENOS_CONTAIN_SUBJ_AEGIS, AIENOS_CONTAIN_SUBJ_AEGIS_ROOT, AIENOS_CONTAIN_SUBJ}; 549 for (int i = 0; i < 3; i++) { 550 AienosCapRef c = mint_root(r, subs[i], AIENOS_CAP_RIGHT_READ); 551 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, subs[i], c); 552 d = submit(r, &q); 553 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 554 q = req_make(AIENOS_CONTAIN_FREEZE_PRINCIPAL, subs[i], (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); /* stand-in type */ 555 d = submit(r, &q); 556 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 557 } 558 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && is_live(r, r->office) && executor_intact(r, ex0)); 559 inv_end("I5.b protected-targets-deny"); 560 561 /* Layer 3 alone (protected list off by seam): the authority refuses the office, -15. */ 562 inv_begin(); 563 AienosContainSeams seams = {AIENOS_CONTAIN_SEAM_NO_PROTECTED, NULL, NULL, NULL, NULL}; 564 CHECK(aienos_contain_set_seams(r->gate, &seams, r->admin, r->office) == AIENOS_CONTAIN_OK); 565 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 0, r->office); 566 d = submit(r, &q); 567 CHECK(d.status == AIENOS_CONTAIN_GRANT && (d.gate_flags & AIENOS_CONTAIN_RF_SEAM)); 568 AienosContainResult x; 569 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_OK); 570 CHECK(x.status == AIENOS_CONTAIN_FAILED && x.rc == AIENOS_CAP_ERR_UNAUTHORIZED && x.resource == (1ull << 32)); 571 CHECK(is_live(r, r->office) && executor_intact(r, ex0) && !aienos_contain_inert(r->gate)); 572 aienos_contain_set_seams(r->gate, NULL, r->admin, r->office); 573 inv_end("I5.b layer3-office-refused"); 574 rig_free(r); 575 } 576 577 /* ---- I3: whole-system never autonomous; budget ---- */ 578 static void test_i3(void) { 579 inv_begin(); 580 Rig *r = rig_new(1, 1); 581 uint32_t o0 = r->nobs; 582 for (uint8_t t = 2; t <= AIENOS_CONTAIN_TYPE_MAX; t++) { 583 AienosContainRequest q = req_make(t, 20 + t, (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); 584 AienosContainDecision d = submit(r, &q); 585 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE); 586 AienosContainResult x; 587 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_ERR_REFUSED); 588 CHECK(x.status != AIENOS_CONTAIN_DONE); 589 } 590 /* No encodable type outside 1..10 */ 591 AienosContainRequest q = req_make(0, 5, (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); 592 CHECK(submit(r, &q).why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 593 q = req_make(11, 5, (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); 594 CHECK(submit(r, &q).why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 595 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 0) == 0 && count_obs(r, o0, AIENOS_CAP_OBS_MINT, 0) == 0); 596 inv_end("I3.c non-revoke-types-never-granted"); 597 rig_free(r); 598 599 /* Storm on distinct leaf caps: at most 8 executed per 64 decisions. */ 600 inv_begin(); 601 r = rig_new(1, 1); 602 AienosCapRef caps[200]; 603 for (int i = 0; i < 200; i++) caps[i] = mint_root(r, 100 + (uint32_t)i, AIENOS_CAP_RIGHT_READ); 604 uint32_t per_window[8] = {0}; 605 uint32_t executed = 0, budget_esc = 0; 606 o0 = r->nobs; 607 for (int i = 0; i < 200; i++) { 608 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 100 + (uint32_t)i, caps[i]); 609 AienosContainDecision d = submit(r, &q); 610 if (d.status == AIENOS_CONTAIN_GRANT) { 611 AienosContainResult x; 612 aienos_contain_execute(r->gate, d.decision_id, &x); 613 if (x.status == AIENOS_CONTAIN_DONE) { 614 executed++; 615 per_window[(d.decision_id - 1) / 64]++; 616 } 617 } else { 618 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_BUDGET); 619 budget_esc++; 620 } 621 } 622 for (int w = 0; w < 4; w++) CHECK(per_window[w] <= AIENOS_CONTAIN_BUDGET); 623 CHECK(executed == 32 && budget_esc == 168); 624 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_REVOKE, 1) == executed); 625 CHECK(count_obs(r, o0, AIENOS_CAP_OBS_MINT, 0) == 0); 626 inv_end("I3.b budget-8-per-64"); 627 printf("GATE CONTAIN_BUDGET %s executed %u escalated %u of 200\n", 628 executed == 32 ? "PASS" : "FAIL", executed, budget_esc); 629 rig_free(r); 630 } 631 632 /* ---- I2: the stand-in type is never executed, never reported done ---- */ 633 static void test_i2(void) { 634 inv_begin(); 635 Rig *r = rig_new(1, 1); 636 AienosCapRef c = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 637 AienosContainRequest q = req_make(AIENOS_CONTAIN_FREEZE_PRINCIPAL, 7, c); /* stand-in type */ 638 AienosContainDecision d = submit(r, &q); 639 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE); 640 printf("SYNTHETIC-STAND-IN type %u decided %u why %u (recorded only; nothing is suspended)\n", 641 (unsigned)q.containment, (unsigned)d.status, (unsigned)d.why); 642 uint8_t wrong[AIENOS_CONTAIN_SECRET_LEN]; 643 memset(wrong, 0x5A, sizeof wrong); 644 AienosContainDecision h; 645 CHECK(aienos_contain_resolve(r->gate, d.decision_id, 1, wrong, &h) == AIENOS_CONTAIN_ERR_REFUSED); 646 CHECK(h.why == AIENOS_CONTAIN_WHY_BAD_SECRET && h.status == AIENOS_CONTAIN_ESCALATE && 647 (h.gate_flags & AIENOS_CONTAIN_RF_REFUSED_CALL) && h.actor == AIENOS_CONTAIN_ACTOR_OFFICE); 648 CHECK(aienos_contain_resolve(r->gate, d.decision_id, 1, NULL, &h) == AIENOS_CONTAIN_ERR_REFUSED); 649 CHECK(aienos_contain_resolve(r->gate, 4242, 1, wrong, &h) == AIENOS_CONTAIN_ERR_UNKNOWN); 650 AienosContainResult x; 651 CHECK(aienos_contain_execute(r->gate, d.decision_id, &x) == AIENOS_CONTAIN_ERR_REFUSED); 652 printf("SYNTHETIC-STAND-IN type %u execute status %u (never DONE)\n", (unsigned)q.containment, 653 (unsigned)x.status); 654 int any_done = 0; 655 for (uint32_t i = 0; i < r->nrec; i++) 656 if (r->rec[i].status == AIENOS_CONTAIN_DONE || r->rec[i].status == AIENOS_CONTAIN_PARTIAL) any_done = 1; 657 CHECK(!any_done && is_live(r, c) && receipts_ordered(r)); 658 inv_end("I2 stand-in-never-done"); 659 rig_free(r); 660 } 661 662 /* ---- I5: no self-expansion; replay; malformed; second create; executor gone ---- */ 663 static void test_i5_and_replay(void) { 664 inv_begin(); 665 Rig *r = rig_new(1, 1); 666 AienosCapRef ex0; 667 aienos_contain_executor(r->gate, &ex0); 668 uint32_t after_create = r->nobs; 669 /* Self-grant shapes: rights on a revoke, unknown rights on restrict, executor / own subject. */ 670 AienosCapRef c = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 671 AienosContainRequest q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 672 q.target_rights = AIENOS_CAP_RIGHT_MINT; 673 CHECK(submit(r, &q).why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 674 q = req_make(AIENOS_CONTAIN_RESTRICT_PRINCIPAL, 7, c); 675 q.target_rights = 0x80000000u; 676 CHECK(submit(r, &q).why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 677 q = req_make(AIENOS_CONTAIN_RESTRICT_PRINCIPAL, AIENOS_CONTAIN_SUBJ, ex0); 678 q.target_rights = AIENOS_CAP_RIGHT_REVOKE; 679 CHECK(submit(r, &q).why == AIENOS_CONTAIN_WHY_PROTECTED_TARGET); 680 /* Second create for the same authority: refused, no mint. */ 681 static _Alignas(16) unsigned char mem2[32 * 1024]; 682 AienosContain *g2 = NULL; 683 AienosContainAuthorizer az = {aienos_contain_table_decide, &r->policy}; 684 uint32_t o0 = r->nobs; 685 CHECK(aienos_contain_create(&g2, mem2, sizeof mem2, r->admin, r->view, r->office, &az) == AIENOS_CONTAIN_ERR_EXISTS); 686 CHECK(g2 == NULL && r->nobs == o0); 687 CHECK(count_subject_mints(r, after_create, AIENOS_CONTAIN_SUBJ) == 0 && executor_intact(r, ex0)); 688 inv_end("I5.a no-self-expansion-second-create-refused"); 689 690 /* Replay, tamper, malformed, oversized. */ 691 inv_begin(); 692 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 693 policy_all(r, AIENOS_CONTAIN_ESCALATE); 694 AienosContainDecision d = submit(r, &q); 695 CHECK(d.status == AIENOS_CONTAIN_ESCALATE); 696 d = submit(r, &q); /* same bytes, same digest */ 697 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_REPLAYED_REQUEST); 698 AienosContainRequest older = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 699 AienosContainRequest newer = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 700 CHECK(submit(r, &newer).status == AIENOS_CONTAIN_ESCALATE); 701 CHECK(submit(r, &older).why == AIENOS_CONTAIN_WHY_REPLAYED_REQUEST); 702 /* tampered after digest */ 703 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 704 uint8_t dig[32]; 705 aienos_contain_request_digest(&q, dig); 706 q.target.generation ^= 1; 707 CHECK(aienos_contain_submit(r->gate, &q, dig, &d) == AIENOS_CONTAIN_OK); 708 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 709 /* malformed fields */ 710 for (int k = 0; k < 8; k++) { 711 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 712 switch (k) { 713 case 0: q.version = 1; break; 714 case 1: q.reserved = 1; break; 715 case 2: q.flags = 0x4; break; 716 case 3: q.request_id = 0; break; 717 case 4: q.finding_sequence = 0; break; 718 case 5: q.severity = 0; break; 719 case 6: q.severity = 6; break; 720 case 7: q.target.cap_id = AIENOS_CAP_MAX; break; 721 } 722 d = submit(r, &q); 723 CHECK(d.status == AIENOS_CONTAIN_DENY && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 724 } 725 /* canonical bytes: oversized, short, good */ 726 uint8_t big[AIENOS_CONTAIN_REQUEST_SIZE + 1]; 727 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, c); 728 aienos_contain_request_encode(&q, big); 729 big[AIENOS_CONTAIN_REQUEST_SIZE] = 0; 730 CHECK(aienos_contain_submit_bytes(r->gate, big, sizeof big, &d) == AIENOS_CONTAIN_OK && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 731 CHECK(aienos_contain_submit_bytes(r->gate, big, AIENOS_CONTAIN_REQUEST_SIZE - 1, &d) == AIENOS_CONTAIN_OK && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 732 CHECK(aienos_contain_submit_bytes(r->gate, big, 0, &d) == AIENOS_CONTAIN_OK && d.why == AIENOS_CONTAIN_WHY_BAD_REQUEST); 733 CHECK(aienos_contain_submit_bytes(r->gate, big, AIENOS_CONTAIN_REQUEST_SIZE, &d) == AIENOS_CONTAIN_OK && 734 d.status == AIENOS_CONTAIN_ESCALATE && d.request_id == q.request_id); 735 CHECK(is_live(r, c) && receipts_ordered(r)); 736 inv_end("I5.d replay-tamper-malformed-refused-and-receipted"); 737 738 /* Executor revoked by the operator: gate never re-mints, escalates, execute UNAVAILABLE. */ 739 inv_begin(); 740 policy_all(r, AIENOS_CONTAIN_GRANT); 741 AienosCapRef c2 = mint_root(r, 8, AIENOS_CAP_RIGHT_READ); 742 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 8, c2); 743 AienosContainDecision granted = submit(r, &q); 744 CHECK(granted.status == AIENOS_CONTAIN_GRANT); 745 CHECK(aienos_cap_revoke(r->admin, r->office, ex0) == AIENOS_CAP_OK); 746 o0 = r->nobs; 747 AienosContainResult x; 748 CHECK(aienos_contain_execute(r->gate, granted.decision_id, &x) == AIENOS_CONTAIN_OK); 749 CHECK(x.status == AIENOS_CONTAIN_UNAVAILABLE && x.why == AIENOS_CONTAIN_WHY_NO_EXECUTOR); 750 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 8, c2); 751 d = submit(r, &q); 752 CHECK(d.status == AIENOS_CONTAIN_ESCALATE && d.why == AIENOS_CONTAIN_WHY_NO_EXECUTOR); 753 CHECK(r->nobs == o0 && is_live(r, c2)); 754 CHECK(count_subject_mints(r, after_create, AIENOS_CONTAIN_SUBJ) == 0); 755 inv_end("I5.a executor-gone-no-remint"); 756 rig_free(r); 757 } 758 759 /* ---- I4: validate never reaches the gate ---- */ 760 static void test_i4(void) { 761 inv_begin(); 762 Rig *r = rig_new(1, 1); 763 AienosCapRef c = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 764 uint32_t n0 = r->nrec; 765 for (int i = 0; i < 1000; i++) { 766 aienos_cap_validate(r->view, c, 7, 0x1007u, AIENOS_CAP_RIGHT_READ, NULL); 767 aienos_cap_validate(r->view, c, 99, 0x1007u, AIENOS_CAP_RIGHT_WRITE, NULL); /* denied */ 768 } 769 CHECK(r->nrec == n0 && is_live(r, c)); 770 inv_end("I4 validate-never-reaches-gate"); 771 rig_free(r); 772 } 773 774 /* Destroy cannot enable a second executor mint from the same authority. */ 775 static void test_destroy_recreate(void) { 776 inv_begin(); 777 Rig *r = rig_new(1, 1); 778 AienosCapRef ex; 779 CHECK(aienos_contain_executor(r->gate, &ex) == AIENOS_CONTAIN_OK); 780 uint32_t mints = count_subject_mints(r, 0, AIENOS_CONTAIN_SUBJ); 781 CHECK(mints == 1); 782 aienos_contain_destroy(r->gate); 783 r->gate = NULL; 784 static _Alignas(16) unsigned char mem[32 * 1024]; 785 AienosContain *again = NULL; 786 AienosContainAuthorizer az = {aienos_contain_table_decide, &r->policy}; 787 uint32_t obs = r->nobs; 788 CHECK(aienos_contain_create(&again, mem, sizeof mem, r->admin, r->view, r->office, &az) == 789 AIENOS_CONTAIN_ERR_EXISTS); 790 CHECK(again == NULL && r->nobs == obs); 791 CHECK(count_subject_mints(r, 0, AIENOS_CONTAIN_SUBJ) == mints); 792 AienosCapEntry entry; 793 CHECK(aienos_cap_inspect(r->view, ex, &entry) == AIENOS_CAP_OK && 794 entry.state == AIENOS_CAP_STATE_LIVE && entry.rights == AIENOS_CAP_RIGHT_REVOKE); 795 inv_end("I5.a destroy-does-not-enable-remint"); 796 rig_free(r); 797 } 798 799 /* ---- Determinism: same request sequence -> same receipts ---- */ 800 typedef struct { 801 uint32_t n; 802 AienosContainReceipt rec[256]; 803 } Run; 804 805 static void scenario(Run *out) { 806 next_request_id = 1; 807 Rig *r = rig_new(1, 1); 808 uint64_t base = r->office.generation; 809 AienosCapRef leaf = mint_root(r, 7, AIENOS_CAP_RIGHT_READ); 810 AienosCapRef par = mint_root(r, 9, AIENOS_CAP_RIGHT_READ | AIENOS_CAP_RIGHT_DELEGATE); 811 mint_child(r, par, 9); 812 AienosContainRequest q; 813 AienosContainDecision d; 814 AienosContainResult x; 815 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 7, leaf); d = submit(r, &q); 816 aienos_contain_execute(r->gate, d.decision_id, &x); 817 aienos_contain_execute(r->gate, d.decision_id, &x); 818 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 9, par); submit(r, &q); 819 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 0, r->office); submit(r, &q); 820 submit(r, &q); 821 q = req_make(AIENOS_CONTAIN_FREEZE_PRINCIPAL, 41, (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); submit(r, &q); /* stand-in */ 822 q = req_make(AIENOS_CONTAIN_QUARANTINE_MACHINE, 5, (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}); submit(r, &q); 823 for (int i = 0; i < 20; i++) { 824 AienosCapRef c = mint_root(r, 50 + (uint32_t)i, AIENOS_CAP_RIGHT_READ); 825 q = req_make(AIENOS_CONTAIN_REVOKE_CAPABILITY, 50 + (uint32_t)i, c); 826 d = submit(r, &q); 827 if (d.status == AIENOS_CONTAIN_GRANT) aienos_contain_execute(r->gate, d.decision_id, &x); 828 } 829 out->n = r->nrec < 256 ? r->nrec : 256; 830 for (uint32_t i = 0; i < out->n; i++) { 831 AienosContainReceipt c = r->rec[i]; 832 /* Each authority starts on its own boot generation: compare generations relative to it, 833 * and digests (which cover the generation) by recomputation, not across runs. */ 834 if (c.target.cap_id < AIENOS_CAP_MAX) c.target.generation -= base; 835 c.executor.generation -= base; 836 memset(c.request_digest, 0, sizeof c.request_digest); 837 out->rec[i] = c; 838 } 839 rig_free(r); 840 } 841 842 static void test_determinism(void) { 843 inv_begin(); 844 static Run a, b; 845 scenario(&a); 846 scenario(&b); 847 CHECK(a.n == b.n && a.n > 20); 848 CHECK(memcmp(a.rec, b.rec, a.n * sizeof a.rec[0]) == 0); 849 for (uint32_t i = 0; i < a.n && i < b.n; i++) 850 if (memcmp(&a.rec[i], &b.rec[i], sizeof a.rec[0]) != 0) { 851 const unsigned char *pa = (const void *)&a.rec[i], *pb = (const void *)&b.rec[i]; 852 for (size_t k = 0; k < sizeof a.rec[0]; k++) 853 if (pa[k] != pb[k]) fprintf(stderr, "DET diff receipt %u byte %zu\n", i, k); 854 } 855 inv_end("DET same-requests-same-receipts"); 856 printf("GATE CONTAIN_DETERMINISM %s receipts %u\n", failures == inv_fail_mark ? "PASS" : "FAIL", a.n); 857 } 858 859 int main(void) { 860 test_sha_and_encoding(); 861 test_positive_path(); 862 test_i1_hostile(); 863 test_protected(); 864 test_i3(); 865 test_i2(); 866 test_i5_and_replay(); 867 test_i4(); 868 test_destroy_recreate(); 869 test_determinism(); 870 CHECK(aienos_contain_footprint() <= 24u * 1024u); 871 printf("contain_test: %d checks, %d failures, footprint %zu bytes (<= 24576)\n", checks, failures, 872 aienos_contain_footprint()); 873 printf("GATE CONTAIN_TEST %s\n", failures ? "FAIL" : "PASS"); 874 return failures ? 1 : 0; 875 }