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

959 lines · 16706 bytes Raw Blame History
1note effects: effect-derived capability manifest for seeded-Omega programs.
2note A unit can reach the kernel only through the closed set of keyword operations in
3note OMEGESE section 10 (no raw syscall, no address-to-pointer), so the set of host
4note words in its source is a complete, finite statement of its OS effects.
5note Modes:
6note effects src [allow=C,..] < unit.om host words, classes, Linux syscalls
7note effects elf < binary syscalls the omc ELF can issue (fail closed)
8note effects verify SRC BIN [allow=C,..] ELF syscalls must be within the source set
9note effects bpf [launch] < unit.om seccomp cBPF allow-list (bwrap --seccomp)
10note effects table the word table, for the probe parity test
11note Exit: 0 pass, 1 refused/undeclared, 2 usage, 3 invalid or unresolvable input, 4 read.
12
13global inb uchar 8388612
14global mk uchar 2097156
15global rc uchar 2097156
16global wq ulong 2097156
17global ob uchar 1048576
18global obn ulong 1
19global dig uchar 24
20global ab uchar 4096
21global wn ulong 128
22global wc ulong 128
23global wsa ulong 128
24global wsn ulong 128
25global wsp ulong 1024
26global wl ulong 128
27global wk ulong 128
28global nw ulong 1
29global cn ulong 16
30global ncl ulong 1
31global sy uchar 512
32global se uchar 512
33global cu uchar 16
34global al uchar 16
35global st ulong 16
36global ef ulong 8
37
38note word class syscalls... ('-' entries are ignored; a row of only '-' issues none). Kept equal
39note to the pinned omc by check.sh, which compiles one probe per word and reads the syscalls back
40note out of the ELF.
41text tab "read io 63 -\nwrite io 64 -\nfclose fd 57 -\nfsync fd 82 -\nfdsync fd 83 -\nfseek fd 62 -\nftrunc fd 46 -\nfchmod fd 52 -\nfchdir fd 50 -\nsyncfs fd 267 -\nflock fd 32 -\ngetdnts fd 61 -\nfstat fd 80 -\npread fd 67 -\npwrite fd 68 -\ndupfd fd 24 -\npipefds fd 59 -\nwaitio fd 73 -\npollfds fd 73 -\nmemfd fd 279 -\neventfd fd 19 -\nfopen path 56 -\nchmod path 53 -\nchown path 54 -\nfaccess path 48 -\ntruncate path 45 -\nunlink path 35 -\nmakedir path 34 -\nrmdir path 35 -\ncwd path 17 -\nchdir path 49 -\nreadlnk path 78 -\nstat path 79 -\nlstat path 79 -\nrenames path 276 -\nlink path 37 -\nsymlnk path 36 -\nsocknew net 198 -\nbind net 200 -\nconnect net 203 -\nlisten net 201 -\naccept net 242 -\nsockname net 204 -\npeername net 205 -\nsockopt net 208 -\nrecv net 207 -\nsend net 206 -\npeercred net 209 -\nsendmsg net 211 -\nshutdown net 210 -\nfork proc 220 -\nexecprog proc 221 436\nexecpure proc 56 57 167 261 277 281 436 444 445 446\nexecenv proc 221 436\nprlimit proc 261 -\ncpuset proc 122 -\ncpuget proc 123 -\ncpunow proc 168 -\nsetprio proc 140 -\ngetprio proc 141 -\nkillpid proc 129 -\nwaitpid proc 260 -\nsetsid proc 157 -\nsetpgid proc 154 -\numask proc 166 -\ngetpid ident 172 -\ngetppid ident 173 -\ngetuid ident 174 -\ngeteuid ident 175 -\ngetgid ident 176 -\ngetegid ident 177 -\ngetpgid ident 155 -\ngetsid ident 156 -\nmonons time 113 -\nrealns time 113 -\nsleepns time 101 -\nargcnt env - -\nargread env - -\nenvcnt env - -\nenvread env - -\nrandom random 278 -\ndevctl device 29 -\ndevcall device 29 -\nuvmctl device 29 -\nvfioctl device 29 -\nvfioirq device 29 -\nkfdctl device 29 80\nkfdmap device 79 80 222\nkfdres device 215 222\nmapnew device 222 -\nvfiomap device 222 -\nvfiomapw device 222 -\nmapdrop device 215 -\nmaphold device 215 222\nmapbind device 215 222\nmapread device - -\nmapwrite device - -\nmapaddr device - -\nmapget device - -\nmapset device - -\nenlarge device 216 222\n"
42text cls "io\nfd\npath\nnet\nproc\nident\ntime\nenv\nrandom\ndevice\n"
43text kwn "note allow= launch src elf verify bpf table "
44text msg "EFFECT \n class=\n syscalls=\n uses=\n line=\nEFFECTS 1 words=\n classes=\nEFFECTS REFUSED word=\nEFFECTS PASS\nEFFECTS REFUSED n=\nELF 1 words=\n svc=\nELF UNRESOLVED at=\n reason=prev-not-movz-x8\n reason=branch-target\n reason=indirect-branch\nELF INVALID\nVERIFY PASS syscalls=\nVERIFY FAIL undeclared=\n unused=\nusage: effects src [allow=C,..] < unit.om | elf < binary | verify SRC BIN [allow=C,..] | bpf [launch] < unit.om | table\neffects: cannot read input\n-\n,\n \nELF REFUSED wx-segment\nEFFECTS REFUSED unknown-class\n reason=nr-range\nELF REFUSED no-exec-segment\n reason=leaves-segment\n reason=entry-svc\n reason=unknown-branch\n"
45
46note ---- output ----
47fn obf to void
48n = get obn 0
49o = ulong 0
50while lt o n
51r = write 1 ob o sub n o
52if le r 0
53exit 4
54end
55o = add o ulong r
56end
57put obn 0 0
58end
59fn ob1 b uchar to void
60n = get obn 0
61if ge n 1048000
62obf
63n = ulong 0
64end
65put ob n b
66put obn 0 add n 1
67end
68fn onl to void
69ob1 10
70end
71fn say id ulong to void
72k = ulong 0
73s = ulong 0
74for i ulong len msg
75if eq get msg i 10
76if eq k id
77for j ulong sub i s
78ob1 get msg add s j
79end
80ret
81end
82k = add k 1
83s = add i 1
84end
85end
86end
87fn od v ulong to void
88x = v
89n = ulong 0
90while ge x 10
91put dig n uchar add 48 rem x 10
92x = div x 10
93n = add n 1
94end
95put dig n uchar add 48 x
96n = add n 1
97for i ulong n
98ob1 get dig sub sub n i 1
99end
100end
101fn pw w ulong to void
102x = w
103for i ulong 8
104if eq x 0
105ret
106end
107ob1 uchar and x 255
108x = shr x 8
109end
110end
111
112note ---- tables ----
113fn kwp id ulong to ulong
114k = ulong 0
115w = ulong 0
116s = ulong 0
117for i ulong len kwn
118c = get kwn i
119if eq c 32
120if eq k id
121ret w
122end
123k = add k 1
124w = ulong 0
125s = ulong 0
126else
127w = or w shl ulong c s
128s = add s 8
129end
130end
131ret 0
132end
133fn clsid w ulong to ulong
134for i ulong get ncl 0
135if eq get cn i w
136ret i
137end
138end
139ret 99
140end
141fn tinit to void
142w = ulong 0
143s = ulong 0
144r = ulong 0
145for i ulong len cls
146c = get cls i
147if eq c 10
148put cn r w
149r = add r 1
150w = ulong 0
151s = ulong 0
152else
153w = or w shl ulong c s
154s = add s 8
155end
156end
157put ncl 0 r
158dash = ulong 45
159r = ulong 0
160f = ulong 0
161p = ulong 0
162w = ulong 0
163s = ulong 0
164d = ulong 0
165for i ulong len tab
166c = get tab i
167if or eq c 32 eq c 10
168if eq f 0
169put wn r w
170end
171if eq f 1
172put wc r clsid w
173put wsa r p
174end
175if and ge f 2 ne w dash
176put wsp p d
177p = add p 1
178put wsn r add get wsn r 1
179end
180f = add f 1
181if eq c 10
182r = add r 1
183f = ulong 0
184end
185w = ulong 0
186s = ulong 0
187d = ulong 0
188else
189w = or w shl ulong c s
190s = add s 8
191d = add mul d 10 sub ulong c 48
192end
193end
194put nw 0 r
195end
196
197note ---- input ----
198fn rdfd fd int to ulong
199n = ulong 0
200for i ulong 8388608
201r = read fd inb n sub 8388612 n
202if lt r 0
203ret 8388613
204end
205if eq r 0
206ret n
207end
208n = add n ulong r
209if ge n 8388612
210ret 8388613
211end
212end
213ret 8388613
214end
215fn rdarg ix ulong to ulong
216m = argread ix ab 0 4095
217if le m 0
218ret 8388613
219end
220fd = int fopen ab 0 ulong m 0 0
221if lt fd 0
222ret 8388613
223end
224n = rdfd fd
225c = fclose fd
226ret n
227end
228fn argw ix ulong to ulong
229m = argread ix ab 0 4095
230if or le m 0 gt m 8
231ret 0
232end
233w = ulong 0
234for i ulong ulong m
235w = or w shl ulong get ab i mul i 8
236end
237ret w
238end
239note allow=C1,C2 in argument ix; returns false on an unknown class name
240fn parsal ix ulong to bool
241m = argread ix ab 0 4095
242if le m 0
243ret false
244end
245if lt m 6
246ret false
247end
248pfx = ulong 0
249for i ulong 6
250pfx = or pfx shl ulong get ab i mul i 8
251end
252if ne pfx kwp 1
253ret false
254end
255put ef 0 1
256w = ulong 0
257s = ulong 0
258for i ulong sub ulong m 5
259j = add i 6
260c = uchar 44
261if lt j ulong m
262c = get ab j
263end
264if eq c 44
265if gt s 0
266k = clsid w
267if eq k 99
268ret false
269end
270put al k 1
271end
272w = ulong 0
273s = ulong 0
274else
275if ge s 64
276ret false
277end
278w = or w shl ulong c s
279s = add s 8
280end
281end
282ret true
283end
284
285note ---- source scan: every host word occurrence outside notes and text literals ----
286fn hit w ulong ln ulong to void
287for r ulong get nw 0
288if eq get wn r w
289put wk r add get wk r 1
290if eq get wl r 0
291put wl r ln
292end
293put cu get wc r 1
294for j ulong get wsn r
295a = get wsp add get wsa r j
296if lt a 512
297put sy a 1
298end
299end
300ret
301end
302end
303end
304note st: 0 packed word, 1 word length, 2 line, 3 skip-to-eol, 4 in quote, 5 escape, 6 first word on line
305fn fin to void
306l = get st 1
307if eq l 0
308ret
309end
310w = get st 0
311if and eq get st 6 1 and eq l 4 eq w kwp 0
312put st 3 1
313else
314if le l 8
315hit w get st 2
316end
317end
318put st 6 0
319put st 0 0
320put st 1 0
321end
322fn scansrc n ulong to void
323put st 0 0
324put st 1 0
325put st 2 1
326put st 3 0
327put st 4 0
328put st 5 0
329put st 6 1
330put sy 93 1
331for i ulong 8388608
332if ge i n
333break
334end
335c = get inb i
336if eq c 10
337fin
338put st 2 add get st 2 1
339put st 3 0
340put st 4 0
341put st 5 0
342put st 6 1
343continue
344end
345if eq get st 3 1
346continue
347end
348if eq get st 4 1
349if eq get st 5 1
350put st 5 0
351else
352if eq c 92
353put st 5 1
354end
355if eq c 34
356put st 4 0
357end
358end
359continue
360end
361if or or eq c 32 eq c 9 or eq c 13 or eq c 40 eq c 41
362fin
363continue
364end
365if eq c 34
366fin
367put st 4 1
368put st 6 0
369continue
370end
371l = get st 1
372if lt l 8
373put st 0 or get st 0 shl ulong c mul l 8
374end
375put st 1 add l 1
376end
377fin
378end
379fn plist a ulong to void
380note a 0: source set, 1: elf set, 2: elf minus source, 3: source minus elf
381k = ulong 0
382for i ulong 512
383x = get sy i
384y = get se i
385p = false
386if eq a 0
387p = eq x 1
388end
389if eq a 1
390p = eq y 1
391end
392if eq a 2
393p = and eq y 1 eq x 0
394end
395if eq a 3
396p = and eq x 1 eq y 0
397end
398if p
399if gt k 0
400say 23
401end
402od i
403k = add k 1
404end
405end
406if eq k 0
407say 22
408end
409end
410fn psys r ulong to void
411n = get wsn r
412if eq n 0
413say 22
414ret
415end
416for j ulong n
417if gt j 0
418say 23
419end
420od get wsp add get wsa r j
421end
422end
423fn report to int
424u = ulong 0
425for r ulong get nw 0
426if gt get wk r 0
427u = add u 1
428say 0
429pw get wn r
430say 1
431pw get cn get wc r
432say 2
433psys r
434say 3
435od get wk r
436say 4
437od get wl r
438onl
439end
440end
441say 5
442od u
443say 6
444k = ulong 0
445for i ulong get ncl 0
446if eq get cu i 1
447if gt k 0
448say 23
449end
450pw get cn i
451k = add k 1
452end
453end
454if eq k 0
455say 22
456end
457say 2
458plist 0
459onl
460ret refuse
461end
462fn refuse to int
463if eq get ef 0 0
464ret 0
465end
466bad = ulong 0
467for r ulong get nw 0
468if and gt get wk r 0 eq get al get wc r 0
469bad = add bad 1
470say 7
471pw get wn r
472say 1
473pw get cn get wc r
474say 4
475od get wl r
476onl
477end
478end
479if eq bad 0
480say 8
481onl
482ret 0
483end
484say 9
485od bad
486onl
487ret 1
488end
489
490note ---- ELF scan of an omc static AArch64 binary ----
491fn r8 o ulong to ulong
492ret ulong get inb o
493end
494fn r16 o ulong to ulong
495ret or r8 o shl r8 add o 1 8
496end
497fn r32 o ulong to ulong
498ret or r16 o shl r16 add o 2 16
499end
500fn r64 o ulong to ulong
501ret or r32 o shl r32 add o 4 32
502end
503fn bad3 id ulong to int
504say id
505onl
506ret 3
507end
508fn unres at ulong why ulong to int
509say 12
510od at
511say why
512onl
513ret 3
514end
515note branch target of word w at index j, or 4294967295 if w is not a direct branch
516fn btgt w ulong j ulong to ulong
517o = ulong 0
518m = ulong 0
519if eq and w 2080374784 335544320
520o = and w 67108863
521m = 67108864
522end
523if or eq and w 4278190096 1409286144 eq and w 2113929216 872415232
524o = and shr w 5 524287
525m = 524288
526end
527if eq and w 2113929216 905969664
528o = and shr w 5 16383
529m = 16384
530end
531if eq m 0
532ret 4294967295
533end
534if ge o div m 2
535b = sub m o
536if gt b j
537ret 4294967294
538end
539ret sub j b
540end
541ret add j o
542end
543fn scantext off ulong fsz ulong ent ulong to int
544nwd = div fsz 4
545if gt nwd 2097152
546ret bad3 16
547end
548for j ulong 2097152
549if ge j nwd
550break
551end
552put mk j 0
553end
554for j ulong 2097152
555if ge j nwd
556break
557end
558w = r32 add off mul j 4
559if or eq and w 4294966303 3592355840 eq and w 4294966303 3594452992
560ret unres j 15
561end
562if eq and w 4292870175 3556769793
563put ef 1 add get ef 1 1
564put mk j 1
565if eq j 0
566ret unres j 13
567end
568p = r32 add off mul sub j 1 4
569if ne and p 4292870175 3531603976
570ret unres j 13
571end
572nr = and shr p 5 65535
573if ge nr 512
574ret unres j 27
575end
576put se nr 1
577end
578end
579note Which words can run as code. Texts and other data share the executable segment with the
580note instructions, so a data word can have the bit pattern of a direct branch. Only a word that
581note control can reach is held to the branch rule. Reach starts at the entry word (ent) and follows
582note the next word, except after an unconditional B, a RET, or an SVC whose number is 93 (exit) or
583note 94 (exit_group), and the target of every direct branch. Such an SVC does not return: it is
584note entered only through its MOVZ X8, because no reached branch lands on it. BR and BLR are refused
585note above wherever they occur, so nothing else can transfer control.
586note Three things would let control run words this walk does not follow, and each is refused:
587note the entry word is itself an SVC (the kernel enters it with no MOVZ before it); a reached
588note direct branch has its target outside this segment; a reached word that falls through is the
589note last word of the segment. A reached branch instruction this walk cannot follow is refused too.
590note When the entry word is not in this segment (ent is 4294967295) every word is held to the rule.
591if ge ent nwd
592for j ulong 2097152
593if ge j nwd
594break
595end
596u = btgt r32 add off mul j 4 j
597if lt u nwd
598if eq get mk u 1
599ret unres u 14
600end
601end
602end
603put ef 2 add get ef 2 nwd
604ret 0
605end
606note rc: 1 once a word is known to be reached. wq: reached words not yet followed; each word
607note enters it once, so it never holds more than nwd entries and the loop ends within nwd turns.
608for j ulong 2097152
609if ge j nwd
610break
611end
612put rc j 0
613end
614if eq get mk ent 1
615ret unres ent 30
616end
617put rc ent 1
618put wq 0 ent
619sp = ulong 1
620for it ulong 2097153
621if eq sp 0
622break
623end
624sp = sub sp 1
625cw = get wq sp
626w = r32 add off mul cw 4
627note a reached word of the register-branch class that is not RET (BRAA, BLRAA, RETAA, ERET and the
628note rest), or a BC.cond, moves control in a way this walk does not follow
629if eq and w 4261412864 3590324224
630if ne and w 4294966303 3596550144
631ret unres cw 31
632end
633end
634if eq and w 4278190096 1409286160
635ret unres cw 31
636end
637t = btgt w cw
638if and ne t 4294967295 ge t nwd
639ret unres cw 29
640end
641if lt t nwd
642if eq get mk t 1
643ret unres t 14
644end
645if eq get rc t 0
646put rc t 1
647put wq sp t
648sp = add sp 1
649end
650end
651fal = ulong 1
652if eq and w 4227858432 335544320
653fal = 0
654end
655if eq and w 4294966303 3596550144
656fal = 0
657end
658if eq get mk cw 1
659p = r32 add off mul sub cw 1 4
660nr = and shr p 5 65535
661if or eq nr 93 eq nr 94
662fal = 0
663end
664end
665nx1 = add cw 1
666if and eq fal 1 ge nx1 nwd
667ret unres cw 29
668end
669if and eq fal 1 lt nx1 nwd
670if eq get rc nx1 0
671put rc nx1 1
672put wq sp nx1
673sp = add sp 1
674end
675end
676end
677put ef 2 add get ef 2 nwd
678ret 0
679end
680note one program header: -1 not an executable load, 0 scanned, >0 exit code
681fn seg p ulong n ulong to int
682t = r32 p
683f = r32 add p 4
684if ne t 1
685ret -1
686end
687if and ne and f 1 0 ne and f 2 0
688say 25
689onl
690ret 3
691end
692if eq and f 1 0
693ret -1
694end
695off = r64 add p 8
696fsz = r64 add p 32
697if or ne rem off 4 0 gt add off fsz n
698ret bad3 16
699end
700note the entry word of the program, as an index into this segment, when it lies in it
701va = r64 add p 16
702en = r64 24
703ent = ulong 4294967295
704if ge en va
705d = sub en va
706if lt d fsz
707if ne rem d 4 0
708ret bad3 16
709end
710ent = div d 4
711end
712end
713ret scantext off fsz ent
714end
715fn scanelf n ulong to int
716if lt n 64
717ret bad3 16
718end
719if or ne r32 0 1179403647 ne r16 18 183
720ret bad3 16
721end
722ph = r64 32
723pe = r16 54
724pn = r16 56
725if or ne pe 56 gt add ph mul pn 56 n
726ret bad3 16
727end
728nx = ulong 0
729for i ulong pn
730v = seg add ph mul i 56 n
731if gt v 0
732ret v
733end
734if eq v 0
735nx = add nx 1
736end
737end
738if eq nx 0
739say 28
740onl
741ret 3
742end
743ret 0
744end
745fn elfrep to void
746say 10
747od get ef 2
748say 11
749od get ef 1
750say 2
751plist 1
752onl
753end
754
755note ---- seccomp cBPF: arch check, one JEQ per allowed nr, kill otherwise ----
756fn binst code ulong jt ulong jf ulong k ulong to void
757ob1 uchar and code 255
758ob1 uchar shr code 8
759ob1 uchar jt
760ob1 uchar jf
761ob1 uchar and k 255
762ob1 uchar and shr k 8 255
763ob1 uchar and shr k 16 255
764ob1 uchar shr k 24
765end
766fn bpfout to void
767m = ulong 0
768for i ulong 512
769if eq get sy i 1
770m = add m 1
771end
772end
773binst 32 0 0 4
774binst 21 1 0 3221225655
775binst 6 0 0 2147483648
776binst 32 0 0 0
777t = ulong 0
778for i ulong 512
779if eq get sy i 1
780binst 21 sub m t 0 i
781t = add t 1
782end
783end
784binst 6 0 0 2147483648
785binst 6 0 0 2147418112
786end
787
788fn ptable to void
789for r ulong get nw 0
790pw get wn r
791ob1 32
792pw get cn get wc r
793ob1 32
794n = get wsn r
795if eq n 0
796say 22
797end
798for j ulong n
799if gt j 0
800ob1 32
801end
802od get wsp add get wsa r j
803end
804onl
805end
806end
807
808fn main to int
809tinit
810ac = argcnt
811if lt ac 2
812say 20
813onl
814obf
815ret 2
816end
817md = argw 1
818if eq md kwp 7
819ptable
820obf
821ret 0
822end
823if eq md kwp 3
824if gt ac 3
825say 20
826onl
827obf
828ret 2
829end
830if eq ac 3
831if not parsal 2
832say 26
833onl
834obf
835ret 2
836end
837end
838n = rdfd 0
839if gt n 8388612
840say 21
841onl
842obf
843ret 4
844end
845scansrc n
846v = report
847obf
848ret v
849end
850if eq md kwp 4
851if ne ac 2
852say 20
853onl
854obf
855ret 2
856end
857n = rdfd 0
858if gt n 8388612
859say 21
860onl
861obf
862ret 4
863end
864v = scanelf n
865if eq v 0
866elfrep
867end
868obf
869ret v
870end
871if eq md kwp 6
872lw = ulong 0
873if eq ac 3
874lw = argw 2
875end
876if or gt ac 3 and eq ac 3 ne lw kwp 2
877say 20
878onl
879obf
880ret 2
881end
882n = rdfd 0
883if gt n 8388612
884say 21
885onl
886obf
887ret 4
888end
889scansrc n
890if eq ac 3
891put sy 221 1
892end
893bpfout
894obf
895ret 0
896end
897if eq md kwp 5
898if or lt ac 4 gt ac 5
899say 20
900onl
901obf
902ret 2
903end
904if eq ac 5
905if not parsal 4
906say 26
907onl
908obf
909ret 2
910end
911end
912n = rdarg 2
913if gt n 8388612
914say 21
915onl
916obf
917ret 4
918end
919scansrc n
920v = report
921n = rdarg 3
922if gt n 8388612
923say 21
924onl
925obf
926ret 4
927end
928e = scanelf n
929if ne e 0
930obf
931ret e
932end
933elfrep
934k = ulong 0
935for i ulong 512
936if and eq get se i 1 eq get sy i 0
937k = add k 1
938end
939end
940if gt k 0
941say 18
942plist 2
943onl
944obf
945ret 1
946end
947say 17
948plist 1
949say 19
950plist 3
951onl
952obf
953ret v
954end
955say 20
956onl
957obf
958ret 2
959end