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 / seed / authority / cap_table_om2.om

536 lines · 13739 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: AGPL-3.0-only
3note Pure seeded OM-2 256-slot capability authority table (ADR 0014 / ADR 0016).
4note Replaces the C capability table state machine in src/runtime/rx_caproot.c
5note and the external libaienos_capability.a (aienos.lock) dependency.
6note
7note Table layout:
8note hdr[4]: 0=capacity (256), 1=epoch, 2=clock, 3=killed (0/1)
9note slots[256 * 13 = 3328]: per slot (stride 13):
10note 0=cap_id, 1=generation, 2=state (0=FREE,1=LIVE,2=REVOKED),
11note 3=issuer, 4=subject, 5=rights, 6=resource, 7=epoch,
12note 8=lease_expiry, 9=parent_id (0xffffffff=none), 10=parent_gen,
13note 11=minted_by_id, 12=minted_by_gen
14
15const CAP_MAX ulong 256
16const CAP_MAX_DEPTH ulong 8
17const CAP_STRIDE ulong 13
18const CAP_NONE ulong 0xffffffff
19const CAP_U64_MAX ulong 0xffffffffffffffff
20
21const STATE_FREE ulong 0
22const STATE_LIVE ulong 1
23const STATE_REVOKED ulong 2
24
25const RIGHT_DELEGATE ulong 0x8
26const RIGHT_MINT ulong 0x10
27const RIGHT_REVOKE ulong 0x20
28const RIGHT_RECLAIM ulong 0x40
29const RIGHT_EPOCH ulong 0x80
30const RIGHT_CLOCK ulong 0x100
31const RIGHT_PRIVILEGED ulong 0x3f0
32const RIGHT_KNOWN ulong 0x3ff
33
34const ERR_OK long 0
35const ERR_BOUNDS long -1
36const ERR_STALE_GEN long -2
37const ERR_REVOKED long -3
38const ERR_EPOCH long -4
39const ERR_SUBJECT long -5
40const ERR_RESOURCE long -6
41const ERR_RIGHTS long -7
42const ERR_EXPIRED long -8
43const ERR_CHAIN long -9
44const ERR_AMPLIFY long -10
45const ERR_NOT_DELEGABLE long -11
46const ERR_FULL long -12
47const ERR_IO long -13
48const ERR_STATE long -14
49const ERR_UNAUTHORIZED long -15
50const ERR_OVERFLOW long -16
51const ERR_EXHAUSTED long -17
52
53note Constant-time 32-byte office token comparison. Returns 1 if equal, 0 else.
54export fn cap_token_eq a view uchar 32 b view uchar 32 to ulong
55 diff = ulong 0
56 for i ulong 32
57 x = ulong get a i
58 y = ulong get b i
59 diff = or diff xor x y
60 end
61 if eq diff 0
62 ret 1
63 end
64 ret 0
65end
66
67note Overflow-checked u64 addition. Returns 0 on ok, -16 on overflow.
68export fn cap_add_u64 a ulong b ulong dst edit ulong 1 to long
69 if gt b sub CAP_U64_MAX a
70 ret ERR_OVERFLOW
71 end
72 put dst 0 add a b
73 ret ERR_OK
74end
75
76note Generation advance; fails closed at UINT64_MAX (-17).
77export fn cap_gen_advance gen ulong dst edit ulong 1 to long
78 if eq gen CAP_U64_MAX
79 ret ERR_EXHAUSTED
80 end
81 put dst 0 add gen 1
82 ret ERR_OK
83end
84
85fn slot_off id ulong to ulong
86 ret mul id CAP_STRIDE
87end
88
89fn chain_ok hdr view ulong 4 slots view ulong 3328 start_id ulong to long
90 cap = get hdr 0
91 epoch = get hdr 1
92 clock = get hdr 2
93 cur = start_id
94 for depth ulong 9
95 base = slot_off cur
96 pid = get slots add base 9
97 if eq pid CAP_NONE
98 ret ERR_OK
99 end
100 if ge depth CAP_MAX_DEPTH
101 ret ERR_CHAIN
102 end
103 if ge pid cap
104 ret ERR_CHAIN
105 end
106 pgen = get slots add base 10
107 pbase = slot_off pid
108 if ne get slots add pbase 1 pgen
109 ret ERR_CHAIN
110 end
111 if ne get slots add pbase 2 STATE_LIVE
112 ret ERR_CHAIN
113 end
114 if ne get slots add pbase 7 epoch
115 ret ERR_CHAIN
116 end
117 pexp = get slots add pbase 8
118 if ne pexp 0
119 if ge clock pexp
120 ret ERR_CHAIN
121 end
122 end
123 cur = pid
124 end
125 ret ERR_CHAIN
126end
127
128fn ancestor_hops hdr view ulong 4 slots view ulong 3328 start_id ulong to ulong
129 cap = get hdr 0
130 cur = start_id
131 hops = ulong 0
132 for step ulong 9
133 base = slot_off cur
134 pid = get slots add base 9
135 if eq pid CAP_NONE
136 ret hops
137 end
138 if ge pid cap
139 ret add CAP_MAX_DEPTH 1
140 end
141 hops = add hops 1
142 cur = pid
143 end
144 ret add CAP_MAX_DEPTH 1
145end
146
147fn auth_check hdr view ulong 4 slots view ulong 3328 delivered view uchar 256 aid ulong agen ulong need ulong to long
148 if ne get hdr 3 0
149 ret ERR_IO
150 end
151 cap = get hdr 0
152 if ge aid cap
153 ret ERR_UNAUTHORIZED
154 end
155 if eq get delivered aid uchar 0
156 ret ERR_UNAUTHORIZED
157 end
158 base = slot_off aid
159 if ne get slots add base 1 agen
160 ret ERR_STALE_GEN
161 end
162 if ne get slots add base 2 STATE_LIVE
163 ret ERR_REVOKED
164 end
165 if ne get slots add base 7 get hdr 1
166 ret ERR_EPOCH
167 end
168 exp = get slots add base 8
169 if ne exp 0
170 if ge get hdr 2 exp
171 ret ERR_EXPIRED
172 end
173 end
174 if ne chain_ok hdr slots aid ERR_OK
175 ret ERR_CHAIN
176 end
177 rights = get slots add base 5
178 if ne and rights need need
179 ret ERR_UNAUTHORIZED
180 end
181 if ne and need RIGHT_PRIVILEGED 0
182 if ne get slots add base 6 0
183 ret ERR_RESOURCE
184 end
185 end
186 ret ERR_OK
187end
188
189note Initialize capability table and bootstrap office at slot 0.
190export fn cap_table_init hdr edit ulong 4 slots edit ulong 3328 delivered edit uchar 256 boot_gen ulong to long
191 if or eq boot_gen 0 eq boot_gen CAP_U64_MAX
192 ret ERR_EXHAUSTED
193 end
194 put hdr 0 CAP_MAX
195 put hdr 1 1
196 put hdr 2 0
197 put hdr 3 0
198 for i ulong 256
199 put delivered i uchar 0
200 base = slot_off i
201 put slots base i
202 put slots add base 1 boot_gen
203 put slots add base 2 STATE_FREE
204 put slots add base 3 0
205 put slots add base 4 0
206 put slots add base 5 0
207 put slots add base 6 0
208 put slots add base 7 0
209 put slots add base 8 0
210 put slots add base 9 CAP_NONE
211 put slots add base 10 0
212 put slots add base 11 0
213 put slots add base 12 0
214 end
215 put slots 2 STATE_LIVE
216 put slots 5 RIGHT_PRIVILEGED
217 put slots 7 1
218 put delivered 0 uchar 1
219 ret ERR_OK
220end
221
222note Validate cap reference against (subject, resource, rights).
223note args[5]: 0=cap_id, 1=gen, 2=subject, 3=resource, 4=rights.
224export fn cap_table_validate hdr view ulong 4 slots view ulong 3328 args view ulong 5 out_slot edit ulong 13 to long
225 if ne get hdr 3 0
226 ret ERR_IO
227 end
228 id = get args 0
229 if ge id get hdr 0
230 ret ERR_BOUNDS
231 end
232 base = slot_off id
233 if ne get slots add base 1 get args 1
234 ret ERR_STALE_GEN
235 end
236 if ne get slots add base 2 STATE_LIVE
237 ret ERR_REVOKED
238 end
239 if ne get slots add base 7 get hdr 1
240 ret ERR_EPOCH
241 end
242 exp = get slots add base 8
243 if ne exp 0
244 if ge get hdr 2 exp
245 ret ERR_EXPIRED
246 end
247 end
248 if ne chain_ok hdr slots id ERR_OK
249 ret ERR_CHAIN
250 end
251 if ne get slots add base 4 get args 2
252 ret ERR_SUBJECT
253 end
254 if ne get slots add base 6 get args 3
255 ret ERR_RESOURCE
256 end
257 need = get args 4
258 if ne and get slots add base 5 need need
259 ret ERR_RIGHTS
260 end
261 for k ulong 13
262 put out_slot k get slots add base k
263 end
264 ret ERR_OK
265end
266
267note Inspect a table entry if generation matches and slot is not FREE.
268export fn cap_table_inspect hdr view ulong 4 slots view ulong 3328 cap_id ulong gen ulong out_slot edit ulong 13 to long
269 if ne get hdr 3 0
270 ret ERR_IO
271 end
272 if ge cap_id get hdr 0
273 ret ERR_BOUNDS
274 end
275 base = slot_off cap_id
276 if ne get slots add base 1 gen
277 ret ERR_STALE_GEN
278 end
279 if eq get slots add base 2 STATE_FREE
280 ret ERR_STATE
281 end
282 for k ulong 13
283 put out_slot k get slots add base k
284 end
285 ret ERR_OK
286end
287
288note Check authority reference possession and rights (used for shutdown etc.).
289note args[3]: 0=auth_id, 1=auth_gen, 2=need_rights.
290export fn cap_table_auth_use hdr view ulong 4 slots view ulong 3328 delivered view uchar 256 args view ulong 3 to long
291 ret auth_check hdr slots delivered get args 0 get args 1 get args 2
292end
293
294fn mint_check_auth hdr view ulong 4 slots view ulong 3328 delivered view uchar 256 req view ulong 9 to long
295 rights = get req 3
296 if or eq rights 0 ne and rights xor RIGHT_KNOWN CAP_U64_MAX 0
297 ret ERR_RIGHTS
298 end
299 if and ne and rights RIGHT_PRIVILEGED 0 ne and rights RIGHT_DELEGATE 0
300 ret ERR_NOT_DELEGABLE
301 end
302 pid = get req 5
303 aid = get req 7
304 agen = get req 8
305 if eq pid CAP_NONE
306 arc = auth_check hdr slots delivered aid agen RIGHT_MINT
307 if ne arc ERR_OK
308 ret arc
309 end
310 arights = get slots add slot_off aid 5
311 if ne and and rights RIGHT_PRIVILEGED xor arights CAP_U64_MAX 0
312 ret ERR_UNAUTHORIZED
313 end
314 ret ERR_OK
315 end
316 if or ne aid pid ne agen get req 6
317 ret ERR_UNAUTHORIZED
318 end
319 arc = auth_check hdr slots delivered aid agen 0
320 if ne arc ERR_OK
321 ret arc
322 end
323 pbase = slot_off pid
324 prights = get slots add pbase 5
325 if eq and prights RIGHT_DELEGATE 0
326 ret ERR_NOT_DELEGABLE
327 end
328 if ne get slots add pbase 6 get req 2
329 ret ERR_RESOURCE
330 end
331 if ne and rights xor prights CAP_U64_MAX 0
332 ret ERR_AMPLIFY
333 end
334 if ne and rights RIGHT_PRIVILEGED 0
335 ret ERR_NOT_DELEGABLE
336 end
337 if ge ancestor_hops hdr slots pid CAP_MAX_DEPTH
338 ret ERR_CHAIN
339 end
340 pexp = get slots add pbase 8
341 if ne pexp 0
342 lticks = get req 4
343 if eq lticks 0
344 ret ERR_AMPLIFY
345 end
346 clk = get hdr 2
347 if gt lticks sub CAP_U64_MAX clk
348 ret ERR_OVERFLOW
349 end
350 if gt add clk lticks pexp
351 ret ERR_AMPLIFY
352 end
353 end
354 ret ERR_OK
355end
356
357fn mint_fill_slot slots edit ulong 3328 req view ulong 9 idx ulong epoch ulong exp ulong to void
358 base = slot_off idx
359 pid = get req 5
360 pgen = ulong 0
361 if ne pid CAP_NONE
362 pgen = get req 6
363 end
364 put slots add base 2 STATE_LIVE
365 put slots add base 3 get req 0
366 put slots add base 4 get req 1
367 put slots add base 5 get req 3
368 put slots add base 6 get req 2
369 put slots add base 7 epoch
370 put slots add base 8 exp
371 put slots add base 9 pid
372 put slots add base 10 pgen
373 put slots add base 11 get req 7
374 put slots add base 12 get req 8
375end
376
377note Mint a root-issued or delegated capability.
378note req[9]: 0=issuer, 1=subject, 2=resource, 3=rights, 4=lease_ticks,
379note 5=parent_id, 6=parent_gen, 7=auth_id, 8=auth_gen.
380export fn cap_table_mint hdr edit ulong 4 slots edit ulong 3328 delivered edit uchar 256 req view ulong 9 out_ref edit ulong 2 to long
381 arc = mint_check_auth hdr slots delivered req
382 if ne arc ERR_OK
383 ret arc
384 end
385 exp = ulong 0
386 if ne get req 4 0
387 if gt get req 4 sub CAP_U64_MAX get hdr 2
388 ret ERR_OVERFLOW
389 end
390 exp = add get hdr 2 get req 4
391 end
392 saw_exh = ulong 0
393 for i ulong get hdr 0 bound 256
394 base = slot_off i
395 if eq get slots add base 2 STATE_FREE
396 gen = get slots add base 1
397 if eq gen CAP_U64_MAX
398 saw_exh = ulong 1
399 else
400 mint_fill_slot slots req i get hdr 1 exp
401 put delivered i uchar 1
402 put out_ref 0 i
403 put out_ref 1 gen
404 ret ERR_OK
405 end
406 end
407 end
408 if ne saw_exh 0
409 ret ERR_EXHAUSTED
410 end
411 ret ERR_FULL
412end
413
414fn cascade_revoke_step slots edit ulong 3328 cap ulong rev_list edit ulong 257 to ulong
415 changed = ulong 0
416 for i ulong cap bound 256
417 base = slot_off i
418 if eq get slots add base 2 STATE_LIVE
419 pid = get slots add base 9
420 if ne pid CAP_NONE
421 if lt pid cap
422 pbase = slot_off pid
423 pgen = get slots add pbase 1
424 if eq pgen get slots add base 10
425 if eq get slots add pbase 2 STATE_REVOKED
426 put slots add base 2 STATE_REVOKED
427 cnt = add get rev_list 0 1
428 put rev_list 0 cnt
429 if le cnt 256
430 put rev_list cnt i
431 end
432 changed = ulong 1
433 end
434 end
435 end
436 end
437 end
438 end
439 ret changed
440end
441
442note Revoke target capability and cascade to all live descendants.
443note args[4]: 0=auth_id, 1=auth_gen, 2=target_id, 3=target_gen.
444note rev_list[257]: rev_list[0]=count, rev_list[1..count]=revoked slot ids.
445export fn cap_table_revoke hdr edit ulong 4 slots edit ulong 3328 delivered view uchar 256 args view ulong 4 rev_list edit ulong 257 to long
446 put rev_list 0 0
447 aid = get args 0
448 arc = auth_check hdr slots delivered aid get args 1 RIGHT_REVOKE
449 if ne arc ERR_OK
450 ret arc
451 end
452 tid = get args 2
453 cap = get hdr 0
454 if ge tid cap
455 ret ERR_BOUNDS
456 end
457 tbase = slot_off tid
458 if ne get slots add tbase 1 get args 3
459 ret ERR_STALE_GEN
460 end
461 if ne get slots add tbase 2 STATE_LIVE
462 ret ERR_STATE
463 end
464 tr = and get slots add tbase 5 RIGHT_PRIVILEGED
465 ar = xor get slots add slot_off aid 5 CAP_U64_MAX
466 if ne and tr ar 0
467 ret ERR_UNAUTHORIZED
468 end
469 put slots add tbase 2 STATE_REVOKED
470 put rev_list 0 1
471 put rev_list 1 tid
472 for pass ulong 256
473 if eq cascade_revoke_step slots cap rev_list 0
474 break
475 end
476 end
477 ret ERR_OK
478end
479
480note Reclaim a revoked slot back to FREE, advancing its generation.
481note args[3]: 0=auth_id, 1=auth_gen, 2=cap_id.
482export fn cap_table_reclaim hdr edit ulong 4 slots edit ulong 3328 delivered edit uchar 256 args view ulong 3 to long
483 arc = auth_check hdr slots delivered get args 0 get args 1 RIGHT_RECLAIM
484 if ne arc ERR_OK
485 ret arc
486 end
487 id = get args 2
488 if ge id get hdr 0
489 ret ERR_BOUNDS
490 end
491 base = slot_off id
492 if ne get slots add base 2 STATE_REVOKED
493 ret ERR_STATE
494 end
495 gen = get slots add base 1
496 if eq gen CAP_U64_MAX
497 ret ERR_EXHAUSTED
498 end
499 put slots add base 1 add gen 1
500 put slots add base 2 STATE_FREE
501 put slots add base 5 0
502 put slots add base 8 0
503 put slots add base 9 CAP_NONE
504 put slots add base 10 0
505 put delivered id uchar 0
506 ret ERR_OK
507end
508
509note Advance logical lease clock. args[3]: 0=auth_id, 1=auth_gen, 2=ticks.
510export fn cap_table_clock hdr edit ulong 4 slots view ulong 3328 delivered view uchar 256 args view ulong 3 to long
511 arc = auth_check hdr slots delivered get args 0 get args 1 RIGHT_CLOCK
512 if ne arc ERR_OK
513 ret arc
514 end
515 now = get hdr 2
516 ticks = get args 2
517 if gt ticks sub CAP_U64_MAX now
518 ret ERR_OVERFLOW
519 end
520 put hdr 2 add now ticks
521 ret ERR_OK
522end
523
524note Bump capability epoch.
525export fn cap_table_epoch hdr edit ulong 4 slots view ulong 3328 delivered view uchar 256 aid ulong agen ulong to long
526 arc = auth_check hdr slots delivered aid agen RIGHT_EPOCH
527 if ne arc ERR_OK
528 ret arc
529 end
530 now = get hdr 1
531 if eq now CAP_U64_MAX
532 ret ERR_OVERFLOW
533 end
534 put hdr 1 add now 1
535 ret ERR_OK
536end