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