Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/capability/contain.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / capability / contain.om` 1174 lines ยท 29575 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/capability/contain.om: Pure-Omega AEGIS Containment Gate (ARGUS-1 lane G). 3 note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux 4 note native/capability/aienos_contain.{h,c}. 5 note Composes with: si/directives/om2.om aienux/capability/lib.om aienux/capability/contain.om om/tls/sha2.om 6 7 note Protected subjects (I5.b) 8 const CON_SUBJ uint 43 9 const CON_SUBJ_AEGIS uint 41 10 const CON_SUBJ_AEGIS_ROOT uint 42 11 12 note Receipt actors 13 const CON_ACTOR_GATE uint 43 14 const CON_ACTOR_OFFICE uint 0 15 16 note Gate limits 17 const CON_BUDGET ulong 8 18 const CON_BUDGET_WINDOW ulong 64 19 const CON_DECISIONS ulong 32 20 const CON_REGISTRY ulong 256 21 const CON_REQ_SIZE ulong 152 22 const CON_REQ_VERSION uchar 2 23 const CON_CAP_NONE uint 0xffffffff 24 25 note Request types (1..10; only REVOKE_CAPABILITY is LIVE) 26 const CON_REVOKE_CAPABILITY uchar 1 27 const CON_FREEZE_PRINCIPAL uchar 2 28 const CON_RESTRICT_PRINCIPAL uchar 3 29 const CON_QUARANTINE_MACHINE uchar 4 30 const CON_QUARANTINE_PROVIDER uchar 5 31 const CON_REVOKE_CRED_LEASE uchar 6 32 const CON_REJECT_ARTIFACT uchar 7 33 const CON_REQUIRE_REATTEST uchar 8 34 const CON_RAISE_EFFECT_CLASS uchar 9 35 const CON_PAUSE_EXT_EFFECTS uchar 10 36 const CON_TYPE_MAX uchar 10 37 38 note Request flags 39 const CON_CREQ_SYNTHETIC ushort 0x0001 40 const CON_CREQ_SATURATION ushort 0x0002 41 const CON_CREQ_KNOWN ushort 0x0003 42 43 note Receipt status (1..3 decision, 4..8 execution) 44 const CON_GRANT uchar 1 45 const CON_DENY uchar 2 46 const CON_ESCALATE uchar 3 47 const CON_DONE uchar 4 48 const CON_PARTIAL uchar 5 49 const CON_FAILED uchar 6 50 const CON_UNAVAILABLE uchar 7 51 const CON_FAILED_SCOPE uchar 8 52 53 note Receipt kind 54 const CON_KIND_DECIDED uchar 91 55 const CON_KIND_EXECUTED uchar 92 56 57 note WHY codes (spec 5.2) 58 const CON_WHY_POLICY_GRANT ushort 1100 59 const CON_WHY_POLICY_DENY ushort 1101 60 const CON_WHY_POLICY_ESCALATE ushort 1102 61 const CON_WHY_PROTECTED_TARGET ushort 1103 62 const CON_WHY_TARGET_NOT_LIVE ushort 1104 63 const CON_WHY_SUBJECT_MISMATCH ushort 1105 64 const CON_WHY_REPLAYED_REQUEST ushort 1106 65 const CON_WHY_BUDGET ushort 1107 66 const CON_WHY_BAD_REQUEST ushort 1108 67 const CON_WHY_NO_EXECUTOR ushort 1109 68 const CON_WHY_HUMAN_DENY ushort 1110 69 const CON_WHY_HUMAN_GRANT ushort 1111 70 const CON_WHY_BAD_SECRET ushort 1112 71 const CON_WHY_DECISION_REPLAY ushort 1113 72 const CON_WHY_NOT_LEAF ushort 1114 73 const CON_WHY_LINEAGE_UNTRUSTED ushort 1115 74 const CON_WHY_NOT_AUTO_ELIGIBLE ushort 1116 75 76 note Receipt gate_flags 77 const CON_RF_REFUSED_CALL ushort 0x0001 78 const CON_RF_HUMAN ushort 0x0002 79 const CON_RF_SEAM ushort 0x0004 80 const CON_RF_INERT ushort 0x0008 81 82 note Function return codes 83 const CON_OK int 0 84 const CON_ERR_ARG int -1 85 const CON_ERR_SPACE int -2 86 const CON_ERR_EXISTS int -3 87 const CON_ERR_AUTHORITY int -4 88 const CON_ERR_UNKNOWN int -5 89 const CON_ERR_REFUSED int -6 90 const CON_ERR_FULL int -7 91 92 note Decision slot states 93 const CON_D_FREE uchar 0 94 const CON_D_GRANTED uchar 1 95 const CON_D_DENIED uchar 2 96 const CON_D_ESCALATED uchar 3 97 const CON_D_EXECUTED uchar 4 98 99 note Receipt array indices (ulong 16) 100 const CON_REC_SEQ ulong 0 101 const CON_REC_DEC_ID ulong 1 102 const CON_REC_REQ_ID ulong 2 103 const CON_REC_FSEQ ulong 3 104 const CON_REC_TICK ulong 4 105 const CON_REC_RESOURCE ulong 5 106 const CON_REC_TGT_ID ulong 6 107 const CON_REC_TGT_GEN ulong 7 108 const CON_REC_EXEC_ID ulong 8 109 const CON_REC_EXEC_GEN ulong 9 110 const CON_REC_PRINCIPAL ulong 10 111 const CON_REC_ACTOR ulong 11 112 const CON_REC_RC ulong 12 113 const CON_REC_WHY ulong 13 114 const CON_REC_FLAGS ulong 14 115 const CON_REC_META ulong 15 116 117 note Gate metadata indices in con_meta (ulong 24) 118 const CON_M_ACTIVE ulong 0 119 const CON_M_OFF_ID ulong 1 120 const CON_M_OFF_GEN ulong 2 121 const CON_M_EXEC_ID ulong 3 122 const CON_M_EXEC_GEN ulong 4 123 const CON_M_SEQ ulong 5 124 const CON_M_LAST_DEC ulong 6 125 const CON_M_LAST_REQ ulong 7 126 const CON_M_WINDOW ulong 8 127 const CON_M_WIN_GRANTS ulong 9 128 const CON_M_INERT ulong 10 129 const CON_M_TRUSTED ulong 11 130 const CON_M_EXEC_DEAD ulong 12 131 const CON_M_OBS_CUR ulong 13 132 const CON_M_COUNTING ulong 14 133 const CON_M_REV_SEEN ulong 15 134 const CON_M_TGT_SEEN ulong 16 135 const CON_M_ETGT_ID ulong 17 136 const CON_M_ETGT_GEN ulong 18 137 const CON_M_SEAMS_ON ulong 19 138 const CON_M_SEAM_FLAGS ulong 20 139 const CON_M_SEAM_TEST_ST ulong 21 140 const CON_M_REG_N ulong 22 141 142 note Global gate state (no heap, bounded static footprint) 143 global con_meta ulong 24 144 global con_reg_gen ulong 256 145 global con_policy uint 11 146 147 note Lineage shadow table (256 slots) 148 global con_lin_gen ulong 256 149 global con_lin_pgen ulong 256 150 global con_lin_subj uint 256 151 global con_lin_rgt uint 256 152 global con_lin_pid uint 256 153 global con_lin_st uint 256 154 global con_lin_known uchar 256 155 156 note Decision ring (32 slots) 157 global con_dec_req uchar 4864 158 global con_dec_dig uchar 1024 159 global con_dec_id ulong 32 160 global con_dec_st uchar 32 161 global con_dec_human uchar 32 162 163 note Scratch for canonical request operations 164 global con_tmp_req uchar 152 165 global con_tmp_dig uchar 32 166 167 note ---- Little-endian wire helpers ---- 168 169 fn con_put16 buf edit uchar * off ulong v ushort to void 170 put buf off uchar and v 0xff 171 put buf add off 1 uchar and shr v 8 0xff 172 end 173 174 fn con_put32 buf edit uchar * off ulong v uint to void 175 put buf off uchar and v 0xff 176 put buf add off 1 uchar and shr v 8 0xff 177 put buf add off 2 uchar and shr v 16 0xff 178 put buf add off 3 uchar and shr v 24 0xff 179 end 180 181 fn con_put64 buf edit uchar * off ulong v ulong to void 182 for i ulong 8 unroll 183 put buf add off i uchar and shr v mul i 8 0xff 184 end 185 end 186 187 fn con_get16 buf view uchar * off ulong to ushort 188 b0 = ushort get buf off 189 b1 = ushort get buf add off 1 190 ret or b0 shl b1 8 191 end 192 193 fn con_get32 buf view uchar * off ulong to uint 194 b0 = uint get buf off 195 b1 = uint get buf add off 1 196 b2 = uint get buf add off 2 197 b3 = uint get buf add off 3 198 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 199 end 200 201 fn con_get64 buf view uchar * off ulong to ulong 202 v = ulong 0 203 for i ulong 8 unroll 204 b = ulong get buf add off i 205 v = or v shl b mul i 8 206 end 207 ret v 208 end 209 210 note ---- Canonical 152-byte request validation & digest ---- 211 212 fn con_req_well_formed buf view uchar * to bool 213 if ne len buf CON_REQ_SIZE 214 ret false 215 end 216 ct = get buf 8 217 if or lt ct 1 gt ct CON_TYPE_MAX 218 ret false 219 end 220 sev = get buf 9 221 if or lt sev 1 gt sev 5 222 ret false 223 end 224 ver = get buf 150 225 if ne ver CON_REQ_VERSION 226 ret false 227 end 228 rsv = get buf 151 229 if ne rsv 0 230 ret false 231 end 232 fl = con_get16 buf 148 233 inv = xor CON_CREQ_KNOWN 0xffff 234 if ne and fl inv 0 235 ret false 236 end 237 rid = con_get64 buf 92 238 if eq rid 0 239 ret false 240 end 241 fseq = con_get64 buf 100 242 if eq fseq 0 243 ret false 244 end 245 ret true 246 end 247 248 fn con_req_narrow_shape buf view uchar * to bool 249 cid = con_get32 buf 16 250 if and ne cid CON_CAP_NONE ge cid CAP_MAX 251 ret false 252 end 253 ct = get buf 8 254 trgt = con_get32 buf 112 255 if and ne ct CON_RESTRICT_PRINCIPAL ne trgt 0 256 ret false 257 end 258 if eq ct CON_RESTRICT_PRINCIPAL 259 inv = xor CAP_RIGHT_KNOWN 0xffffffff 260 if ne and trgt inv 0 261 ret false 262 end 263 end 264 if eq ct CON_REVOKE_CAPABILITY 265 if or eq cid CON_CAP_NONE ne con_get32 buf 108 0 266 ret false 267 end 268 end 269 ret true 270 end 271 272 fn con_req_digest buf view uchar * dst edit uchar * to void 273 dlen = sha2_digest 256 buf dst 274 if eq dlen 0 275 trap 8 276 end 277 end 278 279 fn con_dig_eq a view uchar * b view uchar * to bool 280 diff = uchar 0 281 for i ulong 32 282 diff = or diff xor get a i get b i 283 end 284 ret eq diff 0 285 end 286 287 note ---- Authority registry (prevents duplicate create per authority instance) ---- 288 289 fn con_reg_claim office_gen ulong to int 290 n = get con_meta CON_M_REG_N 291 i = ulong 0 292 while lt i n bound 256 293 if eq get con_reg_gen i office_gen 294 ret CON_ERR_EXISTS 295 end 296 i = add i 1 297 end 298 if ge n CON_REGISTRY 299 ret CON_ERR_FULL 300 end 301 put con_reg_gen n office_gen 302 put con_meta CON_M_REG_N add n 1 303 ret CON_OK 304 end 305 306 fn con_reg_release office_gen ulong to void 307 n = get con_meta CON_M_REG_N 308 i = ulong 0 309 while lt i n bound 256 310 if eq get con_reg_gen i office_gen 311 last = sub n 1 312 put con_reg_gen i get con_reg_gen last 313 put con_meta CON_M_REG_N last 314 ret 315 end 316 i = add i 1 317 end 318 end 319 320 note ---- Table policy ---- 321 322 fn contain_policy_reset to void 323 for i ulong 11 324 put con_policy i uint CON_ESCALATE 325 end 326 end 327 328 fn contain_policy_set rtype uchar verdict uchar to void 329 if and ge rtype 1 le rtype CON_TYPE_MAX 330 put con_policy ulong rtype uint verdict 331 end 332 end 333 334 fn con_policy_decide rtype uchar to uchar 335 if and ge rtype 1 le rtype CON_TYPE_MAX 336 v = get con_policy ulong rtype 337 if eq v uint CON_GRANT 338 ret CON_GRANT 339 end 340 if eq v uint CON_DENY 341 ret CON_DENY 342 end 343 end 344 ret CON_ESCALATE 345 end 346 347 note ---- Lineage shadow index (fed by authority observer) ---- 348 349 fn con_lineage_apply_mint ev view CapObsEvent cid uint idx ulong to void 350 ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 351 cref = new CapRef cid get ev generation 352 irc = cap_inspect cref ent 353 if eq irc CAP_OK 354 put con_lin_gen idx get ent generation 355 put con_lin_pgen idx get ent parent_generation 356 put con_lin_subj idx get ent subject 357 put con_lin_rgt idx get ent rights 358 put con_lin_pid idx get ent parent_id 359 put con_lin_st idx get ent state 360 put con_lin_known idx 1 361 else 362 put con_lin_gen idx get ev generation 363 put con_lin_pgen idx 0 364 put con_lin_subj idx 0 365 put con_lin_rgt idx get ev rights 366 put con_lin_pid idx get ev parent_id 367 put con_lin_st idx get ev state 368 put con_lin_known idx 1 369 end 370 end 371 372 fn con_lineage_apply ev view CapObsEvent to void 373 op = get ev op 374 rc = get ev rc 375 has = get ev has_entry 376 if and ne get con_meta CON_M_COUNTING 0 and eq op CAP_OBS_REVOKE ne has 0 377 put con_meta CON_M_REV_SEEN add get con_meta CON_M_REV_SEEN 1 378 if and eq ulong get ev cap_id get con_meta CON_M_ETGT_ID eq get ev generation get con_meta CON_M_ETGT_GEN 379 put con_meta CON_M_TGT_SEEN 1 380 end 381 end 382 if eq has 0 383 if eq op CAP_OBS_KILL 384 put con_meta CON_M_TRUSTED 0 385 else 386 if eq rc CAP_OK 387 put con_meta CON_M_TRUSTED 0 388 end 389 end 390 ret 391 end 392 cid = get ev cap_id 393 if ge cid CAP_MAX 394 put con_meta CON_M_TRUSTED 0 395 ret 396 end 397 idx = ulong cid 398 if eq op CAP_OBS_MINT 399 con_lineage_apply_mint ev cid idx 400 ret 401 end 402 if eq op CAP_OBS_REVOKE 403 k = uchar 0 404 if and ne get con_lin_known idx 0 eq get con_lin_gen idx get ev generation 405 k = 1 406 end 407 put con_lin_gen idx get ev generation 408 put con_lin_rgt idx get ev rights 409 put con_lin_pid idx get ev parent_id 410 put con_lin_st idx get ev state 411 put con_lin_known idx k 412 if and eq ulong cid get con_meta CON_M_EXEC_ID eq get ev generation get con_meta CON_M_EXEC_GEN 413 put con_meta CON_M_EXEC_DEAD 1 414 end 415 ret 416 end 417 if eq op CAP_OBS_RECLAIM 418 put con_lin_gen idx get ev generation 419 put con_lin_pgen idx 0 420 put con_lin_subj idx 0 421 put con_lin_rgt idx 0 422 put con_lin_pid idx CAP_PARENT_NONE 423 put con_lin_st idx get ev state 424 put con_lin_known idx 1 425 ret 426 end 427 if eq op CAP_OBS_CLOCK 428 ret 429 end 430 put con_meta CON_M_TRUSTED 0 431 end 432 433 note Drain all newly emitted authority observer events into the gate lineage shadow. 434 fn contain_sync_observer to void 435 cur = get con_meta CON_M_OBS_CUR 436 tot = cap_obs_count 437 ev = new CapObsEvent 0 0 0 0 0 0 0 0 0 438 while lt cur tot bound 512 439 if cap_obs_get cur ev 440 con_lineage_apply ev 441 end 442 cur = add cur 1 443 end 444 put con_meta CON_M_OBS_CUR cur 445 end 446 447 note ---- Protected target & structural checks ---- 448 449 fn con_is_protected_subj s uint to bool 450 if eq s CON_SUBJ 451 ret true 452 end 453 if eq s CON_SUBJ_AEGIS 454 ret true 455 end 456 if eq s CON_SUBJ_AEGIS_ROOT 457 ret true 458 end 459 ret false 460 end 461 462 fn con_subj_privileged subj uint to bool 463 for i ulong 256 464 if eq get con_lin_st i CAP_STATE_LIVE 465 if eq get con_lin_subj i subj 466 if ne and get con_lin_rgt i CAP_RIGHT_PRIVILEGED 0 467 ret true 468 end 469 end 470 end 471 end 472 ret false 473 end 474 475 fn con_has_live_child cid uint cgen ulong to bool 476 for i ulong 256 477 if eq get con_lin_st i CAP_STATE_LIVE 478 if and eq get con_lin_pid i cid eq get con_lin_pgen i cgen 479 ret true 480 end 481 end 482 end 483 ret false 484 end 485 486 fn con_lin_is_priv cid uint cgen ulong to bool 487 if ge cid CAP_MAX 488 ret false 489 end 490 idx = ulong cid 491 if ne get con_lin_gen idx cgen 492 ret false 493 end 494 ret ne and get con_lin_rgt idx CAP_RIGHT_PRIVILEGED 0 495 end 496 497 fn con_is_protected buf view uchar * to bool 498 if and ne get con_meta CON_M_SEAMS_ON 0 ne and get con_meta CON_M_SEAM_FLAGS 1 0 499 ret false 500 end 501 princ = con_get32 buf 12 502 if con_is_protected_subj princ 503 ret true 504 end 505 if ne princ 0 506 if con_subj_privileged princ 507 ret true 508 end 509 end 510 cid = con_get32 buf 16 511 if eq cid CON_CAP_NONE 512 ret false 513 end 514 if or eq cid 0 or eq ulong cid get con_meta CON_M_EXEC_ID eq ulong cid get con_meta CON_M_OFF_ID 515 ret true 516 end 517 cgen = con_get64 buf 20 518 cref = new CapRef cid cgen 519 ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 520 if eq cap_inspect cref ent CAP_OK 521 if ne and get ent rights CAP_RIGHT_PRIVILEGED 0 522 ret true 523 end 524 esubj = get ent subject 525 if or con_is_protected_subj esubj con_subj_privileged esubj 526 ret true 527 end 528 end 529 ret con_lin_is_priv cid cgen 530 end 531 532 fn con_executor_live to bool 533 if ne get con_meta CON_M_EXEC_DEAD 0 534 ret false 535 end 536 ex = new CapRef uint get con_meta CON_M_EXEC_ID get con_meta CON_M_EXEC_GEN 537 ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 538 if ne cap_inspect ex ent CAP_OK 539 put con_meta CON_M_EXEC_DEAD 1 540 ret false 541 end 542 if or ne get ent state CAP_STATE_LIVE or ne get ent rights CAP_RIGHT_REVOKE ne get ent subject CON_SUBJ 543 put con_meta CON_M_EXEC_DEAD 1 544 ret false 545 end 546 ret true 547 end 548 549 fn con_lin_is_live cid uint cgen ulong to bool 550 idx = ulong cid 551 if eq get con_lin_known idx 0 552 ret false 553 end 554 if ne get con_lin_gen idx cgen 555 ret false 556 end 557 ret eq get con_lin_st idx CAP_STATE_LIVE 558 end 559 560 note Returns 0 if eligible, else WHY code; ask_out[0]=1 means ESCALATE, 0 means DENY. 561 fn con_revoke_target_check buf view uchar * ask_out edit uchar 1 to ushort 562 put ask_out 0 0 563 cid = con_get32 buf 16 564 cgen = con_get64 buf 20 565 cref = new CapRef cid cgen 566 ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 567 if ne cap_inspect cref ent CAP_OK 568 ret CON_WHY_TARGET_NOT_LIVE 569 end 570 if ne get ent state CAP_STATE_LIVE 571 ret CON_WHY_TARGET_NOT_LIVE 572 end 573 princ = con_get32 buf 12 574 if and ne princ 0 ne get ent subject princ 575 ret CON_WHY_SUBJECT_MISMATCH 576 end 577 if not con_lin_is_live cid cgen 578 put ask_out 0 1 579 ret CON_WHY_LINEAGE_UNTRUSTED 580 end 581 if con_has_live_child cid cgen 582 put ask_out 0 1 583 ret CON_WHY_NOT_LEAF 584 end 585 ret 0 586 end 587 588 note ---- Receipt construction & decision ring ---- 589 590 fn con_pack_flags fcode ushort rflags ushort gflags ushort to ulong 591 ret or or ulong fcode shl ulong rflags 16 shl ulong gflags 32 592 end 593 594 fn con_pack_meta kind uchar status uchar rtype uchar to ulong 595 ret or or ulong kind shl ulong status 8 shl ulong rtype 16 596 end 597 598 fn con_rec_status rec view ulong 16 to uchar 599 ret uchar and shr get rec CON_REC_META 8 0xff 600 end 601 602 fn con_rec_kind rec view ulong 16 to uchar 603 ret uchar and get rec CON_REC_META 0xff 604 end 605 606 fn con_rec_gate_flags rec view ulong 16 to ushort 607 ret ushort and shr get rec CON_REC_FLAGS 32 0xffff 608 end 609 610 fn con_rec_req_flags rec view ulong 16 to ushort 611 ret ushort and shr get rec CON_REC_FLAGS 16 0xffff 612 end 613 614 fn con_receipt_base rec edit ulong 16 rdig edit uchar 32 buf view uchar 152 dig view uchar 32 has_req bool kind uchar to void 615 for i ulong 16 616 put rec i 0 617 end 618 for i ulong 32 619 put rdig i get dig i 620 end 621 seq = add get con_meta CON_M_SEQ 1 622 put con_meta CON_M_SEQ seq 623 put rec CON_REC_SEQ seq 624 put rec CON_REC_ACTOR ulong CON_ACTOR_GATE 625 put rec CON_REC_EXEC_ID get con_meta CON_M_EXEC_ID 626 put rec CON_REC_EXEC_GEN get con_meta CON_M_EXEC_GEN 627 put rec CON_REC_TICK cap_clock 628 gfl = ushort 0 629 if ne get con_meta CON_M_SEAMS_ON 0 630 gfl = or gfl CON_RF_SEAM 631 end 632 if has_req 633 put rec CON_REC_REQ_ID con_get64 buf 92 634 put rec CON_REC_FSEQ con_get64 buf 100 635 put rec CON_REC_TGT_ID ulong con_get32 buf 16 636 put rec CON_REC_TGT_GEN con_get64 buf 20 637 put rec CON_REC_PRINCIPAL ulong con_get32 buf 12 638 put rec CON_REC_FLAGS con_pack_flags con_get16 buf 10 con_get16 buf 148 gfl 639 put rec CON_REC_META con_pack_meta kind 0 get buf 8 640 else 641 put rec CON_REC_FLAGS con_pack_flags 0 0 gfl 642 put rec CON_REC_META con_pack_meta kind 0 0 643 end 644 end 645 646 fn con_dec_find did ulong to int 647 if eq did 0 648 ret -1 649 end 650 for i ulong 32 651 if ne get con_dec_st i CON_D_FREE 652 if eq get con_dec_id i did 653 ret int i 654 end 655 end 656 end 657 ret -1 658 end 659 660 fn con_dec_alloc_slot to ulong 661 best = ulong 0 662 best_id = get con_dec_id 0 663 for i ulong 32 664 if eq get con_dec_st i CON_D_FREE 665 ret i 666 end 667 cid = get con_dec_id i 668 if lt cid best_id 669 best = i 670 best_id = cid 671 end 672 end 673 ret best 674 end 675 676 fn con_pack_dec_arg status uchar why ushort keep bool to ulong 677 k = ulong 0 678 if keep 679 k = 1 680 end 681 ret or or ulong status shl ulong why 8 shl k 24 682 end 683 684 fn con_dec_store did ulong status uchar buf view uchar 152 dig view uchar 32 to void 685 slot = con_dec_alloc_slot 686 put con_dec_id slot did 687 put con_dec_human slot 0 688 if eq status CON_GRANT 689 put con_dec_st slot CON_D_GRANTED 690 else 691 if eq status CON_ESCALATE 692 put con_dec_st slot CON_D_ESCALATED 693 else 694 put con_dec_st slot CON_D_DENIED 695 end 696 end 697 rbase = mul slot CON_REQ_SIZE 698 for i ulong 152 699 put con_dec_req add rbase i get buf i 700 end 701 dbase = mul slot 32 702 for i ulong 32 703 put con_dec_dig add dbase i get dig i 704 end 705 end 706 707 fn con_decide_out buf view uchar 152 dig view uchar 32 darg ulong rec edit ulong 16 rdig edit uchar 32 to void 708 status = uchar and darg 0xff 709 why = ushort and shr darg 8 0xffff 710 keep = ne and shr darg 24 1 0 711 did = add get con_meta CON_M_LAST_DEC 1 712 put con_meta CON_M_LAST_DEC did 713 win = div sub did 1 CON_BUDGET_WINDOW 714 if ne win get con_meta CON_M_WINDOW 715 put con_meta CON_M_WINDOW win 716 put con_meta CON_M_WIN_GRANTS 0 717 end 718 rtype = get buf 8 719 if and eq status CON_GRANT eq rtype CON_REVOKE_CAPABILITY 720 put con_meta CON_M_WIN_GRANTS add get con_meta CON_M_WIN_GRANTS 1 721 end 722 con_receipt_base rec rdig buf dig true CON_KIND_DECIDED 723 put rec CON_REC_DEC_ID did 724 put rec CON_REC_RESOURCE did 725 put rec CON_REC_WHY ulong why 726 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED status rtype 727 if ne get con_meta CON_M_INERT 0 728 gfl = or con_rec_gate_flags rec CON_RF_INERT 729 put rec CON_REC_FLAGS con_pack_flags con_get16 buf 10 con_get16 buf 148 gfl 730 end 731 if keep 732 con_dec_store did status buf dig 733 end 734 end 735 736 note ---- Public Containment Gate API ---- 737 738 fn contain_create off view CapRef to int 739 cur = new CapRef 0 0 740 if ne cap_office cur CAP_OK 741 ret CON_ERR_ARG 742 end 743 if or ne get cur cap_id get off cap_id ne get cur generation get off generation 744 ret CON_ERR_ARG 745 end 746 oe = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 747 if ne cap_inspect off oe CAP_OK 748 ret CON_ERR_ARG 749 end 750 if or ne get oe state CAP_STATE_LIVE eq and get oe rights CAP_RIGHT_MINT 0 751 ret CON_ERR_ARG 752 end 753 rrc = con_reg_claim get off generation 754 if ne rrc CON_OK 755 ret rrc 756 end 757 for i ulong 22 758 put con_meta i 0 759 end 760 for i ulong 256 761 put con_lin_gen i 0 762 put con_lin_pgen i 0 763 put con_lin_subj i 0 764 put con_lin_rgt i 0 765 put con_lin_pid i CAP_PARENT_NONE 766 put con_lin_st i CAP_STATE_FREE 767 put con_lin_known i 0 768 end 769 for i ulong 32 770 put con_dec_id i 0 771 put con_dec_st i CON_D_FREE 772 put con_dec_human i 0 773 end 774 put con_meta CON_M_OFF_ID ulong get off cap_id 775 put con_meta CON_M_OFF_GEN get off generation 776 put con_meta CON_M_EXEC_ID ulong CON_CAP_NONE 777 put con_meta CON_M_OBS_CUR cap_obs_count 778 oidx = ulong get off cap_id 779 put con_lin_gen oidx get oe generation 780 put con_lin_pgen oidx get oe parent_generation 781 put con_lin_subj oidx get oe subject 782 put con_lin_rgt oidx get oe rights 783 put con_lin_pid oidx get oe parent_id 784 put con_lin_st oidx get oe state 785 put con_lin_known oidx 1 786 787 note Single mint (I5.a): REVOKE only, subject CON_SUBJ, no parent, no lease. 788 req = new CapMint get oe subject CON_SUBJ CAP_RES_AUTHORITY CAP_RIGHT_REVOKE 0 CAP_PARENT_NONE 0 get off cap_id get off generation 789 ex = new CapRef 0 0 790 mrc = cap_mint req ex 791 if ne mrc CAP_OK 792 con_reg_release get off generation 793 ret CON_ERR_AUTHORITY 794 end 795 put con_meta CON_M_EXEC_ID ulong get ex cap_id 796 put con_meta CON_M_EXEC_GEN get ex generation 797 put con_meta CON_M_ACTIVE 1 798 contain_sync_observer 799 if con_lin_is_live get ex cap_id get ex generation 800 if eq get con_lin_subj ulong get ex cap_id CON_SUBJ 801 put con_meta CON_M_TRUSTED 1 802 end 803 end 804 ret CON_OK 805 end 806 807 fn contain_executor dst edit CapRef to int 808 if eq get con_meta CON_M_ACTIVE 0 809 ret CON_ERR_ARG 810 end 811 put dst cap_id uint get con_meta CON_M_EXEC_ID 812 put dst generation get con_meta CON_M_EXEC_GEN 813 ret CON_OK 814 end 815 816 fn contain_inert to bool 817 if eq get con_meta CON_M_ACTIVE 0 818 ret true 819 end 820 ret ne get con_meta CON_M_INERT 0 821 end 822 823 fn contain_set_seams off view CapRef flags ulong test_st ulong to int 824 if eq get con_meta CON_M_ACTIVE 0 825 ret CON_ERR_REFUSED 826 end 827 oe = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 828 if ne cap_inspect off oe CAP_OK 829 ret CON_ERR_REFUSED 830 end 831 if or ne get oe state CAP_STATE_LIVE eq and get oe rights CAP_RIGHT_MINT 0 832 ret CON_ERR_REFUSED 833 end 834 if or ne flags 0 ne test_st 0 835 put con_meta CON_M_SEAMS_ON 1 836 put con_meta CON_M_SEAM_FLAGS flags 837 put con_meta CON_M_SEAM_TEST_ST test_st 838 else 839 put con_meta CON_M_SEAMS_ON 0 840 put con_meta CON_M_SEAM_FLAGS 0 841 put con_meta CON_M_SEAM_TEST_ST 0 842 end 843 ret CON_OK 844 end 845 846 fn con_decide_eval buf view uchar * st_out edit uchar 1 why_out edit ushort 1 to void 847 ask = new uchar 1 0 848 if con_is_protected buf 849 put st_out 0 CON_DENY 850 put why_out 0 CON_WHY_PROTECTED_TARGET 851 ret 852 end 853 ct = get buf 8 854 is_live = eq ct CON_REVOKE_CAPABILITY 855 if is_live 856 if or ne get con_meta CON_M_INERT 0 eq get con_meta CON_M_TRUSTED 0 857 put st_out 0 CON_ESCALATE 858 put why_out 0 CON_WHY_LINEAGE_UNTRUSTED 859 ret 860 end 861 if not con_executor_live 862 put st_out 0 CON_ESCALATE 863 put why_out 0 CON_WHY_NO_EXECUTOR 864 ret 865 end 866 w = con_revoke_target_check buf ask 867 if ne w 0 868 if ne get ask 0 0 869 put st_out 0 CON_ESCALATE 870 else 871 put st_out 0 CON_DENY 872 end 873 put why_out 0 w 874 ret 875 end 876 end 877 v = con_policy_decide ct 878 if eq v CON_DENY 879 put st_out 0 CON_DENY 880 put why_out 0 CON_WHY_POLICY_DENY 881 ret 882 end 883 if ne v CON_GRANT 884 put st_out 0 CON_ESCALATE 885 put why_out 0 CON_WHY_POLICY_ESCALATE 886 ret 887 end 888 fl = con_get16 buf 148 889 if or not is_live ne and fl or CON_CREQ_SYNTHETIC CON_CREQ_SATURATION 0 890 put st_out 0 CON_ESCALATE 891 put why_out 0 CON_WHY_NOT_AUTO_ELIGIBLE 892 ret 893 end 894 nwin = div get con_meta CON_M_LAST_DEC CON_BUDGET_WINDOW 895 used = ulong 0 896 if eq nwin get con_meta CON_M_WINDOW 897 used = get con_meta CON_M_WIN_GRANTS 898 end 899 if ge used CON_BUDGET 900 put st_out 0 CON_ESCALATE 901 put why_out 0 CON_WHY_BUDGET 902 ret 903 end 904 put st_out 0 CON_GRANT 905 put why_out 0 CON_WHY_POLICY_GRANT 906 end 907 908 fn contain_submit_bytes raw view uchar * rec edit ulong 16 rdig edit uchar 32 to int 909 if eq get con_meta CON_M_ACTIVE 0 910 ret CON_ERR_ARG 911 end 912 contain_sync_observer 913 d = new uchar 32 0 914 dlen = sha2_digest 256 raw d 915 if eq dlen 0 916 trap 8 917 end 918 zreq = new uchar 152 0 919 if ne len raw CON_REQ_SIZE 920 con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig 921 ret CON_OK 922 end 923 for i ulong 152 924 put zreq i get raw i 925 end 926 if or not con_req_well_formed zreq not con_req_narrow_shape zreq 927 con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig 928 ret CON_OK 929 end 930 rid = con_get64 zreq 92 931 if le rid get con_meta CON_M_LAST_REQ 932 con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_REPLAYED_REQUEST false rec rdig 933 ret CON_OK 934 end 935 put con_meta CON_M_LAST_REQ rid 936 st = new uchar 1 0 937 why = new ushort 1 0 938 con_decide_eval zreq st why 939 sv = get st 0 940 con_decide_out zreq d con_pack_dec_arg sv get why 0 ne sv CON_DENY rec rdig 941 ret CON_OK 942 end 943 944 fn contain_submit raw view uchar * req_dig view uchar 32 rec edit ulong 16 rdig edit uchar 32 to int 945 if eq get con_meta CON_M_ACTIVE 0 946 ret CON_ERR_ARG 947 end 948 if ne len raw CON_REQ_SIZE 949 ret CON_ERR_ARG 950 end 951 contain_sync_observer 952 rbuf = new uchar 152 0 953 for i ulong 152 954 put rbuf i get raw i 955 end 956 d = new uchar 32 0 957 con_req_digest rbuf d 958 if not con_dig_eq d req_dig 959 con_decide_out rbuf d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig 960 ret CON_OK 961 end 962 ret contain_submit_bytes rbuf rec rdig 963 end 964 965 fn con_load_dec_req slot ulong dst edit uchar 152 to void 966 rbase = mul slot CON_REQ_SIZE 967 for i ulong 152 968 put dst i get con_dec_req add rbase i 969 end 970 end 971 972 fn con_load_dec_dig slot ulong dst edit uchar 32 to void 973 dbase = mul slot 32 974 for i ulong 32 975 put dst i get con_dec_dig add dbase i 976 end 977 end 978 979 fn con_resolve_finish slot ulong approve bool rtype uchar rec edit ulong 16 to void 980 if approve 981 put con_dec_st slot CON_D_GRANTED 982 put con_dec_human slot 1 983 if eq rtype CON_REVOKE_CAPABILITY 984 put con_meta CON_M_WIN_GRANTS add get con_meta CON_M_WIN_GRANTS 1 985 end 986 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_GRANT rtype 987 put rec CON_REC_WHY ulong CON_WHY_HUMAN_GRANT 988 else 989 put con_dec_st slot CON_D_DENIED 990 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY rtype 991 put rec CON_REC_WHY ulong CON_WHY_HUMAN_DENY 992 end 993 end 994 995 fn contain_resolve did ulong approve bool secret view uchar 32 rec edit ulong 16 rdig edit uchar 32 to int 996 lreq = new uchar 152 0 997 ldig = new uchar 32 0 998 if eq get con_meta CON_M_ACTIVE 0 999 ret CON_ERR_ARG 1000 end 1001 contain_sync_observer 1002 sec_ok = eq cap_authorize secret CAP_OK 1003 ix = con_dec_find did 1004 if lt ix 0 1005 con_receipt_base rec rdig lreq ldig false CON_KIND_DECIDED 1006 put rec CON_REC_DEC_ID did 1007 put rec CON_REC_RESOURCE did 1008 put rec CON_REC_ACTOR ulong CON_ACTOR_OFFICE 1009 gfl = or or con_rec_gate_flags rec CON_RF_HUMAN CON_RF_REFUSED_CALL 1010 put rec CON_REC_FLAGS con_pack_flags 0 0 gfl 1011 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY 0 1012 put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY 1013 ret CON_ERR_UNKNOWN 1014 end 1015 slot = ulong ix 1016 con_load_dec_req slot lreq 1017 con_load_dec_dig slot ldig 1018 con_receipt_base rec rdig lreq ldig true CON_KIND_DECIDED 1019 put rec CON_REC_DEC_ID did 1020 put rec CON_REC_RESOURCE did 1021 put rec CON_REC_ACTOR ulong CON_ACTOR_OFFICE 1022 rtype = get lreq 8 1023 gfl = or con_rec_gate_flags rec CON_RF_HUMAN 1024 dst_st = get con_dec_st slot 1025 if not sec_ok 1026 gfl = or gfl CON_RF_REFUSED_CALL 1027 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl 1028 st_out = CON_DENY 1029 if eq dst_st CON_D_ESCALATED 1030 st_out = CON_ESCALATE 1031 end 1032 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED st_out rtype 1033 put rec CON_REC_WHY ulong CON_WHY_BAD_SECRET 1034 ret CON_ERR_REFUSED 1035 end 1036 if ne dst_st CON_D_ESCALATED 1037 gfl = or gfl CON_RF_REFUSED_CALL 1038 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl 1039 put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY rtype 1040 put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY 1041 ret CON_ERR_REFUSED 1042 end 1043 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl 1044 con_resolve_finish slot approve rtype rec 1045 ret CON_OK 1046 end 1047 1048 fn con_exec_non_revoke lreq view uchar 152 rtype uchar rfl ushort gfl ushort rec edit ulong 16 to int 1049 st_out = CON_UNAVAILABLE 1050 if and ne get con_meta CON_M_SEAMS_ON 0 ne get con_meta CON_M_SEAM_TEST_ST 0 1051 tst = uchar and get con_meta CON_M_SEAM_TEST_ST 0xff 1052 if and ge tst CON_DONE le tst CON_UNAVAILABLE 1053 st_out = tst 1054 else 1055 st_out = CON_FAILED 1056 end 1057 rfl = or rfl CON_CREQ_SYNTHETIC 1058 end 1059 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1060 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED st_out rtype 1061 if eq st_out CON_UNAVAILABLE 1062 put rec CON_REC_WHY ulong CON_WHY_NO_EXECUTOR 1063 end 1064 ret CON_OK 1065 end 1066 1067 fn con_exec_revoke_check lreq view uchar 152 st_fail_out edit uchar 1 to ushort 1068 ask = new uchar 1 0 1069 put st_fail_out 0 CON_FAILED 1070 if or ne get con_meta CON_M_INERT 0 eq get con_meta CON_M_TRUSTED 0 1071 ret CON_WHY_LINEAGE_UNTRUSTED 1072 end 1073 if not con_executor_live 1074 put st_fail_out 0 CON_UNAVAILABLE 1075 ret CON_WHY_NO_EXECUTOR 1076 end 1077 if con_is_protected lreq 1078 ret CON_WHY_PROTECTED_TARGET 1079 end 1080 ret con_revoke_target_check lreq ask 1081 end 1082 1083 fn con_do_revoke tgt_id uint tgt_gen ulong to int 1084 put con_meta CON_M_COUNTING 1 1085 put con_meta CON_M_REV_SEEN 0 1086 put con_meta CON_M_TGT_SEEN 0 1087 put con_meta CON_M_ETGT_ID ulong tgt_id 1088 put con_meta CON_M_ETGT_GEN tgt_gen 1089 ex = new CapRef uint get con_meta CON_M_EXEC_ID get con_meta CON_M_EXEC_GEN 1090 tgt = new CapRef tgt_id tgt_gen 1091 arc = cap_revoke ex tgt 1092 contain_sync_observer 1093 put con_meta CON_M_COUNTING 0 1094 ret arc 1095 end 1096 1097 fn con_exec_revoke lreq view uchar 152 rtype uchar rfl ushort gfl ushort rec edit ulong 16 to int 1098 st_fail_arr = new uchar 1 0 1099 why = con_exec_revoke_check lreq st_fail_arr 1100 if ne why 0 1101 if ne get con_meta CON_M_INERT 0 1102 gfl = or gfl CON_RF_INERT 1103 end 1104 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1105 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED get st_fail_arr 0 rtype 1106 put rec CON_REC_WHY ulong why 1107 ret CON_OK 1108 end 1109 arc = con_do_revoke con_get32 lreq 16 con_get64 lreq 20 1110 seen = get con_meta CON_M_REV_SEEN 1111 tseen = get con_meta CON_M_TGT_SEEN 1112 put rec CON_REC_RC ulong long arc 1113 put rec CON_REC_TICK cap_clock 1114 if ne arc CAP_OK 1115 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1116 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED rtype 1117 put rec CON_REC_RESOURCE shl ulong 1 32 1118 else 1119 if and eq seen 1 ne tseen 0 1120 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1121 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_DONE rtype 1122 put rec CON_REC_RESOURCE 1 1123 else 1124 put con_meta CON_M_INERT 1 1125 gfl = or gfl CON_RF_INERT 1126 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1127 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED_SCOPE rtype 1128 put rec CON_REC_RESOURCE seen 1129 end 1130 end 1131 ret CON_OK 1132 end 1133 1134 fn contain_execute did ulong rec edit ulong 16 rdig edit uchar 32 to int 1135 lreq = new uchar 152 0 1136 ldig = new uchar 32 0 1137 if eq get con_meta CON_M_ACTIVE 0 1138 ret CON_ERR_ARG 1139 end 1140 contain_sync_observer 1141 ix = con_dec_find did 1142 if lt ix 0 1143 con_receipt_base rec rdig lreq ldig false CON_KIND_EXECUTED 1144 put rec CON_REC_DEC_ID did 1145 gfl = or con_rec_gate_flags rec CON_RF_REFUSED_CALL 1146 put rec CON_REC_FLAGS con_pack_flags 0 0 gfl 1147 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED 0 1148 put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY 1149 ret CON_ERR_UNKNOWN 1150 end 1151 slot = ulong ix 1152 con_load_dec_req slot lreq 1153 con_load_dec_dig slot ldig 1154 con_receipt_base rec rdig lreq ldig true CON_KIND_EXECUTED 1155 put rec CON_REC_DEC_ID did 1156 rtype = get lreq 8 1157 rfl = con_get16 lreq 148 1158 gfl = con_rec_gate_flags rec 1159 if ne get con_dec_human slot 0 1160 gfl = or gfl CON_RF_HUMAN 1161 end 1162 if ne get con_dec_st slot CON_D_GRANTED 1163 gfl = or gfl CON_RF_REFUSED_CALL 1164 put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl 1165 put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED rtype 1166 put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY 1167 ret CON_ERR_REFUSED 1168 end 1169 put con_dec_st slot CON_D_EXECUTED 1170 if ne rtype CON_REVOKE_CAPABILITY 1171 ret con_exec_non_revoke lreq rtype rfl gfl rec 1172 end 1173 ret con_exec_revoke lreq rtype rfl gfl rec 1174 end