curl_fuzzer.h
Go to the documentation of this file.
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.haxx.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 <curl/curl.h>
24 #include <testinput.h>
25 
29 #define TLV_TYPE_URL 1
30 #define TLV_TYPE_RESPONSE1 2
31 #define TLV_TYPE_USERNAME 3
32 #define TLV_TYPE_PASSWORD 4
33 #define TLV_TYPE_POSTFIELDS 5
34 #define TLV_TYPE_HEADER 6
35 #define TLV_TYPE_COOKIE 7
36 #define TLV_TYPE_UPLOAD1 8
37 #define TLV_TYPE_RANGE 9
38 #define TLV_TYPE_CUSTOMREQUEST 10
39 #define TLV_TYPE_MAIL_RECIPIENT 11
40 #define TLV_TYPE_MAIL_FROM 12
41 
45 #define TLV_RC_NO_ERROR 0
46 #define TLV_RC_NO_MORE_TLVS 1
47 #define TLV_RC_SIZE_ERROR 2
48 
49 /* Temporary write array size */
50 #define TEMP_WRITE_ARRAY_SIZE 10
51 
56 typedef struct tlv_raw
57 {
58  /* Type of the TLV - 16 bits. */
59  uint8_t raw_type[2];
60 
61  /* Length of the TLV data - 32 bits. */
62  uint8_t raw_length[4];
63 
64 } TLV_RAW;
65 
66 typedef struct tlv
67 {
68  /* Type of the TLV */
69  uint16_t type;
70 
71  /* Length of the TLV data */
72  uint32_t length;
73 
74  /* Pointer to data if length > 0. */
75  const uint8_t *value;
76 
77 } TLV;
78 
82 typedef struct fuzz_parse_state
83 {
84  /* Data stream */
85  const uint8_t *data;
86  size_t data_len;
87 
88  /* Current position of our "cursor" in processing the data stream. */
89  size_t data_pos;
90 
92 
96 typedef struct fuzz_data
97 {
98  /* CURL easy object */
100 
101  /* Parser state */
103 
104  /* Temporary writefunction state */
105  char write_array[TEMP_WRITE_ARRAY_SIZE];
106 
107  /* Response data and length */
108  const uint8_t *rsp1_data;
110 
111  /* Upload data and length; */
112  const uint8_t *upload1_data;
114 
115  /* Singleton string fields. */
116  char *url;
117  char *username;
118  char *password;
119  char *postfields;
120  char *cookie;
121  char *range;
123  char *mail_from;
124 
125  /* List of headers */
127 
128  /* List of mail recipients */
130 
131 } FUZZ_DATA;
132 
133 /* Function prototypes */
134 uint32_t to_u32(uint8_t b[4]);
135 uint16_t to_u16(uint8_t b[2]);
137  const uint8_t *data,
138  size_t data_len);
140 void fuzz_free(void **ptr);
141 static curl_socket_t fuzz_open_socket(void *ptr,
142  curlsocktype purpose,
143  struct curl_sockaddr *address);
144 static int fuzz_sockopt_callback(void *ptr,
145  curl_socket_t curlfd,
146  curlsocktype purpose);
147 static size_t fuzz_read_callback(char *buffer,
148  size_t size,
149  size_t nitems,
150  void *ptr);
151 static size_t fuzz_write_callback(void *contents,
152  size_t size,
153  size_t nmemb,
154  void *ptr);
155 int fuzz_get_first_tlv(FUZZ_DATA *fuzz, TLV *tlv);
156 int fuzz_get_next_tlv(FUZZ_DATA *fuzz, TLV *tlv);
157 int fuzz_get_tlv_comn(FUZZ_DATA *fuzz, TLV *tlv);
158 int fuzz_parse_tlv(FUZZ_DATA *fuzz, TLV *tlv);
159 char *fuzz_tlv_to_string(TLV *tlv);
160 
161 /* Macros */
162 #define FTRY(FUNC) \
163  { \
164  int _func_rc = (FUNC); \
165  if (_func_rc) \
166  { \
167  rc = _func_rc; \
168  goto EXIT_LABEL; \
169  } \
170  }
171 
172 #define FCHECK(COND) \
173  { \
174  if (!(COND)) \
175  { \
176  rc = 255; \
177  goto EXIT_LABEL; \
178  } \
179  }
180 
181 #define FSINGLETONTLV(TLVNAME, FIELDNAME, OPTNAME) \
182  case TLVNAME: \
183  FCHECK(fuzz->FIELDNAME == NULL); \
184  fuzz->FIELDNAME = fuzz_tlv_to_string(tlv); \
185  FTRY(curl_easy_setopt(fuzz->easy, OPTNAME, fuzz->FIELDNAME)); \
186  break
FUZZ_PARSE_STATE state
Definition: curl_fuzzer.h:102
const uint8_t * rsp1_data
Definition: curl_fuzzer.h:108
struct tlv_raw TLV_RAW
Byte stream representation of the TLV header.
size_t rsp1_data_len
Definition: curl_fuzzer.h:109
int fuzz_initialize_fuzz_data(FUZZ_DATA *fuzz, const uint8_t *data, size_t data_len)
Initialize the local fuzz data structure.
Definition: curl_fuzzer.cc:113
char * password
Definition: curl_fuzzer.h:118
char * customrequest
Definition: curl_fuzzer.h:122
size_t upload1_data_len
Definition: curl_fuzzer.h:113
int fuzz_parse_tlv(FUZZ_DATA *fuzz, TLV *tlv)
Do different actions on the CURL handle for different received TLVs.
Definition: curl_fuzzer.cc:367
CURL * easy
Definition: curl_fuzzer.h:99
uint16_t to_u16(uint8_t b[2])
Utility function to convert 2 bytes to a u16 predictably.
Definition: curl_fuzzer.cc:103
UNITTEST_START char * ptr
Definition: unit1330.c:38
char * cookie
Definition: curl_fuzzer.h:120
uint32_t to_u32(uint8_t b[4])
Utility function to convert 4 bytes to a u32 predictably.
Definition: curl_fuzzer.cc:93
char * fuzz_tlv_to_string(TLV *tlv)
Converts a TLV data and length into an allocated string.
Definition: curl_fuzzer.cc:434
char buffer[]
Definition: unit1308.c:48
char * username
Definition: curl_fuzzer.h:117
uint8_t raw_type[2]
Definition: curl_fuzzer.h:59
void fuzz_free(void **ptr)
If a pointer has been allocated, free that pointer.
Definition: curl_fuzzer.cc:196
char * postfields
Definition: curl_fuzzer.h:119
char * mail_from
Definition: curl_fuzzer.h:123
static int fuzz_sockopt_callback(void *ptr, curl_socket_t curlfd, curlsocktype purpose)
static size_t fuzz_write_callback(void *contents, size_t size, size_t nmemb, void *ptr)
curlsocktype
Definition: curl.h:360
const uint8_t * value
Definition: curl_fuzzer.h:75
struct tlv TLV
struct fuzz_data FUZZ_DATA
Data local to a fuzzing run.
int fuzz_get_first_tlv(FUZZ_DATA *fuzz, TLV *tlv)
TLV access function - gets the first TLV from a data stream.
Definition: curl_fuzzer.cc:311
void fuzz_terminate_fuzz_data(FUZZ_DATA *fuzz)
Terminate the fuzz data structure, including freeing any allocated memory.
Definition: curl_fuzzer.cc:166
#define TEMP_WRITE_ARRAY_SIZE
Definition: curl_fuzzer.h:50
static curl_socket_t fuzz_open_socket(void *ptr, curlsocktype purpose, struct curl_sockaddr *address)
uint32_t length
Definition: curl_fuzzer.h:72
const uint8_t * data
Definition: curl_fuzzer.h:85
Byte stream representation of the TLV header.
Definition: curl_fuzzer.h:56
uint16_t type
Definition: curl_fuzzer.h:69
int fuzz_get_tlv_comn(FUZZ_DATA *fuzz, TLV *tlv)
Common TLV function for accessing TLVs in a data stream.
Definition: curl_fuzzer.cc:340
char * url
Definition: curl_fuzzer.h:116
void CURL
Definition: curl.h:102
Data local to a fuzzing run.
Definition: curl_fuzzer.h:96
uint8_t raw_length[4]
Definition: curl_fuzzer.h:62
size_t size
Definition: unit1302.c:52
struct curl_slist * mail_recipients_list
Definition: curl_fuzzer.h:129
int fuzz_get_next_tlv(FUZZ_DATA *fuzz, TLV *tlv)
TLV access function - gets the next TLV from a data stream.
Definition: curl_fuzzer.cc:322
int curl_socket_t
Definition: curl.h:130
struct fuzz_parse_state FUZZ_PARSE_STATE
Internal state when parsing a TLV data stream.
const uint8_t * upload1_data
Definition: curl_fuzzer.h:112
struct curl_slist * header_list
Definition: curl_fuzzer.h:126
char * range
Definition: curl_fuzzer.h:121
static size_t fuzz_read_callback(char *buffer, size_t size, size_t nitems, void *ptr)
Internal state when parsing a TLV data stream.
Definition: curl_fuzzer.h:82
Definition: debug.c:29


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08