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/aienos_capability.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 / aienos_capability.c` 734 lines ยท 30642 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 * aienos_capability.c -- native AIENOS capability authority. 19 * 20 * AienosCapView can check a reference. It cannot mint, revoke, reclaim, move 21 * the clock, or change the epoch. Those operations take AienosCapAdmin, and 22 * they refuse to run unless the caller presents a live reference holding the 23 * matching office right. The office token is held beside the table, never in 24 * it; no operation here checks it (see aienos_cap_authorize). 25 */ 26 #include "aienos_capability.h" 27 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 37 /* Production ABI layout (LP64: aarch64, x86_64). These values are the binary 38 * contract omega and every other C caller compile against; a change to any 39 * public struct must fail the build here first. The Rust crates use a 40 * different, 32-bit layout under their own aienos_cap32_* symbols. */ 41 _Static_assert(AIENOS_CAP_GENERATION_BITS == 64, "generation width"); 42 _Static_assert(sizeof(((AienosCapRef *)0)->generation) * 8 == AIENOS_CAP_GENERATION_BITS, 43 "AienosCapRef.generation width"); 44 _Static_assert(sizeof(AienosCapRef) == 16, "AienosCapRef size"); 45 _Static_assert(_Alignof(AienosCapRef) == 8, "AienosCapRef align"); 46 _Static_assert(offsetof(AienosCapRef, cap_id) == 0, "AienosCapRef.cap_id offset"); 47 _Static_assert(offsetof(AienosCapRef, generation) == 8, "AienosCapRef.generation offset"); 48 _Static_assert(sizeof(AienosCapEntry) == 88, "AienosCapEntry size"); 49 _Static_assert(offsetof(AienosCapEntry, cap_id) == 0, "AienosCapEntry.cap_id"); 50 _Static_assert(offsetof(AienosCapEntry, generation) == 8, "AienosCapEntry.generation"); 51 _Static_assert(offsetof(AienosCapEntry, state) == 16, "AienosCapEntry.state"); 52 _Static_assert(offsetof(AienosCapEntry, issuer) == 20, "AienosCapEntry.issuer"); 53 _Static_assert(offsetof(AienosCapEntry, subject) == 24, "AienosCapEntry.subject"); 54 _Static_assert(offsetof(AienosCapEntry, rights) == 28, "AienosCapEntry.rights"); 55 _Static_assert(offsetof(AienosCapEntry, resource) == 32, "AienosCapEntry.resource"); 56 _Static_assert(offsetof(AienosCapEntry, epoch) == 40, "AienosCapEntry.epoch"); 57 _Static_assert(offsetof(AienosCapEntry, lease_expiry) == 48, "AienosCapEntry.lease_expiry"); 58 _Static_assert(offsetof(AienosCapEntry, parent_id) == 56, "AienosCapEntry.parent_id"); 59 _Static_assert(offsetof(AienosCapEntry, parent_generation) == 64, 60 "AienosCapEntry.parent_generation"); 61 _Static_assert(offsetof(AienosCapEntry, minted_by_id) == 72, "AienosCapEntry.minted_by_id"); 62 _Static_assert(offsetof(AienosCapEntry, minted_by_generation) == 80, 63 "AienosCapEntry.minted_by_generation"); 64 _Static_assert(sizeof(AienosCapMint) == 64, "AienosCapMint size"); 65 _Static_assert(offsetof(AienosCapMint, issuer) == 0, "AienosCapMint.issuer"); 66 _Static_assert(offsetof(AienosCapMint, subject) == 4, "AienosCapMint.subject"); 67 _Static_assert(offsetof(AienosCapMint, resource) == 8, "AienosCapMint.resource"); 68 _Static_assert(offsetof(AienosCapMint, rights) == 16, "AienosCapMint.rights"); 69 _Static_assert(offsetof(AienosCapMint, lease_ticks) == 24, "AienosCapMint.lease_ticks"); 70 _Static_assert(offsetof(AienosCapMint, parent) == 32, "AienosCapMint.parent"); 71 _Static_assert(offsetof(AienosCapMint, authority) == 48, "AienosCapMint.authority"); 72 73 typedef struct { 74 uint64_t boot_gen; 75 uint64_t epoch; 76 uint64_t clock; 77 AienosCapEntry entries[AIENOS_CAP_MAX]; 78 bool delivered[AIENOS_CAP_MAX]; 79 bool writer_alive; 80 } CapState; 81 82 typedef struct { 83 pthread_mutex_t state_lock; 84 CapState state; 85 pthread_mutex_t token_lock; 86 uint8_t token[AIENOS_CAP_TOKEN_LEN]; 87 atomic_uint holders; 88 } CapShared; 89 90 /* The observer lives on the admin handle, so the view cannot reach it. 91 * Both fields are read and written under shared->state_lock. */ 92 struct AienosCapAdmin { 93 CapShared *shared; 94 AienosCapObserver observer; 95 void *observer_ctx; 96 }; 97 98 struct AienosCapView { 99 CapShared *shared; 100 }; 101 102 /* A later authority starts higher, so a reference from a stopped authority 103 * cannot validate against the new one. The start is the largest of: one 104 * past the last start handed out in this process, one past every 105 * generation the replaced table used (`floor`), and the time since boot in 106 * nanoseconds shifted left by 8. The time term keeps a new process above an 107 * old one on the same boot: a slot would need more than 256 reclaims per 108 * nanosecond to catch up with it. */ 109 static _Atomic uint64_t last_boot_gen = 0; 110 111 static uint64_t boot_time_seed(void) { 112 struct timespec ts; 113 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) return 0; 114 uint64_t ns = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 115 if (ns > (UINT64_MAX >> 9)) return 0; 116 return ns << 8; 117 } 118 119 static int take_boot_gen(uint64_t floor, uint64_t *out) { 120 uint64_t seed = boot_time_seed(); 121 uint64_t cur = atomic_load_explicit(&last_boot_gen, memory_order_relaxed); 122 for (;;) { 123 if (cur == UINT64_MAX) return AIENOS_CAP_ERR_EXHAUSTED; 124 uint64_t next = cur + 1; 125 if (floor > next) next = floor; 126 if (seed > next) next = seed; 127 if (next == UINT64_MAX) return AIENOS_CAP_ERR_EXHAUSTED; 128 if (atomic_compare_exchange_weak_explicit(&last_boot_gen, &cur, next, 129 memory_order_acq_rel, 130 memory_order_relaxed)) { 131 *out = next; 132 return AIENOS_CAP_OK; 133 } 134 } 135 } 136 137 int aienos_cap_generation_advance(uint64_t generation, uint64_t *out) { 138 if (generation == UINT64_MAX) return AIENOS_CAP_ERR_EXHAUSTED; 139 if (out) *out = generation + 1; 140 return AIENOS_CAP_OK; 141 } 142 143 static int add_u64(uint64_t a, uint64_t b, uint64_t *out) { 144 if (b > UINT64_MAX - a) return AIENOS_CAP_ERR_OVERFLOW; 145 *out = a + b; 146 return AIENOS_CAP_OK; 147 } 148 149 static bool expired(const CapState *s, const AienosCapEntry *e) { 150 return e->lease_expiry != 0 && s->clock >= e->lease_expiry; 151 } 152 153 /* One past the largest generation anywhere in the table, including the 154 * generations recorded for parents and minters. */ 155 static uint64_t table_floor(const CapState *s) { 156 uint64_t top = s->boot_gen; 157 for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { 158 const AienosCapEntry *e = &s->entries[i]; 159 if (e->generation > top) top = e->generation; 160 if (e->parent_generation > top) top = e->parent_generation; 161 if (e->minted_by_generation > top) top = e->minted_by_generation; 162 } 163 return top == UINT64_MAX ? UINT64_MAX : top + 1; 164 } 165 166 static int bootstrap(CapState *s, uint64_t boot_gen) { 167 if (boot_gen == 0 || boot_gen == UINT64_MAX) return AIENOS_CAP_ERR_EXHAUSTED; 168 memset(s, 0, sizeof *s); 169 s->boot_gen = boot_gen; 170 s->epoch = 1; 171 s->clock = 0; 172 s->writer_alive = true; 173 for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { 174 s->entries[i].cap_id = i; 175 s->entries[i].generation = boot_gen; 176 s->entries[i].state = AIENOS_CAP_STATE_FREE; 177 s->entries[i].parent_id = AIENOS_CAP_PARENT_NONE; 178 } 179 AienosCapEntry *office = &s->entries[0]; 180 office->state = AIENOS_CAP_STATE_LIVE; 181 office->resource = AIENOS_CAP_RES_AUTHORITY; 182 office->rights = AIENOS_CAP_RIGHT_PRIVILEGED; 183 office->epoch = 1; 184 s->delivered[0] = true; 185 return AIENOS_CAP_OK; 186 } 187 188 static int state_inspect(const CapState *s, AienosCapRef cap, AienosCapEntry *out) { 189 if (cap.cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 190 const AienosCapEntry *e = &s->entries[cap.cap_id]; 191 if (e->generation != cap.generation) return AIENOS_CAP_ERR_STALE_GEN; 192 if (e->state == AIENOS_CAP_STATE_FREE) return AIENOS_CAP_ERR_STATE; 193 *out = *e; 194 return AIENOS_CAP_OK; 195 } 196 197 static int state_validate(const CapState *s, AienosCapRef cap, uint32_t subject, 198 uint64_t resource, uint32_t rights, AienosCapEntry *out) { 199 if (cap.cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 200 AienosCapEntry chain[AIENOS_CAP_MAX_DEPTH + 1]; 201 uint32_t n = 0; 202 uint32_t id = cap.cap_id; 203 while (id != AIENOS_CAP_PARENT_NONE) { 204 if (n > AIENOS_CAP_MAX_DEPTH || id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_CHAIN; 205 chain[n] = s->entries[id]; 206 id = chain[n].parent_id; 207 n++; 208 } 209 const AienosCapEntry *e = &chain[0]; 210 if (e->generation != cap.generation) return AIENOS_CAP_ERR_STALE_GEN; 211 if (e->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_REVOKED; 212 if (e->epoch != s->epoch) return AIENOS_CAP_ERR_EPOCH; 213 if (expired(s, e)) return AIENOS_CAP_ERR_EXPIRED; 214 for (uint32_t i = 1; i < n; i++) { 215 const AienosCapEntry *parent = &chain[i]; 216 const AienosCapEntry *child = &chain[i - 1]; 217 if (parent->generation != child->parent_generation) return AIENOS_CAP_ERR_CHAIN; 218 if (parent->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_CHAIN; 219 if (parent->epoch != s->epoch) return AIENOS_CAP_ERR_CHAIN; 220 if (expired(s, parent)) return AIENOS_CAP_ERR_CHAIN; 221 } 222 if (e->subject != subject) return AIENOS_CAP_ERR_SUBJECT; 223 if (e->resource != resource) return AIENOS_CAP_ERR_RESOURCE; 224 if ((e->rights & rights) != rights) return AIENOS_CAP_ERR_RIGHTS; 225 if (out) *out = *e; 226 return AIENOS_CAP_OK; 227 } 228 229 static int chain_ok(const CapState *s, uint32_t entry_id) { 230 uint32_t depth = 0; 231 for (;;) { 232 const AienosCapEntry *e = &s->entries[entry_id]; 233 if (e->parent_id == AIENOS_CAP_PARENT_NONE) return AIENOS_CAP_OK; 234 depth++; 235 if (depth > AIENOS_CAP_MAX_DEPTH) return AIENOS_CAP_ERR_CHAIN; 236 if (e->parent_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_CHAIN; 237 const AienosCapEntry *parent = &s->entries[e->parent_id]; 238 if (parent->generation != e->parent_generation || 239 parent->state != AIENOS_CAP_STATE_LIVE) 240 return AIENOS_CAP_ERR_CHAIN; 241 entry_id = e->parent_id; 242 } 243 } 244 245 static uint32_t ancestor_hops(const CapState *s, uint32_t entry_id) { 246 uint32_t hops = 0; 247 for (;;) { 248 const AienosCapEntry *e = &s->entries[entry_id]; 249 if (e->parent_id == AIENOS_CAP_PARENT_NONE || hops > AIENOS_CAP_MAX_DEPTH) return hops; 250 if (e->parent_id >= AIENOS_CAP_MAX) return AIENOS_CAP_MAX_DEPTH + 1; 251 hops++; 252 entry_id = e->parent_id; 253 } 254 } 255 256 /* The caller must present a delivered, live, current reference that holds 257 * every right in `need`. A privileged right is an authority-office right: 258 * it is honored only on an entry whose resource is AIENOS_CAP_RES_AUTHORITY, 259 * so a capability that carries (say) the epoch right on some other resource, 260 * such as an operator control resource, cannot step the authority (#266). */ 261 static int auth_use(const CapState *s, AienosCapRef authority, uint32_t need, uint32_t *index) { 262 if (authority.cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_UNAUTHORIZED; 263 uint32_t i = authority.cap_id; 264 if (!s->delivered[i]) return AIENOS_CAP_ERR_UNAUTHORIZED; 265 const AienosCapEntry *e = &s->entries[i]; 266 if (e->generation != authority.generation) return AIENOS_CAP_ERR_STALE_GEN; 267 if (e->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_REVOKED; 268 if (e->epoch != s->epoch) return AIENOS_CAP_ERR_EPOCH; 269 if (expired(s, e)) return AIENOS_CAP_ERR_EXPIRED; 270 int rc = chain_ok(s, i); 271 if (rc != AIENOS_CAP_OK) return rc; 272 if ((e->rights & need) != need) return AIENOS_CAP_ERR_UNAUTHORIZED; 273 if ((need & AIENOS_CAP_RIGHT_PRIVILEGED) && e->resource != AIENOS_CAP_RES_AUTHORITY) 274 return AIENOS_CAP_ERR_RESOURCE; 275 *index = i; 276 return AIENOS_CAP_OK; 277 } 278 279 static int lease_expiry(const CapState *s, uint64_t ticks, uint64_t *out) { 280 if (ticks == 0) { 281 *out = 0; 282 return AIENOS_CAP_OK; 283 } 284 return add_u64(s->clock, ticks, out); 285 } 286 287 static int state_mint(CapState *s, const AienosCapMint *r, AienosCapRef *out) { 288 if (!s->writer_alive) return AIENOS_CAP_ERR_IO; 289 if (r->rights == 0 || (r->rights & ~AIENOS_CAP_RIGHT_KNOWN) != 0) return AIENOS_CAP_ERR_RIGHTS; 290 if ((r->rights & AIENOS_CAP_RIGHT_PRIVILEGED) && (r->rights & AIENOS_CAP_RIGHT_DELEGATE)) 291 return AIENOS_CAP_ERR_NOT_DELEGABLE; 292 293 uint32_t auth_index; 294 int rc; 295 if (r->parent.cap_id == AIENOS_CAP_PARENT_NONE) { 296 rc = auth_use(s, r->authority, AIENOS_CAP_RIGHT_MINT, &auth_index); 297 if (rc != AIENOS_CAP_OK) return rc; 298 const AienosCapEntry *auth = &s->entries[auth_index]; 299 if ((r->rights & AIENOS_CAP_RIGHT_PRIVILEGED) & ~auth->rights) 300 return AIENOS_CAP_ERR_UNAUTHORIZED; 301 } else { 302 if (r->authority.cap_id != r->parent.cap_id || 303 r->authority.generation != r->parent.generation) 304 return AIENOS_CAP_ERR_UNAUTHORIZED; 305 rc = auth_use(s, r->authority, 0, &auth_index); 306 if (rc != AIENOS_CAP_OK) return rc; 307 const AienosCapEntry *auth = &s->entries[auth_index]; 308 if (!(auth->rights & AIENOS_CAP_RIGHT_DELEGATE)) return AIENOS_CAP_ERR_NOT_DELEGABLE; 309 if (r->parent.cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 310 if (auth->resource != r->resource) return AIENOS_CAP_ERR_RESOURCE; 311 if (r->rights & ~auth->rights) return AIENOS_CAP_ERR_AMPLIFY; 312 if (r->rights & AIENOS_CAP_RIGHT_PRIVILEGED) return AIENOS_CAP_ERR_NOT_DELEGABLE; 313 if (ancestor_hops(s, r->parent.cap_id) >= AIENOS_CAP_MAX_DEPTH) 314 return AIENOS_CAP_ERR_CHAIN; 315 if (auth->lease_expiry != 0) { 316 uint64_t child; 317 rc = lease_expiry(s, r->lease_ticks, &child); 318 if (rc != AIENOS_CAP_OK) return rc; 319 if (child == 0 || child > auth->lease_expiry) return AIENOS_CAP_ERR_AMPLIFY; 320 } 321 } 322 uint64_t expiry; 323 rc = lease_expiry(s, r->lease_ticks, &expiry); 324 if (rc != AIENOS_CAP_OK) return rc; 325 const AienosCapEntry auth = s->entries[auth_index]; 326 bool saw_exhausted = false; 327 for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { 328 AienosCapEntry *e = &s->entries[i]; 329 if (e->state != AIENOS_CAP_STATE_FREE) continue; 330 if (e->generation == UINT64_MAX) { 331 saw_exhausted = true; 332 continue; 333 } 334 e->state = AIENOS_CAP_STATE_LIVE; 335 e->issuer = r->issuer; 336 e->subject = r->subject; 337 e->resource = r->resource; 338 e->rights = r->rights; 339 e->epoch = s->epoch; 340 e->lease_expiry = expiry; 341 e->parent_id = r->parent.cap_id; 342 e->parent_generation = 343 r->parent.cap_id == AIENOS_CAP_PARENT_NONE ? 0 : r->parent.generation; 344 e->minted_by_id = auth.cap_id; 345 e->minted_by_generation = auth.generation; 346 s->delivered[i] = true; 347 if (out) *out = (AienosCapRef){e->cap_id, e->generation}; 348 return AIENOS_CAP_OK; 349 } 350 return saw_exhausted ? AIENOS_CAP_ERR_EXHAUSTED : AIENOS_CAP_ERR_FULL; 351 } 352 353 /* Revokes every live descendant of a revoked entry. When `ids` is not 354 * NULL, appends each slot it revokes, in the order revoked; an ancestor is 355 * always revoked in an earlier pass or earlier in the same pass than its 356 * descendants. */ 357 static void cascade_revoke(CapState *s, uint32_t *ids, uint32_t *n) { 358 for (uint32_t guard = 0; guard < AIENOS_CAP_MAX; guard++) { 359 bool changed = false; 360 for (uint32_t i = 0; i < AIENOS_CAP_MAX; i++) { 361 AienosCapEntry *e = &s->entries[i]; 362 uint32_t pid = e->parent_id; 363 if (e->state != AIENOS_CAP_STATE_LIVE || pid == AIENOS_CAP_PARENT_NONE) continue; 364 if (pid >= AIENOS_CAP_MAX) continue; 365 const AienosCapEntry *parent = &s->entries[pid]; 366 if (parent->generation == e->parent_generation && 367 parent->state == AIENOS_CAP_STATE_REVOKED) { 368 e->state = AIENOS_CAP_STATE_REVOKED; 369 if (ids) ids[(*n)++] = i; 370 changed = true; 371 } 372 } 373 if (!changed) break; 374 } 375 } 376 377 static int state_revoke(CapState *s, AienosCapRef authority, AienosCapRef target, 378 uint32_t *ids, uint32_t *n) { 379 if (!s->writer_alive) return AIENOS_CAP_ERR_IO; 380 uint32_t auth_index; 381 int rc = auth_use(s, authority, AIENOS_CAP_RIGHT_REVOKE, &auth_index); 382 if (rc != AIENOS_CAP_OK) return rc; 383 if (target.cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 384 uint32_t auth_rights = s->entries[auth_index].rights; 385 AienosCapEntry *e = &s->entries[target.cap_id]; 386 if (e->generation != target.generation) return AIENOS_CAP_ERR_STALE_GEN; 387 if (e->state != AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_STATE; 388 if ((e->rights & AIENOS_CAP_RIGHT_PRIVILEGED) & ~auth_rights) return AIENOS_CAP_ERR_UNAUTHORIZED; 389 e->state = AIENOS_CAP_STATE_REVOKED; 390 if (ids) ids[(*n)++] = target.cap_id; 391 cascade_revoke(s, ids, n); 392 return AIENOS_CAP_OK; 393 } 394 395 static int state_reclaim(CapState *s, AienosCapRef authority, uint32_t cap_id) { 396 if (!s->writer_alive) return AIENOS_CAP_ERR_IO; 397 uint32_t auth_index; 398 int rc = auth_use(s, authority, AIENOS_CAP_RIGHT_RECLAIM, &auth_index); 399 if (rc != AIENOS_CAP_OK) return rc; 400 if (cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 401 AienosCapEntry *e = &s->entries[cap_id]; 402 if (e->state != AIENOS_CAP_STATE_REVOKED) return AIENOS_CAP_ERR_STATE; 403 uint64_t next; 404 rc = aienos_cap_generation_advance(e->generation, &next); 405 if (rc != AIENOS_CAP_OK) return rc; 406 e->generation = next; 407 e->state = AIENOS_CAP_STATE_FREE; 408 e->rights = 0; 409 e->lease_expiry = 0; 410 e->parent_id = AIENOS_CAP_PARENT_NONE; 411 e->parent_generation = 0; 412 s->delivered[cap_id] = false; 413 return AIENOS_CAP_OK; 414 } 415 416 static int state_advance_clock(CapState *s, AienosCapRef authority, uint64_t ticks) { 417 if (!s->writer_alive) return AIENOS_CAP_ERR_IO; 418 uint32_t idx; 419 int rc = auth_use(s, authority, AIENOS_CAP_RIGHT_CLOCK, &idx); 420 if (rc != AIENOS_CAP_OK) return rc; 421 return add_u64(s->clock, ticks, &s->clock); 422 } 423 424 static int state_bump_epoch(CapState *s, AienosCapRef authority) { 425 if (!s->writer_alive) return AIENOS_CAP_ERR_IO; 426 uint32_t idx; 427 int rc = auth_use(s, authority, AIENOS_CAP_RIGHT_EPOCH, &idx); 428 if (rc != AIENOS_CAP_OK) return rc; 429 return add_u64(s->epoch, 1, &s->epoch); 430 } 431 432 /* Test seam. Puts a free or revoked slot on a chosen generation so the 433 * exhaustion rule can be shown without 2^64 reclamations. Refuses a 434 * live slot, so it cannot mint a forged generation. */ 435 static int state_force_generation(CapState *s, uint32_t cap_id, uint64_t generation) { 436 if (cap_id >= AIENOS_CAP_MAX) return AIENOS_CAP_ERR_BOUNDS; 437 AienosCapEntry *e = &s->entries[cap_id]; 438 if (e->state == AIENOS_CAP_STATE_LIVE) return AIENOS_CAP_ERR_STATE; 439 if (generation == 0) return AIENOS_CAP_ERR_EXHAUSTED; 440 e->generation = generation; 441 return AIENOS_CAP_OK; 442 } 443 444 static bool fill_token(uint8_t token[AIENOS_CAP_TOKEN_LEN]) { 445 FILE *f = fopen("/dev/urandom", "rb"); 446 if (!f) return false; 447 size_t got = fread(token, 1, AIENOS_CAP_TOKEN_LEN, f); 448 fclose(f); 449 return got == AIENOS_CAP_TOKEN_LEN; 450 } 451 452 static void release(CapShared *shared) { 453 if (atomic_fetch_sub_explicit(&shared->holders, 1, memory_order_acq_rel) == 1) { 454 pthread_mutex_destroy(&shared->state_lock); 455 pthread_mutex_destroy(&shared->token_lock); 456 memset(shared->token, 0, sizeof shared->token); 457 free(shared); 458 } 459 } 460 461 #define LOCKED(shared, expr) \ 462 ({ \ 463 pthread_mutex_lock(&(shared)->state_lock); \ 464 __typeof__(expr) locked_rc_ = (expr); \ 465 pthread_mutex_unlock(&(shared)->state_lock); \ 466 locked_rc_; \ 467 }) 468 469 int aienos_cap_start(AienosCapAdmin **admin_out, AienosCapView **view_out) { 470 if (!admin_out || !view_out) return AIENOS_CAP_ERR_STATE; 471 uint64_t boot; 472 int rc = take_boot_gen(0, &boot); 473 if (rc != AIENOS_CAP_OK) return rc; 474 CapShared *shared = calloc(1, sizeof *shared); 475 AienosCapAdmin *admin = calloc(1, sizeof *admin); 476 AienosCapView *view = calloc(1, sizeof *view); 477 if (!shared || !admin || !view) { 478 free(shared); 479 free(admin); 480 free(view); 481 return AIENOS_CAP_ERR_IO; 482 } 483 rc = bootstrap(&shared->state, boot); 484 if (rc == AIENOS_CAP_OK && !fill_token(shared->token)) rc = AIENOS_CAP_ERR_IO; 485 if (rc != AIENOS_CAP_OK) { 486 free(shared); 487 free(admin); 488 free(view); 489 return rc; 490 } 491 pthread_mutex_init(&shared->state_lock, NULL); 492 pthread_mutex_init(&shared->token_lock, NULL); 493 atomic_init(&shared->holders, 2); 494 admin->shared = shared; 495 view->shared = shared; 496 *admin_out = admin; 497 *view_out = view; 498 return AIENOS_CAP_OK; 499 } 500 501 void aienos_cap_stop(AienosCapAdmin *admin, AienosCapView *view) { 502 if (admin) { 503 release(admin->shared); 504 free(admin); 505 } 506 if (view) { 507 release(view->shared); 508 free(view); 509 } 510 } 511 512 int aienos_cap_office(const AienosCapAdmin *admin, AienosCapRef *out) { 513 if (!admin) return AIENOS_CAP_ERR_STATE; 514 CapShared *sh = admin->shared; 515 pthread_mutex_lock(&sh->state_lock); 516 AienosCapRef office = {0, sh->state.entries[0].generation}; 517 pthread_mutex_unlock(&sh->state_lock); 518 if (out) *out = office; 519 return AIENOS_CAP_OK; 520 } 521 522 static void announce(AienosCapObserver fn, void *ctx, uint32_t op, const AienosCapEntry *entry, 523 int rc) { 524 if (fn) fn(ctx, op, entry, rc); 525 } 526 527 int aienos_cap_set_observer(AienosCapAdmin *admin, AienosCapObserver fn, void *ctx) { 528 if (!admin) return AIENOS_CAP_ERR_STATE; 529 CapShared *sh = admin->shared; 530 pthread_mutex_lock(&sh->state_lock); 531 admin->observer = fn; 532 admin->observer_ctx = fn ? ctx : NULL; 533 pthread_mutex_unlock(&sh->state_lock); 534 return AIENOS_CAP_OK; 535 } 536 537 /* Each admin operation below: change the table under the lock, copy the 538 * observer and the changed entries while still holding it, release, then 539 * announce. The observer never runs under the table lock. */ 540 541 int aienos_cap_mint(AienosCapAdmin *admin, const AienosCapMint *request, AienosCapRef *out) { 542 if (!admin || !request) return AIENOS_CAP_ERR_STATE; 543 AienosCapMint r = *request; 544 AienosCapRef got; 545 CapShared *sh = admin->shared; 546 AienosCapEntry snap; 547 pthread_mutex_lock(&sh->state_lock); 548 int rc = state_mint(&sh->state, &r, &got); 549 AienosCapObserver fn = admin->observer; 550 void *ctx = admin->observer_ctx; 551 if (fn && rc == AIENOS_CAP_OK) snap = sh->state.entries[got.cap_id]; 552 pthread_mutex_unlock(&sh->state_lock); 553 announce(fn, ctx, AIENOS_CAP_OBS_MINT, rc == AIENOS_CAP_OK ? &snap : NULL, rc); 554 if (rc == AIENOS_CAP_OK && out) *out = got; 555 return rc; 556 } 557 558 int aienos_cap_revoke(AienosCapAdmin *admin, AienosCapRef authority, AienosCapRef target) { 559 if (!admin) return AIENOS_CAP_ERR_STATE; 560 CapShared *sh = admin->shared; 561 uint32_t ids[AIENOS_CAP_MAX]; 562 uint32_t n = 0; 563 pthread_mutex_lock(&sh->state_lock); 564 AienosCapObserver fn = admin->observer; 565 void *ctx = admin->observer_ctx; 566 int rc = state_revoke(&sh->state, authority, target, fn ? ids : NULL, &n); 567 if (!fn) { 568 pthread_mutex_unlock(&sh->state_lock); 569 return rc; 570 } 571 /* A revoke can take every slot with it, so the copies are made here, 572 * under the lock: a reclaim on another thread could free a slot before 573 * the observer runs. */ 574 AienosCapEntry snaps[AIENOS_CAP_MAX]; 575 for (uint32_t i = 0; i < n; i++) snaps[i] = sh->state.entries[ids[i]]; 576 pthread_mutex_unlock(&sh->state_lock); 577 if (rc != AIENOS_CAP_OK) { 578 announce(fn, ctx, AIENOS_CAP_OBS_REVOKE, NULL, rc); 579 return rc; 580 } 581 for (uint32_t i = 0; i < n; i++) announce(fn, ctx, AIENOS_CAP_OBS_REVOKE, &snaps[i], rc); 582 return rc; 583 } 584 585 int aienos_cap_reclaim(AienosCapAdmin *admin, AienosCapRef authority, uint32_t cap_id) { 586 if (!admin) return AIENOS_CAP_ERR_STATE; 587 CapShared *sh = admin->shared; 588 AienosCapEntry snap; 589 pthread_mutex_lock(&sh->state_lock); 590 int rc = state_reclaim(&sh->state, authority, cap_id); 591 AienosCapObserver fn = admin->observer; 592 void *ctx = admin->observer_ctx; 593 if (fn && rc == AIENOS_CAP_OK) snap = sh->state.entries[cap_id]; 594 pthread_mutex_unlock(&sh->state_lock); 595 announce(fn, ctx, AIENOS_CAP_OBS_RECLAIM, rc == AIENOS_CAP_OK ? &snap : NULL, rc); 596 return rc; 597 } 598 599 int aienos_cap_advance_clock(AienosCapAdmin *admin, AienosCapRef authority, uint64_t ticks) { 600 if (!admin) return AIENOS_CAP_ERR_STATE; 601 CapShared *sh = admin->shared; 602 AienosCapEntry snap; 603 pthread_mutex_lock(&sh->state_lock); 604 int rc = state_advance_clock(&sh->state, authority, ticks); 605 AienosCapObserver fn = admin->observer; 606 void *ctx = admin->observer_ctx; 607 if (fn && rc == AIENOS_CAP_OK) snap = sh->state.entries[authority.cap_id]; 608 pthread_mutex_unlock(&sh->state_lock); 609 announce(fn, ctx, AIENOS_CAP_OBS_CLOCK, rc == AIENOS_CAP_OK ? &snap : NULL, rc); 610 return rc; 611 } 612 613 int aienos_cap_bump_epoch(AienosCapAdmin *admin, AienosCapRef authority) { 614 if (!admin) return AIENOS_CAP_ERR_STATE; 615 CapShared *sh = admin->shared; 616 AienosCapEntry snap; 617 pthread_mutex_lock(&sh->state_lock); 618 int rc = state_bump_epoch(&sh->state, authority); 619 AienosCapObserver fn = admin->observer; 620 void *ctx = admin->observer_ctx; 621 if (fn && rc == AIENOS_CAP_OK) snap = sh->state.entries[authority.cap_id]; 622 pthread_mutex_unlock(&sh->state_lock); 623 announce(fn, ctx, AIENOS_CAP_OBS_EPOCH, rc == AIENOS_CAP_OK ? &snap : NULL, rc); 624 return rc; 625 } 626 627 int aienos_cap_kill(AienosCapAdmin *admin) { 628 if (!admin) return AIENOS_CAP_ERR_STATE; 629 CapShared *sh = admin->shared; 630 pthread_mutex_lock(&sh->state_lock); 631 sh->state.writer_alive = false; 632 AienosCapObserver fn = admin->observer; 633 void *ctx = admin->observer_ctx; 634 pthread_mutex_unlock(&sh->state_lock); 635 announce(fn, ctx, AIENOS_CAP_OBS_KILL, NULL, AIENOS_CAP_OK); 636 return AIENOS_CAP_OK; 637 } 638 639 /* Replace the table with the next boot generation. The new table starts 640 * above every generation the old one used, so outstanding references fail 641 * against it even after reclaims. The office token is replaced. The 642 * observer is on the admin handle, so it stays set across a restart. */ 643 int aienos_cap_restart(AienosCapAdmin *admin) { 644 if (!admin) return AIENOS_CAP_ERR_STATE; 645 CapState *fresh = malloc(sizeof *fresh); 646 if (!fresh) return AIENOS_CAP_ERR_IO; 647 uint8_t token[AIENOS_CAP_TOKEN_LEN]; 648 if (!fill_token(token)) { 649 free(fresh); 650 return AIENOS_CAP_ERR_IO; 651 } 652 CapShared *sh = admin->shared; 653 AienosCapEntry snap; 654 pthread_mutex_lock(&sh->state_lock); 655 uint64_t boot; 656 int rc = take_boot_gen(table_floor(&sh->state), &boot); 657 if (rc == AIENOS_CAP_OK) rc = bootstrap(fresh, boot); 658 if (rc == AIENOS_CAP_OK) sh->state = *fresh; 659 AienosCapObserver fn = admin->observer; 660 void *ctx = admin->observer_ctx; 661 if (fn && rc == AIENOS_CAP_OK) snap = sh->state.entries[0]; 662 pthread_mutex_unlock(&sh->state_lock); 663 free(fresh); 664 if (rc == AIENOS_CAP_OK) { 665 pthread_mutex_lock(&sh->token_lock); 666 memcpy(sh->token, token, sizeof token); 667 pthread_mutex_unlock(&sh->token_lock); 668 } 669 memset(token, 0, sizeof token); 670 announce(fn, ctx, AIENOS_CAP_OBS_RESTART, rc == AIENOS_CAP_OK ? &snap : NULL, rc); 671 return rc; 672 } 673 674 int aienos_cap_validate(const AienosCapView *view, AienosCapRef cap, uint32_t subject, 675 uint64_t resource, uint32_t rights, AienosCapEntry *out) { 676 if (!view) return AIENOS_CAP_ERR_STATE; 677 AienosCapEntry e; 678 int rc = LOCKED(view->shared, 679 state_validate(&view->shared->state, cap, subject, resource, rights, &e)); 680 if (rc == AIENOS_CAP_OK && out) *out = e; 681 return rc; 682 } 683 684 int aienos_cap_inspect(const AienosCapView *view, AienosCapRef cap, AienosCapEntry *out) { 685 if (!view) return AIENOS_CAP_ERR_STATE; 686 AienosCapEntry e; 687 int rc = LOCKED(view->shared, state_inspect(&view->shared->state, cap, &e)); 688 if (rc == AIENOS_CAP_OK && out) *out = e; 689 return rc; 690 } 691 692 uint64_t aienos_cap_clock(const AienosCapView *view) { 693 if (!view) return 0; 694 CapShared *sh = view->shared; 695 pthread_mutex_lock(&sh->state_lock); 696 uint64_t clock = sh->state.clock; 697 pthread_mutex_unlock(&sh->state_lock); 698 return clock; 699 } 700 701 /* Cognition's mint attempt. The view has no office token, so this cannot 702 * change the table no matter what reference or token bytes are supplied. */ 703 int aienos_cap_cognition_mint(const AienosCapView *view, const AienosCapMint *request, 704 const uint8_t *token) { 705 (void)request; 706 (void)token; 707 if (!view) return AIENOS_CAP_ERR_STATE; 708 return AIENOS_CAP_ERR_UNAUTHORIZED; 709 } 710 711 int aienos_cap_cognition_admin(const AienosCapView *view, uint32_t op, AienosCapRef authority, 712 AienosCapRef target) { 713 (void)op; 714 (void)authority; 715 (void)target; 716 if (!view) return AIENOS_CAP_ERR_STATE; 717 return AIENOS_CAP_ERR_UNAUTHORIZED; 718 } 719 720 int aienos_cap_force_generation(AienosCapAdmin *admin, uint32_t cap_id, uint64_t generation) { 721 if (!admin) return AIENOS_CAP_ERR_STATE; 722 return LOCKED(admin->shared, 723 state_force_generation(&admin->shared->state, cap_id, generation)); 724 } 725 726 int aienos_cap_authorize(const AienosCapAdmin *admin, const uint8_t *presented) { 727 if (!admin || !presented) return AIENOS_CAP_ERR_UNAUTHORIZED; 728 CapShared *sh = admin->shared; 729 uint8_t diff = 0; 730 pthread_mutex_lock(&sh->token_lock); 731 for (uint32_t i = 0; i < AIENOS_CAP_TOKEN_LEN; i++) diff |= sh->token[i] ^ presented[i]; 732 pthread_mutex_unlock(&sh->token_lock); 733 return diff == 0 ? AIENOS_CAP_OK : AIENOS_CAP_ERR_UNAUTHORIZED; 734 }