Coverage Report

Created: 2023-03-26 07:01

/src/fluent-bit/tests/internal/fuzzers/aws_util_fuzzer.c
Line
Count
Source
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*  Fluent Bit
4
 *  ==========
5
 *  Copyright (C) 2023 The Fluent Bit Authors
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License");
8
 *  you may not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *      http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
#include <stdint.h>
21
#include <fluent-bit.h>
22
#include <fluent-bit/flb_sds.h>
23
#include <fluent-bit/flb_aws_util.h>
24
#include <fluent-bit/flb_mem.h>
25
#include "flb_fuzz_header.h"
26
27
int initialization_crutch()
28
1.04k
{
29
1.04k
    struct flb_config *config;
30
1.04k
    config = flb_config_init();
31
1.04k
    if (config == NULL) {
32
263
        return -1;
33
263
    }
34
780
    flb_config_exit(config);
35
780
    return 0;
36
1.04k
}
37
38
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39
536
{
40
536
    char *format = NULL;
41
536
    char *tag = NULL;
42
536
    char *tag_delimiter = NULL;
43
44
    /* Set flb_malloc_mod to be fuzzer-data dependent */
45
536
    if (size < 4) {
46
2
        return 0;
47
2
    }
48
534
    flb_malloc_p = 0;
49
534
    flb_malloc_mod = *(int*)data;
50
534
    data += 4;
51
534
    size -= 4;
52
53
    /* Avoid division by zero for modulo operations */
54
534
    if (flb_malloc_mod == 0) {
55
4
        flb_malloc_mod = 1;
56
4
    }
57
58
534
    if (size < 300) {
59
68
        return 0;
60
68
    }
61
62
466
    format = get_null_terminated(50, &data, &size);
63
466
    tag = get_null_terminated(100, &data, &size);
64
466
    tag_delimiter = get_null_terminated(100, &data, &size);
65
66
466
    struct tm day = { 0, 0, 0, 15, 7, 120};
67
466
    time_t t;
68
466
    memset(&t, 0, sizeof(time_t));
69
70
466
    if (format && tag && tag_delimiter) {
71
466
        if (!initialization_crutch()) {
72
332
            flb_sds_t s3_key_format = NULL;
73
332
            s3_key_format = flb_get_s3_key(format, t, tag, tag_delimiter, 0);
74
332
            if (s3_key_format) {
75
285
                flb_sds_destroy(s3_key_format);
76
285
            }
77
332
            if (size > 200) {
78
254
                char *json_val = get_null_terminated(100, &data, &size);
79
254
                if (json_val != NULL) {
80
254
                    flb_sds_t s1 = flb_aws_error(json_val, strlen(json_val));
81
254
                    if (s1 != NULL) {
82
2
                        flb_sds_destroy(s1);
83
2
                    }
84
254
                    flb_free(json_val);
85
254
                }
86
254
                char *xml_val = get_null_terminated(100, &data, &size);
87
254
                if (xml_val != NULL) {
88
254
                    flb_sds_t s2 = flb_aws_xml_error(xml_val, strlen(xml_val));
89
254
                    if (s2 != NULL) {
90
4
                        flb_sds_destroy(s2);
91
4
                    }
92
254
                    flb_free(xml_val);
93
254
                }
94
254
            }
95
332
        }
96
466
    }
97
466
    if (format) {
98
466
        flb_free(format);
99
466
    }
100
466
    if (tag) {
101
466
        flb_free(tag);
102
466
    }
103
466
    if (tag_delimiter) {
104
466
        flb_free(tag_delimiter);
105
466
    }
106
466
    return 0;
107
534
}