Coverage Report

Created: 2022-09-28 06:21

/src/curl_fuzzer/curl_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 2017 - 2022, Max Dymond, <cmeister2@gmail.com>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 ***************************************************************************/
22
23
#include <stdlib.h>
24
#include <signal.h>
25
#include <string.h>
26
#include <unistd.h>
27
#include <curl/curl.h>
28
#include "curl_fuzzer.h"
29
30
/**
31
 * Fuzzing entry point. This function is passed a buffer containing a test
32
 * case.  This test case should drive the CURL API into making a request.
33
 */
34
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
35
101k
{
36
101k
  int rc = 0;
37
101k
  int tlv_rc;
38
101k
  FUZZ_DATA fuzz;
39
101k
  TLV tlv;
40
41
  /* Ignore SIGPIPE errors. We'll handle the errors ourselves. */
42
101k
  signal(SIGPIPE, SIG_IGN);
43
44
  /* Have to set all fields to zero before getting to the terminate function */
45
101k
  memset(&fuzz, 0, sizeof(FUZZ_DATA));
46
47
101k
  if(size < sizeof(TLV_RAW)) {
48
    /* Not enough data for a single TLV - don't continue */
49
72
    goto EXIT_LABEL;
50
72
  }
51
52
  /* Try to initialize the fuzz data */
53
101k
  FTRY(fuzz_initialize_fuzz_data(&fuzz, data, size));
54
55
101k
  for(tlv_rc = fuzz_get_first_tlv(&fuzz, &tlv);
56
7.56M
      tlv_rc == 0;
57
7.46M
      tlv_rc = fuzz_get_next_tlv(&fuzz, &tlv)) {
58
59
    /* Have the TLV in hand. Parse the TLV. */
60
7.46M
    rc = fuzz_parse_tlv(&fuzz, &tlv);
61
62
7.46M
    if(rc != 0) {
63
      /* Failed to parse the TLV. Can't continue. */
64
3.41k
      goto EXIT_LABEL;
65
3.41k
    }
66
7.46M
  }
67
68
98.1k
  if(tlv_rc != TLV_RC_NO_MORE_TLVS) {
69
    /* A TLV call failed. Can't continue. */
70
1.62k
    goto EXIT_LABEL;
71
1.62k
  }
72
73
  /* Set up the standard easy options. */
74
96.5k
  FTRY(fuzz_set_easy_options(&fuzz));
75
76
  /**
77
   * Add in more curl options that have been accumulated over possibly
78
   * multiple TLVs.
79
   */
80
93.2k
  if(fuzz.header_list != NULL) {
81
3.97k
    curl_easy_setopt(fuzz.easy, CURLOPT_HTTPHEADER, fuzz.header_list);
82
3.97k
  }
83
84
93.2k
  if(fuzz.mail_recipients_list != NULL) {
85
1.53k
    curl_easy_setopt(fuzz.easy, CURLOPT_MAIL_RCPT, fuzz.mail_recipients_list);
86
1.53k
  }
87
88
93.2k
  if(fuzz.mime != NULL) {
89
5.06k
    curl_easy_setopt(fuzz.easy, CURLOPT_MIMEPOST, fuzz.mime);
90
5.06k
  }
91
92
  /* Run the transfer. */
93
93.2k
  fuzz_handle_transfer(&fuzz);
94
95
101k
EXIT_LABEL:
96
97
101k
  fuzz_terminate_fuzz_data(&fuzz);
98
99
  /* This function must always return 0. Non-zero codes are reserved. */
100
101k
  return 0;
101
93.2k
}
102
103
/**
104
 * Utility function to convert 4 bytes to a u32 predictably.
105
 */
106
uint32_t to_u32(const uint8_t b[4])
107
8.39M
{
108
8.39M
  uint32_t u;
109
8.39M
  u = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3];
110
8.39M
  return u;
111
8.39M
}
112
113
/**
114
 * Utility function to convert 2 bytes to a u16 predictably.
115
 */
116
uint16_t to_u16(const uint8_t b[2])
117
8.37M
{
118
8.37M
  uint16_t u;
119
8.37M
  u = (b[0] << 8) + b[1];
120
8.37M
  return u;
121
8.37M
}
122
123
/**
124
 * Initialize the local fuzz data structure.
125
 */
126
int fuzz_initialize_fuzz_data(FUZZ_DATA *fuzz,
127
                              const uint8_t *data,
128
                              size_t data_len)
129
101k
{
130
101k
  int rc = 0;
131
101k
  int ii;
132
133
  /* Initialize the fuzz data. */
134
101k
  memset(fuzz, 0, sizeof(FUZZ_DATA));
135
136
  /* Create an easy handle. This will have all of the settings configured on
137
     it. */
138
101k
  fuzz->easy = curl_easy_init();
139
101k
  FCHECK(fuzz->easy != NULL);
140
141
  /* Set up the state parser */
142
101k
  fuzz->state.data = data;
143
101k
  fuzz->state.data_len = data_len;
144
145
  /* Set up the state of the server sockets. */
146
304k
  for(ii = 0; ii < FUZZ_NUM_CONNECTIONS; ii++) {
147
203k
    fuzz->sockman[ii].index = ii;
148
203k
    fuzz->sockman[ii].fd_state = FUZZ_SOCK_CLOSED;
149
203k
  }
150
151
  /* Check for verbose mode. */
152
101k
  fuzz->verbose = (getenv("FUZZ_VERBOSE") != NULL);
153
154
101k
EXIT_LABEL:
155
156
101k
  return rc;
157
101k
}
158
159
/**
160
 * Set standard options on the curl easy.
161
 */
162
int fuzz_set_easy_options(FUZZ_DATA *fuzz)
163
96.5k
{
164
96.5k
  int rc = 0;
165
96.5k
  unsigned long allowed_protocols;
166
167
  /* Set some standard options on the CURL easy handle. We need to override the
168
     socket function so that we create our own sockets to present to CURL. */
169
96.5k
  FTRY(curl_easy_setopt(fuzz->easy,
170
96.5k
                        CURLOPT_OPENSOCKETFUNCTION,
171
96.5k
                        fuzz_open_socket));
172
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_OPENSOCKETDATA, fuzz));
173
174
  /* In case something tries to set a socket option, intercept this. */
175
96.5k
  FTRY(curl_easy_setopt(fuzz->easy,
176
96.5k
                        CURLOPT_SOCKOPTFUNCTION,
177
96.5k
                        fuzz_sockopt_callback));
178
179
  /* Set the standard read function callback. */
180
96.5k
  FTRY(curl_easy_setopt(fuzz->easy,
181
96.5k
                        CURLOPT_READFUNCTION,
182
96.5k
                        fuzz_read_callback));
183
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_READDATA, fuzz));
184
185
  /* Set the standard write function callback. */
186
96.5k
  FTRY(curl_easy_setopt(fuzz->easy,
187
96.5k
                        CURLOPT_WRITEFUNCTION,
188
96.5k
                        fuzz_write_callback));
189
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_WRITEDATA, fuzz));
190
191
  /* Set the cookie jar so cookies are tested. */
192
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_COOKIEJAR, FUZZ_COOKIE_JAR_PATH));
193
194
  /* Time out requests quickly. */
195
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_TIMEOUT_MS, 200L));
196
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_SERVER_RESPONSE_TIMEOUT, 1L));
197
198
  /* Can enable verbose mode by having the environment variable FUZZ_VERBOSE. */
199
96.5k
  if(fuzz->verbose) {
200
0
    FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_VERBOSE, 1L));
201
0
  }
202
203
  /* Force resolution of all addresses to a specific IP address. */
204
96.5k
  fuzz->connect_to_list = curl_slist_append(NULL, "::127.0.1.127:");
205
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_CONNECT_TO, fuzz->connect_to_list));
206
207
  /* Limit the protocols in use by this fuzzer. */
208
96.5k
  FTRY(fuzz_set_allowed_protocols(fuzz));
209
210
96.5k
EXIT_LABEL:
211
212
96.5k
  return rc;
213
93.2k
}
214
215
/**
216
 * Terminate the fuzz data structure, including freeing any allocated memory.
217
 */
218
void fuzz_terminate_fuzz_data(FUZZ_DATA *fuzz)
219
101k
{
220
101k
  int ii;
221
222
101k
  fuzz_free((void **)&fuzz->postfields);
223
224
304k
  for(ii = 0; ii < FUZZ_NUM_CONNECTIONS; ii++) {
225
203k
    if(fuzz->sockman[ii].fd_state != FUZZ_SOCK_CLOSED) {
226
67.6k
      close(fuzz->sockman[ii].fd);
227
67.6k
      fuzz->sockman[ii].fd_state = FUZZ_SOCK_CLOSED;
228
67.6k
    }
229
203k
  }
230
231
101k
  if(fuzz->connect_to_list != NULL) {
232
96.5k
    curl_slist_free_all(fuzz->connect_to_list);
233
96.5k
    fuzz->connect_to_list = NULL;
234
96.5k
  }
235
236
101k
  if(fuzz->header_list != NULL) {
237
4.29k
    curl_slist_free_all(fuzz->header_list);
238
4.29k
    fuzz->header_list = NULL;
239
4.29k
  }
240
241
101k
  if(fuzz->mail_recipients_list != NULL) {
242
1.71k
    curl_slist_free_all(fuzz->mail_recipients_list);
243
1.71k
    fuzz->mail_recipients_list = NULL;
244
1.71k
  }
245
246
101k
  if(fuzz->mime != NULL) {
247
6.53k
    curl_mime_free(fuzz->mime);
248
6.53k
    fuzz->mime = NULL;
249
6.53k
  }
250
251
101k
  if(fuzz->easy != NULL) {
252
101k
    curl_easy_cleanup(fuzz->easy);
253
101k
    fuzz->easy = NULL;
254
101k
  }
255
101k
}
256
257
/**
258
 * If a pointer has been allocated, free that pointer.
259
 */
260
void fuzz_free(void **ptr)
261
8.34M
{
262
8.34M
  if(*ptr != NULL) {
263
1.71M
    free(*ptr);
264
1.71M
    *ptr = NULL;
265
1.71M
  }
266
8.34M
}
267
268
/**
269
 * Function for handling the fuzz transfer, including sending responses to
270
 * requests.
271
 */
272
int fuzz_handle_transfer(FUZZ_DATA *fuzz)
273
93.2k
{
274
93.2k
  int rc = 0;
275
93.2k
  CURLM *multi_handle;
276
93.2k
  int still_running; /* keep number of running handles */
277
93.2k
  CURLMsg *msg; /* for picking up messages with the transfer status */
278
93.2k
  int msgs_left; /* how many messages are left */
279
93.2k
  int double_timeout = 0;
280
93.2k
  fd_set fdread;
281
93.2k
  fd_set fdwrite;
282
93.2k
  fd_set fdexcep;
283
93.2k
  struct timeval timeout;
284
93.2k
  int select_rc;
285
93.2k
  CURLMcode mc;
286
93.2k
  int maxfd = -1;
287
93.2k
  long curl_timeo = -1;
288
93.2k
  int ii;
289
93.2k
  FUZZ_SOCKET_MANAGER *sman[FUZZ_NUM_CONNECTIONS];
290
291
279k
  for(ii = 0; ii < FUZZ_NUM_CONNECTIONS; ii++) {
292
186k
    sman[ii] = &fuzz->sockman[ii];
293
294
    /* Set up the starting index for responses. */
295
186k
    sman[ii]->response_index = 1;
296
186k
  }
297
298
  /* init a multi stack */
299
93.2k
  multi_handle = curl_multi_init();
300
301
  /* add the individual transfers */
302
93.2k
  curl_multi_add_handle(multi_handle, fuzz->easy);
303
304
  /* Do an initial process. This might end the transfer immediately. */
305
93.2k
  curl_multi_perform(multi_handle, &still_running);
306
93.2k
  FV_PRINTF(fuzz,
307
93.2k
            "FUZZ: Initial perform; still running? %d \n",
308
93.2k
            still_running);
309
310
38.4M
  while(still_running) {
311
    /* Reset the sets of file descriptors. */
312
38.3M
    FD_ZERO(&fdread);
313
38.3M
    FD_ZERO(&fdwrite);
314
38.3M
    FD_ZERO(&fdexcep);
315
316
    /* Set a timeout of 10ms. This is lower than recommended by the multi guide
317
       but we're not going to any remote servers, so everything should complete
318
       very quickly. */
319
38.3M
    timeout.tv_sec = 0;
320
38.3M
    timeout.tv_usec = 10000;
321
322
    /* get file descriptors from the transfers */
323
38.3M
    mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
324
38.3M
    if(mc != CURLM_OK) {
325
0
      fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
326
0
      rc = -1;
327
0
      break;
328
0
    }
329
330
115M
    for(ii = 0; ii < FUZZ_NUM_CONNECTIONS; ii++) {
331
      /* Add the socket FD into the readable set if connected. */
332
76.6M
      if(sman[ii]->fd_state == FUZZ_SOCK_OPEN) {
333
4.54M
        FD_SET(sman[ii]->fd, &fdread);
334
335
        /* Work out the maximum FD between the cURL file descriptors and the
336
           server FD. */
337
4.54M
        maxfd = FUZZ_MAX(sman[ii]->fd, maxfd);
338
4.54M
      }
339
76.6M
    }
340
341
    /* Work out what file descriptors need work. */
342
38.3M
    rc = fuzz_select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
343
344
38.3M
    if(rc == -1) {
345
      /* Had an issue while selecting a file descriptor. Let's just exit. */
346
0
      FV_PRINTF(fuzz, "FUZZ: select failed, exiting \n");
347
0
      break;
348
0
    }
349
38.3M
    else if(rc == 0) {
350
1.25k
      FV_PRINTF(fuzz,
351
1.25k
                "FUZZ: Timed out; double timeout? %d \n",
352
1.25k
                double_timeout);
353
354
      /* Timed out. */
355
1.25k
      if(double_timeout == 1) {
356
        /* We don't expect multiple timeouts in a row. If there are double
357
           timeouts then exit. */
358
616
        break;
359
616
      }
360
643
      else {
361
        /* Set the timeout flag for the next time we select(). */
362
643
        double_timeout = 1;
363
643
      }
364
1.25k
    }
365
38.3M
    else {
366
      /* There's an active file descriptor. Reset the timeout flag. */
367
38.3M
      double_timeout = 0;
368
38.3M
    }
369
370
    /* Check to see if a server file descriptor is readable. If it is,
371
       then send the next response from the fuzzing data. */
372
115M
    for(ii = 0; ii < FUZZ_NUM_CONNECTIONS; ii++) {
373
76.6M
      if(sman[ii]->fd_state == FUZZ_SOCK_OPEN &&
374
76.6M
         FD_ISSET(sman[ii]->fd, &fdread)) {
375
29.1k
        rc = fuzz_send_next_response(fuzz, sman[ii]);
376
29.1k
        if(rc != 0) {
377
          /* Failed to send a response. Break out here. */
378
287
          break;
379
287
        }
380
29.1k
      }
381
76.6M
    }
382
383
38.3M
    curl_multi_perform(multi_handle, &still_running);
384
38.3M
  }
385
386
  /* Remove the easy handle from the multi stack. */
387
93.2k
  curl_multi_remove_handle(multi_handle, fuzz->easy);
388
389
  /* Clean up the multi handle - the top level function will handle the easy
390
     handle. */
391
93.2k
  curl_multi_cleanup(multi_handle);
392
393
93.2k
  return(rc);
394
93.2k
}
395
396
/**
397
 * Sends the next fuzzing response to the server file descriptor.
398
 */
399
int fuzz_send_next_response(FUZZ_DATA *fuzz, FUZZ_SOCKET_MANAGER *sman)
400
29.1k
{
401
29.1k
  int rc = 0;
402
29.1k
  ssize_t ret_in;
403
29.1k
  ssize_t ret_out;
404
29.1k
  char buffer[8192];
405
29.1k
  const uint8_t *data;
406
29.1k
  size_t data_len;
407
408
  /* Need to read all data sent by the client so the file descriptor becomes
409
     unreadable. Because the file descriptor is non-blocking we won't just
410
     hang here. */
411
69.0k
  do {
412
69.0k
    ret_in = read(sman->fd, buffer, sizeof(buffer));
413
69.0k
    if(fuzz->verbose && ret_in > 0) {
414
0
      printf("FUZZ[%d]: Received %zu bytes \n==>\n", sman->index, ret_in);
415
0
      fwrite(buffer, ret_in, 1, stdout);
416
0
      printf("\n<==\n");
417
0
    }
418
69.0k
  } while (ret_in > 0);
419
420
  /* Now send a response to the request that the client just made. */
421
29.1k
  FV_PRINTF(fuzz,
422
29.1k
            "FUZZ[%d]: Sending next response: %d \n",
423
29.1k
            sman->index,
424
29.1k
            sman->response_index);
425
29.1k
  data = sman->responses[sman->response_index].data;
426
29.1k
  data_len = sman->responses[sman->response_index].data_len;
427
428
29.1k
  if(data != NULL) {
429
29.1k
    if(write(sman->fd, data, data_len) != (ssize_t)data_len) {
430
      /* Failed to write the data back to the client. Prevent any further
431
         testing. */
432
287
      rc = -1;
433
287
    }
434
29.1k
  }
435
436
  /* Work out if there are any more responses. If not, then shut down the
437
     server. */
438
29.1k
  sman->response_index++;
439
440
29.1k
  if(sman->response_index >= TLV_MAX_NUM_RESPONSES ||
441
29.1k
     sman->responses[sman->response_index].data == NULL) {
442
23.7k
    FV_PRINTF(fuzz,
443
23.7k
              "FUZZ[%d]: Shutting down server socket: %d \n",
444
23.7k
              sman->index,
445
23.7k
              sman->fd);
446
23.7k
    shutdown(sman->fd, SHUT_WR);
447
23.7k
    sman->fd_state = FUZZ_SOCK_SHUTDOWN;
448
23.7k
  }
449
450
29.1k
  return(rc);
451
29.1k
}
452
453
/**
454
 * Wrapper for select() so profiling can track it.
455
 */
456
int fuzz_select(int nfds,
457
                fd_set *readfds,
458
                fd_set *writefds,
459
                fd_set *exceptfds,
460
38.3M
                struct timeval *timeout) {
461
38.3M
  return select(nfds, readfds, writefds, exceptfds, timeout);
462
38.3M
}
463
464
/**
465
 * Set allowed protocols based on the compile options.
466
 *
467
 * Note that it can only use ONE of the FUZZ_PROTOCOLS_* defines.a
468
 */
469
int fuzz_set_allowed_protocols(FUZZ_DATA *fuzz)
470
96.5k
{
471
96.5k
  int rc = 0;
472
96.5k
  const char *allowed_protocols = "";
473
474
#ifdef FUZZ_PROTOCOLS_ALL
475
  /* Do not allow telnet currently as it accepts input from stdin. */
476
  allowed_protocols =
477
    "dict,file,ftp,ftps,gopher,gophers,http,https,imap,imaps,"
478
    "ldap,ldaps,mqtt,pop3,pop3s,rtmp,rtmpe,rtmps,rtmpt,rtmpte,rtmpts,"
479
    "rtsp,scp,sftp,smb,smbs,smtp,smtps,tftp";
480
#endif
481
#ifdef FUZZ_PROTOCOLS_DICT
482
  allowed_protocols = "dict";
483
#endif
484
#ifdef FUZZ_PROTOCOLS_FILE
485
  allowed_protocols = "file";
486
#endif
487
#ifdef FUZZ_PROTOCOLS_FTP
488
  allowed_protocols = "ftp,ftps";
489
#endif
490
#ifdef FUZZ_PROTOCOLS_GOPHER
491
  allowed_protocols = "gopher,gophers";
492
#endif
493
#ifdef FUZZ_PROTOCOLS_HTTP
494
  allowed_protocols = "http";
495
#endif
496
#ifdef FUZZ_PROTOCOLS_HTTPS
497
  allowed_protocols = "https";
498
#endif
499
#ifdef FUZZ_PROTOCOLS_IMAP
500
  allowed_protocols = "imap,imaps";
501
#endif
502
#ifdef FUZZ_PROTOCOLS_LDAP
503
  allowed_protocols = "ldap,ldaps";
504
#endif
505
#ifdef FUZZ_PROTOCOLS_MQTT
506
  allowed_protocols = "mqtt";
507
#endif
508
#ifdef FUZZ_PROTOCOLS_POP3
509
  allowed_protocols = "pop3,pop3s";
510
#endif
511
#ifdef FUZZ_PROTOCOLS_RTMP
512
  allowed_protocols = "rtmp,rtmpe,rtmps,rtmpt,rtmpte,rtmpts";
513
#endif
514
#ifdef FUZZ_PROTOCOLS_RTSP
515
  allowed_protocols = "rtsp";
516
#endif
517
#ifdef FUZZ_PROTOCOLS_SCP
518
  allowed_protocols = "scp";
519
#endif
520
#ifdef FUZZ_PROTOCOLS_SFTP
521
  allowed_protocols = "sftp";
522
#endif
523
#ifdef FUZZ_PROTOCOLS_SMB
524
  allowed_protocols = "smb,smbs";
525
#endif
526
#ifdef FUZZ_PROTOCOLS_SMTP
527
  allowed_protocols = "smtp,smtps";
528
#endif
529
96.5k
#ifdef FUZZ_PROTOCOLS_TFTP
530
96.5k
  allowed_protocols = "tftp";
531
96.5k
#endif
532
#ifdef FUZZ_PROTOCOLS_WS
533
  allowed_protocols = "ws,wss";
534
#endif
535
536
96.5k
  FTRY(curl_easy_setopt(fuzz->easy, CURLOPT_PROTOCOLS_STR, allowed_protocols));
537
538
96.5k
EXIT_LABEL:
539
540
96.5k
  return rc;
541
93.2k
}