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