/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 | 1.73M | { |
33 | | /* Reset the cursor. */ |
34 | 1.73M | fuzz->state.data_pos = 0; |
35 | 1.73M | return fuzz_get_tlv_comn(fuzz, tlv); |
36 | 1.73M | } |
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 | 9.51M | { |
44 | | /* Advance the cursor by the full length of the previous TLV. */ |
45 | 9.51M | 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 | 9.51M | if(fuzz->state.data_pos + sizeof(TLV_RAW) > fuzz->state.data_len) { |
49 | | /* No more TLVs to parse */ |
50 | 1.60M | return TLV_RC_NO_MORE_TLVS; |
51 | 1.60M | } |
52 | | |
53 | 7.91M | return fuzz_get_tlv_comn(fuzz, tlv); |
54 | 9.51M | } |
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 | 9.65M | { |
62 | 9.65M | int rc = 0; |
63 | 9.65M | size_t data_offset; |
64 | 9.65M | TLV_RAW *raw; |
65 | | |
66 | | /* Start by casting the data stream to a TLV. */ |
67 | 9.65M | raw = (TLV_RAW *)&fuzz->state.data[fuzz->state.data_pos]; |
68 | 9.65M | data_offset = fuzz->state.data_pos + sizeof(TLV_RAW); |
69 | | |
70 | | /* Set the TLV values. */ |
71 | 9.65M | tlv->type = to_u16(raw->raw_type); |
72 | 9.65M | tlv->length = to_u32(raw->raw_length); |
73 | 9.65M | tlv->value = &fuzz->state.data[data_offset]; |
74 | | |
75 | 9.65M | 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 | 9.65M | uint64_t check_length = data_offset; |
80 | 9.65M | check_length += tlv->length; |
81 | | |
82 | 9.65M | uint64_t remaining_len = fuzz->state.data_len; |
83 | 9.65M | FV_PRINTF(fuzz, "Check length of data: %lu \n", check_length); |
84 | 9.65M | FV_PRINTF(fuzz, "Remaining length of data: %lu \n", remaining_len); |
85 | | |
86 | | /* Sanity check that the TLV length is ok. */ |
87 | 9.65M | if(check_length > remaining_len) { |
88 | 90.2k | FV_PRINTF(fuzz, "Returning TLV_RC_SIZE_ERROR\n"); |
89 | 90.2k | rc = TLV_RC_SIZE_ERROR; |
90 | 90.2k | } |
91 | | |
92 | 9.65M | return rc; |
93 | 9.65M | } |
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.97M | { |
100 | 7.97M | int rc; |
101 | 7.97M | char *tmp = NULL; |
102 | 7.97M | uint32_t tmp_u32; |
103 | | |
104 | 7.97M | 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 | 24.1k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE0, 0); |
108 | 19.8k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE1, 1); |
109 | 7.66k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE2, 2); |
110 | 32.9k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE3, 3); |
111 | 8.32k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE4, 4); |
112 | 6.93k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE5, 5); |
113 | 6.98k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE6, 6); |
114 | 5.04k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE7, 7); |
115 | 3.96k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE8, 8); |
116 | 3.74k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE9, 9); |
117 | 4.21k | FRESPONSETLV(&fuzz->sockman[0], TLV_TYPE_RESPONSE10, 10); |
118 | | |
119 | 4.09k | FRESPONSETLV(&fuzz->sockman[1], TLV_TYPE_SECOND_RESPONSE0, 0); |
120 | 3.97k | FRESPONSETLV(&fuzz->sockman[1], TLV_TYPE_SECOND_RESPONSE1, 1); |
121 | | |
122 | 1.75k | 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.75k | FCHECK_OPTION_UNSET(fuzz, CURLOPT_UPLOAD); |
127 | | |
128 | 1.73k | fuzz->upload1_data = tlv->value; |
129 | 1.73k | fuzz->upload1_data_len = tlv->length; |
130 | | |
131 | 1.73k | FSET_OPTION(fuzz, CURLOPT_UPLOAD, 1L); |
132 | 1.73k | FSET_OPTION(fuzz, |
133 | 1.73k | CURLOPT_INFILESIZE_LARGE, |
134 | 1.73k | (curl_off_t)fuzz->upload1_data_len); |
135 | 1.73k | break; |
136 | | |
137 | 256k | case TLV_TYPE_HEADER: |
138 | | /* Limit the number of headers that can be added to a message to prevent |
139 | | timeouts. */ |
140 | 256k | if(fuzz->header_list_count >= TLV_MAX_NUM_CURLOPT_HEADER) { |
141 | 18 | rc = 255; |
142 | 18 | goto EXIT_LABEL; |
143 | 18 | } |
144 | | |
145 | 256k | tmp = fuzz_tlv_to_string(tlv); |
146 | 256k | fuzz->header_list = curl_slist_append(fuzz->header_list, tmp); |
147 | 256k | fuzz->header_list_count++; |
148 | 256k | break; |
149 | | |
150 | 196k | case TLV_TYPE_MAIL_RECIPIENT: |
151 | 196k | tmp = fuzz_tlv_to_string(tlv); |
152 | 196k | fuzz->mail_recipients_list = |
153 | 196k | curl_slist_append(fuzz->mail_recipients_list, tmp); |
154 | 196k | break; |
155 | | |
156 | 7.28M | case TLV_TYPE_MIME_PART: |
157 | 7.28M | if(fuzz->mime == NULL) { |
158 | 6.75k | fuzz->mime = curl_mime_init(fuzz->easy); |
159 | 6.75k | } |
160 | | |
161 | 7.28M | fuzz->part = curl_mime_addpart(fuzz->mime); |
162 | | |
163 | | /* This TLV may have sub TLVs. */ |
164 | 7.28M | fuzz_add_mime_part(tlv, fuzz->part); |
165 | | |
166 | 7.28M | break; |
167 | | |
168 | 1.03k | case TLV_TYPE_POSTFIELDS: |
169 | 1.03k | FCHECK_OPTION_UNSET(fuzz, CURLOPT_POSTFIELDS); |
170 | 1.01k | fuzz->postfields = fuzz_tlv_to_string(tlv); |
171 | 1.01k | FSET_OPTION(fuzz, CURLOPT_POSTFIELDS, fuzz->postfields); |
172 | 1.01k | break; |
173 | | |
174 | 683 | case TLV_TYPE_HTTPPOSTBODY: |
175 | 683 | FCHECK_OPTION_UNSET(fuzz, CURLOPT_HTTPPOST); |
176 | 665 | fuzz_setup_http_post(fuzz, tlv); |
177 | 665 | FSET_OPTION(fuzz, CURLOPT_HTTPPOST, fuzz->httppost); |
178 | 665 | break; |
179 | | |
180 | | /* Define a set of u32 options. */ |
181 | 12.3k | FU32TLV(fuzz, TLV_TYPE_HTTPAUTH, CURLOPT_HTTPAUTH); |
182 | 2.41k | FU32TLV(fuzz, TLV_TYPE_OPTHEADER, CURLOPT_HEADER); |
183 | 4.49k | FU32TLV(fuzz, TLV_TYPE_NOBODY, CURLOPT_NOBODY); |
184 | 7.68k | FU32TLV(fuzz, TLV_TYPE_FOLLOWLOCATION, CURLOPT_FOLLOWLOCATION); |
185 | 3.60k | FU32TLV(fuzz, TLV_TYPE_WILDCARDMATCH, CURLOPT_WILDCARDMATCH); |
186 | 7.11k | FU32TLV(fuzz, TLV_TYPE_RTSP_REQUEST, CURLOPT_RTSP_REQUEST); |
187 | 726 | FU32TLV(fuzz, TLV_TYPE_RTSP_CLIENT_CSEQ, CURLOPT_RTSP_CLIENT_CSEQ); |
188 | 24.8k | FU32TLV(fuzz, TLV_TYPE_HTTP_VERSION, CURLOPT_HTTP_VERSION); |
189 | 3.04k | FU32TLV(fuzz, TLV_TYPE_NETRC, CURLOPT_NETRC); |
190 | 498 | FU32TLV(fuzz, TLV_TYPE_WS_OPTIONS, CURLOPT_WS_OPTIONS); |
191 | 3.05k | FU32TLV(fuzz, TLV_TYPE_CONNECT_ONLY, CURLOPT_CONNECT_ONLY); |
192 | 1.81k | FU32TLV(fuzz, TLV_TYPE_POST, CURLOPT_POST); |
193 | | |
194 | | /* Define a set of singleton TLVs - they can only have their value set once |
195 | | and all follow the same pattern. */ |
196 | 134k | FSINGLETONTLV(fuzz, TLV_TYPE_URL, CURLOPT_URL); |
197 | 315 | FSINGLETONTLV(fuzz, TLV_TYPE_DOH_URL, CURLOPT_DOH_URL); |
198 | 4.63k | FSINGLETONTLV(fuzz, TLV_TYPE_USERNAME, CURLOPT_USERNAME); |
199 | 3.46k | FSINGLETONTLV(fuzz, TLV_TYPE_PASSWORD, CURLOPT_PASSWORD); |
200 | 833 | FSINGLETONTLV(fuzz, TLV_TYPE_COOKIE, CURLOPT_COOKIE); |
201 | 1.76k | FSINGLETONTLV(fuzz, TLV_TYPE_RANGE, CURLOPT_RANGE); |
202 | 1.21k | FSINGLETONTLV(fuzz, TLV_TYPE_CUSTOMREQUEST, CURLOPT_CUSTOMREQUEST); |
203 | 834 | FSINGLETONTLV(fuzz, TLV_TYPE_MAIL_FROM, CURLOPT_MAIL_FROM); |
204 | 939 | FSINGLETONTLV(fuzz, TLV_TYPE_ACCEPTENCODING, CURLOPT_ACCEPT_ENCODING); |
205 | 292 | FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_SESSION_ID, CURLOPT_RTSP_SESSION_ID); |
206 | 309 | FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_STREAM_URI, CURLOPT_RTSP_STREAM_URI); |
207 | 272 | FSINGLETONTLV(fuzz, TLV_TYPE_RTSP_TRANSPORT, CURLOPT_RTSP_TRANSPORT); |
208 | 364 | FSINGLETONTLV(fuzz, TLV_TYPE_MAIL_AUTH, CURLOPT_MAIL_AUTH); |
209 | 289 | FSINGLETONTLV(fuzz, TLV_TYPE_LOGIN_OPTIONS, CURLOPT_LOGIN_OPTIONS); |
210 | 474 | FSINGLETONTLV(fuzz, TLV_TYPE_XOAUTH2_BEARER, CURLOPT_XOAUTH2_BEARER); |
211 | 1.28k | FSINGLETONTLV(fuzz, TLV_TYPE_USERPWD, CURLOPT_USERPWD); |
212 | 720 | FSINGLETONTLV(fuzz, TLV_TYPE_USERAGENT, CURLOPT_USERAGENT); |
213 | 132 | FSINGLETONTLV(fuzz, TLV_TYPE_SSH_HOST_PUBLIC_KEY_SHA256, CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256); |
214 | | |
215 | 114 | default: |
216 | | /* The fuzzer generates lots of unknown TLVs - we don't want these in the |
217 | | corpus so we reject any unknown TLVs. */ |
218 | 114 | rc = 127; |
219 | 114 | goto EXIT_LABEL; |
220 | 0 | break; |
221 | 7.97M | } |
222 | | |
223 | 7.97M | rc = 0; |
224 | | |
225 | 7.97M | EXIT_LABEL: |
226 | | |
227 | 7.97M | fuzz_free((void **)&tmp); |
228 | | |
229 | 7.97M | return rc; |
230 | 7.97M | } |
231 | | |
232 | | /** |
233 | | * Converts a TLV data and length into an allocated string. |
234 | | */ |
235 | | char *fuzz_tlv_to_string(TLV *tlv) |
236 | 2.05M | { |
237 | 2.05M | char *tlvstr; |
238 | | |
239 | | /* Allocate enough space, plus a null terminator */ |
240 | 2.05M | tlvstr = (char *)malloc(tlv->length + 1); |
241 | | |
242 | 2.05M | if(tlvstr != NULL) { |
243 | 2.05M | memcpy(tlvstr, tlv->value, tlv->length); |
244 | 2.05M | tlvstr[tlv->length] = 0; |
245 | 2.05M | } |
246 | | |
247 | 2.05M | return tlvstr; |
248 | 2.05M | } |
249 | | |
250 | | /* set up for CURLOPT_HTTPPOST, an alternative API to CURLOPT_MIMEPOST */ |
251 | | void fuzz_setup_http_post(FUZZ_DATA *fuzz, TLV *tlv) |
252 | 665 | { |
253 | 665 | if (fuzz->httppost == NULL) { |
254 | 665 | struct curl_httppost *post = NULL; |
255 | 665 | struct curl_httppost *last = NULL; |
256 | | |
257 | 665 | fuzz->post_body = fuzz_tlv_to_string(tlv); |
258 | | |
259 | | /* This is just one of several possible entrypoints to |
260 | | * the HTTPPOST API. see https://curl.se/libcurl/c/curl_formadd.html |
261 | | * for lots of others which could be added here. |
262 | | */ |
263 | 665 | curl_formadd(&post, &last, |
264 | 665 | CURLFORM_COPYNAME, FUZZ_HTTPPOST_NAME, |
265 | 665 | CURLFORM_PTRCONTENTS, fuzz->post_body, |
266 | 665 | CURLFORM_CONTENTLEN, (curl_off_t) strlen(fuzz->post_body), |
267 | 665 | CURLFORM_END); |
268 | | |
269 | 665 | fuzz->last_post_part = last; |
270 | 665 | fuzz->httppost = post; |
271 | 665 | } |
272 | | |
273 | 665 | return; |
274 | 665 | } |
275 | | |
276 | | /** |
277 | | * Extract the values from the TLV. |
278 | | */ |
279 | | int fuzz_add_mime_part(TLV *src_tlv, curl_mimepart *part) |
280 | 7.28M | { |
281 | 7.28M | FUZZ_DATA part_fuzz; |
282 | 7.28M | TLV tlv; |
283 | 7.28M | int rc = 0; |
284 | 7.28M | int tlv_rc; |
285 | | |
286 | 7.28M | memset(&part_fuzz, 0, sizeof(FUZZ_DATA)); |
287 | | |
288 | 7.28M | if(src_tlv->length < sizeof(TLV_RAW)) { |
289 | | /* Not enough data for a single TLV - don't continue */ |
290 | 5.63M | goto EXIT_LABEL; |
291 | 5.63M | } |
292 | | |
293 | | /* Set up the state parser */ |
294 | 1.65M | part_fuzz.state.data = src_tlv->value; |
295 | 1.65M | part_fuzz.state.data_len = src_tlv->length; |
296 | | |
297 | 1.65M | for(tlv_rc = fuzz_get_first_tlv(&part_fuzz, &tlv); |
298 | 3.19M | tlv_rc == 0; |
299 | 1.65M | tlv_rc = fuzz_get_next_tlv(&part_fuzz, &tlv)) { |
300 | | |
301 | | /* Have the TLV in hand. Parse the TLV. */ |
302 | 1.58M | rc = fuzz_parse_mime_tlv(part, &tlv); |
303 | | |
304 | 1.58M | if(rc != 0) { |
305 | | /* Failed to parse the TLV. Can't continue. */ |
306 | 40.0k | goto EXIT_LABEL; |
307 | 40.0k | } |
308 | 1.58M | } |
309 | | |
310 | 1.61M | if(tlv_rc != TLV_RC_NO_MORE_TLVS) { |
311 | | /* A TLV call failed. Can't continue. */ |
312 | 88.5k | goto EXIT_LABEL; |
313 | 88.5k | } |
314 | | |
315 | 7.28M | EXIT_LABEL: |
316 | | |
317 | 7.28M | return(rc); |
318 | 1.61M | } |
319 | | |
320 | | /** |
321 | | * Do different actions on the mime part for different received TLVs. |
322 | | */ |
323 | | int fuzz_parse_mime_tlv(curl_mimepart *part, TLV *tlv) |
324 | 1.58M | { |
325 | 1.58M | int rc; |
326 | 1.58M | char *tmp; |
327 | | |
328 | 1.58M | switch(tlv->type) { |
329 | 1.52M | case TLV_TYPE_MIME_PART_NAME: |
330 | 1.52M | tmp = fuzz_tlv_to_string(tlv); |
331 | 1.52M | curl_mime_name(part, tmp); |
332 | 1.52M | fuzz_free((void **)&tmp); |
333 | 1.52M | break; |
334 | | |
335 | 20.6k | case TLV_TYPE_MIME_PART_DATA: |
336 | 20.6k | curl_mime_data(part, (const char *)tlv->value, tlv->length); |
337 | 20.6k | break; |
338 | | |
339 | 40.0k | default: |
340 | | /* The fuzzer generates lots of unknown TLVs - we don't want these in the |
341 | | corpus so we reject any unknown TLVs. */ |
342 | 40.0k | rc = 255; |
343 | 40.0k | goto EXIT_LABEL; |
344 | 0 | break; |
345 | 1.58M | } |
346 | | |
347 | 1.54M | rc = 0; |
348 | | |
349 | 1.58M | EXIT_LABEL: |
350 | | |
351 | 1.58M | return rc; |
352 | 1.54M | } |