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