interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / om / root.om

2519 lines · 36639 bytes Raw Blame History
1note aien-supervise-root: the only part of aien-supervise that runs as root. Invoked through one
2note sudoers rule as `aien-supervise-root VERB NAME`, VERB run|stop|kill|check. It reads only
3note /etc/aien-supervise/services/NAME.svc, refuses unless that file, its directories and every
4note program and file it hands on are root-owned and not group/other-writable, and never runs a
5note service as root: it builds the per-service sandbox and execs
6note [nice] -> setpriv (uid/gid/groups, no_new_privs, empty bounding/inheritable/ambient caps)
7note -> bwrap (user, pid, ipc, uts, cgroup namespaces; optional net; read-only /usr and /etc,
8note declared binds, own /proc, minimal /dev, private /tmp; seccomp allowlist) -> program
9note after putting itself into /sys/fs/cgroup/aien-supervise/NAME (memory.max, cpu.max, pids.max)
10note and loading the service's nftables table (meta skuid). Exit 2 = refused; nothing ran.
11text tsdir "/etc/aien-supervise/services/"
12text tbwrap "/usr/bin/bwrap"
13text tsetpr "/usr/bin/setpriv"
14text tprlim "/usr/bin/prlimit"
15text tnice "/usr/bin/nice"
16text tnft "/usr/sbin/nft"
17text twatch "/usr/local/libexec/aien-supervise/aien-watch"
18text tcgr "/sys/fs/cgroup/aien-supervise"
19text tpasswd "/etc/passwd"
20text tgroup "/etc/group"
21text tpfd "/proc/self/fd/"
22text tself "/usr/local/libexec/aien-supervise/aien-supervise-root"
23text vrun "run"
24text vstop "stop"
25text vkill "kill"
26text vcheck "check"
27text kexec "exec"
28text ktype "type"
29text krst "restart"
30text kback "backoff"
31text kevery "every"
32text kdelay "delay"
33text ktmo "timeout"
34text kstop "stoptime"
35text kauto "autostart"
36text klmax "log-max"
37text klkeep "log-keep"
38text kenv "env"
39text kdir "dir"
40text kumask "umask"
41text knice "nice"
42text kwatch "watch"
43text kuser "user"
44text kgroup "group"
45text kgroups "groups"
46text kenvf "envfile"
47text ksec "secret"
48text kro "ro"
49text krw "rw"
50text khide "hide"
51text knet "net"
52text kallow "allow"
53text kdeny "deny"
54text klisten "listen"
55text kndef "net-default"
56text kmem "memory-max"
57text kswap "memory-swap-max"
58text ktasks "tasks-max"
59text kcpu "cpu-quota"
60text knofile "nofile"
61text kns "namespaces"
62text ksys "syscalls"
63text ksysd "syscalls-deny"
64text kserr "syscall-error"
65text kfam "families"
66text kmdwe "mdwe"
67text wsimple "simple"
68text woneshot "oneshot"
69text walways "always"
70text wonfail "on-failure"
71text wno "no"
72text wyes "yes"
73text whost "host"
74text wnone "none"
75text wfilter "filter"
76text waccept "accept"
77text wdrop "drop"
78text weperm "eperm"
79text wkill "kill"
80text gss "@system-service"
81text gmnt "@mount"
82text gns "@ns"
83text gpriv "@privileged"
84text gres "@resources"
85text funix "unix"
86text finet "inet"
87text finet6 "inet6"
88text fnetl "netlink"
89text pvar "/var/"
90text psrv "/srv/"
91text prun "/run/"
92text phome "/home"
93text proot "/root"
94text pproc "/proc"
95text psys "/sys"
96text pdev "/dev"
97text ptmp "/tmp"
98text pruser "/run/user"
99text pasup "/run/aien-supervise"
100text petc "/etc"
101text pusr "/usr"
102text prun1 "/run"
103text pcred "/run/credentials"
104text pcredd "/run/credentials/"
105text ldpre "LD_"
106text ecred "CREDENTIALS_DIRECTORY"
107text eprefix "aien-supervise-root: "
108text eline "line "
109text eusage "usage: aien-supervise-root run|stop|kill|check NAME"
110text enotroot "must run as root (sudo -n)"
111text ename "bad service name"
112text elayout "unsafe host layout (not root-owned, or group/other-writable): "
113text eread "cannot read service file: "
114text ekey "unknown key"
115text earity "wrong number of words"
116text evalue "bad value"
117text epath "bad or forbidden path"
118text eexec "exec program not root-owned/non-writable: "
119text euser "missing or unknown user, or uid outside 1..999"
120text egroup "unknown group, or privileged gid"
121text esys "syscalls line required"
122text eenvf "bad envfile: "
123text esecret "bad secret file (root-owned, mode 0600 or 0400, regular): "
124text ecg "cgroup setup failed: "
125text erunning "already running (cgroup populated)"
126text enft "nftables table failed to load"
127text eprl "prlimit failed"
128text eexecf "exec failed"
129text etoo "too many: "
130text ttbl "aiens_"
131text nft1 "table inet "
132text nft2 " {}\ndelete table inet "
133text nft3 "\ntable inet "
134text nft4 " {\n chain out {\n type filter hook output priority 0; policy accept;\n meta skuid "
135text nft5 " ct state established,related accept\n"
136text nft6 " meta skuid "
137text nft7 " ip daddr "
138text nft7b " ip6 daddr "
139text nft8 " accept\n"
140text nft9 " drop\n"
141text nft10 " meta l4proto { tcp, udp } th dport "
142text nft11 " }\n chain in {\n type filter hook input priority 0; policy accept;\n"
143text nft12 " tcp dport "
144text nft13 " ip saddr != { "
145text nft14 " } drop\n"
146text nft15 " meta nfproto ipv6 drop\n"
147text nft16 " }\n}\n"
148text tcom ", "
149text cprocs "/cgroup.procs"
150text cevents "/cgroup.events"
151text ckill "/cgroup.kill"
152text csub "/cgroup.subtree_control"
153text cmem "/memory.max"
154text cswap "/memory.swap.max"
155text cpids "/pids.max"
156text ccpu "/cpu.max"
157text cctl "+cpu +memory +pids"
158text cmax "max"
159text cperiod " 100000"
160text cpop "populated 1"
161text c1 "1"
162text b1 "--unshare-user"
163text b2 "--unshare-pid"
164text b3 "--unshare-ipc"
165text b4 "--unshare-uts"
166text b5 "--unshare-cgroup-try"
167text b6 "--unshare-net"
168text b7 "--disable-userns"
169text b8 "--die-with-parent"
170text b9 "--new-session"
171text b10 "--ro-bind"
172text b11 "--bind"
173text b12 "--symlink"
174text b13 "--proc"
175text b14 "--dev"
176text b15 "--tmpfs"
177text b16 "--perms"
178text b17 "--dir"
179text b18 "--chdir"
180text b19 "--setenv"
181text b20 "--seccomp"
182text b21 "--ro-bind-data"
183text b22 "--"
184text b23 "--args"
185text b24 "--ro-bind-try"
186text b25 "--remount-ro"
187text kps "/proc/sys /proc/sysrq-trigger /proc/irq /proc/bus /proc/fs /proc/acpi /proc/asound /proc/latency_stats /proc/timer_stats"
188text busr "/usr"
189text bubin "usr/bin"
190text bulib "usr/lib"
191text busbin "usr/sbin"
192text bbin "/bin"
193text blib "/lib"
194text bsbin "/sbin"
195text betc "/etc"
196text bproc "/proc"
197text bdev "/dev"
198text btmp "/tmp"
199text bvar "/var"
200text bvtmp "/var/tmp"
201text broot "/"
202text p1777 "1777"
203text p0755 "0755"
204text p0444 "0444"
205text p0000 "0000"
206text s1 "--reuid="
207text s2 "--regid="
208text s3 "--groups="
209text s4 "--clear-groups"
210text s5 "--no-new-privs"
211text s6 "--bounding-set=-all"
212text s7 "--inh-caps=-all"
213text s8 "--ambient-caps=-all"
214text tdashn "-n"
215text tdashf "-f"
216text r1 "--pid"
217text r2 "--rtprio=0:0"
218text r3 "--nofile="
219text tcolon ":"
220text tredact "<envfile value>"
221text tmemfd "aien-supervise"
222text oarg "arg: "
223text onft "nft:\n"
224text osec "seccomp instructions: "
225text oallow " allowed syscalls: "
226text ochain "chain: "
227global xb uchar 262144
228global sa uchar 1024
229global bp uchar 32768
230global rv ulong 64
231global gid ulong 32
232global stat4 uchar 4
233global pfd uchar 8
234note Slots of rv.
235const RUID ulong 0
236const RGID ulong 1
237const RNG ulong 2
238const RMEM ulong 3
239const RSWP ulong 4
240const RTSK ulong 5
241const RCPU ulong 6
242const RNOF ulong 7
243const RNIC ulong 8
244const RUMK ulong 9
245const RNET ulong 10
246const RNDF ulong 11
247const RNS ulong 12
248const RERR ulong 13
249const RMDW ulong 14
250const RFAM ulong 15
251const RXL ulong 16
252const RSYS ulong 17
253const RNS0 ulong 18
254const RNS1 ulong 19
255const RBPN ulong 20
256const RCHK ulong 21
257const RWAT ulong 22
258const RDIR ulong 23
259const RXN ulong 24
260const RFDA ulong 25
261const RFDS ulong 26
262const RSEC ulong 27
263const RCRD ulong 28
264const RUNL ulong 29
265const RGX ulong 30
266
267note --------------------------------------------------------------------- refusal
268
269fn efl to void
270oflush 2
271end
272
273fn refuse t view uchar * to int
274ostr eprefix
275ostr t
276ostr tnl
277efl
278ret 2
279end
280
281fn refpth t view uchar * to int
282ostr eprefix
283ostr t
284opth
285ostr tnl
286efl
287ret 2
288end
289
290fn refln t view uchar * to int
291ostr eprefix
292ostr eline
293onum long get gu GLNO
294ostr tsp
295ostr t
296ostr tnl
297efl
298ret 2
299end
300
301note ---------------------------------------------------------------- service file
302
303global nmb uchar 64
304global unb uchar 64
305
306fn pname to void
307for i ulong get gu GSV
308pb get nmb i
309end
310end
311
312fn svcpth to void
313pset
314pstr tsdir
315pname
316pstr tsvc
317end
318
319fn okname2 n ulong to bool
320if or eq n 0 gt n 32
321ret false
322end
323if eq get av 0 45
324ret false
325end
326for i ulong n
327c = get av i
328if not or or and ge c 97 le c 122 and ge c 48 le c 57 eq c 45
329ret false
330end
331end
332ret true
333end
334
335note Host layout: every program and directory the helper relies on is root-owned and not
336note group/other-writable (aien-run-exec standard), checked before anything else runs.
337fn layout1 t view uchar * kind ulong to bool
338pset
339pstr t
340if not rootok kind
341ret false
342end
343ret rootdirs
344end
345
346fn hostok to int
347if not layout1 tbwrap 1
348ret refpth elayout
349end
350if not layout1 tsetpr 1
351ret refpth elayout
352end
353if not layout1 tprlim 1
354ret refpth elayout
355end
356if not layout1 tnice 1
357ret refpth elayout
358end
359if not layout1 tnft 1
360ret refpth elayout
361end
362if not layout1 twatch 1
363ret refpth elayout
364end
365if not layout1 tself 1
366ret refpth elayout
367end
368if not layout1 tpasswd 1
369ret refpth elayout
370end
371if not layout1 tgroup 1
372ret refpth elayout
373end
374svcpth
375if not rootok 1
376ret refpth elayout
377end
378if not rootdirs
379ret refpth elayout
380end
381ret 0
382end
383
384note ------------------------------------------------------------- passwd and group
385
386fn rdx to long
387fd = popen ORDO 0
388if lt fd 0
389ret fd
390end
391n = ulong 0
392while true bound 100000
393r = read int fd xb n sub len xb n
394if le r 0
395break
396end
397n = add n ulong r
398if eq n len xb
399c = fclose int fd
400ret -27
401end
402end
403c = fclose int fd
404ret long n
405end
406
407note Compare the colon field xb[a..b) with word k of the current line.
408fn xweq a ulong b ulong k ulong to bool
409if ne sub b a wlen k
410ret false
411end
412s = get ws k
413for i ulong sub b a
414if ne get xb add a i get sf add s i
415ret false
416end
417end
418ret true
419end
420
421fn xnum a ulong b ulong to long
422if or eq a b gt sub b a 9
423ret -1
424end
425v = long 0
426p = a
427while lt p b bound 16
428c = get xb p
429if or lt c 48 gt c 57
430ret -1
431end
432v = add mul v 10 long sub c 48
433p = add p 1
434end
435ret v
436end
437
438fn xcolon p ulong e ulong to ulong
439q = p
440while lt q e bound 999999
441c = get xb q
442if or eq c 58 eq c 10
443ret q
444end
445q = add q 1
446end
447ret e
448end
449
450note Find the line of xb (n bytes) whose first field equals word k; returns its start or n.
451fn xfind n ulong k ulong to ulong
452p = ulong 0
453while lt p n bound 999999
454f = xcolon p n
455if xweq p f k
456ret p
457end
458q = p
459while lt q n bound 999999
460if eq get xb q 10
461break
462end
463q = add q 1
464end
465p = add q 1
466end
467ret n
468end
469
470note Field i (0-based) of the colon line at p as a number, or -1.
471fn xfield p ulong n ulong i ulong to long
472a = p
473for j ulong i
474a = add xcolon a n 1
475if gt a n
476ret -1
477end
478end
479b = xcolon a n
480ret xnum a b
481end
482
483fn lookuser k ulong to bool
484pset
485pstr tpasswd
486r = rdx
487if lt r 0
488ret false
489end
490n = ulong r
491p = xfind n k
492if ge p n
493ret false
494end
495u = xfield p n 2
496g = xfield p n 3
497if or lt u 1 gt u 999
498ret false
499end
500if lt g 100
501ret false
502end
503put rv RUID ulong u
504put rv RGID ulong g
505ret true
506end
507
508note Groups that are root in disguise are refused by name; any gid below 100 is refused.
509text gdocker "docker"
510text gsudo "sudo"
511text gadm "adm"
512text gdisk "disk"
513text glxd "lxd"
514text gkvm "kvm"
515text gshadow "shadow"
516text glibvirt "libvirt"
517text gsysj "systemd-journal"
518
519fn badgrp k ulong to bool
520if or or wis k gdocker wis k gsudo or wis k gadm wis k gdisk
521ret true
522end
523if or or wis k glxd wis k gkvm or wis k gshadow or wis k glibvirt wis k gsysj
524ret true
525end
526ret false
527end
528
529fn lookgrp k ulong to long
530if badgrp k
531ret -1
532end
533pset
534pstr tgroup
535r = rdx
536if lt r 0
537ret -1
538end
539n = ulong r
540p = xfind n k
541if ge p n
542ret -1
543end
544g = xfield p n 2
545if lt g 100
546ret -1
547end
548ret g
549end
550
551note ------------------------------------------------------------------- validation
552
553fn kis t view uchar * to bool
554ret and gt nw 0 wis 0 t
555end
556
557fn pfx k ulong t view uchar * to bool
558n = wlen k
559if lt n len t
560ret false
561end
562s = get ws k
563for i ulong len t
564if ne get sf add s i get t i
565ret false
566end
567end
568if eq n len t
569ret true
570end
571c = get sf add s len t
572ret or eq c 47 eq get t sub len t 1 47
573end
574
575fn badro k ulong to bool
576if or or pfx k phome pfx k proot or pfx k pproc pfx k psys
577ret true
578end
579if or or pfx k pdev pfx k ptmp or pfx k pruser pfx k pasup
580ret true
581end
582ret false
583end
584
585fn okrw k ulong to bool
586if not okpath k
587ret false
588end
589if badro k
590ret false
591end
592if not or or pfx k pvar pfx k psrv pfx k prun
593ret false
594end
595ret gt wlen k 5
596end
597
598fn addrch c uchar to bool
599if or and ge c 48 le c 57 and ge c 97 le c 102
600ret true
601end
602ret or or eq c 46 eq c 58 eq c 47
603end
604
605fn okaddr k ulong to bool
606n = wlen k
607if or eq n 0 gt n 64
608ret false
609end
610dots = false
611for i ulong n
612c = wch k i
613if not addrch c
614ret false
615end
616if or eq c 46 eq c 58
617dots = true
618end
619end
620ret dots
621end
622
623fn isv6 k ulong to bool
624for i ulong wlen k
625if eq wch k i 58
626ret true
627end
628end
629ret false
630end
631
632fn okport k ulong to bool
633v = wnum k
634ret and ge v 1 le v 65535
635end
636
637fn keych c uchar to bool
638if or and ge c 65 le c 90 and ge c 97 le c 122
639ret true
640end
641ret or and ge c 48 le c 57 eq c 95
642end
643
644fn isldpre s ulong e ulong to bool
645if lt sub e s 3
646ret false
647end
648a = eq get sf s 76
649b = eq get sf add s 1 68
650c = eq get sf add s 2 95
651ret and and a b c
652end
653
654fn okenvkey s ulong e ulong to bool
655if or eq s e gt sub e s 64
656ret false
657end
658p = s
659while lt p e bound 70
660if not keych get sf p
661ret false
662end
663p = add p 1
664end
665ret not isldpre s e
666end
667
668fn envkeyend k ulong to ulong
669s = get ws k
670e = get we k
671p = s
672while lt p e bound 999999
673if eq get sf p 61
674ret p
675end
676p = add p 1
677end
678ret e
679end
680
681fn numarg k ulong lo long hi long to bool
682v = wnum k
683ret and ge v lo le v hi
684end
685
686note A root-owned, not group/other-writable regular file (and its directories) named by word k.
687fn rootfile k ulong to bool
688pset
689pword k
690if not rootok 1
691ret false
692end
693ret rootdirs
694end
695
696note Validate the daemon keys (shared with aien-supervise) on the current line; 1 ok, 0 no
697note match, 2 error already reported.
698fn vdaemon to ulong
699if kis ktype
700if or ne nw 2 not or wis 1 wsimple wis 1 woneshot
701ret 2
702end
703ret 1
704end
705if kis krst
706if or ne nw 2 not or or wis 1 walways wis 1 wonfail wis 1 wno
707ret 2
708end
709ret 1
710end
711if kis kback
712if or ne nw 3 or not numarg 1 1 86400 not numarg 2 1 86400
713ret 2
714end
715ret 1
716end
717if or or kis kevery kis kdelay or kis ktmo kis kstop
718if or ne nw 2 not numarg 1 0 31536000
719ret 2
720end
721ret 1
722end
723if or kis klmax kis klkeep
724if or ne nw 2 not numarg 1 1 1099511627776
725ret 2
726end
727ret 1
728end
729if kis kauto
730if or ne nw 2 not or wis 1 wyes wis 1 wno
731ret 2
732end
733ret 1
734end
735ret 0
736end
737
738fn vexec to bool
739if lt nw 2
740ret false
741end
742if not okpath 1
743ret false
744end
745if not rootfile 1
746ret false
747end
748put rv RXL get gu GLNO
749ret true
750end
751
752fn vwatch to bool
753if ne nw 5
754ret false
755end
756if not okpath 1
757ret false
758end
759if or eq wlen 2 0 gt wlen 2 64
760ret false
761end
762put rv RWAT 1
763if not numarg 4 0 86400
764ret false
765end
766ret numarg 3 1 86400
767end
768
769fn vuser to bool
770if ne nw 2
771ret false
772end
773if not lookuser 1
774ret false
775end
776n = wlen 1
777if gt n 32
778ret false
779end
780for i ulong n
781put unb i wch 1 i
782end
783put rv RUNL n
784ret true
785end
786
787fn addgid g ulong to bool
788n = get rv RNG
789if ge n len gid
790ret false
791end
792put gid n g
793put rv RNG add n 1
794ret true
795end
796
797fn vgroups to bool
798if lt nw 2
799ret false
800end
801for i ulong sub nw 1
802g = lookgrp add i 1
803if lt g 0
804ret false
805end
806if not addgid ulong g
807ret false
808end
809end
810ret true
811end
812
813fn vgroup to bool
814if ne nw 2
815ret false
816end
817g = lookgrp 1
818if lt g 0
819ret false
820end
821put rv RGX ulong g
822ret true
823end
824
825note xb[a..b) equals text t.
826fn xeqt a ulong b ulong t view uchar * to bool
827if ne sub b a len t
828ret false
829end
830for i ulong len t
831if ne get xb add a i get t i
832ret false
833end
834end
835ret true
836end
837
838fn xbadgrp a ulong b ulong to bool
839if or or xeqt a b gdocker xeqt a b gsudo or xeqt a b gadm xeqt a b gdisk
840ret true
841end
842if or or xeqt a b glxd xeqt a b gkvm or xeqt a b gshadow or xeqt a b glibvirt xeqt a b gsysj
843ret true
844end
845ret false
846end
847
848note xb[a..b) equals the service user's name.
849fn xisuser a ulong b ulong to bool
850if ne sub b a get rv RUNL
851ret false
852end
853for i ulong sub b a
854if ne get xb add a i get unb i
855ret false
856end
857end
858ret true
859end
860
861fn lnend p ulong n ulong to ulong
862q = p
863while lt q n bound 999999
864if eq get xb q 10
865ret q
866end
867q = add q 1
868end
869ret n
870end
871
872note Is the user named in the member list (field 3) of the /etc/group line at p..e?
873fn ingrp p ulong e ulong to bool
874a = p
875for j ulong 3
876a = add xcolon a e 1
877if gt a e
878ret false
879end
880end
881while lt a e bound 99999
882b = a
883while and lt b e ne get xb b 44 bound 99999
884b = add b 1
885end
886if xisuser a b
887ret true
888end
889a = add b 1
890end
891ret false
892end
893
894note initgroups(3) done here so every supplementary group is checked: the user's member groups
895note from /etc/group are added; a privileged one (gid < 100 or a root-equivalent name) refuses.
896fn initgr to bool
897pset
898pstr tgroup
899r = rdx
900if lt r 0
901ret false
902end
903n = ulong r
904p = ulong 0
905while lt p n bound 99999
906e = lnend p n
907if ingrp p e
908f = xcolon p e
909g = xfield p e 2
910if or lt g 100 xbadgrp p f
911ret false
912end
913if not addgid ulong g
914ret false
915end
916end
917p = add e 1
918end
919ret true
920end
921
922fn vsecret to bool
923if ne nw 3
924ret false
925end
926if not okname get ws 1 get we 1
927ret false
928end
929if not okpath 2
930ret false
931end
932pset
933pword 2
934if not rootok 1
935ret false
936end
937if ne and stmode 63 0
938ret false
939end
940if not rootdirs
941ret false
942end
943put rv RSEC add get rv RSEC 1
944ret le get rv RSEC 8
945end
946
947fn vnet to bool
948if ne nw 2
949ret false
950end
951if wis 1 whost
952put rv RNET 0
953ret true
954end
955if wis 1 wnone
956put rv RNET 1
957ret true
958end
959if wis 1 wfilter
960put rv RNET 2
961ret true
962end
963ret false
964end
965
966fn vallow to bool
967if or lt nw 2 gt nw 3
968ret false
969end
970if not okaddr 1
971ret false
972end
973if eq nw 3
974ret okport 2
975end
976ret true
977end
978
979fn vlisten to bool
980if lt nw 3
981ret false
982end
983if not okport 1
984ret false
985end
986for i ulong sub nw 2
987if not okaddr add i 2
988ret false
989end
990end
991ret true
992end
993
994fn vsysg to bool
995if lt nw 2
996ret false
997end
998for i ulong sub nw 1
999k = add i 1
1000if not or or wis k gss wis k gmnt wis k gns
1001ret false
1002end
1003end
1004put rv RSYS 1
1005ret true
1006end
1007
1008fn vsysd to bool
1009if lt nw 2
1010ret false
1011end
1012for i ulong sub nw 1
1013k = add i 1
1014if not or wis k gpriv wis k gres
1015ret false
1016end
1017end
1018ret true
1019end
1020
1021fn vfam to bool
1022if lt nw 2
1023ret false
1024end
1025m = ulong 0
1026for i ulong sub nw 1
1027k = add i 1
1028if wis k funix
1029m = or m 1
1030else
1031if wis k finet
1032m = or m 2
1033else
1034if wis k finet6
1035m = or m 4
1036else
1037if wis k fnetl
1038m = or m 8
1039else
1040ret false
1041end
1042end
1043end
1044end
1045end
1046put rv RFAM m
1047ret true
1048end
1049
1050fn yesno to long
1051if ne nw 2
1052ret -1
1053end
1054if wis 1 wyes
1055ret 1
1056end
1057if wis 1 wno
1058ret 0
1059end
1060ret -1
1061end
1062
1063note Sandbox keys, part 1. Returns 1 ok, 0 no match, 2 bad value, 3 bad path.
1064fn vbox1 to ulong
1065if kis kexec
1066if vexec
1067ret 1
1068end
1069ret 3
1070end
1071if kis kenv
1072if ne nw 2
1073ret 2
1074end
1075e = envkeyend 1
1076if or not okenvkey get ws 1 e eq e get we 1
1077ret 2
1078end
1079ret 1
1080end
1081if kis kdir
1082if or ne nw 2 not okpath 1
1083ret 3
1084end
1085put rv RDIR get gu GLNO
1086ret 1
1087end
1088if kis kumask
1089v = woct 1
1090if or ne nw 2 or lt v 0 gt v 511
1091ret 2
1092end
1093put rv RUMK ulong v
1094ret 1
1095end
1096if kis knice
1097if or ne nw 2 not numarg 1 0 19
1098ret 2
1099end
1100put rv RNIC ulong wnum 1
1101ret 1
1102end
1103if kis kwatch
1104if vwatch
1105ret 1
1106end
1107ret 2
1108end
1109ret 0
1110end
1111
1112fn vbox2 to ulong
1113if kis kuser
1114if vuser
1115ret 1
1116end
1117ret 4
1118end
1119if kis kgroup
1120if vgroup
1121ret 1
1122end
1123ret 5
1124end
1125if kis kgroups
1126if vgroups
1127ret 1
1128end
1129ret 5
1130end
1131if kis kenvf
1132if or ne nw 2 or not okpath 1 not rootfile 1
1133ret 3
1134end
1135ret 1
1136end
1137if kis ksec
1138if vsecret
1139ret 1
1140end
1141ret 3
1142end
1143if kis kro
1144if or ne nw 2 or not okpath 1 badro 1
1145ret 3
1146end
1147ret 1
1148end
1149if kis krw
1150if or ne nw 2 not okrw 1
1151ret 3
1152end
1153ret 1
1154end
1155if kis khide
1156if or ne nw 2 not okpath 1
1157ret 3
1158end
1159ret 1
1160end
1161ret 0
1162end
1163
1164fn vbox3 to ulong
1165if kis knet
1166if vnet
1167ret 1
1168end
1169ret 2
1170end
1171if or kis kallow kis kdeny
1172if vallow
1173ret 1
1174end
1175ret 2
1176end
1177if kis klisten
1178if vlisten
1179ret 1
1180end
1181ret 2
1182end
1183if kis kndef
1184if or ne nw 2 not or wis 1 waccept wis 1 wdrop
1185ret 2
1186end
1187put rv RNDF 0
1188if wis 1 wdrop
1189put rv RNDF 1
1190end
1191ret 1
1192end
1193if kis kfam
1194if vfam
1195ret 1
1196end
1197ret 2
1198end
1199ret 0
1200end
1201
1202fn bigarg k ulong to long
1203if ne nw 2
1204ret -1
1205end
1206ret wnum 1
1207end
1208
1209fn vbox4 to ulong
1210if or or kis kmem kis kswap or kis ktasks kis knofile
1211v = bigarg 1
1212if or lt v 0 and eq v 0 not kis kswap
1213ret 2
1214end
1215if kis kmem
1216put rv RMEM ulong v
1217end
1218if kis kswap
1219put rv RSWP add ulong v 1
1220end
1221if kis ktasks
1222put rv RTSK ulong v
1223end
1224if kis knofile
1225put rv RNOF ulong v
1226end
1227ret 1
1228end
1229if kis kcpu
1230if or ne nw 2 not numarg 1 1 100000
1231ret 2
1232end
1233put rv RCPU ulong wnum 1
1234ret 1
1235end
1236ret 0
1237end
1238
1239fn vbox5 to ulong
1240if kis kns
1241v = yesno
1242if lt v 0
1243ret 2
1244end
1245put rv RNS ulong v
1246ret 1
1247end
1248if kis kmdwe
1249v = yesno
1250if lt v 0
1251ret 2
1252end
1253put rv RMDW ulong v
1254ret 1
1255end
1256if kis ksys
1257if vsysg
1258ret 1
1259end
1260ret 2
1261end
1262if kis ksysd
1263if vsysd
1264ret 1
1265end
1266ret 2
1267end
1268if kis kserr
1269if or ne nw 2 not or wis 1 weperm wis 1 wkill
1270ret 2
1271end
1272put rv RERR 0
1273if wis 1 weperm
1274put rv RERR 1
1275end
1276ret 1
1277end
1278ret 0
1279end
1280
1281fn vline to ulong
1282r = vdaemon
1283if ne r 0
1284ret r
1285end
1286r = vbox1
1287if ne r 0
1288ret r
1289end
1290r = vbox2
1291if ne r 0
1292ret r
1293end
1294r = vbox3
1295if ne r 0
1296ret r
1297end
1298r = vbox4
1299if ne r 0
1300ret r
1301end
1302ret vbox5
1303end
1304
1305fn defaults to void
1306for i ulong len rv
1307put rv i 0
1308end
1309put rv RMDW 1
1310put rv RUID 0
1311end
1312
1313fn vfail r ulong to int
1314if eq r 0
1315ret refln ekey
1316end
1317if eq r 3
1318ret refln epath
1319end
1320if eq r 4
1321ret refln euser
1322end
1323if eq r 5
1324ret refln egroup
1325end
1326ret refln evalue
1327end
1328
1329note Validate the whole file. 0 ok, else 2 (reported).
1330fn validate to int
1331defaults
1332put gu GLNO 0
1333p = ulong 0
1334users = ulong 0
1335while lt p get gu GSF bound 100000
1336p = lnext p
1337if ne get gu GOVF 0
1338ret refln etoo
1339end
1340if gt nw 0
1341r = vline
1342if ne r 1
1343ret vfail r
1344end
1345if kis kuser
1346users = add users 1
1347end
1348end
1349end
1350if or ne users 1 eq get rv RUID 0
1351ret refuse euser
1352end
1353if gt get rv RGX 0
1354put rv RGID get rv RGX
1355end
1356if not initgr
1357ret refuse egroup
1358end
1359if eq get rv RXL 0
1360ret refuse eexec
1361end
1362if eq get rv RSYS 0
1363ret refuse esys
1364end
1365ret 0
1366end
1367
1368note -------------------------------------------------------------- nftables script
1369
1370fn tblname to void
1371abstr ttbl
1372for i ulong get gu GSV
1373c = get nmb i
1374if eq c 45
1375c = 95
1376end
1377ab1 c
1378end
1379end
1380
1381fn skuid to void
1382abstr nft6
1383abnum get rv RUID
1384end
1385
1386fn nftrule k ulong verdict view uchar * to void
1387skuid
1388if isv6 1
1389abstr nft7b
1390else
1391abstr nft7
1392end
1393abword 1
1394if eq nw 3
1395abstr nft10
1396abword 2
1397end
1398abstr verdict
1399end
1400
1401fn nftout to void
1402p = ulong 0
1403while lt p get gu GSF bound 100000
1404p = lnext p
1405if kis kallow
1406nftrule 1 nft8
1407end
1408end
1409p = 0
1410while lt p get gu GSF bound 100000
1411p = lnext p
1412if kis kdeny
1413nftrule 1 nft9
1414end
1415end
1416if eq get rv RNDF 1
1417skuid
1418abstr nft9
1419end
1420end
1421
1422fn nftlisten to void
1423v6 = false
1424for i ulong sub nw 2
1425if isv6 add i 2
1426v6 = true
1427end
1428end
1429abstr nft12
1430abword 1
1431abstr nft13
1432first = true
1433for i ulong sub nw 2
1434k = add i 2
1435if not isv6 k
1436if not first
1437abstr tcom
1438end
1439abword k
1440first = false
1441end
1442end
1443abstr nft14
1444if not v6
1445abstr nft12
1446abword 1
1447abstr nft15
1448end
1449end
1450
1451note The nft script for this service in ab: the table is created empty, deleted and, for net
1452note filter, recreated in one transaction, so a reload replaces the old rules atomically.
1453fn nftscript to void
1454abreset
1455abstr nft1
1456tblname
1457abstr nft2
1458tblname
1459if eq get rv RNET 2
1460abstr nft3
1461tblname
1462abstr nft4
1463abnum get rv RUID
1464abstr nft5
1465nftout
1466abstr nft11
1467p = ulong 0
1468while lt p get gu GSF bound 100000
1469p = lnext p
1470if kis klisten
1471nftlisten
1472end
1473end
1474abstr nft16
1475else
1476abstr tnl
1477end
1478end
1479
1480note ------------------------------------------------------------------ child runner
1481
1482fn mfd to long
1483ret memfd tmemfd 0 len tmemfd 0
1484end
1485
1486fn abtofd to long
1487fd = mfd
1488if lt fd 0
1489ret fd
1490end
1491w = write int fd ab 0 get gu GAB
1492if ne w long get gu GAB
1493ret -5
1494end
1495s = fseek int fd 0 0
1496ret fd
1497end
1498
1499fn bptofd to long
1500fd = mfd
1501if lt fd 0
1502ret fd
1503end
1504n = mul get rv RBPN 8
1505w = write int fd bp 0 n
1506if ne w long n
1507ret -5
1508end
1509s = fseek int fd 0 0
1510ret fd
1511end
1512
1513note Run the exec packet pk as a child with stdin from /dev/null; its exit status, or -1.
1514text tdevnull "/dev/null"
1515fn runpk to long
1516pid = fork
1517if lt pid 0
1518ret -1
1519end
1520if eq pid 0
1521pset
1522pstr tdevnull
1523nfd = popen ORDO 0
1524if ge nfd 0
1525d = dupfd int nfd 0 0
1526end
1527r = execprog pk 0 get gu GPK
1528exit 127
1529end
1530r = waitpid int pid stat4 0 4 0
1531if ne r pid
1532ret -1
1533end
1534ret long get stat4 1
1535end
1536
1537fn nftapply to bool
1538nftscript
1539fd = abtofd
1540if lt fd 0
1541ret false
1542end
1543pkreset
1544pkstr tnft
1545pkstr tdashf
1546pset
1547pstr tpfd
1548pnum ulong fd
1549pkpth
1550r = runpk
1551c = fclose int fd
1552ret eq r 0
1553end
1554
1555fn prlself to bool
1556pkreset
1557pkstr tprlim
1558pkstr r1
1559pset
1560pnum ulong getpid
1561pkpth
1562pkstr r2
1563if gt get rv RNOF 0
1564pset
1565pstr r3
1566pnum get rv RNOF
1567pstr tcolon
1568pnum get rv RNOF
1569pkpth
1570end
1571ret eq runpk 0
1572end
1573
1574note ------------------------------------------------------------------------ cgroup
1575
1576fn cgdir to void
1577pset
1578pstr tcgr
1579end
1580
1581fn cgleaf to void
1582cgdir
1583pb 47
1584pname
1585end
1586
1587note Write ab's bytes to the file in pth. true on success.
1588fn wrfile to bool
1589fd = popen 524289 0
1590if lt fd 0
1591ret false
1592end
1593w = write int fd ab 0 get gu GAB
1594c = fclose int fd
1595ret eq w long get gu GAB
1596end
1597
1598fn cgset f view uchar * v ulong to bool
1599cgleaf
1600pstr f
1601abreset
1602if eq v 0
1603abstr cmax
1604else
1605abnum v
1606end
1607ret wrfile
1608end
1609
1610fn cgpopulated to bool
1611cgleaf
1612pstr cevents
1613r = rdx
1614if le r 0
1615ret false
1616end
1617n = ulong r
1618for i ulong n
1619if le add i len cpop n
1620ok = true
1621for j ulong len cpop
1622if ne get xb add i j get cpop j
1623ok = false
1624end
1625end
1626if ok
1627ret true
1628end
1629end
1630end
1631ret false
1632end
1633
1634fn cgsetup to int
1635cgdir
1636r = makedir pth 0 plen 493
1637if and lt r 0 ne r -17
1638ret refpth ecg
1639end
1640cgdir
1641pstr csub
1642abreset
1643abstr cctl
1644if not wrfile
1645ret refpth ecg
1646end
1647cgleaf
1648r = makedir pth 0 plen 493
1649if and lt r 0 ne r -17
1650ret refpth ecg
1651end
1652if cgpopulated
1653ret refuse erunning
1654end
1655if not cgset cmem get rv RMEM
1656ret refpth ecg
1657end
1658if not cgset cpids get rv RTSK
1659ret refpth ecg
1660end
1661cgleaf
1662pstr ccpu
1663abreset
1664if eq get rv RCPU 0
1665abstr cmax
1666else
1667abnum mul get rv RCPU 1000
1668end
1669abstr cperiod
1670if not wrfile
1671ret refpth ecg
1672end
1673if gt get rv RSWP 0
1674if not cgset cswap sub get rv RSWP 1
1675ret refpth ecg
1676end
1677end
1678cgleaf
1679pstr cprocs
1680abreset
1681abnum ulong getpid
1682if not wrfile
1683ret refpth ecg
1684end
1685ret 0
1686end
1687
1688note ------------------------------------------------------------------- seccomp cBPF
1689
1690fn bi code ulong jt ulong jf ulong k ulong to void
1691n = get rv RBPN
1692o = mul n 8
1693put bp o uchar and code 255
1694put bp add o 1 uchar shr code 8
1695put bp add o 2 uchar jt
1696put bp add o 3 uchar jf
1697put bp add o 4 uchar and k 255
1698put bp add o 5 uchar and shr k 8 255
1699put bp add o 6 uchar and shr k 16 255
1700put bp add o 7 uchar shr k 24
1701put rv RBPN add n 1
1702end
1703
1704fn ldnr to void
1705bi 32 0 0 0
1706end
1707
1708fn ldarg i ulong to void
1709bi 32 0 0 add 16 mul i 8
1710end
1711
1712fn rdeny to ulong
1713if eq get rv RERR 1
1714ret 327681
1715end
1716ret 2147483648
1717end
1718
1719fn retk k ulong to void
1720bi 6 0 0 k
1721end
1722
1723note Mark the syscall numbers of a group text (space-separated decimals) with v.
1724fn setnums t view uchar * v uchar to void
1725x = ulong 0
1726have = false
1727for i ulong len t
1728c = get t i
1729if and ge c 48 le c 57
1730x = add mul x 10 ulong sub c 48
1731have = true
1732else
1733if and have lt x 1024
1734put sa x v
1735end
1736x = 0
1737have = false
1738end
1739end
1740if and have lt x 1024
1741put sa x v
1742end
1743end
1744
1745fn sysgroups to void
1746for i ulong 1024
1747put sa i 0
1748end
1749p = ulong 0
1750while lt p get gu GSF bound 100000
1751p = lnext p
1752if kis ksys
1753for i ulong sub nw 1
1754k = add i 1
1755if wis k gss
1756setnums sysss 1
1757end
1758if wis k gmnt
1759setnums sysmnt 1
1760end
1761if wis k gns
1762setnums sysns 1
1763end
1764end
1765end
1766end
1767p = 0
1768while lt p get gu GSF bound 100000
1769p = lnext p
1770if kis ksysd
1771for i ulong sub nw 1
1772k = add i 1
1773if wis k gpriv
1774setnums syspriv 0
1775end
1776if wis k gres
1777setnums sysres 0
1778end
1779end
1780end
1781end
1782if eq get rv RNS 0
1783put sa 97 0
1784put sa 268 0
1785end
1786end
1787
1788note if nr == N and (arg & mask) != 0: return e.
1789fn bmask n ulong a ulong m ulong e ulong to void
1790bi 21 0 3 n
1791ldarg a
1792bi 69 0 1 m
1793retk e
1794ldnr
1795end
1796
1797note if nr == N and (arg & m) == m: return e.
1798fn ball n ulong a ulong m ulong e ulong to void
1799bi 21 0 4 n
1800ldarg a
1801bi 84 0 0 m
1802bi 21 0 1 m
1803retk e
1804ldnr
1805end
1806
1807fn bonly n ulong e ulong to void
1808bi 21 0 1 n
1809retk e
1810end
1811
1812note personality: only PER_LINUX (0) and the query 0xffffffff (LockPersonality).
1813fn bpers to void
1814bi 21 0 4 92
1815ldarg 0
1816bi 21 2 0 0
1817bi 21 1 0 4294967295
1818retk 327681
1819ldnr
1820end
1821
1822note socket/socketpair: the family must be one of the families line (RestrictAddressFamilies).
1823fn bfam1 n ulong to void
1824m = get rv RFAM
1825k = ulong 0
1826for i ulong 4
1827if ne and shr m i 1 0
1828k = add k 1
1829end
1830end
1831bi 21 0 add k 2 n
1832ldarg 0
1833j = ulong 0
1834if ne and m 1 0
1835bi 21 sub k j 0 1
1836j = add j 1
1837end
1838if ne and m 2 0
1839bi 21 sub k j 0 2
1840j = add j 1
1841end
1842if ne and m 4 0
1843bi 21 sub k j 0 10
1844j = add j 1
1845end
1846if ne and m 8 0
1847bi 21 sub k j 0 16
1848j = add j 1
1849end
1850retk 327777
1851ldnr
1852end
1853
1854note sched_setscheduler with SCHED_FIFO, SCHED_RR or SCHED_DEADLINE (RestrictRealtime).
1855fn brt to void
1856bi 21 0 7 119
1857ldarg 1
1858bi 84 0 0 1073741823
1859bi 21 3 0 1
1860bi 21 2 0 2
1861bi 21 1 0 6
1862bi 5 0 0 1
1863retk 327681
1864ldnr
1865end
1866
1867fn seccomp to void
1868put rv RBPN 0
1869sysgroups
1870bi 32 0 0 4
1871bi 21 1 0 3221225655
1872retk 2147483648
1873ldnr
1874bpers
1875brt
1876bonly 274 327681
1877note RestrictSUIDSGID: no S_ISUID/S_ISGID mode bits on chmod, open and mknod families.
1878bmask 52 1 3072 327681
1879bmask 53 2 3072 327681
1880bmask 452 2 3072 327681
1881bmask 56 3 3072 327681
1882bmask 34 2 3072 327681
1883bmask 33 2 3072 327681
1884bonly 437 327718
1885if eq get rv RMDW 1
1886ball 222 2 6 327681
1887bmask 226 2 4 327681
1888bmask 288 2 4 327681
1889bmask 196 2 32768 327681
1890end
1891if gt get rv RFAM 0
1892bfam1 198
1893bfam1 199
1894end
1895if eq get rv RNS 0
1896bmask 220 0 2114060416 327681
1897bonly 435 327718
1898end
1899for i ulong 1024
1900if eq get sa i 1
1901bi 21 0 1 i
1902retk 2147418112
1903end
1904end
1905retk rdeny
1906end
1907
1908fn nallowed to ulong
1909c = ulong 0
1910for i ulong 1024
1911if eq get sa i 1
1912c = add c 1
1913end
1914end
1915ret c
1916end
1917
1918note ------------------------------------------------------------------ bwrap arguments
1919
1920fn abline to void
1921abword 1
1922abz
1923end
1924
1925note --perms 0755 --dir for every directory above the path in word 1 (bwrap otherwise makes
1926note them 0700, which the unprivileged service cannot cross).
1927fn abparents to void
1928s = get ws 1
1929e = get we 1
1930p = add s 1
1931while lt p e bound 4096
1932if eq get sf p 47
1933abarg b16
1934abarg p0755
1935abarg b17
1936absf s p
1937abz
1938end
1939p = add p 1
1940end
1941end
1942
1943fn abbinds to void
1944p = ulong 0
1945while lt p get gu GSF bound 100000
1946p = lnext p
1947if or kis kro kis krw
1948abparents
1949if kis kro
1950abarg b10
1951else
1952abarg b11
1953end
1954abline
1955abline
1956end
1957end
1958p = 0
1959while lt p get gu GSF bound 100000
1960p = lnext p
1961if kis khide
1962abparents
1963abarg b16
1964abarg p0000
1965abarg b15
1966abline
1967end
1968end
1969end
1970
1971fn abenv to void
1972p = ulong 0
1973while lt p get gu GSF bound 100000
1974p = lnext p
1975if kis kenv
1976e = envkeyend 1
1977abarg b19
1978absf get ws 1 e
1979abz
1980absf add e 1 get we 1
1981abz
1982end
1983end
1984end
1985
1986note envfile: KEY=VALUE lines of a root-owned file, '#' comments, optional matching quotes.
1987fn efline a ulong b ulong chk bool to bool
1988while and lt a b eq get xb a 32 bound 4096
1989a = add a 1
1990end
1991if or eq a b eq get xb a 35
1992ret true
1993end
1994q = a
1995while lt q b bound 4096
1996if eq get xb q 61
1997break
1998end
1999q = add q 1
2000end
2001if or eq q b eq q a
2002ret false
2003end
2004abarg b19
2005for i ulong sub q a
2006ab1 get xb add a i
2007end
2008abz
2009v = add q 1
2010w = b
2011if ge sub w v 2
2012c = get xb v
2013if and or eq c 34 eq c 39 eq get xb sub w 1 c
2014v = add v 1
2015w = sub w 1
2016end
2017end
2018if chk
2019abstr tredact
2020else
2021for i ulong sub w v
2022ab1 get xb add v i
2023end
2024end
2025abz
2026ret true
2027end
2028
2029fn abenvf chk bool to int
2030p = ulong 0
2031while lt p get gu GSF bound 100000
2032p = lnext p
2033if kis kenvf
2034pset
2035pword 1
2036r = rdx
2037if lt r 0
2038ret refpth eenvf
2039end
2040n = ulong r
2041a = ulong 0
2042while lt a n bound 100000
2043b = a
2044while lt b n bound 999999
2045if eq get xb b 10
2046break
2047end
2048b = add b 1
2049end
2050if not efline a b chk
2051ret refpth eenvf
2052end
2053a = add b 1
2054end
2055end
2056end
2057ret 0
2058end
2059
2060note secret NAME PATH: the root-only file is copied into a memfd that bwrap binds read-only at
2061note /run/credentials/NAME inside this service's mount namespace (ImportCredential's shape).
2062fn secone chk bool to int
2063pset
2064pword 2
2065r = rdx
2066if lt r 0
2067ret refpth esecret
2068end
2069fd = long 99
2070if not chk
2071fd = mfd
2072if lt fd 0
2073ret refpth esecret
2074end
2075w = write int fd xb 0 ulong r
2076s = fseek int fd 0 0
2077end
2078abarg b16
2079abarg p0444
2080abarg b21
2081abnum ulong fd
2082abz
2083abstr pcredd
2084abword 1
2085abz
2086put rv RCRD 1
2087ret 0
2088end
2089
2090fn absecrets chk bool to int
2091if gt get rv RSEC 0
2092abarg b16
2093abarg p0755
2094abarg b17
2095abarg prun1
2096abarg b16
2097abarg p0755
2098abarg b17
2099abarg pcred
2100end
2101p = ulong 0
2102while lt p get gu GSF bound 100000
2103p = lnext p
2104if kis ksec
2105r = secone chk
2106if ne r 0
2107ret r
2108end
2109end
2110end
2111if eq get rv RCRD 1
2112abarg b19
2113abarg ecred
2114abarg pcred
2115end
2116ret 0
2117end
2118
2119fn kpsone a ulong e ulong to void
2120abarg b24
2121for r ulong 2
2122for i ulong sub e a
2123ab1 get kps add a i
2124end
2125abz
2126end
2127end
2128
2129note ProtectKernelTunables: /proc/sys and the other kernel knobs of the fresh /proc are bound
2130note read-only over themselves (--ro-bind-try: some are absent on this kernel).
2131fn prockt to void
2132a = ulong 0
2133for i ulong add len kps 1
2134sp = true
2135if lt i len kps
2136sp = eq get kps i 32
2137end
2138if sp
2139kpsone a i
2140a = add i 1
2141end
2142end
2143end
2144
2145fn abfixed to void
2146abarg b1
2147abarg b2
2148abarg b3
2149abarg b4
2150abarg b5
2151if eq get rv RNET 1
2152abarg b6
2153end
2154if eq get rv RNS 0
2155abarg b7
2156end
2157abarg b8
2158abarg b9
2159abarg b10
2160abarg busr
2161abarg busr
2162abarg b12
2163abarg bubin
2164abarg bbin
2165abarg b12
2166abarg bulib
2167abarg blib
2168abarg b12
2169abarg busbin
2170abarg bsbin
2171abarg b10
2172abarg betc
2173abarg betc
2174abarg b13
2175abarg bproc
2176prockt
2177abarg b14
2178abarg bdev
2179abarg b16
2180abarg p1777
2181abarg b15
2182abarg btmp
2183abarg b16
2184abarg p0755
2185abarg b17
2186abarg bvar
2187abarg b16
2188abarg p1777
2189abarg b15
2190abarg bvtmp
2191end
2192
2193note bwrap stops reading an --args file at "--" and drops what follows, so the command goes in
2194note the exec packet itself: [nice] setpriv ... -- bwrap --args FD -- [aien-watch D S P] PROG ARGS.
2195fn pkcmd to bool
2196pkstr b22
2197p = ulong 0
2198while lt p get gu GSF bound 100000
2199p = lnext p
2200if kis kwatch
2201pkstr twatch
2202pkword 1
2203pkword 2
2204pkword 3
2205pkword 4
2206end
2207end
2208p = 0
2209while lt p get gu GSF bound 100000
2210p = lnext p
2211if kis kexec
2212for i ulong sub nw 1
2213if not pkroom wlen add i 1
2214ret false
2215end
2216pkword add i 1
2217end
2218end
2219end
2220ret true
2221end
2222
2223fn abchdir to void
2224abarg b18
2225if eq get rv RDIR 0
2226abarg broot
2227ret
2228end
2229p = ulong 0
2230while lt p get gu GSF bound 100000
2231p = lnext p
2232if kis kdir
2233abline
2234end
2235end
2236end
2237
2238note Build the complete bwrap argument list in ab. chk: dry run (no secrets read, no memfds).
2239fn bwargs chk bool sfd long to int
2240abreset
2241abfixed
2242abbinds
2243r = absecrets chk
2244if ne r 0
2245ret r
2246end
2247abchdir
2248abenv
2249r = abenvf chk
2250if ne r 0
2251ret r
2252end
2253note ProtectSystem=strict: the sandbox's own root (where bwrap made /var, /opt, ...) is remounted
2254note read-only last; the declared rw binds and the private /tmp and /var/tmp stay writable.
2255abarg b25
2256abarg broot
2257abarg b20
2258abnum ulong sfd
2259abz
2260ret 0
2261end
2262
2263note ------------------------------------------------------------------- exec chain
2264
2265fn grpcsv to void
2266for i ulong get rv RNG
2267if gt i 0
2268pb 44
2269end
2270pnum get gid i
2271end
2272end
2273
2274fn chain afd long to bool
2275pkreset
2276if gt get rv RNIC 0
2277pkstr tnice
2278pkstr tdashn
2279pset
2280pnum get rv RNIC
2281pkpth
2282end
2283pkstr tsetpr
2284pset
2285pstr s1
2286pnum get rv RUID
2287pkpth
2288pset
2289pstr s2
2290pnum get rv RGID
2291pkpth
2292if gt get rv RNG 0
2293pset
2294pstr s3
2295grpcsv
2296pkpth
2297else
2298pkstr s4
2299end
2300pkstr s5
2301pkstr s6
2302pkstr s7
2303pkstr s8
2304pkstr b22
2305pkstr tbwrap
2306pkstr b23
2307pset
2308pnum ulong afd
2309pkpth
2310ret pkcmd
2311end
2312
2313note ---------------------------------------------------------------------- verbs
2314
2315fn loadsvc to int
2316r = hostok
2317if ne r 0
2318ret r
2319end
2320svcpth
2321n = rdfile
2322if lt n 0
2323ret refpth eread
2324end
2325if gt n 65000
2326ret refpth eread
2327end
2328ret validate
2329end
2330
2331fn docheck to int
2332ostr ochain
2333oflush 1
2334if not chain 3
2335ret refuse etoo
2336end
2337pkdump
2338nftscript
2339ostr onft
2340for i ulong get gu GAB
2341ob1 get ab i
2342end
2343oflush 1
2344seccomp
2345ostr osec
2346onum long get rv RBPN
2347ostr oallow
2348onum long nallowed
2349ostr tnl
2350oflush 1
2351r = bwargs true 4
2352if ne r 0
2353ret r
2354end
2355abdump
2356ret 0
2357end
2358
2359fn pkdump to void
2360o = ulong 2
2361for i ulong get gu GPKN
2362n = or ulong get pk o shl ulong get pk add o 1 8
2363for j ulong n
2364ob1 get pk add add o 2 j
2365end
2366ob1 32
2367o = add add o 2 n
2368end
2369ostr tnl
2370oflush 1
2371end
2372
2373fn abdump to void
2374for i ulong get gu GAB
2375c = get ab i
2376if eq c 0
2377ostr tnl
2378else
2379ob1 c
2380end
2381if gt get gu GOB 60000
2382oflush 1
2383end
2384end
2385oflush 1
2386end
2387
2388fn dorun to int
2389r = cgsetup
2390if ne r 0
2391ret r
2392end
2393if not nftapply
2394ret refuse enft
2395end
2396if not prlself
2397ret refuse eprl
2398end
2399m = umask int get rv RUMK
2400if eq get rv RUMK 0
2401m = umask 18
2402end
2403seccomp
2404sfd = bptofd
2405if lt sfd 0
2406ret refuse eexecf
2407end
2408r = bwargs false sfd
2409if ne r 0
2410ret r
2411end
2412afd = abtofd
2413if lt afd 0
2414ret refuse eexecf
2415end
2416if not chain afd
2417ret refuse etoo
2418end
2419x = execprog pk 0 get gu GPK
2420ret refuse eexecf
2421end
2422
2423fn dostop sig int to int
2424cgleaf
2425pstr cprocs
2426r = rdx
2427if lt r 0
2428ret 0
2429end
2430n = ulong r
2431v = long 0
2432have = false
2433for i ulong n
2434c = get xb i
2435if and ge c 48 le c 57
2436v = add mul v 10 long sub c 48
2437have = true
2438else
2439if and have not isbwrap v
2440k = killpid int v sig
2441end
2442v = 0
2443have = false
2444end
2445end
2446ret 0
2447end
2448
2449text texe "/exe"
2450
2451note bwrap's own processes (namespace monitor and init) run as the service user too; stop sends
2452note the signal past them to the service, whose exit then ends the sandbox.
2453fn isbwrap pid long to bool
2454pset
2455pstr tproc
2456pnum ulong pid
2457pstr texe
2458r = readlnk pth 0 plen ebuf 0 len ebuf
2459if ne r long len tbwrap
2460ret false
2461end
2462for i ulong len tbwrap
2463if ne get ebuf i get tbwrap i
2464ret false
2465end
2466end
2467ret true
2468end
2469
2470fn dokill to int
2471cgleaf
2472pstr ckill
2473abreset
2474abstr c1
2475w = wrfile
2476for i ulong 100
2477if not cgpopulated
2478ret 0
2479end
2480s = sleepns 50000000
2481end
2482ret 1
2483end
2484
2485fn main to int
2486if or ne getuid 0 ne geteuid 0
2487ret refuse enotroot
2488end
2489if ne argcnt 3
2490ret refuse eusage
2491end
2492n = arg 2
2493if not okname2 n
2494ret refuse ename
2495end
2496for i ulong n
2497put nmb i get av i
2498end
2499put gu GSV n
2500v = arg 1
2501r = loadsvc
2502if ne r 0
2503ret r
2504end
2505v = arg 1
2506if aveq v vrun
2507ret dorun
2508end
2509if aveq v vcheck
2510ret docheck
2511end
2512if aveq v vstop
2513ret dostop 15
2514end
2515if aveq v vkill
2516ret dokill
2517end
2518ret refuse eusage
2519end