fuzzmain.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "civetweb.h"
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #if defined(_WIN32)
  7. #error "Currently not supported"
  8. #else
  9. #include <unistd.h>
  10. #define test_sleep(x) (sleep(x))
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <netinet/ip.h>
  16. typedef int SOCKET;
  17. #define closesocket(a) (close(a))
  18. #endif
  19. static uint64_t call_count = 0;
  20. static struct mg_context *ctx;
  21. static const char *OPTIONS[] = {
  22. "listening_ports", "8080,8443s",
  23. #ifdef _WIN32
  24. "document_root", "fuzz\\docroot",
  25. "ssl_certificate", "resources\\cert\\server.pem",
  26. #else
  27. "document_root", "fuzz/docroot",
  28. "ssl_certificate", "resources/cert/server.pem",
  29. #endif
  30. NULL, NULL
  31. };
  32. static void
  33. init_civetweb(void)
  34. {
  35. struct mg_callbacks callbacks;
  36. memset(&callbacks, 0, sizeof(callbacks));
  37. ctx = mg_start(&callbacks, 0, OPTIONS);
  38. if (!ctx) {
  39. fprintf(stderr, "\nCivetWeb test server failed to start\n");
  40. abort();
  41. }
  42. /* Give server 5 seconds to start, before flooding with requests.
  43. * Don't know if this is required for fuzz-tests, but it was helpful
  44. * when testing starting/stopping the server multiple times in test
  45. * container environments. */
  46. test_sleep(5);
  47. }
  48. static int
  49. test_http_request(const char *server,
  50. uint16_t port,
  51. int use_ssl,
  52. const char *uri)
  53. {
  54. /* Client var */
  55. struct mg_connection *client;
  56. char client_err_buf[256];
  57. char client_data_buf[4096];
  58. const struct mg_response_info *client_ri;
  59. int64_t data_read;
  60. int r;
  61. client = mg_connect_client(
  62. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  63. if ((client == NULL) || (0 != strcmp(client_err_buf, ""))) {
  64. fprintf(stderr, "%s connection to server [%s] port [%u] failed: [%s]\n",
  65. use_ssl ? "HTTPS" : "HTTP",
  66. server,
  67. port,
  68. client_err_buf);
  69. if (client) {
  70. mg_close_connection(client);
  71. }
  72. /* In heavy fuzz testing, sometimes we run out of available sockets.
  73. * Wait for some seconds, and retry. */
  74. test_sleep(5);
  75. /* retry once */
  76. client = mg_connect_client(
  77. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  78. if (!client) {
  79. fprintf(stderr, "Retry: error\n");
  80. return 1;
  81. }
  82. fprintf(stderr, "Retry: success\n");
  83. }
  84. mg_printf(client, "GET %s HTTP/1.0\r\n\r\n", uri);
  85. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  86. if ((r < 0) || (0 != strcmp(client_err_buf, ""))) {
  87. mg_close_connection(client);
  88. return 1;
  89. }
  90. client_ri = mg_get_response_info(client);
  91. if (client_ri == NULL) {
  92. mg_close_connection(client);
  93. return 1;
  94. }
  95. data_read = 0;
  96. while (data_read < client_ri->content_length) {
  97. /* store the first sizeof(client_data_buf) bytes
  98. * of the HTTP response. */
  99. r = mg_read(client,
  100. client_data_buf + data_read,
  101. sizeof(client_data_buf) - (size_t)data_read);
  102. if (r > 0) {
  103. data_read += r;
  104. }
  105. /* buffer filled? */
  106. if (sizeof(client_data_buf) == (size_t)data_read) {
  107. /* ignore the rest */
  108. while (r > 0) {
  109. char trash[1024];
  110. r = mg_read(client, trash, sizeof(trash));
  111. }
  112. break;
  113. }
  114. }
  115. /* Nothing left to read */
  116. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  117. if (r != 0) {
  118. mg_close_connection(client);
  119. return 1;
  120. }
  121. mg_close_connection(client);
  122. return 0;
  123. }
  124. static int
  125. LLVMFuzzerTestOneInput_URI(const uint8_t *data, size_t size)
  126. {
  127. static char URI[1024 * 64]; /* static, to avoid stack overflow */
  128. if (call_count == 0) {
  129. memset(URI, 0, sizeof(URI));
  130. init_civetweb();
  131. }
  132. call_count++;
  133. if (size < sizeof(URI)) {
  134. memcpy(URI, data, size);
  135. URI[size] = 0;
  136. } else {
  137. return 1;
  138. }
  139. printf("URI: %s\n", URI);
  140. return test_http_request("127.0.0.1", 8080, 0, URI);
  141. }
  142. static int
  143. LLVMFuzzerTestOneInput_REQUEST(const uint8_t *data, size_t size)
  144. {
  145. if (call_count == 0) {
  146. init_civetweb();
  147. }
  148. call_count++;
  149. int r;
  150. SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
  151. if (sock == -1) {
  152. r = errno;
  153. fprintf(stderr, "Error: Cannot create socket [%s]\n", strerror(r));
  154. return 1;
  155. }
  156. struct sockaddr_in sin;
  157. memset(&sin, 0, sizeof(sin));
  158. sin.sin_family = AF_INET;
  159. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  160. sin.sin_port = htons(8080);
  161. r = connect(sock, (struct sockaddr *)&sin, sizeof(sin));
  162. if (r != 0) {
  163. r = errno;
  164. fprintf(stderr, "Error: Cannot connect [%s]\n", strerror(r));
  165. closesocket(sock);
  166. return 1;
  167. }
  168. char trash[1024];
  169. r = send(sock, data, size, 0);
  170. if (r != size) {
  171. fprintf(stderr, "Warning: %i bytes sent (TODO: Repeat)\n", r);
  172. }
  173. int data_read = 0;
  174. while ((r = recv(sock, trash, sizeof(trash), 0)) > 0) {
  175. data_read += r;
  176. };
  177. shutdown(sock, SHUT_RDWR);
  178. closesocket(sock);
  179. static int max_data_read = 0;
  180. if (data_read>max_data_read) {
  181. max_data_read = data_read;
  182. printf("GOT data: %i\n", data_read);
  183. }
  184. }
  185. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  186. {
  187. #if defined(TEST_FUZZ1)
  188. return LLVMFuzzerTestOneInput_URI(data, size);
  189. #elif defined(TEST_FUZZ2)
  190. return LLVMFuzzerTestOneInput_REQUEST(data, size);
  191. #else
  192. #error "Unknown fuzz target"
  193. #endif
  194. }