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
omega-mv / aienux / capability / lib.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/capability/lib.om: Pure-Omega AIENOS 64-bit Generation Capability Authority. |
| 3 | note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux |
| 4 | note native/capability/aienos_capability.{h,c} and crates/aienos-capability/src/lib.rs. |
| 5 | note Compose with: si/directives/om2.om aienux/capability/lib.om <test.om|main.om> |
| 6 | |
| 7 | struct CapRef cap_id uint generation ulong end |
| 8 | |
| 9 | struct CapEntry cap_id uint generation ulong state uint issuer uint subject uint rights uint resource ulong epoch ulong lease_expiry ulong parent_id uint parent_generation ulong minted_by_id uint minted_by_generation ulong end |
| 10 | |
| 11 | struct CapMint issuer uint subject uint resource ulong rights uint lease_ticks ulong parent_id uint parent_generation ulong auth_id uint auth_generation ulong end |
| 12 | |
| 13 | struct CapObsEvent op uint rc int has_entry uint cap_id uint generation ulong state uint rights uint resource ulong parent_id uint end |
| 14 | |
| 15 | note Constants matching native/capability/aienos_capability.h |
| 16 | const CAP_MAX uint 256 |
| 17 | const CAP_MAX_U ulong 256 |
| 18 | const CAP_MAX_DEPTH uint 8 |
| 19 | const CAP_TOKEN_LEN ulong 32 |
| 20 | const CAP_RES_AUTHORITY ulong 0 |
| 21 | const CAP_PARENT_NONE uint 0xffffffff |
| 22 | const CAP_U32_MAX ulong 0xffffffff |
| 23 | const CAP_U64_MAX ulong 0xffffffffffffffff |
| 24 | const CAP_OBS_MAX ulong 512 |
| 25 | |
| 26 | note Return / refusal codes |
| 27 | const CAP_OK int 0 |
| 28 | const CAP_ERR_BOUNDS int -1 |
| 29 | const CAP_ERR_STALE_GEN int -2 |
| 30 | const CAP_ERR_REVOKED int -3 |
| 31 | const CAP_ERR_EPOCH int -4 |
| 32 | const CAP_ERR_SUBJECT int -5 |
| 33 | const CAP_ERR_RESOURCE int -6 |
| 34 | const CAP_ERR_RIGHTS int -7 |
| 35 | const CAP_ERR_EXPIRED int -8 |
| 36 | const CAP_ERR_CHAIN int -9 |
| 37 | const CAP_ERR_AMPLIFY int -10 |
| 38 | const CAP_ERR_NOT_DELEGABLE int -11 |
| 39 | const CAP_ERR_FULL int -12 |
| 40 | const CAP_ERR_IO int -13 |
| 41 | const CAP_ERR_STATE int -14 |
| 42 | const CAP_ERR_UNAUTHORIZED int -15 |
| 43 | const CAP_ERR_OVERFLOW int -16 |
| 44 | const CAP_ERR_EXHAUSTED int -17 |
| 45 | |
| 46 | note Capability rights bitmask |
| 47 | const CAP_RIGHT_READ uint 0x1 |
| 48 | const CAP_RIGHT_WRITE uint 0x2 |
| 49 | const CAP_RIGHT_EFFECT uint 0x4 |
| 50 | const CAP_RIGHT_DELEGATE uint 0x8 |
| 51 | const CAP_RIGHT_MINT uint 0x10 |
| 52 | const CAP_RIGHT_REVOKE uint 0x20 |
| 53 | const CAP_RIGHT_RECLAIM uint 0x40 |
| 54 | const CAP_RIGHT_EPOCH uint 0x80 |
| 55 | const CAP_RIGHT_CLOCK uint 0x100 |
| 56 | const CAP_RIGHT_PROMOTE uint 0x200 |
| 57 | const CAP_RIGHT_PRIVILEGED uint 0x3f0 |
| 58 | const CAP_RIGHT_KNOWN uint 0x3ff |
| 59 | |
| 60 | note Slot states |
| 61 | const CAP_STATE_FREE uint 0 |
| 62 | const CAP_STATE_LIVE uint 1 |
| 63 | const CAP_STATE_REVOKED uint 2 |
| 64 | |
| 65 | note Observer operation codes |
| 66 | const CAP_OBS_MINT uint 1 |
| 67 | const CAP_OBS_REVOKE uint 2 |
| 68 | const CAP_OBS_RECLAIM uint 3 |
| 69 | const CAP_OBS_EPOCH uint 4 |
| 70 | const CAP_OBS_CLOCK uint 5 |
| 71 | const CAP_OBS_KILL uint 6 |
| 72 | const CAP_OBS_RESTART uint 7 |
| 73 | const CAP_OBS_VALIDATE_DENIED uint 8 |
| 74 | |
| 75 | note Authority metadata indices in cap_meta |
| 76 | const CAP_META_BOOT_GEN ulong 0 |
| 77 | const CAP_META_EPOCH ulong 1 |
| 78 | const CAP_META_CLOCK ulong 2 |
| 79 | const CAP_META_ALIVE ulong 3 |
| 80 | const CAP_META_LAST_BOOT ulong 4 |
| 81 | const CAP_META_OBS_ON ulong 5 |
| 82 | const CAP_META_OBS_CNT ulong 6 |
| 83 | const CAP_META_TOKEN_SEQ ulong 7 |
| 84 | |
| 85 | note Authority state tables (256 slots) |
| 86 | global cap_meta ulong 8 |
| 87 | global cap_gen ulong 256 |
| 88 | global cap_state uint 256 |
| 89 | global cap_issuer uint 256 |
| 90 | global cap_subject uint 256 |
| 91 | global cap_rights uint 256 |
| 92 | global cap_res ulong 256 |
| 93 | global cap_epoch ulong 256 |
| 94 | global cap_expiry ulong 256 |
| 95 | global cap_pid uint 256 |
| 96 | global cap_pgen ulong 256 |
| 97 | global cap_mbid uint 256 |
| 98 | global cap_mbgen ulong 256 |
| 99 | global cap_deliv uchar 256 |
| 100 | |
| 101 | note Office token kept outside the capability table |
| 102 | global cap_token uchar 32 |
| 103 | |
| 104 | note Scratch array for cascade revoke slot IDs |
| 105 | global cap_rev_ids uint 256 |
| 106 | |
| 107 | note Observer event log (512 entries) |
| 108 | global cap_obs_op uint 512 |
| 109 | global cap_obs_rc int 512 |
| 110 | global cap_obs_has uint 512 |
| 111 | global cap_obs_cid uint 512 |
| 112 | global cap_obs_gen ulong 512 |
| 113 | global cap_obs_st uint 512 |
| 114 | global cap_obs_rgt uint 512 |
| 115 | global cap_obs_res ulong 512 |
| 116 | global cap_obs_pid uint 512 |
| 117 | |
| 118 | note ---- Checked arithmetic and resource width helpers ---- |
| 119 | |
| 120 | fn cap_widen_resource id uint to ulong |
| 121 | ret ulong id |
| 122 | end |
| 123 | |
| 124 | fn cap_narrow_resource id ulong dst edit uint 1 to int |
| 125 | if gt id CAP_U32_MAX |
| 126 | ret CAP_ERR_RESOURCE |
| 127 | end |
| 128 | put dst 0 uint id |
| 129 | ret CAP_OK |
| 130 | end |
| 131 | |
| 132 | fn cap_generation_advance gen ulong dst edit ulong 1 to int |
| 133 | if eq gen CAP_U64_MAX |
| 134 | ret CAP_ERR_EXHAUSTED |
| 135 | end |
| 136 | put dst 0 add gen 1 |
| 137 | ret CAP_OK |
| 138 | end |
| 139 | |
| 140 | fn cap_add_u64 a ulong b ulong dst edit ulong 1 to int |
| 141 | if gt b sub CAP_U64_MAX a |
| 142 | ret CAP_ERR_OVERFLOW |
| 143 | end |
| 144 | put dst 0 add a b |
| 145 | ret CAP_OK |
| 146 | end |
| 147 | |
| 148 | note ---- Internal table and observer helpers ---- |
| 149 | |
| 150 | fn cap_slot_expired idx ulong to bool |
| 151 | exp = get cap_expiry idx |
| 152 | if eq exp 0 |
| 153 | ret false |
| 154 | end |
| 155 | clk = get cap_meta CAP_META_CLOCK |
| 156 | ret ge clk exp |
| 157 | end |
| 158 | |
| 159 | fn cap_load_entry idx ulong dst edit CapEntry to void |
| 160 | put dst cap_id uint idx |
| 161 | put dst generation get cap_gen idx |
| 162 | put dst state get cap_state idx |
| 163 | put dst issuer get cap_issuer idx |
| 164 | put dst subject get cap_subject idx |
| 165 | put dst rights get cap_rights idx |
| 166 | put dst resource get cap_res idx |
| 167 | put dst epoch get cap_epoch idx |
| 168 | put dst lease_expiry get cap_expiry idx |
| 169 | put dst parent_id get cap_pid idx |
| 170 | put dst parent_generation get cap_pgen idx |
| 171 | put dst minted_by_id get cap_mbid idx |
| 172 | put dst minted_by_generation get cap_mbgen idx |
| 173 | end |
| 174 | |
| 175 | fn cap_obs_emit_none op uint rc int to void |
| 176 | if eq get cap_meta CAP_META_OBS_ON 0 |
| 177 | ret |
| 178 | end |
| 179 | cnt = get cap_meta CAP_META_OBS_CNT |
| 180 | if ge cnt CAP_OBS_MAX |
| 181 | ret |
| 182 | end |
| 183 | put cap_obs_op cnt op |
| 184 | put cap_obs_rc cnt rc |
| 185 | put cap_obs_has cnt 0 |
| 186 | put cap_obs_cid cnt 0 |
| 187 | put cap_obs_gen cnt 0 |
| 188 | put cap_obs_st cnt 0 |
| 189 | put cap_obs_rgt cnt 0 |
| 190 | put cap_obs_res cnt 0 |
| 191 | put cap_obs_pid cnt CAP_PARENT_NONE |
| 192 | put cap_meta CAP_META_OBS_CNT add cnt 1 |
| 193 | end |
| 194 | |
| 195 | fn cap_obs_emit_slot op uint idx ulong rc int to void |
| 196 | if eq get cap_meta CAP_META_OBS_ON 0 |
| 197 | ret |
| 198 | end |
| 199 | cnt = get cap_meta CAP_META_OBS_CNT |
| 200 | if ge cnt CAP_OBS_MAX |
| 201 | ret |
| 202 | end |
| 203 | put cap_obs_op cnt op |
| 204 | put cap_obs_rc cnt rc |
| 205 | put cap_obs_has cnt 1 |
| 206 | put cap_obs_cid cnt uint idx |
| 207 | put cap_obs_gen cnt get cap_gen idx |
| 208 | put cap_obs_st cnt get cap_state idx |
| 209 | put cap_obs_rgt cnt get cap_rights idx |
| 210 | put cap_obs_res cnt get cap_res idx |
| 211 | put cap_obs_pid cnt get cap_pid idx |
| 212 | put cap_meta CAP_META_OBS_CNT add cnt 1 |
| 213 | end |
| 214 | |
| 215 | fn cap_set_observer enabled bool to void |
| 216 | if enabled |
| 217 | put cap_meta CAP_META_OBS_ON 1 |
| 218 | else |
| 219 | put cap_meta CAP_META_OBS_ON 0 |
| 220 | end |
| 221 | end |
| 222 | |
| 223 | fn cap_obs_clear to void |
| 224 | put cap_meta CAP_META_OBS_CNT 0 |
| 225 | end |
| 226 | |
| 227 | fn cap_obs_count to ulong |
| 228 | ret get cap_meta CAP_META_OBS_CNT |
| 229 | end |
| 230 | |
| 231 | fn cap_obs_get idx ulong dst edit CapObsEvent to bool |
| 232 | cnt = get cap_meta CAP_META_OBS_CNT |
| 233 | if ge idx cnt |
| 234 | ret false |
| 235 | end |
| 236 | put dst op get cap_obs_op idx |
| 237 | put dst rc get cap_obs_rc idx |
| 238 | put dst has_entry get cap_obs_has idx |
| 239 | put dst cap_id get cap_obs_cid idx |
| 240 | put dst generation get cap_obs_gen idx |
| 241 | put dst state get cap_obs_st idx |
| 242 | put dst rights get cap_obs_rgt idx |
| 243 | put dst resource get cap_obs_res idx |
| 244 | put dst parent_id get cap_obs_pid idx |
| 245 | ret true |
| 246 | end |
| 247 | |
| 248 | fn cap_fill_token to void |
| 249 | seq = add get cap_meta CAP_META_TOKEN_SEQ 1 |
| 250 | put cap_meta CAP_META_TOKEN_SEQ seq |
| 251 | bgen = get cap_meta CAP_META_BOOT_GEN |
| 252 | state = xor mul seq 0x9e3779b97f4a7c15 bgen |
| 253 | for i ulong 32 |
| 254 | state = xor state shr state 12 |
| 255 | state = xor state shl state 25 |
| 256 | state = xor state shr state 27 |
| 257 | mix = mul state 0x2545f4914f6cdd1d |
| 258 | put cap_token i uchar and shr mix 56 0xff |
| 259 | end |
| 260 | end |
| 261 | |
| 262 | fn cap_take_boot_gen floor ulong dst edit ulong 1 to int |
| 263 | cur = get cap_meta CAP_META_LAST_BOOT |
| 264 | if eq cur CAP_U64_MAX |
| 265 | ret CAP_ERR_EXHAUSTED |
| 266 | end |
| 267 | nxt = add cur 1 |
| 268 | if gt floor nxt |
| 269 | nxt = floor |
| 270 | end |
| 271 | if eq nxt CAP_U64_MAX |
| 272 | ret CAP_ERR_EXHAUSTED |
| 273 | end |
| 274 | put cap_meta CAP_META_LAST_BOOT nxt |
| 275 | put dst 0 nxt |
| 276 | ret CAP_OK |
| 277 | end |
| 278 | |
| 279 | fn cap_table_floor to ulong |
| 280 | top = get cap_meta CAP_META_BOOT_GEN |
| 281 | for i ulong 256 |
| 282 | g = get cap_gen i |
| 283 | if gt g top |
| 284 | top = g |
| 285 | end |
| 286 | pg = get cap_pgen i |
| 287 | if gt pg top |
| 288 | top = pg |
| 289 | end |
| 290 | mg = get cap_mbgen i |
| 291 | if gt mg top |
| 292 | top = mg |
| 293 | end |
| 294 | end |
| 295 | if eq top CAP_U64_MAX |
| 296 | ret CAP_U64_MAX |
| 297 | end |
| 298 | ret add top 1 |
| 299 | end |
| 300 | |
| 301 | fn cap_bootstrap boot_gen ulong to int |
| 302 | if or eq boot_gen 0 eq boot_gen CAP_U64_MAX |
| 303 | ret CAP_ERR_EXHAUSTED |
| 304 | end |
| 305 | put cap_meta CAP_META_BOOT_GEN boot_gen |
| 306 | put cap_meta CAP_META_EPOCH 1 |
| 307 | put cap_meta CAP_META_CLOCK 0 |
| 308 | put cap_meta CAP_META_ALIVE 1 |
| 309 | for i ulong 256 |
| 310 | put cap_gen i boot_gen |
| 311 | put cap_state i CAP_STATE_FREE |
| 312 | put cap_issuer i 0 |
| 313 | put cap_subject i 0 |
| 314 | put cap_rights i 0 |
| 315 | put cap_res i 0 |
| 316 | put cap_epoch i 0 |
| 317 | put cap_expiry i 0 |
| 318 | put cap_pid i CAP_PARENT_NONE |
| 319 | put cap_pgen i 0 |
| 320 | put cap_mbid i 0 |
| 321 | put cap_mbgen i 0 |
| 322 | put cap_deliv i 0 |
| 323 | end |
| 324 | put cap_state 0 CAP_STATE_LIVE |
| 325 | put cap_res 0 CAP_RES_AUTHORITY |
| 326 | put cap_rights 0 CAP_RIGHT_PRIVILEGED |
| 327 | put cap_epoch 0 1 |
| 328 | put cap_deliv 0 1 |
| 329 | ret CAP_OK |
| 330 | end |
| 331 | |
| 332 | fn cap_chain_ok entry_id uint to int |
| 333 | cur = entry_id |
| 334 | depth = uint 0 |
| 335 | note At most CAP_MAX_DEPTH + 1 hops before returning CAP_ERR_CHAIN. |
| 336 | while true bound 16 |
| 337 | idx = ulong cur |
| 338 | pid = get cap_pid idx |
| 339 | if eq pid CAP_PARENT_NONE |
| 340 | ret CAP_OK |
| 341 | end |
| 342 | depth = add depth 1 |
| 343 | if gt depth CAP_MAX_DEPTH |
| 344 | ret CAP_ERR_CHAIN |
| 345 | end |
| 346 | if ge pid CAP_MAX |
| 347 | ret CAP_ERR_CHAIN |
| 348 | end |
| 349 | pidx = ulong pid |
| 350 | if ne get cap_gen pidx get cap_pgen idx |
| 351 | ret CAP_ERR_CHAIN |
| 352 | end |
| 353 | if ne get cap_state pidx CAP_STATE_LIVE |
| 354 | ret CAP_ERR_CHAIN |
| 355 | end |
| 356 | cur = pid |
| 357 | end |
| 358 | ret CAP_ERR_CHAIN |
| 359 | end |
| 360 | |
| 361 | fn cap_ancestor_hops entry_id uint to uint |
| 362 | cur = entry_id |
| 363 | hops = uint 0 |
| 364 | note Stops once parent is NONE or hops exceeds CAP_MAX_DEPTH. |
| 365 | while true bound 16 |
| 366 | idx = ulong cur |
| 367 | pid = get cap_pid idx |
| 368 | if eq pid CAP_PARENT_NONE |
| 369 | ret hops |
| 370 | end |
| 371 | if gt hops CAP_MAX_DEPTH |
| 372 | ret hops |
| 373 | end |
| 374 | if ge pid CAP_MAX |
| 375 | ret add CAP_MAX_DEPTH 1 |
| 376 | end |
| 377 | hops = add hops 1 |
| 378 | cur = pid |
| 379 | end |
| 380 | ret add CAP_MAX_DEPTH 1 |
| 381 | end |
| 382 | |
| 383 | fn cap_auth_use auth_id uint auth_gen ulong need uint to int |
| 384 | if ge auth_id CAP_MAX |
| 385 | ret CAP_ERR_UNAUTHORIZED |
| 386 | end |
| 387 | idx = ulong auth_id |
| 388 | if eq get cap_deliv idx 0 |
| 389 | ret CAP_ERR_UNAUTHORIZED |
| 390 | end |
| 391 | if ne get cap_gen idx auth_gen |
| 392 | ret CAP_ERR_STALE_GEN |
| 393 | end |
| 394 | if ne get cap_state idx CAP_STATE_LIVE |
| 395 | ret CAP_ERR_REVOKED |
| 396 | end |
| 397 | if ne get cap_epoch idx get cap_meta CAP_META_EPOCH |
| 398 | ret CAP_ERR_EPOCH |
| 399 | end |
| 400 | if cap_slot_expired idx |
| 401 | ret CAP_ERR_EXPIRED |
| 402 | end |
| 403 | rc = cap_chain_ok auth_id |
| 404 | if ne rc CAP_OK |
| 405 | ret rc |
| 406 | end |
| 407 | rgt = get cap_rights idx |
| 408 | if ne and rgt need need |
| 409 | ret CAP_ERR_UNAUTHORIZED |
| 410 | end |
| 411 | if ne and need CAP_RIGHT_PRIVILEGED 0 |
| 412 | if ne get cap_res idx CAP_RES_AUTHORITY |
| 413 | ret CAP_ERR_RESOURCE |
| 414 | end |
| 415 | end |
| 416 | ret CAP_OK |
| 417 | end |
| 418 | |
| 419 | fn cap_lease_expiry ticks ulong dst edit ulong 1 to int |
| 420 | if eq ticks 0 |
| 421 | put dst 0 0 |
| 422 | ret CAP_OK |
| 423 | end |
| 424 | clk = get cap_meta CAP_META_CLOCK |
| 425 | ret cap_add_u64 clk ticks dst |
| 426 | end |
| 427 | |
| 428 | note ---- Public Capability Authority API ---- |
| 429 | |
| 430 | fn cap_start to int |
| 431 | b = new ulong 1 0 |
| 432 | rc = cap_take_boot_gen 0 b |
| 433 | if ne rc CAP_OK |
| 434 | ret rc |
| 435 | end |
| 436 | rc = cap_bootstrap get b 0 |
| 437 | if ne rc CAP_OK |
| 438 | ret rc |
| 439 | end |
| 440 | cap_fill_token |
| 441 | ret CAP_OK |
| 442 | end |
| 443 | |
| 444 | fn cap_office dst edit CapRef to int |
| 445 | put dst cap_id 0 |
| 446 | put dst generation get cap_gen 0 |
| 447 | ret CAP_OK |
| 448 | end |
| 449 | |
| 450 | fn cap_clock to ulong |
| 451 | ret get cap_meta CAP_META_CLOCK |
| 452 | end |
| 453 | |
| 454 | fn cap_epoch_now to ulong |
| 455 | ret get cap_meta CAP_META_EPOCH |
| 456 | end |
| 457 | |
| 458 | fn cap_copy_token dst edit uchar 32 to void |
| 459 | for i ulong 32 |
| 460 | put dst i get cap_token i |
| 461 | end |
| 462 | end |
| 463 | |
| 464 | fn cap_authorize presented view uchar 32 to int |
| 465 | diff = uchar 0 |
| 466 | for i ulong 32 |
| 467 | x = xor get cap_token i get presented i |
| 468 | diff = or diff x |
| 469 | end |
| 470 | if eq diff 0 |
| 471 | ret CAP_OK |
| 472 | end |
| 473 | ret CAP_ERR_UNAUTHORIZED |
| 474 | end |
| 475 | |
| 476 | fn cap_inspect cap view CapRef dst edit CapEntry to int |
| 477 | cid = get cap cap_id |
| 478 | if ge cid CAP_MAX |
| 479 | ret CAP_ERR_BOUNDS |
| 480 | end |
| 481 | idx = ulong cid |
| 482 | if ne get cap_gen idx get cap generation |
| 483 | ret CAP_ERR_STALE_GEN |
| 484 | end |
| 485 | if eq get cap_state idx CAP_STATE_FREE |
| 486 | ret CAP_ERR_STATE |
| 487 | end |
| 488 | cap_load_entry idx dst |
| 489 | ret CAP_OK |
| 490 | end |
| 491 | |
| 492 | fn cap_validate_chain cap view CapRef to int |
| 493 | cid = get cap cap_id |
| 494 | if ge cid CAP_MAX |
| 495 | ret CAP_ERR_BOUNDS |
| 496 | end |
| 497 | chain = new uint 9 0 |
| 498 | n = uint 0 |
| 499 | cur = cid |
| 500 | note Walk up to CAP_MAX_DEPTH + 1 slots; cycle or depth > 8 returns CAP_ERR_CHAIN. |
| 501 | while ne cur CAP_PARENT_NONE bound 16 |
| 502 | if gt n CAP_MAX_DEPTH |
| 503 | ret CAP_ERR_CHAIN |
| 504 | end |
| 505 | if ge cur CAP_MAX |
| 506 | ret CAP_ERR_CHAIN |
| 507 | end |
| 508 | put chain ulong n cur |
| 509 | cur = get cap_pid ulong cur |
| 510 | n = add n 1 |
| 511 | end |
| 512 | eidx = ulong get chain 0 |
| 513 | if ne get cap_gen eidx get cap generation |
| 514 | ret CAP_ERR_STALE_GEN |
| 515 | end |
| 516 | if ne get cap_state eidx CAP_STATE_LIVE |
| 517 | ret CAP_ERR_REVOKED |
| 518 | end |
| 519 | cur_ep = get cap_meta CAP_META_EPOCH |
| 520 | if ne get cap_epoch eidx cur_ep |
| 521 | ret CAP_ERR_EPOCH |
| 522 | end |
| 523 | if cap_slot_expired eidx |
| 524 | ret CAP_ERR_EXPIRED |
| 525 | end |
| 526 | i = uint 1 |
| 527 | while lt i n bound 16 |
| 528 | pidx = ulong get chain ulong i |
| 529 | cidx = ulong get chain ulong sub i 1 |
| 530 | if ne get cap_gen pidx get cap_pgen cidx |
| 531 | ret CAP_ERR_CHAIN |
| 532 | end |
| 533 | if ne get cap_state pidx CAP_STATE_LIVE |
| 534 | ret CAP_ERR_CHAIN |
| 535 | end |
| 536 | if ne get cap_epoch pidx cur_ep |
| 537 | ret CAP_ERR_CHAIN |
| 538 | end |
| 539 | if cap_slot_expired pidx |
| 540 | ret CAP_ERR_CHAIN |
| 541 | end |
| 542 | i = add i 1 |
| 543 | end |
| 544 | ret CAP_OK |
| 545 | end |
| 546 | |
| 547 | fn cap_validate cap view CapRef subj uint res ulong req_rights uint dst edit CapEntry to int |
| 548 | rc = cap_validate_chain cap |
| 549 | if ne rc CAP_OK |
| 550 | ret rc |
| 551 | end |
| 552 | eidx = ulong get cap cap_id |
| 553 | if ne get cap_subject eidx subj |
| 554 | ret CAP_ERR_SUBJECT |
| 555 | end |
| 556 | if ne get cap_res eidx res |
| 557 | ret CAP_ERR_RESOURCE |
| 558 | end |
| 559 | if ne and get cap_rights eidx req_rights req_rights |
| 560 | ret CAP_ERR_RIGHTS |
| 561 | end |
| 562 | cap_load_entry eidx dst |
| 563 | ret CAP_OK |
| 564 | end |
| 565 | |
| 566 | fn cap_mint_check_root req view CapMint to int |
| 567 | aid = get req auth_id |
| 568 | agen = get req auth_generation |
| 569 | rc = cap_auth_use aid agen CAP_RIGHT_MINT |
| 570 | if ne rc CAP_OK |
| 571 | ret rc |
| 572 | end |
| 573 | arights = get cap_rights ulong aid |
| 574 | req_priv = and get req rights CAP_RIGHT_PRIVILEGED |
| 575 | missing = and req_priv xor arights 0xffffffff |
| 576 | if ne missing 0 |
| 577 | ret CAP_ERR_UNAUTHORIZED |
| 578 | end |
| 579 | ret CAP_OK |
| 580 | end |
| 581 | |
| 582 | fn cap_mint_check_deleg req view CapMint to int |
| 583 | cexp = new ulong 1 0 |
| 584 | aid = get req auth_id |
| 585 | agen = get req auth_generation |
| 586 | pid = get req parent_id |
| 587 | pgen = get req parent_generation |
| 588 | if ne aid pid |
| 589 | ret CAP_ERR_UNAUTHORIZED |
| 590 | end |
| 591 | if ne agen pgen |
| 592 | ret CAP_ERR_UNAUTHORIZED |
| 593 | end |
| 594 | rc = cap_auth_use aid agen 0 |
| 595 | if ne rc CAP_OK |
| 596 | ret rc |
| 597 | end |
| 598 | aidx = ulong aid |
| 599 | arights = get cap_rights aidx |
| 600 | if eq and arights CAP_RIGHT_DELEGATE 0 |
| 601 | ret CAP_ERR_NOT_DELEGABLE |
| 602 | end |
| 603 | if ge pid CAP_MAX |
| 604 | ret CAP_ERR_BOUNDS |
| 605 | end |
| 606 | if ne get cap_res aidx get req resource |
| 607 | ret CAP_ERR_RESOURCE |
| 608 | end |
| 609 | rgt = get req rights |
| 610 | if ne and rgt xor arights 0xffffffff 0 |
| 611 | ret CAP_ERR_AMPLIFY |
| 612 | end |
| 613 | if ne and rgt CAP_RIGHT_PRIVILEGED 0 |
| 614 | ret CAP_ERR_NOT_DELEGABLE |
| 615 | end |
| 616 | if ge cap_ancestor_hops pid CAP_MAX_DEPTH |
| 617 | ret CAP_ERR_CHAIN |
| 618 | end |
| 619 | aexp = get cap_expiry aidx |
| 620 | if ne aexp 0 |
| 621 | rc = cap_lease_expiry get req lease_ticks cexp |
| 622 | if ne rc CAP_OK |
| 623 | ret rc |
| 624 | end |
| 625 | cval = get cexp 0 |
| 626 | if or eq cval 0 gt cval aexp |
| 627 | ret CAP_ERR_AMPLIFY |
| 628 | end |
| 629 | end |
| 630 | ret CAP_OK |
| 631 | end |
| 632 | |
| 633 | fn cap_mint_alloc req view CapMint expiry ulong dst edit CapRef to int |
| 634 | aid = get req auth_id |
| 635 | aidx = ulong aid |
| 636 | amb_id = aid |
| 637 | amb_gen = get cap_gen aidx |
| 638 | pid = get req parent_id |
| 639 | pgen = ulong 0 |
| 640 | if ne pid CAP_PARENT_NONE |
| 641 | pgen = get req parent_generation |
| 642 | end |
| 643 | saw_exh = false |
| 644 | for i ulong 256 |
| 645 | if eq get cap_state i CAP_STATE_FREE |
| 646 | g = get cap_gen i |
| 647 | if eq g CAP_U64_MAX |
| 648 | saw_exh = true |
| 649 | else |
| 650 | put cap_state i CAP_STATE_LIVE |
| 651 | put cap_issuer i get req issuer |
| 652 | put cap_subject i get req subject |
| 653 | put cap_res i get req resource |
| 654 | put cap_rights i get req rights |
| 655 | put cap_epoch i get cap_meta CAP_META_EPOCH |
| 656 | put cap_expiry i expiry |
| 657 | put cap_pid i pid |
| 658 | put cap_pgen i pgen |
| 659 | put cap_mbid i amb_id |
| 660 | put cap_mbgen i amb_gen |
| 661 | put cap_deliv i 1 |
| 662 | put dst cap_id uint i |
| 663 | put dst generation g |
| 664 | ret CAP_OK |
| 665 | end |
| 666 | end |
| 667 | end |
| 668 | if saw_exh |
| 669 | ret CAP_ERR_EXHAUSTED |
| 670 | end |
| 671 | ret CAP_ERR_FULL |
| 672 | end |
| 673 | |
| 674 | fn cap_mint req view CapMint dst edit CapRef to int |
| 675 | if eq get cap_meta CAP_META_ALIVE 0 |
| 676 | cap_obs_emit_none CAP_OBS_MINT CAP_ERR_IO |
| 677 | ret CAP_ERR_IO |
| 678 | end |
| 679 | rgt = get req rights |
| 680 | inv_known = xor CAP_RIGHT_KNOWN 0xffffffff |
| 681 | if or eq rgt 0 ne and rgt inv_known 0 |
| 682 | cap_obs_emit_none CAP_OBS_MINT CAP_ERR_RIGHTS |
| 683 | ret CAP_ERR_RIGHTS |
| 684 | end |
| 685 | if and ne and rgt CAP_RIGHT_PRIVILEGED 0 ne and rgt CAP_RIGHT_DELEGATE 0 |
| 686 | cap_obs_emit_none CAP_OBS_MINT CAP_ERR_NOT_DELEGABLE |
| 687 | ret CAP_ERR_NOT_DELEGABLE |
| 688 | end |
| 689 | rc = int 0 |
| 690 | if eq get req parent_id CAP_PARENT_NONE |
| 691 | rc = cap_mint_check_root req |
| 692 | else |
| 693 | rc = cap_mint_check_deleg req |
| 694 | end |
| 695 | if ne rc CAP_OK |
| 696 | cap_obs_emit_none CAP_OBS_MINT rc |
| 697 | ret rc |
| 698 | end |
| 699 | exp = new ulong 1 0 |
| 700 | rc = cap_lease_expiry get req lease_ticks exp |
| 701 | if ne rc CAP_OK |
| 702 | cap_obs_emit_none CAP_OBS_MINT rc |
| 703 | ret rc |
| 704 | end |
| 705 | rc = cap_mint_alloc req get exp 0 dst |
| 706 | if ne rc CAP_OK |
| 707 | cap_obs_emit_none CAP_OBS_MINT rc |
| 708 | ret rc |
| 709 | end |
| 710 | cap_obs_emit_slot CAP_OBS_MINT ulong get dst cap_id CAP_OK |
| 711 | ret CAP_OK |
| 712 | end |
| 713 | |
| 714 | fn cap_cascade_revoke start_n uint to uint |
| 715 | n = start_n |
| 716 | for guard uint 256 |
| 717 | changed = false |
| 718 | for i ulong 256 |
| 719 | if eq get cap_state i CAP_STATE_LIVE |
| 720 | pid = get cap_pid i |
| 721 | if ne pid CAP_PARENT_NONE |
| 722 | if lt pid CAP_MAX |
| 723 | pidx = ulong pid |
| 724 | if and eq get cap_gen pidx get cap_pgen i eq get cap_state pidx CAP_STATE_REVOKED |
| 725 | put cap_state i CAP_STATE_REVOKED |
| 726 | if lt n CAP_MAX |
| 727 | put cap_rev_ids ulong n uint i |
| 728 | n = add n 1 |
| 729 | end |
| 730 | changed = true |
| 731 | end |
| 732 | end |
| 733 | end |
| 734 | end |
| 735 | end |
| 736 | if not changed |
| 737 | break |
| 738 | end |
| 739 | end |
| 740 | ret n |
| 741 | end |
| 742 | |
| 743 | fn cap_revoke auth view CapRef target view CapRef to int |
| 744 | if eq get cap_meta CAP_META_ALIVE 0 |
| 745 | cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_IO |
| 746 | ret CAP_ERR_IO |
| 747 | end |
| 748 | rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_REVOKE |
| 749 | if ne rc CAP_OK |
| 750 | cap_obs_emit_none CAP_OBS_REVOKE rc |
| 751 | ret rc |
| 752 | end |
| 753 | tid = get target cap_id |
| 754 | if ge tid CAP_MAX |
| 755 | cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_BOUNDS |
| 756 | ret CAP_ERR_BOUNDS |
| 757 | end |
| 758 | tidx = ulong tid |
| 759 | if ne get cap_gen tidx get target generation |
| 760 | cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_STALE_GEN |
| 761 | ret CAP_ERR_STALE_GEN |
| 762 | end |
| 763 | if ne get cap_state tidx CAP_STATE_LIVE |
| 764 | cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_STATE |
| 765 | ret CAP_ERR_STATE |
| 766 | end |
| 767 | arights = get cap_rights ulong get auth cap_id |
| 768 | tpriv = and get cap_rights tidx CAP_RIGHT_PRIVILEGED |
| 769 | if ne and tpriv xor arights 0xffffffff 0 |
| 770 | cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_UNAUTHORIZED |
| 771 | ret CAP_ERR_UNAUTHORIZED |
| 772 | end |
| 773 | put cap_state tidx CAP_STATE_REVOKED |
| 774 | put cap_rev_ids 0 tid |
| 775 | n = cap_cascade_revoke 1 |
| 776 | k = uint 0 |
| 777 | note At most CAP_MAX slots can be revoked in one cascade. |
| 778 | while lt k n bound 256 |
| 779 | sid = get cap_rev_ids ulong k |
| 780 | cap_obs_emit_slot CAP_OBS_REVOKE ulong sid CAP_OK |
| 781 | k = add k 1 |
| 782 | end |
| 783 | ret CAP_OK |
| 784 | end |
| 785 | |
| 786 | fn cap_reclaim auth view CapRef cid uint to int |
| 787 | if eq get cap_meta CAP_META_ALIVE 0 |
| 788 | cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_IO |
| 789 | ret CAP_ERR_IO |
| 790 | end |
| 791 | rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_RECLAIM |
| 792 | if ne rc CAP_OK |
| 793 | cap_obs_emit_none CAP_OBS_RECLAIM rc |
| 794 | ret rc |
| 795 | end |
| 796 | if ge cid CAP_MAX |
| 797 | cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_BOUNDS |
| 798 | ret CAP_ERR_BOUNDS |
| 799 | end |
| 800 | idx = ulong cid |
| 801 | if ne get cap_state idx CAP_STATE_REVOKED |
| 802 | cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_STATE |
| 803 | ret CAP_ERR_STATE |
| 804 | end |
| 805 | nxt = new ulong 1 0 |
| 806 | rc = cap_generation_advance get cap_gen idx nxt |
| 807 | if ne rc CAP_OK |
| 808 | cap_obs_emit_none CAP_OBS_RECLAIM rc |
| 809 | ret rc |
| 810 | end |
| 811 | put cap_gen idx get nxt 0 |
| 812 | put cap_state idx CAP_STATE_FREE |
| 813 | put cap_rights idx 0 |
| 814 | put cap_expiry idx 0 |
| 815 | put cap_pid idx CAP_PARENT_NONE |
| 816 | put cap_pgen idx 0 |
| 817 | put cap_deliv idx 0 |
| 818 | cap_obs_emit_slot CAP_OBS_RECLAIM idx CAP_OK |
| 819 | ret CAP_OK |
| 820 | end |
| 821 | |
| 822 | fn cap_advance_clock auth view CapRef ticks ulong to int |
| 823 | if eq get cap_meta CAP_META_ALIVE 0 |
| 824 | cap_obs_emit_none CAP_OBS_CLOCK CAP_ERR_IO |
| 825 | ret CAP_ERR_IO |
| 826 | end |
| 827 | rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_CLOCK |
| 828 | if ne rc CAP_OK |
| 829 | cap_obs_emit_none CAP_OBS_CLOCK rc |
| 830 | ret rc |
| 831 | end |
| 832 | nxt = new ulong 1 0 |
| 833 | rc = cap_add_u64 get cap_meta CAP_META_CLOCK ticks nxt |
| 834 | if ne rc CAP_OK |
| 835 | cap_obs_emit_none CAP_OBS_CLOCK rc |
| 836 | ret rc |
| 837 | end |
| 838 | put cap_meta CAP_META_CLOCK get nxt 0 |
| 839 | cap_obs_emit_slot CAP_OBS_CLOCK ulong get auth cap_id CAP_OK |
| 840 | ret CAP_OK |
| 841 | end |
| 842 | |
| 843 | fn cap_bump_epoch auth view CapRef to int |
| 844 | if eq get cap_meta CAP_META_ALIVE 0 |
| 845 | cap_obs_emit_none CAP_OBS_EPOCH CAP_ERR_IO |
| 846 | ret CAP_ERR_IO |
| 847 | end |
| 848 | rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_EPOCH |
| 849 | if ne rc CAP_OK |
| 850 | cap_obs_emit_none CAP_OBS_EPOCH rc |
| 851 | ret rc |
| 852 | end |
| 853 | nxt = new ulong 1 0 |
| 854 | rc = cap_add_u64 get cap_meta CAP_META_EPOCH 1 nxt |
| 855 | if ne rc CAP_OK |
| 856 | cap_obs_emit_none CAP_OBS_EPOCH rc |
| 857 | ret rc |
| 858 | end |
| 859 | put cap_meta CAP_META_EPOCH get nxt 0 |
| 860 | cap_obs_emit_slot CAP_OBS_EPOCH ulong get auth cap_id CAP_OK |
| 861 | ret CAP_OK |
| 862 | end |
| 863 | |
| 864 | fn cap_kill to int |
| 865 | put cap_meta CAP_META_ALIVE 0 |
| 866 | cap_obs_emit_none CAP_OBS_KILL CAP_OK |
| 867 | ret CAP_OK |
| 868 | end |
| 869 | |
| 870 | fn cap_restart to int |
| 871 | fl = cap_table_floor |
| 872 | b = new ulong 1 0 |
| 873 | rc = cap_take_boot_gen fl b |
| 874 | if ne rc CAP_OK |
| 875 | cap_obs_emit_none CAP_OBS_RESTART rc |
| 876 | ret rc |
| 877 | end |
| 878 | rc = cap_bootstrap get b 0 |
| 879 | if ne rc CAP_OK |
| 880 | cap_obs_emit_none CAP_OBS_RESTART rc |
| 881 | ret rc |
| 882 | end |
| 883 | cap_fill_token |
| 884 | cap_obs_emit_slot CAP_OBS_RESTART 0 CAP_OK |
| 885 | ret CAP_OK |
| 886 | end |
| 887 | |
| 888 | fn cap_cognition_mint req view CapMint tok view uchar 32 to int |
| 889 | ret CAP_ERR_UNAUTHORIZED |
| 890 | end |
| 891 | |
| 892 | fn cap_cognition_admin op uint auth view CapRef target view CapRef to int |
| 893 | ret CAP_ERR_UNAUTHORIZED |
| 894 | end |
| 895 | |
| 896 | fn cap_force_generation cid uint gen ulong to int |
| 897 | if ge cid CAP_MAX |
| 898 | ret CAP_ERR_BOUNDS |
| 899 | end |
| 900 | idx = ulong cid |
| 901 | if eq get cap_state idx CAP_STATE_LIVE |
| 902 | ret CAP_ERR_STATE |
| 903 | end |
| 904 | if eq gen 0 |
| 905 | ret CAP_ERR_EXHAUSTED |
| 906 | end |
| 907 | put cap_gen idx gen |
| 908 | ret CAP_OK |
| 909 | end |
| 910 | |
| 911 | note Test seam for corrupt-table negative control (simulates non-live parent with live child) |
| 912 | fn cap_corrupt_slot_state cid uint st uint to int |
| 913 | if ge cid CAP_MAX |
| 914 | ret CAP_ERR_BOUNDS |
| 915 | end |
| 916 | put cap_state ulong cid st |
| 917 | ret CAP_OK |
| 918 | end |
| 919 | |
| 920 | note ---- Small I/O helpers for test drivers ---- |
| 921 | |
| 922 | fn cap_write_all fd int buf view uchar * to bool |
| 923 | total = len buf |
| 924 | off = ulong 0 |
| 925 | note Each write advances off by at least 1 byte on progress; 4096 covers small test lines. |
| 926 | while lt off total bound 4096 |
| 927 | n = write fd buf off sub total off |
| 928 | if le n 0 |
| 929 | ret false |
| 930 | end |
| 931 | off = add off ulong n |
| 932 | end |
| 933 | ret true |
| 934 | end |