fuzzmain.c 13 KB

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