Debian Codegraph @

fuzzer/fuzzer_fragment.c

raw
1 /*-
2 * Copyright (c) 2020 Yuquan Wang
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <usrsctp.h>
33 #include "../programs/programs_helper.h"
34 #define FUZZ_B_RESERVED1 (1 << 0)
35 #define SACK_FLAG (1 << 1)
36 #define FUZZ_B_RESERVED3 (1 << 2)
37 #define FUZZ_B_RESERVED4 (1 << 3)
38 #define NR_SACK_FLAG (1 << 4)
39 #define FUZZ_B_RESERVED5 (1 << 5)
40 #define I_DATA_FLAG (1 << 6)
41 #define FUZZ_B_RESERVED6 (1 << 7)
42
43 #define fuzzer_printf(...)
44 #define BUFFER_SIZE 4096
45 #define COMMON_HEADER_SIZE 12
46 #define SCTP_INTERLEAVING_SUPPORTED 0x00001206
47 #define SEND_DATA_SIZE 4096
48 static uint32_t assoc_vtag = 0;
49
50 static void
51 handle_upcall(struct socket *sock, void *arg, int flgs)
52 {
53 fuzzer_printf("handle_upcall()\n");
54 int events = usrsctp_get_events(sock);
55
56 while (events & SCTP_EVENT_READ) {
57 struct sctp_recvv_rn rn;
58 ssize_t n;
59 struct sockaddr_in addr;
60 char *buf = calloc(1, BUFFER_SIZE);
61 int flags = 0;
62 socklen_t len = (socklen_t)sizeof(struct sockaddr_in);
63 unsigned int infotype = 0;
64 socklen_t infolen = sizeof(struct sctp_recvv_rn);
65 memset(&rn, 0, sizeof(struct sctp_recvv_rn));
66 n = usrsctp_recvv(sock, buf, BUFFER_SIZE, (struct sockaddr *) &addr, &len, (void *)&rn, &infolen, &infotype, &flags);
67 fuzzer_printf("usrsctp_recvv() - returned %zd\n", n);
68
69 if (flags & MSG_NOTIFICATION) {
70 fuzzer_printf("NOTIFICATION received\n");
71 #ifdef FUZZ_VERBOSE
72 handle_notification((union sctp_notification *)buf, n);
73 #endif // FUZZ_VERBOSE
74 } else {
75 fuzzer_printf("DATA received\n");
76 }
77
78 free(buf);
79
80 if (n <= 0) {
81 break;
82 }
83
84 events = usrsctp_get_events(sock);
85 }
86 }
87
88
89 static int
90 conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
91 {
92 struct sctp_init_chunk *init_chunk;
93 const char *init_chunk_first_bytes = "\x13\x88\x13\x89\x00\x00\x00\x00\x00\x00\x00\x00\x01";
94 // Looking for the outgoing VTAG.
95 // length >= (COMMON_HEADER_SIZE + 16 (min size of INIT))
96 // If the common header has no VTAG (all zero), we're assuming it carries an INIT
97 if ((length >= (COMMON_HEADER_SIZE + 16)) && (memcmp(buf, init_chunk_first_bytes, COMMON_HEADER_SIZE) == 0)) {
98 init_chunk = (struct sctp_init_chunk*) ((char *)buf + sizeof(struct sctp_common_header));
99 fuzzer_printf("Found outgoing INIT, extracting VTAG : %u\n", init_chunk->initiate_tag);
100 assoc_vtag = init_chunk->initiate_tag;
101 }
102
103 return (0);
104 }
105
106
107 int
108 initialize_fuzzer(void) {
109 usrsctp_init(0, conn_output, NULL);
110
111 usrsctp_enable_crc32c_offload();
112
113 #ifdef SCTP_DEBUG
114 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
115 #endif // SCTP_DEBUG
116
117 usrsctp_register_address((void *)1);
118 usrsctp_sysctl_set_sctp_pktdrop_enable(1);
119 usrsctp_sysctl_set_sctp_nrsack_enable(1);
120 fuzzer_printf("usrsctp initialized\n");
121
122 return (1);
123 }
124
125 int
126 LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size)
127 {
128 static int initialized;
129 char *fuzz_packet_buffer;
130 size_t fuzz_data_count;
131 struct socket *socket_client;
132 struct linger so_linger;
133 struct sctp_initmsg initmsg;
134 struct sctp_event event;
135 struct sockaddr_conn sconn;
136 struct sctp_common_header* common_header;
137 struct sctp_assoc_value assoc_val;
138 int result;
139 int optval;
140 unsigned long i;
141 char* send_data_buffer;
142 uint16_t event_types[] = {
143 SCTP_ASSOC_CHANGE,
144 SCTP_PEER_ADDR_CHANGE,
145 SCTP_REMOTE_ERROR,
146 SCTP_SEND_FAILED,
147 SCTP_SHUTDOWN_EVENT,
148 SCTP_ADAPTATION_INDICATION,
149 SCTP_PARTIAL_DELIVERY_EVENT,
150 SCTP_AUTHENTICATION_EVENT,
151 SCTP_STREAM_RESET_EVENT,
152 SCTP_SENDER_DRY_EVENT,
153 SCTP_ASSOC_RESET_EVENT,
154 SCTP_STREAM_CHANGE_EVENT,
155 SCTP_SEND_FAILED_EVENT
156 };
157 char fuzz_init_ack[] = "\x13\x89\x13\x88\x49\xa4\xac\xb2\x00\x00\x00\x00\x02\x00\x01\xb4" \
158 "\x2b\xe8\x47\x40\x00\x1c\x71\xc7\xff\xff\xff\xff\xed\x69\x58\xec" \
159 "\xc0\x06\x00\x08\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04" \
160 "\x80\x08\x00\x0b\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
161 "\x40\x39\xcf\x32\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6" \
162 "\x2f\xb7\x81\x96\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa" \
163 "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
164 "\x00\x07\x01\x50\x4b\x41\x4d\x45\x2d\x42\x53\x44\x20\x31\x2e\x31" \
165 "\x00\x00\x00\x00\x64\xdb\x63\x00\x00\x00\x00\x00\xc9\x76\x03\x00" \
166 "\x00\x00\x00\x00\x60\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
167 "\xb2\xac\xa4\x49\x2b\xe8\x47\x40\xd4\xc9\x79\x52\x00\x00\x00\x00" \
168 "\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\xd4\xc9\x79\x53" \
169 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00" \
170 "\x00\x00\x00\x00\x5a\x76\x13\x89\x01\x00\x00\x00\x00\x00\x00\x00" \
171 "\x00\x00\x00\x00\x01\x00\x00\x62\x49\xa4\xac\xb2\x00\x1c\x71\xc7" \
172 "\x00\x01\xff\xff\x82\xe6\xc8\x44\x80\x00\x00\x04\xc0\x00\x00\x04" \
173 "\x80\x08\x00\x0b\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
174 "\xb6\xbb\xb5\x7f\xbb\x4b\x0e\xb5\x42\xf6\x75\x18\x4f\x79\x0f\x24" \
175 "\x1c\x44\x0b\xd6\x62\xa9\x84\xe7\x2c\x3c\x7f\xad\x1b\x67\x81\x57" \
176 "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
177 "\x00\x0c\x00\x06\x00\x05\x00\x00\x02\x00\x01\xb4\x2b\xe8\x47\x40" \
178 "\x00\x1c\x71\xc7\x00\x01\xff\xff\xed\x69\x58\xec\xc0\x06\x00\x08" \
179 "\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04\x80\x08\x00\x0b" \
180 "\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24\x40\x39\xcf\x32" \
181 "\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6\x2f\xb7\x81\x96" \
182 "\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa\x80\x04\x00\x08" \
183 "\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00\x81\xe1\x1e\x81" \
184 "\xea\x41\xeb\xf0\x12\xd9\x74\xbe\x13\xfd\x4b\x6c\x5c\xa2\x8f\x00";
185 char fuzz_init_ack_nrsack_support[] = "\x13\x89\x13\x88\x49\xa4\xac\xb2\x00\x00\x00\x00\x02\x00\x01\xb4" \
186 "\x2b\xe8\x47\x40\x00\x1c\x71\xc7\xff\xff\xff\xff\xed\x69\x58\xec" \
187 "\xc0\x06\x00\x08\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04" \
188 "\x80\x08\x00\x0b\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
189 "\x40\x39\xcf\x32\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6" \
190 "\x2f\xb7\x81\x96\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa" \
191 "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
192 "\x00\x07\x01\x50\x4b\x41\x4d\x45\x2d\x42\x53\x44\x20\x31\x2e\x31" \
193 "\x00\x00\x00\x00\x64\xdb\x63\x00\x00\x00\x00\x00\xc9\x76\x03\x00" \
194 "\x00\x00\x00\x00\x60\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
195 "\xb2\xac\xa4\x49\x2b\xe8\x47\x40\xd4\xc9\x79\x52\x00\x00\x00\x00" \
196 "\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\xd4\xc9\x79\x53" \
197 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00" \
198 "\x00\x00\x00\x00\x5a\x76\x13\x89\x01\x00\x00\x00\x00\x00\x00\x00" \
199 "\x00\x00\x00\x00\x01\x00\x00\x62\x49\xa4\xac\xb2\x00\x1c\x71\xc7" \
200 "\x00\x01\xff\xff\x82\xe6\xc8\x44\x80\x00\x00\x04\xc0\x00\x00\x04" \
201 "\x80\x08\x00\x0b\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
202 "\xb6\xbb\xb5\x7f\xbb\x4b\x0e\xb5\x42\xf6\x75\x18\x4f\x79\x0f\x24" \
203 "\x1c\x44\x0b\xd6\x62\xa9\x84\xe7\x2c\x3c\x7f\xad\x1b\x67\x81\x57" \
204 "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
205 "\x00\x0c\x00\x06\x00\x05\x00\x00\x02\x00\x01\xb4\x2b\xe8\x47\x40" \
206 "\x00\x1c\x71\xc7\x00\x01\xff\xff\xed\x69\x58\xec\xc0\x06\x00\x08" \
207 "\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04\x80\x08\x00\x0b" \
208 "\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24\x40\x39\xcf\x32" \
209 "\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6\x2f\xb7\x81\x96" \
210 "\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa\x80\x04\x00\x08" \
211 "\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00\x81\xe1\x1e\x81" \
212 "\xea\x41\xeb\xf0\x12\xd9\x74\xbe\x13\xfd\x4b\x6c\x5c\xa2\x8f\x00";
213 char fuzz_cookie_ack[] = "\x13\x89\x13\x88\xb7\x0d\x32\x66\x00\x00\x00\x00\x0b\x00\x00\x04";
214
215 char data_common_headr[] = "\x13\x89\x13\x88\xb7\x0d\x32\x66\x00\x00\x00\x00";
216
217 if (!initialized) {
218 initialized = initialize_fuzzer();
219 }
220
221 if (data_size < 10) {
222 // Skip too small packets
223 fuzzer_printf("data_size %zu makes no sense, skipping\n", data_size);
224 return (0);
225 }
226
227 socket_client = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, 0);
228 FUZZER_ASSERT(socket_client != NULL);
229
230 usrsctp_set_non_blocking(socket_client, 1);
231
232 memset(&initmsg, 1, sizeof(struct sctp_initmsg));
233 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(struct sctp_initmsg));
234 FUZZER_ASSERT(result == 0);
235
236 so_linger.l_onoff = 1;
237 so_linger.l_linger = 0;
238 result = usrsctp_setsockopt(socket_client, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger));
239 FUZZER_ASSERT(result == 0);
240
241 memset(&event, 0, sizeof(event));
242 event.se_on = 1;
243 for (i = 0; i < (sizeof(event_types) / sizeof(uint16_t)); i++) {
244 event.se_type = event_types[i];
245 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event));
246 }
247
248 optval = 1;
249 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_RECVRCVINFO, &optval, sizeof(optval));
250 FUZZER_ASSERT(result == 0);
251
252 optval = 1;
253 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_RECVNXTINFO, &optval, sizeof(optval));
254 FUZZER_ASSERT(result == 0);
255
256 if (data[0] & I_DATA_FLAG) {
257 // set the program supporting I-DATA
258 optval = 2;
259 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, &optval, sizeof(optval));
260 FUZZER_ASSERT(result == 0);
261
262 memset(&assoc_val, 0, sizeof(assoc_val));
263 assoc_val.assoc_value = 1;
264 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_INTERLEAVING_SUPPORTED, &assoc_val, sizeof(assoc_val));
265 FUZZER_ASSERT(result == 0);
266 }
267
268 optval = 1;
269 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_REUSE_PORT, &optval, sizeof(optval));
270 FUZZER_ASSERT(result == 0);
271
272 memset(&sconn, 0, sizeof(struct sockaddr_conn));
273 sconn.sconn_family = AF_CONN;
274 #ifdef HAVE_SCONN_LEN
275 sconn.sconn_len = sizeof(struct sockaddr_conn);
276 #endif // HAVE_SCONN_LEN
277 sconn.sconn_port = htons(5000);
278 sconn.sconn_addr = (void *)1;
279
280 result = usrsctp_bind(socket_client, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
281 FUZZER_ASSERT(result == 0);
282
283 optval = 1;
284 result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_NODELAY, &optval, sizeof(optval));
285 FUZZER_ASSERT(result == 0);
286
287 usrsctp_set_upcall(socket_client, handle_upcall, NULL);
288
289 memset(&sconn, 0, sizeof(struct sockaddr_conn));
290 sconn.sconn_family = AF_CONN;
291 #ifdef HAVE_SCONN_LEN
292 sconn.sconn_len = sizeof(struct sockaddr_conn);
293 #endif // HAVE_SCONN_LEN
294 sconn.sconn_port = htons(5001);
295 sconn.sconn_addr = (void *)1;
296
297 result = usrsctp_connect(socket_client, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
298 FUZZER_ASSERT(result == 0 || errno == EINPROGRESS);
299
300 if (data[0] & NR_SACK_FLAG) {
301 common_header = (struct sctp_common_header*) fuzz_init_ack_nrsack_support;
302 common_header->verification_tag = assoc_vtag;
303 usrsctp_conninput((void *)1, fuzz_init_ack_nrsack_support, 448, 0);
304 } else {
305 common_header = (struct sctp_common_header*) fuzz_init_ack;
306 common_header->verification_tag = assoc_vtag;
307 usrsctp_conninput((void *)1, fuzz_init_ack, 448, 0);
308 }
309
310
311 common_header = (struct sctp_common_header*) fuzz_cookie_ack;
312 common_header->verification_tag = assoc_vtag;
313 usrsctp_conninput((void *)1, fuzz_cookie_ack, 16, 0);
314
315 fuzz_data_count = 0;
316 while (fuzz_data_count+4 < data_size) {
317 size_t data_chunk_size = (data[fuzz_data_count+2]<<8) + data[fuzz_data_count+3];
318 if (data_chunk_size == 0) {
319 break;
320 }
321 if (fuzz_data_count + data_chunk_size > data_size) {
322 data_chunk_size = data_size - fuzz_data_count;
323 }
324 if (data[fuzz_data_count] & NR_SACK_FLAG ||
325 data[fuzz_data_count] & SACK_FLAG) {
326 send_data_buffer = malloc(SEND_DATA_SIZE);
327 FUZZER_ASSERT(send_data_buffer != NULL );
328 memset(send_data_buffer, 1, SEND_DATA_SIZE);
329 fuzzer_printf("Calling usrsctp_sendv()\n");
330 usrsctp_sendv(socket_client, send_data_buffer, SEND_DATA_SIZE, NULL, 0, NULL, 0, SCTP_SENDV_NOINFO, 0);
331 free(send_data_buffer);
332 }
333 fuzz_packet_buffer = malloc(data_chunk_size + COMMON_HEADER_SIZE);
334 memcpy(fuzz_packet_buffer, data_common_headr, COMMON_HEADER_SIZE); // common header
335 memcpy(fuzz_packet_buffer + COMMON_HEADER_SIZE, data+fuzz_data_count, data_chunk_size);
336 usrsctp_conninput((void *)1, fuzz_packet_buffer, data_chunk_size + COMMON_HEADER_SIZE, 0);
337 free(fuzz_packet_buffer);
338 fuzz_data_count += data_chunk_size;
339 }
340
341 usrsctp_close(socket_client);
342
343 return (0);
344 }