interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / envprobe.om

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