interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / test / envprobe.om
| 1 | om2 |
| 2 | note envprobe (test-only, sandbox gate). Runs inside the sandbox in place of forged. |
| 3 | note For /proc/self and for every process visible in /proc it reads environ (at most |
| 4 | note 1048576 bytes) and says whether the canary given as argv[1] occurs in it. |
| 5 | note Exit 0: absent everywhere. 1: present somewhere. 2: usage error or /proc not listable. |
| 6 | note Linux ARM64: O_RDONLY|O_CLOEXEC 524288; O_DIRECTORY|O_CLOEXEC 540672 (asm/fcntl.h). |
| 7 | note getdents64 records: d_reclen at byte 16 (little-endian u16), name from byte 19, NUL-ended. |
| 8 | global ndl uchar 256 |
| 9 | global buf uchar 1048576 |
| 10 | global pth uchar 64 |
| 11 | global dents uchar 65536 |
| 12 | global num uchar 32 |
| 13 | global tally ulong 2 |
| 14 | text tproc "/proc" |
| 15 | text tself "/proc/self/environ" |
| 16 | text tpre "/proc/" |
| 17 | text tenv "/environ" |
| 18 | text tlead "environ " |
| 19 | text tbytes " bytes " |
| 20 | text tyes " canary PRESENT\n" |
| 21 | text tno " canary absent\n" |
| 22 | text tnord " unreadable\n" |
| 23 | text tseen "processes seen " |
| 24 | text tnl "\n" |
| 25 | fn putdec v ulong to void |
| 26 | x = v |
| 27 | i = ulong 32 |
| 28 | while gt i 0 bound 32 |
| 29 | i = sub i 1 |
| 30 | put num i add uchar rem x 10 48 |
| 31 | x = div x 10 |
| 32 | if eq x 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | k = write 1 num i sub 32 i |
| 37 | end |
| 38 | fn found n ulong m ulong to bool |
| 39 | if or eq m 0 lt n m |
| 40 | ret false |
| 41 | end |
| 42 | for i ulong add sub n m 1 bound 1048577 |
| 43 | j = ulong 0 |
| 44 | while lt j m bound 257 |
| 45 | if ne get buf add i j get ndl j |
| 46 | break |
| 47 | end |
| 48 | j = add j 1 |
| 49 | end |
| 50 | if eq j m |
| 51 | ret true |
| 52 | end |
| 53 | end |
| 54 | ret false |
| 55 | end |
| 56 | fn probe pl ulong m ulong to int |
| 57 | k = write 1 tlead 0 len tlead |
| 58 | k = write 1 pth 0 pl |
| 59 | fd = fopen pth 0 pl 524288 0 |
| 60 | if lt fd 0 |
| 61 | k = write 1 tnord 0 len tnord |
| 62 | ret 0 |
| 63 | end |
| 64 | n = ulong 0 |
| 65 | while lt n len buf bound 1048577 |
| 66 | r = read int fd buf n sub len buf n |
| 67 | if le r 0 |
| 68 | break |
| 69 | end |
| 70 | n = add n ulong r |
| 71 | end |
| 72 | c = fclose int fd |
| 73 | k = write 1 tbytes 0 len tbytes |
| 74 | putdec n |
| 75 | if found n m |
| 76 | k = write 1 tyes 0 len tyes |
| 77 | ret 1 |
| 78 | end |
| 79 | k = write 1 tno 0 len tno |
| 80 | ret 0 |
| 81 | end |
| 82 | fn digits o ulong to ulong |
| 83 | note Length of an all-digit name at dents[o+19..], or 0 when it is not a process id. |
| 84 | n = ulong 0 |
| 85 | while lt add add o 19 n len dents bound 64 |
| 86 | c = get dents add add o 19 n |
| 87 | if eq c 0 |
| 88 | ret n |
| 89 | end |
| 90 | if or lt c 48 gt c 57 |
| 91 | ret 0 |
| 92 | end |
| 93 | n = add n 1 |
| 94 | end |
| 95 | ret 0 |
| 96 | end |
| 97 | fn mkpath o ulong n ulong to ulong |
| 98 | for i ulong len tpre |
| 99 | put pth i get tpre i |
| 100 | end |
| 101 | p = len tpre |
| 102 | for i ulong n bound 20 |
| 103 | put pth add p i get dents add add o 19 i |
| 104 | end |
| 105 | p = add p n |
| 106 | for i ulong len tenv |
| 107 | put pth add p i get tenv i |
| 108 | end |
| 109 | ret add p len tenv |
| 110 | end |
| 111 | fn batch g ulong m ulong to void |
| 112 | note One getdents64 batch of g bytes. Totals go to tally: [0] processes seen, [1] canary hits. |
| 113 | o = ulong 0 |
| 114 | while lt o g bound 65536 |
| 115 | rl = or ulong get dents add o 16 shl ulong get dents add o 17 8 |
| 116 | n = digits o |
| 117 | if and gt n 0 le n 20 |
| 118 | pl = mkpath o n |
| 119 | h = probe pl m |
| 120 | put tally 0 add get tally 0 1 |
| 121 | put tally 1 add get tally 1 ulong h |
| 122 | end |
| 123 | if eq rl 0 |
| 124 | break |
| 125 | end |
| 126 | o = add o rl |
| 127 | end |
| 128 | end |
| 129 | fn walk m ulong to int |
| 130 | pd = fopen tproc 0 len tproc 540672 0 |
| 131 | if lt pd 0 |
| 132 | ret 2 |
| 133 | end |
| 134 | g = getdnts int pd dents 0 len dents |
| 135 | while gt g 0 bound 100000 |
| 136 | batch ulong g m |
| 137 | g = getdnts int pd dents 0 len dents |
| 138 | end |
| 139 | c = fclose int pd |
| 140 | k = write 1 tseen 0 len tseen |
| 141 | putdec get tally 0 |
| 142 | k = write 1 tnl 0 len tnl |
| 143 | if lt g 0 |
| 144 | ret 2 |
| 145 | end |
| 146 | if gt get tally 1 0 |
| 147 | ret 1 |
| 148 | end |
| 149 | ret 0 |
| 150 | end |
| 151 | fn main to int |
| 152 | if ne argcnt 2 |
| 153 | ret 2 |
| 154 | end |
| 155 | m = argread 1 ndl 0 len ndl |
| 156 | if le m 0 |
| 157 | ret 2 |
| 158 | end |
| 159 | for i ulong len tself |
| 160 | put pth i get tself i |
| 161 | end |
| 162 | hit = probe len tself ulong m |
| 163 | w = walk ulong m |
| 164 | if eq w 2 |
| 165 | ret 2 |
| 166 | end |
| 167 | ret or hit w |
| 168 | end |