aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / capability / aienos_contain.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 | /* |
| 18 | * aienos_contain.c -- AEGIS containment gate (ARGUS-1 lane G). |
| 19 | * See aienos_contain.h and native/argus/docs/ARGUS1_SPEC.md section 5. |
| 20 | * |
| 21 | * Authority calls made here, and only here: aienos_cap_mint (once, in |
| 22 | * aienos_contain_create), aienos_cap_revoke (in execute, with the executor), |
| 23 | * aienos_cap_inspect, aienos_cap_office, aienos_cap_clock, |
| 24 | * aienos_cap_authorize. Nothing else from the authority is referenced (I3.a). |
| 25 | */ |
| 26 | #include "aienos_contain.h" |
| 27 | |
| 28 | #include <pthread.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* ---- SHA-256 (FIPS 180-4), self-contained so the gate never links libargus ---- */ |
| 33 | |
| 34 | static const uint32_t K256[64] = { |
| 35 | 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, |
| 36 | 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, |
| 37 | 0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, |
| 38 | 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, |
| 39 | 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, |
| 40 | 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu, |
| 41 | 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u, |
| 42 | 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u, |
| 43 | 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, |
| 44 | 0xc67178f2u}; |
| 45 | |
| 46 | #define ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) |
| 47 | |
| 48 | static void sha_block(uint32_t h[8], const uint8_t *p) { |
| 49 | uint32_t w[64]; |
| 50 | for (int i = 0; i < 16; i++) |
| 51 | w[i] = (uint32_t)p[4 * i] << 24 | (uint32_t)p[4 * i + 1] << 16 | |
| 52 | (uint32_t)p[4 * i + 2] << 8 | (uint32_t)p[4 * i + 3]; |
| 53 | for (int i = 16; i < 64; i++) { |
| 54 | uint32_t s0 = ROR(w[i - 15], 7) ^ ROR(w[i - 15], 18) ^ (w[i - 15] >> 3); |
| 55 | uint32_t s1 = ROR(w[i - 2], 17) ^ ROR(w[i - 2], 19) ^ (w[i - 2] >> 10); |
| 56 | w[i] = w[i - 16] + s0 + w[i - 7] + s1; |
| 57 | } |
| 58 | uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], k = h[7]; |
| 59 | for (int i = 0; i < 64; i++) { |
| 60 | uint32_t t1 = k + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i]; |
| 61 | uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); |
| 62 | k = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; |
| 63 | } |
| 64 | h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += k; |
| 65 | } |
| 66 | |
| 67 | void aienos_contain_sha256(const uint8_t *data, size_t len, uint8_t out[AIENOS_CONTAIN_DIGEST_LEN]) { |
| 68 | uint32_t h[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au, |
| 69 | 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u}; |
| 70 | size_t full = len / 64; |
| 71 | for (size_t i = 0; i < full; i++) sha_block(h, data + 64 * i); |
| 72 | uint8_t tail[128]; |
| 73 | size_t rem = len - 64 * full; |
| 74 | memset(tail, 0, sizeof tail); |
| 75 | if (rem) memcpy(tail, data + 64 * full, rem); |
| 76 | tail[rem] = 0x80; |
| 77 | size_t tl = rem + 1 + 8 <= 64 ? 64 : 128; |
| 78 | uint64_t bits = (uint64_t)len * 8u; |
| 79 | for (int i = 0; i < 8; i++) tail[tl - 1 - i] = (uint8_t)(bits >> (8 * i)); |
| 80 | sha_block(h, tail); |
| 81 | if (tl == 128) sha_block(h, tail + 64); |
| 82 | for (int i = 0; i < 8; i++) { |
| 83 | out[4 * i] = (uint8_t)(h[i] >> 24); |
| 84 | out[4 * i + 1] = (uint8_t)(h[i] >> 16); |
| 85 | out[4 * i + 2] = (uint8_t)(h[i] >> 8); |
| 86 | out[4 * i + 3] = (uint8_t)h[i]; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* ---- Canonical request encoding (spec 2) ---- */ |
| 91 | |
| 92 | static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 93 | static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 94 | static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 95 | static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); } |
| 96 | static uint32_t get32(const uint8_t *p) { |
| 97 | uint32_t v = 0; |
| 98 | for (int i = 3; i >= 0; i--) v = v << 8 | p[i]; |
| 99 | return v; |
| 100 | } |
| 101 | static uint64_t get64(const uint8_t *p) { |
| 102 | uint64_t v = 0; |
| 103 | for (int i = 7; i >= 0; i--) v = v << 8 | p[i]; |
| 104 | return v; |
| 105 | } |
| 106 | |
| 107 | void aienos_contain_request_encode(const AienosContainRequest *r, |
| 108 | uint8_t out[AIENOS_CONTAIN_REQUEST_SIZE]) { |
| 109 | put64(out + 0, r->incident_id); |
| 110 | out[8] = r->containment; |
| 111 | out[9] = r->severity; |
| 112 | put16(out + 10, r->finding_code); |
| 113 | put32(out + 12, r->principal); |
| 114 | put32(out + 16, r->target.cap_id); |
| 115 | put64(out + 20, r->target.generation); |
| 116 | memcpy(out + 28, r->machine_id, AIENOS_CONTAIN_MACHINE_ID_LEN); |
| 117 | memcpy(out + 60, r->finding_digest, AIENOS_CONTAIN_DIGEST_LEN); |
| 118 | put64(out + 92, r->request_id); |
| 119 | put64(out + 100, r->finding_sequence); |
| 120 | put32(out + 108, r->target_object); |
| 121 | put32(out + 112, r->target_rights); |
| 122 | memcpy(out + 116, r->target_digest, AIENOS_CONTAIN_DIGEST_LEN); |
| 123 | put16(out + 148, r->flags); |
| 124 | out[150] = r->version; |
| 125 | out[151] = r->reserved; |
| 126 | } |
| 127 | |
| 128 | static bool request_well_formed(const AienosContainRequest *r) { |
| 129 | return r->containment >= 1 && r->containment <= AIENOS_CONTAIN_TYPE_MAX && |
| 130 | r->severity >= 1 && r->severity <= 5 && |
| 131 | r->version == AIENOS_CONTAIN_REQUEST_VERSION && r->reserved == 0 && |
| 132 | (r->flags & ~AIENOS_CREQ_KNOWN) == 0 && r->request_id != 0 && |
| 133 | r->finding_sequence != 0; |
| 134 | } |
| 135 | |
| 136 | int aienos_contain_request_decode(const uint8_t in[AIENOS_CONTAIN_REQUEST_SIZE], |
| 137 | AienosContainRequest *out) { |
| 138 | if (!in || !out) return AIENOS_CONTAIN_ERR_ARG; |
| 139 | AienosContainRequest r; |
| 140 | memset(&r, 0, sizeof r); |
| 141 | r.incident_id = get64(in + 0); |
| 142 | r.containment = in[8]; |
| 143 | r.severity = in[9]; |
| 144 | r.finding_code = get16(in + 10); |
| 145 | r.principal = get32(in + 12); |
| 146 | r.target.cap_id = get32(in + 16); |
| 147 | r.target.generation = get64(in + 20); |
| 148 | memcpy(r.machine_id, in + 28, AIENOS_CONTAIN_MACHINE_ID_LEN); |
| 149 | memcpy(r.finding_digest, in + 60, AIENOS_CONTAIN_DIGEST_LEN); |
| 150 | r.request_id = get64(in + 92); |
| 151 | r.finding_sequence = get64(in + 100); |
| 152 | r.target_object = get32(in + 108); |
| 153 | r.target_rights = get32(in + 112); |
| 154 | memcpy(r.target_digest, in + 116, AIENOS_CONTAIN_DIGEST_LEN); |
| 155 | r.flags = get16(in + 148); |
| 156 | r.version = in[150]; |
| 157 | r.reserved = in[151]; |
| 158 | *out = r; |
| 159 | return request_well_formed(&r) ? AIENOS_CONTAIN_OK : AIENOS_CONTAIN_ERR_ARG; |
| 160 | } |
| 161 | |
| 162 | void aienos_contain_request_digest(const AienosContainRequest *r, |
| 163 | uint8_t out[AIENOS_CONTAIN_DIGEST_LEN]) { |
| 164 | uint8_t b[AIENOS_CONTAIN_REQUEST_SIZE]; |
| 165 | aienos_contain_request_encode(r, b); |
| 166 | aienos_contain_sha256(b, sizeof b, out); |
| 167 | } |
| 168 | |
| 169 | /* ---- Gate state ---- */ |
| 170 | |
| 171 | /* Lineage shadow of one authority slot, fed only by observer announcements. */ |
| 172 | typedef struct { |
| 173 | uint64_t generation; |
| 174 | uint64_t parent_generation; |
| 175 | uint32_t subject; |
| 176 | uint32_t rights; |
| 177 | uint32_t parent_id; |
| 178 | uint8_t state; /* AIENOS_CAP_STATE_* */ |
| 179 | uint8_t known; /* 1: every change to this slot since the gate existed was seen */ |
| 180 | uint8_t pad[2]; |
| 181 | } Lin; |
| 182 | |
| 183 | enum { D_FREE = 0, D_GRANTED, D_DENIED, D_ESCALATED, D_EXECUTED }; |
| 184 | |
| 185 | typedef struct { |
| 186 | AienosContainRequest req; |
| 187 | uint8_t digest[AIENOS_CONTAIN_DIGEST_LEN]; |
| 188 | uint64_t decision_id; |
| 189 | uint8_t state; |
| 190 | uint8_t human; |
| 191 | uint8_t pad[6]; |
| 192 | } Dec; |
| 193 | |
| 194 | struct AienosContain { |
| 195 | pthread_mutex_t lock; |
| 196 | AienosCapAdmin *admin; |
| 197 | const AienosCapView *view; |
| 198 | AienosCapRef office; |
| 199 | AienosCapRef executor; |
| 200 | AienosContainAuthorizer authz; |
| 201 | AienosContainSink sink; |
| 202 | void *sink_ctx; |
| 203 | AienosContainSeams seams; |
| 204 | uint32_t seams_on; |
| 205 | uint64_t seq; |
| 206 | uint64_t last_decision_id; |
| 207 | uint64_t last_request_id; |
| 208 | uint64_t window; |
| 209 | uint32_t window_grants; |
| 210 | uint8_t inert; /* I1.b: scope exceeded; sticky */ |
| 211 | uint8_t lineage_trusted; /* saw the executor's own MINT; cleared by EPOCH/KILL/RESTART/unknown */ |
| 212 | uint8_t executor_dead; |
| 213 | uint8_t counting; |
| 214 | uint32_t revokes_seen; |
| 215 | uint8_t target_seen; |
| 216 | AienosCapRef exec_target; |
| 217 | Lin lin[AIENOS_CAP_MAX]; |
| 218 | Dec dec[AIENOS_CONTAIN_DECISIONS]; |
| 219 | }; |
| 220 | |
| 221 | _Static_assert(sizeof(struct AienosContain) <= 24u * 1024u, "gate footprint over 24 KiB (G7)"); |
| 222 | |
| 223 | size_t aienos_contain_footprint(void) { return sizeof(struct AienosContain); } |
| 224 | |
| 225 | /* Authorities that already have a gate: (admin handle, office generation). |
| 226 | * The office generation is unique per authority instance in a process. */ |
| 227 | typedef struct { |
| 228 | const AienosCapAdmin *admin; |
| 229 | uint64_t office_generation; |
| 230 | } RegEntry; |
| 231 | static pthread_mutex_t reg_lock = PTHREAD_MUTEX_INITIALIZER; |
| 232 | static RegEntry reg[AIENOS_CONTAIN_REGISTRY]; |
| 233 | static uint32_t reg_n; |
| 234 | |
| 235 | static int reg_claim(const AienosCapAdmin *admin, uint64_t gen) { |
| 236 | int rc = AIENOS_CONTAIN_OK; |
| 237 | pthread_mutex_lock(®_lock); |
| 238 | for (uint32_t i = 0; i < reg_n; i++) |
| 239 | if (reg[i].admin == admin && reg[i].office_generation == gen) rc = AIENOS_CONTAIN_ERR_EXISTS; |
| 240 | if (rc == AIENOS_CONTAIN_OK) { |
| 241 | if (reg_n >= AIENOS_CONTAIN_REGISTRY) rc = AIENOS_CONTAIN_ERR_FULL; |
| 242 | else reg[reg_n++] = (RegEntry){admin, gen}; |
| 243 | } |
| 244 | pthread_mutex_unlock(®_lock); |
| 245 | return rc; |
| 246 | } |
| 247 | |
| 248 | static void reg_release(const AienosCapAdmin *admin, uint64_t gen) { |
| 249 | pthread_mutex_lock(®_lock); |
| 250 | for (uint32_t i = 0; i < reg_n; i++) |
| 251 | if (reg[i].admin == admin && reg[i].office_generation == gen) { |
| 252 | reg[i] = reg[--reg_n]; |
| 253 | break; |
| 254 | } |
| 255 | pthread_mutex_unlock(®_lock); |
| 256 | } |
| 257 | |
| 258 | /* ---- Table policy ---- */ |
| 259 | |
| 260 | int aienos_contain_table_decide(void *ctx, const AienosContainRequest *r, |
| 261 | const AienosCapView *view, uint32_t *verdict, uint32_t *why) { |
| 262 | (void)view; |
| 263 | const AienosContainTablePolicy *t = ctx; |
| 264 | uint32_t v = AIENOS_CONTAIN_ESCALATE; |
| 265 | if (t && r && r->containment >= 1 && r->containment <= AIENOS_CONTAIN_TYPE_MAX) |
| 266 | v = t->verdict[r->containment]; |
| 267 | *verdict = v; |
| 268 | *why = v == AIENOS_CONTAIN_GRANT ? AIENOS_CONTAIN_WHY_POLICY_GRANT |
| 269 | : v == AIENOS_CONTAIN_DENY ? AIENOS_CONTAIN_WHY_POLICY_DENY |
| 270 | : AIENOS_CONTAIN_WHY_POLICY_ESCALATE; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* ---- Helpers (gate lock held) ---- */ |
| 275 | |
| 276 | static bool is_protected_subject(uint32_t s) { |
| 277 | return s == AIENOS_CONTAIN_SUBJ || s == AIENOS_CONTAIN_SUBJ_AEGIS || |
| 278 | s == AIENOS_CONTAIN_SUBJ_AEGIS_ROOT; |
| 279 | } |
| 280 | |
| 281 | static bool subject_privileged(const AienosContain *g, uint32_t subject) { |
| 282 | for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { |
| 283 | const Lin *l = &g->lin[i]; |
| 284 | if (l->state == AIENOS_CAP_STATE_LIVE && l->subject == subject && |
| 285 | (l->rights & AIENOS_CAP_RIGHT_PRIVILEGED)) |
| 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | static bool has_live_child(const AienosContain *g, uint32_t id, uint64_t gen) { |
| 292 | for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { |
| 293 | const Lin *l = &g->lin[i]; |
| 294 | if (l->state == AIENOS_CAP_STATE_LIVE && l->parent_id == id && l->parent_generation == gen) |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | /* Layer 2: protected targets (I5.b), whatever the policy says. */ |
| 301 | static bool is_protected(const AienosContain *g, const AienosContainRequest *r) { |
| 302 | if (g->seams_on && (g->seams.flags & AIENOS_CONTAIN_SEAM_NO_PROTECTED)) return false; |
| 303 | if (is_protected_subject(r->principal)) return true; |
| 304 | if (r->principal != 0 && subject_privileged(g, r->principal)) return true; |
| 305 | uint32_t id = r->target.cap_id; |
| 306 | if (id == AIENOS_CONTAIN_CAP_NONE) return false; |
| 307 | if (id == 0 || id == g->executor.cap_id || id == g->office.cap_id) return true; |
| 308 | AienosCapEntry e; |
| 309 | if (aienos_cap_inspect(g->view, r->target, &e) == AIENOS_CAP_OK) { |
| 310 | if (e.rights & AIENOS_CAP_RIGHT_PRIVILEGED) return true; |
| 311 | if (is_protected_subject(e.subject) || subject_privileged(g, e.subject)) return true; |
| 312 | } |
| 313 | if (id < AIENOS_CAP_MAX) { |
| 314 | const Lin *l = &g->lin[id]; |
| 315 | if (l->generation == r->target.generation && (l->rights & AIENOS_CAP_RIGHT_PRIVILEGED)) |
| 316 | return true; |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | static bool executor_live(AienosContain *g) { |
| 322 | if (g->executor_dead) return false; |
| 323 | AienosCapEntry e; |
| 324 | if (aienos_cap_inspect(g->view, g->executor, &e) != AIENOS_CAP_OK || |
| 325 | e.state != AIENOS_CAP_STATE_LIVE || e.rights != AIENOS_CAP_RIGHT_REVOKE || |
| 326 | e.subject != AIENOS_CONTAIN_SUBJ) { |
| 327 | g->executor_dead = 1; |
| 328 | return false; |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | /* The I1.a target conditions that the authority can answer plus the |
| 334 | * lineage: 0 if eligible, else the WHY and *status_escalate for "ask". */ |
| 335 | static uint16_t revoke_target_check(AienosContain *g, const AienosContainRequest *r, bool *ask) { |
| 336 | *ask = false; |
| 337 | AienosCapEntry e; |
| 338 | if (aienos_cap_inspect(g->view, r->target, &e) != AIENOS_CAP_OK || |
| 339 | e.state != AIENOS_CAP_STATE_LIVE) |
| 340 | return AIENOS_CONTAIN_WHY_TARGET_NOT_LIVE; |
| 341 | if (r->principal != 0 && e.subject != r->principal) return AIENOS_CONTAIN_WHY_SUBJECT_MISMATCH; |
| 342 | const Lin *l = &g->lin[r->target.cap_id]; |
| 343 | if (!l->known || l->generation != r->target.generation || l->state != AIENOS_CAP_STATE_LIVE) { |
| 344 | *ask = true; |
| 345 | return AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED; |
| 346 | } |
| 347 | if (has_live_child(g, r->target.cap_id, r->target.generation)) { |
| 348 | *ask = true; |
| 349 | return AIENOS_CONTAIN_WHY_NOT_LEAF; |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static void receipt_base(AienosContain *g, AienosContainReceipt *o, const AienosContainRequest *r, |
| 355 | const uint8_t *digest, uint8_t kind) { |
| 356 | memset(o, 0, sizeof *o); |
| 357 | o->seq = ++g->seq; |
| 358 | o->kind = kind; |
| 359 | o->actor = AIENOS_CONTAIN_ACTOR_GATE; |
| 360 | o->executor.cap_id = g->executor.cap_id; /* field by field: padding stays zero */ |
| 361 | o->executor.generation = g->executor.generation; |
| 362 | o->tick = aienos_cap_clock(g->view); |
| 363 | if (r) { |
| 364 | o->request_id = r->request_id; |
| 365 | o->finding_sequence = r->finding_sequence; |
| 366 | o->target.cap_id = r->target.cap_id; |
| 367 | o->target.generation = r->target.generation; |
| 368 | o->principal = r->principal; |
| 369 | o->finding_code = r->finding_code; |
| 370 | o->flags = r->flags; |
| 371 | o->type = r->containment; |
| 372 | } |
| 373 | if (digest) memcpy(o->request_digest, digest, AIENOS_CONTAIN_DIGEST_LEN); |
| 374 | if (g->seams_on) o->gate_flags |= AIENOS_CONTAIN_RF_SEAM; |
| 375 | } |
| 376 | |
| 377 | static void emit(AienosContain *g, const AienosContainReceipt *o) { |
| 378 | /* Called with the lock released. */ |
| 379 | AienosContainSink fn; |
| 380 | void *ctx; |
| 381 | pthread_mutex_lock(&g->lock); |
| 382 | fn = g->sink; |
| 383 | ctx = g->sink_ctx; |
| 384 | pthread_mutex_unlock(&g->lock); |
| 385 | if (fn) fn(ctx, o); |
| 386 | } |
| 387 | |
| 388 | static Dec *dec_find(AienosContain *g, uint64_t id) { |
| 389 | if (id == 0) return NULL; |
| 390 | for (uint32_t i = 0; i < AIENOS_CONTAIN_DECISIONS; i++) |
| 391 | if (g->dec[i].state != D_FREE && g->dec[i].decision_id == id) return &g->dec[i]; |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | static Dec *dec_slot(AienosContain *g) { |
| 396 | Dec *best = &g->dec[0]; |
| 397 | for (uint32_t i = 0; i < AIENOS_CONTAIN_DECISIONS; i++) { |
| 398 | if (g->dec[i].state == D_FREE) return &g->dec[i]; |
| 399 | if (g->dec[i].decision_id < best->decision_id) best = &g->dec[i]; |
| 400 | } |
| 401 | return best; /* oldest decision is forgotten: it can no longer be resolved or executed */ |
| 402 | } |
| 403 | |
| 404 | /* ---- Create / destroy ---- */ |
| 405 | |
| 406 | int aienos_contain_create(AienosContain **gp, void *mem, size_t bytes, AienosCapAdmin *admin, |
| 407 | const AienosCapView *view, AienosCapRef office, |
| 408 | const AienosContainAuthorizer *authz) { |
| 409 | if (!gp || !mem || !admin || !view || !authz || !authz->decide) return AIENOS_CONTAIN_ERR_ARG; |
| 410 | if (bytes < sizeof(struct AienosContain)) return AIENOS_CONTAIN_ERR_SPACE; |
| 411 | if ((uintptr_t)mem % _Alignof(struct AienosContain) != 0) return AIENOS_CONTAIN_ERR_ARG; |
| 412 | AienosCapRef cur; |
| 413 | if (aienos_cap_office(admin, &cur) != AIENOS_CAP_OK || cur.cap_id != office.cap_id || |
| 414 | cur.generation != office.generation) |
| 415 | return AIENOS_CONTAIN_ERR_ARG; |
| 416 | AienosCapEntry oe; |
| 417 | if (aienos_cap_inspect(view, office, &oe) != AIENOS_CAP_OK || |
| 418 | oe.state != AIENOS_CAP_STATE_LIVE || !(oe.rights & AIENOS_CAP_RIGHT_MINT)) |
| 419 | return AIENOS_CONTAIN_ERR_ARG; |
| 420 | int rc = reg_claim(admin, office.generation); |
| 421 | if (rc != AIENOS_CONTAIN_OK) return rc; |
| 422 | |
| 423 | AienosContain *g = mem; |
| 424 | memset(g, 0, sizeof *g); |
| 425 | if (pthread_mutex_init(&g->lock, NULL) != 0) { |
| 426 | reg_release(admin, office.generation); |
| 427 | return AIENOS_CONTAIN_ERR_ARG; |
| 428 | } |
| 429 | g->admin = admin; |
| 430 | g->view = view; |
| 431 | g->office = office; |
| 432 | g->executor = (AienosCapRef){AIENOS_CONTAIN_CAP_NONE, 0}; |
| 433 | g->authz = *authz; |
| 434 | for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) g->lin[i].parent_id = AIENOS_CAP_PARENT_NONE; |
| 435 | Lin *ol = &g->lin[office.cap_id]; |
| 436 | *ol = (Lin){oe.generation, oe.parent_generation, oe.subject, oe.rights, oe.parent_id, |
| 437 | (uint8_t)oe.state, 1, {0, 0}}; |
| 438 | *gp = g; /* before the mint: an observer routed through *gp sees it */ |
| 439 | |
| 440 | /* The only mint (I5.a): REVOKE only, subject AIENOS_CONTAIN_SUBJ, no parent, no lease. */ |
| 441 | AienosCapMint m = {oe.subject, AIENOS_CONTAIN_SUBJ, AIENOS_CAP_RES_AUTHORITY, |
| 442 | AIENOS_CAP_RIGHT_REVOKE, 0, {AIENOS_CAP_PARENT_NONE, 0}, office}; |
| 443 | AienosCapRef ex; |
| 444 | if (aienos_cap_mint(admin, &m, &ex) != AIENOS_CAP_OK) { |
| 445 | *gp = NULL; |
| 446 | pthread_mutex_destroy(&g->lock); |
| 447 | memset(g, 0, sizeof *g); |
| 448 | reg_release(admin, office.generation); |
| 449 | return AIENOS_CONTAIN_ERR_AUTHORITY; |
| 450 | } |
| 451 | pthread_mutex_lock(&g->lock); |
| 452 | g->executor = ex; |
| 453 | const Lin *xl = &g->lin[ex.cap_id]; |
| 454 | g->lineage_trusted = (uint8_t)(xl->known && xl->generation == ex.generation && |
| 455 | xl->state == AIENOS_CAP_STATE_LIVE && |
| 456 | xl->subject == AIENOS_CONTAIN_SUBJ); |
| 457 | pthread_mutex_unlock(&g->lock); |
| 458 | return AIENOS_CONTAIN_OK; |
| 459 | } |
| 460 | |
| 461 | void aienos_contain_destroy(AienosContain *g) { |
| 462 | if (!g) return; |
| 463 | pthread_mutex_destroy(&g->lock); |
| 464 | memset(g, 0, sizeof *g); |
| 465 | } |
| 466 | |
| 467 | int aienos_contain_set_sink(AienosContain *g, AienosContainSink fn, void *ctx) { |
| 468 | if (!g) return AIENOS_CONTAIN_ERR_ARG; |
| 469 | pthread_mutex_lock(&g->lock); |
| 470 | g->sink = fn; |
| 471 | g->sink_ctx = fn ? ctx : NULL; |
| 472 | pthread_mutex_unlock(&g->lock); |
| 473 | return AIENOS_CONTAIN_OK; |
| 474 | } |
| 475 | |
| 476 | int aienos_contain_executor(const AienosContain *g, AienosCapRef *out) { |
| 477 | if (!g || !out) return AIENOS_CONTAIN_ERR_ARG; |
| 478 | *out = g->executor; |
| 479 | return AIENOS_CONTAIN_OK; |
| 480 | } |
| 481 | |
| 482 | int aienos_contain_inert(const AienosContain *g) { return g ? g->inert : 1; } |
| 483 | |
| 484 | int aienos_contain_set_seams(AienosContain *g, const AienosContainSeams *s, AienosCapAdmin *admin, |
| 485 | AienosCapRef office) { |
| 486 | if (!g || admin != g->admin) return AIENOS_CONTAIN_ERR_REFUSED; |
| 487 | AienosCapEntry e; |
| 488 | if (aienos_cap_inspect(g->view, office, &e) != AIENOS_CAP_OK || |
| 489 | e.state != AIENOS_CAP_STATE_LIVE || !(e.rights & AIENOS_CAP_RIGHT_MINT)) |
| 490 | return AIENOS_CONTAIN_ERR_REFUSED; |
| 491 | pthread_mutex_lock(&g->lock); |
| 492 | if (s) { |
| 493 | g->seams = *s; |
| 494 | g->seams_on = 1; |
| 495 | } else { |
| 496 | memset(&g->seams, 0, sizeof g->seams); |
| 497 | g->seams_on = 0; |
| 498 | } |
| 499 | pthread_mutex_unlock(&g->lock); |
| 500 | return AIENOS_CONTAIN_OK; |
| 501 | } |
| 502 | |
| 503 | /* ---- Lineage feed ---- */ |
| 504 | |
| 505 | void aienos_contain_lineage_observe(AienosContain *g, uint32_t op, const AienosCapEntry *e, |
| 506 | int result) { |
| 507 | if (!g) return; |
| 508 | pthread_mutex_lock(&g->lock); |
| 509 | if (g->counting && op == AIENOS_CAP_OBS_REVOKE && e) { |
| 510 | g->revokes_seen++; |
| 511 | if (e->cap_id == g->exec_target.cap_id && e->generation == g->exec_target.generation) |
| 512 | g->target_seen = 1; |
| 513 | } |
| 514 | if (!e) { |
| 515 | /* A refused operation changes nothing; a kill ends trust. */ |
| 516 | if (op == AIENOS_CAP_OBS_KILL) g->lineage_trusted = 0; |
| 517 | else if (result == AIENOS_CAP_OK) g->lineage_trusted = 0; /* unexpected shape */ |
| 518 | pthread_mutex_unlock(&g->lock); |
| 519 | return; |
| 520 | } |
| 521 | if (e->cap_id >= AIENOS_CAP_MAX) { |
| 522 | g->lineage_trusted = 0; |
| 523 | pthread_mutex_unlock(&g->lock); |
| 524 | return; |
| 525 | } |
| 526 | Lin *l = &g->lin[e->cap_id]; |
| 527 | switch (op) { |
| 528 | case AIENOS_CAP_OBS_MINT: |
| 529 | *l = (Lin){e->generation, e->parent_generation, e->subject, e->rights, e->parent_id, |
| 530 | (uint8_t)e->state, 1, {0, 0}}; |
| 531 | break; |
| 532 | case AIENOS_CAP_OBS_REVOKE: { |
| 533 | uint8_t known = (uint8_t)(l->known && l->generation == e->generation); |
| 534 | *l = (Lin){e->generation, e->parent_generation, e->subject, e->rights, e->parent_id, |
| 535 | (uint8_t)e->state, known, {0, 0}}; |
| 536 | if (e->cap_id == g->executor.cap_id && e->generation == g->executor.generation) |
| 537 | g->executor_dead = 1; |
| 538 | break; |
| 539 | } |
| 540 | case AIENOS_CAP_OBS_RECLAIM: |
| 541 | /* Free slot on its next generation: the next mint here will be seen. */ |
| 542 | *l = (Lin){e->generation, 0, 0, 0, AIENOS_CAP_PARENT_NONE, (uint8_t)e->state, 1, {0, 0}}; |
| 543 | break; |
| 544 | case AIENOS_CAP_OBS_CLOCK: |
| 545 | break; |
| 546 | default: /* EPOCH, RESTART, anything unknown: the shadow can no longer be trusted */ |
| 547 | g->lineage_trusted = 0; |
| 548 | break; |
| 549 | } |
| 550 | pthread_mutex_unlock(&g->lock); |
| 551 | } |
| 552 | |
| 553 | /* ---- Decide ---- */ |
| 554 | |
| 555 | /* Records a decision (lock held) and fills the receipt. */ |
| 556 | static void decide_out(AienosContain *g, const AienosContainRequest *r, const uint8_t *digest, |
| 557 | uint8_t status, uint16_t why, bool keep, AienosContainReceipt *o) { |
| 558 | uint64_t id = ++g->last_decision_id; |
| 559 | uint64_t window = (id - 1) / AIENOS_CONTAIN_BUDGET_WINDOW; |
| 560 | if (window != g->window) { |
| 561 | g->window = window; |
| 562 | g->window_grants = 0; |
| 563 | } |
| 564 | if (status == AIENOS_CONTAIN_GRANT && r && r->containment == AIENOS_CONTAIN_REVOKE_CAPABILITY) |
| 565 | g->window_grants++; |
| 566 | receipt_base(g, o, r, digest, AIENOS_CONTAIN_KIND_DECIDED); |
| 567 | o->decision_id = id; |
| 568 | o->resource = id; |
| 569 | o->status = status; |
| 570 | o->why = why; |
| 571 | if (g->inert) o->gate_flags |= AIENOS_CONTAIN_RF_INERT; |
| 572 | if (keep && r) { |
| 573 | Dec *d = dec_slot(g); |
| 574 | memset(d, 0, sizeof *d); |
| 575 | d->req = *r; |
| 576 | memcpy(d->digest, digest, AIENOS_CONTAIN_DIGEST_LEN); |
| 577 | d->decision_id = id; |
| 578 | d->state = status == AIENOS_CONTAIN_GRANT ? D_GRANTED |
| 579 | : status == AIENOS_CONTAIN_ESCALATE ? D_ESCALATED |
| 580 | : D_DENIED; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static bool narrow_shape(const AienosContainRequest *r) { |
| 585 | uint32_t id = r->target.cap_id; |
| 586 | if (id != AIENOS_CONTAIN_CAP_NONE && id >= AIENOS_CAP_MAX) return false; |
| 587 | if (r->containment != AIENOS_CONTAIN_RESTRICT_PRINCIPAL && r->target_rights != 0) return false; |
| 588 | if (r->containment == AIENOS_CONTAIN_RESTRICT_PRINCIPAL && |
| 589 | (r->target_rights & ~AIENOS_CAP_RIGHT_KNOWN) != 0) |
| 590 | return false; |
| 591 | if (r->containment == AIENOS_CONTAIN_REVOKE_CAPABILITY) |
| 592 | return id != AIENOS_CONTAIN_CAP_NONE && r->target_object == 0; |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | /* Lock held. Returns the verdict and why. */ |
| 597 | static void decide_locked(AienosContain *g, const AienosContainRequest *r, uint8_t *status, |
| 598 | uint16_t *why) { |
| 599 | if (is_protected(g, r)) { |
| 600 | *status = AIENOS_CONTAIN_DENY; |
| 601 | *why = AIENOS_CONTAIN_WHY_PROTECTED_TARGET; |
| 602 | return; |
| 603 | } |
| 604 | bool live = r->containment == AIENOS_CONTAIN_REVOKE_CAPABILITY; |
| 605 | if (live) { |
| 606 | if (g->inert || !g->lineage_trusted) { |
| 607 | *status = AIENOS_CONTAIN_ESCALATE; |
| 608 | *why = AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED; |
| 609 | return; |
| 610 | } |
| 611 | if (!executor_live(g)) { |
| 612 | *status = AIENOS_CONTAIN_ESCALATE; |
| 613 | *why = AIENOS_CONTAIN_WHY_NO_EXECUTOR; |
| 614 | return; |
| 615 | } |
| 616 | bool ask; |
| 617 | uint16_t w = revoke_target_check(g, r, &ask); |
| 618 | if (w) { |
| 619 | *status = ask ? AIENOS_CONTAIN_ESCALATE : AIENOS_CONTAIN_DENY; |
| 620 | *why = w; |
| 621 | return; |
| 622 | } |
| 623 | } |
| 624 | uint32_t verdict = AIENOS_CONTAIN_ESCALATE, pwhy = 0; |
| 625 | if (g->authz.decide(g->authz.ctx, r, g->view, &verdict, &pwhy) != 0) |
| 626 | verdict = AIENOS_CONTAIN_ESCALATE; |
| 627 | if (verdict == AIENOS_CONTAIN_DENY) { |
| 628 | *status = AIENOS_CONTAIN_DENY; |
| 629 | *why = AIENOS_CONTAIN_WHY_POLICY_DENY; |
| 630 | return; |
| 631 | } |
| 632 | if (verdict != AIENOS_CONTAIN_GRANT) { |
| 633 | *status = AIENOS_CONTAIN_ESCALATE; |
| 634 | *why = AIENOS_CONTAIN_WHY_POLICY_ESCALATE; |
| 635 | return; |
| 636 | } |
| 637 | /* Policy said GRANT. Only a LIVE, auto-eligible revoke inside budget is granted. */ |
| 638 | if (!live || (r->flags & (AIENOS_CREQ_SYNTHETIC | AIENOS_CREQ_SATURATION))) { |
| 639 | *status = AIENOS_CONTAIN_ESCALATE; |
| 640 | *why = AIENOS_CONTAIN_WHY_NOT_AUTO_ELIGIBLE; |
| 641 | return; |
| 642 | } |
| 643 | uint64_t next_window = g->last_decision_id / AIENOS_CONTAIN_BUDGET_WINDOW; |
| 644 | uint32_t used = next_window == g->window ? g->window_grants : 0; |
| 645 | if (used >= AIENOS_CONTAIN_BUDGET) { |
| 646 | *status = AIENOS_CONTAIN_ESCALATE; |
| 647 | *why = AIENOS_CONTAIN_WHY_BUDGET; |
| 648 | return; |
| 649 | } |
| 650 | *status = AIENOS_CONTAIN_GRANT; |
| 651 | *why = AIENOS_CONTAIN_WHY_POLICY_GRANT; |
| 652 | } |
| 653 | |
| 654 | static int submit_checked(AienosContain *g, const AienosContainRequest *r, const uint8_t *digest, |
| 655 | bool bad, AienosContainDecision *out) { |
| 656 | AienosContainReceipt o; |
| 657 | pthread_mutex_lock(&g->lock); |
| 658 | if (bad || !request_well_formed(r) || !narrow_shape(r)) { |
| 659 | decide_out(g, r, digest, AIENOS_CONTAIN_DENY, AIENOS_CONTAIN_WHY_BAD_REQUEST, false, &o); |
| 660 | } else if (r->request_id <= g->last_request_id) { |
| 661 | decide_out(g, r, digest, AIENOS_CONTAIN_DENY, AIENOS_CONTAIN_WHY_REPLAYED_REQUEST, false, &o); |
| 662 | } else { |
| 663 | g->last_request_id = r->request_id; |
| 664 | uint8_t status; |
| 665 | uint16_t why; |
| 666 | decide_locked(g, r, &status, &why); |
| 667 | decide_out(g, r, digest, status, why, status != AIENOS_CONTAIN_DENY, &o); |
| 668 | } |
| 669 | pthread_mutex_unlock(&g->lock); |
| 670 | if (out) *out = o; |
| 671 | emit(g, &o); |
| 672 | return AIENOS_CONTAIN_OK; |
| 673 | } |
| 674 | |
| 675 | int aienos_contain_submit(AienosContain *g, const AienosContainRequest *r, |
| 676 | const uint8_t request_digest[AIENOS_CONTAIN_DIGEST_LEN], |
| 677 | AienosContainDecision *out) { |
| 678 | if (!g || !r || !request_digest) return AIENOS_CONTAIN_ERR_ARG; |
| 679 | uint8_t d[AIENOS_CONTAIN_DIGEST_LEN]; |
| 680 | aienos_contain_request_digest(r, d); |
| 681 | bool bad = memcmp(d, request_digest, sizeof d) != 0; /* tampered after digest */ |
| 682 | return submit_checked(g, r, d, bad, out); |
| 683 | } |
| 684 | |
| 685 | int aienos_contain_submit_bytes(AienosContain *g, const uint8_t *bytes, size_t len, |
| 686 | AienosContainDecision *out) { |
| 687 | if (!g || !bytes) return AIENOS_CONTAIN_ERR_ARG; |
| 688 | uint8_t d[AIENOS_CONTAIN_DIGEST_LEN]; |
| 689 | aienos_contain_sha256(bytes, len, d); |
| 690 | AienosContainRequest r; |
| 691 | memset(&r, 0, sizeof r); |
| 692 | bool bad = len != AIENOS_CONTAIN_REQUEST_SIZE || |
| 693 | aienos_contain_request_decode(bytes, &r) != AIENOS_CONTAIN_OK; |
| 694 | if (len != AIENOS_CONTAIN_REQUEST_SIZE) memset(&r, 0, sizeof r); |
| 695 | return submit_checked(g, &r, d, bad, out); |
| 696 | } |
| 697 | |
| 698 | /* ---- Resolve (the human, office secret) ---- */ |
| 699 | |
| 700 | int aienos_contain_resolve(AienosContain *g, uint64_t decision_id, int approve, |
| 701 | const uint8_t *office_secret, AienosContainDecision *out) { |
| 702 | if (!g) return AIENOS_CONTAIN_ERR_ARG; |
| 703 | AienosContainReceipt o; |
| 704 | int rc = AIENOS_CONTAIN_OK; |
| 705 | bool secret_ok = office_secret && aienos_cap_authorize(g->admin, office_secret) == AIENOS_CAP_OK; |
| 706 | pthread_mutex_lock(&g->lock); |
| 707 | Dec *d = dec_find(g, decision_id); |
| 708 | receipt_base(g, &o, d ? &d->req : NULL, d ? d->digest : NULL, AIENOS_CONTAIN_KIND_DECIDED); |
| 709 | o.decision_id = decision_id; |
| 710 | o.resource = decision_id; |
| 711 | o.actor = AIENOS_CONTAIN_ACTOR_OFFICE; |
| 712 | o.gate_flags |= AIENOS_CONTAIN_RF_HUMAN; |
| 713 | if (!d) { |
| 714 | o.status = AIENOS_CONTAIN_DENY; |
| 715 | o.why = AIENOS_CONTAIN_WHY_DECISION_REPLAY; |
| 716 | o.gate_flags |= AIENOS_CONTAIN_RF_REFUSED_CALL; |
| 717 | rc = AIENOS_CONTAIN_ERR_UNKNOWN; |
| 718 | } else if (!secret_ok) { |
| 719 | o.status = d->state == D_ESCALATED ? AIENOS_CONTAIN_ESCALATE : AIENOS_CONTAIN_DENY; |
| 720 | o.why = AIENOS_CONTAIN_WHY_BAD_SECRET; |
| 721 | o.gate_flags |= AIENOS_CONTAIN_RF_REFUSED_CALL; |
| 722 | rc = AIENOS_CONTAIN_ERR_REFUSED; |
| 723 | } else if (d->state != D_ESCALATED) { |
| 724 | o.status = AIENOS_CONTAIN_DENY; |
| 725 | o.why = AIENOS_CONTAIN_WHY_DECISION_REPLAY; |
| 726 | o.gate_flags |= AIENOS_CONTAIN_RF_REFUSED_CALL; |
| 727 | rc = AIENOS_CONTAIN_ERR_REFUSED; |
| 728 | } else if (approve) { |
| 729 | d->state = D_GRANTED; |
| 730 | d->human = 1; |
| 731 | if (d->req.containment == AIENOS_CONTAIN_REVOKE_CAPABILITY) g->window_grants++; |
| 732 | o.status = AIENOS_CONTAIN_GRANT; |
| 733 | o.why = AIENOS_CONTAIN_WHY_HUMAN_GRANT; |
| 734 | } else { |
| 735 | d->state = D_DENIED; |
| 736 | o.status = AIENOS_CONTAIN_DENY; |
| 737 | o.why = AIENOS_CONTAIN_WHY_HUMAN_DENY; |
| 738 | } |
| 739 | pthread_mutex_unlock(&g->lock); |
| 740 | if (out) *out = o; |
| 741 | emit(g, &o); |
| 742 | return rc; |
| 743 | } |
| 744 | |
| 745 | /* ---- Execute ---- */ |
| 746 | |
| 747 | int aienos_contain_execute(AienosContain *g, uint64_t decision_id, AienosContainResult *out) { |
| 748 | if (!g) return AIENOS_CONTAIN_ERR_ARG; |
| 749 | AienosContainReceipt o; |
| 750 | int rc = AIENOS_CONTAIN_OK; |
| 751 | pthread_mutex_lock(&g->lock); |
| 752 | Dec *d = dec_find(g, decision_id); |
| 753 | receipt_base(g, &o, d ? &d->req : NULL, d ? d->digest : NULL, AIENOS_CONTAIN_KIND_EXECUTED); |
| 754 | o.decision_id = decision_id; |
| 755 | if (d && d->human) o.gate_flags |= AIENOS_CONTAIN_RF_HUMAN; |
| 756 | if (!d || d->state != D_GRANTED) { |
| 757 | /* Unknown, not granted, or already executed: refused and announced. */ |
| 758 | o.status = AIENOS_CONTAIN_FAILED; |
| 759 | o.why = AIENOS_CONTAIN_WHY_DECISION_REPLAY; |
| 760 | o.gate_flags |= AIENOS_CONTAIN_RF_REFUSED_CALL; |
| 761 | rc = d ? AIENOS_CONTAIN_ERR_REFUSED : AIENOS_CONTAIN_ERR_UNKNOWN; |
| 762 | pthread_mutex_unlock(&g->lock); |
| 763 | if (out) *out = o; |
| 764 | emit(g, &o); |
| 765 | return rc; |
| 766 | } |
| 767 | d->state = D_EXECUTED; /* once, whatever happens next */ |
| 768 | AienosContainRequest req = d->req; |
| 769 | |
| 770 | if (req.containment != AIENOS_CONTAIN_REVOKE_CAPABILITY) { |
| 771 | /* No executor exists for this type (I2, I3.c). */ |
| 772 | uint32_t (*tx)(void *, const AienosContainRequest *) = |
| 773 | g->seams_on ? g->seams.test_executor : NULL; |
| 774 | void *tctx = g->seams.test_executor_ctx; |
| 775 | pthread_mutex_unlock(&g->lock); |
| 776 | uint32_t st = AIENOS_CONTAIN_UNAVAILABLE; |
| 777 | if (tx) { |
| 778 | st = tx(tctx, &req); |
| 779 | if (st < AIENOS_CONTAIN_DONE || st > AIENOS_CONTAIN_UNAVAILABLE) st = AIENOS_CONTAIN_FAILED; |
| 780 | o.flags |= AIENOS_CREQ_SYNTHETIC; /* test executor: always labelled (I2) */ |
| 781 | } |
| 782 | o.status = (uint8_t)st; |
| 783 | o.why = st == AIENOS_CONTAIN_UNAVAILABLE ? AIENOS_CONTAIN_WHY_NO_EXECUTOR : 0; |
| 784 | if (out) *out = o; |
| 785 | emit(g, &o); |
| 786 | return AIENOS_CONTAIN_OK; |
| 787 | } |
| 788 | |
| 789 | /* Re-check I1.a immediately before the revoke. */ |
| 790 | uint16_t why = 0; |
| 791 | uint8_t status = AIENOS_CONTAIN_FAILED; |
| 792 | bool ask; |
| 793 | if (g->inert || !g->lineage_trusted) why = AIENOS_CONTAIN_WHY_LINEAGE_UNTRUSTED; |
| 794 | else if (!executor_live(g)) { |
| 795 | why = AIENOS_CONTAIN_WHY_NO_EXECUTOR; |
| 796 | status = AIENOS_CONTAIN_UNAVAILABLE; |
| 797 | } else if (is_protected(g, &req)) why = AIENOS_CONTAIN_WHY_PROTECTED_TARGET; |
| 798 | else why = revoke_target_check(g, &req, &ask); |
| 799 | if (why) { |
| 800 | o.status = status; |
| 801 | o.why = why; |
| 802 | if (g->inert) o.gate_flags |= AIENOS_CONTAIN_RF_INERT; |
| 803 | pthread_mutex_unlock(&g->lock); |
| 804 | if (out) *out = o; |
| 805 | emit(g, &o); |
| 806 | return AIENOS_CONTAIN_OK; |
| 807 | } |
| 808 | void (*hook)(void *) = g->seams_on ? g->seams.pre_revoke : NULL; |
| 809 | void *hctx = g->seams.pre_revoke_ctx; |
| 810 | g->counting = 1; |
| 811 | g->revokes_seen = 0; |
| 812 | g->target_seen = 0; |
| 813 | g->exec_target = req.target; |
| 814 | AienosCapRef ex = g->executor; |
| 815 | pthread_mutex_unlock(&g->lock); |
| 816 | |
| 817 | if (hook) hook(hctx); |
| 818 | int arc = aienos_cap_revoke(g->admin, ex, req.target); |
| 819 | |
| 820 | pthread_mutex_lock(&g->lock); |
| 821 | g->counting = 0; |
| 822 | uint32_t seen = g->revokes_seen; |
| 823 | bool target_seen = g->target_seen; |
| 824 | o.rc = arc; |
| 825 | o.tick = aienos_cap_clock(g->view); |
| 826 | if (arc != AIENOS_CAP_OK) { |
| 827 | o.status = AIENOS_CONTAIN_FAILED; |
| 828 | o.resource = 1ull << 32; /* one refused, none revoked */ |
| 829 | } else if (seen == 1 && target_seen) { |
| 830 | o.status = AIENOS_CONTAIN_DONE; |
| 831 | o.resource = 1; |
| 832 | } else { |
| 833 | /* Cascade, or the observer feed is not wired: I1.b cannot be shown. */ |
| 834 | g->inert = 1; |
| 835 | o.status = AIENOS_CONTAIN_FAILED_SCOPE; |
| 836 | o.resource = seen; |
| 837 | o.gate_flags |= AIENOS_CONTAIN_RF_INERT; |
| 838 | } |
| 839 | pthread_mutex_unlock(&g->lock); |
| 840 | if (out) *out = o; |
| 841 | emit(g, &o); |
| 842 | return AIENOS_CONTAIN_OK; |
| 843 | } |