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 / aegis / aegis_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/aegis/aegis_test.om: Pure-Omega test suite for AIENOS AEGIS |
| 3 | note (Capability Graph, Path Confinement, Intent/Grant Crypto, J-Space Worlds, Policy & Broker). |
| 4 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/aegis/aegis.om aienux/aegis/aegis_test.om |
| 5 | |
| 6 | text a_pass "PASS " |
| 7 | text a_fail "FAIL " |
| 8 | text a_nl "\n" |
| 9 | text a_t01 "aegis_lexical_path_confinement" |
| 10 | text a_t02 "aegis_cap_issue_sig_tamper_and_expiry" |
| 11 | text a_t03 "aegis_attenuated_delegation_and_depth_limit" |
| 12 | text a_t04 "aegis_cascading_subtree_revocation" |
| 13 | text a_t05 "aegis_intent_boundaries_and_operator_grant_crypto" |
| 14 | text a_t06 "aegis_standing_fetch_fs_read_and_negatives" |
| 15 | text a_t07 "aegis_high_impact_boundary_requires_grant" |
| 16 | text a_t08 "aegis_jspace_world_delta_and_rollback" |
| 17 | text a_t09 "aegis_broker_audit_ledger_integrity" |
| 18 | text a_t10 "aegis_mutant_control_skip_grant_caught" |
| 19 | text a_sum "AEGIS_TEST_PASS total=10 failed=0\n" |
| 20 | text a_fsum "AEGIS_TEST_FAIL\n" |
| 21 | |
| 22 | text p_ws "/workspace" |
| 23 | text p_ws_eq "/workspace" |
| 24 | text p_ws_sub "/workspace/project/file.om" |
| 25 | text p_ws_sub2 "/workspace/project" |
| 26 | text p_ws_sib "/workspace2/file.om" |
| 27 | text p_ws_trav "/workspace/../etc/passwd" |
| 28 | text p_rel "workspace/file.om" |
| 29 | text p_root "/" |
| 30 | text p_etc "/etc/passwd" |
| 31 | text p_host_ok "interplane.io" |
| 32 | text p_host_bad "evil.example" |
| 33 | text p_host_star "*" |
| 34 | text p_op_drake "operator-drake" |
| 35 | text p_k_ab "ab" |
| 36 | text p_v_c "c" |
| 37 | text p_k_a "a" |
| 38 | text p_v_bc "bc" |
| 39 | text p_dev_nvme "/dev/nvme0n1" |
| 40 | text p_empty "" |
| 41 | |
| 42 | fn awrite_all fd int buf view uchar * to bool |
| 43 | total = len buf |
| 44 | off = ulong 0 |
| 45 | while lt off total bound 4096 |
| 46 | n = write fd buf off sub total off |
| 47 | if le n 0 |
| 48 | ret false |
| 49 | end |
| 50 | off = add off ulong n |
| 51 | end |
| 52 | ret true |
| 53 | end |
| 54 | |
| 55 | fn areport name view uchar * ok bool to uint |
| 56 | if ok |
| 57 | w1 = awrite_all 1 a_pass |
| 58 | w2 = awrite_all 1 name |
| 59 | w3 = awrite_all 1 a_nl |
| 60 | if and w1 and w2 w3 |
| 61 | ret 0 |
| 62 | end |
| 63 | ret 1 |
| 64 | end |
| 65 | w1 = awrite_all 1 a_fail |
| 66 | w2 = awrite_all 1 name |
| 67 | w3 = awrite_all 1 a_nl |
| 68 | if and w1 and w2 w3 |
| 69 | ret 1 |
| 70 | end |
| 71 | ret 1 |
| 72 | end |
| 73 | |
| 74 | fn init_test_aegis op_sec edit uchar 32 cap_sec edit uchar 32 to void |
| 75 | for i ulong 32 |
| 76 | put op_sec i uchar add 0x10 and i 0x3f |
| 77 | put cap_sec i uchar add 0x80 and i 0x3f |
| 78 | end |
| 79 | aegis_reset op_sec cap_sec |
| 80 | end |
| 81 | |
| 82 | fn test_a01_path_within to bool |
| 83 | if not aegis_path_within p_ws_sub p_ws |
| 84 | ret false |
| 85 | end |
| 86 | if not aegis_path_within p_ws_eq p_ws |
| 87 | ret false |
| 88 | end |
| 89 | if not aegis_path_within p_ws_sub p_root |
| 90 | ret false |
| 91 | end |
| 92 | note Prefix sibling /workspace2 must NOT match /workspace |
| 93 | if aegis_path_within p_ws_sib p_ws |
| 94 | ret false |
| 95 | end |
| 96 | note Parent-dir traversal .. must be rejected |
| 97 | if aegis_path_within p_ws_trav p_ws |
| 98 | ret false |
| 99 | end |
| 100 | note Relative path must be rejected |
| 101 | if aegis_path_within p_rel p_ws |
| 102 | ret false |
| 103 | end |
| 104 | ret true |
| 105 | end |
| 106 | |
| 107 | fn test_a02_cap_sig_and_expiry to bool |
| 108 | op = new uchar 32 0 |
| 109 | cs = new uchar 32 0 |
| 110 | init_test_aegis op cs |
| 111 | s = aegis_cap_issue 100 AEGIS_SCOPE_FS 0 0 1000 |
| 112 | if lt s 0 |
| 113 | ret false |
| 114 | end |
| 115 | su = ulong s |
| 116 | aegis_cap_set_strings su p_ws part p_empty 0 0 |
| 117 | note Valid before expiry |
| 118 | if ne aegis_cap_validate su 500 AEGIS_OK |
| 119 | ret false |
| 120 | end |
| 121 | note Expired at 1001 |
| 122 | if ne aegis_cap_validate su 1001 AEGIS_ERR_EXPIRED |
| 123 | ret false |
| 124 | end |
| 125 | note Tamper 1 byte of stored signature -> must fail AEGIS_ERR_BAD_SIG |
| 126 | sb = mul su 32 |
| 127 | put aegis_cap_sig sb xor get aegis_cap_sig sb 0x01 |
| 128 | if ne aegis_cap_validate su 500 AEGIS_ERR_BAD_SIG |
| 129 | ret false |
| 130 | end |
| 131 | ret true |
| 132 | end |
| 133 | |
| 134 | fn test_a03_delegation_attenuation to bool |
| 135 | op = new uchar 32 0 |
| 136 | cs = new uchar 32 0 |
| 137 | init_test_aegis op cs |
| 138 | note Root RW cap on /workspace |
| 139 | r = aegis_cap_issue 100 AEGIS_SCOPE_FS 0 0 0 |
| 140 | if lt r 0 |
| 141 | ret false |
| 142 | end |
| 143 | ru = ulong r |
| 144 | aegis_cap_set_strings ru p_ws part p_empty 0 0 |
| 145 | note Attenuate to subdir /workspace/project, RO=1 -> OK |
| 146 | c1 = aegis_cap_delegate_scoped ru 200 AEGIS_SCOPE_FS 1 p_ws_sub2 |
| 147 | if lt c1 0 |
| 148 | ret false |
| 149 | end |
| 150 | note Attempt RO -> RW escalation from c1 -> REFUSED |
| 151 | if ne aegis_cap_delegate_scoped ulong c1 300 AEGIS_SCOPE_FS 0 p_ws_sub2 AEGIS_ERR_SCOPE |
| 152 | ret false |
| 153 | end |
| 154 | note Attempt directory escape /etc/passwd -> REFUSED |
| 155 | if ne aegis_cap_delegate_scoped ru 300 AEGIS_SCOPE_FS 1 p_etc AEGIS_ERR_SCOPE |
| 156 | ret false |
| 157 | end |
| 158 | note Chain up to max depth (4), 5th delegation -> AEGIS_ERR_DEPTH |
| 159 | c2 = aegis_cap_delegate_scoped ulong c1 201 AEGIS_SCOPE_FS 1 p_ws_sub2 |
| 160 | c3 = aegis_cap_delegate_scoped ulong c2 202 AEGIS_SCOPE_FS 1 p_ws_sub2 |
| 161 | c4 = aegis_cap_delegate_scoped ulong c3 203 AEGIS_SCOPE_FS 1 p_ws_sub2 |
| 162 | if or lt c2 0 or lt c3 0 lt c4 0 |
| 163 | ret false |
| 164 | end |
| 165 | if ne aegis_cap_delegate_scoped ulong c4 204 AEGIS_SCOPE_FS 1 p_ws_sub2 AEGIS_ERR_DEPTH |
| 166 | ret false |
| 167 | end |
| 168 | ret true |
| 169 | end |
| 170 | |
| 171 | fn test_a04_subtree_revocation to bool |
| 172 | op = new uchar 32 0 |
| 173 | cs = new uchar 32 0 |
| 174 | init_test_aegis op cs |
| 175 | root = aegis_cap_issue 100 AEGIS_SCOPE_FS 0 0 0 |
| 176 | aegis_cap_set_strings ulong root p_ws part p_empty 0 0 |
| 177 | sib = aegis_cap_issue 101 AEGIS_SCOPE_FS 1 0 0 |
| 178 | aegis_cap_set_strings ulong sib p_ws part p_empty 0 0 |
| 179 | child = aegis_cap_delegate_scoped ulong root 200 AEGIS_SCOPE_FS 0 p_ws_sub2 |
| 180 | gchild = aegis_cap_delegate_scoped ulong child 300 AEGIS_SCOPE_FS 1 p_ws_sub2 |
| 181 | if or lt child 0 lt gchild 0 |
| 182 | ret false |
| 183 | end |
| 184 | if ne aegis_cap_revoke ulong root AEGIS_OK |
| 185 | ret false |
| 186 | end |
| 187 | if ne aegis_cap_validate ulong root 0 AEGIS_ERR_REVOKED |
| 188 | ret false |
| 189 | end |
| 190 | if ne aegis_cap_validate ulong child 0 AEGIS_ERR_REVOKED |
| 191 | ret false |
| 192 | end |
| 193 | if ne aegis_cap_validate ulong gchild 0 AEGIS_ERR_REVOKED |
| 194 | ret false |
| 195 | end |
| 196 | note Unrelated sibling remains valid |
| 197 | if ne aegis_cap_validate ulong sib 0 AEGIS_OK |
| 198 | ret false |
| 199 | end |
| 200 | ret true |
| 201 | end |
| 202 | |
| 203 | fn test_a05_intent_and_grant_crypto to bool |
| 204 | op = new uchar 32 0 |
| 205 | cs = new uchar 32 0 |
| 206 | bad = new uchar 32 0x77 |
| 207 | init_test_aegis op cs |
| 208 | note Parameter boundary separation: ("ab","c") != ("a","bc") |
| 209 | aegis_intent_hash_kv 42 p_k_ab p_v_c |
| 210 | id1 = new uchar 16 0 |
| 211 | for i ulong 16 |
| 212 | put id1 i get aegis_cur_iid i |
| 213 | end |
| 214 | aegis_intent_hash_kv 42 p_k_a p_v_bc |
| 215 | id2 = new uchar 16 0 |
| 216 | for j ulong 16 |
| 217 | put id2 j get aegis_cur_iid j |
| 218 | end |
| 219 | if aegis_eq16 id1 id2 |
| 220 | ret false |
| 221 | end |
| 222 | note Valid grant verifies |
| 223 | aegis_grant_issue_with_secret p_op_drake 1700000000 op |
| 224 | if ne aegis_grant_verify_current AEGIS_OK |
| 225 | ret false |
| 226 | end |
| 227 | note Grant signed with wrong secret rejected |
| 228 | aegis_grant_issue_with_secret p_op_drake 1700000000 bad |
| 229 | if ne aegis_grant_verify_current AEGIS_ERR_GRANT_SIG |
| 230 | ret false |
| 231 | end |
| 232 | note Grant for different intent rejected |
| 233 | aegis_grant_issue_with_secret p_op_drake 1700000000 op |
| 234 | put aegis_cur_iid 0 xor get aegis_cur_iid 0 0xff |
| 235 | if ne aegis_grant_verify_current AEGIS_ERR_GRANT_INTENT |
| 236 | ret false |
| 237 | end |
| 238 | ret true |
| 239 | end |
| 240 | |
| 241 | fn test_a06_standing_fetch_and_fs_read to bool |
| 242 | op = new uchar 32 0 |
| 243 | cs = new uchar 32 0 |
| 244 | init_test_aegis op cs |
| 245 | aegis_grant_clear |
| 246 | sf = aegis_cap_issue 10 AEGIS_SCOPE_STANDING_FETCH 0 0 0 |
| 247 | aegis_cap_set_strings ulong sf p_host_ok part p_empty 0 0 |
| 248 | fs = aegis_cap_issue 10 AEGIS_SCOPE_FS 1 0 0 |
| 249 | aegis_cap_set_strings ulong fs p_ws part p_empty 0 0 |
| 250 | aegis_dispatch_ctx 0 0 100 0 |
| 251 | note StandingFetch to interplane.io approved autonomously without grant |
| 252 | if ne aegis_broker_dispatch 10 ulong sf aegis_act_net_fetch p_host_ok AEGIS_OK |
| 253 | ret false |
| 254 | end |
| 255 | note Wrong host refused |
| 256 | if ne aegis_broker_dispatch 10 ulong sf aegis_act_net_fetch p_host_bad AEGIS_ERR_SCOPE |
| 257 | ret false |
| 258 | end |
| 259 | note Wrong holder refused |
| 260 | if ne aegis_broker_dispatch 99 ulong sf aegis_act_net_fetch p_host_ok AEGIS_ERR_HOLDER |
| 261 | ret false |
| 262 | end |
| 263 | note fs.read within /workspace approved autonomously |
| 264 | if ne aegis_broker_dispatch 10 ulong fs aegis_act_fs_read p_ws_sub AEGIS_OK |
| 265 | ret false |
| 266 | end |
| 267 | note fs.write on RO cap refused by scope |
| 268 | if ne aegis_broker_dispatch 10 ulong fs aegis_act_fs_write p_ws_sub AEGIS_ERR_SCOPE |
| 269 | ret false |
| 270 | end |
| 271 | ret true |
| 272 | end |
| 273 | |
| 274 | fn test_a07_high_impact_requires_grant to bool |
| 275 | op = new uchar 32 0 |
| 276 | cs = new uchar 32 0 |
| 277 | bad = new uchar 32 0x55 |
| 278 | init_test_aegis op cs |
| 279 | aegis_grant_clear |
| 280 | adm = aegis_cap_issue 20 AEGIS_SCOPE_SYSADMIN 0 0 0 |
| 281 | aegis_dispatch_ctx 0 0 200 0 |
| 282 | note disk.format without grant -> AEGIS_ERR_MISSING_GRANT + DEC_REQUIRES_OP |
| 283 | if ne aegis_broker_dispatch 20 ulong adm aegis_act_disk_format p_dev_nvme AEGIS_ERR_MISSING_GRANT |
| 284 | ret false |
| 285 | end |
| 286 | if ne get aegis_last_dec 0 AEGIS_DEC_REQUIRES_OP |
| 287 | ret false |
| 288 | end |
| 289 | note Forged grant -> AEGIS_ERR_GRANT_SIG + DEC_REJECTED |
| 290 | aegis_compute_intent_id 20 ulong adm aegis_act_disk_format p_dev_nvme |
| 291 | aegis_grant_issue_with_secret p_op_drake 200 bad |
| 292 | if ne aegis_broker_dispatch 20 ulong adm aegis_act_disk_format p_dev_nvme AEGIS_ERR_GRANT_SIG |
| 293 | ret false |
| 294 | end |
| 295 | if ne get aegis_last_dec 0 AEGIS_DEC_REJECTED |
| 296 | ret false |
| 297 | end |
| 298 | note Valid operator grant -> AEGIS_OK + DEC_APPROVED |
| 299 | aegis_compute_intent_id 20 ulong adm aegis_act_disk_format p_dev_nvme |
| 300 | aegis_grant_issue_with_secret p_op_drake 200 op |
| 301 | if ne aegis_broker_dispatch 20 ulong adm aegis_act_disk_format p_dev_nvme AEGIS_OK |
| 302 | ret false |
| 303 | end |
| 304 | if ne get aegis_last_dec 0 AEGIS_DEC_APPROVED |
| 305 | ret false |
| 306 | end |
| 307 | ret true |
| 308 | end |
| 309 | |
| 310 | fn test_a08_jspace_world_delta to bool |
| 311 | op = new uchar 32 0 |
| 312 | cs = new uchar 32 0 |
| 313 | init_test_aegis op cs |
| 314 | aegis_grant_clear |
| 315 | fs = aegis_cap_issue 30 AEGIS_SCOPE_FS 0 0 0 |
| 316 | aegis_cap_set_strings ulong fs p_ws part p_empty 0 0 |
| 317 | wi = aegis_world_create 777 |
| 318 | if lt wi 0 |
| 319 | ret false |
| 320 | end |
| 321 | note Reversible fs.write to active World 777 approved without grant! |
| 322 | aegis_dispatch_ctx 777 1 300 0 |
| 323 | if ne aegis_broker_dispatch 30 ulong fs aegis_act_fs_write p_ws_sub AEGIS_OK |
| 324 | ret false |
| 325 | end |
| 326 | if or ne get aegis_w_deltas ulong wi 1 ne get aegis_w_writes ulong wi 1 |
| 327 | ret false |
| 328 | end |
| 329 | note Rollback clears deltas and transitions World to ROLLEDBACK |
| 330 | if ne aegis_world_rollback 777 AEGIS_OK |
| 331 | ret false |
| 332 | end |
| 333 | if or ne get aegis_w_deltas ulong wi 0 ne get aegis_w_status ulong wi AEGIS_WORLD_ROLLEDBACK |
| 334 | ret false |
| 335 | end |
| 336 | note Dispatching again to rolled-back World falls back to requiring OperatorGrant! |
| 337 | if ne aegis_broker_dispatch 30 ulong fs aegis_act_fs_write p_ws_sub AEGIS_ERR_MISSING_GRANT |
| 338 | ret false |
| 339 | end |
| 340 | ret true |
| 341 | end |
| 342 | |
| 343 | fn test_a09_audit_ledger to bool |
| 344 | op = new uchar 32 0 |
| 345 | cs = new uchar 32 0 |
| 346 | init_test_aegis op cs |
| 347 | aegis_grant_clear |
| 348 | fs = aegis_cap_issue 40 AEGIS_SCOPE_FS 0 0 0 |
| 349 | aegis_cap_set_strings ulong fs p_ws part p_empty 0 0 |
| 350 | aegis_dispatch_ctx 0 0 401 0 |
| 351 | r1 = aegis_broker_dispatch 40 ulong fs aegis_act_fs_read p_ws_sub |
| 352 | aegis_dispatch_ctx 0 0 402 0 |
| 353 | r2 = aegis_broker_dispatch 40 ulong fs aegis_act_fs_write p_ws_sub |
| 354 | aegis_dispatch_ctx 0 0 403 0 |
| 355 | r3 = aegis_broker_dispatch 40 ulong fs aegis_act_fs_read p_etc |
| 356 | if or ne r1 AEGIS_OK or ne r2 AEGIS_ERR_MISSING_GRANT ne r3 AEGIS_ERR_SCOPE |
| 357 | ret false |
| 358 | end |
| 359 | if ne get aegis_audit_cnt 0 3 |
| 360 | ret false |
| 361 | end |
| 362 | if or ne get aegis_audit_dec 0 AEGIS_DEC_APPROVED ne get aegis_audit_exec 0 1 |
| 363 | ret false |
| 364 | end |
| 365 | if or ne get aegis_audit_dec 1 AEGIS_DEC_REQUIRES_OP ne get aegis_audit_exec 1 0 |
| 366 | ret false |
| 367 | end |
| 368 | if or ne get aegis_audit_dec 2 AEGIS_DEC_REJECTED ne get aegis_audit_ts 2 403 |
| 369 | ret false |
| 370 | end |
| 371 | ret true |
| 372 | end |
| 373 | |
| 374 | fn test_a10_mutant_skip_grant to bool |
| 375 | op = new uchar 32 0 |
| 376 | cs = new uchar 32 0 |
| 377 | init_test_aegis op cs |
| 378 | aegis_grant_clear |
| 379 | adm = aegis_cap_issue 50 AEGIS_SCOPE_SYSADMIN 0 0 0 |
| 380 | aegis_dispatch_ctx 0 0 500 0 |
| 381 | note With mutant OFF: kernel.replace without grant MUST be refused |
| 382 | if ne aegis_broker_dispatch 50 ulong adm aegis_act_kern_rep p_dev_nvme AEGIS_ERR_MISSING_GRANT |
| 383 | ret false |
| 384 | end |
| 385 | note With mutant ON (skip_grant=1): unapproved kernel.replace slips through -> mutant detected! |
| 386 | aegis_set_mutant_skip_grant 1 |
| 387 | rc_mut = aegis_broker_dispatch 50 ulong adm aegis_act_kern_rep p_dev_nvme |
| 388 | aegis_set_mutant_skip_grant 0 |
| 389 | if eq rc_mut AEGIS_ERR_MISSING_GRANT |
| 390 | ret false |
| 391 | end |
| 392 | ret true |
| 393 | end |
| 394 | |
| 395 | fn main to int |
| 396 | fails = uint 0 |
| 397 | fails = add fails areport a_t01 test_a01_path_within |
| 398 | fails = add fails areport a_t02 test_a02_cap_sig_and_expiry |
| 399 | fails = add fails areport a_t03 test_a03_delegation_attenuation |
| 400 | fails = add fails areport a_t04 test_a04_subtree_revocation |
| 401 | fails = add fails areport a_t05 test_a05_intent_and_grant_crypto |
| 402 | fails = add fails areport a_t06 test_a06_standing_fetch_and_fs_read |
| 403 | fails = add fails areport a_t07 test_a07_high_impact_requires_grant |
| 404 | fails = add fails areport a_t08 test_a08_jspace_world_delta |
| 405 | fails = add fails areport a_t09 test_a09_audit_ledger |
| 406 | fails = add fails areport a_t10 test_a10_mutant_skip_grant |
| 407 | if eq fails 0 |
| 408 | w = awrite_all 1 a_sum |
| 409 | if w |
| 410 | ret 0 |
| 411 | end |
| 412 | ret 1 |
| 413 | end |
| 414 | w2 = awrite_all 1 a_fsum |
| 415 | ret 1 |
| 416 | end |