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/aegis-runtime-mv/blob/main/om/policy.om # aegis-runtime-mv Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aegis-runtime-mv / om / policy.om` 485 lines ยท 11315 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note core/aegis/policy.om: AEGIS invariant and authority gate (pure OM-2 library unit). 3 note Converted from aegis-runtime-mv/src/auth.rs, src/enforcement.rs, src/policy_guard.rs, 4 note and src/defense/decision.rs. 5 note Fail-closed capability verification per route/action, constant-time operator token check, 6 note SHA-256 keyed envelope authentication with monotonic anti-replay nonces, privilege 7 note escalation refusal, skill/command allowlist membrane, and containment levels. 8 9 const POL_MIN_TOKEN_LEN ulong 32 10 const POL_MAX_TOKEN_LEN ulong 64 11 const POL_MAX_PRINCIPALS ulong 32 12 13 note Deterministic policy decision codes 14 const POL_ALLOW ulong 0 15 const POL_ALLOW_RESTRICTED ulong 1 16 const POL_REQUIRE_APPROVAL ulong 2 17 const POL_DENY_NO_TOKEN_CONFIG ulong 10 18 const POL_DENY_UNAUTHORIZED ulong 11 19 const POL_DENY_MISSING_CAP ulong 12 20 const POL_DENY_UNVERIFIED_ENV ulong 13 21 const POL_DENY_ESCALATION ulong 14 22 const POL_DENY_CONTAINED ulong 15 23 const POL_DENY_INVALID_ROUTE ulong 16 24 const POL_DENY_BAD_TOKEN ulong 17 25 const POL_DENY_METHOD_NOT_ALLOWED ulong 18 26 27 note Capability bitmask 28 const CAP_NONE ulong 0 29 const CAP_HEALTH_READ ulong 1 30 const CAP_EVENTS_READ ulong 2 31 const CAP_TASK_READ ulong 4 32 const CAP_TASK_SUBMIT ulong 8 33 const CAP_HEARTBEAT_WRITE ulong 16 34 const CAP_JOURNAL_ADMIN ulong 32 35 const CAP_SKILL_EXEC ulong 64 36 const CAP_POLICY_ADMIN ulong 128 37 38 const ROLE_PUBLIC ulong 3 39 const ROLE_WORKER ulong 23 40 const ROLE_OPERATOR ulong 255 41 42 note Methods and routes 43 const METHOD_UNKNOWN ulong 0 44 const METHOD_GET ulong 1 45 const METHOD_HEAD ulong 2 46 const METHOD_POST ulong 3 47 48 const ROUTE_UNKNOWN ulong 0 49 const ROUTE_HEALTH ulong 1 50 const ROUTE_EVENTS ulong 2 51 const ROUTE_TASKS ulong 3 52 const ROUTE_TASKS_SUBMIT ulong 4 53 const ROUTE_HEARTBEAT ulong 5 54 55 note Containment levels (from defense/decision.rs) 56 const CONTAIN_OBSERVE ulong 0 57 const CONTAIN_RESTRICT ulong 1 58 const CONTAIN_SUSPEND ulong 2 59 const CONTAIN_EMERGENCY_STOP ulong 3 60 61 note pol_meta: [0]=tok_configured, [1]=tok_len, [2]=containment, [3]=princ_cnt, [4]=deny_cnt, [5]=allow_cnt 62 global pol_meta ulong 8 63 global pol_token uchar 64 64 65 global pol_princ_id ulong 32 66 global pol_princ_caps ulong 32 67 global pol_princ_nonce ulong 32 68 global pol_princ_key uchar 1024 69 70 text pol_env_dom "AEGIS-ENV-V1" 71 text pol_sk_bash "bash_eval" 72 text pol_sk_read "read_file" 73 text pol_sk_write "write_file" 74 text pol_sk_list "list_dir" 75 text pol_sk_gstat "git_status" 76 text pol_sk_tele "telemetry_ping" 77 78 text pol_cmd_gstat "git status" 79 text pol_cmd_gdiff "git diff" 80 text pol_cmd_glog "git log -1 --oneline" 81 text pol_cmd_ls "ls" 82 83 fn pol_reset to void 84 for i ulong 8 85 put pol_meta i 0 86 end 87 for i ulong 64 88 put pol_token i 0 89 end 90 for i ulong 32 91 put pol_princ_id i 0 92 put pol_princ_caps i 0 93 put pol_princ_nonce i 0 94 end 95 for i ulong 1024 96 put pol_princ_key i 0 97 end 98 end 99 100 fn pol_is_configured to bool 101 ret ne get pol_meta 0 0 102 end 103 104 fn pol_containment to ulong 105 ret get pol_meta 2 106 end 107 108 fn pol_set_containment lvl ulong to void 109 if le lvl CONTAIN_EMERGENCY_STOP 110 put pol_meta 2 lvl 111 end 112 end 113 114 fn pol_deny_count to ulong 115 ret get pol_meta 4 116 end 117 118 fn pol_allow_count to ulong 119 ret get pol_meta 5 120 end 121 122 fn pol_disable_token to void 123 put pol_meta 0 0 124 put pol_meta 1 0 125 for i ulong 64 126 put pol_token i 0 127 end 128 end 129 130 note Configure operator token: must be 32..64 printable ASCII graphic bytes (0x21..0x7e). 131 fn pol_set_operator_token tok view uchar * to ulong 132 n = len tok 133 if or lt n POL_MIN_TOKEN_LEN gt n POL_MAX_TOKEN_LEN 134 ret POL_DENY_BAD_TOKEN 135 end 136 for i ulong n bound 64 137 c = get tok i 138 if or lt c 0x21 gt c 0x7e 139 ret POL_DENY_BAD_TOKEN 140 end 141 end 142 for i ulong 64 143 put pol_token i 0 144 end 145 for i ulong n bound 64 146 put pol_token i get tok i 147 end 148 put pol_meta 1 n 149 put pol_meta 0 1 150 ret POL_ALLOW 151 end 152 153 note Constant-time byte slice equality (matches auth::constant_time_eq). 154 fn pol_ct_eq a view uchar * b view uchar * to bool 155 na = len a 156 if ne na len b 157 ret false 158 end 159 diff = uchar 0 160 for i ulong na bound 1024 161 diff = or diff xor get a i get b i 162 end 163 ret eq diff 0 164 end 165 166 note Constant-time check of presented operator token against configured token. 167 fn pol_verify_token presented view uchar * to bool 168 if eq get pol_meta 0 0 169 ret false 170 end 171 tlen = get pol_meta 1 172 if ne len presented tlen 173 ret false 174 end 175 diff = uchar 0 176 for i ulong tlen bound 64 177 diff = or diff xor get pol_token i get presented i 178 end 179 ret eq diff 0 180 end 181 182 fn pol_find_principal actor_id ulong to long 183 if eq actor_id 0 184 ret -1 185 end 186 n = get pol_meta 3 187 for i ulong n bound 32 188 if eq get pol_princ_id i actor_id 189 ret long i 190 end 191 end 192 ret -1 193 end 194 195 fn pol_register_principal actor_id ulong caps ulong key view uchar 32 to ulong 196 if eq actor_id 0 197 ret POL_DENY_UNAUTHORIZED 198 end 199 s = pol_find_principal actor_id 200 slot = ulong 0 201 if ge s 0 202 slot = ulong s 203 else 204 slot = get pol_meta 3 205 if ge slot POL_MAX_PRINCIPALS 206 ret POL_DENY_CONTAINED 207 end 208 put pol_meta 3 add slot 1 209 end 210 put pol_princ_id slot actor_id 211 put pol_princ_caps slot caps 212 put pol_princ_nonce slot 0 213 base = mul slot 32 214 for i ulong 32 215 put pol_princ_key add base i get key i 216 end 217 ret POL_ALLOW 218 end 219 220 fn pol_actor_caps actor_id ulong to ulong 221 s = pol_find_principal actor_id 222 if lt s 0 223 ret CAP_NONE 224 end 225 ret get pol_princ_caps ulong s 226 end 227 228 note Refuse any capability delegation or request exceeding actor's granted ceiling. 229 fn pol_check_escalation granted_caps ulong requested_caps ulong to ulong 230 extra = and requested_caps xor granted_caps 0xffffffffffffffff 231 if ne extra 0 232 put pol_meta 4 add get pol_meta 4 1 233 ret POL_DENY_ESCALATION 234 end 235 ret POL_ALLOW 236 end 237 238 fn pol_put_be64 buf edit uchar * off ulong v ulong to void 239 put buf add off 0 uchar and shr v 56 0xff 240 put buf add off 1 uchar and shr v 48 0xff 241 put buf add off 2 uchar and shr v 40 0xff 242 put buf add off 3 uchar and shr v 32 0xff 243 put buf add off 4 uchar and shr v 24 0xff 244 put buf add off 5 uchar and shr v 16 0xff 245 put buf add off 6 uchar and shr v 8 0xff 246 put buf add off 7 uchar and v 0xff 247 end 248 249 note Compute 32-byte keyed SHA-256 envelope MAC: 250 note MAC = SHA256("AEGIS-ENV-V1" || key[32] || be64(actor_id) || be64(route) || be64(nonce) || payload) 251 fn pol_compute_env_mac slot ulong route ulong nonce ulong payload view uchar * dst_mac edit uchar 32 to void 252 ctx = new ulong 40 0 253 hdr = new uchar 68 0 254 for i ulong 12 255 put hdr i get pol_env_dom i 256 end 257 base = mul slot 32 258 for i ulong 32 259 put hdr add 12 i get pol_princ_key add base i 260 end 261 pol_put_be64 hdr 44 get pol_princ_id slot 262 pol_put_be64 hdr 52 route 263 pol_put_be64 hdr 60 nonce 264 sha256_init ctx 265 sha256_update ctx part hdr 0 68 266 if gt len payload 0 267 sha256_update ctx payload 268 end 269 sha256_final ctx dst_mac 270 for i ulong 40 271 put ctx i 0 272 end 273 for i ulong 68 274 put hdr i 0 275 end 276 end 277 278 fn pol_seal_envelope actor_id ulong route ulong nonce ulong payload view uchar * dst_mac edit uchar 32 to ulong 279 if eq nonce 0 280 ret POL_DENY_UNVERIFIED_ENV 281 end 282 s = pol_find_principal actor_id 283 if lt s 0 284 ret POL_DENY_UNAUTHORIZED 285 end 286 pol_compute_env_mac ulong s route nonce payload dst_mac 287 ret POL_ALLOW 288 end 289 290 note Verify signed envelope MAC in constant time and enforce strictly increasing nonce per actor. 291 fn pol_verify_envelope actor_id ulong route ulong nonce ulong payload view uchar * mac view uchar 32 to ulong 292 if eq nonce 0 293 put pol_meta 4 add get pol_meta 4 1 294 ret POL_DENY_UNVERIFIED_ENV 295 end 296 s = pol_find_principal actor_id 297 if lt s 0 298 put pol_meta 4 add get pol_meta 4 1 299 ret POL_DENY_UNAUTHORIZED 300 end 301 slot = ulong s 302 if le nonce get pol_princ_nonce slot 303 put pol_meta 4 add get pol_meta 4 1 304 ret POL_DENY_UNVERIFIED_ENV 305 end 306 expected = new uchar 32 0 307 pol_compute_env_mac slot route nonce payload expected 308 diff = uchar 0 309 for i ulong 32 310 diff = or diff xor get expected i get mac i 311 end 312 if ne diff 0 313 put pol_meta 4 add get pol_meta 4 1 314 ret POL_DENY_UNVERIFIED_ENV 315 end 316 put pol_princ_nonce slot nonce 317 ret POL_ALLOW 318 end 319 320 note Exact-match byte equality helper for allowlist checks. 321 fn pol_str_eq a view uchar * b view uchar * to bool 322 if ne len a len b 323 ret false 324 end 325 n = len a 326 for i ulong n bound 256 327 if ne get a i get b i 328 ret false 329 end 330 end 331 ret true 332 end 333 334 note Deterministic skill dispatch allowlist (from enforcement.rs). 335 fn pol_check_skill name view uchar * to ulong 336 if eq len name 0 337 put pol_meta 4 add get pol_meta 4 1 338 ret POL_DENY_ESCALATION 339 end 340 if pol_str_eq name pol_sk_bash 341 ret POL_ALLOW 342 end 343 if pol_str_eq name pol_sk_read 344 ret POL_ALLOW 345 end 346 if pol_str_eq name pol_sk_write 347 ret POL_ALLOW 348 end 349 if pol_str_eq name pol_sk_list 350 ret POL_ALLOW 351 end 352 if pol_str_eq name pol_sk_gstat 353 ret POL_ALLOW 354 end 355 if pol_str_eq name pol_sk_tele 356 ret POL_ALLOW 357 end 358 put pol_meta 4 add get pol_meta 4 1 359 ret POL_DENY_ESCALATION 360 end 361 362 note Deterministic local command allowlist (from enforcement.rs / security.rs). 363 fn pol_check_local_cmd cmd view uchar * to ulong 364 if eq len cmd 0 365 put pol_meta 4 add get pol_meta 4 1 366 ret POL_DENY_ESCALATION 367 end 368 if pol_str_eq cmd pol_cmd_gstat 369 ret POL_ALLOW 370 end 371 if pol_str_eq cmd pol_cmd_gdiff 372 ret POL_ALLOW 373 end 374 if pol_str_eq cmd pol_cmd_glog 375 ret POL_ALLOW 376 end 377 if pol_str_eq cmd pol_cmd_ls 378 ret POL_ALLOW 379 end 380 put pol_meta 4 add get pol_meta 4 1 381 ret POL_DENY_ESCALATION 382 end 383 384 note Required capability bit for (method, route). Returns 0 if route/method invalid. 385 fn pol_route_required_cap method ulong route ulong to ulong 386 if eq route ROUTE_HEALTH 387 if or eq method METHOD_GET eq method METHOD_HEAD 388 ret CAP_HEALTH_READ 389 end 390 ret 0 391 end 392 if eq route ROUTE_EVENTS 393 if or eq method METHOD_GET eq method METHOD_HEAD 394 ret CAP_EVENTS_READ 395 end 396 ret 0 397 end 398 if eq route ROUTE_TASKS 399 if eq method METHOD_GET 400 ret CAP_TASK_READ 401 end 402 ret 0 403 end 404 if eq route ROUTE_TASKS_SUBMIT 405 if eq method METHOD_POST 406 ret CAP_TASK_SUBMIT 407 end 408 ret 0 409 end 410 if eq route ROUTE_HEARTBEAT 411 if eq method METHOD_POST 412 ret CAP_HEARTBEAT_WRITE 413 end 414 ret 0 415 end 416 ret 0 417 end 418 419 fn pol_is_public_route method ulong route ulong to bool 420 if and ne method METHOD_GET ne method METHOD_HEAD 421 ret false 422 end 423 if or eq route ROUTE_HEALTH eq route ROUTE_EVENTS 424 ret true 425 end 426 ret false 427 end 428 429 note Master fail-closed route authority gate. 430 note Evaluates containment, route/method validity, public read allowlist, operator token, 431 note envelope verification, privilege escalation ceiling, and required route capability. 432 fn pol_authorize_request method ulong route ulong actor_id ulong req_caps ulong tok_ok bool env_ok bool to ulong 433 cont = get pol_meta 2 434 if and eq cont CONTAIN_EMERGENCY_STOP ne route ROUTE_HEALTH 435 put pol_meta 4 add get pol_meta 4 1 436 ret POL_DENY_CONTAINED 437 end 438 if eq route ROUTE_UNKNOWN 439 put pol_meta 4 add get pol_meta 4 1 440 ret POL_DENY_INVALID_ROUTE 441 end 442 need_cap = pol_route_required_cap method route 443 if eq need_cap 0 444 put pol_meta 4 add get pol_meta 4 1 445 ret POL_DENY_METHOD_NOT_ALLOWED 446 end 447 if pol_is_public_route method route 448 put pol_meta 5 add get pol_meta 5 1 449 ret POL_ALLOW 450 end 451 if ge cont CONTAIN_SUSPEND 452 put pol_meta 4 add get pol_meta 4 1 453 ret POL_DENY_CONTAINED 454 end 455 if eq get pol_meta 0 0 456 put pol_meta 4 add get pol_meta 4 1 457 ret POL_DENY_NO_TOKEN_CONFIG 458 end 459 if not tok_ok 460 put pol_meta 4 add get pol_meta 4 1 461 ret POL_DENY_UNAUTHORIZED 462 end 463 if not env_ok 464 put pol_meta 4 add get pol_meta 4 1 465 ret POL_DENY_UNVERIFIED_ENV 466 end 467 granted = ROLE_OPERATOR 468 if ne actor_id 0 469 granted = pol_actor_caps actor_id 470 end 471 esc = pol_check_escalation granted req_caps 472 if ne esc POL_ALLOW 473 ret esc 474 end 475 if eq and req_caps need_cap 0 476 put pol_meta 4 add get pol_meta 4 1 477 ret POL_DENY_MISSING_CAP 478 end 479 if and eq cont CONTAIN_RESTRICT eq method METHOD_POST 480 put pol_meta 4 add get pol_meta 4 1 481 ret POL_REQUIRE_APPROVAL 482 end 483 put pol_meta 5 add get pol_meta 5 1 484 ret POL_ALLOW 485 end