summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/tests/internal/fuzzers/config_fuzzer.c
blob: 5512dac2933547329f5d4dbbb14b24956e754689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <fluent-bit/flb_parser.h>
#include <fluent-bit/flb_slist.h>
#include <fluent-bit/flb_kv.h>
#include "flb_fuzz_header.h"

/* A sample of configurations */
char conf_file[] = "# Parser: no_year\n"
"# ===============\n"
"# the given format don't contain the Year, this is a common\n"
"# case on old Syslog implementations.\n"
"#\n"
"[PARSER]\n"
"    Name        no_year\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S\n"
"    Time_Keep   On\n"
"\n"
"# Parser: no_year_N\n"
"# =================\n"
"# Just for compatibility, check a string with no year but including Nanoseconds.\n"
"#\n"
"[PARSER]\n"
"    Name        no_year_N\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S.%L\n"
"    Time_Keep   On\n"
"\n"
"# Parser: no_year_NC\n"
"# =================\n"
"# Just for compatibility, check a string with no year but including Nanoseconds with comma as fractional separator.\n"
"#\n"
"[PARSER]\n"
"    Name        no_year_NC\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S,%L\n"
"    Time_Keep   On\n"
"\n"
"# Parser: no_year_TZ\n"
"# =================\n"
"# Time string with no year and including timezone\n"
"#\n"
"[PARSER]\n"
"    Name        no_year_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S %z\n"
"    Time_Keep   On\n"
"\n"
"# Parser: no_year_N_TZ\n"
"# ====================\n"
"# Time string with no year, nanoseconds and timezone\n"
"#\n"
"[PARSER]\n"
"    Name        no_year_N_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S.%L %z\n"
"    Time_Keep   On\n"
"\n"
"\n"
"# Parser: no_year_NC_TZ\n"
"# ====================\n"
"# Time string with no year, nanoseconds and timezone with comma as fractional separator.\n"
"#\n"
"[PARSER]\n"
"    Name        no_year_NC_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %b %d %H:%M:%S,%L %z\n"
"    Time_Keep   On\n"
"\n"
"\n"
"# Parser: default_UTC\n"
"# ===================\n"
"# Time string with timezone in UTC\n"
"#\n"
"[PARSER]\n"
"    Name        default_UTC\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S\n"
"    Time_Keep   On\n"
"\n"
"# Parser: default_UTC_Z\n"
"# =====================\n"
"# Time string with timezone in UTC and ending Z\n"
"#\n"
"[PARSER]\n"
"    Name        default_UTC_Z\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%SZ\n"
"    Time_Keep   On\n"
"\n"
"# Parser: default_UTC_N_Z\n"
"# =======================\n"
"# Time string with timezone in UTC, nanoseconds and ending Z\n"
"#\n"
"[PARSER]\n"
"    Name        default_UTC_N_Z\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S.%LZ\n"
"    Time_Keep   On\n"
"\n"
"# Parser: default_UTC_NC_Z\n"
"# =======================\n"
"# Time string with timezone in UTC, nanoseconds with comma as fractional separator and ending Z\n"
"#\n"
"[PARSER]\n"
"    Name        default_UTC_NC_Z\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S,%LZ\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic_TZ\n"
"# ==================\n"
"# Generic date with timezone\n"
"#\n"
"[PARSER]\n"
"    Name        generic_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S %z\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic\n"
"# ===============\n"
"# Generic date\n"
"#\n"
"[PARSER]\n"
"    Name        generic\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic_N\n"
"# ===============\n"
"# Generic date with nanoseconds\n"
"#\n"
"[PARSER]\n"
"    Name        generic_N\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S.%L\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic_NC\n"
"# ===============\n"
"# Generic date with nanoseconds with comma as fractional separator\n"
"#\n"
"[PARSER]\n"
"    Name        generic_NC\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S,%L\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic_N_TZ\n"
"# ====================\n"
"# Generic date with nanoseconds and timezone\n"
"#\n"
"[PARSER]\n"
"    Name        generic_N_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S.%L %z\n"
"    Time_Keep   On\n"
"\n"
"# Parser: generic_NC_TZ\n"
"# ====================\n"
"# Generic date with nanoseconds with comma as fractional separator and timezone\n"
"#\n"
"[PARSER]\n"
"    Name        generic_NC_TZ\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S,%L %z\n"
"    Time_Keep   On\n"
"\n"
"# Parser: apache_error\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        apache_error\n"
"    Format      json\n"
"    Time_Key    time\n"
"    Time_Format %a %b %d %H:%M:%S.%L %Y\n"
"    Time_Keep   On\n"
"# Parser: mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        mysql_quoted_stuff\n"
"    Format      regex\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   mysql_quoted key001\n"
"# Parser: REGEX_generic_NC_TZ\n"
"# ====================\n"
"# Generic date with nanoseconds with comma as fractional separator and timezone\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX_generic_NC_TZ\n"
"    Format      regex\n"
"    Regex       ^(?<key001>[^ ]*) (?<key002>[^ ]*) (?<time>.+)$\n"
"    Time_Key    time\n"
"    Time_Format %m/%d/%Y %H:%M:%S,%L %z\n"
"    Time_Keep   On\n"
"\n"
"# Parser: REGEX_apache_error\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX_apache_error\n"
"    Format      regex\n"
"    Regex       ^(?<key001>[^ ]*) (?<key002>[^ ]*) (?<time>.+)$\n"
"    Time_Key    time\n"
"    Time_Format %a %b %d %H:%M:%S.%L %Y\n"
"    Time_Keep   On\n"
"\n"
"\n"
"\n"
"# Parser: REGEX_mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX_mysql_quoted_stuff\n"
"    Format      regex\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   mysql_quoted key001\n"
"\n"
"\n"
"\n"
"# Parser: REGEX2_mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX2_mysql_quoted_stuff\n"
"    Format      logfmt\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   mysql_quoted key001\n"
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
"\n"
"\n"
"\n"
"# Parser: REGEX3_mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX3_mysql_quoted_stuff\n"
"    Format      json\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   escaped_utf8 key001\n"
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
"\n"
"\n"
"\n"
"# Parser: REGEX33_mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX33_mysql_quoted_stuff\n"
"    Format      json\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   escaped key001\n"
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
"\n"
"\n"
"\n"
"# Parser: REGEX4_mysql_quoted_stuff\n"
"# ====================\n"
"# Apache error log time format\n"
"#\n"
"[PARSER]\n"
"    Name        REGEX4_mysql_quoted_stuff\n"
"    Format      json\n"
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
"    Time_Key    time\n"
"    Time_Format %Y-%M-%S %H:%M:%S\n"
"    Time_Keep   On\n"
"    Decode_Field_As   json key001\n"
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
"[MULTILINE_PARSER]\n"
"    name          exception_test\n"
"    type          regex\n"
"    flush_timeout 1000\n"
"    rule          \"start_state\"  \"/(Dec \\d+ \\d+\\:\\d+\\:\\d+)(.*)/\" \"cont\"\n"
"    rule          \"cont\" \"/^\\s+at.*/\" \"cont\"\n";


int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    /* Set fuzzer-malloc chance of failure */
    flb_malloc_p = 0;
    flb_malloc_mod = 25000;

    /* Limit the size of the config files to 32KB. */
    if (size > 32768) {
        return 0;
    }

    /* Write the config file to a location we know OSS-Fuzz has */
    char filename[256];
    sprintf(filename, "/tmp/libfuzzer.%d", getpid());
    FILE *fp = fopen(filename, "wb");
    if (!fp) {
        return 0;
    }
    fwrite(conf_file, strlen(conf_file), 1, fp);
    fclose(fp);


    /* Now parse random data based on the config files */
    struct flb_config *config = NULL;
    config = flb_config_init();
    int ret = flb_parser_conf_file(filename, config);
    if (ret == 0) {
        struct mk_list *head = NULL;
        mk_list_foreach(head, &config->parsers) {
            size_t out_size;
            char *out_buf = NULL;
            struct flb_parser *parser = NULL;
            struct flb_time out_time;
            parser = mk_list_entry(head, struct flb_parser, _head);
            flb_parser_do(parser, (const char*)data, size, (void **)&out_buf,
                          &out_size, &out_time);
            if (out_buf != NULL) {
                free(out_buf);
            }
        }
    }
    flb_parser_exit(config);
    flb_config_exit(config);

    if (size > 100) {
        /* Now let's do a second run where we also call flb_config_set_property */
        config = flb_config_init();
        ret = flb_parser_conf_file(filename, config);
        char *key_1 = get_null_terminated(15, &data, &size);
        char *val_1 = get_null_terminated(15, &data, &size);
        char *key_2 = get_null_terminated(15, &data, &size);
        char *val_2 = get_null_terminated(15, &data, &size);
        char *progname = get_null_terminated(15, &data, &size);

        flb_config_set_property(config, key_1, val_1);
        flb_config_set_property(config, key_2, val_2);
        flb_config_set_program_name(config, progname);
        set_log_level_from_env(config);

        struct mk_list prop;
        flb_kv_init(&prop);
        flb_kv_item_create(&prop, key_1, val_1);
        flb_config_prop_get(progname, &prop);
        flb_slist_entry_get(&prop, (int)data[0]);
        flb_slist_dump(&prop);
        
        if (ret == 0) {
            struct mk_list *head = NULL;
            mk_list_foreach(head, &config->parsers) {
                size_t out_size;
                char *out_buf = NULL;
                struct flb_parser *parser = NULL;
                struct flb_time out_time;
                
                parser = mk_list_entry(head, struct flb_parser, _head);
                flb_parser_do(parser, (const char*)data, size, (void **)&out_buf,
                              &out_size, &out_time);
                if (out_buf != NULL) {
                    free(out_buf);
                }
            }
        }
        flb_parser_exit(config);
        flb_config_exit(config);
        flb_free(key_1);
        flb_free(val_1);
        flb_free(key_2);
        flb_free(val_2);
        flb_free(progname);
        flb_kv_release(&prop);
    }

    /* clean up the file */
    unlink(filename);

    return 0;
}