fuzzer_fragment.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <usrsctp.h>
  32. #include "../programs/programs_helper.h"
  33. #define FUZZ_B_RESERVED1 (1 << 0)
  34. #define SACK_FLAG (1 << 1)
  35. #define FUZZ_B_RESERVED3 (1 << 2)
  36. #define FUZZ_B_RESERVED4 (1 << 3)
  37. #define NR_SACK_FLAG (1 << 4)
  38. #define FUZZ_B_RESERVED5 (1 << 5)
  39. #define I_DATA_FLAG (1 << 6)
  40. #define FUZZ_B_RESERVED6 (1 << 7)
  41. #define fuzzer_printf(...)
  42. #define BUFFER_SIZE 4096
  43. #define COMMON_HEADER_SIZE 12
  44. #define SCTP_INTERLEAVING_SUPPORTED 0x00001206
  45. #define SEND_DATA_SIZE 4096
  46. static uint32_t assoc_vtag = 0;
  47. static void
  48. handle_upcall(struct socket *sock, void *arg, int flgs)
  49. {
  50. fuzzer_printf("handle_upcall()\n");
  51. int events = usrsctp_get_events(sock);
  52. while (events & SCTP_EVENT_READ) {
  53. struct sctp_recvv_rn rn;
  54. ssize_t n;
  55. struct sockaddr_in addr;
  56. char *buf = calloc(1, BUFFER_SIZE);
  57. int flags = 0;
  58. socklen_t len = (socklen_t)sizeof(struct sockaddr_in);
  59. unsigned int infotype = 0;
  60. socklen_t infolen = sizeof(struct sctp_recvv_rn);
  61. memset(&rn, 0, sizeof(struct sctp_recvv_rn));
  62. n = usrsctp_recvv(sock, buf, BUFFER_SIZE, (struct sockaddr *) &addr, &len, (void *)&rn, &infolen, &infotype, &flags);
  63. fuzzer_printf("usrsctp_recvv() - returned %zd\n", n);
  64. if (flags & MSG_NOTIFICATION) {
  65. fuzzer_printf("NOTIFICATION received\n");
  66. #ifdef FUZZ_VERBOSE
  67. handle_notification((union sctp_notification *)buf, n);
  68. #endif // FUZZ_VERBOSE
  69. } else {
  70. fuzzer_printf("DATA received\n");
  71. }
  72. free(buf);
  73. if (n <= 0) {
  74. break;
  75. }
  76. events = usrsctp_get_events(sock);
  77. }
  78. }
  79. static int
  80. conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
  81. {
  82. struct sctp_init_chunk *init_chunk;
  83. const char *init_chunk_first_bytes = "\x13\x88\x13\x89\x00\x00\x00\x00\x00\x00\x00\x00\x01";
  84. // Looking for the outgoing VTAG.
  85. // length >= (COMMON_HEADER_SIZE + 16 (min size of INIT))
  86. // If the common header has no VTAG (all zero), we're assuming it carries an INIT
  87. if ((length >= (COMMON_HEADER_SIZE + 16)) && (memcmp(buf, init_chunk_first_bytes, COMMON_HEADER_SIZE) == 0)) {
  88. init_chunk = (struct sctp_init_chunk*) ((char *)buf + sizeof(struct sctp_common_header));
  89. fuzzer_printf("Found outgoing INIT, extracting VTAG : %u\n", init_chunk->initiate_tag);
  90. assoc_vtag = init_chunk->initiate_tag;
  91. }
  92. return (0);
  93. }
  94. int
  95. initialize_fuzzer(void) {
  96. usrsctp_init(0, conn_output, NULL);
  97. usrsctp_enable_crc32c_offload();
  98. #ifdef SCTP_DEBUG
  99. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
  100. #endif // SCTP_DEBUG
  101. usrsctp_register_address((void *)1);
  102. usrsctp_sysctl_set_sctp_pktdrop_enable(1);
  103. usrsctp_sysctl_set_sctp_nrsack_enable(1);
  104. fuzzer_printf("usrsctp initialized\n");
  105. return (1);
  106. }
  107. int
  108. LLVMFuzzerTestOneInput(const uint8_t* data, size_t data_size)
  109. {
  110. static int initialized;
  111. char *fuzz_packet_buffer;
  112. size_t fuzz_data_count;
  113. struct socket *socket_client;
  114. struct linger so_linger;
  115. struct sctp_initmsg initmsg;
  116. struct sctp_event event;
  117. struct sockaddr_conn sconn;
  118. struct sctp_common_header* common_header;
  119. struct sctp_assoc_value assoc_val;
  120. int result;
  121. int optval;
  122. unsigned long i;
  123. char* send_data_buffer;
  124. uint16_t event_types[] = {
  125. SCTP_ASSOC_CHANGE,
  126. SCTP_PEER_ADDR_CHANGE,
  127. SCTP_REMOTE_ERROR,
  128. SCTP_SEND_FAILED,
  129. SCTP_SHUTDOWN_EVENT,
  130. SCTP_ADAPTATION_INDICATION,
  131. SCTP_PARTIAL_DELIVERY_EVENT,
  132. SCTP_AUTHENTICATION_EVENT,
  133. SCTP_STREAM_RESET_EVENT,
  134. SCTP_SENDER_DRY_EVENT,
  135. SCTP_ASSOC_RESET_EVENT,
  136. SCTP_STREAM_CHANGE_EVENT,
  137. SCTP_SEND_FAILED_EVENT
  138. };
  139. char fuzz_init_ack[] = "\x13\x89\x13\x88\x49\xa4\xac\xb2\x00\x00\x00\x00\x02\x00\x01\xb4" \
  140. "\x2b\xe8\x47\x40\x00\x1c\x71\xc7\xff\xff\xff\xff\xed\x69\x58\xec" \
  141. "\xc0\x06\x00\x08\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04" \
  142. "\x80\x08\x00\x0b\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
  143. "\x40\x39\xcf\x32\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6" \
  144. "\x2f\xb7\x81\x96\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa" \
  145. "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
  146. "\x00\x07\x01\x50\x4b\x41\x4d\x45\x2d\x42\x53\x44\x20\x31\x2e\x31" \
  147. "\x00\x00\x00\x00\x64\xdb\x63\x00\x00\x00\x00\x00\xc9\x76\x03\x00" \
  148. "\x00\x00\x00\x00\x60\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  149. "\xb2\xac\xa4\x49\x2b\xe8\x47\x40\xd4\xc9\x79\x52\x00\x00\x00\x00" \
  150. "\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\xd4\xc9\x79\x53" \
  151. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00" \
  152. "\x00\x00\x00\x00\x5a\x76\x13\x89\x01\x00\x00\x00\x00\x00\x00\x00" \
  153. "\x00\x00\x00\x00\x01\x00\x00\x62\x49\xa4\xac\xb2\x00\x1c\x71\xc7" \
  154. "\x00\x01\xff\xff\x82\xe6\xc8\x44\x80\x00\x00\x04\xc0\x00\x00\x04" \
  155. "\x80\x08\x00\x0b\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
  156. "\xb6\xbb\xb5\x7f\xbb\x4b\x0e\xb5\x42\xf6\x75\x18\x4f\x79\x0f\x24" \
  157. "\x1c\x44\x0b\xd6\x62\xa9\x84\xe7\x2c\x3c\x7f\xad\x1b\x67\x81\x57" \
  158. "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
  159. "\x00\x0c\x00\x06\x00\x05\x00\x00\x02\x00\x01\xb4\x2b\xe8\x47\x40" \
  160. "\x00\x1c\x71\xc7\x00\x01\xff\xff\xed\x69\x58\xec\xc0\x06\x00\x08" \
  161. "\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04\x80\x08\x00\x0b" \
  162. "\xc0\xc2\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24\x40\x39\xcf\x32" \
  163. "\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6\x2f\xb7\x81\x96" \
  164. "\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa\x80\x04\x00\x08" \
  165. "\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00\x81\xe1\x1e\x81" \
  166. "\xea\x41\xeb\xf0\x12\xd9\x74\xbe\x13\xfd\x4b\x6c\x5c\xa2\x8f\x00";
  167. char fuzz_init_ack_nrsack_support[] = "\x13\x89\x13\x88\x49\xa4\xac\xb2\x00\x00\x00\x00\x02\x00\x01\xb4" \
  168. "\x2b\xe8\x47\x40\x00\x1c\x71\xc7\xff\xff\xff\xff\xed\x69\x58\xec" \
  169. "\xc0\x06\x00\x08\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04" \
  170. "\x80\x08\x00\x0b\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
  171. "\x40\x39\xcf\x32\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6" \
  172. "\x2f\xb7\x81\x96\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa" \
  173. "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
  174. "\x00\x07\x01\x50\x4b\x41\x4d\x45\x2d\x42\x53\x44\x20\x31\x2e\x31" \
  175. "\x00\x00\x00\x00\x64\xdb\x63\x00\x00\x00\x00\x00\xc9\x76\x03\x00" \
  176. "\x00\x00\x00\x00\x60\xea\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  177. "\xb2\xac\xa4\x49\x2b\xe8\x47\x40\xd4\xc9\x79\x52\x00\x00\x00\x00" \
  178. "\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\xd4\xc9\x79\x53" \
  179. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00" \
  180. "\x00\x00\x00\x00\x5a\x76\x13\x89\x01\x00\x00\x00\x00\x00\x00\x00" \
  181. "\x00\x00\x00\x00\x01\x00\x00\x62\x49\xa4\xac\xb2\x00\x1c\x71\xc7" \
  182. "\x00\x01\xff\xff\x82\xe6\xc8\x44\x80\x00\x00\x04\xc0\x00\x00\x04" \
  183. "\x80\x08\x00\x0b\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24" \
  184. "\xb6\xbb\xb5\x7f\xbb\x4b\x0e\xb5\x42\xf6\x75\x18\x4f\x79\x0f\x24" \
  185. "\x1c\x44\x0b\xd6\x62\xa9\x84\xe7\x2c\x3c\x7f\xad\x1b\x67\x81\x57" \
  186. "\x80\x04\x00\x08\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00" \
  187. "\x00\x0c\x00\x06\x00\x05\x00\x00\x02\x00\x01\xb4\x2b\xe8\x47\x40" \
  188. "\x00\x1c\x71\xc7\x00\x01\xff\xff\xed\x69\x58\xec\xc0\x06\x00\x08" \
  189. "\x00\x00\x07\xc4\x80\x00\x00\x04\xc0\x00\x00\x04\x80\x08\x00\x0b" \
  190. "\xc0\x10\x0f\xc1\x80\x82\x40\x00\x80\x02\x00\x24\x40\x39\xcf\x32" \
  191. "\xd6\x60\xcf\xfa\x3f\x2f\xa9\x52\xed\x2b\xf2\xe6\x2f\xb7\x81\x96" \
  192. "\xf8\xda\xe9\xa0\x62\x01\x79\xe1\x0d\x5f\x38\xaa\x80\x04\x00\x08" \
  193. "\x00\x03\x00\x01\x80\x03\x00\x06\x80\xc1\x00\x00\x81\xe1\x1e\x81" \
  194. "\xea\x41\xeb\xf0\x12\xd9\x74\xbe\x13\xfd\x4b\x6c\x5c\xa2\x8f\x00";
  195. char fuzz_cookie_ack[] = "\x13\x89\x13\x88\xb7\x0d\x32\x66\x00\x00\x00\x00\x0b\x00\x00\x04";
  196. char data_common_headr[] = "\x13\x89\x13\x88\xb7\x0d\x32\x66\x00\x00\x00\x00";
  197. if (!initialized) {
  198. initialized = initialize_fuzzer();
  199. }
  200. if (data_size < 10) {
  201. // Skip too small packets
  202. fuzzer_printf("data_size %zu makes no sense, skipping\n", data_size);
  203. return (0);
  204. }
  205. socket_client = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, 0);
  206. FUZZER_ASSERT(socket_client != NULL);
  207. usrsctp_set_non_blocking(socket_client, 1);
  208. memset(&initmsg, 1, sizeof(struct sctp_initmsg));
  209. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(struct sctp_initmsg));
  210. FUZZER_ASSERT(result == 0);
  211. so_linger.l_onoff = 1;
  212. so_linger.l_linger = 0;
  213. result = usrsctp_setsockopt(socket_client, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger));
  214. FUZZER_ASSERT(result == 0);
  215. memset(&event, 0, sizeof(event));
  216. event.se_on = 1;
  217. for (i = 0; i < (sizeof(event_types) / sizeof(uint16_t)); i++) {
  218. event.se_type = event_types[i];
  219. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event));
  220. }
  221. optval = 1;
  222. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_RECVRCVINFO, &optval, sizeof(optval));
  223. FUZZER_ASSERT(result == 0);
  224. optval = 1;
  225. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_RECVNXTINFO, &optval, sizeof(optval));
  226. FUZZER_ASSERT(result == 0);
  227. if (data[0] & I_DATA_FLAG) {
  228. // set the program supporting I-DATA
  229. optval = 2;
  230. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, &optval, sizeof(optval));
  231. FUZZER_ASSERT(result == 0);
  232. memset(&assoc_val, 0, sizeof(assoc_val));
  233. assoc_val.assoc_value = 1;
  234. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_INTERLEAVING_SUPPORTED, &assoc_val, sizeof(assoc_val));
  235. FUZZER_ASSERT(result == 0);
  236. }
  237. optval = 1;
  238. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_REUSE_PORT, &optval, sizeof(optval));
  239. FUZZER_ASSERT(result == 0);
  240. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  241. sconn.sconn_family = AF_CONN;
  242. #ifdef HAVE_SCONN_LEN
  243. sconn.sconn_len = sizeof(struct sockaddr_conn);
  244. #endif // HAVE_SCONN_LEN
  245. sconn.sconn_port = htons(5000);
  246. sconn.sconn_addr = (void *)1;
  247. result = usrsctp_bind(socket_client, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
  248. FUZZER_ASSERT(result == 0);
  249. optval = 1;
  250. result = usrsctp_setsockopt(socket_client, IPPROTO_SCTP, SCTP_NODELAY, &optval, sizeof(optval));
  251. FUZZER_ASSERT(result == 0);
  252. usrsctp_set_upcall(socket_client, handle_upcall, NULL);
  253. memset(&sconn, 0, sizeof(struct sockaddr_conn));
  254. sconn.sconn_family = AF_CONN;
  255. #ifdef HAVE_SCONN_LEN
  256. sconn.sconn_len = sizeof(struct sockaddr_conn);
  257. #endif // HAVE_SCONN_LEN
  258. sconn.sconn_port = htons(5001);
  259. sconn.sconn_addr = (void *)1;
  260. result = usrsctp_connect(socket_client, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
  261. FUZZER_ASSERT(result == 0 || errno == EINPROGRESS);
  262. if (data[0] & NR_SACK_FLAG) {
  263. common_header = (struct sctp_common_header*) fuzz_init_ack_nrsack_support;
  264. common_header->verification_tag = assoc_vtag;
  265. usrsctp_conninput((void *)1, fuzz_init_ack_nrsack_support, 448, 0);
  266. } else {
  267. common_header = (struct sctp_common_header*) fuzz_init_ack;
  268. common_header->verification_tag = assoc_vtag;
  269. usrsctp_conninput((void *)1, fuzz_init_ack, 448, 0);
  270. }
  271. common_header = (struct sctp_common_header*) fuzz_cookie_ack;
  272. common_header->verification_tag = assoc_vtag;
  273. usrsctp_conninput((void *)1, fuzz_cookie_ack, 16, 0);
  274. fuzz_data_count = 0;
  275. while (fuzz_data_count+4 < data_size) {
  276. size_t data_chunk_size = (data[fuzz_data_count+2]<<8) + data[fuzz_data_count+3];
  277. if (data_chunk_size == 0) {
  278. break;
  279. }
  280. if (fuzz_data_count + data_chunk_size > data_size) {
  281. data_chunk_size = data_size - fuzz_data_count;
  282. }
  283. if (data[fuzz_data_count] & NR_SACK_FLAG ||
  284. data[fuzz_data_count] & SACK_FLAG) {
  285. send_data_buffer = malloc(SEND_DATA_SIZE);
  286. FUZZER_ASSERT(send_data_buffer != NULL );
  287. memset(send_data_buffer, 1, SEND_DATA_SIZE);
  288. fuzzer_printf("Calling usrsctp_sendv()\n");
  289. usrsctp_sendv(socket_client, send_data_buffer, SEND_DATA_SIZE, NULL, 0, NULL, 0, SCTP_SENDV_NOINFO, 0);
  290. free(send_data_buffer);
  291. }
  292. fuzz_packet_buffer = malloc(data_chunk_size + COMMON_HEADER_SIZE);
  293. memcpy(fuzz_packet_buffer, data_common_headr, COMMON_HEADER_SIZE); // common header
  294. memcpy(fuzz_packet_buffer + COMMON_HEADER_SIZE, data+fuzz_data_count, data_chunk_size);
  295. usrsctp_conninput((void *)1, fuzz_packet_buffer, data_chunk_size + COMMON_HEADER_SIZE, 0);
  296. free(fuzz_packet_buffer);
  297. fuzz_data_count += data_chunk_size;
  298. }
  299. usrsctp_close(socket_client);
  300. return (0);
  301. }