interplane.Get Omega
Read in your language

omega-mcp

aien-dev/omega-mcp

Brought by drakestapleton

branch main Branches Tags History

omega-mcp / src / sandbox.om

669 lines · 12868 bytes Raw Blame History
1note omega-mcp: per-call temporary directory, the sandboxed spawn and the in-sandbox reaper.
2note
3note One compile or run:
4note 1. the parent creates the stage's stdin/stdout/stderr files in the 0700 temp dir and three close-on-exec
5note pipes (status, block, info), then forks child A;
6note 2. execprog closes every descriptor at or above 3 (omega-mv OMEGESE.md, Path and process boundaries),
7note so A moves the block, info and status pipes to fds 0, 1 and 2 and execs /usr/bin/bwrap (no shell,
8note empty environment) with every namespace unshared, a cleared environment, a read-only root holding
9note only this executable (/r), the program (/p) and the three stage files (/i read-only, /o and /e
10note writable), a 16 MiB tmpfs /w, and --block-fd 0 / --info-fd 1;
11note 3. the parent reads the sandbox pid 1 from the info fd, sets its limits with the prlimit host word (cpu,
12note as, fsize, nproc, nofile, core; omega-mv keyword 235) and only then writes the byte that lets bwrap
13note continue;
14note 4. inside, /r reap /p forks; the child opens /i, /o and /e onto fds 0..2 and execs /p (execprog closes
15note fds >= 3, the status pipe among them), and the reaper writes the raw wait status to fd 2 (the
16note status pipe) as 'X' + 4 bytes ('E' + errno when the stage files could not be opened or fork failed;
17note a failed exec leaves only fds 0..2, so the child reports it on /e and exits 127);
18note 5. the parent polls the status pipe and waitpid(WNOHANG) against a monons deadline and kills bwrap
19note with SIGKILL when the wall clock runs out (--die-with-parent tears the namespaces down).
20
21text kbwrap "/usr/bin/bwrap"
22text kenv "/usr/bin/env"
23text a_ua "--unshare-all"
24text a_uu "--unshare-user"
25text a_du "--disable-userns"
26text a_dwp "--die-with-parent"
27text a_ns "--new-session"
28text a_ce "--clearenv"
29text a_bf "--block-fd"
30text a_if "--info-fd"
31text a_rob "--ro-bind"
32text a_bind "--bind"
33text a_size "--size"
34text a_tmpfs "--tmpfs"
35text a_chdir "--chdir"
36text a_rro "--remount-ro"
37text a_0 "0"
38text a_1 "1"
39text a_r "/r"
40text a_p "/p"
41text a_w "/w"
42text a_i "/i"
43text a_o "/o"
44text a_e "/e"
45text k_xf "omega-mcp reaper: exec of the program failed, errno "
46text a_root "/"
47text a_16m "16777216"
48text a_reap "reap"
49text f_src "src"
50text f_bin "bin"
51text f_in "in"
52text f_out "out"
53text f_err "err"
54text f_cerr "cerr"
55text kcpid "child-pid"
56
57note ---- temporary directory ----
58fn mktemp to bool
59for a ulong 8
60r = random rnd 0 8 0
61if ne r 8
62continue
63end
64n = len ktmp
65for i ulong n
66put tdir i get ktmp i
67end
68for i ulong 8
69c = get rnd i
70put tdir add n mul i 2 get khex ulong shr c 4
71put tdir add add n mul i 2 1 get khex ulong and c 15
72end
73put gv gtdl add n 16
74m = makedir tdir 0 add n 16 448
75if eq m 0
76ret true
77end
78end
79put gv gtdl 0
80ret false
81end
82
83note rmtemp: remove the fixed files, then the directory; false when the directory stays behind
84fn rmtemp to bool
85if eq get gv gtdl 0
86ret true
87end
88tdel f_src
89tdel f_bin
90tdel f_in
91tdel f_out
92tdel f_err
93tdel f_cerr
94r = rmdir tdir 0 get gv gtdl
95put gv gtdl 0
96ret eq r 0
97end
98
99note ---- descriptors ----
100fn pipe2 rs ulong ws ulong to bool
101r = pipefds fds 0 8 524288
102if ne r 0
103ret false
104end
105put gv rs fds32 0
106put gv ws fds32 4
107ret true
108end
109
110fn cl slot ulong to void
111v = get gv slot
112if lt v 1024
113c = fclose int v
114end
115put gv slot 9999
116end
117
118fn clall to void
119cl gfin
120cl gfout
121cl gferr
122cl gsr
123cl gsw
124cl gbr
125cl gbw
126cl gir
127cl giw
128end
129
130note setfd: store a nonnegative open result in a slot; false otherwise
131fn setfd slot ulong r long to bool
132if lt r 0
133put gv slot 9999
134ret false
135end
136put gv slot ulong r
137ret true
138end
139
140note ---- argument lists ----
141fn pk_bwrap md ulong to void
142pk_reset
143pk_arg kbwrap
144pk_arg a_ua
145pk_arg a_uu
146pk_arg a_du
147pk_arg a_dwp
148pk_arg a_ns
149pk_arg a_ce
150pk_arg a_bf
151pk_arg a_0
152pk_arg a_if
153pk_arg a_1
154pk_arg a_rob
155pk_beg
156pk_sx
157pk_end
158pk_arg a_r
159pk_arg a_rob
160pk_beg
161if eq md 0
162pk_omc
163end
164if ne md 0
165pk_tdir
166pk_b 47
167pk_t f_bin
168end
169pk_end
170pk_arg a_p
171pk_arg a_rob
172pk_stage f_src f_in md
173pk_arg a_i
174pk_arg a_bind
175pk_stage f_bin f_out md
176pk_arg a_o
177pk_arg a_bind
178pk_stage f_cerr f_err md
179pk_arg a_e
180pk_arg a_size
181pk_arg a_16m
182pk_arg a_tmpfs
183pk_arg a_w
184pk_arg a_chdir
185pk_arg a_w
186pk_arg a_rro
187pk_arg a_root
188pk_arg a_r
189pk_arg a_reap
190pk_arg a_p
191end
192
193note pk_stage: one packed temp-dir path, tdir/c for the compile stage (md 0) or tdir/r for the run stage
194fn pk_stage c view uchar * r view uchar * md ulong to void
195pk_beg
196pk_tdir
197pk_b 47
198if eq md 0
199pk_t c
200end
201if ne md 0
202pk_t r
203end
204pk_end
205end
206
207note pk_noiso: the isolation-off control (--test-only-no-isolation). The program runs directly on the
208note host through /usr/bin/env with the server environment entries that fit the packed list (at most 62
209note entries and 4096 bytes; later entries are dropped), in the temp dir, with no limits.
210fn pk_noiso md ulong to void
211pk_reset
212pk_arg kenv
213nenv = ulong envcnt
214b = new uchar 1024 0
215for i ulong nenv
216r = envread i b 0 1024
217if lt r 0
218continue
219end
220if ge get gv gpkn 63
221break
222end
223if gt add add get gv gpkl ulong r 600 4096
224continue
225end
226pk_beg
227for j ulong ulong r
228pk_b get b j
229end
230pk_end
231end
232pk_beg
233if eq md 0
234pk_omc
235end
236if ne md 0
237pk_tdir
238pk_b 47
239pk_t f_bin
240end
241pk_end
242end
243
244note ---- child side ----
245fn fdof slot ulong to int
246ret int get gv slot
247end
248
249note childgo: isolation off, the stage files become fds 0..2 of the program itself; otherwise the block,
250note info and status pipes become fds 0..2 of bwrap (execprog closes everything at or above 3)
251fn childgo to void
252a = gbr
253b = giw
254c = gsw
255if ne get gv gnoiso 0
256a = gfin
257b = gfout
258c = gferr
259end
260z = dupfd fdof a 100 524288
261z = dupfd fdof b 101 524288
262z = dupfd fdof c 102 524288
263z = dupfd 100 0 0
264z = dupfd 101 1 0
265z = dupfd 102 2 0
266if ne get gv gnoiso 0
267q = chdir tdir 0 get gv gtdl
268end
269r = execprog pk 0 get gv gpkl
270exit 127
271end
272
273note ---- parent side ----
274note waitkid: reap pid within ms milliseconds (SIGKILL after that); returns the raw status or 65535
275fn waitkid pid long ms ulong to ulong
276dl = add monons mul long ms 1000000
277k = false
278for i ulong 999999
279r = waitpid int pid wst 0 4 1
280if eq r pid
281ret wst32
282end
283if and lt r 0 ne r -4
284ret 65535
285end
286if and not k gt monons dl
287z = killpid int pid 9
288k = true
289end
290z = sleepns long 1000000
291end
292ret 65535
293end
294
295note readinfo: the child-pid bwrap reports on the info pipe within 5 s; 0 when it never comes
296fn readinfo to ulong
297dl = add monons 5000000000
298n = ulong 0
299fd = fdof gir
300for i ulong 999999
301if ge n 1000
302break
303end
304rem0 = sub dl monons
305if le rem0 0
306break
307end
308w = waitio fd 1 rem0
309if le w 0
310break
311end
312r = read fd inf n sub 1000 n
313if le r 0
314break
315end
316n = add n ulong r
317if eq get inf sub n 1 125
318break
319end
320end
321ret infopid n
322end
323
324fn infat s ulong to bool
325for j ulong len kcpid
326if ne get inf add s j get kcpid j
327ret false
328end
329end
330ret true
331end
332
333note infnum: the first run of decimal digits in inf[s..n)
334fn infnum s ulong n ulong to ulong
335v = ulong 0
336dig = false
337for p ulong sub n s
338c = get inf add s p
339if and ge c 48 le c 57
340v = add mul v 10 ulong sub c 48
341dig = true
342continue
343end
344if dig
345break
346end
347end
348ret v
349end
350
351fn infopid n ulong to ulong
352k = len kcpid
353if lt n k
354ret 0
355end
356for s ulong add sub n k 1
357if infat s
358ret infnum add s k n
359end
360end
361ret 0
362end
363
364note setrl: set resource res of process pid to soft limit lo and hard limit hi with the prlimit host word
365note (n 32: the new pair is read from rl[0..16) as two little-endian u64, the old pair is written to rl[16..32));
366note true when it returned 0
367fn setrl pid int res int lo ulong hi ulong to bool
368for i ulong 8
369put rl i uchar and shr lo mul i 8 255
370put rl add i 8 uchar and shr hi mul i 8 255
371end
372ret eq prlimit pid res rl 0 32 0
373end
374
375note limits: the stage's resource limits on the sandbox pid 1, as the former prlimit(1) call set them
376note (Linux RLIMIT_CPU 0, FSIZE 1, CORE 4, NPROC 6, NOFILE 7, AS 9); true when every call returned 0
377fn limits md ulong cpid ulong to bool
378p = int cpid
379cpu = runcpu
380asz = runas
381fsz = runfsz
382npr = runnpr
383if eq md 0
384cpu = cmpcpu
385asz = cmpas
386fsz = cmpfsz
387npr = cmpnpr
388end
389if not setrl p 0 cpu add cpu 1
390ret false
391end
392if not setrl p 9 asz asz
393ret false
394end
395if not setrl p 1 fsz fsz
396ret false
397end
398if not setrl p 6 npr npr
399ret false
400end
401if not setrl p 7 64 64
402ret false
403end
404ret setrl p 4 0 0
405end
406
407note drain: move status-pipe bytes into sbuf; true while the pipe may still deliver
408fn drain wait long to bool
409fd = fdof gsr
410g = get gv gsgot
411w = waitio fd 1 wait
412if le w 0
413ret true
414end
415if ge g 8
416z = read fd hbuf 0 64
417ret gt z 0
418end
419r = read fd sbuf g sub 8 g
420if le r 0
421ret false
422end
423put gv gsgot add g ulong r
424ret true
425end
426
427note watch: wait for pid (bwrap or the direct child) until the wall-clock deadline
428fn watch pid long tmo ulong to void
429t0 = monons
430dl = add t0 long tmo
431k = false
432live = eq get gv gnoiso 0
433for i ulong 999999
434if live
435live = drain 2000000
436end
437if not live
438z = sleepns long 2000000
439end
440r = waitpid int pid wst 0 4 1
441if eq r pid
442break
443end
444if and lt r 0 ne r -4
445break
446end
447if and not k gt monons dl
448z = killpid int pid 9
449k = true
450put gv gwall 1
451end
452end
453if live
454for j ulong 8
455if not drain 0
456break
457end
458end
459end
460put gv gbws wst32
461put gv gelms ulong div sub monons t0 1000000
462end
463
464note status: decode the reaper record (or, isolation off, the direct wait status)
465fn status to void
466if ne get gv gnoiso 0
467put gv gtag 88
468put gv gst get gv gbws
469ret
470end
471if lt get gv gsgot 5
472put gv gtag 0
473ret
474end
475put gv gtag ulong get sbuf 0
476put gv gst sb32 1
477end
478
479note spawn: one stage. md 0 compiles tdir/src into tdir/bin with the pinned compiler, md 1 runs tdir/bin
480note on tdir/in. tmo is the wall clock in ns. gsbf: 0 ran, 1 setup failed, 2 no sandbox pid,
481note 3 the resource limits could not be set, 4 argument list too long.
482fn spawn md ulong tmo ulong to void
483put gv gsbf 0
484put gv gwall 0
485put gv gsgot 0
486put gv gtag 0
487for i ulong 8
488put sbuf i 0
489end
490for s ulong 9
491put gv add gfin s 9999
492end
493ok = true
494if eq md 0
495ok = and setfd gfin trd f_src and setfd gfout tnew f_bin 448 setfd gferr tnew f_cerr 384
496end
497if ne md 0
498ok = and setfd gfin trd f_in and setfd gfout tnew f_out 384 setfd gferr tnew f_err 384
499end
500ok = and ok and pipe2 gsr gsw and pipe2 gbr gbw pipe2 gir giw
501if ne get gv gnoiso 0
502pk_noiso md
503end
504if eq get gv gnoiso 0
505pk_bwrap md
506end
507if ne get gv gpkov 0
508put gv gsbf 4
509clall
510ret
511end
512if not ok
513put gv gsbf 1
514clall
515ret
516end
517pid = fork
518if eq pid 0
519childgo
520end
521cl gfin
522cl gfout
523cl gferr
524cl gsw
525cl gbr
526cl giw
527if lt pid 0
528put gv gsbf 1
529clall
530ret
531end
532if eq get gv gnoiso 0
533gate md pid
534end
535cl gbw
536if eq get gv gsbf 0
537watch pid tmo
538end
539status
540clall
541end
542
543note gate: the prlimit handshake; on failure the sandbox is killed before it runs anything
544fn gate md ulong pid long to void
545cp = readinfo
546put gv gcpid cp
547if eq cp 0
548put gv gsbf 2
549end
550if ne cp 0
551if not limits md cp
552put gv gsbf 3
553end
554end
555if ne get gv gsbf 0
556z = killpid int pid 9
557s = waitkid pid 5000
558ret
559end
560one = new uchar 1 120
561w = write fdof gbw one 0 1
562end
563
564note ---- reaper: runs as /r reap /p inside the sandbox ----
565fn reapmain to int
566one = new uchar 5 69
567rec = new uchar 5 88
568n = argread 2 pa 0 4096
569if le n 0
570ret 126
571end
572pid = fork
573if eq pid 0
574reapkid ulong n
575end
576if lt pid 0
577note exec of the program never happened: 'E' + errno 0
578w = write 2 one 0 5
579ret 0
580end
581for i ulong 999999
582r = waitpid int pid wst 0 4 0
583if eq r pid
584break
585end
586if ne r -4
587break
588end
589end
590for i ulong 4
591put rec add i 1 get wst i
592end
593w = write 2 rec 0 5
594ret 0
595end
596
597note reapkid: fds 0 and 1 arrive closed (bwrap closes its block and info fds), fd 2 is the status pipe.
598note Each stage file is staged at 60..62 first (the sandbox runs under --nofile=64), so an open that lands
599note on 0 or 1 is never dup3'd onto itself; the status pipe is staged at 63 for reapfail.
600fn reapkid n ulong to void
601z = dupfd 2 63 524288
602if lt z 0
603exit 127
604end
605a = fopen a_i 0 len a_i ofrd 0
606reapstage a 60
607b = fopen a_o 0 len a_o ofwr 0
608reapstage b 61
609c = fopen a_e 0 len a_e ofwr 0
610reapstage c 62
611reapstage long dupfd 60 0 0 0
612reapstage long dupfd 61 1 0 1
613reapstage long dupfd 62 2 0 2
614pk_reset
615pk_beg
616for i ulong n
617pk_b get pa i
618end
619pk_end
620r = execprog pk 0 get gv gpkl
621reapxf r
622end
623
624note reapxf: execprog failed after closing fds >= 3, so the errno goes to the program's stderr (/e)
625fn reapxf r long to void
626v = ulong neg r
627d = new uchar 20 0
628k = ulong 0
629for i ulong 20
630put d sub 19 i uchar add 48 rem v 10
631k = add k 1
632v = div v 10
633if eq v 0
634break
635end
636end
637w = write 2 k_xf 0 len k_xf
638w = write 2 d sub 20 k k
639put d 0 10
640w = write 2 d 0 1
641exit 127
642end
643
644note reapstage: move an open stage file r to fd t (or check that a final dup3 landed on t); 'E' + errno
645note when it failed
646fn reapstage r long t int to void
647if ge r 0
648if eq r long t
649ret
650end
651z = dupfd int r t 524288
652if ge z 0
653ret
654end
655reapfail long z
656end
657reapfail r
658end
659
660note reapfail: a stage file did not open; 'E' + errno on the status pipe (staged at fd 63), nothing ran
661fn reapfail r long to void
662e = new uchar 5 69
663v = ulong neg r
664for i ulong 4
665put e add i 1 uchar and shr v mul i 8 255
666end
667w = write 63 e 0 5
668exit 127
669end