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 / om / tls / test / fuzz / common.om

254 lines · 6223 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note common.om: shared helpers of the om/tls fuzz targets (om/tls/test/fuzz/run.sh). Composed by
3note run.sh after the om/tls units, seed/net/http.om and seed/net/https.om, before one target.
4note A target reads its whole input from stdin (at most FZCAP bytes; the rest is ignored), runs the
5note library under test and returns 0. The only failure signal is a runtime trap (exit 100 + code),
6note which is what the fuzzer counts as a crash: "untrusted input must never trap" (om/tls/README.md).
7
8const FZCAP ulong 65536
9text FZHOST "www.example.test"
10
11note fz_read: stdin into b (a target's `in = new uchar FZCAP 0`); the byte count (the rest is not read)
12fn fz_read b edit uchar * to ulong
13t = ulong 0
14while lt t len b bound 70000
15r = read 0 b t sub len b t
16if le r 0
17break
18end
19t = add t ulong r
20end
21ret t
22end
23
24note fz_dnsconf: exercise the live streaming resolver parser from one fuzz input, without I/O.
25fn fz_dnsconf b view uchar * ip edit uchar * to void
26a = new uchar 15 0
27st = new ulong 3 0
28for i ulong len b bound 9223372036854775807
29c = get b i
30if eq c 10
31dns_cfgend a st ip
32else
33dns_cfgbyte a st c
34end
35end
36dns_cfgend a st ip
37end
38
39note fz_be2: big-endian 16-bit value at a[p], 0 past the end
40fn fz_be2 a view uchar * p ulong to ulong
41if gt add p 2 len a
42ret 0
43end
44ret or shl ulong get a p 8 ulong get a add p 1
45end
46
47note fz_say: "rc NNN" and a newline on stderr: the library's result code for the input, so a replay
48note (run.sh) can tell how deep a seed got; the fuzzer ignores it (only a trap is a crash)
49fn fz_say e ulong to void
50b = new uchar 7 0
51put b 0 114
52put b 1 99
53put b 2 32
54put b 3 uchar add 48 rem div e 100 10
55put b 4 uchar add 48 rem div e 10 10
56put b 5 uchar add 48 rem e 10
57put b 6 10
58r = write 2 b 0 7
59end
60
61note fz_arg: argument i into o; its length (0 when absent or too long)
62fn fz_arg i ulong o edit uchar * to ulong
63if ge i ulong argcnt
64ret 0
65end
66n = argread i o 0 len o
67if le n 0
68ret 0
69end
70ret ulong n
71end
72
73note fz_store: the PEM bundle named by argument 1 into st; 0 ok, 1 none or unreadable
74fn fz_store st edit uchar * to ulong
75p = new uchar 4096 0
76n = fz_arg 1 p
77if eq n 0
78ret 1
79end
80if ne tls_storef st part p 0 n 0
81ret 1
82end
83ret 0
84end
85
86note fz_pair: a connected AF_UNIX stream pair in one process (abstract name "omfz" + pid):
87note fds[0] the blocking client end (the TLS side), fds[1] the nonblocking server end the target
88note writes the input into. 0 ok, 1 failure (the target then returns without running the library).
89fn fz_pair fds edit ulong * to ulong
90a = new uchar 40 0
91put a 0 1
92put a 3 111
93put a 4 109
94put a 5 102
95put a 6 122
96k = ulong 7
97v = ulong getpid
98d = new uchar 20 0
99m = ulong 0
100while true bound 20
101put d m uchar add 48 rem v 10
102m = add m 1
103v = div v 10
104if eq v 0
105break
106end
107end
108for i ulong m
109put a k get d sub sub m 1 i
110k = add k 1
111end
112l = socknew 1 524289 0
113if lt l 0
114ret 1
115end
116r = bind int l a 0 k
117if eq r 0
118r = listen int l 4
119end
120if ne r 0
121x = fclose int l
122ret 1
123end
124c = socknew 1 524289 0
125if lt c 0
126x = fclose int l
127ret 1
128end
129r = connect int c a 0 k
130s = long -1
131if eq r 0
132s = accept int l 526336
133end
134x = fclose int l
135if lt s 0
136x = fclose int c
137ret 1
138end
139put fds 0 ulong c
140put fds 1 ulong s
141ret 0
142end
143
144note fz_send: as much of b[p, p+n) as the server end accepts (it is nonblocking and nobody reads
145note concurrently, so a full socket buffer ends the feed instead of hanging); 0 all sent, 1 stopped
146fn fz_send fd int b view uchar * p ulong n ulong to ulong
147d = ulong 0
148while lt d n bound 70000
149r = send fd b add p d sub n d 0
150if le r 0
151ret 1
152end
153d = add d ulong r
154end
155ret 0
156end
157
158note fz_feed: the record stream of a target, from in[p] on, written to the server end of the mirror
159note state m (m holds the peer's write key: what the client under test reads with). Chunks:
160note T L1 L0 bytes[L] T < 128: one record sealed by tls_wrbuf with inner content type T;
161note T >= 128: the bytes sent raw (forged, tampered or plaintext records).
162note L is capped at 16384 and at the input end. A sealed handshake chunk starting with 24
163note (KeyUpdate) also advances m's key like the client's tls_kupd, so later chunks stay readable.
164note 0 when every chunk went out, 1 when the socket buffer filled or sealing refused.
165fn fz_feed m edit uchar * in view uchar * p0 ulong to ulong
166p = p0
167while le add p 3 len in bound 70000
168n = fz_be2 in add p 1
169if gt n sub sub len in p 3
170n = sub sub len in p 3
171end
172if gt n 16384
173n = 16384
174end
175if ne fz_chunk m part in p add n 3 0
176ret 1
177end
178p = add add p n 3
179end
180ret 0
181end
182
183note fz_chunk: one chunk c = T L1 L0 bytes (its length already clipped); 0 sent, 1 stopped
184fn fz_chunk m edit uchar * c view uchar * to ulong
185e = new uchar 1 0
186ty = ulong get c 0
187n = sub len c 3
188if ge ty 128
189ret fz_send tls_fd m c 3 n
190end
191for i ulong n
192put m add add TLWB 5 i get c add 3 i
193end
194if ne tls_wrbuf m ty n 0
195ret 1
196end
197if and eq ty 22 eq tls_at c 3 24
198r = tls_drv m tls_sel TLSAP TLSAP 32 TLIUPD part e 0 0
199r = or r tls_keys m TLSAP 0
200end
201ret 0
202end
203
204note fz_fill: b[o, o+n) = v, v+1, ... (fixed key material shared by a client state and its mirror)
205fn fz_fill b edit uchar * o ulong n ulong v ulong to void
206for i ulong n
207put b add o i uchar and add v i 255
208end
209end
210
211note fz_dnsq: rest = H name[H] response; dns_parse of the response to dns_query(name, id 0x4f4d)
212note (a name dns_query refuses falls back to FZHOST). Shared by fz_dns and the fz_plant control.
213fn fz_dnsq rest view uchar * to void
214q = new uchar 300 0
215ip = new uchar 4 0
216h = tls_at rest 0
217if gt h sub len rest 1
218h = sub len rest 1
219end
220ql = dns_query q part rest 1 h 0x4f4d
221if eq ql 0
222ql = dns_query q FZHOST 0x4f4d
223end
224e = dns_parse part rest add h 1 sub sub len rest h 1 part q 0 ql ip
225end
226
227
228note fz_load: the file named by argument i into o; its length (0 when absent, unreadable or empty)
229fn fz_load i ulong o edit uchar * to ulong
230p = new uchar 4096 0
231k = fz_arg i p
232if eq k 0
233ret 0
234end
235f = fopen p 0 k 524288 0
236if lt f 0
237ret 0
238end
239m = read int f o 0 len o
240x = fclose int f
241if le m 0
242ret 0
243end
244ret ulong m
245end
246
247note fz_hq: hq = now 1792972800 (8 bytes, big-endian) || FZHOST; its length
248fn fz_hq hq edit uchar * to ulong
249tls_pn hq 0 1792972800 8
250for i ulong len FZHOST
251put hq add 8 i get FZHOST i
252end
253ret add 8 len FZHOST
254end