interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aegis-runtime-mv / om / policy.om

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