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

934 lines · 20677 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/capability/lib.om: Pure-Omega AIENOS 64-bit Generation Capability Authority.
3note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux
4note native/capability/aienos_capability.{h,c} and crates/aienos-capability/src/lib.rs.
5note Compose with: si/directives/om2.om aienux/capability/lib.om <test.om|main.om>
6
7struct CapRef cap_id uint generation ulong end
8
9struct CapEntry cap_id uint generation ulong state uint issuer uint subject uint rights uint resource ulong epoch ulong lease_expiry ulong parent_id uint parent_generation ulong minted_by_id uint minted_by_generation ulong end
10
11struct CapMint issuer uint subject uint resource ulong rights uint lease_ticks ulong parent_id uint parent_generation ulong auth_id uint auth_generation ulong end
12
13struct CapObsEvent op uint rc int has_entry uint cap_id uint generation ulong state uint rights uint resource ulong parent_id uint end
14
15note Constants matching native/capability/aienos_capability.h
16const CAP_MAX uint 256
17const CAP_MAX_U ulong 256
18const CAP_MAX_DEPTH uint 8
19const CAP_TOKEN_LEN ulong 32
20const CAP_RES_AUTHORITY ulong 0
21const CAP_PARENT_NONE uint 0xffffffff
22const CAP_U32_MAX ulong 0xffffffff
23const CAP_U64_MAX ulong 0xffffffffffffffff
24const CAP_OBS_MAX ulong 512
25
26note Return / refusal codes
27const CAP_OK int 0
28const CAP_ERR_BOUNDS int -1
29const CAP_ERR_STALE_GEN int -2
30const CAP_ERR_REVOKED int -3
31const CAP_ERR_EPOCH int -4
32const CAP_ERR_SUBJECT int -5
33const CAP_ERR_RESOURCE int -6
34const CAP_ERR_RIGHTS int -7
35const CAP_ERR_EXPIRED int -8
36const CAP_ERR_CHAIN int -9
37const CAP_ERR_AMPLIFY int -10
38const CAP_ERR_NOT_DELEGABLE int -11
39const CAP_ERR_FULL int -12
40const CAP_ERR_IO int -13
41const CAP_ERR_STATE int -14
42const CAP_ERR_UNAUTHORIZED int -15
43const CAP_ERR_OVERFLOW int -16
44const CAP_ERR_EXHAUSTED int -17
45
46note Capability rights bitmask
47const CAP_RIGHT_READ uint 0x1
48const CAP_RIGHT_WRITE uint 0x2
49const CAP_RIGHT_EFFECT uint 0x4
50const CAP_RIGHT_DELEGATE uint 0x8
51const CAP_RIGHT_MINT uint 0x10
52const CAP_RIGHT_REVOKE uint 0x20
53const CAP_RIGHT_RECLAIM uint 0x40
54const CAP_RIGHT_EPOCH uint 0x80
55const CAP_RIGHT_CLOCK uint 0x100
56const CAP_RIGHT_PROMOTE uint 0x200
57const CAP_RIGHT_PRIVILEGED uint 0x3f0
58const CAP_RIGHT_KNOWN uint 0x3ff
59
60note Slot states
61const CAP_STATE_FREE uint 0
62const CAP_STATE_LIVE uint 1
63const CAP_STATE_REVOKED uint 2
64
65note Observer operation codes
66const CAP_OBS_MINT uint 1
67const CAP_OBS_REVOKE uint 2
68const CAP_OBS_RECLAIM uint 3
69const CAP_OBS_EPOCH uint 4
70const CAP_OBS_CLOCK uint 5
71const CAP_OBS_KILL uint 6
72const CAP_OBS_RESTART uint 7
73const CAP_OBS_VALIDATE_DENIED uint 8
74
75note Authority metadata indices in cap_meta
76const CAP_META_BOOT_GEN ulong 0
77const CAP_META_EPOCH ulong 1
78const CAP_META_CLOCK ulong 2
79const CAP_META_ALIVE ulong 3
80const CAP_META_LAST_BOOT ulong 4
81const CAP_META_OBS_ON ulong 5
82const CAP_META_OBS_CNT ulong 6
83const CAP_META_TOKEN_SEQ ulong 7
84
85note Authority state tables (256 slots)
86global cap_meta ulong 8
87global cap_gen ulong 256
88global cap_state uint 256
89global cap_issuer uint 256
90global cap_subject uint 256
91global cap_rights uint 256
92global cap_res ulong 256
93global cap_epoch ulong 256
94global cap_expiry ulong 256
95global cap_pid uint 256
96global cap_pgen ulong 256
97global cap_mbid uint 256
98global cap_mbgen ulong 256
99global cap_deliv uchar 256
100
101note Office token kept outside the capability table
102global cap_token uchar 32
103
104note Scratch array for cascade revoke slot IDs
105global cap_rev_ids uint 256
106
107note Observer event log (512 entries)
108global cap_obs_op uint 512
109global cap_obs_rc int 512
110global cap_obs_has uint 512
111global cap_obs_cid uint 512
112global cap_obs_gen ulong 512
113global cap_obs_st uint 512
114global cap_obs_rgt uint 512
115global cap_obs_res ulong 512
116global cap_obs_pid uint 512
117
118note ---- Checked arithmetic and resource width helpers ----
119
120fn cap_widen_resource id uint to ulong
121ret ulong id
122end
123
124fn cap_narrow_resource id ulong dst edit uint 1 to int
125if gt id CAP_U32_MAX
126ret CAP_ERR_RESOURCE
127end
128put dst 0 uint id
129ret CAP_OK
130end
131
132fn cap_generation_advance gen ulong dst edit ulong 1 to int
133if eq gen CAP_U64_MAX
134ret CAP_ERR_EXHAUSTED
135end
136put dst 0 add gen 1
137ret CAP_OK
138end
139
140fn cap_add_u64 a ulong b ulong dst edit ulong 1 to int
141if gt b sub CAP_U64_MAX a
142ret CAP_ERR_OVERFLOW
143end
144put dst 0 add a b
145ret CAP_OK
146end
147
148note ---- Internal table and observer helpers ----
149
150fn cap_slot_expired idx ulong to bool
151exp = get cap_expiry idx
152if eq exp 0
153ret false
154end
155clk = get cap_meta CAP_META_CLOCK
156ret ge clk exp
157end
158
159fn cap_load_entry idx ulong dst edit CapEntry to void
160put dst cap_id uint idx
161put dst generation get cap_gen idx
162put dst state get cap_state idx
163put dst issuer get cap_issuer idx
164put dst subject get cap_subject idx
165put dst rights get cap_rights idx
166put dst resource get cap_res idx
167put dst epoch get cap_epoch idx
168put dst lease_expiry get cap_expiry idx
169put dst parent_id get cap_pid idx
170put dst parent_generation get cap_pgen idx
171put dst minted_by_id get cap_mbid idx
172put dst minted_by_generation get cap_mbgen idx
173end
174
175fn cap_obs_emit_none op uint rc int to void
176if eq get cap_meta CAP_META_OBS_ON 0
177ret
178end
179cnt = get cap_meta CAP_META_OBS_CNT
180if ge cnt CAP_OBS_MAX
181ret
182end
183put cap_obs_op cnt op
184put cap_obs_rc cnt rc
185put cap_obs_has cnt 0
186put cap_obs_cid cnt 0
187put cap_obs_gen cnt 0
188put cap_obs_st cnt 0
189put cap_obs_rgt cnt 0
190put cap_obs_res cnt 0
191put cap_obs_pid cnt CAP_PARENT_NONE
192put cap_meta CAP_META_OBS_CNT add cnt 1
193end
194
195fn cap_obs_emit_slot op uint idx ulong rc int to void
196if eq get cap_meta CAP_META_OBS_ON 0
197ret
198end
199cnt = get cap_meta CAP_META_OBS_CNT
200if ge cnt CAP_OBS_MAX
201ret
202end
203put cap_obs_op cnt op
204put cap_obs_rc cnt rc
205put cap_obs_has cnt 1
206put cap_obs_cid cnt uint idx
207put cap_obs_gen cnt get cap_gen idx
208put cap_obs_st cnt get cap_state idx
209put cap_obs_rgt cnt get cap_rights idx
210put cap_obs_res cnt get cap_res idx
211put cap_obs_pid cnt get cap_pid idx
212put cap_meta CAP_META_OBS_CNT add cnt 1
213end
214
215fn cap_set_observer enabled bool to void
216if enabled
217put cap_meta CAP_META_OBS_ON 1
218else
219put cap_meta CAP_META_OBS_ON 0
220end
221end
222
223fn cap_obs_clear to void
224put cap_meta CAP_META_OBS_CNT 0
225end
226
227fn cap_obs_count to ulong
228ret get cap_meta CAP_META_OBS_CNT
229end
230
231fn cap_obs_get idx ulong dst edit CapObsEvent to bool
232cnt = get cap_meta CAP_META_OBS_CNT
233if ge idx cnt
234ret false
235end
236put dst op get cap_obs_op idx
237put dst rc get cap_obs_rc idx
238put dst has_entry get cap_obs_has idx
239put dst cap_id get cap_obs_cid idx
240put dst generation get cap_obs_gen idx
241put dst state get cap_obs_st idx
242put dst rights get cap_obs_rgt idx
243put dst resource get cap_obs_res idx
244put dst parent_id get cap_obs_pid idx
245ret true
246end
247
248fn cap_fill_token to void
249seq = add get cap_meta CAP_META_TOKEN_SEQ 1
250put cap_meta CAP_META_TOKEN_SEQ seq
251bgen = get cap_meta CAP_META_BOOT_GEN
252state = xor mul seq 0x9e3779b97f4a7c15 bgen
253for i ulong 32
254state = xor state shr state 12
255state = xor state shl state 25
256state = xor state shr state 27
257mix = mul state 0x2545f4914f6cdd1d
258put cap_token i uchar and shr mix 56 0xff
259end
260end
261
262fn cap_take_boot_gen floor ulong dst edit ulong 1 to int
263cur = get cap_meta CAP_META_LAST_BOOT
264if eq cur CAP_U64_MAX
265ret CAP_ERR_EXHAUSTED
266end
267nxt = add cur 1
268if gt floor nxt
269nxt = floor
270end
271if eq nxt CAP_U64_MAX
272ret CAP_ERR_EXHAUSTED
273end
274put cap_meta CAP_META_LAST_BOOT nxt
275put dst 0 nxt
276ret CAP_OK
277end
278
279fn cap_table_floor to ulong
280top = get cap_meta CAP_META_BOOT_GEN
281for i ulong 256
282g = get cap_gen i
283if gt g top
284top = g
285end
286pg = get cap_pgen i
287if gt pg top
288top = pg
289end
290mg = get cap_mbgen i
291if gt mg top
292top = mg
293end
294end
295if eq top CAP_U64_MAX
296ret CAP_U64_MAX
297end
298ret add top 1
299end
300
301fn cap_bootstrap boot_gen ulong to int
302if or eq boot_gen 0 eq boot_gen CAP_U64_MAX
303ret CAP_ERR_EXHAUSTED
304end
305put cap_meta CAP_META_BOOT_GEN boot_gen
306put cap_meta CAP_META_EPOCH 1
307put cap_meta CAP_META_CLOCK 0
308put cap_meta CAP_META_ALIVE 1
309for i ulong 256
310put cap_gen i boot_gen
311put cap_state i CAP_STATE_FREE
312put cap_issuer i 0
313put cap_subject i 0
314put cap_rights i 0
315put cap_res i 0
316put cap_epoch i 0
317put cap_expiry i 0
318put cap_pid i CAP_PARENT_NONE
319put cap_pgen i 0
320put cap_mbid i 0
321put cap_mbgen i 0
322put cap_deliv i 0
323end
324put cap_state 0 CAP_STATE_LIVE
325put cap_res 0 CAP_RES_AUTHORITY
326put cap_rights 0 CAP_RIGHT_PRIVILEGED
327put cap_epoch 0 1
328put cap_deliv 0 1
329ret CAP_OK
330end
331
332fn cap_chain_ok entry_id uint to int
333cur = entry_id
334depth = uint 0
335note At most CAP_MAX_DEPTH + 1 hops before returning CAP_ERR_CHAIN.
336while true bound 16
337idx = ulong cur
338pid = get cap_pid idx
339if eq pid CAP_PARENT_NONE
340ret CAP_OK
341end
342depth = add depth 1
343if gt depth CAP_MAX_DEPTH
344ret CAP_ERR_CHAIN
345end
346if ge pid CAP_MAX
347ret CAP_ERR_CHAIN
348end
349pidx = ulong pid
350if ne get cap_gen pidx get cap_pgen idx
351ret CAP_ERR_CHAIN
352end
353if ne get cap_state pidx CAP_STATE_LIVE
354ret CAP_ERR_CHAIN
355end
356cur = pid
357end
358ret CAP_ERR_CHAIN
359end
360
361fn cap_ancestor_hops entry_id uint to uint
362cur = entry_id
363hops = uint 0
364note Stops once parent is NONE or hops exceeds CAP_MAX_DEPTH.
365while true bound 16
366idx = ulong cur
367pid = get cap_pid idx
368if eq pid CAP_PARENT_NONE
369ret hops
370end
371if gt hops CAP_MAX_DEPTH
372ret hops
373end
374if ge pid CAP_MAX
375ret add CAP_MAX_DEPTH 1
376end
377hops = add hops 1
378cur = pid
379end
380ret add CAP_MAX_DEPTH 1
381end
382
383fn cap_auth_use auth_id uint auth_gen ulong need uint to int
384if ge auth_id CAP_MAX
385ret CAP_ERR_UNAUTHORIZED
386end
387idx = ulong auth_id
388if eq get cap_deliv idx 0
389ret CAP_ERR_UNAUTHORIZED
390end
391if ne get cap_gen idx auth_gen
392ret CAP_ERR_STALE_GEN
393end
394if ne get cap_state idx CAP_STATE_LIVE
395ret CAP_ERR_REVOKED
396end
397if ne get cap_epoch idx get cap_meta CAP_META_EPOCH
398ret CAP_ERR_EPOCH
399end
400if cap_slot_expired idx
401ret CAP_ERR_EXPIRED
402end
403rc = cap_chain_ok auth_id
404if ne rc CAP_OK
405ret rc
406end
407rgt = get cap_rights idx
408if ne and rgt need need
409ret CAP_ERR_UNAUTHORIZED
410end
411if ne and need CAP_RIGHT_PRIVILEGED 0
412if ne get cap_res idx CAP_RES_AUTHORITY
413ret CAP_ERR_RESOURCE
414end
415end
416ret CAP_OK
417end
418
419fn cap_lease_expiry ticks ulong dst edit ulong 1 to int
420if eq ticks 0
421put dst 0 0
422ret CAP_OK
423end
424clk = get cap_meta CAP_META_CLOCK
425ret cap_add_u64 clk ticks dst
426end
427
428note ---- Public Capability Authority API ----
429
430fn cap_start to int
431b = new ulong 1 0
432rc = cap_take_boot_gen 0 b
433if ne rc CAP_OK
434ret rc
435end
436rc = cap_bootstrap get b 0
437if ne rc CAP_OK
438ret rc
439end
440cap_fill_token
441ret CAP_OK
442end
443
444fn cap_office dst edit CapRef to int
445put dst cap_id 0
446put dst generation get cap_gen 0
447ret CAP_OK
448end
449
450fn cap_clock to ulong
451ret get cap_meta CAP_META_CLOCK
452end
453
454fn cap_epoch_now to ulong
455ret get cap_meta CAP_META_EPOCH
456end
457
458fn cap_copy_token dst edit uchar 32 to void
459for i ulong 32
460put dst i get cap_token i
461end
462end
463
464fn cap_authorize presented view uchar 32 to int
465diff = uchar 0
466for i ulong 32
467x = xor get cap_token i get presented i
468diff = or diff x
469end
470if eq diff 0
471ret CAP_OK
472end
473ret CAP_ERR_UNAUTHORIZED
474end
475
476fn cap_inspect cap view CapRef dst edit CapEntry to int
477cid = get cap cap_id
478if ge cid CAP_MAX
479ret CAP_ERR_BOUNDS
480end
481idx = ulong cid
482if ne get cap_gen idx get cap generation
483ret CAP_ERR_STALE_GEN
484end
485if eq get cap_state idx CAP_STATE_FREE
486ret CAP_ERR_STATE
487end
488cap_load_entry idx dst
489ret CAP_OK
490end
491
492fn cap_validate_chain cap view CapRef to int
493cid = get cap cap_id
494if ge cid CAP_MAX
495ret CAP_ERR_BOUNDS
496end
497chain = new uint 9 0
498n = uint 0
499cur = cid
500note Walk up to CAP_MAX_DEPTH + 1 slots; cycle or depth > 8 returns CAP_ERR_CHAIN.
501while ne cur CAP_PARENT_NONE bound 16
502if gt n CAP_MAX_DEPTH
503ret CAP_ERR_CHAIN
504end
505if ge cur CAP_MAX
506ret CAP_ERR_CHAIN
507end
508put chain ulong n cur
509cur = get cap_pid ulong cur
510n = add n 1
511end
512eidx = ulong get chain 0
513if ne get cap_gen eidx get cap generation
514ret CAP_ERR_STALE_GEN
515end
516if ne get cap_state eidx CAP_STATE_LIVE
517ret CAP_ERR_REVOKED
518end
519cur_ep = get cap_meta CAP_META_EPOCH
520if ne get cap_epoch eidx cur_ep
521ret CAP_ERR_EPOCH
522end
523if cap_slot_expired eidx
524ret CAP_ERR_EXPIRED
525end
526i = uint 1
527while lt i n bound 16
528pidx = ulong get chain ulong i
529cidx = ulong get chain ulong sub i 1
530if ne get cap_gen pidx get cap_pgen cidx
531ret CAP_ERR_CHAIN
532end
533if ne get cap_state pidx CAP_STATE_LIVE
534ret CAP_ERR_CHAIN
535end
536if ne get cap_epoch pidx cur_ep
537ret CAP_ERR_CHAIN
538end
539if cap_slot_expired pidx
540ret CAP_ERR_CHAIN
541end
542i = add i 1
543end
544ret CAP_OK
545end
546
547fn cap_validate cap view CapRef subj uint res ulong req_rights uint dst edit CapEntry to int
548rc = cap_validate_chain cap
549if ne rc CAP_OK
550ret rc
551end
552eidx = ulong get cap cap_id
553if ne get cap_subject eidx subj
554ret CAP_ERR_SUBJECT
555end
556if ne get cap_res eidx res
557ret CAP_ERR_RESOURCE
558end
559if ne and get cap_rights eidx req_rights req_rights
560ret CAP_ERR_RIGHTS
561end
562cap_load_entry eidx dst
563ret CAP_OK
564end
565
566fn cap_mint_check_root req view CapMint to int
567aid = get req auth_id
568agen = get req auth_generation
569rc = cap_auth_use aid agen CAP_RIGHT_MINT
570if ne rc CAP_OK
571ret rc
572end
573arights = get cap_rights ulong aid
574req_priv = and get req rights CAP_RIGHT_PRIVILEGED
575missing = and req_priv xor arights 0xffffffff
576if ne missing 0
577ret CAP_ERR_UNAUTHORIZED
578end
579ret CAP_OK
580end
581
582fn cap_mint_check_deleg req view CapMint to int
583cexp = new ulong 1 0
584aid = get req auth_id
585agen = get req auth_generation
586pid = get req parent_id
587pgen = get req parent_generation
588if ne aid pid
589ret CAP_ERR_UNAUTHORIZED
590end
591if ne agen pgen
592ret CAP_ERR_UNAUTHORIZED
593end
594rc = cap_auth_use aid agen 0
595if ne rc CAP_OK
596ret rc
597end
598aidx = ulong aid
599arights = get cap_rights aidx
600if eq and arights CAP_RIGHT_DELEGATE 0
601ret CAP_ERR_NOT_DELEGABLE
602end
603if ge pid CAP_MAX
604ret CAP_ERR_BOUNDS
605end
606if ne get cap_res aidx get req resource
607ret CAP_ERR_RESOURCE
608end
609rgt = get req rights
610if ne and rgt xor arights 0xffffffff 0
611ret CAP_ERR_AMPLIFY
612end
613if ne and rgt CAP_RIGHT_PRIVILEGED 0
614ret CAP_ERR_NOT_DELEGABLE
615end
616if ge cap_ancestor_hops pid CAP_MAX_DEPTH
617ret CAP_ERR_CHAIN
618end
619aexp = get cap_expiry aidx
620if ne aexp 0
621rc = cap_lease_expiry get req lease_ticks cexp
622if ne rc CAP_OK
623ret rc
624end
625cval = get cexp 0
626if or eq cval 0 gt cval aexp
627ret CAP_ERR_AMPLIFY
628end
629end
630ret CAP_OK
631end
632
633fn cap_mint_alloc req view CapMint expiry ulong dst edit CapRef to int
634aid = get req auth_id
635aidx = ulong aid
636amb_id = aid
637amb_gen = get cap_gen aidx
638pid = get req parent_id
639pgen = ulong 0
640if ne pid CAP_PARENT_NONE
641pgen = get req parent_generation
642end
643saw_exh = false
644for i ulong 256
645if eq get cap_state i CAP_STATE_FREE
646g = get cap_gen i
647if eq g CAP_U64_MAX
648saw_exh = true
649else
650put cap_state i CAP_STATE_LIVE
651put cap_issuer i get req issuer
652put cap_subject i get req subject
653put cap_res i get req resource
654put cap_rights i get req rights
655put cap_epoch i get cap_meta CAP_META_EPOCH
656put cap_expiry i expiry
657put cap_pid i pid
658put cap_pgen i pgen
659put cap_mbid i amb_id
660put cap_mbgen i amb_gen
661put cap_deliv i 1
662put dst cap_id uint i
663put dst generation g
664ret CAP_OK
665end
666end
667end
668if saw_exh
669ret CAP_ERR_EXHAUSTED
670end
671ret CAP_ERR_FULL
672end
673
674fn cap_mint req view CapMint dst edit CapRef to int
675if eq get cap_meta CAP_META_ALIVE 0
676cap_obs_emit_none CAP_OBS_MINT CAP_ERR_IO
677ret CAP_ERR_IO
678end
679rgt = get req rights
680inv_known = xor CAP_RIGHT_KNOWN 0xffffffff
681if or eq rgt 0 ne and rgt inv_known 0
682cap_obs_emit_none CAP_OBS_MINT CAP_ERR_RIGHTS
683ret CAP_ERR_RIGHTS
684end
685if and ne and rgt CAP_RIGHT_PRIVILEGED 0 ne and rgt CAP_RIGHT_DELEGATE 0
686cap_obs_emit_none CAP_OBS_MINT CAP_ERR_NOT_DELEGABLE
687ret CAP_ERR_NOT_DELEGABLE
688end
689rc = int 0
690if eq get req parent_id CAP_PARENT_NONE
691rc = cap_mint_check_root req
692else
693rc = cap_mint_check_deleg req
694end
695if ne rc CAP_OK
696cap_obs_emit_none CAP_OBS_MINT rc
697ret rc
698end
699exp = new ulong 1 0
700rc = cap_lease_expiry get req lease_ticks exp
701if ne rc CAP_OK
702cap_obs_emit_none CAP_OBS_MINT rc
703ret rc
704end
705rc = cap_mint_alloc req get exp 0 dst
706if ne rc CAP_OK
707cap_obs_emit_none CAP_OBS_MINT rc
708ret rc
709end
710cap_obs_emit_slot CAP_OBS_MINT ulong get dst cap_id CAP_OK
711ret CAP_OK
712end
713
714fn cap_cascade_revoke start_n uint to uint
715n = start_n
716for guard uint 256
717changed = false
718for i ulong 256
719if eq get cap_state i CAP_STATE_LIVE
720pid = get cap_pid i
721if ne pid CAP_PARENT_NONE
722if lt pid CAP_MAX
723pidx = ulong pid
724if and eq get cap_gen pidx get cap_pgen i eq get cap_state pidx CAP_STATE_REVOKED
725put cap_state i CAP_STATE_REVOKED
726if lt n CAP_MAX
727put cap_rev_ids ulong n uint i
728n = add n 1
729end
730changed = true
731end
732end
733end
734end
735end
736if not changed
737break
738end
739end
740ret n
741end
742
743fn cap_revoke auth view CapRef target view CapRef to int
744if eq get cap_meta CAP_META_ALIVE 0
745cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_IO
746ret CAP_ERR_IO
747end
748rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_REVOKE
749if ne rc CAP_OK
750cap_obs_emit_none CAP_OBS_REVOKE rc
751ret rc
752end
753tid = get target cap_id
754if ge tid CAP_MAX
755cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_BOUNDS
756ret CAP_ERR_BOUNDS
757end
758tidx = ulong tid
759if ne get cap_gen tidx get target generation
760cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_STALE_GEN
761ret CAP_ERR_STALE_GEN
762end
763if ne get cap_state tidx CAP_STATE_LIVE
764cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_STATE
765ret CAP_ERR_STATE
766end
767arights = get cap_rights ulong get auth cap_id
768tpriv = and get cap_rights tidx CAP_RIGHT_PRIVILEGED
769if ne and tpriv xor arights 0xffffffff 0
770cap_obs_emit_none CAP_OBS_REVOKE CAP_ERR_UNAUTHORIZED
771ret CAP_ERR_UNAUTHORIZED
772end
773put cap_state tidx CAP_STATE_REVOKED
774put cap_rev_ids 0 tid
775n = cap_cascade_revoke 1
776k = uint 0
777note At most CAP_MAX slots can be revoked in one cascade.
778while lt k n bound 256
779sid = get cap_rev_ids ulong k
780cap_obs_emit_slot CAP_OBS_REVOKE ulong sid CAP_OK
781k = add k 1
782end
783ret CAP_OK
784end
785
786fn cap_reclaim auth view CapRef cid uint to int
787if eq get cap_meta CAP_META_ALIVE 0
788cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_IO
789ret CAP_ERR_IO
790end
791rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_RECLAIM
792if ne rc CAP_OK
793cap_obs_emit_none CAP_OBS_RECLAIM rc
794ret rc
795end
796if ge cid CAP_MAX
797cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_BOUNDS
798ret CAP_ERR_BOUNDS
799end
800idx = ulong cid
801if ne get cap_state idx CAP_STATE_REVOKED
802cap_obs_emit_none CAP_OBS_RECLAIM CAP_ERR_STATE
803ret CAP_ERR_STATE
804end
805nxt = new ulong 1 0
806rc = cap_generation_advance get cap_gen idx nxt
807if ne rc CAP_OK
808cap_obs_emit_none CAP_OBS_RECLAIM rc
809ret rc
810end
811put cap_gen idx get nxt 0
812put cap_state idx CAP_STATE_FREE
813put cap_rights idx 0
814put cap_expiry idx 0
815put cap_pid idx CAP_PARENT_NONE
816put cap_pgen idx 0
817put cap_deliv idx 0
818cap_obs_emit_slot CAP_OBS_RECLAIM idx CAP_OK
819ret CAP_OK
820end
821
822fn cap_advance_clock auth view CapRef ticks ulong to int
823if eq get cap_meta CAP_META_ALIVE 0
824cap_obs_emit_none CAP_OBS_CLOCK CAP_ERR_IO
825ret CAP_ERR_IO
826end
827rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_CLOCK
828if ne rc CAP_OK
829cap_obs_emit_none CAP_OBS_CLOCK rc
830ret rc
831end
832nxt = new ulong 1 0
833rc = cap_add_u64 get cap_meta CAP_META_CLOCK ticks nxt
834if ne rc CAP_OK
835cap_obs_emit_none CAP_OBS_CLOCK rc
836ret rc
837end
838put cap_meta CAP_META_CLOCK get nxt 0
839cap_obs_emit_slot CAP_OBS_CLOCK ulong get auth cap_id CAP_OK
840ret CAP_OK
841end
842
843fn cap_bump_epoch auth view CapRef to int
844if eq get cap_meta CAP_META_ALIVE 0
845cap_obs_emit_none CAP_OBS_EPOCH CAP_ERR_IO
846ret CAP_ERR_IO
847end
848rc = cap_auth_use get auth cap_id get auth generation CAP_RIGHT_EPOCH
849if ne rc CAP_OK
850cap_obs_emit_none CAP_OBS_EPOCH rc
851ret rc
852end
853nxt = new ulong 1 0
854rc = cap_add_u64 get cap_meta CAP_META_EPOCH 1 nxt
855if ne rc CAP_OK
856cap_obs_emit_none CAP_OBS_EPOCH rc
857ret rc
858end
859put cap_meta CAP_META_EPOCH get nxt 0
860cap_obs_emit_slot CAP_OBS_EPOCH ulong get auth cap_id CAP_OK
861ret CAP_OK
862end
863
864fn cap_kill to int
865put cap_meta CAP_META_ALIVE 0
866cap_obs_emit_none CAP_OBS_KILL CAP_OK
867ret CAP_OK
868end
869
870fn cap_restart to int
871fl = cap_table_floor
872b = new ulong 1 0
873rc = cap_take_boot_gen fl b
874if ne rc CAP_OK
875cap_obs_emit_none CAP_OBS_RESTART rc
876ret rc
877end
878rc = cap_bootstrap get b 0
879if ne rc CAP_OK
880cap_obs_emit_none CAP_OBS_RESTART rc
881ret rc
882end
883cap_fill_token
884cap_obs_emit_slot CAP_OBS_RESTART 0 CAP_OK
885ret CAP_OK
886end
887
888fn cap_cognition_mint req view CapMint tok view uchar 32 to int
889ret CAP_ERR_UNAUTHORIZED
890end
891
892fn cap_cognition_admin op uint auth view CapRef target view CapRef to int
893ret CAP_ERR_UNAUTHORIZED
894end
895
896fn cap_force_generation cid uint gen ulong to int
897if ge cid CAP_MAX
898ret CAP_ERR_BOUNDS
899end
900idx = ulong cid
901if eq get cap_state idx CAP_STATE_LIVE
902ret CAP_ERR_STATE
903end
904if eq gen 0
905ret CAP_ERR_EXHAUSTED
906end
907put cap_gen idx gen
908ret CAP_OK
909end
910
911note Test seam for corrupt-table negative control (simulates non-live parent with live child)
912fn cap_corrupt_slot_state cid uint st uint to int
913if ge cid CAP_MAX
914ret CAP_ERR_BOUNDS
915end
916put cap_state ulong cid st
917ret CAP_OK
918end
919
920note ---- Small I/O helpers for test drivers ----
921
922fn cap_write_all fd int buf view uchar * to bool
923total = len buf
924off = ulong 0
925note Each write advances off by at least 1 byte on progress; 4096 covers small test lines.
926while lt off total bound 4096
927n = write fd buf off sub total off
928if le n 0
929ret false
930end
931off = add off ulong n
932end
933ret true
934end