Coverage Report

Created: 2024-09-19 07:08

/src/fluent-bit/tests/internal/fuzzers/config_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdint.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include <fluent-bit/flb_parser.h>
5
#include <fluent-bit/flb_slist.h>
6
#include <fluent-bit/flb_kv.h>
7
#include "flb_fuzz_header.h"
8
9
/* A sample of configurations */
10
char conf_file[] = "# Parser: no_year\n"
11
"# ===============\n"
12
"# the given format don't contain the Year, this is a common\n"
13
"# case on old Syslog implementations.\n"
14
"#\n"
15
"[PARSER]\n"
16
"    Name        no_year\n"
17
"    Format      json\n"
18
"    Time_Key    time\n"
19
"    Time_Format %b %d %H:%M:%S\n"
20
"    Time_Keep   On\n"
21
"\n"
22
"# Parser: no_year_N\n"
23
"# =================\n"
24
"# Just for compatibility, check a string with no year but including Nanoseconds.\n"
25
"#\n"
26
"[PARSER]\n"
27
"    Name        no_year_N\n"
28
"    Format      json\n"
29
"    Time_Key    time\n"
30
"    Time_Format %b %d %H:%M:%S.%L\n"
31
"    Time_Keep   On\n"
32
"\n"
33
"# Parser: no_year_NC\n"
34
"# =================\n"
35
"# Just for compatibility, check a string with no year but including Nanoseconds with comma as fractional separator.\n"
36
"#\n"
37
"[PARSER]\n"
38
"    Name        no_year_NC\n"
39
"    Format      json\n"
40
"    Time_Key    time\n"
41
"    Time_Format %b %d %H:%M:%S,%L\n"
42
"    Time_Keep   On\n"
43
"\n"
44
"# Parser: no_year_TZ\n"
45
"# =================\n"
46
"# Time string with no year and including timezone\n"
47
"#\n"
48
"[PARSER]\n"
49
"    Name        no_year_TZ\n"
50
"    Format      json\n"
51
"    Time_Key    time\n"
52
"    Time_Format %b %d %H:%M:%S %z\n"
53
"    Time_Keep   On\n"
54
"\n"
55
"# Parser: no_year_N_TZ\n"
56
"# ====================\n"
57
"# Time string with no year, nanoseconds and timezone\n"
58
"#\n"
59
"[PARSER]\n"
60
"    Name        no_year_N_TZ\n"
61
"    Format      json\n"
62
"    Time_Key    time\n"
63
"    Time_Format %b %d %H:%M:%S.%L %z\n"
64
"    Time_Keep   On\n"
65
"\n"
66
"\n"
67
"# Parser: no_year_NC_TZ\n"
68
"# ====================\n"
69
"# Time string with no year, nanoseconds and timezone with comma as fractional separator.\n"
70
"#\n"
71
"[PARSER]\n"
72
"    Name        no_year_NC_TZ\n"
73
"    Format      json\n"
74
"    Time_Key    time\n"
75
"    Time_Format %b %d %H:%M:%S,%L %z\n"
76
"    Time_Keep   On\n"
77
"\n"
78
"\n"
79
"# Parser: default_UTC\n"
80
"# ===================\n"
81
"# Time string with timezone in UTC\n"
82
"#\n"
83
"[PARSER]\n"
84
"    Name        default_UTC\n"
85
"    Format      json\n"
86
"    Time_Key    time\n"
87
"    Time_Format %m/%d/%Y %H:%M:%S\n"
88
"    Time_Keep   On\n"
89
"\n"
90
"# Parser: default_UTC_Z\n"
91
"# =====================\n"
92
"# Time string with timezone in UTC and ending Z\n"
93
"#\n"
94
"[PARSER]\n"
95
"    Name        default_UTC_Z\n"
96
"    Format      json\n"
97
"    Time_Key    time\n"
98
"    Time_Format %m/%d/%Y %H:%M:%SZ\n"
99
"    Time_Keep   On\n"
100
"\n"
101
"# Parser: default_UTC_N_Z\n"
102
"# =======================\n"
103
"# Time string with timezone in UTC, nanoseconds and ending Z\n"
104
"#\n"
105
"[PARSER]\n"
106
"    Name        default_UTC_N_Z\n"
107
"    Format      json\n"
108
"    Time_Key    time\n"
109
"    Time_Format %m/%d/%Y %H:%M:%S.%LZ\n"
110
"    Time_Keep   On\n"
111
"\n"
112
"# Parser: default_UTC_NC_Z\n"
113
"# =======================\n"
114
"# Time string with timezone in UTC, nanoseconds with comma as fractional separator and ending Z\n"
115
"#\n"
116
"[PARSER]\n"
117
"    Name        default_UTC_NC_Z\n"
118
"    Format      json\n"
119
"    Time_Key    time\n"
120
"    Time_Format %m/%d/%Y %H:%M:%S,%LZ\n"
121
"    Time_Keep   On\n"
122
"\n"
123
"# Parser: generic_TZ\n"
124
"# ==================\n"
125
"# Generic date with timezone\n"
126
"#\n"
127
"[PARSER]\n"
128
"    Name        generic_TZ\n"
129
"    Format      json\n"
130
"    Time_Key    time\n"
131
"    Time_Format %m/%d/%Y %H:%M:%S %z\n"
132
"    Time_Keep   On\n"
133
"\n"
134
"# Parser: generic\n"
135
"# ===============\n"
136
"# Generic date\n"
137
"#\n"
138
"[PARSER]\n"
139
"    Name        generic\n"
140
"    Format      json\n"
141
"    Time_Key    time\n"
142
"    Time_Format %m/%d/%Y %H:%M:%S\n"
143
"    Time_Keep   On\n"
144
"\n"
145
"# Parser: generic_N\n"
146
"# ===============\n"
147
"# Generic date with nanoseconds\n"
148
"#\n"
149
"[PARSER]\n"
150
"    Name        generic_N\n"
151
"    Format      json\n"
152
"    Time_Key    time\n"
153
"    Time_Format %m/%d/%Y %H:%M:%S.%L\n"
154
"    Time_Keep   On\n"
155
"\n"
156
"# Parser: generic_NC\n"
157
"# ===============\n"
158
"# Generic date with nanoseconds with comma as fractional separator\n"
159
"#\n"
160
"[PARSER]\n"
161
"    Name        generic_NC\n"
162
"    Format      json\n"
163
"    Time_Key    time\n"
164
"    Time_Format %m/%d/%Y %H:%M:%S,%L\n"
165
"    Time_Keep   On\n"
166
"\n"
167
"# Parser: generic_N_TZ\n"
168
"# ====================\n"
169
"# Generic date with nanoseconds and timezone\n"
170
"#\n"
171
"[PARSER]\n"
172
"    Name        generic_N_TZ\n"
173
"    Format      json\n"
174
"    Time_Key    time\n"
175
"    Time_Format %m/%d/%Y %H:%M:%S.%L %z\n"
176
"    Time_Keep   On\n"
177
"\n"
178
"# Parser: generic_NC_TZ\n"
179
"# ====================\n"
180
"# Generic date with nanoseconds with comma as fractional separator and timezone\n"
181
"#\n"
182
"[PARSER]\n"
183
"    Name        generic_NC_TZ\n"
184
"    Format      json\n"
185
"    Time_Key    time\n"
186
"    Time_Format %m/%d/%Y %H:%M:%S,%L %z\n"
187
"    Time_Keep   On\n"
188
"\n"
189
"# Parser: apache_error\n"
190
"# ====================\n"
191
"# Apache error log time format\n"
192
"#\n"
193
"[PARSER]\n"
194
"    Name        apache_error\n"
195
"    Format      json\n"
196
"    Time_Key    time\n"
197
"    Time_Format %a %b %d %H:%M:%S.%L %Y\n"
198
"    Time_Keep   On\n"
199
"# Parser: mysql_quoted_stuff\n"
200
"# ====================\n"
201
"# Apache error log time format\n"
202
"#\n"
203
"[PARSER]\n"
204
"    Name        mysql_quoted_stuff\n"
205
"    Format      regex\n"
206
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
207
"    Time_Key    time\n"
208
"    Time_Format %Y-%M-%S %H:%M:%S\n"
209
"    Time_Keep   On\n"
210
"    Decode_Field_As   mysql_quoted key001\n"
211
"# Parser: REGEX_generic_NC_TZ\n"
212
"# ====================\n"
213
"# Generic date with nanoseconds with comma as fractional separator and timezone\n"
214
"#\n"
215
"[PARSER]\n"
216
"    Name        REGEX_generic_NC_TZ\n"
217
"    Format      regex\n"
218
"    Regex       ^(?<key001>[^ ]*) (?<key002>[^ ]*) (?<time>.+)$\n"
219
"    Time_Key    time\n"
220
"    Time_Format %m/%d/%Y %H:%M:%S,%L %z\n"
221
"    Time_Keep   On\n"
222
"\n"
223
"# Parser: REGEX_apache_error\n"
224
"# ====================\n"
225
"# Apache error log time format\n"
226
"#\n"
227
"[PARSER]\n"
228
"    Name        REGEX_apache_error\n"
229
"    Format      regex\n"
230
"    Regex       ^(?<key001>[^ ]*) (?<key002>[^ ]*) (?<time>.+)$\n"
231
"    Time_Key    time\n"
232
"    Time_Format %a %b %d %H:%M:%S.%L %Y\n"
233
"    Time_Keep   On\n"
234
"\n"
235
"\n"
236
"\n"
237
"# Parser: REGEX_mysql_quoted_stuff\n"
238
"# ====================\n"
239
"# Apache error log time format\n"
240
"#\n"
241
"[PARSER]\n"
242
"    Name        REGEX_mysql_quoted_stuff\n"
243
"    Format      regex\n"
244
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
245
"    Time_Key    time\n"
246
"    Time_Format %Y-%M-%S %H:%M:%S\n"
247
"    Time_Keep   On\n"
248
"    Decode_Field_As   mysql_quoted key001\n"
249
"\n"
250
"\n"
251
"\n"
252
"# Parser: REGEX2_mysql_quoted_stuff\n"
253
"# ====================\n"
254
"# Apache error log time format\n"
255
"#\n"
256
"[PARSER]\n"
257
"    Name        REGEX2_mysql_quoted_stuff\n"
258
"    Format      logfmt\n"
259
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
260
"    Time_Key    time\n"
261
"    Time_Format %Y-%M-%S %H:%M:%S\n"
262
"    Time_Keep   On\n"
263
"    Decode_Field_As   mysql_quoted key001\n"
264
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
265
"\n"
266
"\n"
267
"\n"
268
"# Parser: REGEX3_mysql_quoted_stuff\n"
269
"# ====================\n"
270
"# Apache error log time format\n"
271
"#\n"
272
"[PARSER]\n"
273
"    Name        REGEX3_mysql_quoted_stuff\n"
274
"    Format      json\n"
275
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
276
"    Time_Key    time\n"
277
"    Time_Format %Y-%M-%S %H:%M:%S\n"
278
"    Time_Keep   On\n"
279
"    Decode_Field_As   escaped_utf8 key001\n"
280
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
281
"\n"
282
"\n"
283
"\n"
284
"# Parser: REGEX33_mysql_quoted_stuff\n"
285
"# ====================\n"
286
"# Apache error log time format\n"
287
"#\n"
288
"[PARSER]\n"
289
"    Name        REGEX33_mysql_quoted_stuff\n"
290
"    Format      json\n"
291
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
292
"    Time_Key    time\n"
293
"    Time_Format %Y-%M-%S %H:%M:%S\n"
294
"    Time_Keep   On\n"
295
"    Decode_Field_As   escaped key001\n"
296
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
297
"\n"
298
"\n"
299
"\n"
300
"# Parser: REGEX4_mysql_quoted_stuff\n"
301
"# ====================\n"
302
"# Apache error log time format\n"
303
"#\n"
304
"[PARSER]\n"
305
"    Name        REGEX4_mysql_quoted_stuff\n"
306
"    Format      json\n"
307
"    Regex       ^(?<time>.*?),(?<key001>.*)$\n"
308
"    Time_Key    time\n"
309
"    Time_Format %Y-%M-%S %H:%M:%S\n"
310
"    Time_Keep   On\n"
311
"    Decode_Field_As   json key001\n"
312
"    Types A1:integer A2:string A3:bool A4:float A5:hex\n"
313
"[MULTILINE_PARSER]\n"
314
"    name          exception_test\n"
315
"    type          regex\n"
316
"    flush_timeout 1000\n"
317
"    rule          \"start_state\"  \"/(Dec \\d+ \\d+\\:\\d+\\:\\d+)(.*)/\" \"cont\"\n"
318
"    rule          \"cont\" \"/^\\s+at.*/\" \"cont\"\n";
319
320
321
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
322
7.41k
{
323
    /* Set fuzzer-malloc chance of failure */
324
7.41k
    flb_malloc_p = 0;
325
7.41k
    flb_malloc_mod = 25000;
326
327
    /* Limit the size of the config files to 32KB. */
328
7.41k
    if (size > 32768) {
329
7
        return 0;
330
7
    }
331
332
    /* Write the config file to a location we know OSS-Fuzz has */
333
7.40k
    char filename[256];
334
7.40k
    sprintf(filename, "/tmp/libfuzzer.%d", getpid());
335
7.40k
    FILE *fp = fopen(filename, "wb");
336
7.40k
    if (!fp) {
337
0
        return 0;
338
0
    }
339
7.40k
    fwrite(conf_file, strlen(conf_file), 1, fp);
340
7.40k
    fclose(fp);
341
342
343
    /* Now parse random data based on the config files */
344
7.40k
    struct flb_config *config = NULL;
345
7.40k
    config = flb_config_init();
346
7.40k
    int ret = flb_parser_conf_file(filename, config);
347
7.40k
    if (ret == 0) {
348
7.40k
        struct mk_list *head = NULL;
349
199k
        mk_list_foreach(head, &config->parsers) {
350
199k
            size_t out_size;
351
199k
            char *out_buf = NULL;
352
199k
            struct flb_parser *parser = NULL;
353
199k
            struct flb_time out_time;
354
199k
            parser = mk_list_entry(head, struct flb_parser, _head);
355
199k
            flb_parser_do(parser, (const char*)data, size, (void **)&out_buf,
356
199k
                          &out_size, &out_time);
357
199k
            if (out_buf != NULL) {
358
66.2k
                free(out_buf);
359
66.2k
            }
360
199k
        }
361
7.40k
    }
362
7.40k
    flb_parser_exit(config);
363
7.40k
    flb_config_exit(config);
364
365
7.40k
    if (size > 100) {
366
        /* Now let's do a second run where we also call flb_config_set_property */
367
2.65k
        config = flb_config_init();
368
2.65k
        ret = flb_parser_conf_file(filename, config);
369
2.65k
        char *key_1 = get_null_terminated(15, &data, &size);
370
2.65k
        char *val_1 = get_null_terminated(15, &data, &size);
371
2.65k
        char *key_2 = get_null_terminated(15, &data, &size);
372
2.65k
        char *val_2 = get_null_terminated(15, &data, &size);
373
2.65k
        char *progname = get_null_terminated(15, &data, &size);
374
375
2.65k
        flb_config_set_property(config, key_1, val_1);
376
2.65k
        flb_config_set_property(config, key_2, val_2);
377
2.65k
        flb_config_set_program_name(config, progname);
378
2.65k
        set_log_level_from_env(config);
379
380
2.65k
        struct mk_list prop;
381
2.65k
        flb_kv_init(&prop);
382
2.65k
        flb_kv_item_create(&prop, key_1, val_1);
383
2.65k
        flb_config_prop_get(progname, &prop);
384
2.65k
        flb_slist_entry_get(&prop, (int)data[0]);
385
2.65k
        flb_slist_dump(&prop);
386
        
387
2.65k
        if (ret == 0) {
388
2.65k
            struct mk_list *head = NULL;
389
71.6k
            mk_list_foreach(head, &config->parsers) {
390
71.6k
                size_t out_size;
391
71.6k
                char *out_buf = NULL;
392
71.6k
                struct flb_parser *parser = NULL;
393
71.6k
                struct flb_time out_time;
394
                
395
71.6k
                parser = mk_list_entry(head, struct flb_parser, _head);
396
71.6k
                flb_parser_do(parser, (const char*)data, size, (void **)&out_buf,
397
71.6k
                              &out_size, &out_time);
398
71.6k
                if (out_buf != NULL) {
399
16.8k
                    free(out_buf);
400
16.8k
                }
401
71.6k
            }
402
2.65k
        }
403
2.65k
        flb_parser_exit(config);
404
2.65k
        flb_config_exit(config);
405
2.65k
        flb_free(key_1);
406
2.65k
        flb_free(val_1);
407
2.65k
        flb_free(key_2);
408
2.65k
        flb_free(val_2);
409
2.65k
        flb_free(progname);
410
2.65k
        flb_kv_release(&prop);
411
2.65k
    }
412
413
    /* clean up the file */
414
7.40k
    unlink(filename);
415
416
7.40k
    return 0;
417
7.40k
}