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

1174 lines · 29575 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/capability/contain.om: Pure-Omega AEGIS Containment Gate (ARGUS-1 lane G).
3note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux
4note native/capability/aienos_contain.{h,c}.
5note Composes with: si/directives/om2.om aienux/capability/lib.om aienux/capability/contain.om <test.om> om/tls/sha2.om
6
7note Protected subjects (I5.b)
8const CON_SUBJ uint 43
9const CON_SUBJ_AEGIS uint 41
10const CON_SUBJ_AEGIS_ROOT uint 42
11
12note Receipt actors
13const CON_ACTOR_GATE uint 43
14const CON_ACTOR_OFFICE uint 0
15
16note Gate limits
17const CON_BUDGET ulong 8
18const CON_BUDGET_WINDOW ulong 64
19const CON_DECISIONS ulong 32
20const CON_REGISTRY ulong 256
21const CON_REQ_SIZE ulong 152
22const CON_REQ_VERSION uchar 2
23const CON_CAP_NONE uint 0xffffffff
24
25note Request types (1..10; only REVOKE_CAPABILITY is LIVE)
26const CON_REVOKE_CAPABILITY uchar 1
27const CON_FREEZE_PRINCIPAL uchar 2
28const CON_RESTRICT_PRINCIPAL uchar 3
29const CON_QUARANTINE_MACHINE uchar 4
30const CON_QUARANTINE_PROVIDER uchar 5
31const CON_REVOKE_CRED_LEASE uchar 6
32const CON_REJECT_ARTIFACT uchar 7
33const CON_REQUIRE_REATTEST uchar 8
34const CON_RAISE_EFFECT_CLASS uchar 9
35const CON_PAUSE_EXT_EFFECTS uchar 10
36const CON_TYPE_MAX uchar 10
37
38note Request flags
39const CON_CREQ_SYNTHETIC ushort 0x0001
40const CON_CREQ_SATURATION ushort 0x0002
41const CON_CREQ_KNOWN ushort 0x0003
42
43note Receipt status (1..3 decision, 4..8 execution)
44const CON_GRANT uchar 1
45const CON_DENY uchar 2
46const CON_ESCALATE uchar 3
47const CON_DONE uchar 4
48const CON_PARTIAL uchar 5
49const CON_FAILED uchar 6
50const CON_UNAVAILABLE uchar 7
51const CON_FAILED_SCOPE uchar 8
52
53note Receipt kind
54const CON_KIND_DECIDED uchar 91
55const CON_KIND_EXECUTED uchar 92
56
57note WHY codes (spec 5.2)
58const CON_WHY_POLICY_GRANT ushort 1100
59const CON_WHY_POLICY_DENY ushort 1101
60const CON_WHY_POLICY_ESCALATE ushort 1102
61const CON_WHY_PROTECTED_TARGET ushort 1103
62const CON_WHY_TARGET_NOT_LIVE ushort 1104
63const CON_WHY_SUBJECT_MISMATCH ushort 1105
64const CON_WHY_REPLAYED_REQUEST ushort 1106
65const CON_WHY_BUDGET ushort 1107
66const CON_WHY_BAD_REQUEST ushort 1108
67const CON_WHY_NO_EXECUTOR ushort 1109
68const CON_WHY_HUMAN_DENY ushort 1110
69const CON_WHY_HUMAN_GRANT ushort 1111
70const CON_WHY_BAD_SECRET ushort 1112
71const CON_WHY_DECISION_REPLAY ushort 1113
72const CON_WHY_NOT_LEAF ushort 1114
73const CON_WHY_LINEAGE_UNTRUSTED ushort 1115
74const CON_WHY_NOT_AUTO_ELIGIBLE ushort 1116
75
76note Receipt gate_flags
77const CON_RF_REFUSED_CALL ushort 0x0001
78const CON_RF_HUMAN ushort 0x0002
79const CON_RF_SEAM ushort 0x0004
80const CON_RF_INERT ushort 0x0008
81
82note Function return codes
83const CON_OK int 0
84const CON_ERR_ARG int -1
85const CON_ERR_SPACE int -2
86const CON_ERR_EXISTS int -3
87const CON_ERR_AUTHORITY int -4
88const CON_ERR_UNKNOWN int -5
89const CON_ERR_REFUSED int -6
90const CON_ERR_FULL int -7
91
92note Decision slot states
93const CON_D_FREE uchar 0
94const CON_D_GRANTED uchar 1
95const CON_D_DENIED uchar 2
96const CON_D_ESCALATED uchar 3
97const CON_D_EXECUTED uchar 4
98
99note Receipt array indices (ulong 16)
100const CON_REC_SEQ ulong 0
101const CON_REC_DEC_ID ulong 1
102const CON_REC_REQ_ID ulong 2
103const CON_REC_FSEQ ulong 3
104const CON_REC_TICK ulong 4
105const CON_REC_RESOURCE ulong 5
106const CON_REC_TGT_ID ulong 6
107const CON_REC_TGT_GEN ulong 7
108const CON_REC_EXEC_ID ulong 8
109const CON_REC_EXEC_GEN ulong 9
110const CON_REC_PRINCIPAL ulong 10
111const CON_REC_ACTOR ulong 11
112const CON_REC_RC ulong 12
113const CON_REC_WHY ulong 13
114const CON_REC_FLAGS ulong 14
115const CON_REC_META ulong 15
116
117note Gate metadata indices in con_meta (ulong 24)
118const CON_M_ACTIVE ulong 0
119const CON_M_OFF_ID ulong 1
120const CON_M_OFF_GEN ulong 2
121const CON_M_EXEC_ID ulong 3
122const CON_M_EXEC_GEN ulong 4
123const CON_M_SEQ ulong 5
124const CON_M_LAST_DEC ulong 6
125const CON_M_LAST_REQ ulong 7
126const CON_M_WINDOW ulong 8
127const CON_M_WIN_GRANTS ulong 9
128const CON_M_INERT ulong 10
129const CON_M_TRUSTED ulong 11
130const CON_M_EXEC_DEAD ulong 12
131const CON_M_OBS_CUR ulong 13
132const CON_M_COUNTING ulong 14
133const CON_M_REV_SEEN ulong 15
134const CON_M_TGT_SEEN ulong 16
135const CON_M_ETGT_ID ulong 17
136const CON_M_ETGT_GEN ulong 18
137const CON_M_SEAMS_ON ulong 19
138const CON_M_SEAM_FLAGS ulong 20
139const CON_M_SEAM_TEST_ST ulong 21
140const CON_M_REG_N ulong 22
141
142note Global gate state (no heap, bounded static footprint)
143global con_meta ulong 24
144global con_reg_gen ulong 256
145global con_policy uint 11
146
147note Lineage shadow table (256 slots)
148global con_lin_gen ulong 256
149global con_lin_pgen ulong 256
150global con_lin_subj uint 256
151global con_lin_rgt uint 256
152global con_lin_pid uint 256
153global con_lin_st uint 256
154global con_lin_known uchar 256
155
156note Decision ring (32 slots)
157global con_dec_req uchar 4864
158global con_dec_dig uchar 1024
159global con_dec_id ulong 32
160global con_dec_st uchar 32
161global con_dec_human uchar 32
162
163note Scratch for canonical request operations
164global con_tmp_req uchar 152
165global con_tmp_dig uchar 32
166
167note ---- Little-endian wire helpers ----
168
169fn con_put16 buf edit uchar * off ulong v ushort to void
170put buf off uchar and v 0xff
171put buf add off 1 uchar and shr v 8 0xff
172end
173
174fn con_put32 buf edit uchar * off ulong v uint to void
175put buf off uchar and v 0xff
176put buf add off 1 uchar and shr v 8 0xff
177put buf add off 2 uchar and shr v 16 0xff
178put buf add off 3 uchar and shr v 24 0xff
179end
180
181fn con_put64 buf edit uchar * off ulong v ulong to void
182for i ulong 8 unroll
183put buf add off i uchar and shr v mul i 8 0xff
184end
185end
186
187fn con_get16 buf view uchar * off ulong to ushort
188b0 = ushort get buf off
189b1 = ushort get buf add off 1
190ret or b0 shl b1 8
191end
192
193fn con_get32 buf view uchar * off ulong to uint
194b0 = uint get buf off
195b1 = uint get buf add off 1
196b2 = uint get buf add off 2
197b3 = uint get buf add off 3
198ret or or b0 shl b1 8 or shl b2 16 shl b3 24
199end
200
201fn con_get64 buf view uchar * off ulong to ulong
202v = ulong 0
203for i ulong 8 unroll
204b = ulong get buf add off i
205v = or v shl b mul i 8
206end
207ret v
208end
209
210note ---- Canonical 152-byte request validation & digest ----
211
212fn con_req_well_formed buf view uchar * to bool
213if ne len buf CON_REQ_SIZE
214ret false
215end
216ct = get buf 8
217if or lt ct 1 gt ct CON_TYPE_MAX
218ret false
219end
220sev = get buf 9
221if or lt sev 1 gt sev 5
222ret false
223end
224ver = get buf 150
225if ne ver CON_REQ_VERSION
226ret false
227end
228rsv = get buf 151
229if ne rsv 0
230ret false
231end
232fl = con_get16 buf 148
233inv = xor CON_CREQ_KNOWN 0xffff
234if ne and fl inv 0
235ret false
236end
237rid = con_get64 buf 92
238if eq rid 0
239ret false
240end
241fseq = con_get64 buf 100
242if eq fseq 0
243ret false
244end
245ret true
246end
247
248fn con_req_narrow_shape buf view uchar * to bool
249cid = con_get32 buf 16
250if and ne cid CON_CAP_NONE ge cid CAP_MAX
251ret false
252end
253ct = get buf 8
254trgt = con_get32 buf 112
255if and ne ct CON_RESTRICT_PRINCIPAL ne trgt 0
256ret false
257end
258if eq ct CON_RESTRICT_PRINCIPAL
259inv = xor CAP_RIGHT_KNOWN 0xffffffff
260if ne and trgt inv 0
261ret false
262end
263end
264if eq ct CON_REVOKE_CAPABILITY
265if or eq cid CON_CAP_NONE ne con_get32 buf 108 0
266ret false
267end
268end
269ret true
270end
271
272fn con_req_digest buf view uchar * dst edit uchar * to void
273dlen = sha2_digest 256 buf dst
274if eq dlen 0
275trap 8
276end
277end
278
279fn con_dig_eq a view uchar * b view uchar * to bool
280diff = uchar 0
281for i ulong 32
282diff = or diff xor get a i get b i
283end
284ret eq diff 0
285end
286
287note ---- Authority registry (prevents duplicate create per authority instance) ----
288
289fn con_reg_claim office_gen ulong to int
290n = get con_meta CON_M_REG_N
291i = ulong 0
292while lt i n bound 256
293if eq get con_reg_gen i office_gen
294ret CON_ERR_EXISTS
295end
296i = add i 1
297end
298if ge n CON_REGISTRY
299ret CON_ERR_FULL
300end
301put con_reg_gen n office_gen
302put con_meta CON_M_REG_N add n 1
303ret CON_OK
304end
305
306fn con_reg_release office_gen ulong to void
307n = get con_meta CON_M_REG_N
308i = ulong 0
309while lt i n bound 256
310if eq get con_reg_gen i office_gen
311last = sub n 1
312put con_reg_gen i get con_reg_gen last
313put con_meta CON_M_REG_N last
314ret
315end
316i = add i 1
317end
318end
319
320note ---- Table policy ----
321
322fn contain_policy_reset to void
323for i ulong 11
324put con_policy i uint CON_ESCALATE
325end
326end
327
328fn contain_policy_set rtype uchar verdict uchar to void
329if and ge rtype 1 le rtype CON_TYPE_MAX
330put con_policy ulong rtype uint verdict
331end
332end
333
334fn con_policy_decide rtype uchar to uchar
335if and ge rtype 1 le rtype CON_TYPE_MAX
336v = get con_policy ulong rtype
337if eq v uint CON_GRANT
338ret CON_GRANT
339end
340if eq v uint CON_DENY
341ret CON_DENY
342end
343end
344ret CON_ESCALATE
345end
346
347note ---- Lineage shadow index (fed by authority observer) ----
348
349fn con_lineage_apply_mint ev view CapObsEvent cid uint idx ulong to void
350ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
351cref = new CapRef cid get ev generation
352irc = cap_inspect cref ent
353if eq irc CAP_OK
354put con_lin_gen idx get ent generation
355put con_lin_pgen idx get ent parent_generation
356put con_lin_subj idx get ent subject
357put con_lin_rgt idx get ent rights
358put con_lin_pid idx get ent parent_id
359put con_lin_st idx get ent state
360put con_lin_known idx 1
361else
362put con_lin_gen idx get ev generation
363put con_lin_pgen idx 0
364put con_lin_subj idx 0
365put con_lin_rgt idx get ev rights
366put con_lin_pid idx get ev parent_id
367put con_lin_st idx get ev state
368put con_lin_known idx 1
369end
370end
371
372fn con_lineage_apply ev view CapObsEvent to void
373op = get ev op
374rc = get ev rc
375has = get ev has_entry
376if and ne get con_meta CON_M_COUNTING 0 and eq op CAP_OBS_REVOKE ne has 0
377put con_meta CON_M_REV_SEEN add get con_meta CON_M_REV_SEEN 1
378if and eq ulong get ev cap_id get con_meta CON_M_ETGT_ID eq get ev generation get con_meta CON_M_ETGT_GEN
379put con_meta CON_M_TGT_SEEN 1
380end
381end
382if eq has 0
383if eq op CAP_OBS_KILL
384put con_meta CON_M_TRUSTED 0
385else
386if eq rc CAP_OK
387put con_meta CON_M_TRUSTED 0
388end
389end
390ret
391end
392cid = get ev cap_id
393if ge cid CAP_MAX
394put con_meta CON_M_TRUSTED 0
395ret
396end
397idx = ulong cid
398if eq op CAP_OBS_MINT
399con_lineage_apply_mint ev cid idx
400ret
401end
402if eq op CAP_OBS_REVOKE
403k = uchar 0
404if and ne get con_lin_known idx 0 eq get con_lin_gen idx get ev generation
405k = 1
406end
407put con_lin_gen idx get ev generation
408put con_lin_rgt idx get ev rights
409put con_lin_pid idx get ev parent_id
410put con_lin_st idx get ev state
411put con_lin_known idx k
412if and eq ulong cid get con_meta CON_M_EXEC_ID eq get ev generation get con_meta CON_M_EXEC_GEN
413put con_meta CON_M_EXEC_DEAD 1
414end
415ret
416end
417if eq op CAP_OBS_RECLAIM
418put con_lin_gen idx get ev generation
419put con_lin_pgen idx 0
420put con_lin_subj idx 0
421put con_lin_rgt idx 0
422put con_lin_pid idx CAP_PARENT_NONE
423put con_lin_st idx get ev state
424put con_lin_known idx 1
425ret
426end
427if eq op CAP_OBS_CLOCK
428ret
429end
430put con_meta CON_M_TRUSTED 0
431end
432
433note Drain all newly emitted authority observer events into the gate lineage shadow.
434fn contain_sync_observer to void
435cur = get con_meta CON_M_OBS_CUR
436tot = cap_obs_count
437ev = new CapObsEvent 0 0 0 0 0 0 0 0 0
438while lt cur tot bound 512
439if cap_obs_get cur ev
440con_lineage_apply ev
441end
442cur = add cur 1
443end
444put con_meta CON_M_OBS_CUR cur
445end
446
447note ---- Protected target & structural checks ----
448
449fn con_is_protected_subj s uint to bool
450if eq s CON_SUBJ
451ret true
452end
453if eq s CON_SUBJ_AEGIS
454ret true
455end
456if eq s CON_SUBJ_AEGIS_ROOT
457ret true
458end
459ret false
460end
461
462fn con_subj_privileged subj uint to bool
463for i ulong 256
464if eq get con_lin_st i CAP_STATE_LIVE
465if eq get con_lin_subj i subj
466if ne and get con_lin_rgt i CAP_RIGHT_PRIVILEGED 0
467ret true
468end
469end
470end
471end
472ret false
473end
474
475fn con_has_live_child cid uint cgen ulong to bool
476for i ulong 256
477if eq get con_lin_st i CAP_STATE_LIVE
478if and eq get con_lin_pid i cid eq get con_lin_pgen i cgen
479ret true
480end
481end
482end
483ret false
484end
485
486fn con_lin_is_priv cid uint cgen ulong to bool
487if ge cid CAP_MAX
488ret false
489end
490idx = ulong cid
491if ne get con_lin_gen idx cgen
492ret false
493end
494ret ne and get con_lin_rgt idx CAP_RIGHT_PRIVILEGED 0
495end
496
497fn con_is_protected buf view uchar * to bool
498if and ne get con_meta CON_M_SEAMS_ON 0 ne and get con_meta CON_M_SEAM_FLAGS 1 0
499ret false
500end
501princ = con_get32 buf 12
502if con_is_protected_subj princ
503ret true
504end
505if ne princ 0
506if con_subj_privileged princ
507ret true
508end
509end
510cid = con_get32 buf 16
511if eq cid CON_CAP_NONE
512ret false
513end
514if or eq cid 0 or eq ulong cid get con_meta CON_M_EXEC_ID eq ulong cid get con_meta CON_M_OFF_ID
515ret true
516end
517cgen = con_get64 buf 20
518cref = new CapRef cid cgen
519ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
520if eq cap_inspect cref ent CAP_OK
521if ne and get ent rights CAP_RIGHT_PRIVILEGED 0
522ret true
523end
524esubj = get ent subject
525if or con_is_protected_subj esubj con_subj_privileged esubj
526ret true
527end
528end
529ret con_lin_is_priv cid cgen
530end
531
532fn con_executor_live to bool
533if ne get con_meta CON_M_EXEC_DEAD 0
534ret false
535end
536ex = new CapRef uint get con_meta CON_M_EXEC_ID get con_meta CON_M_EXEC_GEN
537ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
538if ne cap_inspect ex ent CAP_OK
539put con_meta CON_M_EXEC_DEAD 1
540ret false
541end
542if or ne get ent state CAP_STATE_LIVE or ne get ent rights CAP_RIGHT_REVOKE ne get ent subject CON_SUBJ
543put con_meta CON_M_EXEC_DEAD 1
544ret false
545end
546ret true
547end
548
549fn con_lin_is_live cid uint cgen ulong to bool
550idx = ulong cid
551if eq get con_lin_known idx 0
552ret false
553end
554if ne get con_lin_gen idx cgen
555ret false
556end
557ret eq get con_lin_st idx CAP_STATE_LIVE
558end
559
560note Returns 0 if eligible, else WHY code; ask_out[0]=1 means ESCALATE, 0 means DENY.
561fn con_revoke_target_check buf view uchar * ask_out edit uchar 1 to ushort
562put ask_out 0 0
563cid = con_get32 buf 16
564cgen = con_get64 buf 20
565cref = new CapRef cid cgen
566ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
567if ne cap_inspect cref ent CAP_OK
568ret CON_WHY_TARGET_NOT_LIVE
569end
570if ne get ent state CAP_STATE_LIVE
571ret CON_WHY_TARGET_NOT_LIVE
572end
573princ = con_get32 buf 12
574if and ne princ 0 ne get ent subject princ
575ret CON_WHY_SUBJECT_MISMATCH
576end
577if not con_lin_is_live cid cgen
578put ask_out 0 1
579ret CON_WHY_LINEAGE_UNTRUSTED
580end
581if con_has_live_child cid cgen
582put ask_out 0 1
583ret CON_WHY_NOT_LEAF
584end
585ret 0
586end
587
588note ---- Receipt construction & decision ring ----
589
590fn con_pack_flags fcode ushort rflags ushort gflags ushort to ulong
591ret or or ulong fcode shl ulong rflags 16 shl ulong gflags 32
592end
593
594fn con_pack_meta kind uchar status uchar rtype uchar to ulong
595ret or or ulong kind shl ulong status 8 shl ulong rtype 16
596end
597
598fn con_rec_status rec view ulong 16 to uchar
599ret uchar and shr get rec CON_REC_META 8 0xff
600end
601
602fn con_rec_kind rec view ulong 16 to uchar
603ret uchar and get rec CON_REC_META 0xff
604end
605
606fn con_rec_gate_flags rec view ulong 16 to ushort
607ret ushort and shr get rec CON_REC_FLAGS 32 0xffff
608end
609
610fn con_rec_req_flags rec view ulong 16 to ushort
611ret ushort and shr get rec CON_REC_FLAGS 16 0xffff
612end
613
614fn con_receipt_base rec edit ulong 16 rdig edit uchar 32 buf view uchar 152 dig view uchar 32 has_req bool kind uchar to void
615for i ulong 16
616put rec i 0
617end
618for i ulong 32
619put rdig i get dig i
620end
621seq = add get con_meta CON_M_SEQ 1
622put con_meta CON_M_SEQ seq
623put rec CON_REC_SEQ seq
624put rec CON_REC_ACTOR ulong CON_ACTOR_GATE
625put rec CON_REC_EXEC_ID get con_meta CON_M_EXEC_ID
626put rec CON_REC_EXEC_GEN get con_meta CON_M_EXEC_GEN
627put rec CON_REC_TICK cap_clock
628gfl = ushort 0
629if ne get con_meta CON_M_SEAMS_ON 0
630gfl = or gfl CON_RF_SEAM
631end
632if has_req
633put rec CON_REC_REQ_ID con_get64 buf 92
634put rec CON_REC_FSEQ con_get64 buf 100
635put rec CON_REC_TGT_ID ulong con_get32 buf 16
636put rec CON_REC_TGT_GEN con_get64 buf 20
637put rec CON_REC_PRINCIPAL ulong con_get32 buf 12
638put rec CON_REC_FLAGS con_pack_flags con_get16 buf 10 con_get16 buf 148 gfl
639put rec CON_REC_META con_pack_meta kind 0 get buf 8
640else
641put rec CON_REC_FLAGS con_pack_flags 0 0 gfl
642put rec CON_REC_META con_pack_meta kind 0 0
643end
644end
645
646fn con_dec_find did ulong to int
647if eq did 0
648ret -1
649end
650for i ulong 32
651if ne get con_dec_st i CON_D_FREE
652if eq get con_dec_id i did
653ret int i
654end
655end
656end
657ret -1
658end
659
660fn con_dec_alloc_slot to ulong
661best = ulong 0
662best_id = get con_dec_id 0
663for i ulong 32
664if eq get con_dec_st i CON_D_FREE
665ret i
666end
667cid = get con_dec_id i
668if lt cid best_id
669best = i
670best_id = cid
671end
672end
673ret best
674end
675
676fn con_pack_dec_arg status uchar why ushort keep bool to ulong
677k = ulong 0
678if keep
679k = 1
680end
681ret or or ulong status shl ulong why 8 shl k 24
682end
683
684fn con_dec_store did ulong status uchar buf view uchar 152 dig view uchar 32 to void
685slot = con_dec_alloc_slot
686put con_dec_id slot did
687put con_dec_human slot 0
688if eq status CON_GRANT
689put con_dec_st slot CON_D_GRANTED
690else
691if eq status CON_ESCALATE
692put con_dec_st slot CON_D_ESCALATED
693else
694put con_dec_st slot CON_D_DENIED
695end
696end
697rbase = mul slot CON_REQ_SIZE
698for i ulong 152
699put con_dec_req add rbase i get buf i
700end
701dbase = mul slot 32
702for i ulong 32
703put con_dec_dig add dbase i get dig i
704end
705end
706
707fn con_decide_out buf view uchar 152 dig view uchar 32 darg ulong rec edit ulong 16 rdig edit uchar 32 to void
708status = uchar and darg 0xff
709why = ushort and shr darg 8 0xffff
710keep = ne and shr darg 24 1 0
711did = add get con_meta CON_M_LAST_DEC 1
712put con_meta CON_M_LAST_DEC did
713win = div sub did 1 CON_BUDGET_WINDOW
714if ne win get con_meta CON_M_WINDOW
715put con_meta CON_M_WINDOW win
716put con_meta CON_M_WIN_GRANTS 0
717end
718rtype = get buf 8
719if and eq status CON_GRANT eq rtype CON_REVOKE_CAPABILITY
720put con_meta CON_M_WIN_GRANTS add get con_meta CON_M_WIN_GRANTS 1
721end
722con_receipt_base rec rdig buf dig true CON_KIND_DECIDED
723put rec CON_REC_DEC_ID did
724put rec CON_REC_RESOURCE did
725put rec CON_REC_WHY ulong why
726put rec CON_REC_META con_pack_meta CON_KIND_DECIDED status rtype
727if ne get con_meta CON_M_INERT 0
728gfl = or con_rec_gate_flags rec CON_RF_INERT
729put rec CON_REC_FLAGS con_pack_flags con_get16 buf 10 con_get16 buf 148 gfl
730end
731if keep
732con_dec_store did status buf dig
733end
734end
735
736note ---- Public Containment Gate API ----
737
738fn contain_create off view CapRef to int
739cur = new CapRef 0 0
740if ne cap_office cur CAP_OK
741ret CON_ERR_ARG
742end
743if or ne get cur cap_id get off cap_id ne get cur generation get off generation
744ret CON_ERR_ARG
745end
746oe = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
747if ne cap_inspect off oe CAP_OK
748ret CON_ERR_ARG
749end
750if or ne get oe state CAP_STATE_LIVE eq and get oe rights CAP_RIGHT_MINT 0
751ret CON_ERR_ARG
752end
753rrc = con_reg_claim get off generation
754if ne rrc CON_OK
755ret rrc
756end
757for i ulong 22
758put con_meta i 0
759end
760for i ulong 256
761put con_lin_gen i 0
762put con_lin_pgen i 0
763put con_lin_subj i 0
764put con_lin_rgt i 0
765put con_lin_pid i CAP_PARENT_NONE
766put con_lin_st i CAP_STATE_FREE
767put con_lin_known i 0
768end
769for i ulong 32
770put con_dec_id i 0
771put con_dec_st i CON_D_FREE
772put con_dec_human i 0
773end
774put con_meta CON_M_OFF_ID ulong get off cap_id
775put con_meta CON_M_OFF_GEN get off generation
776put con_meta CON_M_EXEC_ID ulong CON_CAP_NONE
777put con_meta CON_M_OBS_CUR cap_obs_count
778oidx = ulong get off cap_id
779put con_lin_gen oidx get oe generation
780put con_lin_pgen oidx get oe parent_generation
781put con_lin_subj oidx get oe subject
782put con_lin_rgt oidx get oe rights
783put con_lin_pid oidx get oe parent_id
784put con_lin_st oidx get oe state
785put con_lin_known oidx 1
786
787note Single mint (I5.a): REVOKE only, subject CON_SUBJ, no parent, no lease.
788req = new CapMint get oe subject CON_SUBJ CAP_RES_AUTHORITY CAP_RIGHT_REVOKE 0 CAP_PARENT_NONE 0 get off cap_id get off generation
789ex = new CapRef 0 0
790mrc = cap_mint req ex
791if ne mrc CAP_OK
792con_reg_release get off generation
793ret CON_ERR_AUTHORITY
794end
795put con_meta CON_M_EXEC_ID ulong get ex cap_id
796put con_meta CON_M_EXEC_GEN get ex generation
797put con_meta CON_M_ACTIVE 1
798contain_sync_observer
799if con_lin_is_live get ex cap_id get ex generation
800if eq get con_lin_subj ulong get ex cap_id CON_SUBJ
801put con_meta CON_M_TRUSTED 1
802end
803end
804ret CON_OK
805end
806
807fn contain_executor dst edit CapRef to int
808if eq get con_meta CON_M_ACTIVE 0
809ret CON_ERR_ARG
810end
811put dst cap_id uint get con_meta CON_M_EXEC_ID
812put dst generation get con_meta CON_M_EXEC_GEN
813ret CON_OK
814end
815
816fn contain_inert to bool
817if eq get con_meta CON_M_ACTIVE 0
818ret true
819end
820ret ne get con_meta CON_M_INERT 0
821end
822
823fn contain_set_seams off view CapRef flags ulong test_st ulong to int
824if eq get con_meta CON_M_ACTIVE 0
825ret CON_ERR_REFUSED
826end
827oe = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
828if ne cap_inspect off oe CAP_OK
829ret CON_ERR_REFUSED
830end
831if or ne get oe state CAP_STATE_LIVE eq and get oe rights CAP_RIGHT_MINT 0
832ret CON_ERR_REFUSED
833end
834if or ne flags 0 ne test_st 0
835put con_meta CON_M_SEAMS_ON 1
836put con_meta CON_M_SEAM_FLAGS flags
837put con_meta CON_M_SEAM_TEST_ST test_st
838else
839put con_meta CON_M_SEAMS_ON 0
840put con_meta CON_M_SEAM_FLAGS 0
841put con_meta CON_M_SEAM_TEST_ST 0
842end
843ret CON_OK
844end
845
846fn con_decide_eval buf view uchar * st_out edit uchar 1 why_out edit ushort 1 to void
847ask = new uchar 1 0
848if con_is_protected buf
849put st_out 0 CON_DENY
850put why_out 0 CON_WHY_PROTECTED_TARGET
851ret
852end
853ct = get buf 8
854is_live = eq ct CON_REVOKE_CAPABILITY
855if is_live
856if or ne get con_meta CON_M_INERT 0 eq get con_meta CON_M_TRUSTED 0
857put st_out 0 CON_ESCALATE
858put why_out 0 CON_WHY_LINEAGE_UNTRUSTED
859ret
860end
861if not con_executor_live
862put st_out 0 CON_ESCALATE
863put why_out 0 CON_WHY_NO_EXECUTOR
864ret
865end
866w = con_revoke_target_check buf ask
867if ne w 0
868if ne get ask 0 0
869put st_out 0 CON_ESCALATE
870else
871put st_out 0 CON_DENY
872end
873put why_out 0 w
874ret
875end
876end
877v = con_policy_decide ct
878if eq v CON_DENY
879put st_out 0 CON_DENY
880put why_out 0 CON_WHY_POLICY_DENY
881ret
882end
883if ne v CON_GRANT
884put st_out 0 CON_ESCALATE
885put why_out 0 CON_WHY_POLICY_ESCALATE
886ret
887end
888fl = con_get16 buf 148
889if or not is_live ne and fl or CON_CREQ_SYNTHETIC CON_CREQ_SATURATION 0
890put st_out 0 CON_ESCALATE
891put why_out 0 CON_WHY_NOT_AUTO_ELIGIBLE
892ret
893end
894nwin = div get con_meta CON_M_LAST_DEC CON_BUDGET_WINDOW
895used = ulong 0
896if eq nwin get con_meta CON_M_WINDOW
897used = get con_meta CON_M_WIN_GRANTS
898end
899if ge used CON_BUDGET
900put st_out 0 CON_ESCALATE
901put why_out 0 CON_WHY_BUDGET
902ret
903end
904put st_out 0 CON_GRANT
905put why_out 0 CON_WHY_POLICY_GRANT
906end
907
908fn contain_submit_bytes raw view uchar * rec edit ulong 16 rdig edit uchar 32 to int
909if eq get con_meta CON_M_ACTIVE 0
910ret CON_ERR_ARG
911end
912contain_sync_observer
913d = new uchar 32 0
914dlen = sha2_digest 256 raw d
915if eq dlen 0
916trap 8
917end
918zreq = new uchar 152 0
919if ne len raw CON_REQ_SIZE
920con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig
921ret CON_OK
922end
923for i ulong 152
924put zreq i get raw i
925end
926if or not con_req_well_formed zreq not con_req_narrow_shape zreq
927con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig
928ret CON_OK
929end
930rid = con_get64 zreq 92
931if le rid get con_meta CON_M_LAST_REQ
932con_decide_out zreq d con_pack_dec_arg CON_DENY CON_WHY_REPLAYED_REQUEST false rec rdig
933ret CON_OK
934end
935put con_meta CON_M_LAST_REQ rid
936st = new uchar 1 0
937why = new ushort 1 0
938con_decide_eval zreq st why
939sv = get st 0
940con_decide_out zreq d con_pack_dec_arg sv get why 0 ne sv CON_DENY rec rdig
941ret CON_OK
942end
943
944fn contain_submit raw view uchar * req_dig view uchar 32 rec edit ulong 16 rdig edit uchar 32 to int
945if eq get con_meta CON_M_ACTIVE 0
946ret CON_ERR_ARG
947end
948if ne len raw CON_REQ_SIZE
949ret CON_ERR_ARG
950end
951contain_sync_observer
952rbuf = new uchar 152 0
953for i ulong 152
954put rbuf i get raw i
955end
956d = new uchar 32 0
957con_req_digest rbuf d
958if not con_dig_eq d req_dig
959con_decide_out rbuf d con_pack_dec_arg CON_DENY CON_WHY_BAD_REQUEST false rec rdig
960ret CON_OK
961end
962ret contain_submit_bytes rbuf rec rdig
963end
964
965fn con_load_dec_req slot ulong dst edit uchar 152 to void
966rbase = mul slot CON_REQ_SIZE
967for i ulong 152
968put dst i get con_dec_req add rbase i
969end
970end
971
972fn con_load_dec_dig slot ulong dst edit uchar 32 to void
973dbase = mul slot 32
974for i ulong 32
975put dst i get con_dec_dig add dbase i
976end
977end
978
979fn con_resolve_finish slot ulong approve bool rtype uchar rec edit ulong 16 to void
980if approve
981put con_dec_st slot CON_D_GRANTED
982put con_dec_human slot 1
983if eq rtype CON_REVOKE_CAPABILITY
984put con_meta CON_M_WIN_GRANTS add get con_meta CON_M_WIN_GRANTS 1
985end
986put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_GRANT rtype
987put rec CON_REC_WHY ulong CON_WHY_HUMAN_GRANT
988else
989put con_dec_st slot CON_D_DENIED
990put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY rtype
991put rec CON_REC_WHY ulong CON_WHY_HUMAN_DENY
992end
993end
994
995fn contain_resolve did ulong approve bool secret view uchar 32 rec edit ulong 16 rdig edit uchar 32 to int
996lreq = new uchar 152 0
997ldig = new uchar 32 0
998if eq get con_meta CON_M_ACTIVE 0
999ret CON_ERR_ARG
1000end
1001contain_sync_observer
1002sec_ok = eq cap_authorize secret CAP_OK
1003ix = con_dec_find did
1004if lt ix 0
1005con_receipt_base rec rdig lreq ldig false CON_KIND_DECIDED
1006put rec CON_REC_DEC_ID did
1007put rec CON_REC_RESOURCE did
1008put rec CON_REC_ACTOR ulong CON_ACTOR_OFFICE
1009gfl = or or con_rec_gate_flags rec CON_RF_HUMAN CON_RF_REFUSED_CALL
1010put rec CON_REC_FLAGS con_pack_flags 0 0 gfl
1011put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY 0
1012put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY
1013ret CON_ERR_UNKNOWN
1014end
1015slot = ulong ix
1016con_load_dec_req slot lreq
1017con_load_dec_dig slot ldig
1018con_receipt_base rec rdig lreq ldig true CON_KIND_DECIDED
1019put rec CON_REC_DEC_ID did
1020put rec CON_REC_RESOURCE did
1021put rec CON_REC_ACTOR ulong CON_ACTOR_OFFICE
1022rtype = get lreq 8
1023gfl = or con_rec_gate_flags rec CON_RF_HUMAN
1024dst_st = get con_dec_st slot
1025if not sec_ok
1026gfl = or gfl CON_RF_REFUSED_CALL
1027put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl
1028st_out = CON_DENY
1029if eq dst_st CON_D_ESCALATED
1030st_out = CON_ESCALATE
1031end
1032put rec CON_REC_META con_pack_meta CON_KIND_DECIDED st_out rtype
1033put rec CON_REC_WHY ulong CON_WHY_BAD_SECRET
1034ret CON_ERR_REFUSED
1035end
1036if ne dst_st CON_D_ESCALATED
1037gfl = or gfl CON_RF_REFUSED_CALL
1038put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl
1039put rec CON_REC_META con_pack_meta CON_KIND_DECIDED CON_DENY rtype
1040put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY
1041ret CON_ERR_REFUSED
1042end
1043put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 con_get16 lreq 148 gfl
1044con_resolve_finish slot approve rtype rec
1045ret CON_OK
1046end
1047
1048fn con_exec_non_revoke lreq view uchar 152 rtype uchar rfl ushort gfl ushort rec edit ulong 16 to int
1049st_out = CON_UNAVAILABLE
1050if and ne get con_meta CON_M_SEAMS_ON 0 ne get con_meta CON_M_SEAM_TEST_ST 0
1051tst = uchar and get con_meta CON_M_SEAM_TEST_ST 0xff
1052if and ge tst CON_DONE le tst CON_UNAVAILABLE
1053st_out = tst
1054else
1055st_out = CON_FAILED
1056end
1057rfl = or rfl CON_CREQ_SYNTHETIC
1058end
1059put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1060put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED st_out rtype
1061if eq st_out CON_UNAVAILABLE
1062put rec CON_REC_WHY ulong CON_WHY_NO_EXECUTOR
1063end
1064ret CON_OK
1065end
1066
1067fn con_exec_revoke_check lreq view uchar 152 st_fail_out edit uchar 1 to ushort
1068ask = new uchar 1 0
1069put st_fail_out 0 CON_FAILED
1070if or ne get con_meta CON_M_INERT 0 eq get con_meta CON_M_TRUSTED 0
1071ret CON_WHY_LINEAGE_UNTRUSTED
1072end
1073if not con_executor_live
1074put st_fail_out 0 CON_UNAVAILABLE
1075ret CON_WHY_NO_EXECUTOR
1076end
1077if con_is_protected lreq
1078ret CON_WHY_PROTECTED_TARGET
1079end
1080ret con_revoke_target_check lreq ask
1081end
1082
1083fn con_do_revoke tgt_id uint tgt_gen ulong to int
1084put con_meta CON_M_COUNTING 1
1085put con_meta CON_M_REV_SEEN 0
1086put con_meta CON_M_TGT_SEEN 0
1087put con_meta CON_M_ETGT_ID ulong tgt_id
1088put con_meta CON_M_ETGT_GEN tgt_gen
1089ex = new CapRef uint get con_meta CON_M_EXEC_ID get con_meta CON_M_EXEC_GEN
1090tgt = new CapRef tgt_id tgt_gen
1091arc = cap_revoke ex tgt
1092contain_sync_observer
1093put con_meta CON_M_COUNTING 0
1094ret arc
1095end
1096
1097fn con_exec_revoke lreq view uchar 152 rtype uchar rfl ushort gfl ushort rec edit ulong 16 to int
1098st_fail_arr = new uchar 1 0
1099why = con_exec_revoke_check lreq st_fail_arr
1100if ne why 0
1101if ne get con_meta CON_M_INERT 0
1102gfl = or gfl CON_RF_INERT
1103end
1104put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1105put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED get st_fail_arr 0 rtype
1106put rec CON_REC_WHY ulong why
1107ret CON_OK
1108end
1109arc = con_do_revoke con_get32 lreq 16 con_get64 lreq 20
1110seen = get con_meta CON_M_REV_SEEN
1111tseen = get con_meta CON_M_TGT_SEEN
1112put rec CON_REC_RC ulong long arc
1113put rec CON_REC_TICK cap_clock
1114if ne arc CAP_OK
1115put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1116put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED rtype
1117put rec CON_REC_RESOURCE shl ulong 1 32
1118else
1119if and eq seen 1 ne tseen 0
1120put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1121put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_DONE rtype
1122put rec CON_REC_RESOURCE 1
1123else
1124put con_meta CON_M_INERT 1
1125gfl = or gfl CON_RF_INERT
1126put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1127put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED_SCOPE rtype
1128put rec CON_REC_RESOURCE seen
1129end
1130end
1131ret CON_OK
1132end
1133
1134fn contain_execute did ulong rec edit ulong 16 rdig edit uchar 32 to int
1135lreq = new uchar 152 0
1136ldig = new uchar 32 0
1137if eq get con_meta CON_M_ACTIVE 0
1138ret CON_ERR_ARG
1139end
1140contain_sync_observer
1141ix = con_dec_find did
1142if lt ix 0
1143con_receipt_base rec rdig lreq ldig false CON_KIND_EXECUTED
1144put rec CON_REC_DEC_ID did
1145gfl = or con_rec_gate_flags rec CON_RF_REFUSED_CALL
1146put rec CON_REC_FLAGS con_pack_flags 0 0 gfl
1147put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED 0
1148put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY
1149ret CON_ERR_UNKNOWN
1150end
1151slot = ulong ix
1152con_load_dec_req slot lreq
1153con_load_dec_dig slot ldig
1154con_receipt_base rec rdig lreq ldig true CON_KIND_EXECUTED
1155put rec CON_REC_DEC_ID did
1156rtype = get lreq 8
1157rfl = con_get16 lreq 148
1158gfl = con_rec_gate_flags rec
1159if ne get con_dec_human slot 0
1160gfl = or gfl CON_RF_HUMAN
1161end
1162if ne get con_dec_st slot CON_D_GRANTED
1163gfl = or gfl CON_RF_REFUSED_CALL
1164put rec CON_REC_FLAGS con_pack_flags con_get16 lreq 10 rfl gfl
1165put rec CON_REC_META con_pack_meta CON_KIND_EXECUTED CON_FAILED rtype
1166put rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY
1167ret CON_ERR_REFUSED
1168end
1169put con_dec_st slot CON_D_EXECUTED
1170if ne rtype CON_REVOKE_CAPABILITY
1171ret con_exec_non_revoke lreq rtype rfl gfl rec
1172end
1173ret con_exec_revoke lreq rtype rfl gfl rec
1174end