spark-connector
aien-dev/spark-connector
Brought by drakestapleton
spark-connector / runner / 30cell.om
| 1 | note runner/30cell.om -- the cell: every byte of brought code runs only through omega-mv execpure. |
| 2 | note A stage forks a child that opens its three stage files onto descriptors 0..2 and calls |
| 3 | note execpure on one program. execpure (OMEGESE.md, word 198) closes every descriptor at or above 3, |
| 4 | note pins the program as descriptor 3, restricts the process with Landlock (read and execute of |
| 5 | note that program only), PR_SET_NO_NEW_PRIVS, seven prlimits (CPU 1 s soft / 2 s hard, file size |
| 6 | note 1 MiB, stack 8 MiB, no core, no new process, 4 descriptors, 128 MiB address space) and a |
| 7 | note seccomp filter that allows only read(0), write(1|2), exit, exit_group and execveat of the |
| 8 | note pinned program (everything else EPERM, a non-AArch64 call kills), then executes it with an |
| 9 | note empty environment. The parent kills the stage's process group at the 3 s wall clock. |
| 10 | note Both stages run in the cell: the compiler too. |
| 11 | note When the cell cannot start (a stage file does not open, or execpure itself fails) the child |
| 12 | note writes "E" and the errno to its stage stderr file and exits 125 (r_kfail, r_cfail), so "the |
| 13 | note cell could not start" is never mistaken for a program's own exit status. |
| 14 | |
| 15 | text rtomc "bin/omc" |
| 16 | text rtsrc "source.om" |
| 17 | text rtcout "compiler.out" |
| 18 | text rtcerr "compile.err" |
| 19 | text rtelf "part.elf" |
| 20 | text rtsin "stdin" |
| 21 | text rtsout "stdout" |
| 22 | text rtserr "stderr" |
| 23 | const rwall ulong 3000000000 |
| 24 | |
| 25 | note r_kidio: open the stage file nm onto descriptor t (t 0: read; 1, 2: new file 0600); on failure |
| 26 | note report the errno as a cell failure (r_kfail) |
| 27 | fn r_kidio nm view uchar * t int to void |
| 28 | n = r_jpath nm |
| 29 | fl = rfonew |
| 30 | if eq t 0 |
| 31 | fl = rford |
| 32 | end |
| 33 | fd = fopen rph 0 n fl 384 |
| 34 | if lt fd 0 |
| 35 | r_kfio t fd |
| 36 | end |
| 37 | r = dupfd int fd t 0 |
| 38 | if lt r 0 |
| 39 | r_kfio t r |
| 40 | end |
| 41 | if ne fd long t |
| 42 | x = fclose int fd |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | note r_kfio: a stage file could not be set up. Without its stderr file the child writes nothing |
| 47 | note (descriptor 2 is still the runner's own log) and exits 125; the parent then finds no stderr |
| 48 | note file and reports the cell failure. |
| 49 | fn r_kfio t int v long to void |
| 50 | if eq t 2 |
| 51 | exit 125 |
| 52 | end |
| 53 | r_kfail v |
| 54 | end |
| 55 | |
| 56 | note r_kfail: the cell could not start. execpure closes every descriptor at or above 3 before any |
| 57 | note step that can fail (close_range first), so the record goes to descriptor 2, the stage's stderr |
| 58 | note file (opened first; seccomp, when it is already installed, still allows write(2)): "E" and |
| 59 | note the errno in 7 bytes little endian, then exit 125. The parent counts it as a cell failure |
| 60 | note only when the status is 125 and the stderr file is exactly such a record or does not exist; |
| 61 | note a program that imitates it only makes its own result uncounted. |
| 62 | fn r_kfail v long to void |
| 63 | e = new uchar 8 0 |
| 64 | put e 0 69 |
| 65 | u = ulong neg v |
| 66 | for i ulong 7 |
| 67 | put e add i 1 uchar and shr u mul i 8 255 |
| 68 | end |
| 69 | x = write 2 e 0 8 |
| 70 | exit 125 |
| 71 | end |
| 72 | |
| 73 | note r_kid: the stage child. sg 0 compiles source.om with <state>/bin/omc, sg 1 runs part.elf. |
| 74 | fn r_kid sg ulong to void |
| 75 | pk = new uchar 4200 0 |
| 76 | r = setpgid 0 0 |
| 77 | n = ulong 0 |
| 78 | if eq sg 0 |
| 79 | r_kidio rtcerr 2 |
| 80 | r_kidio rtsrc 0 |
| 81 | r_kidio rtcout 1 |
| 82 | n = r_path rtomc |
| 83 | else |
| 84 | r_kidio rtserr 2 |
| 85 | r_kidio rtsin 0 |
| 86 | r_kidio rtsout 1 |
| 87 | n = r_jpath rtelf |
| 88 | end |
| 89 | put pk 0 1 |
| 90 | put pk 1 0 |
| 91 | put pk 2 uchar and n 255 |
| 92 | put pk 3 uchar shr n 8 |
| 93 | for i ulong n bound 4096 |
| 94 | put pk add 4 i get rph i |
| 95 | end |
| 96 | v = execpure pk 0 add 4 n |
| 97 | r_kfail v |
| 98 | end |
| 99 | |
| 100 | fn r_u32 b view uchar * o ulong to ulong |
| 101 | ret or or ulong get b o shl ulong get b add o 1 8 or shl ulong get b add o 2 16 shl ulong get b add o 3 24 |
| 102 | end |
| 103 | |
| 104 | note r_cell: run stage sg; sets rgv rwhy (0 exited with rcode, 1 killed by signal rcode, 2 wall |
| 105 | note clock, 3 the cell could not start: errno rcode). False when no stage could be started. |
| 106 | fn r_cell sg ulong to bool |
| 107 | pid = fork |
| 108 | if lt pid 0 |
| 109 | ret false |
| 110 | end |
| 111 | if eq pid 0 |
| 112 | r_kid sg |
| 113 | exit 125 |
| 114 | end |
| 115 | r_cwait pid |
| 116 | if and eq get rgv rwhy 0 eq get rgv rcode 125 |
| 117 | r_cfail sg |
| 118 | end |
| 119 | ret true |
| 120 | end |
| 121 | |
| 122 | note r_cwait: wait for the stage until the wall clock, then kill its process group |
| 123 | fn r_cwait pid long to void |
| 124 | st = new uchar 4 0 |
| 125 | dl = add monons long rwall |
| 126 | put rgv rwhy 9 |
| 127 | put rgv rcode 0 |
| 128 | while true bound 9223372036854775807 |
| 129 | w = waitpid int pid st 0 4 1 |
| 130 | if eq w pid |
| 131 | r_cstat r_u32 st 0 |
| 132 | ret |
| 133 | end |
| 134 | if lt w 0 |
| 135 | ret |
| 136 | end |
| 137 | if gt monons dl |
| 138 | x = killpid int neg pid 9 |
| 139 | x = killpid int pid 9 |
| 140 | w = waitpid int pid st 0 4 0 |
| 141 | put rgv rwhy 2 |
| 142 | ret |
| 143 | end |
| 144 | x = sleepns 2000000 |
| 145 | end |
| 146 | end |
| 147 | |
| 148 | fn r_cstat s ulong to void |
| 149 | if eq and s 127 0 |
| 150 | put rgv rwhy 0 |
| 151 | put rgv rcode and shr s 8 255 |
| 152 | else |
| 153 | put rgv rwhy 1 |
| 154 | put rgv rcode and s 127 |
| 155 | end |
| 156 | end |
| 157 | |
| 158 | note r_cfail: status 125: the stage's stderr file is absent (it could not be opened) or exactly the |
| 159 | note 8-byte record "E" + errno that r_kfail writes, then the cell could not start |
| 160 | fn r_cfail sg ulong to void |
| 161 | eb = new uchar 9 0 |
| 162 | n = ulong 0 |
| 163 | if eq sg 0 |
| 164 | n = r_jpath rtcerr |
| 165 | else |
| 166 | n = r_jpath rtserr |
| 167 | end |
| 168 | r = r_rd eb |
| 169 | if eq r -2 |
| 170 | put rgv rwhy 3 |
| 171 | put rgv rcode 2 |
| 172 | ret |
| 173 | end |
| 174 | if and eq r 8 eq get eb 0 69 |
| 175 | put rgv rwhy 3 |
| 176 | put rgv rcode r_u32 eb 1 |
| 177 | end |
| 178 | end |
| 179 | |
| 180 | note r_seal: copy compiler.out to a new inode part.elf, mode 0500 (a fresh inode with no writer) |
| 181 | fn r_seal to bool |
| 182 | b = new uchar 65536 0 |
| 183 | p = new uchar 4100 0 |
| 184 | n = r_jpath rtcout |
| 185 | pl = r_phcp p |
| 186 | src = fopen p 0 pl rford 0 |
| 187 | if lt src 0 |
| 188 | ret false |
| 189 | end |
| 190 | n = r_jpath rtelf |
| 191 | dst = fopen rph 0 n rfonew 384 |
| 192 | if lt dst 0 |
| 193 | x = fclose int src |
| 194 | ret false |
| 195 | end |
| 196 | ok = false |
| 197 | while true bound 9223372036854775807 |
| 198 | r = read int src b 0 65536 |
| 199 | if eq r 0 |
| 200 | ok = true |
| 201 | break |
| 202 | end |
| 203 | if lt r 0 |
| 204 | break |
| 205 | end |
| 206 | o = ulong 0 |
| 207 | while lt o ulong r bound 65536 |
| 208 | w = write int dst b o sub ulong r o |
| 209 | if le w 0 |
| 210 | break |
| 211 | end |
| 212 | o = add o ulong w |
| 213 | end |
| 214 | if lt o ulong r |
| 215 | break |
| 216 | end |
| 217 | end |
| 218 | x = fclose int src |
| 219 | s = fsync int dst |
| 220 | m = fchmod int dst 320 |
| 221 | x = fclose int dst |
| 222 | ret and and ok eq s 0 eq m 0 |
| 223 | end |
| 224 | |
| 225 | note r_fsha: SHA-256 of the file at rph into h (32 bytes) |
| 226 | fn r_fsha h edit uchar * to bool |
| 227 | c = new ulong 40 0 |
| 228 | b = new uchar 65536 0 |
| 229 | fd = fopen rph 0 get rgv rphl rford 0 |
| 230 | if lt fd 0 |
| 231 | ret false |
| 232 | end |
| 233 | sha256_init c |
| 234 | ok = false |
| 235 | while true bound 9223372036854775807 |
| 236 | r = read int fd b 0 65536 |
| 237 | if eq r 0 |
| 238 | ok = true |
| 239 | break |
| 240 | end |
| 241 | if lt r 0 |
| 242 | break |
| 243 | end |
| 244 | sha256_update c part b 0 ulong r |
| 245 | end |
| 246 | x = fclose int fd |
| 247 | sha256_final c h |
| 248 | ret ok |
| 249 | end |
| 250 | |
| 251 | note r_jfhex: SHA-256 of the job file nm as hex into rjf at o |
| 252 | fn r_jfhex nm view uchar * o ulong to bool |
| 253 | h = new uchar 32 0 |
| 254 | x = new uchar 64 0 |
| 255 | n = r_jpath nm |
| 256 | if not r_fsha h |
| 257 | ret false |
| 258 | end |
| 259 | k = r_hex h x 0 |
| 260 | for i ulong 64 |
| 261 | put rjf add o i get x i |
| 262 | end |
| 263 | ret true |
| 264 | end |
| 265 | |
| 266 | note r_jfzero: 64 zeros into rjf at o (no binary) |
| 267 | fn r_jfzero o ulong to void |
| 268 | for i ulong 64 |
| 269 | put rjf add o i 48 |
| 270 | end |
| 271 | end |
| 272 | |
| 273 | note r_exset: the exit text pre + decimal v (no decimal when v is 2^64-1) into rjf at rfexit |
| 274 | fn r_exset pre view uchar * v ulong to void |
| 275 | d = new uchar 40 0 |
| 276 | k = r_cp d 0 pre |
| 277 | if ne v 18446744073709551615 |
| 278 | k = r_dec d k v |
| 279 | end |
| 280 | for i ulong k bound 40 |
| 281 | put rjf add rfexit i get d i |
| 282 | end |
| 283 | put rgv rexl k |
| 284 | end |
| 285 | |
| 286 | fn r_cp d edit uchar * o ulong s view uchar * to ulong |
| 287 | for i ulong len s bound 16777216 |
| 288 | put d add o i get s i |
| 289 | end |
| 290 | ret add o len s |
| 291 | end |
| 292 | |
| 293 | text rtxomc "omc" |
| 294 | text rtxomcs "omcsignal" |
| 295 | text rtxomct "omctimeout" |
| 296 | text rtxsig "signal" |
| 297 | text rtxtmo "timeout" |
| 298 | text rtxcell "cell" |
| 299 | text rtxnone "" |
| 300 | |
| 301 | note r_outcome: the exit text for the last stage (sg 0 compile, 1 run) |
| 302 | fn r_outcome sg ulong to void |
| 303 | why = get rgv rwhy |
| 304 | c = get rgv rcode |
| 305 | none = ulong 18446744073709551615 |
| 306 | if eq why 3 |
| 307 | r_exset rtxcell c |
| 308 | ret |
| 309 | end |
| 310 | if eq sg 0 |
| 311 | if eq why 0 |
| 312 | r_exset rtxomc c |
| 313 | end |
| 314 | if eq why 1 |
| 315 | r_exset rtxomcs c |
| 316 | end |
| 317 | if eq why 2 |
| 318 | r_exset rtxomct none |
| 319 | end |
| 320 | ret |
| 321 | end |
| 322 | if eq why 0 |
| 323 | r_exset rtxnone c |
| 324 | end |
| 325 | if eq why 1 |
| 326 | r_exset rtxsig c |
| 327 | end |
| 328 | if eq why 2 |
| 329 | r_exset rtxtmo none |
| 330 | end |
| 331 | end |
| 332 | |
| 333 | note r_check: compile and run the job directory (source.om, stdin) in the cell; fills binary, |
| 334 | note stdout and exit in rjf. False only when a stage could not be started at all. |
| 335 | fn r_check to bool |
| 336 | if not r_cell 0 |
| 337 | ret false |
| 338 | end |
| 339 | if or ne get rgv rwhy 0 ne get rgv rcode 0 |
| 340 | r_outcome 0 |
| 341 | r_jfzero rfbin |
| 342 | r_jfnil rfout |
| 343 | ret true |
| 344 | end |
| 345 | if not r_jfhex rtcout rfbin |
| 346 | ret false |
| 347 | end |
| 348 | if not r_seal |
| 349 | ret false |
| 350 | end |
| 351 | if not r_cell 1 |
| 352 | ret false |
| 353 | end |
| 354 | r_outcome 1 |
| 355 | note LIEPOINT: the gate's lying-runner mutant changes the stdout hash line below |
| 356 | ret r_jfhex rtsout rfout |
| 357 | end |
| 358 | |
| 359 | note r_jfnil: the SHA-256 of no bytes as hex into rjf at o |
| 360 | fn r_jfnil o ulong to void |
| 361 | h = new uchar 32 0 |
| 362 | x = new uchar 64 0 |
| 363 | e = new uchar 1 0 |
| 364 | r_sha part e 0 0 h |
| 365 | k = r_hex h x 0 |
| 366 | for i ulong 64 |
| 367 | put rjf add o i get x i |
| 368 | end |
| 369 | end |