Coverage Report

Created: 2022-09-28 06:21

/src/curl_fuzzer/curl_fuzzer_tlv.cc
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 2017, 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
#include <stdlib.h>
23
#include <string.h>
24
#include <curl/curl.h>
25
#include "curl_fuzzer.h"
26
27
/**
28
 * TLV access function - gets the first TLV from a data stream.
29
 */
30
int fuzz_get_first_tlv(FUZZ_DATA *fuzz,
31
                       TLV *tlv)
32
996k
{
33
  /* Reset the cursor. */
34
996k
  fuzz->state.data_pos = 0;
35
996k
  return fuzz_get_tlv_comn(fuzz, tlv);
36
996k
}
37
38
/**
39
 * TLV access function - gets the next TLV from a data stream.
40
*/
41
int fuzz_get_next_tlv(FUZZ_DATA *fuzz,
42
                      TLV *tlv)
43
8.26M
{
44
  /* Advance the cursor by the full length of the previous TLV. */
45
8.26M
  fuzz->state.data_pos += sizeof(TLV_RAW) + tlv->length;
46
47
  /* Work out if there's a TLV's worth of data to read */
48
8.26M
  if(fuzz->state.data_pos + sizeof(TLV_RAW) > fuzz->state.data_len) {
49
    /* No more TLVs to parse */
50
881k
    return TLV_RC_NO_MORE_TLVS;
51
881k
  }
52
53
7.38M
  return fuzz_get_tlv_comn(fuzz, tlv);
54
8.26M
}
55
56
/**
57
 * Common TLV function for accessing TLVs in a data stream.
58
 */
59
int fuzz_get_tlv_comn(FUZZ_DATA *fuzz,
60
                      TLV *tlv)
61
8.37M
{
62
8.37M
  int rc = 0;
63
8.37M
  size_t data_offset;
64
8.37M
  TLV_RAW *raw;
65
66
  /* Start by casting the data stream to a TLV. */
67
8.37M
  raw = (TLV_RAW *)&fuzz->state.data[fuzz->state.data_pos];
68
8.37M
  data_offset = fuzz->state.data_pos + sizeof(TLV_RAW);
69
70
  /* Set the TLV values. */
71
8.37M
  tlv->type = to_u16(raw->raw_type);
72
8.37M
  tlv->length = to_u32(raw->raw_length);
73
8.37M
  tlv->value = &fuzz->state.data[data_offset];
74
75
8.37M
  FV_PRINTF(fuzz, "TLV: type %x length %u\n", tlv->type, tlv->length);
76
77
  /* Use uint64s to verify lengths of TLVs so that overflow problems don't
78
     matter. */
79
8.37M
  uint64_t check_length = data_offset;
80
8.37M
  check_length += tlv->length;
81
82
8.37M
  uint64_t remaining_len = fuzz->state.data_len;
83
8.37M
  FV_PRINTF(fuzz, "Check length of data: %lu \n", check_length);
84
8.37M
  FV_PRINTF(fuzz, "Remaining length of data: %lu \n", remaining_len);
85
86
  /* Sanity check that the TLV length is ok. */
87
8.37M
  if(check_length > remaining_len) {
88
60.4k
    FV_PRINTF(fuzz, "Returning TLV_RC_SIZE_ERROR\n");
89
60.4k
    rc = TLV_RC_SIZE_ERROR;
90
60.4k
  }
91
92
8.37M
  return rc;
93
8.37M
}
94
95
/**
96
 * Do different actions on the CURL handle for different received TLVs.
97
 */
98
int fuzz_parse_tlv(FUZZ_DATA *fuzz, TLV *tlv)
99
7.46M
{
100
7.46M
  int rc;
101
7.46M
  char *tmp = NULL;
102
7.46M
  uint32_t tmp_u32;
103
104
7.46M
  switch(tlv->type) {
105
    /* The pointers in response TLVs will always be valid as long as the fuzz
106
       data is in scope, which is the entirety of this file. */
107
31.1k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE0, 0);
108
29.4k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE1, 1);
109
7.45k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE2, 2);
110
90.8k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE3, 3);
111
6.58k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE4, 4);
112
5.56k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE5, 5);
113
4.85k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE6, 6);
114
4.32k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE7, 7);
115
4.43k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE8, 8);
116
3.77k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE9, 9);
117
3.92k
    FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE10, 10);
118
119
4.30k
    FRESPONSETLV(&fuzz->sockman[1], TLV_TYPE_SECOND_RESPONSE0, 0);
120
4.20k
    FRESPONSETLV(&fuzz->sockman[1], TLV_TYPE_SECOND_RESPONSE1, 1);
121
122
1.58k
    case TLV_TYPE_UPLOAD1:
123
      /* The pointers in the TLV will always be valid as long as the fuzz data
124
         is in scope, which is the entirety of this file. */
125
126
1.58k
      FCHECK_OPTION_UNSET(fuzz, CURLOPT_UPLOAD);
127
128
1.56k
      fuzz->upload1_data = tlv->value;
129
1.56k
      fuzz->upload1_data_len = tlv->length;
130
131
1.56k
      FSET_OPTION(fuzz, CURLOPT_UPLOAD, 1L);
132
1.56k
      FSET_OPTION(fuzz,
133
1.56k
                  CURLOPT_INFILESIZE_LARGE,
134
1.56k
                  (curl_off_t)fuzz->upload1_data_len);
135
1.56k
      break;
136
137
253k
    case TLV_TYPE_HEADER:
138
      /* Limit the number of headers that can be added to a message to prevent
139
         timeouts. */
140
253k
      if(fuzz->header_list_count >= TLV_MAX_NUM_CURLOPT_HEADER) {
141
17
        rc = 255;
142
17
        goto EXIT_LABEL;
143
17
      }
144
145
253k
      tmp = fuzz_tlv_to_string(tlv);
146
253k
      fuzz->header_list = curl_slist_append(fuzz->header_list, tmp);
147
253k
      fuzz->header_list_count++;
148
253k
      break;
149
150
584k
    case TLV_TYPE_MAIL_RECIPIENT:
151
584k
      tmp = fuzz_tlv_to_string(tlv);
152
584k
      fuzz->mail_recipients_list =
153
584k
                            curl_slist_append(fuzz->mail_recipients_list, tmp);
154
584k
      break;
155
156
6.30M
    case TLV_TYPE_MIME_PART:
157
6.30M
      if(fuzz->mime == NULL) {
158
6.53k
        fuzz->mime = curl_mime_init(fuzz->easy);
159
6.53k
      }
160
161
6.30M
      fuzz->part = curl_mime_addpart(fuzz->mime);
162
163
      /* This TLV may have sub TLVs. */
164
6.30M
      fuzz_add_mime_part(tlv, fuzz->part);
165
6.30M
      break;
166
167
1.00k
    case TLV_TYPE_POSTFIELDS:
168
1.00k
      FCHECK_OPTION_UNSET(fuzz, CURLOPT_POSTFIELDS);
169
984
      fuzz->postfields = fuzz_tlv_to_string(tlv);
170
984
      FSET_OPTION(fuzz, CURLOPT_POSTFIELDS, fuzz->postfields);
171
984
      break;
172
173
    /* Define a set of u32 options. */
174
8.58k
    FU32TLV(fuzz, TLV_TYPE_HTTPAUTH, CURLOPT_HTTPAUTH);
175
2.32k
    FU32TLV(fuzz, TLV_TYPE_OPTHEADER, CURLOPT_HEADER);
176
4.69k
    FU32TLV(fuzz, TLV_TYPE_NOBODY, CURLOPT_NOBODY);
177
7.62k
    FU32TLV(fuzz, TLV_TYPE_FOLLOWLOCATION, CURLOPT_FOLLOWLOCATION);
178
2.79k
    FU32TLV(fuzz, TLV_TYPE_WILDCARDMATCH, CURLOPT_WILDCARDMATCH);
179
6.90k
    FU32TLV(fuzz, TLV_TYPE_RTSP_REQUEST, CURLOPT_RTSP_REQUEST);
180
574
    FU32TLV(fuzz, TLV_TYPE_RTSP_CLIENT_CSEQ, CURLOPT_RTSP_CLIENT_CSEQ);
181
24.3k
    FU32TLV(fuzz, TLV_TYPE_HTTP_VERSION, CURLOPT_HTTP_VERSION);
182
183
    /* Define a set of singleton TLVs - they can only have their value set once
184
       and all follow the same pattern. */
185
174k
    FSINGLETONTLV(fuzz, TLV_TYPE_URL, CURLOPT_URL);
186
288
    FSINGLETONTLV(fuzz, TLV_TYPE_DOH_URL, CURLOPT_DOH_URL);
187
4.88k
    FSINGLETONTLV(fuzz, TLV_TYPE_USERNAME, CURLOPT_USERNAME);
188
3.69k
    FSINGLETONTLV(fuzz, TLV_TYPE_PASSWORD, CURLOPT_PASSWORD);
189
622
    FSINGLETONTLV(fuzz, TLV_TYPE_COOKIE, CURLOPT_COOKIE);
190
2.05k
    FSINGLETONTLV(fuzz, TLV_TYPE_RANGE, CURLOPT_RANGE);
191
1.18k
    FSINGLETONTLV(fuzz, TLV_TYPE_CUSTOMREQUEST, CURLOPT_CUSTOMREQUEST);
192
752
    FSINGLETONTLV(fuzz, TLV_TYPE_MAIL_FROM, CURLOPT_MAIL_FROM);
193
973
    FSINGLETONTLV(fuzz, TLV_TYPE_ACCEPTENCODING, CURLOPT_ACCEPT_ENCODING);
194
300
    FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_SESSION_ID, CURLOPT_RTSP_SESSION_ID);
195
253
    FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_STREAM_URI, CURLOPT_RTSP_STREAM_URI);
196
265
    FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_TRANSPORT, CURLOPT_RTSP_TRANSPORT);
197
296
    FSINGLETONTLV(fuzz, TLV_TYPE_MAIL_AUTH, CURLOPT_MAIL_AUTH);
198
199
121
    default:
200
      /* The fuzzer generates lots of unknown TLVs - we don't want these in the
201
         corpus so we reject any unknown TLVs. */
202
121
      rc = 127;
203
121
      goto EXIT_LABEL;
204
0
      break;
205
7.46M
  }
206
207
7.45M
  rc = 0;
208
209
7.46M
EXIT_LABEL:
210
211
7.46M
  fuzz_free((void **)&tmp);
212
213
7.46M
  return rc;
214
7.45M
}
215
216
/**
217
 * Converts a TLV data and length into an allocated string.
218
 */
219
char *fuzz_tlv_to_string(TLV *tlv)
220
1.71M
{
221
1.71M
  char *tlvstr;
222
223
  /* Allocate enough space, plus a null terminator */
224
1.71M
  tlvstr = (char *)malloc(tlv->length + 1);
225
226
1.71M
  if(tlvstr != NULL) {
227
1.71M
    memcpy(tlvstr, tlv->value, tlv->length);
228
1.71M
    tlvstr[tlv->length] = 0;
229
1.71M
  }
230
231
1.71M
  return tlvstr;
232
1.71M
}
233
234
/**
235
 * Extract the values from the TLV.
236
 */
237
int fuzz_add_mime_part(TLV *src_tlv, curl_mimepart *part)
238
6.30M
{
239
6.30M
  FUZZ_DATA part_fuzz;
240
6.30M
  TLV tlv;
241
6.30M
  int rc = 0;
242
6.30M
  int tlv_rc;
243
244
6.30M
  memset(&part_fuzz, 0, sizeof(FUZZ_DATA));
245
246
6.30M
  if(src_tlv->length < sizeof(TLV_RAW)) {
247
    /* Not enough data for a single TLV - don't continue */
248
5.41M
    goto EXIT_LABEL;
249
5.41M
  }
250
251
  /* Set up the state parser */
252
894k
  part_fuzz.state.data = src_tlv->value;
253
894k
  part_fuzz.state.data_len = src_tlv->length;
254
255
894k
  for(tlv_rc = fuzz_get_first_tlv(&part_fuzz, &tlv);
256
1.69M
      tlv_rc == 0;
257
894k
      tlv_rc = fuzz_get_next_tlv(&part_fuzz, &tlv)) {
258
259
    /* Have the TLV in hand. Parse the TLV. */
260
853k
    rc = fuzz_parse_mime_tlv(part, &tlv);
261
262
853k
    if(rc != 0) {
263
      /* Failed to parse the TLV. Can't continue. */
264
51.4k
      goto EXIT_LABEL;
265
51.4k
    }
266
853k
  }
267
268
843k
  if(tlv_rc != TLV_RC_NO_MORE_TLVS) {
269
    /* A TLV call failed. Can't continue. */
270
58.8k
    goto EXIT_LABEL;
271
58.8k
  }
272
273
6.30M
EXIT_LABEL:
274
275
6.30M
  return(rc);
276
843k
}
277
278
/**
279
 * Do different actions on the mime part for different received TLVs.
280
 */
281
int fuzz_parse_mime_tlv(curl_mimepart *part, TLV *tlv)
282
853k
{
283
853k
  int rc;
284
853k
  char *tmp;
285
286
853k
  switch(tlv->type) {
287
779k
    case TLV_TYPE_MIME_PART_NAME:
288
779k
      tmp = fuzz_tlv_to_string(tlv);
289
779k
      curl_mime_name(part, tmp);
290
779k
      fuzz_free((void **)&tmp);
291
779k
      break;
292
293
22.5k
    case TLV_TYPE_MIME_PART_DATA:
294
22.5k
      curl_mime_data(part, (const char *)tlv->value, tlv->length);
295
22.5k
      break;
296
297
51.4k
    default:
298
      /* The fuzzer generates lots of unknown TLVs - we don't want these in the
299
         corpus so we reject any unknown TLVs. */
300
51.4k
      rc = 255;
301
51.4k
      goto EXIT_LABEL;
302
0
      break;
303
853k
  }
304
305
801k
  rc = 0;
306
307
853k
EXIT_LABEL:
308
309
853k
  return rc;
310
801k
}