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 / apps / interplane-forge / salvage / 10verify.om

669 lines · 12724 bytes Raw Blame History
1note Collector: pinned compiler in an empty bwrap namespace, then execpure for the declared example.
2note No uploaded file is ever executed outside execpure. All command arguments are length-prefixed.
3text tlwork "work/"
4text tljlock "collector.lock"
5text tlccenv "OMEGA_COMPILER="
6global glccpath uchar 1024
7global glccn ulong 1
8text tlcc "omc"
9text tlcode "source.om"
10text tlbin "part.elf"
11text tlbuilt "compiler.out"
12text tlin "stdin"
13text tlout "stdout"
14text tlerr "stderr"
15text tlprlim "/usr/bin/prlimit"
16text tlbw "/usr/bin/bwrap"
17text tlaas "--as=268435456"
18text tlacpu "--cpu=2"
19text tlafsz "--fsize=8388608"
20text tladash "--"
21text tlunsh "--unshare-all"
22text tldie "--die-with-parent"
23text tlnew "--new-session"
24text tlcaps "--cap-drop"
25text tlall "ALL"
26text tlbind "--ro-bind"
27text tlinside "/omc"
28text tlpol "salvage-example-v1:bwrap-compile:execpure-run:3s-wall:4096-byte-output"
29global glpk uchar 8192
30global glpkn ulong 1
31global glpkc ulong 1
32global glphase ulong 1
33global glstatus uchar 4
34note The scratch root. Inside the server's own process (tests, replay) it is <store>work/. The
35note collector program sets one outside the store: the sandboxed server can write everything in
36note the store, and scratch files are compiled and executed, so they must be out of its reach.
37global glwroot uchar 512
38global glwrootn ulong 1
39note slwroot: an absolute path ending in /, no control bytes, no segment starting with a dot
40fn slwroot d view uchar * to bool
41if or lt len d 2 gt len d 500
42ret false
43end
44if or ne get d 0 47 ne get d sub len d 1 47
45ret false
46end
47for i ulong len d bound 512
48c = get d i
49if or lt c 32 eq c 127
50ret false
51end
52if gt i 0
53if and eq c 46 eq get d sub i 1 47
54ret false
55end
56end
57put glwroot i c
58end
59put glwrootn 0 len d
60ret true
61end
62fn slwpath name view uchar * to void
63n = get glwrootn 0
64if eq n 0
65slpath tlwork name
66ret
67end
68for i ulong n bound 512
69put glpath i get glwroot i
70end
71for i ulong len name bound 128
72put glpath add n i get name i
73end
74put glpathn 0 add n len name
75end
76note slwmk: the scratch folder exists and is a private 0700 directory
77fn slwmk to bool
78if eq get glwrootn 0 0
79ret slmkdir tlwork
80end
81r = makedir glwroot 0 get glwrootn 0 448
82if not or eq r 0 eq r -17
83ret false
84end
85fd = fopen glwroot 0 get glwrootn 0 573440 0
86if lt fd 0
87ret false
88end
89ok = slprivate int fd
90x = fclose int fd
91ret ok
92end
93fn slpknew to void
94put glpkn 0 2
95put glpkc 0 0
96end
97fn slpkadd s view uchar * to void
98n = get glpkn 0
99put glpk n uchar and len s 255
100put glpk add n 1 uchar shr len s 8
101for i ulong len s bound 1024
102put glpk add add n 2 i get s i
103end
104put glpkn 0 add add n 2 len s
105put glpkc 0 add get glpkc 0 1
106put glpk 0 uchar get glpkc 0
107end
108fn slpkpath name view uchar * to void
109p = new uchar 1024 0
110slwpath name
111n = slpget p
112slpkadd view part p 0 n
113end
114fn slunlink name view uchar * to void
115slwpath name
116r = unlink glpath 0 get glpathn 0
117end
118fn slwput name view uchar * data view uchar * to bool
119p = new uchar 1024 0
120slunlink name
121slwpath name
122n = slpget p
123ret slwrite view part p 0 n data 557249
124end
125fn slwfield name view uchar * fld ulong to bool
126b = new uchar 16384 0
127n = slfield fld b
128ret slwput name view part b 0 n
129end
130fn sltohex h view uchar * dst edit uchar * to void
131for i ulong 32
132put dst mul i 2 get tlhex ulong shr get h i 4
133put dst add mul i 2 1 get tlhex ulong and get h i 15
134end
135end
136fn slfilehash path view uchar * dst edit uchar * to bool
137b = new uchar 65536 0
138c = new ulong 40 0
139h = new uchar 32 0
140fd = fopen path 0 len path 557056 0
141if lt fd 0
142ret false
143end
144sha256_init c
145ok = false
146for i ulong 129
147r = read int fd b 0 65536
148if eq r 0
149ok = true
150break
151end
152if lt r 0
153break
154end
155sha256_update c view part b 0 ulong r
156end
157x = fclose int fd
158if not ok
159ret false
160end
161sha256_final c h
162sltohex h dst
163ret true
164end
165fn slccconfig to bool
166b = new uchar 2048 0
167put glccn 0 0
168for i ulong ulong envcnt bound 4096
169n = envread i b 0 2048
170if gt n long len tlccenv
171same = true
172for j ulong len tlccenv
173if ne get b j get tlccenv j
174same = false
175end
176end
177if same
178sz = sub ulong n len tlccenv
179if gt sz 1024
180ret false
181end
182if ne get b len tlccenv 47
183ret false
184end
185for j ulong sz bound 1024
186c = get b add len tlccenv j
187if or lt c 32 eq c 127
188ret false
189end
190put glccpath j c
191end
192put glccn 0 sz
193ret true
194end
195end
196end
197ret false
198end
199fn slpincc to bool
200b = new uchar 65536 0
201p = new uchar 1024 0
202h = new uchar 64 0
203if not slccconfig
204ret false
205end
206slunlink tlcc
207slwpath tlcc
208n = slpget p
209src = fopen glccpath 0 get glccn 0 557056 0
210if lt src 0
211ret false
212end
213dst = fopen p 0 n 557249 384
214if lt dst 0
215x = fclose int src
216ret false
217end
218ok = false
219for i ulong 129
220r = read int src b 0 65536
221if eq r 0
222ok = true
223break
224end
225if lt r 0
226break
227end
228w = ulong 0
229for j ulong 65536
230if ge w ulong r
231break
232end
233v = write int dst b w sub ulong r w
234if le v 0
235break
236end
237w = add w ulong v
238end
239if ne w ulong r
240break
241end
242end
243x = fclose int src
244r = fsync int dst
245z = fchmod int dst 320
246x = fclose int dst
247if not and and ok eq r 0 eq z 0
248ret false
249end
250if not slfilehash view part p 0 n h
251ret false
252end
253ret sleq h tlpin
254end
255fn slcompilepk to void
256slpknew
257slpkadd tlprlim
258slpkadd tlaas
259slpkadd tlacpu
260slpkadd tlafsz
261slpkadd tladash
262slpkadd tlbw
263slpkadd tlunsh
264slpkadd tldie
265slpkadd tlnew
266slpkadd tlcaps
267slpkadd tlall
268slpkadd tlbind
269slpkpath tlcc
270slpkadd tlinside
271slpkadd tladash
272slpkadd tlinside
273end
274fn slchild inf view uchar * outf view uchar * pure bool to void
275pk = new uchar 8192 0
276r = setpgid 0 0
277slwpath inf
278fd = fopen glpath 0 get glpathn 0 557056 0
279if lt fd 0
280exit 121
281end
282r = dupfd int fd 0 0
283x = fclose int fd
284if lt r 0
285exit 122
286end
287slunlink outf
288slwpath outf
289fd = fopen glpath 0 get glpathn 0 557249 384
290if lt fd 0
291exit 123
292end
293r = dupfd int fd 1 0
294x = fclose int fd
295if lt r 0
296exit 124
297end
298slunlink tlerr
299slwpath tlerr
300fd = fopen glpath 0 get glpathn 0 557249 384
301if lt fd 0
302exit 125
303end
304r = dupfd int fd 2 0
305x = fclose int fd
306if lt r 0
307exit 126
308end
309n = get glpkn 0
310for i ulong n bound 8192
311put pk i get glpk i
312end
313if pure
314r = execpure pk 0 n
315else
316r = execprog pk 0 n
317end
318if lt r 0
319exit int neg r
320end
321exit 127
322end
323fn slrun inf view uchar * outf view uchar * pure bool to bool
324st = new uchar 4 0
325pid = fork
326if lt pid 0
327ret false
328end
329if eq pid 0
330slchild inf outf pure
331exit 127
332end
333r = setpgid int pid int pid
334ok = false
335for i ulong 300
336r = waitpid int pid st 0 4 1
337if eq r pid
338ok = true
339break
340end
341if lt r 0
342break
343end
344r = sleepns 10000000
345end
346if not ok
347r = killpid int neg pid 9
348r = killpid int pid 9
349for i ulong 100
350r = waitpid int pid st 0 4 1
351if ne r 0
352break
353end
354r = sleepns 10000000
355end
356ret false
357end
358for i ulong 4
359put glstatus i get st i
360end
361for i ulong 4
362if ne get st i 0
363ret false
364end
365end
366ret true
367end
368note Linux ETXTBSY was observed at this boundary. The outstanding natural writer was not identified.
369note Copy completed compiler output to a new inode before execution. No compiler process inherited
370note this inode or its write descriptor. Retrying exec or sleeping would not establish that property.
371fn slfdwrite fd int data view uchar * to bool
372n = ulong 0
373for i ulong 65536
374if ge n len data
375ret true
376end
377r = write fd data n sub len data n
378if le r 0
379ret false
380end
381n = add n ulong r
382end
383ret eq n len data
384end
385fn slseal to bool
386b = new uchar 65536 0
387slwpath tlbuilt
388src = fopen glpath 0 get glpathn 0 557056 0
389if lt src 0
390ret false
391end
392slunlink tlbin
393slwpath tlbin
394dst = fopen glpath 0 get glpathn 0 557249 384
395if lt dst 0
396x = fclose int src
397ret false
398end
399ok = false
400for i ulong 129
401r = read int src b 0 65536
402if eq r 0
403ok = true
404break
405end
406if lt r 0
407break
408end
409if not slfdwrite int dst view part b 0 ulong r
410break
411end
412end
413x = fclose int src
414r = fsync int dst
415mode = fchmod int dst 320
416x = fclose int dst
417ret and and ok eq r 0 eq mode 0
418end
419fn slcheckex to bool
420b = new uchar 4097 0
421e = new uchar 4096 0
422slwpath tlout
423n = slpread b
424en = slfield 8 e
425if or lt n 0 gt n 4096
426ret false
427end
428if not sleq view part b 0 ulong n view part e 0 en
429ret false
430end
431note Negative control changes the declared expected bytes; it must fail the same exact-byte comparator.
432if eq en 0
433put e 0 88
434ret not sleq view part b 0 ulong n view part e 0 1
435end
436put e 0 xor get e 0 1
437ret not sleq view part b 0 ulong n view part e 0 en
438end
439fn slverify to bool
440put glphase 0 1
441if ne get glrec 8 2
442ret false
443end
444if not slwmk
445ret false
446end
447if not slpincc
448ret false
449end
450if not slwfield tlcode 5
451ret false
452end
453if not slwfield tlin 7
454ret false
455end
456put glphase 0 2
457slcompilepk
458if not slrun tlcode tlbuilt false
459ret false
460end
461if not slseal
462ret false
463end
464put glphase 0 3
465slpknew
466slpkpath tlbin
467if not slrun tlin tlout true
468ret false
469end
470put glphase 0 4
471ret slcheckex
472end
473global glrcpt uchar 353
474fn slrh fld ulong name view uchar * to bool
475p = new uchar 1024 0
476h = new uchar 64 0
477slwpath name
478n = slpget p
479if not slfilehash view part p 0 n h
480ret false
481end
482for i ulong 64
483put glrcpt add fld i get h i
484end
485ret true
486end
487fn slreceipt id view uchar * ok bool to bool
488b = new uchar 353 0
489h = new uchar 64 0
490m = new uchar 32 0
491for i ulong 353
492put glrcpt i 0
493end
494for i ulong 64
495put glrcpt i get id i
496put glrcpt add i 193 get tlpin i
497end
498put glrcpt 64 2
499put glrcpt 65 uchar get glphase 0
500put glrcpt 66 get glstatus 1
501put glrcpt 67 get glstatus 0
502if ok
503if not slrh 65 tlbin
504ret false
505end
506if not slrh 129 tlout
507ret false
508end
509put glrcpt 64 1
510end
511slhash tlpol h
512for i ulong 64
513put glrcpt add i 257 get h i
514end
515for i ulong 321
516put b i get glrcpt i
517end
518slmac view part b 0 321 m
519for i ulong 32
520put b add i 321 get m i
521end
522slpath tlrece id
523ret slpatom b
524end
525fn slreceiptget id view uchar * to ulong
526b = new uchar 354 0
527m = new uchar 32 0
528h = new uchar 64 0
529slpath tlrece id
530n = slpread b
531if eq n -2
532ret 0
533end
534if ne n 353
535ret 9
536end
537slmac view part b 0 321 m
538if not sleq view part b 321 32 m
539ret 9
540end
541if not sleq view part b 0 64 id
542ret 9
543end
544if not sleq view part b 193 64 tlpin
545ret 9
546end
547slhash tlpol h
548if not sleq view part b 257 64 h
549ret 9
550end
551if or lt get b 64 1 gt get b 64 2
552ret 9
553end
554for i ulong 353
555put glrcpt i get b i
556end
557ret ulong get b 64
558end
559note ---- the verification job. The server never compiles or runs a submission: the live forged
560note runs in a namespace with no compiler, no bwrap and nested namespaces disabled. It only queues
561note one job, the file <root>job holding the 64-character record id. The collector, its own program
562note (salvage/collector.om) outside that namespace, takes the job, verifies, writes the receipt and
563note removes the file. One job at a time: while the file exists every other request is "busy".
564text tljob "job"
565note sljobpoll: kept for the route. The server holds no child, so there is nothing to reap.
566fn sljobpoll to void
567ret
568end
569note sljobid: the queued record id into dst. False when no job, or the file is not 64 hex characters.
570fn sljobid dst edit uchar * to bool
571p = new uchar 1024 0
572b = new uchar 64 0
573slpath tlempty tljob
574n = slpget p
575r = slread view part p 0 n b
576if ne r 64
577ret false
578end
579if not slhex b
580ret false
581end
582for i ulong 64
583put dst i get b i
584end
585ret true
586end
587note sljobstart: queue the job. The id is written and synced under <root>job.next, then renamed to
588note <root>job with RENAME_NOREPLACE: the collector never sees a job file that exists before its 64
589note bytes are written (it removes a short one as damaged), and a queued job refuses the next one.
590fn sljobstart id view uchar * to bool
591p = new uchar 1024 0
592t = new uchar 1100 0
593if not slhex id
594ret false
595end
596slpath tlempty tljob
597n = slpget p
598for i ulong n bound 1024
599put t i get p i
600end
601for i ulong len tlnext
602put t add n i get tlnext i
603end
604m = add n len tlnext
605r = unlink t 0 m
606if not slwrite view part t 0 m id 557249
607r = unlink t 0 m
608ret false
609end
610if not slrename view part t 0 m view part p 0 n 1
611r = unlink t 0 m
612ret false
613end
614ret slsync view part p 0 n
615end
616note sljobdrop: remove the job file
617fn sljobdrop to void
618p = new uchar 1024 0
619slpath tlempty tljob
620n = slpget p
621r = unlink p 0 n
622end
623note slcollect: one pass of the collector. 0 nothing queued; 1 the job got its receipt; 2 the job
624note ended without a saved receipt, or another collector holds the lock. A job file that is not a
625note record id is removed (2), so a damaged file cannot hold the queue for ever.
626note The collector lock is held through verification, receipt and cleanup.
627fn slcollect to ulong
628id = new uchar 64 0
629p = new uchar 1024 0
630b = new uchar 65 0
631if not sljobid id
632slpath tlempty tljob
633n = slpget p
634r = slread view part p 0 n b
635if eq r -2
636ret 0
637end
638sljobdrop
639ret 2
640end
641slpath tlempty tljlock
642lockfd = fopen glpath 0 get glpathn 0 557122 384
643if lt lockfd 0
644ret 2
645end
646locked = flock int lockfd 6
647if ne locked 0
648x = fclose int lockfd
649ret 2
650end
651ok = slload id
652if ok
653ok = slverify
654end
655saved = slreceipt id ok
656slunlink tlcc
657slunlink tlbuilt
658slunlink tlbin
659slunlink tlcode
660slunlink tlin
661slunlink tlout
662slunlink tlerr
663sljobdrop
664x = fclose int lockfd
665if not saved
666ret 2
667end
668ret 1
669end