fuzzmain.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. // Silence unused args warning.
  94. (void)(argc);
  95. (void)(argv);
  96. civetweb_init();
  97. return 0;
  98. }
  99. #if defined(TEST_FUZZ1)
  100. static int
  101. test_civetweb_client(const char *server,
  102. uint16_t port,
  103. int use_ssl,
  104. const char *uri)
  105. {
  106. /* Client var */
  107. struct mg_connection *client;
  108. char client_err_buf[256];
  109. char client_data_buf[4096];
  110. const struct mg_response_info *client_ri;
  111. int64_t data_read;
  112. int r;
  113. client = mg_connect_client(
  114. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  115. if ((client == NULL) || (0 != strcmp(client_err_buf, ""))) {
  116. fprintf(stderr,
  117. "%s connection to server [%s] port [%u] failed: [%s]\n",
  118. use_ssl ? "HTTPS" : "HTTP",
  119. server,
  120. port,
  121. client_err_buf);
  122. if (client) {
  123. mg_close_connection(client);
  124. }
  125. /* In heavy fuzz testing, sometimes we run out of available sockets.
  126. * Wait for some seconds, and retry. */
  127. test_sleep(5);
  128. /* retry once */
  129. client = mg_connect_client(
  130. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  131. if (!client) {
  132. fprintf(stderr, "Retry: error\n");
  133. return 1;
  134. }
  135. fprintf(stderr, "Retry: success\n");
  136. }
  137. mg_printf(client, "GET %s HTTP/1.0\r\n\r\n", uri);
  138. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  139. if ((r < 0) || (0 != strcmp(client_err_buf, ""))) {
  140. mg_close_connection(client);
  141. return 1;
  142. }
  143. client_ri = mg_get_response_info(client);
  144. if (client_ri == NULL) {
  145. mg_close_connection(client);
  146. return 1;
  147. }
  148. data_read = 0;
  149. while (data_read < client_ri->content_length) {
  150. /* store the first sizeof(client_data_buf) bytes
  151. * of the HTTP response. */
  152. r = mg_read(client,
  153. client_data_buf + data_read,
  154. sizeof(client_data_buf) - (size_t)data_read);
  155. if (r > 0) {
  156. data_read += r;
  157. }
  158. /* buffer filled? */
  159. if (sizeof(client_data_buf) == (size_t)data_read) {
  160. /* ignore the rest */
  161. while (r > 0) {
  162. char trash[1024];
  163. r = mg_read(client, trash, sizeof(trash));
  164. }
  165. break;
  166. }
  167. }
  168. /* Nothing left to read */
  169. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  170. if (r != 0) {
  171. mg_close_connection(client);
  172. return 1;
  173. }
  174. mg_close_connection(client);
  175. return 0;
  176. }
  177. static int
  178. LLVMFuzzerTestOneInput_URI(const uint8_t *data, size_t size)
  179. {
  180. static char URI[1024 * 64]; /* static, to avoid stack overflow */
  181. if (size+1 < sizeof(URI)) {
  182. memcpy(URI, data, size);
  183. URI[size] = 0;
  184. } else {
  185. return 1;
  186. }
  187. return test_civetweb_client("127.0.0.1", PORT_NUM_HTTP, 0, URI);
  188. }
  189. #endif
  190. #if defined(TEST_FUZZ2)
  191. static int
  192. LLVMFuzzerTestOneInput_REQUEST(const uint8_t *data, size_t size)
  193. {
  194. int r;
  195. SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
  196. if (sock == -1) {
  197. r = errno;
  198. fprintf(stderr, "Error: Cannot create socket [%s]\n", strerror(r));
  199. return 1;
  200. }
  201. struct sockaddr_in sin;
  202. memset(&sin, 0, sizeof(sin));
  203. sin.sin_family = AF_INET;
  204. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  205. sin.sin_port = htons(PORT_NUM_HTTP);
  206. r = connect(sock, (struct sockaddr *)&sin, sizeof(sin));
  207. if (r != 0) {
  208. r = errno;
  209. fprintf(stderr, "Error: Cannot connect [%s]\n", strerror(r));
  210. closesocket(sock);
  211. return 1;
  212. }
  213. char trash[1024];
  214. r = send(sock, data, size, 0);
  215. if (r != (int)size) {
  216. fprintf(stderr, "Warning: %i bytes sent (TODO: Repeat)\n", r);
  217. }
  218. int data_read = 0;
  219. while ((r = recv(sock, trash, sizeof(trash), 0)) > 0) {
  220. data_read += r;
  221. };
  222. shutdown(sock, SHUT_RDWR);
  223. closesocket(sock);
  224. static int max_data_read = 0;
  225. if (data_read > max_data_read) {
  226. max_data_read = data_read;
  227. printf("GOT data: %i\n", data_read);
  228. }
  229. return 0;
  230. }
  231. #endif
  232. #endif // defined(TEST_FUZZ1) || defined(TEST_FUZZ2)
  233. /********************************************************/
  234. /* Init mock server ... test with CivetWeb client */
  235. /********************************************************/
  236. #if defined(TEST_FUZZ3)
  237. struct tcp_func_prm {
  238. SOCKET sock;
  239. };
  240. struct tRESPONSE {
  241. char data[4096];
  242. size_t size;
  243. } RESPONSE;
  244. volatile int mock_server_stop_flag = 0;
  245. static void
  246. mock_server_exit(void)
  247. {
  248. printf("MOCK server exit\n");
  249. mock_server_stop_flag = 1;
  250. test_sleep(5);
  251. }
  252. static void *
  253. mock_server_thread(void *arg)
  254. {
  255. char req[1024 * 16];
  256. SOCKET svr = (SOCKET)(-1);
  257. /* Get thread parameters and free arg */
  258. {
  259. struct tcp_func_prm *ptcp_func_prm = (struct tcp_func_prm *)arg;
  260. svr = ptcp_func_prm->sock;
  261. free(arg);
  262. }
  263. mock_server_stop_flag = 0;
  264. printf("MOCK server ready, sock %i\n", svr);
  265. next_request:
  266. while (!mock_server_stop_flag) {
  267. struct sockaddr_in cliadr;
  268. socklen_t adrlen = sizeof(cliadr);
  269. int buf_filled = 0;
  270. int req_ready = 0;
  271. memset(&cliadr, 0, sizeof(cliadr));
  272. SOCKET cli = accept(svr, (struct sockaddr *)&cliadr, &adrlen);
  273. if (cli == -1) {
  274. int er = errno;
  275. fprintf(stderr, "Error: Accept failed [%s]\n", strerror(er));
  276. test_sleep(1);
  277. goto next_request;
  278. }
  279. /* Read request */
  280. do {
  281. int r =
  282. recv(cli, req + buf_filled, sizeof(req) - buf_filled - 1, 0);
  283. if (r > 0) {
  284. buf_filled += r;
  285. req[buf_filled] = 0;
  286. if (strstr(req, "\r\n\r\n") != NULL) {
  287. req_ready = 1;
  288. }
  289. } else {
  290. /* some error */
  291. int er = errno;
  292. fprintf(stderr, "Error: Recv failed [%s]\n", strerror(er));
  293. test_sleep(1);
  294. goto next_request;
  295. }
  296. } while (!req_ready);
  297. /* Request is complete here.
  298. * Now send response */
  299. send(cli, RESPONSE.data, RESPONSE.size, MSG_NOSIGNAL);
  300. /* Close connection. */
  301. shutdown(cli, SHUT_RDWR);
  302. closesocket(cli);
  303. }
  304. return 0;
  305. }
  306. static void
  307. mock_server_init(void)
  308. {
  309. int r;
  310. int bind_success = 0;
  311. SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
  312. if (sock == -1) {
  313. r = errno;
  314. fprintf(stderr, "Error: Cannot create socket [%s]\n", strerror(r));
  315. TESTabort();
  316. }
  317. for (PORT_NUM_HTTP = 1024; PORT_NUM_HTTP != 0; PORT_NUM_HTTP++) {
  318. struct sockaddr_in sin;
  319. memset(&sin, 0, sizeof(sin));
  320. sin.sin_family = AF_INET;
  321. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  322. sin.sin_port = htons(PORT_NUM_HTTP);
  323. r = bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  324. if (r == 0) {
  325. bind_success = 1;
  326. break;
  327. }
  328. r = errno;
  329. fprintf(stderr, "Warning: Cannot bind [%s]\n", strerror(r));
  330. }
  331. if (!bind_success) {
  332. fprintf(stderr, "Error: Cannot bind to any port\n");
  333. closesocket(sock);
  334. TESTabort();
  335. }
  336. printf("MOCK server running on port %i\n", (int)PORT_NUM_HTTP);
  337. r = listen(sock, 128);
  338. if (r != 0) {
  339. r = errno;
  340. fprintf(stderr, "Error: Cannot listen [%s]\n", strerror(r));
  341. closesocket(sock);
  342. TESTabort();
  343. }
  344. pthread_t thread_id;
  345. pthread_attr_t attr;
  346. int result;
  347. struct tcp_func_prm *thread_prm;
  348. thread_prm = (struct tcp_func_prm *)malloc(sizeof(struct tcp_func_prm));
  349. if (!thread_prm) {
  350. fprintf(stderr, "Error: Out of memory\n");
  351. closesocket(sock);
  352. TESTabort();
  353. }
  354. thread_prm->sock = sock;
  355. (void)pthread_attr_init(&attr);
  356. (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  357. result = pthread_create(&thread_id,
  358. &attr,
  359. mock_server_thread,
  360. (void *)thread_prm);
  361. (void)pthread_attr_destroy(&attr);
  362. if (result != 0) {
  363. r = errno;
  364. fprintf(stderr, "Error: Cannot create thread [%s]\n", strerror(r));
  365. closesocket(sock);
  366. TESTabort();
  367. }
  368. test_sleep(3);
  369. atexit(mock_server_exit);
  370. }
  371. int LLVMFuzzerInitialize(int *argc, char ***argv);
  372. int
  373. LLVMFuzzerInitialize(int *argc, char ***argv) {
  374. // Silence unused args warning.
  375. (void)(argc);
  376. (void)(argv);
  377. mock_server_init();
  378. return 0;
  379. }
  380. static int
  381. LLVMFuzzerTestOneInput_RESPONSE(const uint8_t *data, size_t size)
  382. {
  383. if (size > sizeof(RESPONSE.data)) {
  384. return 1;
  385. }
  386. memcpy(RESPONSE.data, data, size);
  387. RESPONSE.size = size;
  388. char errbuf[256];
  389. struct mg_connection *conn = mg_connect_client(
  390. "127.0.0.1", PORT_NUM_HTTP, 0, errbuf, sizeof(errbuf));
  391. if (!conn) {
  392. printf("Connect error: %s\n", errbuf);
  393. test_sleep(1);
  394. return 1;
  395. }
  396. mg_printf(conn, "GET / HTTP/1.0\r\n\r\n");
  397. int r = mg_get_response(conn, errbuf, sizeof(errbuf), 1000);
  398. const struct mg_response_info *ri = mg_get_response_info(conn);
  399. (void)r;
  400. (void)ri;
  401. mg_close_connection(conn);
  402. return 0;
  403. }
  404. #endif // defined(TEST_FUZZ3)
  405. /********************************************************/
  406. /* MAIN for fuzztest */
  407. /********************************************************/
  408. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  409. // warning: no previous prototype for function 'LLVMFuzzerTestOneInput'
  410. int
  411. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  412. {
  413. #if defined(TEST_FUZZ1)
  414. /* fuzz target 1: different URI for HTTP/1 server */
  415. LLVMFuzzerTestOneInput_URI(data, size);
  416. return 0;
  417. #elif defined(TEST_FUZZ2)
  418. /* fuzz target 2: different requests for HTTP/1 server */
  419. LLVMFuzzerTestOneInput_REQUEST(data, size);
  420. return 0;
  421. #elif defined(TEST_FUZZ3)
  422. /* fuzz target 3: different responses for HTTP/1 client */
  423. LLVMFuzzerTestOneInput_RESPONSE(data, size);
  424. return 0;
  425. #elif defined(TEST_FUZZ4)
  426. #error "Only useful in HTTP/2 feature branch"
  427. /* fuzz target 4: different requests for HTTP/2 server */
  428. LLVMFuzzerTestOneInput_REQUEST_HTTP2(data, size);
  429. return 0;
  430. #elif defined(TEST_FUZZ5)
  431. /* fuzz target 5: calling an internal server test function,
  432. * bypassing network sockets */
  433. LLVMFuzzerTestOneInput_process_new_connection(data, size);
  434. return 0;
  435. #else
  436. /* planned targets */
  437. #error "Unknown fuzz target"
  438. #endif
  439. }