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
omega-mv / seed / effects / effects.om
| 1 | note effects: effect-derived capability manifest for seeded-Omega programs. |
| 2 | note A unit can reach the kernel only through the closed set of keyword operations in |
| 3 | note OMEGESE section 10 (no raw syscall, no address-to-pointer), so the set of host |
| 4 | note words in its source is a complete, finite statement of its OS effects. |
| 5 | note Modes: |
| 6 | note effects src [allow=C,..] < unit.om host words, classes, Linux syscalls |
| 7 | note effects elf < binary syscalls the omc ELF can issue (fail closed) |
| 8 | note effects verify SRC BIN [allow=C,..] ELF syscalls must be within the source set |
| 9 | note effects bpf [launch] < unit.om seccomp cBPF allow-list (bwrap --seccomp) |
| 10 | note effects table the word table, for the probe parity test |
| 11 | note Exit: 0 pass, 1 refused/undeclared, 2 usage, 3 invalid or unresolvable input, 4 read. |
| 12 | |
| 13 | global inb uchar 8388612 |
| 14 | global mk uchar 2097156 |
| 15 | global rc uchar 2097156 |
| 16 | global wq ulong 2097156 |
| 17 | global ob uchar 1048576 |
| 18 | global obn ulong 1 |
| 19 | global dig uchar 24 |
| 20 | global ab uchar 4096 |
| 21 | global wn ulong 128 |
| 22 | global wc ulong 128 |
| 23 | global wsa ulong 128 |
| 24 | global wsn ulong 128 |
| 25 | global wsp ulong 1024 |
| 26 | global wl ulong 128 |
| 27 | global wk ulong 128 |
| 28 | global nw ulong 1 |
| 29 | global cn ulong 16 |
| 30 | global ncl ulong 1 |
| 31 | global sy uchar 512 |
| 32 | global se uchar 512 |
| 33 | global cu uchar 16 |
| 34 | global al uchar 16 |
| 35 | global st ulong 16 |
| 36 | global ef ulong 8 |
| 37 | |
| 38 | note word class syscalls... ('-' entries are ignored; a row of only '-' issues none). Kept equal |
| 39 | note to the pinned omc by check.sh, which compiles one probe per word and reads the syscalls back |
| 40 | note out of the ELF. |
| 41 | text tab "read io 63 -\nwrite io 64 -\nfclose fd 57 -\nfsync fd 82 -\nfdsync fd 83 -\nfseek fd 62 -\nftrunc fd 46 -\nfchmod fd 52 -\nfchdir fd 50 -\nsyncfs fd 267 -\nflock fd 32 -\ngetdnts fd 61 -\nfstat fd 80 -\npread fd 67 -\npwrite fd 68 -\ndupfd fd 24 -\npipefds fd 59 -\nwaitio fd 73 -\npollfds fd 73 -\nmemfd fd 279 -\neventfd fd 19 -\nfopen path 56 -\nchmod path 53 -\nchown path 54 -\nfaccess path 48 -\ntruncate path 45 -\nunlink path 35 -\nmakedir path 34 -\nrmdir path 35 -\ncwd path 17 -\nchdir path 49 -\nreadlnk path 78 -\nstat path 79 -\nlstat path 79 -\nrenames path 276 -\nlink path 37 -\nsymlnk path 36 -\nsocknew net 198 -\nbind net 200 -\nconnect net 203 -\nlisten net 201 -\naccept net 242 -\nsockname net 204 -\npeername net 205 -\nsockopt net 208 -\nrecv net 207 -\nsend net 206 -\npeercred net 209 -\nsendmsg net 211 -\nshutdown net 210 -\nfork proc 220 -\nexecprog proc 221 436\nexecpure proc 56 57 167 261 277 281 436 444 445 446\nexecenv proc 221 436\nprlimit proc 261 -\ncpuset proc 122 -\ncpuget proc 123 -\ncpunow proc 168 -\nsetprio proc 140 -\ngetprio proc 141 -\nkillpid proc 129 -\nwaitpid proc 260 -\nsetsid proc 157 -\nsetpgid proc 154 -\numask proc 166 -\ngetpid ident 172 -\ngetppid ident 173 -\ngetuid ident 174 -\ngeteuid ident 175 -\ngetgid ident 176 -\ngetegid ident 177 -\ngetpgid ident 155 -\ngetsid ident 156 -\nmonons time 113 -\nrealns time 113 -\nsleepns time 101 -\nargcnt env - -\nargread env - -\nenvcnt env - -\nenvread env - -\nrandom random 278 -\ndevctl device 29 -\ndevcall device 29 -\nuvmctl device 29 -\nvfioctl device 29 -\nvfioirq device 29 -\nkfdctl device 29 80\nkfdmap device 79 80 222\nkfdres device 215 222\nmapnew device 222 -\nvfiomap device 222 -\nvfiomapw device 222 -\nmapdrop device 215 -\nmaphold device 215 222\nmapbind device 215 222\nmapread device - -\nmapwrite device - -\nmapaddr device - -\nmapget device - -\nmapset device - -\nenlarge device 216 222\n" |
| 42 | text cls "io\nfd\npath\nnet\nproc\nident\ntime\nenv\nrandom\ndevice\n" |
| 43 | text kwn "note allow= launch src elf verify bpf table " |
| 44 | text msg "EFFECT \n class=\n syscalls=\n uses=\n line=\nEFFECTS 1 words=\n classes=\nEFFECTS REFUSED word=\nEFFECTS PASS\nEFFECTS REFUSED n=\nELF 1 words=\n svc=\nELF UNRESOLVED at=\n reason=prev-not-movz-x8\n reason=branch-target\n reason=indirect-branch\nELF INVALID\nVERIFY PASS syscalls=\nVERIFY FAIL undeclared=\n unused=\nusage: effects src [allow=C,..] < unit.om | elf < binary | verify SRC BIN [allow=C,..] | bpf [launch] < unit.om | table\neffects: cannot read input\n-\n,\n \nELF REFUSED wx-segment\nEFFECTS REFUSED unknown-class\n reason=nr-range\nELF REFUSED no-exec-segment\n reason=leaves-segment\n reason=entry-svc\n reason=unknown-branch\n" |
| 45 | |
| 46 | note ---- output ---- |
| 47 | fn obf to void |
| 48 | n = get obn 0 |
| 49 | o = ulong 0 |
| 50 | while lt o n |
| 51 | r = write 1 ob o sub n o |
| 52 | if le r 0 |
| 53 | exit 4 |
| 54 | end |
| 55 | o = add o ulong r |
| 56 | end |
| 57 | put obn 0 0 |
| 58 | end |
| 59 | fn ob1 b uchar to void |
| 60 | n = get obn 0 |
| 61 | if ge n 1048000 |
| 62 | obf |
| 63 | n = ulong 0 |
| 64 | end |
| 65 | put ob n b |
| 66 | put obn 0 add n 1 |
| 67 | end |
| 68 | fn onl to void |
| 69 | ob1 10 |
| 70 | end |
| 71 | fn say id ulong to void |
| 72 | k = ulong 0 |
| 73 | s = ulong 0 |
| 74 | for i ulong len msg |
| 75 | if eq get msg i 10 |
| 76 | if eq k id |
| 77 | for j ulong sub i s |
| 78 | ob1 get msg add s j |
| 79 | end |
| 80 | ret |
| 81 | end |
| 82 | k = add k 1 |
| 83 | s = add i 1 |
| 84 | end |
| 85 | end |
| 86 | end |
| 87 | fn od v ulong to void |
| 88 | x = v |
| 89 | n = ulong 0 |
| 90 | while ge x 10 |
| 91 | put dig n uchar add 48 rem x 10 |
| 92 | x = div x 10 |
| 93 | n = add n 1 |
| 94 | end |
| 95 | put dig n uchar add 48 x |
| 96 | n = add n 1 |
| 97 | for i ulong n |
| 98 | ob1 get dig sub sub n i 1 |
| 99 | end |
| 100 | end |
| 101 | fn pw w ulong to void |
| 102 | x = w |
| 103 | for i ulong 8 |
| 104 | if eq x 0 |
| 105 | ret |
| 106 | end |
| 107 | ob1 uchar and x 255 |
| 108 | x = shr x 8 |
| 109 | end |
| 110 | end |
| 111 | |
| 112 | note ---- tables ---- |
| 113 | fn kwp id ulong to ulong |
| 114 | k = ulong 0 |
| 115 | w = ulong 0 |
| 116 | s = ulong 0 |
| 117 | for i ulong len kwn |
| 118 | c = get kwn i |
| 119 | if eq c 32 |
| 120 | if eq k id |
| 121 | ret w |
| 122 | end |
| 123 | k = add k 1 |
| 124 | w = ulong 0 |
| 125 | s = ulong 0 |
| 126 | else |
| 127 | w = or w shl ulong c s |
| 128 | s = add s 8 |
| 129 | end |
| 130 | end |
| 131 | ret 0 |
| 132 | end |
| 133 | fn clsid w ulong to ulong |
| 134 | for i ulong get ncl 0 |
| 135 | if eq get cn i w |
| 136 | ret i |
| 137 | end |
| 138 | end |
| 139 | ret 99 |
| 140 | end |
| 141 | fn tinit to void |
| 142 | w = ulong 0 |
| 143 | s = ulong 0 |
| 144 | r = ulong 0 |
| 145 | for i ulong len cls |
| 146 | c = get cls i |
| 147 | if eq c 10 |
| 148 | put cn r w |
| 149 | r = add r 1 |
| 150 | w = ulong 0 |
| 151 | s = ulong 0 |
| 152 | else |
| 153 | w = or w shl ulong c s |
| 154 | s = add s 8 |
| 155 | end |
| 156 | end |
| 157 | put ncl 0 r |
| 158 | dash = ulong 45 |
| 159 | r = ulong 0 |
| 160 | f = ulong 0 |
| 161 | p = ulong 0 |
| 162 | w = ulong 0 |
| 163 | s = ulong 0 |
| 164 | d = ulong 0 |
| 165 | for i ulong len tab |
| 166 | c = get tab i |
| 167 | if or eq c 32 eq c 10 |
| 168 | if eq f 0 |
| 169 | put wn r w |
| 170 | end |
| 171 | if eq f 1 |
| 172 | put wc r clsid w |
| 173 | put wsa r p |
| 174 | end |
| 175 | if and ge f 2 ne w dash |
| 176 | put wsp p d |
| 177 | p = add p 1 |
| 178 | put wsn r add get wsn r 1 |
| 179 | end |
| 180 | f = add f 1 |
| 181 | if eq c 10 |
| 182 | r = add r 1 |
| 183 | f = ulong 0 |
| 184 | end |
| 185 | w = ulong 0 |
| 186 | s = ulong 0 |
| 187 | d = ulong 0 |
| 188 | else |
| 189 | w = or w shl ulong c s |
| 190 | s = add s 8 |
| 191 | d = add mul d 10 sub ulong c 48 |
| 192 | end |
| 193 | end |
| 194 | put nw 0 r |
| 195 | end |
| 196 | |
| 197 | note ---- input ---- |
| 198 | fn rdfd fd int to ulong |
| 199 | n = ulong 0 |
| 200 | for i ulong 8388608 |
| 201 | r = read fd inb n sub 8388612 n |
| 202 | if lt r 0 |
| 203 | ret 8388613 |
| 204 | end |
| 205 | if eq r 0 |
| 206 | ret n |
| 207 | end |
| 208 | n = add n ulong r |
| 209 | if ge n 8388612 |
| 210 | ret 8388613 |
| 211 | end |
| 212 | end |
| 213 | ret 8388613 |
| 214 | end |
| 215 | fn rdarg ix ulong to ulong |
| 216 | m = argread ix ab 0 4095 |
| 217 | if le m 0 |
| 218 | ret 8388613 |
| 219 | end |
| 220 | fd = int fopen ab 0 ulong m 0 0 |
| 221 | if lt fd 0 |
| 222 | ret 8388613 |
| 223 | end |
| 224 | n = rdfd fd |
| 225 | c = fclose fd |
| 226 | ret n |
| 227 | end |
| 228 | fn argw ix ulong to ulong |
| 229 | m = argread ix ab 0 4095 |
| 230 | if or le m 0 gt m 8 |
| 231 | ret 0 |
| 232 | end |
| 233 | w = ulong 0 |
| 234 | for i ulong ulong m |
| 235 | w = or w shl ulong get ab i mul i 8 |
| 236 | end |
| 237 | ret w |
| 238 | end |
| 239 | note allow=C1,C2 in argument ix; returns false on an unknown class name |
| 240 | fn parsal ix ulong to bool |
| 241 | m = argread ix ab 0 4095 |
| 242 | if le m 0 |
| 243 | ret false |
| 244 | end |
| 245 | if lt m 6 |
| 246 | ret false |
| 247 | end |
| 248 | pfx = ulong 0 |
| 249 | for i ulong 6 |
| 250 | pfx = or pfx shl ulong get ab i mul i 8 |
| 251 | end |
| 252 | if ne pfx kwp 1 |
| 253 | ret false |
| 254 | end |
| 255 | put ef 0 1 |
| 256 | w = ulong 0 |
| 257 | s = ulong 0 |
| 258 | for i ulong sub ulong m 5 |
| 259 | j = add i 6 |
| 260 | c = uchar 44 |
| 261 | if lt j ulong m |
| 262 | c = get ab j |
| 263 | end |
| 264 | if eq c 44 |
| 265 | if gt s 0 |
| 266 | k = clsid w |
| 267 | if eq k 99 |
| 268 | ret false |
| 269 | end |
| 270 | put al k 1 |
| 271 | end |
| 272 | w = ulong 0 |
| 273 | s = ulong 0 |
| 274 | else |
| 275 | if ge s 64 |
| 276 | ret false |
| 277 | end |
| 278 | w = or w shl ulong c s |
| 279 | s = add s 8 |
| 280 | end |
| 281 | end |
| 282 | ret true |
| 283 | end |
| 284 | |
| 285 | note ---- source scan: every host word occurrence outside notes and text literals ---- |
| 286 | fn hit w ulong ln ulong to void |
| 287 | for r ulong get nw 0 |
| 288 | if eq get wn r w |
| 289 | put wk r add get wk r 1 |
| 290 | if eq get wl r 0 |
| 291 | put wl r ln |
| 292 | end |
| 293 | put cu get wc r 1 |
| 294 | for j ulong get wsn r |
| 295 | a = get wsp add get wsa r j |
| 296 | if lt a 512 |
| 297 | put sy a 1 |
| 298 | end |
| 299 | end |
| 300 | ret |
| 301 | end |
| 302 | end |
| 303 | end |
| 304 | note st: 0 packed word, 1 word length, 2 line, 3 skip-to-eol, 4 in quote, 5 escape, 6 first word on line |
| 305 | fn fin to void |
| 306 | l = get st 1 |
| 307 | if eq l 0 |
| 308 | ret |
| 309 | end |
| 310 | w = get st 0 |
| 311 | if and eq get st 6 1 and eq l 4 eq w kwp 0 |
| 312 | put st 3 1 |
| 313 | else |
| 314 | if le l 8 |
| 315 | hit w get st 2 |
| 316 | end |
| 317 | end |
| 318 | put st 6 0 |
| 319 | put st 0 0 |
| 320 | put st 1 0 |
| 321 | end |
| 322 | fn scansrc n ulong to void |
| 323 | put st 0 0 |
| 324 | put st 1 0 |
| 325 | put st 2 1 |
| 326 | put st 3 0 |
| 327 | put st 4 0 |
| 328 | put st 5 0 |
| 329 | put st 6 1 |
| 330 | put sy 93 1 |
| 331 | for i ulong 8388608 |
| 332 | if ge i n |
| 333 | break |
| 334 | end |
| 335 | c = get inb i |
| 336 | if eq c 10 |
| 337 | fin |
| 338 | put st 2 add get st 2 1 |
| 339 | put st 3 0 |
| 340 | put st 4 0 |
| 341 | put st 5 0 |
| 342 | put st 6 1 |
| 343 | continue |
| 344 | end |
| 345 | if eq get st 3 1 |
| 346 | continue |
| 347 | end |
| 348 | if eq get st 4 1 |
| 349 | if eq get st 5 1 |
| 350 | put st 5 0 |
| 351 | else |
| 352 | if eq c 92 |
| 353 | put st 5 1 |
| 354 | end |
| 355 | if eq c 34 |
| 356 | put st 4 0 |
| 357 | end |
| 358 | end |
| 359 | continue |
| 360 | end |
| 361 | if or or eq c 32 eq c 9 or eq c 13 or eq c 40 eq c 41 |
| 362 | fin |
| 363 | continue |
| 364 | end |
| 365 | if eq c 34 |
| 366 | fin |
| 367 | put st 4 1 |
| 368 | put st 6 0 |
| 369 | continue |
| 370 | end |
| 371 | l = get st 1 |
| 372 | if lt l 8 |
| 373 | put st 0 or get st 0 shl ulong c mul l 8 |
| 374 | end |
| 375 | put st 1 add l 1 |
| 376 | end |
| 377 | fin |
| 378 | end |
| 379 | fn plist a ulong to void |
| 380 | note a 0: source set, 1: elf set, 2: elf minus source, 3: source minus elf |
| 381 | k = ulong 0 |
| 382 | for i ulong 512 |
| 383 | x = get sy i |
| 384 | y = get se i |
| 385 | p = false |
| 386 | if eq a 0 |
| 387 | p = eq x 1 |
| 388 | end |
| 389 | if eq a 1 |
| 390 | p = eq y 1 |
| 391 | end |
| 392 | if eq a 2 |
| 393 | p = and eq y 1 eq x 0 |
| 394 | end |
| 395 | if eq a 3 |
| 396 | p = and eq x 1 eq y 0 |
| 397 | end |
| 398 | if p |
| 399 | if gt k 0 |
| 400 | say 23 |
| 401 | end |
| 402 | od i |
| 403 | k = add k 1 |
| 404 | end |
| 405 | end |
| 406 | if eq k 0 |
| 407 | say 22 |
| 408 | end |
| 409 | end |
| 410 | fn psys r ulong to void |
| 411 | n = get wsn r |
| 412 | if eq n 0 |
| 413 | say 22 |
| 414 | ret |
| 415 | end |
| 416 | for j ulong n |
| 417 | if gt j 0 |
| 418 | say 23 |
| 419 | end |
| 420 | od get wsp add get wsa r j |
| 421 | end |
| 422 | end |
| 423 | fn report to int |
| 424 | u = ulong 0 |
| 425 | for r ulong get nw 0 |
| 426 | if gt get wk r 0 |
| 427 | u = add u 1 |
| 428 | say 0 |
| 429 | pw get wn r |
| 430 | say 1 |
| 431 | pw get cn get wc r |
| 432 | say 2 |
| 433 | psys r |
| 434 | say 3 |
| 435 | od get wk r |
| 436 | say 4 |
| 437 | od get wl r |
| 438 | onl |
| 439 | end |
| 440 | end |
| 441 | say 5 |
| 442 | od u |
| 443 | say 6 |
| 444 | k = ulong 0 |
| 445 | for i ulong get ncl 0 |
| 446 | if eq get cu i 1 |
| 447 | if gt k 0 |
| 448 | say 23 |
| 449 | end |
| 450 | pw get cn i |
| 451 | k = add k 1 |
| 452 | end |
| 453 | end |
| 454 | if eq k 0 |
| 455 | say 22 |
| 456 | end |
| 457 | say 2 |
| 458 | plist 0 |
| 459 | onl |
| 460 | ret refuse |
| 461 | end |
| 462 | fn refuse to int |
| 463 | if eq get ef 0 0 |
| 464 | ret 0 |
| 465 | end |
| 466 | bad = ulong 0 |
| 467 | for r ulong get nw 0 |
| 468 | if and gt get wk r 0 eq get al get wc r 0 |
| 469 | bad = add bad 1 |
| 470 | say 7 |
| 471 | pw get wn r |
| 472 | say 1 |
| 473 | pw get cn get wc r |
| 474 | say 4 |
| 475 | od get wl r |
| 476 | onl |
| 477 | end |
| 478 | end |
| 479 | if eq bad 0 |
| 480 | say 8 |
| 481 | onl |
| 482 | ret 0 |
| 483 | end |
| 484 | say 9 |
| 485 | od bad |
| 486 | onl |
| 487 | ret 1 |
| 488 | end |
| 489 | |
| 490 | note ---- ELF scan of an omc static AArch64 binary ---- |
| 491 | fn r8 o ulong to ulong |
| 492 | ret ulong get inb o |
| 493 | end |
| 494 | fn r16 o ulong to ulong |
| 495 | ret or r8 o shl r8 add o 1 8 |
| 496 | end |
| 497 | fn r32 o ulong to ulong |
| 498 | ret or r16 o shl r16 add o 2 16 |
| 499 | end |
| 500 | fn r64 o ulong to ulong |
| 501 | ret or r32 o shl r32 add o 4 32 |
| 502 | end |
| 503 | fn bad3 id ulong to int |
| 504 | say id |
| 505 | onl |
| 506 | ret 3 |
| 507 | end |
| 508 | fn unres at ulong why ulong to int |
| 509 | say 12 |
| 510 | od at |
| 511 | say why |
| 512 | onl |
| 513 | ret 3 |
| 514 | end |
| 515 | note branch target of word w at index j, or 4294967295 if w is not a direct branch |
| 516 | fn btgt w ulong j ulong to ulong |
| 517 | o = ulong 0 |
| 518 | m = ulong 0 |
| 519 | if eq and w 2080374784 335544320 |
| 520 | o = and w 67108863 |
| 521 | m = 67108864 |
| 522 | end |
| 523 | if or eq and w 4278190096 1409286144 eq and w 2113929216 872415232 |
| 524 | o = and shr w 5 524287 |
| 525 | m = 524288 |
| 526 | end |
| 527 | if eq and w 2113929216 905969664 |
| 528 | o = and shr w 5 16383 |
| 529 | m = 16384 |
| 530 | end |
| 531 | if eq m 0 |
| 532 | ret 4294967295 |
| 533 | end |
| 534 | if ge o div m 2 |
| 535 | b = sub m o |
| 536 | if gt b j |
| 537 | ret 4294967294 |
| 538 | end |
| 539 | ret sub j b |
| 540 | end |
| 541 | ret add j o |
| 542 | end |
| 543 | fn scantext off ulong fsz ulong ent ulong to int |
| 544 | nwd = div fsz 4 |
| 545 | if gt nwd 2097152 |
| 546 | ret bad3 16 |
| 547 | end |
| 548 | for j ulong 2097152 |
| 549 | if ge j nwd |
| 550 | break |
| 551 | end |
| 552 | put mk j 0 |
| 553 | end |
| 554 | for j ulong 2097152 |
| 555 | if ge j nwd |
| 556 | break |
| 557 | end |
| 558 | w = r32 add off mul j 4 |
| 559 | if or eq and w 4294966303 3592355840 eq and w 4294966303 3594452992 |
| 560 | ret unres j 15 |
| 561 | end |
| 562 | if eq and w 4292870175 3556769793 |
| 563 | put ef 1 add get ef 1 1 |
| 564 | put mk j 1 |
| 565 | if eq j 0 |
| 566 | ret unres j 13 |
| 567 | end |
| 568 | p = r32 add off mul sub j 1 4 |
| 569 | if ne and p 4292870175 3531603976 |
| 570 | ret unres j 13 |
| 571 | end |
| 572 | nr = and shr p 5 65535 |
| 573 | if ge nr 512 |
| 574 | ret unres j 27 |
| 575 | end |
| 576 | put se nr 1 |
| 577 | end |
| 578 | end |
| 579 | note Which words can run as code. Texts and other data share the executable segment with the |
| 580 | note instructions, so a data word can have the bit pattern of a direct branch. Only a word that |
| 581 | note control can reach is held to the branch rule. Reach starts at the entry word (ent) and follows |
| 582 | note the next word, except after an unconditional B, a RET, or an SVC whose number is 93 (exit) or |
| 583 | note 94 (exit_group), and the target of every direct branch. Such an SVC does not return: it is |
| 584 | note entered only through its MOVZ X8, because no reached branch lands on it. BR and BLR are refused |
| 585 | note above wherever they occur, so nothing else can transfer control. |
| 586 | note Three things would let control run words this walk does not follow, and each is refused: |
| 587 | note the entry word is itself an SVC (the kernel enters it with no MOVZ before it); a reached |
| 588 | note direct branch has its target outside this segment; a reached word that falls through is the |
| 589 | note last word of the segment. A reached branch instruction this walk cannot follow is refused too. |
| 590 | note When the entry word is not in this segment (ent is 4294967295) every word is held to the rule. |
| 591 | if ge ent nwd |
| 592 | for j ulong 2097152 |
| 593 | if ge j nwd |
| 594 | break |
| 595 | end |
| 596 | u = btgt r32 add off mul j 4 j |
| 597 | if lt u nwd |
| 598 | if eq get mk u 1 |
| 599 | ret unres u 14 |
| 600 | end |
| 601 | end |
| 602 | end |
| 603 | put ef 2 add get ef 2 nwd |
| 604 | ret 0 |
| 605 | end |
| 606 | note rc: 1 once a word is known to be reached. wq: reached words not yet followed; each word |
| 607 | note enters it once, so it never holds more than nwd entries and the loop ends within nwd turns. |
| 608 | for j ulong 2097152 |
| 609 | if ge j nwd |
| 610 | break |
| 611 | end |
| 612 | put rc j 0 |
| 613 | end |
| 614 | if eq get mk ent 1 |
| 615 | ret unres ent 30 |
| 616 | end |
| 617 | put rc ent 1 |
| 618 | put wq 0 ent |
| 619 | sp = ulong 1 |
| 620 | for it ulong 2097153 |
| 621 | if eq sp 0 |
| 622 | break |
| 623 | end |
| 624 | sp = sub sp 1 |
| 625 | cw = get wq sp |
| 626 | w = r32 add off mul cw 4 |
| 627 | note a reached word of the register-branch class that is not RET (BRAA, BLRAA, RETAA, ERET and the |
| 628 | note rest), or a BC.cond, moves control in a way this walk does not follow |
| 629 | if eq and w 4261412864 3590324224 |
| 630 | if ne and w 4294966303 3596550144 |
| 631 | ret unres cw 31 |
| 632 | end |
| 633 | end |
| 634 | if eq and w 4278190096 1409286160 |
| 635 | ret unres cw 31 |
| 636 | end |
| 637 | t = btgt w cw |
| 638 | if and ne t 4294967295 ge t nwd |
| 639 | ret unres cw 29 |
| 640 | end |
| 641 | if lt t nwd |
| 642 | if eq get mk t 1 |
| 643 | ret unres t 14 |
| 644 | end |
| 645 | if eq get rc t 0 |
| 646 | put rc t 1 |
| 647 | put wq sp t |
| 648 | sp = add sp 1 |
| 649 | end |
| 650 | end |
| 651 | fal = ulong 1 |
| 652 | if eq and w 4227858432 335544320 |
| 653 | fal = 0 |
| 654 | end |
| 655 | if eq and w 4294966303 3596550144 |
| 656 | fal = 0 |
| 657 | end |
| 658 | if eq get mk cw 1 |
| 659 | p = r32 add off mul sub cw 1 4 |
| 660 | nr = and shr p 5 65535 |
| 661 | if or eq nr 93 eq nr 94 |
| 662 | fal = 0 |
| 663 | end |
| 664 | end |
| 665 | nx1 = add cw 1 |
| 666 | if and eq fal 1 ge nx1 nwd |
| 667 | ret unres cw 29 |
| 668 | end |
| 669 | if and eq fal 1 lt nx1 nwd |
| 670 | if eq get rc nx1 0 |
| 671 | put rc nx1 1 |
| 672 | put wq sp nx1 |
| 673 | sp = add sp 1 |
| 674 | end |
| 675 | end |
| 676 | end |
| 677 | put ef 2 add get ef 2 nwd |
| 678 | ret 0 |
| 679 | end |
| 680 | note one program header: -1 not an executable load, 0 scanned, >0 exit code |
| 681 | fn seg p ulong n ulong to int |
| 682 | t = r32 p |
| 683 | f = r32 add p 4 |
| 684 | if ne t 1 |
| 685 | ret -1 |
| 686 | end |
| 687 | if and ne and f 1 0 ne and f 2 0 |
| 688 | say 25 |
| 689 | onl |
| 690 | ret 3 |
| 691 | end |
| 692 | if eq and f 1 0 |
| 693 | ret -1 |
| 694 | end |
| 695 | off = r64 add p 8 |
| 696 | fsz = r64 add p 32 |
| 697 | if or ne rem off 4 0 gt add off fsz n |
| 698 | ret bad3 16 |
| 699 | end |
| 700 | note the entry word of the program, as an index into this segment, when it lies in it |
| 701 | va = r64 add p 16 |
| 702 | en = r64 24 |
| 703 | ent = ulong 4294967295 |
| 704 | if ge en va |
| 705 | d = sub en va |
| 706 | if lt d fsz |
| 707 | if ne rem d 4 0 |
| 708 | ret bad3 16 |
| 709 | end |
| 710 | ent = div d 4 |
| 711 | end |
| 712 | end |
| 713 | ret scantext off fsz ent |
| 714 | end |
| 715 | fn scanelf n ulong to int |
| 716 | if lt n 64 |
| 717 | ret bad3 16 |
| 718 | end |
| 719 | if or ne r32 0 1179403647 ne r16 18 183 |
| 720 | ret bad3 16 |
| 721 | end |
| 722 | ph = r64 32 |
| 723 | pe = r16 54 |
| 724 | pn = r16 56 |
| 725 | if or ne pe 56 gt add ph mul pn 56 n |
| 726 | ret bad3 16 |
| 727 | end |
| 728 | nx = ulong 0 |
| 729 | for i ulong pn |
| 730 | v = seg add ph mul i 56 n |
| 731 | if gt v 0 |
| 732 | ret v |
| 733 | end |
| 734 | if eq v 0 |
| 735 | nx = add nx 1 |
| 736 | end |
| 737 | end |
| 738 | if eq nx 0 |
| 739 | say 28 |
| 740 | onl |
| 741 | ret 3 |
| 742 | end |
| 743 | ret 0 |
| 744 | end |
| 745 | fn elfrep to void |
| 746 | say 10 |
| 747 | od get ef 2 |
| 748 | say 11 |
| 749 | od get ef 1 |
| 750 | say 2 |
| 751 | plist 1 |
| 752 | onl |
| 753 | end |
| 754 | |
| 755 | note ---- seccomp cBPF: arch check, one JEQ per allowed nr, kill otherwise ---- |
| 756 | fn binst code ulong jt ulong jf ulong k ulong to void |
| 757 | ob1 uchar and code 255 |
| 758 | ob1 uchar shr code 8 |
| 759 | ob1 uchar jt |
| 760 | ob1 uchar jf |
| 761 | ob1 uchar and k 255 |
| 762 | ob1 uchar and shr k 8 255 |
| 763 | ob1 uchar and shr k 16 255 |
| 764 | ob1 uchar shr k 24 |
| 765 | end |
| 766 | fn bpfout to void |
| 767 | m = ulong 0 |
| 768 | for i ulong 512 |
| 769 | if eq get sy i 1 |
| 770 | m = add m 1 |
| 771 | end |
| 772 | end |
| 773 | binst 32 0 0 4 |
| 774 | binst 21 1 0 3221225655 |
| 775 | binst 6 0 0 2147483648 |
| 776 | binst 32 0 0 0 |
| 777 | t = ulong 0 |
| 778 | for i ulong 512 |
| 779 | if eq get sy i 1 |
| 780 | binst 21 sub m t 0 i |
| 781 | t = add t 1 |
| 782 | end |
| 783 | end |
| 784 | binst 6 0 0 2147483648 |
| 785 | binst 6 0 0 2147418112 |
| 786 | end |
| 787 | |
| 788 | fn ptable to void |
| 789 | for r ulong get nw 0 |
| 790 | pw get wn r |
| 791 | ob1 32 |
| 792 | pw get cn get wc r |
| 793 | ob1 32 |
| 794 | n = get wsn r |
| 795 | if eq n 0 |
| 796 | say 22 |
| 797 | end |
| 798 | for j ulong n |
| 799 | if gt j 0 |
| 800 | ob1 32 |
| 801 | end |
| 802 | od get wsp add get wsa r j |
| 803 | end |
| 804 | onl |
| 805 | end |
| 806 | end |
| 807 | |
| 808 | fn main to int |
| 809 | tinit |
| 810 | ac = argcnt |
| 811 | if lt ac 2 |
| 812 | say 20 |
| 813 | onl |
| 814 | obf |
| 815 | ret 2 |
| 816 | end |
| 817 | md = argw 1 |
| 818 | if eq md kwp 7 |
| 819 | ptable |
| 820 | obf |
| 821 | ret 0 |
| 822 | end |
| 823 | if eq md kwp 3 |
| 824 | if gt ac 3 |
| 825 | say 20 |
| 826 | onl |
| 827 | obf |
| 828 | ret 2 |
| 829 | end |
| 830 | if eq ac 3 |
| 831 | if not parsal 2 |
| 832 | say 26 |
| 833 | onl |
| 834 | obf |
| 835 | ret 2 |
| 836 | end |
| 837 | end |
| 838 | n = rdfd 0 |
| 839 | if gt n 8388612 |
| 840 | say 21 |
| 841 | onl |
| 842 | obf |
| 843 | ret 4 |
| 844 | end |
| 845 | scansrc n |
| 846 | v = report |
| 847 | obf |
| 848 | ret v |
| 849 | end |
| 850 | if eq md kwp 4 |
| 851 | if ne ac 2 |
| 852 | say 20 |
| 853 | onl |
| 854 | obf |
| 855 | ret 2 |
| 856 | end |
| 857 | n = rdfd 0 |
| 858 | if gt n 8388612 |
| 859 | say 21 |
| 860 | onl |
| 861 | obf |
| 862 | ret 4 |
| 863 | end |
| 864 | v = scanelf n |
| 865 | if eq v 0 |
| 866 | elfrep |
| 867 | end |
| 868 | obf |
| 869 | ret v |
| 870 | end |
| 871 | if eq md kwp 6 |
| 872 | lw = ulong 0 |
| 873 | if eq ac 3 |
| 874 | lw = argw 2 |
| 875 | end |
| 876 | if or gt ac 3 and eq ac 3 ne lw kwp 2 |
| 877 | say 20 |
| 878 | onl |
| 879 | obf |
| 880 | ret 2 |
| 881 | end |
| 882 | n = rdfd 0 |
| 883 | if gt n 8388612 |
| 884 | say 21 |
| 885 | onl |
| 886 | obf |
| 887 | ret 4 |
| 888 | end |
| 889 | scansrc n |
| 890 | if eq ac 3 |
| 891 | put sy 221 1 |
| 892 | end |
| 893 | bpfout |
| 894 | obf |
| 895 | ret 0 |
| 896 | end |
| 897 | if eq md kwp 5 |
| 898 | if or lt ac 4 gt ac 5 |
| 899 | say 20 |
| 900 | onl |
| 901 | obf |
| 902 | ret 2 |
| 903 | end |
| 904 | if eq ac 5 |
| 905 | if not parsal 4 |
| 906 | say 26 |
| 907 | onl |
| 908 | obf |
| 909 | ret 2 |
| 910 | end |
| 911 | end |
| 912 | n = rdarg 2 |
| 913 | if gt n 8388612 |
| 914 | say 21 |
| 915 | onl |
| 916 | obf |
| 917 | ret 4 |
| 918 | end |
| 919 | scansrc n |
| 920 | v = report |
| 921 | n = rdarg 3 |
| 922 | if gt n 8388612 |
| 923 | say 21 |
| 924 | onl |
| 925 | obf |
| 926 | ret 4 |
| 927 | end |
| 928 | e = scanelf n |
| 929 | if ne e 0 |
| 930 | obf |
| 931 | ret e |
| 932 | end |
| 933 | elfrep |
| 934 | k = ulong 0 |
| 935 | for i ulong 512 |
| 936 | if and eq get se i 1 eq get sy i 0 |
| 937 | k = add k 1 |
| 938 | end |
| 939 | end |
| 940 | if gt k 0 |
| 941 | say 18 |
| 942 | plist 2 |
| 943 | onl |
| 944 | obf |
| 945 | ret 1 |
| 946 | end |
| 947 | say 17 |
| 948 | plist 1 |
| 949 | say 19 |
| 950 | plist 3 |
| 951 | onl |
| 952 | obf |
| 953 | ret v |
| 954 | end |
| 955 | say 20 |
| 956 | onl |
| 957 | obf |
| 958 | ret 2 |
| 959 | end |