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 / contain_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/capability/contain_test.om: Behavioral self-test for AEGIS Containment Gate. |
| 3 | note Covers invariants I1..I5 from native/capability/tests/contain_test.c. |
| 4 | note Compose with: si/directives/om2.om aienux/capability/lib.om aienux/capability/contain.om aienux/capability/contain_test.om om/tls/sha2.om |
| 5 | |
| 6 | text c_pass "PASS " |
| 7 | text c_fail "FAIL " |
| 8 | text c_nl "\n" |
| 9 | text c_t01 "create_executor_and_duplicate_refused" |
| 10 | text c_t02 "canonical_request_malformed_and_replay" |
| 11 | text c_t03 "auto_grant_and_execute_leaf_revoke" |
| 12 | text c_t04 "non_leaf_escalates_and_protected_denied" |
| 13 | text c_t05 "human_resolve_secret_and_replay" |
| 14 | text c_t06 "non_revoke_unavailable_and_budget_window" |
| 15 | text c_t07 "cascade_scope_exceeded_makes_gate_inert" |
| 16 | text c_t08 "kill_untrusts_lineage_and_escalates" |
| 17 | text c_sum "CONTAIN_TEST_PASS total=8 failed=0\n" |
| 18 | text c_fsum "CONTAIN_TEST_FAIL\n" |
| 19 | |
| 20 | fn creport name view uchar * ok bool to uint |
| 21 | if ok |
| 22 | w1 = cap_write_all 1 c_pass |
| 23 | w2 = cap_write_all 1 name |
| 24 | w3 = cap_write_all 1 c_nl |
| 25 | if and w1 and w2 w3 |
| 26 | ret 0 |
| 27 | end |
| 28 | ret 1 |
| 29 | end |
| 30 | w1 = cap_write_all 1 c_fail |
| 31 | w2 = cap_write_all 1 name |
| 32 | w3 = cap_write_all 1 c_nl |
| 33 | if and w1 and w2 w3 |
| 34 | ret 1 |
| 35 | end |
| 36 | ret 1 |
| 37 | end |
| 38 | |
| 39 | fn mk_req buf edit uchar 152 rid ulong ctype uchar princ uint cid uint cgen ulong to void |
| 40 | for i ulong 152 |
| 41 | put buf i 0 |
| 42 | end |
| 43 | con_put64 buf 0 1 |
| 44 | put buf 8 ctype |
| 45 | put buf 9 4 |
| 46 | con_put16 buf 10 100 |
| 47 | con_put32 buf 12 princ |
| 48 | con_put32 buf 16 cid |
| 49 | con_put64 buf 20 cgen |
| 50 | con_put64 buf 92 rid |
| 51 | con_put64 buf 100 rid |
| 52 | con_put16 buf 148 0 |
| 53 | put buf 150 CON_REQ_VERSION |
| 54 | put buf 151 0 |
| 55 | end |
| 56 | |
| 57 | fn setup_gate off edit CapRef to bool |
| 58 | if ne cap_start CAP_OK |
| 59 | ret false |
| 60 | end |
| 61 | cap_obs_clear |
| 62 | cap_set_observer true |
| 63 | if ne cap_office off CAP_OK |
| 64 | ret false |
| 65 | end |
| 66 | contain_policy_reset |
| 67 | contain_policy_set CON_REVOKE_CAPABILITY CON_GRANT |
| 68 | if ne contain_create off CON_OK |
| 69 | ret false |
| 70 | end |
| 71 | ret true |
| 72 | end |
| 73 | |
| 74 | fn mint_ordinary off view CapRef subj uint rgt uint dst edit CapRef to bool |
| 75 | m = new CapMint 1 subj 500 rgt 0 CAP_PARENT_NONE 0 get off cap_id get off generation |
| 76 | ret eq cap_mint m dst CAP_OK |
| 77 | end |
| 78 | |
| 79 | fn mint_child par view CapRef subj uint rgt uint dst edit CapRef to bool |
| 80 | m = new CapMint 2 subj 500 rgt 0 get par cap_id get par generation get par cap_id get par generation |
| 81 | ret eq cap_mint m dst CAP_OK |
| 82 | end |
| 83 | |
| 84 | fn ct_01_create_and_dup to bool |
| 85 | off = new CapRef 0 0 |
| 86 | if not setup_gate off |
| 87 | ret false |
| 88 | end |
| 89 | ex = new CapRef 0 0 |
| 90 | if ne contain_executor ex CON_OK |
| 91 | ret false |
| 92 | end |
| 93 | ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 94 | if ne cap_inspect ex ent CAP_OK |
| 95 | ret false |
| 96 | end |
| 97 | if or ne get ent subject CON_SUBJ ne get ent rights CAP_RIGHT_REVOKE |
| 98 | ret false |
| 99 | end |
| 100 | note Second create on same authority instance must fail with CON_ERR_EXISTS (I5.a) |
| 101 | if ne contain_create off CON_ERR_EXISTS |
| 102 | ret false |
| 103 | end |
| 104 | if contain_inert |
| 105 | ret false |
| 106 | end |
| 107 | ret true |
| 108 | end |
| 109 | |
| 110 | fn ct_02_malformed_and_replay to bool |
| 111 | off = new CapRef 0 0 |
| 112 | if not setup_gate off |
| 113 | ret false |
| 114 | end |
| 115 | c1 = new CapRef 0 0 |
| 116 | if not mint_ordinary off 10 CAP_RIGHT_READ c1 |
| 117 | ret false |
| 118 | end |
| 119 | rec = new ulong 16 0 |
| 120 | rdig = new uchar 32 0 |
| 121 | note Short buffer denied with WHY_BAD_REQUEST |
| 122 | shortb = new uchar 16 0 |
| 123 | if ne contain_submit_bytes shortb rec rdig CON_OK |
| 124 | ret false |
| 125 | end |
| 126 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_BAD_REQUEST |
| 127 | ret false |
| 128 | end |
| 129 | note Good request succeeds, replay with same request_id is denied |
| 130 | req = new uchar 152 0 |
| 131 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation |
| 132 | if ne contain_submit_bytes req rec rdig CON_OK |
| 133 | ret false |
| 134 | end |
| 135 | if ne con_rec_status rec CON_GRANT |
| 136 | ret false |
| 137 | end |
| 138 | if ne contain_submit_bytes req rec rdig CON_OK |
| 139 | ret false |
| 140 | end |
| 141 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_REPLAYED_REQUEST |
| 142 | ret false |
| 143 | end |
| 144 | note Tampered digest denied with WHY_BAD_REQUEST |
| 145 | mk_req req 2 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation |
| 146 | bad_dig = new uchar 32 0xaa |
| 147 | if ne contain_submit req bad_dig rec rdig CON_OK |
| 148 | ret false |
| 149 | end |
| 150 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_BAD_REQUEST |
| 151 | ret false |
| 152 | end |
| 153 | ret true |
| 154 | end |
| 155 | |
| 156 | fn ct_03_auto_grant_and_execute to bool |
| 157 | off = new CapRef 0 0 |
| 158 | if not setup_gate off |
| 159 | ret false |
| 160 | end |
| 161 | c1 = new CapRef 0 0 |
| 162 | if not mint_ordinary off 10 CAP_RIGHT_READ c1 |
| 163 | ret false |
| 164 | end |
| 165 | req = new uchar 152 0 |
| 166 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation |
| 167 | dig = new uchar 32 0 |
| 168 | con_req_digest req dig |
| 169 | rec = new ulong 16 0 |
| 170 | rdig = new uchar 32 0 |
| 171 | if ne contain_submit req dig rec rdig CON_OK |
| 172 | ret false |
| 173 | end |
| 174 | if or ne con_rec_status rec CON_GRANT ne get rec CON_REC_WHY ulong CON_WHY_POLICY_GRANT |
| 175 | ret false |
| 176 | end |
| 177 | did = get rec CON_REC_DEC_ID |
| 178 | if ne contain_execute did rec rdig CON_OK |
| 179 | ret false |
| 180 | end |
| 181 | if or ne con_rec_status rec CON_DONE ne get rec CON_REC_RESOURCE 1 |
| 182 | ret false |
| 183 | end |
| 184 | note Capability is now revoked |
| 185 | ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 186 | if ne cap_validate c1 10 500 CAP_RIGHT_READ ent CAP_ERR_REVOKED |
| 187 | ret false |
| 188 | end |
| 189 | note Re-executing same decision is refused |
| 190 | if ne contain_execute did rec rdig CON_ERR_REFUSED |
| 191 | ret false |
| 192 | end |
| 193 | if ne get rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY |
| 194 | ret false |
| 195 | end |
| 196 | ret true |
| 197 | end |
| 198 | |
| 199 | fn ct_04_non_leaf_and_protected to bool |
| 200 | off = new CapRef 0 0 |
| 201 | if not setup_gate off |
| 202 | ret false |
| 203 | end |
| 204 | par = new CapRef 0 0 |
| 205 | if not mint_ordinary off 10 or CAP_RIGHT_READ CAP_RIGHT_DELEGATE par |
| 206 | ret false |
| 207 | end |
| 208 | ch = new CapRef 0 0 |
| 209 | if not mint_child par 11 CAP_RIGHT_READ ch |
| 210 | ret false |
| 211 | end |
| 212 | req = new uchar 152 0 |
| 213 | rec = new ulong 16 0 |
| 214 | rdig = new uchar 32 0 |
| 215 | note Non-leaf target must ESCALATE with WHY_NOT_LEAF |
| 216 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get par cap_id get par generation |
| 217 | if ne contain_submit_bytes req rec rdig CON_OK |
| 218 | ret false |
| 219 | end |
| 220 | if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_NOT_LEAF |
| 221 | ret false |
| 222 | end |
| 223 | note Office cap (protected target) must DENY with WHY_PROTECTED_TARGET |
| 224 | mk_req req 2 CON_REVOKE_CAPABILITY 0 get off cap_id get off generation |
| 225 | if ne contain_submit_bytes req rec rdig CON_OK |
| 226 | ret false |
| 227 | end |
| 228 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_PROTECTED_TARGET |
| 229 | ret false |
| 230 | end |
| 231 | note AEGIS subject (41) must DENY with WHY_PROTECTED_TARGET |
| 232 | mk_req req 3 CON_REVOKE_CAPABILITY CON_SUBJ_AEGIS get ch cap_id get ch generation |
| 233 | if ne contain_submit_bytes req rec rdig CON_OK |
| 234 | ret false |
| 235 | end |
| 236 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_PROTECTED_TARGET |
| 237 | ret false |
| 238 | end |
| 239 | note Subject mismatch must DENY with WHY_SUBJECT_MISMATCH |
| 240 | mk_req req 4 CON_REVOKE_CAPABILITY 99 get ch cap_id get ch generation |
| 241 | if ne contain_submit_bytes req rec rdig CON_OK |
| 242 | ret false |
| 243 | end |
| 244 | if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_SUBJECT_MISMATCH |
| 245 | ret false |
| 246 | end |
| 247 | ret true |
| 248 | end |
| 249 | |
| 250 | fn ct_05_human_resolve to bool |
| 251 | off = new CapRef 0 0 |
| 252 | if not setup_gate off |
| 253 | ret false |
| 254 | end |
| 255 | contain_policy_set CON_REVOKE_CAPABILITY CON_ESCALATE |
| 256 | c1 = new CapRef 0 0 |
| 257 | if not mint_ordinary off 10 CAP_RIGHT_READ c1 |
| 258 | ret false |
| 259 | end |
| 260 | req = new uchar 152 0 |
| 261 | rec = new ulong 16 0 |
| 262 | rdig = new uchar 32 0 |
| 263 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation |
| 264 | if ne contain_submit_bytes req rec rdig CON_OK |
| 265 | ret false |
| 266 | end |
| 267 | if ne con_rec_status rec CON_ESCALATE |
| 268 | ret false |
| 269 | end |
| 270 | did = get rec CON_REC_DEC_ID |
| 271 | note Bad secret refused, decision stays escalated |
| 272 | bad_sec = new uchar 32 0x55 |
| 273 | if ne contain_resolve did true bad_sec rec rdig CON_ERR_REFUSED |
| 274 | ret false |
| 275 | end |
| 276 | if ne get rec CON_REC_WHY ulong CON_WHY_BAD_SECRET |
| 277 | ret false |
| 278 | end |
| 279 | note Valid office secret approves |
| 280 | good_sec = new uchar 32 0 |
| 281 | cap_copy_token good_sec |
| 282 | if ne contain_resolve did true good_sec rec rdig CON_OK |
| 283 | ret false |
| 284 | end |
| 285 | if or ne con_rec_status rec CON_GRANT ne get rec CON_REC_WHY ulong CON_WHY_HUMAN_GRANT |
| 286 | ret false |
| 287 | end |
| 288 | note Second resolve on already-granted decision refused |
| 289 | if ne contain_resolve did false good_sec rec rdig CON_ERR_REFUSED |
| 290 | ret false |
| 291 | end |
| 292 | note Execute human-approved grant succeeds and carries RF_HUMAN |
| 293 | if ne contain_execute did rec rdig CON_OK |
| 294 | ret false |
| 295 | end |
| 296 | if or ne con_rec_status rec CON_DONE eq and con_rec_gate_flags rec CON_RF_HUMAN 0 |
| 297 | ret false |
| 298 | end |
| 299 | ret true |
| 300 | end |
| 301 | |
| 302 | fn ct_06_non_revoke_and_budget to bool |
| 303 | off = new CapRef 0 0 |
| 304 | if not setup_gate off |
| 305 | ret false |
| 306 | end |
| 307 | contain_policy_set CON_FREEZE_PRINCIPAL CON_GRANT |
| 308 | req = new uchar 152 0 |
| 309 | rec = new ulong 16 0 |
| 310 | rdig = new uchar 32 0 |
| 311 | note Non-revoke type cannot auto-grant (escalates with WHY_NOT_AUTO_ELIGIBLE) |
| 312 | mk_req req 1 CON_FREEZE_PRINCIPAL 10 CON_CAP_NONE 0 |
| 313 | if ne contain_submit_bytes req rec rdig CON_OK |
| 314 | ret false |
| 315 | end |
| 316 | if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_NOT_AUTO_ELIGIBLE |
| 317 | ret false |
| 318 | end |
| 319 | did = get rec CON_REC_DEC_ID |
| 320 | sec = new uchar 32 0 |
| 321 | cap_copy_token sec |
| 322 | if ne contain_resolve did true sec rec rdig CON_OK |
| 323 | ret false |
| 324 | end |
| 325 | note Executing non-revoke without seam returns UNAVAILABLE |
| 326 | if ne contain_execute did rec rdig CON_OK |
| 327 | ret false |
| 328 | end |
| 329 | if or ne con_rec_status rec CON_UNAVAILABLE ne get rec CON_REC_WHY ulong CON_WHY_NO_EXECUTOR |
| 330 | ret false |
| 331 | end |
| 332 | note Exhaust auto-grant budget (8 grants per 64 decisions) |
| 333 | rid = ulong 2 |
| 334 | c = new CapRef 0 0 |
| 335 | for i ulong 8 |
| 336 | if not mint_ordinary off 20 CAP_RIGHT_READ c |
| 337 | ret false |
| 338 | end |
| 339 | mk_req req rid CON_REVOKE_CAPABILITY 20 get c cap_id get c generation |
| 340 | if ne contain_submit_bytes req rec rdig CON_OK |
| 341 | ret false |
| 342 | end |
| 343 | if ne con_rec_status rec CON_GRANT |
| 344 | ret false |
| 345 | end |
| 346 | rid = add rid 1 |
| 347 | end |
| 348 | note 9th grant in same window must ESCALATE with WHY_BUDGET |
| 349 | c9 = new CapRef 0 0 |
| 350 | if not mint_ordinary off 20 CAP_RIGHT_READ c9 |
| 351 | ret false |
| 352 | end |
| 353 | mk_req req rid CON_REVOKE_CAPABILITY 20 get c9 cap_id get c9 generation |
| 354 | if ne contain_submit_bytes req rec rdig CON_OK |
| 355 | ret false |
| 356 | end |
| 357 | if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_BUDGET |
| 358 | ret false |
| 359 | end |
| 360 | ret true |
| 361 | end |
| 362 | |
| 363 | fn ct_07_cascade_makes_inert to bool |
| 364 | off = new CapRef 0 0 |
| 365 | if not setup_gate off |
| 366 | ret false |
| 367 | end |
| 368 | par = new CapRef 0 0 |
| 369 | if not mint_ordinary off 10 or CAP_RIGHT_READ CAP_RIGHT_DELEGATE par |
| 370 | ret false |
| 371 | end |
| 372 | req = new uchar 152 0 |
| 373 | rec = new ulong 16 0 |
| 374 | rdig = new uchar 32 0 |
| 375 | note Grant while par is still a leaf |
| 376 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get par cap_id get par generation |
| 377 | if ne contain_submit_bytes req rec rdig CON_OK |
| 378 | ret false |
| 379 | end |
| 380 | if ne con_rec_status rec CON_GRANT |
| 381 | ret false |
| 382 | end |
| 383 | did = get rec CON_REC_DEC_ID |
| 384 | note Mint child with observer disabled so lineage shadow does not see it, forcing cascade on revoke! |
| 385 | cap_set_observer false |
| 386 | ch = new CapRef 0 0 |
| 387 | if not mint_child par 11 CAP_RIGHT_READ ch |
| 388 | ret false |
| 389 | end |
| 390 | cap_set_observer true |
| 391 | if ne contain_execute did rec rdig CON_OK |
| 392 | ret false |
| 393 | end |
| 394 | if ne con_rec_status rec CON_FAILED_SCOPE |
| 395 | ret false |
| 396 | end |
| 397 | if not contain_inert |
| 398 | ret false |
| 399 | end |
| 400 | if eq and con_rec_gate_flags rec CON_RF_INERT 0 |
| 401 | ret false |
| 402 | end |
| 403 | note Subsequent revoke request must ESCALATE because gate is inert |
| 404 | c2 = new CapRef 0 0 |
| 405 | if not mint_ordinary off 12 CAP_RIGHT_READ c2 |
| 406 | ret false |
| 407 | end |
| 408 | mk_req req 2 CON_REVOKE_CAPABILITY 12 get c2 cap_id get c2 generation |
| 409 | if ne contain_submit_bytes req rec rdig CON_OK |
| 410 | ret false |
| 411 | end |
| 412 | if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_LINEAGE_UNTRUSTED |
| 413 | ret false |
| 414 | end |
| 415 | ret true |
| 416 | end |
| 417 | |
| 418 | fn ct_08_kill_untrusts_lineage to bool |
| 419 | off = new CapRef 0 0 |
| 420 | if not setup_gate off |
| 421 | ret false |
| 422 | end |
| 423 | c1 = new CapRef 0 0 |
| 424 | if not mint_ordinary off 10 CAP_RIGHT_READ c1 |
| 425 | ret false |
| 426 | end |
| 427 | if ne cap_kill CAP_OK |
| 428 | ret false |
| 429 | end |
| 430 | req = new uchar 152 0 |
| 431 | rec = new ulong 16 0 |
| 432 | rdig = new uchar 32 0 |
| 433 | mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation |
| 434 | if ne contain_submit_bytes req rec rdig CON_OK |
| 435 | ret false |
| 436 | end |
| 437 | if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_LINEAGE_UNTRUSTED |
| 438 | ret false |
| 439 | end |
| 440 | ret true |
| 441 | end |
| 442 | |
| 443 | fn main to int |
| 444 | fails = uint 0 |
| 445 | fails = add fails creport c_t01 ct_01_create_and_dup |
| 446 | fails = add fails creport c_t02 ct_02_malformed_and_replay |
| 447 | fails = add fails creport c_t03 ct_03_auto_grant_and_execute |
| 448 | fails = add fails creport c_t04 ct_04_non_leaf_and_protected |
| 449 | fails = add fails creport c_t05 ct_05_human_resolve |
| 450 | fails = add fails creport c_t06 ct_06_non_revoke_and_budget |
| 451 | fails = add fails creport c_t07 ct_07_cascade_makes_inert |
| 452 | fails = add fails creport c_t08 ct_08_kill_untrusts_lineage |
| 453 | if eq fails 0 |
| 454 | w = cap_write_all 1 c_sum |
| 455 | if w |
| 456 | ret 0 |
| 457 | end |
| 458 | ret 1 |
| 459 | end |
| 460 | w = cap_write_all 1 c_fsum |
| 461 | if w |
| 462 | ret 1 |
| 463 | end |
| 464 | ret 1 |
| 465 | end |