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_test.om

465 lines · 11992 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/capability/contain_test.om: Behavioral self-test for AEGIS Containment Gate.
3note Covers invariants I1..I5 from native/capability/tests/contain_test.c.
4note Compose with: si/directives/om2.om aienux/capability/lib.om aienux/capability/contain.om aienux/capability/contain_test.om om/tls/sha2.om
5
6text c_pass "PASS "
7text c_fail "FAIL "
8text c_nl "\n"
9text c_t01 "create_executor_and_duplicate_refused"
10text c_t02 "canonical_request_malformed_and_replay"
11text c_t03 "auto_grant_and_execute_leaf_revoke"
12text c_t04 "non_leaf_escalates_and_protected_denied"
13text c_t05 "human_resolve_secret_and_replay"
14text c_t06 "non_revoke_unavailable_and_budget_window"
15text c_t07 "cascade_scope_exceeded_makes_gate_inert"
16text c_t08 "kill_untrusts_lineage_and_escalates"
17text c_sum "CONTAIN_TEST_PASS total=8 failed=0\n"
18text c_fsum "CONTAIN_TEST_FAIL\n"
19
20fn creport name view uchar * ok bool to uint
21if ok
22w1 = cap_write_all 1 c_pass
23w2 = cap_write_all 1 name
24w3 = cap_write_all 1 c_nl
25if and w1 and w2 w3
26ret 0
27end
28ret 1
29end
30w1 = cap_write_all 1 c_fail
31w2 = cap_write_all 1 name
32w3 = cap_write_all 1 c_nl
33if and w1 and w2 w3
34ret 1
35end
36ret 1
37end
38
39fn mk_req buf edit uchar 152 rid ulong ctype uchar princ uint cid uint cgen ulong to void
40for i ulong 152
41put buf i 0
42end
43con_put64 buf 0 1
44put buf 8 ctype
45put buf 9 4
46con_put16 buf 10 100
47con_put32 buf 12 princ
48con_put32 buf 16 cid
49con_put64 buf 20 cgen
50con_put64 buf 92 rid
51con_put64 buf 100 rid
52con_put16 buf 148 0
53put buf 150 CON_REQ_VERSION
54put buf 151 0
55end
56
57fn setup_gate off edit CapRef to bool
58if ne cap_start CAP_OK
59ret false
60end
61cap_obs_clear
62cap_set_observer true
63if ne cap_office off CAP_OK
64ret false
65end
66contain_policy_reset
67contain_policy_set CON_REVOKE_CAPABILITY CON_GRANT
68if ne contain_create off CON_OK
69ret false
70end
71ret true
72end
73
74fn mint_ordinary off view CapRef subj uint rgt uint dst edit CapRef to bool
75m = new CapMint 1 subj 500 rgt 0 CAP_PARENT_NONE 0 get off cap_id get off generation
76ret eq cap_mint m dst CAP_OK
77end
78
79fn mint_child par view CapRef subj uint rgt uint dst edit CapRef to bool
80m = new CapMint 2 subj 500 rgt 0 get par cap_id get par generation get par cap_id get par generation
81ret eq cap_mint m dst CAP_OK
82end
83
84fn ct_01_create_and_dup to bool
85off = new CapRef 0 0
86if not setup_gate off
87ret false
88end
89ex = new CapRef 0 0
90if ne contain_executor ex CON_OK
91ret false
92end
93ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
94if ne cap_inspect ex ent CAP_OK
95ret false
96end
97if or ne get ent subject CON_SUBJ ne get ent rights CAP_RIGHT_REVOKE
98ret false
99end
100note Second create on same authority instance must fail with CON_ERR_EXISTS (I5.a)
101if ne contain_create off CON_ERR_EXISTS
102ret false
103end
104if contain_inert
105ret false
106end
107ret true
108end
109
110fn ct_02_malformed_and_replay to bool
111off = new CapRef 0 0
112if not setup_gate off
113ret false
114end
115c1 = new CapRef 0 0
116if not mint_ordinary off 10 CAP_RIGHT_READ c1
117ret false
118end
119rec = new ulong 16 0
120rdig = new uchar 32 0
121note Short buffer denied with WHY_BAD_REQUEST
122shortb = new uchar 16 0
123if ne contain_submit_bytes shortb rec rdig CON_OK
124ret false
125end
126if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_BAD_REQUEST
127ret false
128end
129note Good request succeeds, replay with same request_id is denied
130req = new uchar 152 0
131mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation
132if ne contain_submit_bytes req rec rdig CON_OK
133ret false
134end
135if ne con_rec_status rec CON_GRANT
136ret false
137end
138if ne contain_submit_bytes req rec rdig CON_OK
139ret false
140end
141if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_REPLAYED_REQUEST
142ret false
143end
144note Tampered digest denied with WHY_BAD_REQUEST
145mk_req req 2 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation
146bad_dig = new uchar 32 0xaa
147if ne contain_submit req bad_dig rec rdig CON_OK
148ret false
149end
150if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_BAD_REQUEST
151ret false
152end
153ret true
154end
155
156fn ct_03_auto_grant_and_execute to bool
157off = new CapRef 0 0
158if not setup_gate off
159ret false
160end
161c1 = new CapRef 0 0
162if not mint_ordinary off 10 CAP_RIGHT_READ c1
163ret false
164end
165req = new uchar 152 0
166mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation
167dig = new uchar 32 0
168con_req_digest req dig
169rec = new ulong 16 0
170rdig = new uchar 32 0
171if ne contain_submit req dig rec rdig CON_OK
172ret false
173end
174if or ne con_rec_status rec CON_GRANT ne get rec CON_REC_WHY ulong CON_WHY_POLICY_GRANT
175ret false
176end
177did = get rec CON_REC_DEC_ID
178if ne contain_execute did rec rdig CON_OK
179ret false
180end
181if or ne con_rec_status rec CON_DONE ne get rec CON_REC_RESOURCE 1
182ret false
183end
184note Capability is now revoked
185ent = new CapEntry 0 0 0 0 0 0 0 0 0 0 0 0 0
186if ne cap_validate c1 10 500 CAP_RIGHT_READ ent CAP_ERR_REVOKED
187ret false
188end
189note Re-executing same decision is refused
190if ne contain_execute did rec rdig CON_ERR_REFUSED
191ret false
192end
193if ne get rec CON_REC_WHY ulong CON_WHY_DECISION_REPLAY
194ret false
195end
196ret true
197end
198
199fn ct_04_non_leaf_and_protected to bool
200off = new CapRef 0 0
201if not setup_gate off
202ret false
203end
204par = new CapRef 0 0
205if not mint_ordinary off 10 or CAP_RIGHT_READ CAP_RIGHT_DELEGATE par
206ret false
207end
208ch = new CapRef 0 0
209if not mint_child par 11 CAP_RIGHT_READ ch
210ret false
211end
212req = new uchar 152 0
213rec = new ulong 16 0
214rdig = new uchar 32 0
215note Non-leaf target must ESCALATE with WHY_NOT_LEAF
216mk_req req 1 CON_REVOKE_CAPABILITY 10 get par cap_id get par generation
217if ne contain_submit_bytes req rec rdig CON_OK
218ret false
219end
220if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_NOT_LEAF
221ret false
222end
223note Office cap (protected target) must DENY with WHY_PROTECTED_TARGET
224mk_req req 2 CON_REVOKE_CAPABILITY 0 get off cap_id get off generation
225if ne contain_submit_bytes req rec rdig CON_OK
226ret false
227end
228if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_PROTECTED_TARGET
229ret false
230end
231note AEGIS subject (41) must DENY with WHY_PROTECTED_TARGET
232mk_req req 3 CON_REVOKE_CAPABILITY CON_SUBJ_AEGIS get ch cap_id get ch generation
233if ne contain_submit_bytes req rec rdig CON_OK
234ret false
235end
236if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_PROTECTED_TARGET
237ret false
238end
239note Subject mismatch must DENY with WHY_SUBJECT_MISMATCH
240mk_req req 4 CON_REVOKE_CAPABILITY 99 get ch cap_id get ch generation
241if ne contain_submit_bytes req rec rdig CON_OK
242ret false
243end
244if or ne con_rec_status rec CON_DENY ne get rec CON_REC_WHY ulong CON_WHY_SUBJECT_MISMATCH
245ret false
246end
247ret true
248end
249
250fn ct_05_human_resolve to bool
251off = new CapRef 0 0
252if not setup_gate off
253ret false
254end
255contain_policy_set CON_REVOKE_CAPABILITY CON_ESCALATE
256c1 = new CapRef 0 0
257if not mint_ordinary off 10 CAP_RIGHT_READ c1
258ret false
259end
260req = new uchar 152 0
261rec = new ulong 16 0
262rdig = new uchar 32 0
263mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation
264if ne contain_submit_bytes req rec rdig CON_OK
265ret false
266end
267if ne con_rec_status rec CON_ESCALATE
268ret false
269end
270did = get rec CON_REC_DEC_ID
271note Bad secret refused, decision stays escalated
272bad_sec = new uchar 32 0x55
273if ne contain_resolve did true bad_sec rec rdig CON_ERR_REFUSED
274ret false
275end
276if ne get rec CON_REC_WHY ulong CON_WHY_BAD_SECRET
277ret false
278end
279note Valid office secret approves
280good_sec = new uchar 32 0
281cap_copy_token good_sec
282if ne contain_resolve did true good_sec rec rdig CON_OK
283ret false
284end
285if or ne con_rec_status rec CON_GRANT ne get rec CON_REC_WHY ulong CON_WHY_HUMAN_GRANT
286ret false
287end
288note Second resolve on already-granted decision refused
289if ne contain_resolve did false good_sec rec rdig CON_ERR_REFUSED
290ret false
291end
292note Execute human-approved grant succeeds and carries RF_HUMAN
293if ne contain_execute did rec rdig CON_OK
294ret false
295end
296if or ne con_rec_status rec CON_DONE eq and con_rec_gate_flags rec CON_RF_HUMAN 0
297ret false
298end
299ret true
300end
301
302fn ct_06_non_revoke_and_budget to bool
303off = new CapRef 0 0
304if not setup_gate off
305ret false
306end
307contain_policy_set CON_FREEZE_PRINCIPAL CON_GRANT
308req = new uchar 152 0
309rec = new ulong 16 0
310rdig = new uchar 32 0
311note Non-revoke type cannot auto-grant (escalates with WHY_NOT_AUTO_ELIGIBLE)
312mk_req req 1 CON_FREEZE_PRINCIPAL 10 CON_CAP_NONE 0
313if ne contain_submit_bytes req rec rdig CON_OK
314ret false
315end
316if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_NOT_AUTO_ELIGIBLE
317ret false
318end
319did = get rec CON_REC_DEC_ID
320sec = new uchar 32 0
321cap_copy_token sec
322if ne contain_resolve did true sec rec rdig CON_OK
323ret false
324end
325note Executing non-revoke without seam returns UNAVAILABLE
326if ne contain_execute did rec rdig CON_OK
327ret false
328end
329if or ne con_rec_status rec CON_UNAVAILABLE ne get rec CON_REC_WHY ulong CON_WHY_NO_EXECUTOR
330ret false
331end
332note Exhaust auto-grant budget (8 grants per 64 decisions)
333rid = ulong 2
334c = new CapRef 0 0
335for i ulong 8
336if not mint_ordinary off 20 CAP_RIGHT_READ c
337ret false
338end
339mk_req req rid CON_REVOKE_CAPABILITY 20 get c cap_id get c generation
340if ne contain_submit_bytes req rec rdig CON_OK
341ret false
342end
343if ne con_rec_status rec CON_GRANT
344ret false
345end
346rid = add rid 1
347end
348note 9th grant in same window must ESCALATE with WHY_BUDGET
349c9 = new CapRef 0 0
350if not mint_ordinary off 20 CAP_RIGHT_READ c9
351ret false
352end
353mk_req req rid CON_REVOKE_CAPABILITY 20 get c9 cap_id get c9 generation
354if ne contain_submit_bytes req rec rdig CON_OK
355ret false
356end
357if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_BUDGET
358ret false
359end
360ret true
361end
362
363fn ct_07_cascade_makes_inert to bool
364off = new CapRef 0 0
365if not setup_gate off
366ret false
367end
368par = new CapRef 0 0
369if not mint_ordinary off 10 or CAP_RIGHT_READ CAP_RIGHT_DELEGATE par
370ret false
371end
372req = new uchar 152 0
373rec = new ulong 16 0
374rdig = new uchar 32 0
375note Grant while par is still a leaf
376mk_req req 1 CON_REVOKE_CAPABILITY 10 get par cap_id get par generation
377if ne contain_submit_bytes req rec rdig CON_OK
378ret false
379end
380if ne con_rec_status rec CON_GRANT
381ret false
382end
383did = get rec CON_REC_DEC_ID
384note Mint child with observer disabled so lineage shadow does not see it, forcing cascade on revoke!
385cap_set_observer false
386ch = new CapRef 0 0
387if not mint_child par 11 CAP_RIGHT_READ ch
388ret false
389end
390cap_set_observer true
391if ne contain_execute did rec rdig CON_OK
392ret false
393end
394if ne con_rec_status rec CON_FAILED_SCOPE
395ret false
396end
397if not contain_inert
398ret false
399end
400if eq and con_rec_gate_flags rec CON_RF_INERT 0
401ret false
402end
403note Subsequent revoke request must ESCALATE because gate is inert
404c2 = new CapRef 0 0
405if not mint_ordinary off 12 CAP_RIGHT_READ c2
406ret false
407end
408mk_req req 2 CON_REVOKE_CAPABILITY 12 get c2 cap_id get c2 generation
409if ne contain_submit_bytes req rec rdig CON_OK
410ret false
411end
412if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_LINEAGE_UNTRUSTED
413ret false
414end
415ret true
416end
417
418fn ct_08_kill_untrusts_lineage to bool
419off = new CapRef 0 0
420if not setup_gate off
421ret false
422end
423c1 = new CapRef 0 0
424if not mint_ordinary off 10 CAP_RIGHT_READ c1
425ret false
426end
427if ne cap_kill CAP_OK
428ret false
429end
430req = new uchar 152 0
431rec = new ulong 16 0
432rdig = new uchar 32 0
433mk_req req 1 CON_REVOKE_CAPABILITY 10 get c1 cap_id get c1 generation
434if ne contain_submit_bytes req rec rdig CON_OK
435ret false
436end
437if or ne con_rec_status rec CON_ESCALATE ne get rec CON_REC_WHY ulong CON_WHY_LINEAGE_UNTRUSTED
438ret false
439end
440ret true
441end
442
443fn main to int
444fails = uint 0
445fails = add fails creport c_t01 ct_01_create_and_dup
446fails = add fails creport c_t02 ct_02_malformed_and_replay
447fails = add fails creport c_t03 ct_03_auto_grant_and_execute
448fails = add fails creport c_t04 ct_04_non_leaf_and_protected
449fails = add fails creport c_t05 ct_05_human_resolve
450fails = add fails creport c_t06 ct_06_non_revoke_and_budget
451fails = add fails creport c_t07 ct_07_cascade_makes_inert
452fails = add fails creport c_t08 ct_08_kill_untrusts_lineage
453if eq fails 0
454w = cap_write_all 1 c_sum
455if w
456ret 0
457end
458ret 1
459end
460w = cap_write_all 1 c_fsum
461if w
462ret 1
463end
464ret 1
465end