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 / contain.om

574 lines · 16086 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/argus/contain.om: Pure-Omega ARGUS Deterministic Shadow-State Core,
3note Hard-Invariant Detectors, and Containment State Machine (Invariants I1..I5).
4note Converted from upstream aienux native/argus/argus_core.c, argus_detect.c, and argus_contain.c.
5
6note Shadow capability slot states
7const ARGUS_SH_UNSEEN uchar 0
8const ARGUS_SH_LIVE uchar 1
9const ARGUS_SH_REVOKED uchar 2
10
11note Containment pending states
12const ARGUS_ST_FREE uchar 0
13const ARGUS_ST_PROPOSED uchar 1
14const ARGUS_ST_GRANTED uchar 2
15const ARGUS_ST_ESCALATED uchar 3
16const ARGUS_ST_CONFIRMED uchar 4
17const ARGUS_ST_DENIED uchar 5
18const ARGUS_ST_FAILED uchar 6
19const ARGUS_ST_FAILED_SCOPE uchar 7
20const ARGUS_ST_EXPIRED uchar 8
21
22const ARGUS_CONTAIN_PENDING ulong 32
23const ARGUS_CONTAIN_WINDOW_EVENTS ulong 4096
24const ARGUS_CONTAIN_WINDOW_MAX uint 16
25const ARGUS_STREAMS_MAX ulong 32
26const ARGUS_WORLDS_MAX ulong 8
27
28note Shadow capability table (256 slots)
29global argus_sh_state uchar 256
30global argus_sh_gen ulong 256
31global argus_sh_subj uint 256
32global argus_sh_rgt uint 256
33global argus_sh_res ulong 256
34global argus_sh_seq ulong 256
35
36note Producer sequence stream table (32 streams)
37global argus_strm_used uchar 32
38global argus_strm_key uint 32
39global argus_strm_seq ulong 32
40
41note World lineage table (8 stores)
42global argus_wld_used uchar 8
43global argus_wld_id uint 8
44global argus_wld_gen ulong 8
45
46note Core health & SHA-256 event hash chain
47note meta: 0=received, 1=rejected, 2=findings, 3=not_applied
48global argus_core_meta ulong 8
49global argus_core_chain uchar 32
50
51note Containment state machine globals (32 pending slots)
52note cmeta: 0=events, 1=next_req_id, 2=win_start, 3=win_count
53global argus_cmeta ulong 8
54note chealth: 0=proposed, 1=granted, 2=denied, 3=escalated, 4=confirmed,
55note 5=failed_scope, 6=unmatched, 7=unauth, 8=unconfirmed, 9=rate_limited
56global argus_chealth ulong 10
57
58global argus_pend_st uchar 32
59global argus_pend_typ uchar 32
60global argus_pend_fcode ushort 32
61global argus_pend_princ uint 32
62global argus_pend_cid uint 32
63global argus_pend_cgen ulong 32
64global argus_pend_rid ulong 32
65global argus_pend_k4 uchar 32
66note 32 pending slots * 32-byte canonical request digest = 1024 bytes
67global argus_pend_dig uchar 1024
68
69note ---- Core Initialization & Helpers ----
70
71fn argus_core_init to void
72for i ulong 256
73put argus_sh_state i ARGUS_SH_UNSEEN
74put argus_sh_gen i 0
75put argus_sh_subj i 0
76put argus_sh_rgt i 0
77put argus_sh_res i 0
78put argus_sh_seq i 0
79end
80for i ulong 32
81put argus_strm_used i 0
82put argus_strm_key i 0
83put argus_strm_seq i 0
84put argus_core_chain i 0
85end
86for i ulong 8
87put argus_wld_used i 0
88put argus_wld_id i 0
89put argus_wld_gen i 0
90put argus_core_meta i 0
91end
92end
93
94fn argus_core_get_chain dst edit uchar 32 to void
95for i ulong 32
96put dst i get argus_core_chain i
97end
98end
99
100fn argus_core_not_applied to ulong
101ret get argus_core_meta 3
102end
103
104fn argus_check_stream flags ushort seq ulong to bool
105sid = uint argus_stream_of flags
106is_cons = uint 0
107if ne and flags ARGUS_FLAG_CONSUMER 0
108is_cons = uint 1
109end
110key = or shl sid 1 is_cons
111for i ulong 32
112if ne get argus_strm_used i 0
113if eq get argus_strm_key i key
114prev = get argus_strm_seq i
115if le seq prev
116ret false
117end
118put argus_strm_seq i seq
119ret true
120end
121end
122end
123for i ulong 32
124if eq get argus_strm_used i 0
125put argus_strm_used i 1
126put argus_strm_key i key
127put argus_strm_seq i seq
128ret true
129end
130end
131ret true
132end
133
134fn argus_check_world_commit store_id uint wgen ulong to bool
135for i ulong 8
136if ne get argus_wld_used i 0
137if eq get argus_wld_id i store_id
138prev = get argus_wld_gen i
139if ne wgen add prev 1
140ret false
141end
142put argus_wld_gen i wgen
143ret true
144end
145end
146end
147for i ulong 8
148if eq get argus_wld_used i 0
149put argus_wld_used i 1
150put argus_wld_id i store_id
151put argus_wld_gen i wgen
152ret true
153end
154end
155ret false
156end
157
158fn argus_set_finding dst edit ArgusFinding fcode ushort sev uchar ev view ArgusEvent prior ulong to void
159put dst code fcode
160put dst severity sev
161put dst confidence ARGUS_CONF_DETERMINISTIC
162put dst sync_allowed 0
163put dst containment ARGUS_CONTAIN_NONE
164put dst detector fcode
165put dst sequence get ev sequence
166put dst prior_sequence prior
167put dst principal get ev principal
168put dst cap_id get ev cap_id
169put dst cap_generation get ev cap_generation
170put argus_core_meta 2 add get argus_core_meta 2 1
171end
172
173fn argus_detect_cap_use ev view ArgusEvent dst edit ArgusFinding to ushort
174if ne get ev outcome ARGUS_OUTCOME_OK
175ret ARGUS_F_NONE
176end
177cid = get ev cap_id
178if ge cid ARGUS_CAP_MAX
179ret ARGUS_F_NONE
180end
181idx = ulong cid
182st = get argus_sh_state idx
183sgen = get argus_sh_gen idx
184sseq = get argus_sh_seq idx
185if eq st ARGUS_SH_UNSEEN
186argus_set_finding dst ARGUS_F_FORGED_CAPABILITY ARGUS_SEV_CRITICAL ev 0
187put dst containment ARGUS_CONTAIN_REVOKE_CAPABILITY
188ret ARGUS_F_FORGED_CAPABILITY
189end
190if eq st ARGUS_SH_REVOKED
191if le get ev cap_generation sgen
192argus_set_finding dst ARGUS_F_REVOKED_CAPABILITY_USED ARGUS_SEV_CRITICAL ev sseq
193put dst containment ARGUS_CONTAIN_REVOKE_CAPABILITY
194ret ARGUS_F_REVOKED_CAPABILITY_USED
195end
196argus_set_finding dst ARGUS_F_FORGED_CAPABILITY ARGUS_SEV_CRITICAL ev sseq
197put dst containment ARGUS_CONTAIN_REVOKE_CAPABILITY
198ret ARGUS_F_FORGED_CAPABILITY
199end
200note LIVE slot: check generation (summary checks world_generation as MIN)
201chk_gen = get ev cap_generation
202if eq get ev kind ARGUS_EV_CAPABILITY_USE_SUMMARY
203chk_gen = get ev world_generation
204end
205if lt chk_gen sgen
206argus_set_finding dst ARGUS_F_STALE_GENERATION ARGUS_SEV_CRITICAL ev sseq
207put dst containment ARGUS_CONTAIN_REVOKE_CAPABILITY
208ret ARGUS_F_STALE_GENERATION
209end
210if ne get ev principal get argus_sh_subj idx
211argus_set_finding dst ARGUS_F_SUBJECT_MISMATCH ARGUS_SEV_HIGH ev sseq
212put dst containment ARGUS_CONTAIN_REVOKE_CAPABILITY
213ret ARGUS_F_SUBJECT_MISMATCH
214end
215ret ARGUS_F_NONE
216end
217
218fn argus_core_apply ev view ArgusEvent dst edit ArgusFinding to ushort
219k = get ev kind
220if eq k ARGUS_EV_TELEMETRY_DROPPED
221lost = get ev object_id
222if or eq lost uint ARGUS_CLASS_CRITICAL eq lost uint ARGUS_CLASS_SECURITY
223argus_set_finding dst ARGUS_F_TELEMETRY_LOSS ARGUS_SEV_CRITICAL ev 0
224ret ARGUS_F_TELEMETRY_LOSS
225end
226ret ARGUS_F_NONE
227end
228if eq k ARGUS_EV_CAPABILITY_GRANTED
229cid = get ev cap_id
230if ge cid ARGUS_CAP_MAX
231ret ARGUS_F_NONE
232end
233idx = ulong cid
234st = get argus_sh_state idx
235if and ne st ARGUS_SH_UNSEEN le get ev cap_generation get argus_sh_gen idx
236put argus_core_meta 3 add get argus_core_meta 3 1
237argus_set_finding dst ARGUS_F_AUTHORITY_REPLAY ARGUS_SEV_CRITICAL ev get argus_sh_seq idx
238ret ARGUS_F_AUTHORITY_REPLAY
239end
240put argus_sh_state idx ARGUS_SH_LIVE
241put argus_sh_gen idx get ev cap_generation
242put argus_sh_subj idx get ev principal
243put argus_sh_rgt idx get ev object_id
244put argus_sh_res idx get ev resource
245put argus_sh_seq idx get ev sequence
246ret ARGUS_F_NONE
247end
248if eq k ARGUS_EV_CAPABILITY_REVOKED
249cid = get ev cap_id
250if ge cid ARGUS_CAP_MAX
251ret ARGUS_F_NONE
252end
253idx = ulong cid
254if eq get argus_sh_state idx ARGUS_SH_UNSEEN
255put argus_core_meta 3 add get argus_core_meta 3 1
256argus_set_finding dst ARGUS_F_AUTHORITY_REPLAY ARGUS_SEV_CRITICAL ev 0
257ret ARGUS_F_AUTHORITY_REPLAY
258end
259put argus_sh_state idx ARGUS_SH_REVOKED
260put argus_sh_gen idx get ev cap_generation
261put argus_sh_seq idx get ev sequence
262ret ARGUS_F_NONE
263end
264if or eq k ARGUS_EV_CAPABILITY_USED eq k ARGUS_EV_CAPABILITY_USE_SUMMARY
265ret argus_detect_cap_use ev dst
266end
267if eq k ARGUS_EV_WORLD_COMMITTED
268if not argus_check_world_commit get ev object_id get ev world_generation
269put argus_core_meta 3 add get argus_core_meta 3 1
270argus_set_finding dst ARGUS_F_WORLD_PROVENANCE_INCONSISTENT ARGUS_SEV_CRITICAL ev 0
271ret ARGUS_F_WORLD_PROVENANCE_INCONSISTENT
272end
273ret ARGUS_F_NONE
274end
275ret ARGUS_F_NONE
276end
277
278note Ingest one event: validate -> chain -> detect -> apply.
279note Returns finding code (ARGUS_F_NONE if clean) and populates dst on finding.
280fn argus_core_ingest ev view ArgusEvent mach view uchar 32 evdig view uchar 32 dst edit ArgusFinding to ushort
281put argus_core_meta 0 add get argus_core_meta 0 1
282vrc = argus_event_validate ev
283if ne vrc ARGUS_OK
284put argus_core_meta 1 add get argus_core_meta 1 1
285put argus_core_meta 3 add get argus_core_meta 3 1
286argus_set_finding dst ARGUS_F_MALFORMED_EVENT ARGUS_SEV_HIGH ev 0
287ret ARGUS_F_MALFORMED_EVENT
288end
289note Extend SHA-256 event hash chain
290tmp_chain = new uchar 32 0
291for i ulong 32
292put tmp_chain i get argus_core_chain i
293end
294argus_chain_extend tmp_chain ev mach evdig
295for i ulong 32
296put argus_core_chain i get tmp_chain i
297end
298note Sequence monotonicity check: anomaly is detected and NOT applied
299if not argus_check_stream get ev flags get ev sequence
300put argus_core_meta 3 add get argus_core_meta 3 1
301argus_set_finding dst ARGUS_F_SEQUENCE_ANOMALY ARGUS_SEV_HIGH ev 0
302ret ARGUS_F_SEQUENCE_ANOMALY
303end
304ret argus_core_apply ev dst
305end
306
307note ---- Containment State Machine (Invariants I1..I5) ----
308
309fn argus_contain_init to void
310for i ulong 8
311put argus_cmeta i 0
312end
313put argus_cmeta 1 1
314for i ulong 10
315put argus_chealth i 0
316end
317for i ulong 32
318put argus_pend_st i ARGUS_ST_FREE
319put argus_pend_typ i 0
320put argus_pend_fcode i 0
321put argus_pend_princ i 0
322put argus_pend_cid i 0
323put argus_pend_cgen i 0
324put argus_pend_rid i 0
325put argus_pend_k4 i 0
326end
327for i ulong 1024
328put argus_pend_dig i 0
329end
330end
331
332fn argus_contain_health dst edit ArgusContainHealth to void
333put dst proposed get argus_chealth 0
334put dst granted get argus_chealth 1
335put dst denied get argus_chealth 2
336put dst escalated get argus_chealth 3
337put dst confirmed get argus_chealth 4
338put dst failed_scope get argus_chealth 5
339put dst decisions_unmatched get argus_chealth 6
340put dst executions_unauthorized get argus_chealth 7
341put dst executions_unconfirmed get argus_chealth 8
342put dst rate_limited get argus_chealth 9
343end
344
345fn argus_pend_is_open st uchar to bool
346ret or eq st ARGUS_ST_PROPOSED or eq st ARGUS_ST_GRANTED eq st ARGUS_ST_ESCALATED
347end
348
349fn argus_pend_find_rid rid ulong to int
350for i ulong 32
351if ne get argus_pend_st i ARGUS_ST_FREE
352if eq get argus_pend_rid i rid
353ret int i
354end
355end
356end
357ret -1
358end
359
360fn argus_pend_echo_matches slot ulong ev view ArgusEvent evdig view uchar 32 to bool
361obj = get ev object_id
362if ne argus_contain_type_of obj get argus_pend_typ slot
363ret false
364end
365if ne argus_contain_code_of obj get argus_pend_fcode slot
366ret false
367end
368if ne get ev principal get argus_pend_princ slot
369ret false
370end
371if ne get ev cap_id get argus_pend_cid slot
372ret false
373end
374if ne get ev cap_generation get argus_pend_cgen slot
375ret false
376end
377if ne get ev world_generation get argus_pend_rid slot
378ret false
379end
380base = mul slot 32
381diff = uchar 0
382for i ulong 32
383diff = or diff xor get argus_pend_dig add base i get evdig i
384end
385ret eq diff 0
386end
387
388note Propose a containment request: enforces I1.c, I2, I3.b, computes canonical 152-byte digest.
389fn argus_contain_propose req edit ArgusContainReq mach view uchar 32 fdig view uchar 32 tdig view uchar 32 dig_out edit uchar 32 to int
390ct = get req containment
391if or lt ct 1 gt ct ARGUS_CONTAIN_MAX
392ret ARGUS_ERR_MALFORMED
393end
394if eq get req finding_sequence 0
395ret ARGUS_ERR_MALFORMED
396end
397note I2: only REVOKE_CAPABILITY has a live executor; non-live types require SYNTHETIC
398if ne ct ARGUS_CONTAIN_REVOKE_CAPABILITY
399if eq and get req flags ARGUS_CREQ_SYNTHETIC 0
400ret ARGUS_ERR_MALFORMED
401end
402end
403note I3.b: global proposal budget per window
404wcnt = get argus_cmeta 3
405if ge wcnt ulong ARGUS_CONTAIN_WINDOW_MAX
406put argus_chealth 9 add get argus_chealth 9 1
407ret ARGUS_ERR_FULL
408end
409slot = int -1
410for i ulong 32
411if eq get argus_pend_st i ARGUS_ST_FREE
412slot = int i
413break
414end
415end
416if lt slot 0
417ret ARGUS_ERR_FULL
418end
419rid = get argus_cmeta 1
420put argus_cmeta 1 add rid 1
421put req request_id rid
422put req version ARGUS_CONTAIN_REQ_VERSION
423put req reserved 0
424argus_creq_digest req mach fdig tdig dig_out
425idx = ulong slot
426put argus_pend_st idx ARGUS_ST_PROPOSED
427put argus_pend_typ idx ct
428put argus_pend_fcode idx get req finding_code
429put argus_pend_princ idx get req principal
430put argus_pend_cid idx get req target_cap_id
431put argus_pend_cgen idx get req target_cap_gen
432put argus_pend_rid idx rid
433put argus_pend_k4 idx 0
434base = mul idx 32
435for i ulong 32
436put argus_pend_dig add base i get dig_out i
437end
438put argus_cmeta 3 add wcnt 1
439put argus_chealth 0 add get argus_chealth 0 1
440ret ARGUS_OK
441end
442
443fn argus_contain_observe_revoke ev view ArgusEvent to ushort
444cid = get ev cap_id
445cgen = get ev cap_generation
446for i ulong 32
447st = get argus_pend_st i
448if argus_pend_is_open st
449if and eq get argus_pend_cid i cid eq get argus_pend_cgen i cgen
450if ne st ARGUS_ST_GRANTED
451put argus_chealth 7 add get argus_chealth 7 1
452ret ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED
453end
454k4 = add get argus_pend_k4 i 1
455put argus_pend_k4 i k4
456if gt k4 1
457put argus_pend_st i ARGUS_ST_FAILED_SCOPE
458put argus_chealth 5 add get argus_chealth 5 1
459ret ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED
460end
461ret ARGUS_F_NONE
462end
463end
464end
465ret ARGUS_F_NONE
466end
467
468fn argus_contain_observe_decided idx ulong ev view ArgusEvent to ushort
469st = get argus_pend_st idx
470cst = argus_contain_status_of get ev object_id
471if eq st ARGUS_ST_PROPOSED
472if eq cst ARGUS_CSTATUS_GRANT
473put argus_pend_st idx ARGUS_ST_GRANTED
474put argus_chealth 1 add get argus_chealth 1 1
475ret ARGUS_F_NONE
476end
477if eq cst ARGUS_CSTATUS_DENY
478put argus_pend_st idx ARGUS_ST_DENIED
479put argus_chealth 2 add get argus_chealth 2 1
480ret ARGUS_F_NONE
481end
482if eq cst ARGUS_CSTATUS_ESCALATE
483put argus_pend_st idx ARGUS_ST_ESCALATED
484put argus_chealth 3 add get argus_chealth 3 1
485ret ARGUS_F_NONE
486end
487end
488if eq st ARGUS_ST_ESCALATED
489if eq cst ARGUS_CSTATUS_GRANT
490put argus_pend_st idx ARGUS_ST_GRANTED
491put argus_chealth 1 add get argus_chealth 1 1
492ret ARGUS_F_NONE
493end
494if eq cst ARGUS_CSTATUS_DENY
495put argus_pend_st idx ARGUS_ST_DENIED
496put argus_chealth 2 add get argus_chealth 2 1
497ret ARGUS_F_NONE
498end
499end
500put argus_chealth 6 add get argus_chealth 6 1
501ret ARGUS_F_CONTAINMENT_DECISION_UNMATCHED
502end
503
504fn argus_contain_observe_executed idx ulong ev view ArgusEvent to ushort
505st = get argus_pend_st idx
506cst = argus_contain_status_of get ev object_id
507if ne st ARGUS_ST_GRANTED
508if or eq cst ARGUS_CSTATUS_DONE eq cst ARGUS_CSTATUS_PARTIAL
509put argus_chealth 7 add get argus_chealth 7 1
510ret ARGUS_F_CONTAINMENT_EXECUTION_UNAUTHORIZED
511end
512put argus_chealth 6 add get argus_chealth 6 1
513ret ARGUS_F_CONTAINMENT_DECISION_UNMATCHED
514end
515lo32 = uint and get ev resource 0xffffffff
516bad_scope = eq cst ARGUS_CSTATUS_FAILED_SCOPE
517if and eq cst ARGUS_CSTATUS_DONE ne lo32 1
518bad_scope = true
519end
520if and eq cst ARGUS_CSTATUS_PARTIAL gt lo32 1
521bad_scope = true
522end
523if bad_scope
524put argus_pend_st idx ARGUS_ST_FAILED_SCOPE
525put argus_chealth 5 add get argus_chealth 5 1
526ret ARGUS_F_CONTAINMENT_SCOPE_EXCEEDED
527end
528if eq cst ARGUS_CSTATUS_DONE
529if eq get argus_pend_k4 idx 1
530put argus_pend_st idx ARGUS_ST_CONFIRMED
531put argus_chealth 4 add get argus_chealth 4 1
532ret ARGUS_F_NONE
533end
534put argus_chealth 8 add get argus_chealth 8 1
535ret ARGUS_F_CONTAINMENT_EXECUTION_UNCONFIRMED
536end
537if or eq cst ARGUS_CSTATUS_FAILED eq cst ARGUS_CSTATUS_UNAVAILABLE
538put argus_pend_st idx ARGUS_ST_FAILED
539ret ARGUS_F_NONE
540end
541ret ARGUS_F_NONE
542end
543
544note Observe an event for containment lifecycle tracking. Returns finding code (ARGUS_F_NONE if clean).
545fn argus_contain_observe ev view ArgusEvent evdig view uchar 32 to ushort
546evs = add get argus_cmeta 0 1
547put argus_cmeta 0 evs
548wstart = get argus_cmeta 2
549if gt evs add wstart ARGUS_CONTAIN_WINDOW_EVENTS
550put argus_cmeta 2 mul div sub evs 1 ARGUS_CONTAIN_WINDOW_EVENTS ARGUS_CONTAIN_WINDOW_EVENTS
551put argus_cmeta 3 0
552end
553k = get ev kind
554if eq k ARGUS_EV_CAPABILITY_REVOKED
555ret argus_contain_observe_revoke ev
556end
557if or eq k ARGUS_EV_CONTAINMENT_DECIDED eq k ARGUS_EV_CONTAINMENT_EXECUTED
558ix = argus_pend_find_rid get ev world_generation
559if lt ix 0
560put argus_chealth 6 add get argus_chealth 6 1
561ret ARGUS_F_CONTAINMENT_DECISION_UNMATCHED
562end
563idx = ulong ix
564if or not argus_pend_is_open get argus_pend_st idx not argus_pend_echo_matches idx ev evdig
565put argus_chealth 6 add get argus_chealth 6 1
566ret ARGUS_F_CONTAINMENT_DECISION_UNMATCHED
567end
568if eq k ARGUS_EV_CONTAINMENT_DECIDED
569ret argus_contain_observe_decided idx ev
570end
571ret argus_contain_observe_executed idx ev
572end
573ret ARGUS_F_NONE
574end