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 / si / architecture / tools / zero-legacy / elfclose.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note elfclose: ELF and process closure inventory (aien-architecture-mv #316 item 2, DRA-178). |
| 3 | note Seeded Omega program, built only by the pinned seed compiler in omega.pin. |
| 4 | note usage: elfclose elf PATH... one row per file |
| 5 | note elfclose proc [PROCROOT] every process under PROCROOT (default /proc) |
| 6 | note An ELF row is ELF path class machine type link interp needed producer verdict (tabs). |
| 7 | note class ELF64LE, or UNSUPPORTED (32-bit or big-endian), MALFORMED (a header, program |
| 8 | note header, PT_LOAD, dynamic or section field points outside the file or breaks a rule, |
| 9 | note or the interpreter or a DT_NEEDED name holds a control byte (below 0x20, 0x7f), |
| 10 | note or a DT_NEEDED name holds a comma), NOT_ELF, UNREADABLE (cannot be opened, or not |
| 11 | note a regular file: FIFO, directory, device, socket; opened without blocking). |
| 12 | note link static (no PT_INTERP, no PT_DYNAMIC), dynamic (PT_INTERP), static-pie |
| 13 | note (PT_DYNAMIC without PT_INTERP). |
| 14 | note needed the DT_NEEDED names, comma separated, read through DT_STRTAB mapped by PT_LOAD. |
| 15 | note producer the toolchain marks found, joined by +, or none: gcc, clang, rustc, lld (in |
| 16 | note .comment), go (.go.buildinfo or .note.go.buildid), gnu (PT_GNU_* program headers, |
| 17 | note .gnu.* or .note.gnu.* sections), crt (.note.ABI-tag), note (PT_NOTE). |
| 18 | note verdict FOREIGN when a toolchain mark is present or the file links dynamically |
| 19 | note (dynamic or static-pie); UNMARKED when an ELF64LE file carries neither (seeded |
| 20 | note omc output: PT_LOAD only, no sections; that is not proof of its source); |
| 21 | note UNKNOWN for every other class. |
| 22 | note A process row is PROC pid owner exe-link-target class ... verdict, classified by reading |
| 23 | note PROCROOT/pid/exe (the running image); owner is self when PROCROOT/pid belongs to the |
| 24 | note scanning uid, else that uid. MAP pid path rows list the file paths of its executable |
| 25 | note mappings in PROCROOT/pid/maps, dropping a path equal to the one on the MAP row just before |
| 26 | note (consecutive duplicates), not every repeat. Processes whose exe link does not exist |
| 27 | note (kernel threads, exited) are counted as NOEXE; other link errors give an UNREADABLE row. |
| 28 | note Paths, exe link targets and MAP paths are written with every control byte and backslash |
| 29 | note as \xHH, so no file name can end a row or add a field. PROCROOT is at most 4027 bytes. |
| 30 | note Capacities: 64 program headers, 1024 sections, a 64 KiB section-name table, the first 512 |
| 31 | note dynamic entries and sonames up to 255 bytes; beyond them a file is MALFORMED (dynamic |
| 32 | note entries past 512 are not read). Only the first 4 KiB of .comment is searched. |
| 33 | note Summary lines: # ELF, # FOREIGN, # UNMARKED, # UNKNOWN, # PROC, # MAP, # NOEXE, |
| 34 | note # MAPTRUNC (maps over 960 KiB), each with a count. Exit 0 done, 64 usage. |
| 35 | |
| 36 | text tuse "usage: elfclose elf PATH... | elfclose proc [PROCROOT]\n" |
| 37 | text telf "elf" |
| 38 | text tprc "proc" |
| 39 | text tdpr "/proc" |
| 40 | text texe "/exe" |
| 41 | text tmps "/maps" |
| 42 | text tnl "\n" |
| 43 | text ttab "\t" |
| 44 | text tdash "-" |
| 45 | text tplus "+" |
| 46 | text tcom "," |
| 47 | text thash "# " |
| 48 | text tsp " " |
| 49 | text trELF "ELF\t" |
| 50 | text trPRC "PROC\t" |
| 51 | text trMAP "MAP\t" |
| 52 | text tcls "ELF64LE\nUNSUPPORTED\nMALFORMED\nNOT_ELF\nUNREADABLE\n" |
| 53 | text tlnk "static\ndynamic\nstatic-pie\n" |
| 54 | text tprd "gcc\nclang\nrustc\ngo\nlld\ngnu\ncrt\nnote\n" |
| 55 | text tvrd "FOREIGN\nUNMARKED\nUNKNOWN\n" |
| 56 | text tcnt "ELF\nFOREIGN\nUNMARKED\nUNKNOWN\nPROC\nMAP\nNOEXE\nMAPTRUNC\n" |
| 57 | text tsn ".comment\n.go.buildinfo\n.note.go.buildid\n.note.ABI-tag\n.gnu.\n.note.gnu.\n" |
| 58 | text tnd "GCC:\nclang version\nrustc version\nLinker: LLD\n" |
| 59 | text tmach "aarch64" |
| 60 | text tx86 "x86_64" |
| 61 | text tem "em" |
| 62 | text tet "et" |
| 63 | text ttyp "rel\nexec\ndyn\ncore\n" |
| 64 | text tnone "none" |
| 65 | text tself "self" |
| 66 | |
| 67 | global hb uchar 64 |
| 68 | global ph uchar 3584 |
| 69 | global dy uchar 8192 |
| 70 | global st uchar 4096 |
| 71 | global sh uchar 65536 |
| 72 | global ss uchar 65536 |
| 73 | global ssn ulong 1 |
| 74 | global cm uchar 4096 |
| 75 | global ib uchar 4096 |
| 76 | global ibn ulong 1 |
| 77 | global nd uchar 8192 |
| 78 | global ndn ulong 1 |
| 79 | global fsz ulong 1 |
| 80 | global fdg long 1 |
| 81 | global res ulong 8 |
| 82 | global pv ulong 8 |
| 83 | global path uchar 4096 |
| 84 | global pthn ulong 1 |
| 85 | global lnk uchar 4096 |
| 86 | global lnkn ulong 1 |
| 87 | global db uchar 65536 |
| 88 | global dfd long 1 |
| 89 | global root uchar 4096 |
| 90 | global rootn ulong 1 |
| 91 | global pid uchar 64 |
| 92 | global pidn ulong 1 |
| 93 | global mp uchar 983040 |
| 94 | global prv uchar 4096 |
| 95 | global prvn ulong 1 |
| 96 | global cnt ulong 8 |
| 97 | global nb uchar 24 |
| 98 | global arg uchar 16 |
| 99 | global stb uchar 128 |
| 100 | |
| 101 | note ---- little-endian readers, one per buffer (a global is never an argument) ---- |
| 102 | |
| 103 | fn h16 o ulong to ulong |
| 104 | ret add ulong get hb o mul 256 ulong get hb add o 1 |
| 105 | end |
| 106 | |
| 107 | fn h64 o ulong to ulong |
| 108 | v = ulong 0 |
| 109 | for i ulong 8 |
| 110 | v = or shl v 8 ulong get hb add o sub 7 i |
| 111 | end |
| 112 | ret v |
| 113 | end |
| 114 | |
| 115 | fn p32 o ulong to ulong |
| 116 | v = ulong 0 |
| 117 | for i ulong 4 |
| 118 | v = or shl v 8 ulong get ph add o sub 3 i |
| 119 | end |
| 120 | ret v |
| 121 | end |
| 122 | |
| 123 | fn p64 o ulong to ulong |
| 124 | v = ulong 0 |
| 125 | for i ulong 8 |
| 126 | v = or shl v 8 ulong get ph add o sub 7 i |
| 127 | end |
| 128 | ret v |
| 129 | end |
| 130 | |
| 131 | fn d64 o ulong to ulong |
| 132 | v = ulong 0 |
| 133 | for i ulong 8 |
| 134 | v = or shl v 8 ulong get dy add o sub 7 i |
| 135 | end |
| 136 | ret v |
| 137 | end |
| 138 | |
| 139 | fn s32 o ulong to ulong |
| 140 | v = ulong 0 |
| 141 | for i ulong 4 |
| 142 | v = or shl v 8 ulong get sh add o sub 3 i |
| 143 | end |
| 144 | ret v |
| 145 | end |
| 146 | |
| 147 | fn s64 o ulong to ulong |
| 148 | v = ulong 0 |
| 149 | for i ulong 8 |
| 150 | v = or shl v 8 ulong get sh add o sub 7 i |
| 151 | end |
| 152 | ret v |
| 153 | end |
| 154 | |
| 155 | note ---- text tables: entry e of a newline-separated table ---- |
| 156 | |
| 157 | fn tst e ulong k ulong to ulong |
| 158 | s = ulong 0 |
| 159 | j = ulong 0 |
| 160 | while lt j e bound 4096 |
| 161 | if eq k 0 |
| 162 | if eq get tcls s 10 |
| 163 | j = add j 1 |
| 164 | end |
| 165 | end |
| 166 | if eq k 1 |
| 167 | if eq get tlnk s 10 |
| 168 | j = add j 1 |
| 169 | end |
| 170 | end |
| 171 | if eq k 2 |
| 172 | if eq get tprd s 10 |
| 173 | j = add j 1 |
| 174 | end |
| 175 | end |
| 176 | if eq k 3 |
| 177 | if eq get tvrd s 10 |
| 178 | j = add j 1 |
| 179 | end |
| 180 | end |
| 181 | if eq k 4 |
| 182 | if eq get tcnt s 10 |
| 183 | j = add j 1 |
| 184 | end |
| 185 | end |
| 186 | if eq k 5 |
| 187 | if eq get tsn s 10 |
| 188 | j = add j 1 |
| 189 | end |
| 190 | end |
| 191 | if eq k 6 |
| 192 | if eq get tnd s 10 |
| 193 | j = add j 1 |
| 194 | end |
| 195 | end |
| 196 | if eq k 7 |
| 197 | if eq get ttyp s 10 |
| 198 | j = add j 1 |
| 199 | end |
| 200 | end |
| 201 | s = add s 1 |
| 202 | end |
| 203 | ret s |
| 204 | end |
| 205 | |
| 206 | fn tch k ulong i ulong to uchar |
| 207 | if eq k 0 |
| 208 | ret get tcls i |
| 209 | end |
| 210 | if eq k 1 |
| 211 | ret get tlnk i |
| 212 | end |
| 213 | if eq k 2 |
| 214 | ret get tprd i |
| 215 | end |
| 216 | if eq k 3 |
| 217 | ret get tvrd i |
| 218 | end |
| 219 | if eq k 4 |
| 220 | ret get tcnt i |
| 221 | end |
| 222 | if eq k 5 |
| 223 | ret get tsn i |
| 224 | end |
| 225 | if eq k 6 |
| 226 | ret get tnd i |
| 227 | end |
| 228 | ret get ttyp i |
| 229 | end |
| 230 | |
| 231 | fn tln e ulong k ulong to ulong |
| 232 | s = tst e k |
| 233 | n = ulong 0 |
| 234 | while ne tch k add s n 10 bound 64 |
| 235 | n = add n 1 |
| 236 | end |
| 237 | ret n |
| 238 | end |
| 239 | |
| 240 | fn wtbl e ulong k ulong to void |
| 241 | s = tst e k |
| 242 | n = tln e k |
| 243 | for i ulong n |
| 244 | c = tch k add s i |
| 245 | put nb 0 c |
| 246 | r = write 1 nb 0 1 |
| 247 | end |
| 248 | end |
| 249 | |
| 250 | note ---- output ---- |
| 251 | |
| 252 | fn wnl to void |
| 253 | r = write 1 tnl 0 1 |
| 254 | end |
| 255 | |
| 256 | note byte i of buffer k (0 path, 1 lnk, 2 mp): a global is never an argument |
| 257 | fn ebyte k ulong i ulong to uchar |
| 258 | if eq k 0 |
| 259 | ret get path i |
| 260 | end |
| 261 | if eq k 1 |
| 262 | ret get lnk i |
| 263 | end |
| 264 | ret get mp i |
| 265 | end |
| 266 | |
| 267 | fn wrun k ulong s ulong n ulong to void |
| 268 | if eq n 0 |
| 269 | ret |
| 270 | end |
| 271 | if eq k 0 |
| 272 | r = write 1 path s n |
| 273 | ret |
| 274 | end |
| 275 | if eq k 1 |
| 276 | r = write 1 lnk s n |
| 277 | ret |
| 278 | end |
| 279 | r = write 1 mp s n |
| 280 | end |
| 281 | |
| 282 | fn hexd v uchar to uchar |
| 283 | if lt v 10 |
| 284 | ret add v 48 |
| 285 | end |
| 286 | ret add v 87 |
| 287 | end |
| 288 | |
| 289 | note write buffer k bytes [o, o+n) into a report row: a control byte or a backslash is written as |
| 290 | note \xHH (two lowercase hex digits), so a path or link target can never end a row or add a field |
| 291 | fn wesc k ulong o ulong n ulong to void |
| 292 | s = o |
| 293 | e = add o n |
| 294 | i = o |
| 295 | while lt i e bound 983041 |
| 296 | c = ebyte k i |
| 297 | esc = ctlb c |
| 298 | if eq c 92 |
| 299 | esc = true |
| 300 | end |
| 301 | if esc |
| 302 | wrun k s sub i s |
| 303 | put nb 0 92 |
| 304 | put nb 1 120 |
| 305 | put nb 2 hexd shr c 4 |
| 306 | put nb 3 hexd and c 15 |
| 307 | r = write 1 nb 0 4 |
| 308 | s = add i 1 |
| 309 | end |
| 310 | i = add i 1 |
| 311 | end |
| 312 | wrun k s sub e s |
| 313 | end |
| 314 | |
| 315 | fn wtab to void |
| 316 | r = write 1 ttab 0 1 |
| 317 | end |
| 318 | |
| 319 | fn wdash to void |
| 320 | r = write 1 tdash 0 1 |
| 321 | end |
| 322 | |
| 323 | fn wnum v ulong to void |
| 324 | n = ulong 0 |
| 325 | if eq v 0 |
| 326 | put nb 23 48 |
| 327 | n = 1 |
| 328 | end |
| 329 | x = v |
| 330 | while gt x 0 bound 20 |
| 331 | put nb sub 23 n uchar add 48 rem x 10 |
| 332 | x = div x 10 |
| 333 | n = add n 1 |
| 334 | end |
| 335 | r = write 1 nb sub 24 n n |
| 336 | end |
| 337 | |
| 338 | note ---- file ranges ---- |
| 339 | |
| 340 | fn inrange o ulong n ulong to bool |
| 341 | f = get fsz 0 |
| 342 | if gt o f |
| 343 | ret false |
| 344 | end |
| 345 | ret ge sub f o n |
| 346 | end |
| 347 | |
| 348 | note ---- program headers ---- |
| 349 | |
| 350 | fn interp o ulong to ulong |
| 351 | off = p64 add o 8 |
| 352 | fz = p64 add o 32 |
| 353 | if eq fz 0 |
| 354 | ret 2 |
| 355 | end |
| 356 | if gt fz 4095 |
| 357 | ret 2 |
| 358 | end |
| 359 | if not inrange off fz |
| 360 | ret 2 |
| 361 | end |
| 362 | k = pread int get fdg 0 ib 0 fz long off |
| 363 | if ne k long fz |
| 364 | ret 2 |
| 365 | end |
| 366 | n = ulong 0 |
| 367 | while lt n fz bound 4096 |
| 368 | if eq get ib n 0 |
| 369 | break |
| 370 | end |
| 371 | n = add n 1 |
| 372 | end |
| 373 | if eq n 0 |
| 374 | ret 2 |
| 375 | end |
| 376 | for i ulong n |
| 377 | if ctlb get ib i |
| 378 | ret 2 |
| 379 | end |
| 380 | end |
| 381 | put ibn 0 n |
| 382 | ret 0 |
| 383 | end |
| 384 | |
| 385 | note a control byte (below 0x20, or 0x7f): never valid in an interpreter path or a soname, and |
| 386 | note never written raw into a report row (it could end the row or forge a field) |
| 387 | fn ctlb c uchar to bool |
| 388 | if lt c 32 |
| 389 | ret true |
| 390 | end |
| 391 | ret eq c 127 |
| 392 | end |
| 393 | |
| 394 | note file offset of virtual address v through the PT_LOAD headers, or 2^64-1 when unmapped |
| 395 | fn vtoo v ulong pn ulong to ulong |
| 396 | for i ulong pn |
| 397 | b = mul i 56 |
| 398 | if eq p32 b 1 |
| 399 | va = p64 add b 16 |
| 400 | fz = p64 add b 32 |
| 401 | if ge v va |
| 402 | if lt sub v va fz |
| 403 | ret add p64 add b 8 sub v va |
| 404 | end |
| 405 | end |
| 406 | end |
| 407 | end |
| 408 | ret sub ulong 0 1 |
| 409 | end |
| 410 | |
| 411 | fn phscan pn ulong to ulong |
| 412 | put pv 0 0 |
| 413 | put pv 1 0 |
| 414 | for i ulong pn |
| 415 | b = mul i 56 |
| 416 | t = p32 b |
| 417 | if eq t 1 |
| 418 | note a PT_LOAD whose file bytes leave the file is MALFORMED, so vtoo never wraps an offset |
| 419 | if not inrange p64 add b 8 p64 add b 32 |
| 420 | ret 2 |
| 421 | end |
| 422 | end |
| 423 | if eq t 3 |
| 424 | if eq get pv 0 1 |
| 425 | ret 2 |
| 426 | end |
| 427 | put pv 0 1 |
| 428 | r = interp b |
| 429 | if ne r 0 |
| 430 | ret r |
| 431 | end |
| 432 | end |
| 433 | if eq t 2 |
| 434 | put pv 1 1 |
| 435 | put pv 2 p64 add b 8 |
| 436 | put pv 3 p64 add b 32 |
| 437 | end |
| 438 | if eq t 4 |
| 439 | put res 4 or get res 4 128 |
| 440 | end |
| 441 | if ge t 1685382480 |
| 442 | if ge 1685382483 t |
| 443 | note PT_GNU_EH_FRAME, PT_GNU_STACK, PT_GNU_RELRO, PT_GNU_PROPERTY |
| 444 | put res 4 or get res 4 32 |
| 445 | end |
| 446 | end |
| 447 | end |
| 448 | ret 0 |
| 449 | end |
| 450 | |
| 451 | note append the NUL-terminated name at strtab offset o (strsz z, strtab at file offset pv 7) |
| 452 | fn addneed o ulong z ulong to ulong |
| 453 | if ge o z |
| 454 | ret 2 |
| 455 | end |
| 456 | w = sub z o |
| 457 | if gt w 256 |
| 458 | w = 256 |
| 459 | end |
| 460 | k = pread int get fdg 0 st 0 w long add get pv 7 o |
| 461 | if ne k long w |
| 462 | ret 2 |
| 463 | end |
| 464 | n = ulong 0 |
| 465 | while lt n w bound 4096 |
| 466 | if eq get st n 0 |
| 467 | break |
| 468 | end |
| 469 | n = add n 1 |
| 470 | end |
| 471 | if eq n w |
| 472 | ret 2 |
| 473 | end |
| 474 | if eq n 0 |
| 475 | ret 2 |
| 476 | end |
| 477 | for i ulong n |
| 478 | c = get st i |
| 479 | if ctlb c |
| 480 | ret 2 |
| 481 | end |
| 482 | if eq c 44 |
| 483 | ret 2 |
| 484 | end |
| 485 | end |
| 486 | m = get ndn 0 |
| 487 | if gt add add m n 1 8192 |
| 488 | ret 2 |
| 489 | end |
| 490 | if gt m 0 |
| 491 | put nd m 44 |
| 492 | m = add m 1 |
| 493 | end |
| 494 | for i ulong n |
| 495 | put nd add m i get st i |
| 496 | end |
| 497 | put ndn 0 add m n |
| 498 | ret 0 |
| 499 | end |
| 500 | |
| 501 | fn dynamic pn ulong to ulong |
| 502 | z = get pv 3 |
| 503 | if gt z 8192 |
| 504 | z = 8192 |
| 505 | end |
| 506 | z = mul div z 16 16 |
| 507 | off = get pv 2 |
| 508 | if not inrange off z |
| 509 | ret 2 |
| 510 | end |
| 511 | k = pread int get fdg 0 dy 0 z long off |
| 512 | if ne k long z |
| 513 | ret 2 |
| 514 | end |
| 515 | nent = div z 16 |
| 516 | put pv 4 0 |
| 517 | put pv 5 0 |
| 518 | put pv 6 0 |
| 519 | for i ulong nent |
| 520 | t = d64 mul i 16 |
| 521 | if eq t 0 |
| 522 | break |
| 523 | end |
| 524 | if eq t 5 |
| 525 | put pv 4 d64 add mul i 16 8 |
| 526 | put pv 6 1 |
| 527 | end |
| 528 | if eq t 10 |
| 529 | put pv 5 d64 add mul i 16 8 |
| 530 | end |
| 531 | end |
| 532 | ret needs nent pn |
| 533 | end |
| 534 | |
| 535 | fn needs nent ulong pn ulong to ulong |
| 536 | sz = get pv 5 |
| 537 | if eq get pv 6 0 |
| 538 | for i ulong nent |
| 539 | t = d64 mul i 16 |
| 540 | if eq t 0 |
| 541 | break |
| 542 | end |
| 543 | if eq t 1 |
| 544 | ret 2 |
| 545 | end |
| 546 | end |
| 547 | ret 0 |
| 548 | end |
| 549 | so = vtoo get pv 4 pn |
| 550 | if not inrange so sz |
| 551 | ret 2 |
| 552 | end |
| 553 | put pv 7 so |
| 554 | for i ulong nent |
| 555 | t = d64 mul i 16 |
| 556 | if eq t 0 |
| 557 | break |
| 558 | end |
| 559 | if eq t 1 |
| 560 | r = addneed d64 add mul i 16 8 sz |
| 561 | if ne r 0 |
| 562 | ret r |
| 563 | end |
| 564 | end |
| 565 | end |
| 566 | ret 0 |
| 567 | end |
| 568 | |
| 569 | fn phdrs to ulong |
| 570 | put res 3 0 |
| 571 | po = h64 32 |
| 572 | pe = h16 54 |
| 573 | pn = h16 56 |
| 574 | if eq pn 0 |
| 575 | ret 0 |
| 576 | end |
| 577 | if ne pe 56 |
| 578 | ret 2 |
| 579 | end |
| 580 | if gt pn 64 |
| 581 | ret 2 |
| 582 | end |
| 583 | sz = mul pn 56 |
| 584 | if not inrange po sz |
| 585 | ret 2 |
| 586 | end |
| 587 | k = pread int get fdg 0 ph 0 sz long po |
| 588 | if ne k long sz |
| 589 | ret 2 |
| 590 | end |
| 591 | r = phscan pn |
| 592 | if ne r 0 |
| 593 | ret r |
| 594 | end |
| 595 | if eq get pv 1 1 |
| 596 | r = dynamic pn |
| 597 | if ne r 0 |
| 598 | ret r |
| 599 | end |
| 600 | end |
| 601 | if eq get pv 0 1 |
| 602 | put res 3 1 |
| 603 | ret 0 |
| 604 | end |
| 605 | if eq get pv 1 1 |
| 606 | put res 3 2 |
| 607 | end |
| 608 | ret 0 |
| 609 | end |
| 610 | |
| 611 | note ---- sections ---- |
| 612 | |
| 613 | note does the shstrtab name at ss[o] equal entry e of tsn (with its terminating NUL) |
| 614 | fn ssis o ulong e ulong to bool |
| 615 | s = tst e 5 |
| 616 | n = tln e 5 |
| 617 | if gt add add o n 1 get ssn 0 |
| 618 | ret false |
| 619 | end |
| 620 | for i ulong n |
| 621 | if ne get ss add o i get tsn add s i |
| 622 | ret false |
| 623 | end |
| 624 | end |
| 625 | ret eq get ss add o n 0 |
| 626 | end |
| 627 | |
| 628 | note does the shstrtab name at ss[o] start with entry e of tsn |
| 629 | fn sspre o ulong e ulong to bool |
| 630 | s = tst e 5 |
| 631 | n = tln e 5 |
| 632 | if gt add o n get ssn 0 |
| 633 | ret false |
| 634 | end |
| 635 | for i ulong n |
| 636 | if ne get ss add o i get tsn add s i |
| 637 | ret false |
| 638 | end |
| 639 | end |
| 640 | ret true |
| 641 | end |
| 642 | |
| 643 | note does needle (tnd from s, m bytes) occur at cm[i] |
| 644 | fn cmat i ulong s ulong m ulong to bool |
| 645 | for j ulong m |
| 646 | c = get cm add i j |
| 647 | if ne c get tnd add s j |
| 648 | ret false |
| 649 | end |
| 650 | end |
| 651 | ret true |
| 652 | end |
| 653 | |
| 654 | fn cmhas e ulong n ulong to bool |
| 655 | s = tst e 6 |
| 656 | m = tln e 6 |
| 657 | if lt n m |
| 658 | ret false |
| 659 | end |
| 660 | lim = sub n m |
| 661 | i = ulong 0 |
| 662 | while le i lim bound 8192 |
| 663 | if cmat i s m |
| 664 | ret true |
| 665 | end |
| 666 | i = add i 1 |
| 667 | end |
| 668 | ret false |
| 669 | end |
| 670 | |
| 671 | fn comment b ulong to ulong |
| 672 | off = s64 add b 24 |
| 673 | z = s64 add b 32 |
| 674 | if gt z 4096 |
| 675 | z = 4096 |
| 676 | end |
| 677 | if not inrange off z |
| 678 | ret 2 |
| 679 | end |
| 680 | k = pread int get fdg 0 cm 0 z long off |
| 681 | if ne k long z |
| 682 | ret 2 |
| 683 | end |
| 684 | p = get res 4 |
| 685 | if cmhas 0 z |
| 686 | p = or p 1 |
| 687 | end |
| 688 | if cmhas 1 z |
| 689 | p = or p 2 |
| 690 | end |
| 691 | if cmhas 2 z |
| 692 | p = or p 4 |
| 693 | end |
| 694 | if cmhas 3 z |
| 695 | p = or p 16 |
| 696 | end |
| 697 | put res 4 p |
| 698 | ret 0 |
| 699 | end |
| 700 | |
| 701 | fn shstr sn ulong sx ulong to ulong |
| 702 | if ge sx sn |
| 703 | ret 2 |
| 704 | end |
| 705 | b = mul sx 64 |
| 706 | xo = s64 add b 24 |
| 707 | xz = s64 add b 32 |
| 708 | if gt xz 65536 |
| 709 | ret 2 |
| 710 | end |
| 711 | if not inrange xo xz |
| 712 | ret 2 |
| 713 | end |
| 714 | k = pread int get fdg 0 ss 0 xz long xo |
| 715 | if ne k long xz |
| 716 | ret 2 |
| 717 | end |
| 718 | put ssn 0 xz |
| 719 | ret 0 |
| 720 | end |
| 721 | |
| 722 | fn secscan sn ulong to ulong |
| 723 | for i ulong sn |
| 724 | b = mul i 64 |
| 725 | nm = s32 b |
| 726 | if ge nm get ssn 0 |
| 727 | ret 2 |
| 728 | end |
| 729 | if ssis nm 0 |
| 730 | r = comment b |
| 731 | if ne r 0 |
| 732 | ret r |
| 733 | end |
| 734 | end |
| 735 | go = ssis nm 1 |
| 736 | if not go |
| 737 | go = ssis nm 2 |
| 738 | end |
| 739 | if go |
| 740 | put res 4 or get res 4 8 |
| 741 | end |
| 742 | if ssis nm 3 |
| 743 | put res 4 or get res 4 64 |
| 744 | end |
| 745 | gn = sspre nm 4 |
| 746 | if not gn |
| 747 | gn = sspre nm 5 |
| 748 | end |
| 749 | if gn |
| 750 | put res 4 or get res 4 32 |
| 751 | end |
| 752 | end |
| 753 | ret 0 |
| 754 | end |
| 755 | |
| 756 | fn sects to ulong |
| 757 | so = h64 40 |
| 758 | se = h16 58 |
| 759 | sn = h16 60 |
| 760 | if eq so 0 |
| 761 | ret 0 |
| 762 | end |
| 763 | if eq sn 0 |
| 764 | ret 0 |
| 765 | end |
| 766 | if ne se 64 |
| 767 | ret 2 |
| 768 | end |
| 769 | if gt sn 1024 |
| 770 | ret 2 |
| 771 | end |
| 772 | sz = mul sn 64 |
| 773 | if not inrange so sz |
| 774 | ret 2 |
| 775 | end |
| 776 | k = pread int get fdg 0 sh 0 sz long so |
| 777 | if ne k long sz |
| 778 | ret 2 |
| 779 | end |
| 780 | r = shstr sn h16 62 |
| 781 | if ne r 0 |
| 782 | ret r |
| 783 | end |
| 784 | ret secscan sn |
| 785 | end |
| 786 | |
| 787 | note ---- one file ---- |
| 788 | |
| 789 | fn parse to ulong |
| 790 | k = pread int get fdg 0 hb 0 64 0 |
| 791 | if lt k 4 |
| 792 | ret 3 |
| 793 | end |
| 794 | if ne get hb 0 127 |
| 795 | ret 3 |
| 796 | end |
| 797 | if ne get hb 1 69 |
| 798 | ret 3 |
| 799 | end |
| 800 | if ne get hb 2 76 |
| 801 | ret 3 |
| 802 | end |
| 803 | if ne get hb 3 70 |
| 804 | ret 3 |
| 805 | end |
| 806 | if lt k 64 |
| 807 | ret 2 |
| 808 | end |
| 809 | if ne get hb 4 2 |
| 810 | ret 1 |
| 811 | end |
| 812 | if ne get hb 5 1 |
| 813 | ret 1 |
| 814 | end |
| 815 | put res 1 h16 18 |
| 816 | put res 2 h16 16 |
| 817 | r = phdrs |
| 818 | if ne r 0 |
| 819 | ret r |
| 820 | end |
| 821 | ret sects |
| 822 | end |
| 823 | |
| 824 | note classify the file named by path; results in res, ib, nd |
| 825 | fn classify to void |
| 826 | put res 0 4 |
| 827 | put res 3 0 |
| 828 | put res 4 0 |
| 829 | put ndn 0 0 |
| 830 | put ibn 0 0 |
| 831 | put ssn 0 0 |
| 832 | note open without blocking (a FIFO would block a plain open) and accept only a regular file: |
| 833 | note anything else (FIFO, directory, device, socket) is UNREADABLE |
| 834 | fd = fopen path 0 get pthn 0 2048 0 |
| 835 | if lt fd 0 |
| 836 | ret |
| 837 | end |
| 838 | s = fstat int fd stb 0 128 |
| 839 | if lt s 0 |
| 840 | r = fclose int fd |
| 841 | ret |
| 842 | end |
| 843 | m = or ulong get stb 16 shl ulong get stb 17 8 |
| 844 | if ne and m 61440 32768 |
| 845 | r = fclose int fd |
| 846 | ret |
| 847 | end |
| 848 | put fdg 0 fd |
| 849 | e = fseek int fd 0 2 |
| 850 | if lt e 0 |
| 851 | r = fclose int fd |
| 852 | ret |
| 853 | end |
| 854 | put fsz 0 ulong e |
| 855 | c = parse |
| 856 | put res 0 c |
| 857 | r = fclose int fd |
| 858 | end |
| 859 | |
| 860 | fn wmach m ulong to void |
| 861 | if eq m 183 |
| 862 | r = write 1 tmach 0 len tmach |
| 863 | ret |
| 864 | end |
| 865 | if eq m 62 |
| 866 | r = write 1 tx86 0 len tx86 |
| 867 | ret |
| 868 | end |
| 869 | r = write 1 tem 0 len tem |
| 870 | wnum m |
| 871 | end |
| 872 | |
| 873 | fn wtype t ulong to void |
| 874 | if ge t 1 |
| 875 | if ge 4 t |
| 876 | wtbl sub t 1 7 |
| 877 | ret |
| 878 | end |
| 879 | end |
| 880 | r = write 1 tet 0 len tet |
| 881 | wnum t |
| 882 | end |
| 883 | |
| 884 | fn wprod to void |
| 885 | p = get res 4 |
| 886 | if eq p 0 |
| 887 | r = write 1 tnone 0 len tnone |
| 888 | ret |
| 889 | end |
| 890 | first = true |
| 891 | for i ulong 8 |
| 892 | if ne and shr p i 1 0 |
| 893 | if not first |
| 894 | r = write 1 tplus 0 1 |
| 895 | end |
| 896 | first = false |
| 897 | wtbl i 2 |
| 898 | end |
| 899 | end |
| 900 | end |
| 901 | |
| 902 | note write class onward for the classified file, count it |
| 903 | fn wtail to void |
| 904 | c = get res 0 |
| 905 | wtbl c 0 |
| 906 | wtab |
| 907 | if ne c 0 |
| 908 | for i ulong 5 |
| 909 | wdash |
| 910 | wtab |
| 911 | end |
| 912 | wdash |
| 913 | wtab |
| 914 | wtbl 2 3 |
| 915 | wnl |
| 916 | put cnt 3 add get cnt 3 1 |
| 917 | ret |
| 918 | end |
| 919 | wmach get res 1 |
| 920 | wtab |
| 921 | wtype get res 2 |
| 922 | wtab |
| 923 | wtbl get res 3 1 |
| 924 | wtab |
| 925 | if eq get ibn 0 0 |
| 926 | wdash |
| 927 | else |
| 928 | r = write 1 ib 0 get ibn 0 |
| 929 | end |
| 930 | wtab |
| 931 | if eq get ndn 0 0 |
| 932 | wdash |
| 933 | else |
| 934 | r = write 1 nd 0 get ndn 0 |
| 935 | end |
| 936 | wtab |
| 937 | wprod |
| 938 | wtab |
| 939 | fx = ne get res 4 0 |
| 940 | if ne get res 3 0 |
| 941 | fx = true |
| 942 | end |
| 943 | if not fx |
| 944 | wtbl 1 3 |
| 945 | put cnt 2 add get cnt 2 1 |
| 946 | else |
| 947 | wtbl 0 3 |
| 948 | put cnt 1 add get cnt 1 1 |
| 949 | end |
| 950 | wnl |
| 951 | end |
| 952 | |
| 953 | note ---- elf mode ---- |
| 954 | |
| 955 | fn elfmode to void |
| 956 | for a ulong sub ulong argcnt 2 |
| 957 | n = argread add a 2 path 0 4095 |
| 958 | if gt n 0 |
| 959 | put pthn 0 ulong n |
| 960 | classify |
| 961 | r = write 1 trELF 0 len trELF |
| 962 | wesc 0 0 ulong n |
| 963 | wtab |
| 964 | wtail |
| 965 | put cnt 0 add get cnt 0 1 |
| 966 | end |
| 967 | end |
| 968 | end |
| 969 | |
| 970 | note ---- proc mode ---- |
| 971 | |
| 972 | note path = root / pid / suffix (0 exe, 1 maps) |
| 973 | fn mkpath sfx ulong to void |
| 974 | n = get rootn 0 |
| 975 | for i ulong n |
| 976 | put path i get root i |
| 977 | end |
| 978 | put path n 47 |
| 979 | m = get pidn 0 |
| 980 | for i ulong m |
| 981 | put path add add n 1 i get pid i |
| 982 | end |
| 983 | k = add add n 1 m |
| 984 | if eq sfx 2 |
| 985 | put pthn 0 k |
| 986 | ret |
| 987 | end |
| 988 | if eq sfx 0 |
| 989 | for i ulong len texe |
| 990 | put path add k i get texe i |
| 991 | end |
| 992 | put pthn 0 add k len texe |
| 993 | ret |
| 994 | end |
| 995 | for i ulong len tmps |
| 996 | put path add k i get tmps i |
| 997 | end |
| 998 | put pthn 0 add k len tmps |
| 999 | end |
| 1000 | |
| 1001 | fn wpid to void |
| 1002 | r = write 1 pid 0 get pidn 0 |
| 1003 | end |
| 1004 | |
| 1005 | note one maps line mp[s, e): print MAP for an executable mapping of a file path |
| 1006 | fn mapline s ulong e ulong to void |
| 1007 | if not mapx s e |
| 1008 | ret |
| 1009 | end |
| 1010 | note skip five fields (range perms offset dev inode) to the path |
| 1011 | i = s |
| 1012 | f = ulong 0 |
| 1013 | while lt i e bound 4096 |
| 1014 | if eq get mp i 32 |
| 1015 | f = add f 1 |
| 1016 | while lt i e bound 4096 |
| 1017 | if ne get mp i 32 |
| 1018 | break |
| 1019 | end |
| 1020 | i = add i 1 |
| 1021 | end |
| 1022 | if eq f 5 |
| 1023 | break |
| 1024 | end |
| 1025 | else |
| 1026 | i = add i 1 |
| 1027 | end |
| 1028 | end |
| 1029 | if ge i e |
| 1030 | ret |
| 1031 | end |
| 1032 | if ne get mp i 47 |
| 1033 | ret |
| 1034 | end |
| 1035 | n = sub e i |
| 1036 | if gt n 4095 |
| 1037 | ret |
| 1038 | end |
| 1039 | if eq n get prvn 0 |
| 1040 | same = true |
| 1041 | for j ulong n |
| 1042 | if ne get prv j get mp add i j |
| 1043 | same = false |
| 1044 | break |
| 1045 | end |
| 1046 | end |
| 1047 | if same |
| 1048 | ret |
| 1049 | end |
| 1050 | end |
| 1051 | for j ulong n |
| 1052 | put prv j get mp add i j |
| 1053 | end |
| 1054 | put prvn 0 n |
| 1055 | r = write 1 trMAP 0 len trMAP |
| 1056 | wpid |
| 1057 | wtab |
| 1058 | wesc 2 i n |
| 1059 | wnl |
| 1060 | put cnt 5 add get cnt 5 1 |
| 1061 | end |
| 1062 | |
| 1063 | note the perms field of the maps line at mp[s, e) has x as its third byte |
| 1064 | fn mapx s ulong e ulong to bool |
| 1065 | i = s |
| 1066 | while lt i e bound 4096 |
| 1067 | if eq get mp i 32 |
| 1068 | break |
| 1069 | end |
| 1070 | i = add i 1 |
| 1071 | end |
| 1072 | if gt add i 4 e |
| 1073 | ret false |
| 1074 | end |
| 1075 | ret eq get mp add i 3 120 |
| 1076 | end |
| 1077 | |
| 1078 | fn maps to void |
| 1079 | mkpath 1 |
| 1080 | fd = fopen path 0 get pthn 0 0 0 |
| 1081 | if lt fd 0 |
| 1082 | ret |
| 1083 | end |
| 1084 | n = ulong 0 |
| 1085 | c = true |
| 1086 | while c bound 983041 |
| 1087 | r = read int fd mp n sub 983040 n |
| 1088 | if le r 0 |
| 1089 | c = false |
| 1090 | else |
| 1091 | n = add n ulong r |
| 1092 | if eq n 983040 |
| 1093 | c = false |
| 1094 | put cnt 7 add get cnt 7 1 |
| 1095 | end |
| 1096 | end |
| 1097 | end |
| 1098 | r = fclose int fd |
| 1099 | put prvn 0 0 |
| 1100 | s = ulong 0 |
| 1101 | i = ulong 0 |
| 1102 | while lt i n bound 983041 |
| 1103 | if eq get mp i 10 |
| 1104 | if ge 4000 sub i s |
| 1105 | mapline s i |
| 1106 | end |
| 1107 | s = add i 1 |
| 1108 | end |
| 1109 | i = add i 1 |
| 1110 | end |
| 1111 | end |
| 1112 | |
| 1113 | note the owner uid of PROCROOT/pid, or - when stat fails |
| 1114 | fn wuid to void |
| 1115 | mkpath 2 |
| 1116 | r = stat path 0 get pthn 0 stb 0 128 |
| 1117 | if lt r 0 |
| 1118 | wdash |
| 1119 | ret |
| 1120 | end |
| 1121 | u = ulong 0 |
| 1122 | for i ulong 4 |
| 1123 | u = or shl u 8 ulong get stb add 24 sub 3 i |
| 1124 | end |
| 1125 | if eq u ulong getuid |
| 1126 | r = write 1 tself 0 len tself |
| 1127 | ret |
| 1128 | end |
| 1129 | wnum u |
| 1130 | end |
| 1131 | |
| 1132 | fn onepid to void |
| 1133 | mkpath 0 |
| 1134 | k = readlnk path 0 get pthn 0 lnk 0 4096 |
| 1135 | if eq k -2 |
| 1136 | put cnt 6 add get cnt 6 1 |
| 1137 | ret |
| 1138 | end |
| 1139 | r = write 1 trPRC 0 len trPRC |
| 1140 | wpid |
| 1141 | wtab |
| 1142 | wuid |
| 1143 | wtab |
| 1144 | mkpath 0 |
| 1145 | put cnt 4 add get cnt 4 1 |
| 1146 | if lt k 0 |
| 1147 | wdash |
| 1148 | wtab |
| 1149 | put res 0 4 |
| 1150 | wtail |
| 1151 | ret |
| 1152 | end |
| 1153 | wesc 1 0 ulong k |
| 1154 | wtab |
| 1155 | classify |
| 1156 | wtail |
| 1157 | maps |
| 1158 | end |
| 1159 | |
| 1160 | fn isnum o ulong m ulong to bool |
| 1161 | if eq m 0 |
| 1162 | ret false |
| 1163 | end |
| 1164 | if gt m 63 |
| 1165 | ret false |
| 1166 | end |
| 1167 | for i ulong m |
| 1168 | c = get db add o i |
| 1169 | if lt c 48 |
| 1170 | ret false |
| 1171 | end |
| 1172 | if gt c 57 |
| 1173 | ret false |
| 1174 | end |
| 1175 | end |
| 1176 | ret true |
| 1177 | end |
| 1178 | |
| 1179 | fn dent o ulong to void |
| 1180 | k = add o 19 |
| 1181 | m = ulong 0 |
| 1182 | while ne get db add k m 0 bound 256 |
| 1183 | m = add m 1 |
| 1184 | end |
| 1185 | if not isnum k m |
| 1186 | ret |
| 1187 | end |
| 1188 | for i ulong m |
| 1189 | put pid i get db add k i |
| 1190 | end |
| 1191 | put pidn 0 m |
| 1192 | onepid |
| 1193 | end |
| 1194 | |
| 1195 | fn procmode to void |
| 1196 | n = get rootn 0 |
| 1197 | for i ulong n |
| 1198 | put path i get root i |
| 1199 | end |
| 1200 | fd = fopen path 0 n 16384 0 |
| 1201 | if lt fd 0 |
| 1202 | ret |
| 1203 | end |
| 1204 | while true bound 100000 |
| 1205 | r = getdnts int fd db 0 len db |
| 1206 | if le r 0 |
| 1207 | break |
| 1208 | end |
| 1209 | o = ulong 0 |
| 1210 | while lt o ulong r bound 65536 |
| 1211 | lo = ulong get db add o 16 |
| 1212 | hi = ulong get db add o 17 |
| 1213 | dent o |
| 1214 | o = add o add lo mul 256 hi |
| 1215 | end |
| 1216 | end |
| 1217 | r = fclose int fd |
| 1218 | end |
| 1219 | |
| 1220 | note ---- main ---- |
| 1221 | |
| 1222 | fn summary to void |
| 1223 | for k ulong 8 |
| 1224 | r = write 1 thash 0 2 |
| 1225 | wtbl k 4 |
| 1226 | r = write 1 tsp 0 1 |
| 1227 | wnum get cnt k |
| 1228 | wnl |
| 1229 | end |
| 1230 | end |
| 1231 | |
| 1232 | fn mode to ulong |
| 1233 | n = argread 1 arg 0 15 |
| 1234 | if eq n long len telf |
| 1235 | ok = true |
| 1236 | for i ulong len telf |
| 1237 | if ne get arg i get telf i |
| 1238 | ok = false |
| 1239 | end |
| 1240 | end |
| 1241 | if ok |
| 1242 | ret 1 |
| 1243 | end |
| 1244 | end |
| 1245 | if eq n long len tprc |
| 1246 | ok = true |
| 1247 | for i ulong len tprc |
| 1248 | if ne get arg i get tprc i |
| 1249 | ok = false |
| 1250 | end |
| 1251 | end |
| 1252 | if ok |
| 1253 | ret 2 |
| 1254 | end |
| 1255 | end |
| 1256 | ret 0 |
| 1257 | end |
| 1258 | |
| 1259 | fn main to int |
| 1260 | if lt argcnt 2 |
| 1261 | r = write 2 tuse 0 len tuse |
| 1262 | ret 64 |
| 1263 | end |
| 1264 | m = mode |
| 1265 | if eq m 0 |
| 1266 | r = write 2 tuse 0 len tuse |
| 1267 | ret 64 |
| 1268 | end |
| 1269 | if eq m 1 |
| 1270 | elfmode |
| 1271 | summary |
| 1272 | ret 0 |
| 1273 | end |
| 1274 | n = long len tdpr |
| 1275 | if ge argcnt 3 |
| 1276 | n = argread 2 root 0 4095 |
| 1277 | if lt n 1 |
| 1278 | r = write 2 tuse 0 len tuse |
| 1279 | ret 64 |
| 1280 | end |
| 1281 | else |
| 1282 | for i ulong len tdpr |
| 1283 | put root i get tdpr i |
| 1284 | end |
| 1285 | end |
| 1286 | while gt n 1 bound 4095 |
| 1287 | if ne get root sub ulong n 1 47 |
| 1288 | break |
| 1289 | end |
| 1290 | n = sub n 1 |
| 1291 | end |
| 1292 | note PROCROOT + / + a pid of up to 63 digits + /maps must fit the 4096-byte path buffer |
| 1293 | if gt n 4027 |
| 1294 | r = write 2 tuse 0 len tuse |
| 1295 | ret 64 |
| 1296 | end |
| 1297 | put rootn 0 ulong n |
| 1298 | procmode |
| 1299 | summary |
| 1300 | ret 0 |
| 1301 | end |