fuzzmain.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "civetweb.h"
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <string.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 <arpa/inet.h>
  12. #include <netinet/in.h>
  13. #include <netinet/ip.h>
  14. #include <pthread.h>
  15. #include <sys/socket.h>
  16. #include <sys/types.h>
  17. typedef int SOCKET;
  18. #define closesocket(a) (close(a))
  19. #endif // not _WIN32
  20. /* Port configuration */
  21. unsigned short PORT_NUM_HTTP = 0; /* set dynamically */
  22. #define TESTabort() \
  23. { \
  24. fprintf(stderr, "!!! Precondition in test environment not met !!!\n"); \
  25. fprintf(stderr, "!!! aborting fuzz test in line %u !!!", __LINE__); \
  26. abort(); \
  27. }
  28. static uint64_t call_count = 0;
  29. /********************************************************/
  30. /* Init CivetWeb server ... test with mock client */
  31. /********************************************************/
  32. #if defined(TEST_FUZZ1) || defined(TEST_FUZZ2)
  33. static struct mg_context *ctx = 0;
  34. static const char *OPTIONS[] = {"listening_ports",
  35. "0", /* port: auto */
  36. "document_root",
  37. "fuzztest/docroot",
  38. NULL,
  39. NULL};
  40. static void
  41. civetweb_exit(void)
  42. {
  43. printf("CivetWeb server exit\n");
  44. mg_stop(ctx);
  45. ctx = 0;
  46. test_sleep(5);
  47. }
  48. static void
  49. civetweb_init(void)
  50. {
  51. struct mg_callbacks callbacks;
  52. struct mg_server_port ports[8];
  53. memset(&callbacks, 0, sizeof(callbacks));
  54. memset(&ports, 0, sizeof(ports));
  55. ctx = mg_start(&callbacks, 0, OPTIONS);
  56. if (!ctx) {
  57. fprintf(stderr, "\nCivetWeb test server failed to start\n");
  58. TESTabort();
  59. }
  60. int ret = mg_get_server_ports(ctx, 8, ports);
  61. if (ret != 1) {
  62. fprintf(stderr,
  63. "\nCivetWeb test server: cannot determine port number\n");
  64. TESTabort();
  65. }
  66. if (ports[0].is_ssl != 0) {
  67. fprintf(stderr,
  68. "\nCivetWeb fuzz test works on HTTP, not HTTPS.\n"
  69. "TLS librarys should be fuzzed separately.\n");
  70. TESTabort();
  71. }
  72. PORT_NUM_HTTP = ports[0].port;
  73. printf("CivetWeb server running on port %i\n", (int)PORT_NUM_HTTP);
  74. /* Give server 5 seconds to start, before flooding with requests.
  75. * Don't know if this is required for fuzz-tests, but it was helpful
  76. * when testing starting/stopping the server multiple times in test
  77. * container environments. */
  78. test_sleep(5);
  79. atexit(civetweb_exit);
  80. }
  81. #if defined(TEST_FUZZ1)
  82. static int
  83. test_civetweb_client(const char *server,
  84. uint16_t port,
  85. int use_ssl,
  86. const char *uri)
  87. {
  88. /* Client var */
  89. struct mg_connection *client;
  90. char client_err_buf[256];
  91. char client_data_buf[4096];
  92. const struct mg_response_info *client_ri;
  93. int64_t data_read;
  94. int r;
  95. client = mg_connect_client(
  96. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  97. if ((client == NULL) || (0 != strcmp(client_err_buf, ""))) {
  98. fprintf(stderr,
  99. "%s connection to server [%s] port [%u] failed: [%s]\n",
  100. use_ssl ? "HTTPS" : "HTTP",
  101. server,
  102. port,
  103. client_err_buf);
  104. if (client) {
  105. mg_close_connection(client);
  106. }
  107. /* In heavy fuzz testing, sometimes we run out of available sockets.
  108. * Wait for some seconds, and retry. */
  109. test_sleep(5);
  110. /* retry once */
  111. client = mg_connect_client(
  112. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  113. if (!client) {
  114. fprintf(stderr, "Retry: error\n");
  115. return 1;
  116. }
  117. fprintf(stderr, "Retry: success\n");
  118. }
  119. mg_printf(client, "GET %s HTTP/1.0\r\n\r\n", uri);
  120. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  121. if ((r < 0) || (0 != strcmp(client_err_buf, ""))) {
  122. mg_close_connection(client);
  123. return 1;
  124. }
  125. client_ri = mg_get_response_info(client);
  126. if (client_ri == NULL) {
  127. mg_close_connection(client);
  128. return 1;
  129. }
  130. data_read = 0;
  131. while (data_read < client_ri->content_length) {
  132. /* store the first sizeof(client_data_buf) bytes
  133. * of the HTTP response. */
  134. r = mg_read(client,
  135. client_data_buf + data_read,
  136. sizeof(client_data_buf) - (size_t)data_read);
  137. if (r > 0) {
  138. data_read += r;
  139. }
  140. /* buffer filled? */
  141. if (sizeof(client_data_buf) == (size_t)data_read) {
  142. /* ignore the rest */
  143. while (r > 0) {
  144. char trash[1024];
  145. r = mg_read(client, trash, sizeof(trash));
  146. }
  147. break;
  148. }
  149. }
  150. /* Nothing left to read */
  151. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  152. if (r != 0) {
  153. mg_close_connection(client);
  154. return 1;
  155. }
  156. mg_close_connection(client);
  157. return 0;
  158. }
  159. static int
  160. LLVMFuzzerTestOneInput_URI(const uint8_t *data, size_t size)
  161. {
  162. static char URI[1024 * 64]; /* static, to avoid stack overflow */
  163. if (call_count == 0) {
  164. memset(URI, 0, sizeof(URI));
  165. civetweb_init();
  166. }
  167. call_count++;
  168. if (size < sizeof(URI)) {
  169. memcpy(URI, data, size);
  170. URI[size] = 0;
  171. } else {
  172. return 1;
  173. }
  174. return test_civetweb_client("127.0.0.1", PORT_NUM_HTTP, 0, URI);
  175. }
  176. #endif
  177. #if defined(TEST_FUZZ2)
  178. static int
  179. LLVMFuzzerTestOneInput_REQUEST(const uint8_t *data, size_t size)
  180. {
  181. if (call_count == 0) {
  182. civetweb_init();
  183. }
  184. call_count++;
  185. int r;
  186. SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
  187. if (sock == -1) {
  188. r = errno;
  189. fprintf(stderr, "Error: Cannot create socket [%s]\n", strerror(r));
  190. return 1;
  191. }
  192. struct sockaddr_in sin;
  193. memset(&sin, 0, sizeof(sin));
  194. sin.sin_family = AF_INET;
  195. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  196. sin.sin_port = htons(PORT_NUM_HTTP);
  197. r = connect(sock, (struct sockaddr *)&sin, sizeof(sin));
  198. if (r != 0) {
  199. r = errno;
  200. fprintf(stderr, "Error: Cannot connect [%s]\n", strerror(r));
  201. closesocket(sock);
  202. return 1;
  203. }
  204. char trash[1024];
  205. r = send(sock, data, size, 0);
  206. if (r != (int)size) {
  207. fprintf(stderr, "Warning: %i bytes sent (TODO: Repeat)\n", r);
  208. }
  209. int data_read = 0;
  210. while ((r = recv(sock, trash, sizeof(trash), 0)) > 0) {
  211. data_read += r;
  212. };
  213. shutdown(sock, SHUT_RDWR);
  214. closesocket(sock);
  215. static int max_data_read = 0;
  216. if (data_read > max_data_read) {
  217. max_data_read = data_read;
  218. printf("GOT data: %i\n", data_read);
  219. }
  220. return 0;
  221. }
  222. #endif
  223. #endif // defined(TEST_FUZZ1) || defined(TEST_FUZZ2)
  224. /********************************************************/
  225. /* Init mock server ... test with CivetWeb client */
  226. /********************************************************/
  227. #if defined(TEST_FUZZ3)
  228. struct tcp_func_prm {
  229. SOCKET sock;
  230. };
  231. struct tRESPONSE {
  232. char data[4096];
  233. size_t size;
  234. } RESPONSE;
  235. volatile int mock_server_stop_flag = 0;
  236. static void
  237. mock_server_exit(void)
  238. {
  239. printf("MOCK server exit\n");
  240. mock_server_stop_flag = 1;
  241. test_sleep(5);
  242. }
  243. static void *
  244. mock_server_thread(void *arg)
  245. {
  246. char req[1024 * 16];
  247. SOCKET svr = (SOCKET)(-1);
  248. /* Get thread parameters and free arg */
  249. {
  250. struct tcp_func_prm *ptcp_func_prm = (struct tcp_func_prm *)arg;
  251. svr = ptcp_func_prm->sock;
  252. free(arg);
  253. }
  254. mock_server_stop_flag = 0;
  255. printf("MOCK server ready, sock %i\n", svr);
  256. next_request:
  257. while (!mock_server_stop_flag) {
  258. struct sockaddr_in cliadr;
  259. socklen_t adrlen = sizeof(cliadr);
  260. int buf_filled = 0;
  261. int req_ready = 0;
  262. memset(&cliadr, 0, sizeof(cliadr));
  263. SOCKET cli = accept(svr, (struct sockaddr *)&cliadr, &adrlen);
  264. if (cli == -1) {
  265. int er = errno;
  266. fprintf(stderr, "Error: Accept failed [%s]\n", strerror(er));
  267. test_sleep(1);
  268. goto next_request;
  269. }
  270. /* Read request */
  271. do {
  272. int r =
  273. recv(cli, req + buf_filled, sizeof(req) - buf_filled - 1, 0);
  274. if (r > 0) {
  275. buf_filled += r;
  276. req[buf_filled] = 0;
  277. if (strstr(req, "\r\n\r\n") != NULL) {
  278. req_ready = 1;
  279. }
  280. } else {
  281. /* some error */
  282. int er = errno;
  283. fprintf(stderr, "Error: Recv failed [%s]\n", strerror(er));
  284. test_sleep(1);
  285. goto next_request;
  286. }
  287. } while (!req_ready);
  288. /* Request is complete here.
  289. * Now send response */
  290. send(cli, RESPONSE.data, RESPONSE.size, MSG_NOSIGNAL);
  291. /* Close connection. */
  292. shutdown(cli, SHUT_RDWR);
  293. closesocket(cli);
  294. }
  295. return 0;
  296. }
  297. static void
  298. mock_server_init(void)
  299. {
  300. int r;
  301. int bind_success = 0;
  302. SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
  303. if (sock == -1) {
  304. r = errno;
  305. fprintf(stderr, "Error: Cannot create socket [%s]\n", strerror(r));
  306. TESTabort();
  307. }
  308. for (PORT_NUM_HTTP = 1024; PORT_NUM_HTTP != 0; PORT_NUM_HTTP++) {
  309. struct sockaddr_in sin;
  310. memset(&sin, 0, sizeof(sin));
  311. sin.sin_family = AF_INET;
  312. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  313. sin.sin_port = htons(PORT_NUM_HTTP);
  314. r = bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  315. if (r == 0) {
  316. bind_success = 1;
  317. break;
  318. }
  319. r = errno;
  320. fprintf(stderr, "Warning: Cannot bind [%s]\n", strerror(r));
  321. }
  322. if (!bind_success) {
  323. fprintf(stderr, "Error: Cannot bind to any port\n");
  324. closesocket(sock);
  325. TESTabort();
  326. }
  327. printf("MOCK server running on port %i\n", (int)PORT_NUM_HTTP);
  328. r = listen(sock, 128);
  329. if (r != 0) {
  330. r = errno;
  331. fprintf(stderr, "Error: Cannot listen [%s]\n", strerror(r));
  332. closesocket(sock);
  333. TESTabort();
  334. }
  335. pthread_t thread_id;
  336. pthread_attr_t attr;
  337. int result;
  338. struct tcp_func_prm *thread_prm;
  339. thread_prm = (struct tcp_func_prm *)malloc(sizeof(struct tcp_func_prm));
  340. if (!thread_prm) {
  341. fprintf(stderr, "Error: Out of memory\n");
  342. closesocket(sock);
  343. TESTabort();
  344. }
  345. thread_prm->sock = sock;
  346. (void)pthread_attr_init(&attr);
  347. (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  348. result = pthread_create(&thread_id,
  349. &attr,
  350. mock_server_thread,
  351. (void *)thread_prm);
  352. (void)pthread_attr_destroy(&attr);
  353. if (result != 0) {
  354. r = errno;
  355. fprintf(stderr, "Error: Cannot create thread [%s]\n", strerror(r));
  356. closesocket(sock);
  357. TESTabort();
  358. }
  359. test_sleep(3);
  360. atexit(mock_server_exit);
  361. }
  362. static int
  363. LLVMFuzzerTestOneInput_RESPONSE(const uint8_t *data, size_t size)
  364. {
  365. if (call_count == 0) {
  366. mock_server_init();
  367. }
  368. call_count++;
  369. if (size > sizeof(RESPONSE.data)) {
  370. return 1;
  371. }
  372. memcpy(RESPONSE.data, data, size);
  373. RESPONSE.size = size;
  374. char errbuf[256];
  375. struct mg_connection *conn = mg_connect_client(
  376. "127.0.0.1", PORT_NUM_HTTP, 0, errbuf, sizeof(errbuf));
  377. if (!conn) {
  378. printf("Connect error: %s\n", errbuf);
  379. test_sleep(1);
  380. return 1;
  381. }
  382. mg_printf(conn, "GET / HTTP/1.0\r\n\r\n");
  383. int r = mg_get_response(conn, errbuf, sizeof(errbuf), 1000);
  384. const struct mg_response_info *ri = mg_get_response_info(conn);
  385. (void)r;
  386. (void)ri;
  387. mg_close_connection(conn);
  388. return 0;
  389. }
  390. #endif // defined(TEST_FUZZ3)
  391. /********************************************************/
  392. /* MAIN for fuzztest */
  393. /********************************************************/
  394. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  395. // warning: no previous prototype for function 'LLVMFuzzerTestOneInput'
  396. int
  397. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  398. {
  399. #if defined(TEST_FUZZ1)
  400. /* fuzz target 1: different URI for HTTP/1 server */
  401. return LLVMFuzzerTestOneInput_URI(data, size);
  402. #elif defined(TEST_FUZZ2)
  403. /* fuzz target 2: different requests for HTTP/1 server */
  404. return LLVMFuzzerTestOneInput_REQUEST(data, size);
  405. #elif defined(TEST_FUZZ3)
  406. /* fuzz target 3: different responses for HTTP/1 client */
  407. return LLVMFuzzerTestOneInput_RESPONSE(data, size);
  408. #elif defined(TEST_FUZZ4)
  409. #error "Only useful in HTTP/2 feature branch"
  410. /* fuzz target 4: different requests for HTTP/2 server */
  411. return LLVMFuzzerTestOneInput_REQUEST_HTTP2(data, size);
  412. #elif defined(TEST_FUZZ5)
  413. /* fuzz target 5: calling an internal server test function,
  414. * bypassing network sockets */
  415. return LLVMFuzzerTestOneInput_process_new_connection(data, size);
  416. #else
  417. /* planned targets */
  418. #error "Unknown fuzz target"
  419. #endif
  420. }