interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / argus / bridge.om

561 lines · 14149 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/argus/bridge.om: Pure-Omega ARGUS-AEGIS Bridge.
3note Converted from upstream aienux native/argus/bridge/argus_aegis_bridge.{h,c}.
4note Compose with: si/directives/om2.om aienux/argus/event.om aienux/argus/contain.om aienux/argus/bridge.om <test.om> om/tls/sha2.om
5
6const BRIDGE_REQUESTS ulong 32
7const BRIDGE_FINDINGS ulong 128
8const ARGUS_FLAG_STREAM_SHIFT ushort 2
9const ARGUS_STREAM_BRIDGE ushort 16383
10const BRIDGE_STREAM_FLAGS ushort 0xfffc
11
12const BRIDGE_OBS_MINT uint 1
13const BRIDGE_OBS_REVOKE uint 2
14const BRIDGE_CAP_OK int 0
15
16const BRIDGE_SUBJ_AEGIS uint 41
17const BRIDGE_SUBJ_AEGIS_ROOT uint 42
18const BRIDGE_SUBJ_CONTAIN uint 43
19
20const BRIDGE_GATE_BUDGET ulong 8
21const BRIDGE_GATE_WINDOW ulong 64
22
23const BRIDGE_WHY_POLICY_GRANT ushort 1100
24const BRIDGE_WHY_POLICY_DENY ushort 1101
25const BRIDGE_WHY_POLICY_ESCALATE ushort 1102
26const BRIDGE_WHY_PROTECTED ushort 1103
27const BRIDGE_WHY_NOT_LIVE ushort 1104
28const BRIDGE_WHY_SUBJECT_MISMATCH ushort 1105
29const BRIDGE_WHY_BUDGET ushort 1107
30const BRIDGE_WHY_BAD_REQUEST ushort 1108
31const BRIDGE_WHY_NO_EXECUTOR ushort 1109
32const BRIDGE_WHY_NOT_AUTO_ELIGIBLE ushort 1116
33
34note Bridge state globals
35global br_init uchar 1
36global br_attached uchar 1
37global br_mach uchar 32
38global br_seq ulong 1
39global br_err int 1
40global br_clock ulong 1
41
42note Saved BridgeRequest slots (32 entries, indexed by request_id % 32)
43global br_req_id ulong 32
44global br_req_mach uchar 1024
45global br_req_dig uchar 1024
46
47note Queued ArgusFinding ring (128 entries)
48global br_find_cnt ulong 1
49global br_f_code ushort 128
50global br_f_sev uchar 128
51global br_f_conf uchar 128
52global br_f_sync uchar 128
53global br_f_cont uchar 128
54global br_f_det ushort 128
55global br_f_seq ulong 128
56global br_f_pseq ulong 128
57global br_f_princ uint 128
58global br_f_cid uint 128
59global br_f_cgen ulong 128
60
61note AEGIS containment gate state (coupled to bridge)
62global br_gate_seq ulong 1
63global br_gate_dec_id ulong 1
64global br_gate_inert uchar 1
65global br_gate_grants ulong 1
66global br_gate_decs ulong 1
67global br_gate_pol uchar 11
68global br_gate_last_st uchar 1
69global br_gate_last_why ushort 1
70global br_gate_last_est uchar 1
71global br_gate_last_erc int 1
72global br_gate_last_rev uint 1
73
74note Receipt staging buffer for receipt_sink (avoids >6 params)
75global br_rcp_kind ushort 1
76global br_rcp_st uchar 1
77global br_rcp_typ uchar 1
78global br_rcp_fcode ushort 1
79global br_rcp_rid ulong 1
80global br_rcp_princ uint 1
81global br_rcp_cid uint 1
82global br_rcp_cgen ulong 1
83global br_rcp_res ulong 1
84global br_rcp_why ushort 1
85global br_rcp_rc int 1
86global br_rcp_flags ushort 1
87global br_rcp_dig uchar 32
88
89fn br_zero_digest d view uchar 32 to bool
90v = uchar 0
91for i ulong 32
92v = or v get d i
93end
94ret eq v 0
95end
96
97fn argus_bridge_init mach view uchar 32 to int
98for i ulong 32
99put br_mach i get mach i
100put br_req_id i 0
101end
102for i ulong 1024
103put br_req_mach i 0
104put br_req_dig i 0
105end
106for i ulong 128
107put br_f_code i 0
108put br_f_sev i 0
109put br_f_conf i 0
110put br_f_sync i 0
111put br_f_cont i 0
112put br_f_det i 0
113put br_f_seq i 0
114put br_f_pseq i 0
115put br_f_princ i 0
116put br_f_cid i 0
117put br_f_cgen i 0
118end
119for i ulong 11
120put br_gate_pol i ARGUS_CSTATUS_ESCALATE
121end
122put br_gate_pol 1 ARGUS_CSTATUS_GRANT
123put br_seq 0 1
124put br_err 0 ARGUS_OK
125put br_attached 0 0
126put br_clock 0 0
127put br_find_cnt 0 0
128put br_gate_seq 0 1
129put br_gate_dec_id 0 1
130put br_gate_inert 0 0
131put br_gate_grants 0 0
132put br_gate_decs 0 0
133put br_gate_last_st 0 0
134put br_gate_last_why 0 0
135put br_gate_last_est 0 0
136put br_gate_last_erc 0 0
137put br_gate_last_rev 0 0
138put br_init 0 1
139ret ARGUS_OK
140end
141
142fn argus_bridge_destroy to void
143put br_init 0 0
144put br_attached 0 0
145end
146
147fn argus_bridge_attach_gate to int
148if eq get br_init 0 0
149ret ARGUS_ERR_ARG
150end
151put br_attached 0 1
152ret ARGUS_OK
153end
154
155fn argus_bridge_set_clock tick ulong to void
156put br_clock 0 tick
157end
158
159fn argus_bridge_set_policy ctype uchar verdict uchar to void
160if and ge ctype 1 le ctype ARGUS_CONTAIN_MAX
161put br_gate_pol ulong ctype verdict
162end
163end
164
165fn argus_bridge_set_inert v uchar to void
166put br_gate_inert 0 v
167end
168
169fn argus_bridge_error to int
170if eq get br_init 0 0
171ret ARGUS_ERR_ARG
172end
173ret get br_err 0
174end
175
176fn argus_bridge_finding_count to ulong
177ret get br_find_cnt 0
178end
179
180fn argus_bridge_append_finding f view ArgusFinding to int
181cnt = get br_find_cnt 0
182if ge cnt BRIDGE_FINDINGS
183ret ARGUS_ERR_OVERFLOW
184end
185put br_f_code cnt get f code
186put br_f_sev cnt get f severity
187put br_f_conf cnt get f confidence
188put br_f_sync cnt get f sync_allowed
189put br_f_cont cnt get f containment
190put br_f_det cnt get f detector
191put br_f_seq cnt get f sequence
192put br_f_pseq cnt get f prior_sequence
193put br_f_princ cnt get f principal
194put br_f_cid cnt get f cap_id
195put br_f_cgen cnt get f cap_generation
196put br_find_cnt 0 add cnt 1
197ret ARGUS_OK
198end
199
200fn argus_bridge_take_finding dst edit ArgusFinding to bool
201cnt = get br_find_cnt 0
202if eq cnt 0
203ret false
204end
205put dst code get br_f_code 0
206put dst severity get br_f_sev 0
207put dst confidence get br_f_conf 0
208put dst sync_allowed get br_f_sync 0
209put dst containment get br_f_cont 0
210put dst detector get br_f_det 0
211put dst sequence get br_f_seq 0
212put dst prior_sequence get br_f_pseq 0
213put dst principal get br_f_princ 0
214put dst cap_id get br_f_cid 0
215put dst cap_generation get br_f_cgen 0
216rem_cnt = sub cnt 1
217i = ulong 0
218while lt i rem_cnt bound 128
219nxt = add i 1
220put br_f_code i get br_f_code nxt
221put br_f_sev i get br_f_sev nxt
222put br_f_conf i get br_f_conf nxt
223put br_f_sync i get br_f_sync nxt
224put br_f_cont i get br_f_cont nxt
225put br_f_det i get br_f_det nxt
226put br_f_seq i get br_f_seq nxt
227put br_f_pseq i get br_f_pseq nxt
228put br_f_princ i get br_f_princ nxt
229put br_f_cid i get br_f_cid nxt
230put br_f_cgen i get br_f_cgen nxt
231i = nxt
232end
233put br_find_cnt 0 rem_cnt
234ret true
235end
236
237fn br_find_req_slot rid ulong to int
238for i ulong 32
239if eq get br_req_id i rid
240ret int i
241end
242end
243ret -1
244end
245
246fn argus_bridge_ingest ev view ArgusEvent mach view uchar 32 evdig view uchar 32 to int
247fcore = new ArgusFinding 0 0 0 0 0 0 0 0 0 0 0
248fcont = new ArgusFinding 0 0 0 0 0 0 0 0 0 0 0
249if eq get br_init 0 0
250ret ARGUS_ERR_ARG
251end
252fc = argus_core_ingest ev mach evdig fcore
253rc = ARGUS_OK
254if ne fc ARGUS_F_NONE
255rc = argus_bridge_append_finding fcore
256if ne rc ARGUS_OK
257put br_err 0 rc
258ret rc
259end
260end
261cc = argus_contain_observe ev evdig
262if ne cc ARGUS_F_NONE
263put fcont code cc
264put fcont severity ARGUS_SEV_CRITICAL
265put fcont confidence ARGUS_CONF_DETERMINISTIC
266put fcont sync_allowed 0
267put fcont containment ARGUS_CONTAIN_NONE
268put fcont detector cc
269put fcont sequence get ev sequence
270put fcont prior_sequence 0
271put fcont principal get ev principal
272put fcont cap_id get ev cap_id
273put fcont cap_generation get ev cap_generation
274rc = argus_bridge_append_finding fcont
275if ne rc ARGUS_OK
276put br_err 0 rc
277ret rc
278end
279end
280ret ARGUS_OK
281end
282
283fn br_receipt_outcome st uchar rcode int to uchar
284if or eq st ARGUS_CSTATUS_GRANT or eq st ARGUS_CSTATUS_DONE eq st ARGUS_CSTATUS_PARTIAL
285ret ARGUS_OUTCOME_OK
286end
287if eq st ARGUS_CSTATUS_DENY
288ret ARGUS_OUTCOME_DENIED
289end
290if and eq st ARGUS_CSTATUS_FAILED lt rcode 0
291ret ARGUS_OUTCOME_DENIED
292end
293ret ARGUS_OUTCOME_ERROR
294end
295
296fn br_receipt_sink to void
297ev = new ArgusEvent 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
298mach = new uchar 32 0
299evdig = new uchar 32 0
300if eq get br_init 0 0
301ret
302end
303seq = get br_seq 0
304if or eq seq 0 eq seq ARGUS_U64_MAX
305put br_err 0 ARGUS_ERR_FULL
306ret
307end
308rid = get br_rcp_rid 0
309slot = br_find_req_slot rid
310if lt slot 0
311put br_err 0 ARGUS_ERR_STATE
312ret
313end
314put br_seq 0 add seq 1
315st = get br_rcp_st 0
316kd = get br_rcp_kind 0
317ev_code = get br_rcp_rc 0
318if eq kd ARGUS_EV_CONTAINMENT_DECIDED
319ev_code = int get br_rcp_why 0
320end
321ev_flags = BRIDGE_STREAM_FLAGS
322if ne and get br_rcp_flags 0 ARGUS_CREQ_SYNTHETIC 0
323ev_flags = or ev_flags ARGUS_FLAG_SYNTHETIC
324end
325put ev version ARGUS_ABI_VERSION
326put ev class_ ARGUS_CLASS_CRITICAL
327put ev kind kd
328put ev effect_class ARGUS_EFFECT_NONE
329put ev outcome br_receipt_outcome st get br_rcp_rc 0
330put ev flags ev_flags
331put ev sequence seq
332put ev tick get br_clock 0
333put ev principal get br_rcp_princ 0
334put ev code ev_code
335put ev cap_id get br_rcp_cid 0
336put ev object_id argus_contain_pack get br_rcp_typ 0 st get br_rcp_fcode 0
337put ev cap_generation get br_rcp_cgen 0
338put ev world_generation rid
339put ev resource get br_rcp_res 0
340base = mul ulong slot 32
341for i ulong 32
342put mach i get br_req_mach add base i
343put evdig i get br_rcp_dig i
344end
345irc = argus_bridge_ingest ev mach evdig
346if and ne irc ARGUS_OK ne irc ARGUS_ERR_FULL
347put br_err 0 irc
348end
349end
350
351fn br_ingest_rec ev view ArgusEvent mach view uchar 32 evdig view uchar 32 to void
352irc = argus_bridge_ingest ev mach evdig
353if and ne irc ARGUS_OK ne irc ARGUS_ERR_FULL
354put br_err 0 irc
355end
356end
357
358fn argus_bridge_observer op uint cap_id uint cap_gen ulong subj uint rights uint res ulong to void
359ev = new ArgusEvent 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
360mach = new uchar 32 0
361evdig = new uchar 32 0
362if eq get br_init 0 0
363ret
364end
365if and ne op BRIDGE_OBS_MINT ne op BRIDGE_OBS_REVOKE
366ret
367end
368seq = get br_seq 0
369if or eq seq 0 eq seq ARGUS_U64_MAX
370put br_err 0 ARGUS_ERR_FULL
371ret
372end
373put br_seq 0 add seq 1
374put ev version ARGUS_ABI_VERSION
375if eq op BRIDGE_OBS_MINT
376put ev class_ ARGUS_CLASS_SECURITY
377put ev kind ARGUS_EV_CAPABILITY_GRANTED
378else
379put ev class_ ARGUS_CLASS_CRITICAL
380put ev kind ARGUS_EV_CAPABILITY_REVOKED
381end
382put ev effect_class ARGUS_EFFECT_NONE
383put ev outcome ARGUS_OUTCOME_OK
384put ev flags BRIDGE_STREAM_FLAGS
385put ev sequence seq
386put ev tick get br_clock 0
387put ev principal subj
388put ev code 0
389put ev cap_id cap_id
390put ev object_id rights
391put ev cap_generation cap_gen
392put ev world_generation 0
393put ev resource res
394for i ulong 32
395put mach i get br_mach i
396end
397br_ingest_rec ev mach evdig
398end
399
400fn br_gate_is_protected subj uint to bool
401if or eq subj BRIDGE_SUBJ_AEGIS or eq subj BRIDGE_SUBJ_AEGIS_ROOT eq subj BRIDGE_SUBJ_CONTAIN
402ret true
403end
404ret false
405end
406
407fn br_gate_decide req view ArgusContainReq to void
408decs = get br_gate_decs 0
409if ge decs BRIDGE_GATE_WINDOW
410put br_gate_decs 0 0
411put br_gate_grants 0 0
412end
413put br_gate_decs 0 add get br_gate_decs 0 1
414ct = get req containment
415if or lt ct 1 gt ct ARGUS_CONTAIN_MAX
416put br_gate_last_st 0 ARGUS_CSTATUS_DENY
417put br_gate_last_why 0 BRIDGE_WHY_BAD_REQUEST
418ret
419end
420if ne get br_gate_inert 0 0
421put br_gate_last_st 0 ARGUS_CSTATUS_ESCALATE
422put br_gate_last_why 0 BRIDGE_WHY_POLICY_ESCALATE
423ret
424end
425if br_gate_is_protected get req principal
426put br_gate_last_st 0 ARGUS_CSTATUS_DENY
427put br_gate_last_why 0 BRIDGE_WHY_PROTECTED
428ret
429end
430pol = get br_gate_pol ulong ct
431if eq pol ARGUS_CSTATUS_DENY
432put br_gate_last_st 0 ARGUS_CSTATUS_DENY
433put br_gate_last_why 0 BRIDGE_WHY_POLICY_DENY
434ret
435end
436if ne ct ARGUS_CONTAIN_REVOKE_CAPABILITY
437put br_gate_last_st 0 ARGUS_CSTATUS_ESCALATE
438put br_gate_last_why 0 BRIDGE_WHY_NO_EXECUTOR
439ret
440end
441if ne and get req flags ARGUS_CREQ_KNOWN 0
442put br_gate_last_st 0 ARGUS_CSTATUS_ESCALATE
443put br_gate_last_why 0 BRIDGE_WHY_NOT_AUTO_ELIGIBLE
444ret
445end
446cid = get req target_cap_id
447if ge cid ARGUS_CAP_MAX
448put br_gate_last_st 0 ARGUS_CSTATUS_DENY
449put br_gate_last_why 0 BRIDGE_WHY_NOT_LIVE
450ret
451end
452idx = ulong cid
453if ne get argus_sh_state idx ARGUS_SH_LIVE
454put br_gate_last_st 0 ARGUS_CSTATUS_DENY
455put br_gate_last_why 0 BRIDGE_WHY_NOT_LIVE
456ret
457end
458if ne get argus_sh_gen idx get req target_cap_gen
459put br_gate_last_st 0 ARGUS_CSTATUS_DENY
460put br_gate_last_why 0 BRIDGE_WHY_NOT_LIVE
461ret
462end
463if ne get argus_sh_subj idx get req principal
464put br_gate_last_st 0 ARGUS_CSTATUS_DENY
465put br_gate_last_why 0 BRIDGE_WHY_SUBJECT_MISMATCH
466ret
467end
468if ge get br_gate_grants 0 BRIDGE_GATE_BUDGET
469put br_gate_last_st 0 ARGUS_CSTATUS_ESCALATE
470put br_gate_last_why 0 BRIDGE_WHY_BUDGET
471ret
472end
473if ne pol ARGUS_CSTATUS_GRANT
474put br_gate_last_st 0 ARGUS_CSTATUS_ESCALATE
475put br_gate_last_why 0 BRIDGE_WHY_POLICY_ESCALATE
476ret
477end
478put br_gate_grants 0 add get br_gate_grants 0 1
479put br_gate_last_st 0 ARGUS_CSTATUS_GRANT
480put br_gate_last_why 0 BRIDGE_WHY_POLICY_GRANT
481end
482
483fn br_stage_receipt kd ushort st uchar req view ArgusContainReq dig view uchar 32 res ulong to void
484put br_rcp_kind 0 kd
485put br_rcp_st 0 st
486put br_rcp_typ 0 get req containment
487put br_rcp_fcode 0 get req finding_code
488put br_rcp_rid 0 get req request_id
489put br_rcp_princ 0 get req principal
490put br_rcp_cid 0 get req target_cap_id
491put br_rcp_cgen 0 get req target_cap_gen
492put br_rcp_res 0 res
493put br_rcp_flags 0 get req flags
494for i ulong 32
495put br_rcp_dig i get dig i
496end
497end
498
499fn br_save_req rid ulong mach view uchar 32 tdig view uchar 32 dig view uchar 32 to void
500slot = rem rid BRIDGE_REQUESTS
501put br_req_id slot rid
502base = mul slot 32
503use_tdig = br_zero_digest mach
504for i ulong 32
505if use_tdig
506put br_req_mach add base i get tdig i
507else
508put br_req_mach add base i get mach i
509end
510put br_req_dig add base i get dig i
511end
512end
513
514fn br_execute_grant req view ArgusContainReq dig view uchar 32 to void
515argus_bridge_observer BRIDGE_OBS_REVOKE get req target_cap_id get req target_cap_gen get req principal 0 0
516put br_gate_seq 0 add get br_gate_seq 0 1
517put br_gate_last_est 0 ARGUS_CSTATUS_DONE
518put br_gate_last_erc 0 0
519put br_gate_last_rev 0 1
520put br_rcp_why 0 0
521put br_rcp_rc 0 0
522br_stage_receipt ARGUS_EV_CONTAINMENT_EXECUTED ARGUS_CSTATUS_DONE req dig 1
523br_receipt_sink
524end
525
526fn argus_bridge_submit req view ArgusContainReq mach view uchar 32 fdig view uchar 32 tdig view uchar 32 to int
527dig = new uchar 32 0
528if or eq get br_init 0 0 eq get br_attached 0 0
529ret ARGUS_ERR_ARG
530end
531argus_creq_digest req mach fdig tdig dig
532br_save_req get req request_id mach tdig dig
533br_gate_decide req
534did = get br_gate_dec_id 0
535put br_gate_dec_id 0 add did 1
536put br_gate_seq 0 add get br_gate_seq 0 1
537dst = get br_gate_last_st 0
538put br_rcp_why 0 get br_gate_last_why 0
539put br_rcp_rc 0 0
540br_stage_receipt ARGUS_EV_CONTAINMENT_DECIDED dst req dig did
541br_receipt_sink
542put br_gate_last_est 0 0
543put br_gate_last_erc 0 0
544put br_gate_last_rev 0 0
545if eq dst ARGUS_CSTATUS_GRANT
546br_execute_grant req dig
547end
548ret argus_bridge_error
549end
550
551fn argus_bridge_last_decision to uchar
552ret get br_gate_last_st 0
553end
554
555fn argus_bridge_last_why to ushort
556ret get br_gate_last_why 0
557end
558
559fn argus_bridge_last_exec_status to uchar
560ret get br_gate_last_est 0
561end