Debian Codegraph @

fuzzer/fuzzer_listen.c

raw
1 /*
2 * Copyright (C) 2017-2020 Felix Weinrank
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 * 3. Neither the name of the project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <usrsctp.h>
35 #include "../programs/programs_helper.h"
36
37 #define FUZZ_FAST 1
38
39 #ifdef FUZZ_VERBOSE
40 #define fuzzer_printf(...) debug_printf(__VA_ARGS__)
41 #else
42 #define fuzzer_printf(...)
43 #endif
44
45 struct sockaddr_conn sconn;
46 struct socket *s_l;
47
48 static void
49 dump_packet(const void *buffer, size_t bufferlen, int inout)
50 {
51 #ifdef FUZZ_VERBOSE
52 char *dump_buf;
53
54 if ((dump_buf = usrsctp_dumppacket(buffer, bufferlen, inout)) != NULL) {
55 fprintf(stderr, "%s", dump_buf);
56 usrsctp_freedumpbuffer(dump_buf);
57 }
58 #endif // FUZZ_VERBOSE
59 }
60
61 static int
62 conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
63 {
64 dump_packet(buf, length, SCTP_DUMP_OUTBOUND);
65 return (0);
66 }
67
68 static void
69 handle_upcall(struct socket *sock, void *arg, int flgs)
70 {
71 struct socket *conn_sock;
72 struct sockaddr_in remote_addr;
73 socklen_t addr_len = sizeof(struct sockaddr_in);
74
75 memset(&remote_addr, 0, sizeof(struct sockaddr_in));
76 if (((conn_sock = usrsctp_accept(sock, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) && (errno != EINPROGRESS)) {
77 perror("usrsctp_accept");
78 }
79
80 usrsctp_close(conn_sock);
81 }
82
83 int
84 init_fuzzer(void) {
85 static uint8_t initialized = 0;
86 struct sctp_event event;
87 uint16_t event_types[] = {
88 SCTP_ASSOC_CHANGE,
89 SCTP_PEER_ADDR_CHANGE,
90 SCTP_SEND_FAILED_EVENT,
91 SCTP_REMOTE_ERROR,
92 SCTP_SHUTDOWN_EVENT,
93 SCTP_ADAPTATION_INDICATION,
94 SCTP_PARTIAL_DELIVERY_EVENT};
95 unsigned long i;
96 struct linger so_linger;
97 int result;
98
99 #if defined(FUZZ_FAST)
100 if (initialized) {
101 return 0;
102 }
103 #endif
104
105 #ifdef FUZZ_VERBOSE
106 usrsctp_init(0, conn_output, debug_printf_stack);
107 #else
108 usrsctp_init(0, conn_output, NULL);
109 #endif
110
111 usrsctp_enable_crc32c_offload();
112
113 #ifdef SCTP_DEBUG
114 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
115 #endif
116 usrsctp_register_address((void *)1);
117
118 if ((s_l = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, 0)) == NULL) {
119 perror("usrsctp_socket");
120 exit(EXIT_FAILURE);
121 }
122 usrsctp_set_non_blocking(s_l, 1);
123
124 /* Bind the server side. */
125 memset(&sconn, 0, sizeof(struct sockaddr_conn));
126 sconn.sconn_family = AF_CONN;
127 #ifdef HAVE_SCONN_LEN
128 sconn.sconn_len = sizeof(struct sockaddr_conn);
129 #endif
130 sconn.sconn_port = htons(5001);
131 sconn.sconn_addr = (void *)1;
132 if (usrsctp_bind(s_l, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
133 perror("usrsctp_bind");
134 exit(EXIT_FAILURE);
135 }
136
137 memset(&event, 0, sizeof(event));
138 event.se_assoc_id = SCTP_FUTURE_ASSOC;
139 event.se_on = 1;
140 for (i = 0; i < sizeof(event_types)/sizeof(uint16_t); i++) {
141 event.se_type = event_types[i];
142 if (usrsctp_setsockopt(s_l, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) {
143 perror("setsockopt SCTP_EVENT s_l");
144 exit(EXIT_FAILURE);
145 }
146 }
147
148 /* Make server side passive... */
149 if (usrsctp_listen(s_l, 1) < 0) {
150 perror("usrsctp_listen");
151 exit(EXIT_FAILURE);
152 }
153
154 so_linger.l_onoff = 1;
155 so_linger.l_linger = 0;
156 result = usrsctp_setsockopt(s_l, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger));
157 FUZZER_ASSERT(result == 0);
158
159 usrsctp_set_upcall(s_l, handle_upcall, NULL);
160
161 initialized = 1;
162
163 return (0);
164 }
165
166 int
167 LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size)
168 {
169 init_fuzzer();
170
171 if (data_size < 8 || data_size > 65535) {
172 // Skip too small and too large packets
173 return (0);
174 }
175
176 dump_packet(data, data_size, SCTP_DUMP_INBOUND);
177
178 usrsctp_conninput((void *)1, data, data_size, 0);
179
180 #if !defined(FUZZ_FAST)
181 usrsctp_close(s_l);
182 while (usrsctp_finish() != 0) {
183 //sleep(1);
184 }
185 #endif
186
187 return (0);
188 }
189
190