interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / host / prlimit / prlimit.om

433 lines · 8297 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/host/prlimit/prlimit.om: fixture for prlimit (keyword 235), program mode. Run by the OM-2
3note conductor through seed/host/prlimit/prlimit.cases from the repository root. Kernel truth is
4note /proc/self/limits, read independently of the word under test.
5note Checks (parent exit = number of the first failed check, 0 when all hold):
6note 1 n 16 reads RLIMIT_NOFILE: 0, both u64 words written over a 0xAA poison, cur <= max.
7note 2 the pair equals the "Max open files" soft/hard columns of /proc/self/limits.
8note 3 n 32 sets soft 40 (hard kept): 0, and the old pair lands at A[off+16..off+32).
9note 4 n 16 reads back {40, max}; /proc/self/limits shows soft 40.
10note 5 the limit acts: opening files stops with -24 (EMFILE), every descriptor below 40.
11note 6 n 32 restores the original pair; the old pair returned is {40, max}.
12note 7 n 24, 8 and 0 return -22 without a kernel call: A[16..32) keeps its poison although A[0..16)
13note holds a valid pair a set would accept.
14note 8 res 99 returns -22 (kernel EINVAL) and the output slice keeps its poison.
15note 9 pid 2147483647 returns -3 (ESRCH).
16note 10 pid 1 returns -1 (EPERM) unless this process runs with euid 0.
17note 11 n 16 reads RLIMIT_AS (9) equal to "Max address space" ("unlimited" is 2^64-1).
18note 12 a slice past the array end traps 3 (child exit 103), for n 16 and for n 32.
19note 16 a nonzero, unaligned off (9): n 16 writes the pair only to A[9..25), n 32 reads the new pair from
20note A[9..25) and writes the old pair only to A[25..41); every other byte keeps its poison.
21note Without an argument (AArch64 native) the RLIMIT_AS effect is checked across execprog; the x86linux
22note row passes "noas" because qemu-user ignores RLIMIT_AS writes:
23note 13 a child sets RLIMIT_AS soft 1 GiB and execprogs [argv0, "cap"]: the new image reads 1 GiB (exit 8).
24note 14 a child sets RLIMIT_AS soft 64 MiB and execprogs [argv0, "big"]: the 128 MiB image cannot be
25note mapped (execve ENOMEM after the point of no return), so the kernel kills it with SIGSEGV (signal 11).
26note 15 control: [argv0, "big"] without a cap runs and exits 7.
27om2
28text plim "/proc/self/limits"
29text knof "Max open files"
30text kas "Max address space"
31text cmcap "cap"
32text cmbig "big"
33text cmnoas "noas"
34global hugeimg ulong 16777216
35global a uchar 64
36global lim uchar 8192
37global pv ulong 2
38global argv uchar 4096
39global buf uchar 4096
40global status uchar 4
41global ofd ulong 64
42
43fn rd64 off ulong to ulong
44x = ulong 0
45for i ulong 8
46x = or x shl ulong get a add off i mul i 8
47end
48ret x
49end
50
51fn wr64 off ulong v ulong to void
52for i ulong 8
53put a add off i uchar and shr v mul i 8 255
54end
55end
56
57fn poison to void
58for i ulong len a
59put a i 170
60end
61end
62
63note 1 when a[lo..hi) still holds the poison byte everywhere.
64fn intact lo ulong hi ulong to ulong
65for i ulong sub hi lo
66if ne get a add lo i 170
67ret 0
68end
69end
70ret 1
71end
72
73note Read /proc/self/limits into lim; returns its length (0 on failure).
74fn loadlim to ulong
75fd = fopen plim 0 len plim 524288 0
76if lt fd 0
77ret 0
78end
79n = read int fd lim 0 len lim
80c = fclose int fd
81if le n 0
82ret 0
83end
84ret ulong n
85end
86
87note Parse one column at p (skipping spaces): a decimal or "unlimited" (2^64-1) into pv[k]; returns
88note the position after it, or 0 when neither is there.
89fn col p ulong k ulong n ulong to ulong
90q = p
91while lt q n
92if ne get lim q 32
93break
94end
95q = add q 1
96end
97if ge q n
98ret 0
99end
100if eq get lim q 117
101put pv k not ulong 0
102ret add q 9
103end
104v = ulong 0
105s = q
106while lt q n
107d = ulong get lim q
108if or lt d 48 gt d 57
109break
110end
111v = add mul v 10 sub d 48
112q = add q 1
113end
114if eq q s
115ret 0
116end
117put pv k v
118ret q
119end
120
121note Find the row named key and parse its soft/hard columns into pv; 1 on success.
122fn row key view uchar * to ulong
123n = loadlim
124if eq n 0
125ret 0
126end
127k = len key
128i = ulong 0
129while le add i k n
130hit = ulong 1
131for j ulong k
132if ne get lim add i j get key j
133hit = 0
134break
135end
136end
137if eq hit 1
138p = col add i k 0 n
139if eq p 0
140ret 0
141end
142if eq col p 1 n 0
143ret 0
144end
145ret 1
146end
147i = add i 1
148end
149ret 0
150end
151
152note 1 when argv[1] equals m.
153fn argis m view uchar * to ulong
154n = argread 1 buf 0 len buf
155if ne n long len m
156ret 0
157end
158for i ulong len m
159if ne get buf i get m i
160ret 0
161end
162end
163ret 1
164end
165
166note argv packet [argv0, m]; returns its length.
167fn mkargv m view uchar * to ulong
168n = argread 0 buf 0 len buf
169if le n 0
170exit 90
171end
172k = ulong n
173put argv 0 2
174put argv 1 0
175put argv 2 uchar and k 255
176put argv 3 uchar shr k 8
177for i ulong k
178put argv add i 4 get buf i
179end
180pos = add k 4
181put argv pos uchar len m
182put argv add pos 1 0
183for j ulong len m
184put argv add add pos 2 j get m j
185end
186ret add add pos 2 len m
187end
188
189fn reap pid long to ulong
190r = waitpid int pid status 0 len status 0
191if ne r pid
192ret 1000
193end
194if ne get status 0 0
195ret add 2000 ulong and get status 0 127
196end
197ret ulong get status 1
198end
199
200note A forked child sets RLIMIT_AS soft to cap (cap 0: no change) and execprogs [argv0, m]; returns
201note the child's exit, or 2000 + signal.
202fn capexec cap ulong m view uchar * to ulong
203an = mkargv m
204pid = fork
205if lt pid 0
206ret 1001
207end
208if eq pid 0
209if ne cap 0
210r = prlimit 0 9 a 0 16
211if ne r 0
212exit 96
213end
214wr64 0 cap
215r = prlimit 0 9 a 0 32
216if ne r 0
217exit 97
218end
219end
220r = execprog argv 0 an
221exit 98
222end
223ret reap pid
224end
225
226note A forked child calls prlimit with an out-of-range slice of a (64 bytes); returns its exit.
227fn trapkid off ulong n ulong to ulong
228pid = fork
229if lt pid 0
230ret 1001
231end
232if eq pid 0
233r = prlimit 0 7 a off n
234exit 98
235end
236ret reap pid
237end
238
239note Child modes: "cap" exits 8 when RLIMIT_AS soft reads 1 GiB (9 otherwise); "big" touches the
240note last byte of the 128 MiB global and exits 7.
241fn child to int
242if eq argis cmcap 1
243r = prlimit 0 9 a 0 16
244if and eq r 0 eq rd64 0 1073741824
245ret 8
246end
247ret 9
248end
249put hugeimg 16777215 1
250ret int add 6 get hugeimg 16777215
251end
252
253note 1-6: read, compare, set, read back, effect, restore (RLIMIT_NOFILE).
254fn nofile to int
255poison
256r = prlimit 0 7 a 0 16
257if or ne r 0 eq intact 0 16 1
258ret 1
259end
260cur = rd64 0
261max = rd64 8
262if gt cur max
263ret 1
264end
265if eq row knof 0
266ret 2
267end
268if or ne get pv 0 cur ne get pv 1 max
269ret 2
270end
271poison
272wr64 0 40
273wr64 8 max
274r = prlimit 0 7 a 0 32
275if or ne r 0 or ne rd64 16 cur ne rd64 24 max
276ret 3
277end
278poison
279r = prlimit 0 7 a 0 16
280if or ne r 0 or ne rd64 0 40 ne rd64 8 max
281ret 4
282end
283if or eq row knof 0 ne get pv 0 40
284ret 4
285end
286if ne fill 0
287ret 5
288end
289poison
290wr64 0 cur
291wr64 8 max
292r = prlimit 0 7 a 0 32
293if or ne r 0 or ne rd64 16 40 ne rd64 24 max
294ret 6
295end
296ret 0
297end
298
299note 5: open /proc/self/limits until refused; the refusal is -24 and every descriptor is below 40.
300fn fill to int
301got = ulong 0
302bad = ulong 1
303for i ulong 64
304fd = fopen plim 0 len plim 524288 0
305if lt fd 0
306if eq fd neg long 24
307bad = 0
308end
309break
310end
311put ofd got ulong fd
312got = add got 1
313if ge fd 40
314break
315end
316end
317for j ulong got
318if ge get ofd j 40
319bad = 1
320end
321c = fclose int get ofd j
322end
323ret int bad
324end
325
326note 7-12: refusals, errno and slice traps.
327fn errs to int
328for n ulong 3
329poison
330r = prlimit 0 7 a 0 16
331w = ulong 24
332if eq n 1
333w = 8
334end
335if eq n 2
336w = 0
337end
338r = prlimit 0 7 a 0 w
339if or ne r neg long 22 eq intact 16 32 0
340ret 7
341end
342end
343poison
344r = prlimit 0 99 a 0 16
345if or ne r neg long 22 eq intact 0 16 0
346ret 8
347end
348r = prlimit 2147483647 7 a 0 16
349if ne r neg long 3
350ret 9
351end
352if ne geteuid 0
353r = prlimit 1 7 a 0 16
354if ne r neg long 1
355ret 10
356end
357end
358poison
359r = prlimit 0 9 a 0 16
360if or ne r 0 eq row kas 0
361ret 11
362end
363if or ne get pv 0 rd64 0 ne get pv 1 rd64 8
364ret 11
365end
366if or ne trapkid 56 16 103 ne trapkid 40 32 103
367ret 12
368end
369ret 0
370end
371
372note 16: the slice offset reaches the kernel (n 16 and n 32 at off 9, unaligned).
373fn offs to int
374poison
375r = prlimit 0 7 a 0 16
376if ne r 0
377ret 16
378end
379cur = rd64 0
380max = rd64 8
381poison
382r = prlimit 0 7 a 9 16
383if or ne r 0 or ne rd64 9 cur ne rd64 17 max
384ret 16
385end
386if or eq intact 0 9 0 eq intact 25 64 0
387ret 16
388end
389poison
390wr64 9 cur
391wr64 17 max
392r = prlimit 0 7 a 9 32
393if or ne r 0 or ne rd64 25 cur ne rd64 33 max
394ret 16
395end
396if or eq intact 0 9 0 eq intact 41 64 0
397ret 16
398end
399ret 0
400end
401
402fn main to int
403if eq argcnt 2
404if eq argis cmnoas 0
405ret child
406end
407end
408k = nofile
409if ne k 0
410ret k
411end
412k = errs
413if ne k 0
414ret k
415end
416k = offs
417if ne k 0
418ret k
419end
420if eq argcnt 2
421ret 0
422end
423if ne capexec 1073741824 cmcap 8
424ret 13
425end
426if ne capexec 67108864 cmbig 2011
427ret 14
428end
429if ne capexec 0 cmbig 7
430ret 15
431end
432ret 0
433end