|
| 1 | +#include <stdint.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <fluent-bit/flb_time.h> |
| 5 | +#include <fluent-bit/flb_parser.h> |
| 6 | +#include <fluent-bit/flb_parser_decoder.h> |
| 7 | + |
| 8 | +#define TYPES_LEN 5 |
| 9 | +#define GET_MOD_EQ(max, idx) (data[0] % max) == idx |
| 10 | +#define MOVE_INPUT(offset) data += offset; size -= offset; |
| 11 | + |
| 12 | +char *get_null_terminated(size_t size, char **data, *total_data_size) { |
| 13 | + char *tmp = flb_malloc(size+1); |
| 14 | + memcpy(tmp, *data, size); |
| 15 | + tmp[size] = '\0'; |
| 16 | + |
| 17 | + // Modify the fuzz variables |
| 18 | + *total_data_size -= size; |
| 19 | + *data += size; |
| 20 | + |
| 21 | + return tmp; |
| 22 | +} |
| 23 | + |
| 24 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size){ |
| 25 | + char *format = NULL; |
| 26 | + char *time_fmt = NULL; |
| 27 | + char *time_key = NULL; |
| 28 | + char *time_offset = NULL; |
| 29 | + char *pregex = NULL; |
| 30 | + struct flb_parser_types *types = NULL; |
| 31 | + struct flb_config *fuzz_config = NULL; |
| 32 | + struct flb_parser *fuzz_parser = NULL; |
| 33 | + int time_keep = 0; |
| 34 | + int types_len = 0; |
| 35 | + |
| 36 | + if (size < 100) { |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + /* json parser */ |
| 41 | + fuzz_config = flb_config_init(); |
| 42 | + |
| 43 | + /* format + pregex */ |
| 44 | + if (GET_MOD_EQ(4,0)) { |
| 45 | + format = "json"; |
| 46 | + } |
| 47 | + else if (GET_MOD_EQ(4,1)) { |
| 48 | + format = "regex"; |
| 49 | + /* |
| 50 | + pregex = malloc(30); |
| 51 | + pregex[29] = '\0'; |
| 52 | + memcpy(pregex, data, 29); |
| 53 | + data += 29; |
| 54 | + size -= 29; |
| 55 | + */ |
| 56 | + } |
| 57 | + else if (GET_MOD_EQ(4,2)) { |
| 58 | + format = "ltsv"; |
| 59 | + } |
| 60 | + else { |
| 61 | + format = "logfmt"; |
| 62 | + } |
| 63 | + MOVE_INPUT(1); |
| 64 | + |
| 65 | + /* time_fmt */ |
| 66 | + if (GET_MOD_EQ(2,1)) { |
| 67 | + time_fmt = get_null_terminated(15, &data, &size); |
| 68 | + } |
| 69 | + MOVE_INPUT(1); |
| 70 | + |
| 71 | + /* time_key */ |
| 72 | + if (GET_MOD_EQ(2,1)) { |
| 73 | + time_key = get_null_terminated(15, &data, &size); |
| 74 | + } |
| 75 | + MOVE_INPUT(1); |
| 76 | + |
| 77 | + /* time_offset */ |
| 78 | + if (GET_MOD_EQ(2,1)) { |
| 79 | + time_offset = get_null_terminated(15, &data, &size); |
| 80 | + } |
| 81 | + MOVE_INPUT(1); |
| 82 | + |
| 83 | + /* time_keep */ |
| 84 | + time_keep = (GET_MOD_EQ(2,1)) ? MK_TRUE : MK_FALSE; |
| 85 | + MOVE_INPUT(1); |
| 86 | + |
| 87 | + /* types_str */ |
| 88 | + if (GET_MOD_EQ(2,1)) { |
| 89 | + types = flb_malloc(sizeof(struct flb_parser_types) * TYPES_LEN); |
| 90 | + char *parser_type_keys[5] = {"AAA", "BBB", "CCC", "DDD", "EEE" }; |
| 91 | + int parser_types[5] = {FLB_PARSER_TYPE_INT, FLB_PARSER_TYPE_FLOAT, |
| 92 | + FLB_PARSER_TYPE_BOOL, FLB_PARSER_TYPE_STRING, |
| 93 | + FLB_PARSER_TYPE_HEX}; |
| 94 | + for (int i = 0; i < TYPES_LEN; i++) { |
| 95 | + types[i].key = strdup(parser_type_keys[i]); |
| 96 | + types[i].key_len = strlen(parser_type_keys[i]); |
| 97 | + types[i].type = parser_types[i]; |
| 98 | + } |
| 99 | + types_len = TYPES_LEN; |
| 100 | + } |
| 101 | + MOVE_INPUT(1); |
| 102 | + |
| 103 | + /* decoders */ |
| 104 | + struct mk_list *list = NULL; |
| 105 | + if (GET_MOD_EQ(2,1)) { |
| 106 | + MOVE_INPUT(1); |
| 107 | + list = flb_malloc(sizeof(struct mk_list)); |
| 108 | + mk_list_init(list); |
| 109 | + |
| 110 | + struct flb_parser_dec *dec = malloc(sizeof(struct flb_parser_dec)); |
| 111 | + dec->key = flb_sds_create_len("AAA", 3); |
| 112 | + dec->buffer = flb_sds_create_size(FLB_PARSER_DEC_BUF_SIZE); |
| 113 | + dec->add_extra_keys = FLB_TRUE; |
| 114 | + mk_list_init(&dec->rules); |
| 115 | + mk_list_add(&dec->_head, list); |
| 116 | + |
| 117 | + struct flb_parser_dec_rule *dec_rule = malloc(sizeof(struct flb_parser_dec_rule)); |
| 118 | + dec_rule->type = (int)(data[0] % 0x02); |
| 119 | + MOVE_INPUT(1); |
| 120 | + dec_rule->backend = (int)(data[0] % 0x04); |
| 121 | + MOVE_INPUT(1); |
| 122 | + dec_rule->action = (int)data[0] % 0x03; |
| 123 | + mk_list_add(&dec_rule->_head, &dec->rules); |
| 124 | + |
| 125 | + if (GET_MOD_EQ(2,1)) { |
| 126 | + struct flb_parser_dec_rule *dec_rule2 = malloc(sizeof(struct flb_parser_dec_rule)); |
| 127 | + dec_rule2->type = (int)(data[0] % 0x02); |
| 128 | + MOVE_INPUT(1); |
| 129 | + dec_rule2->backend = (int)(data[0] % 0x04); |
| 130 | + MOVE_INPUT(1); |
| 131 | + dec_rule->action = (int)data[0] % 0x03; |
| 132 | + mk_list_add(&dec_rule2->_head, &dec->rules); |
| 133 | + } |
| 134 | + } |
| 135 | + MOVE_INPUT(1); |
| 136 | + fuzz_parser = flb_parser_create("fuzzer", format, pregex, |
| 137 | + time_fmt, time_key, time_offset, time_keep, |
| 138 | + types, types_len, list, fuzz_config); |
| 139 | + |
| 140 | + /* Second step is to use the random parser to parse random input */ |
| 141 | + if (fuzz_parser != NULL) { |
| 142 | + void *out_buf = NULL; |
| 143 | + size_t out_size = 0; |
| 144 | + struct flb_time out_time; |
| 145 | + flb_parser_do(fuzz_parser, (char*)data, size, |
| 146 | + &out_buf, &out_size, &out_time); |
| 147 | + if (out_buf != NULL) { |
| 148 | + free(out_buf); |
| 149 | + } |
| 150 | + flb_parser_destroy(fuzz_parser); |
| 151 | + } |
| 152 | + else { |
| 153 | + /* Parser creation failed but we still need to clean |
| 154 | + * up types and decoders */ |
| 155 | + if (types != NULL) { |
| 156 | + for (int i=0; i< TYPES_LEN; i++){ |
| 157 | + flb_free(types[i].key); |
| 158 | + } |
| 159 | + flb_free(types); |
| 160 | + } |
| 161 | + if (list != NULL) { |
| 162 | + flb_parser_decoder_list_destroy(list); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /* Cleanup everything but the parser */ |
| 167 | + flb_config_exit(fuzz_config); |
| 168 | + if (time_fmt != NULL) { |
| 169 | + flb_free(time_fmt); |
| 170 | + } |
| 171 | + if (time_key != NULL) { |
| 172 | + flb_free(time_key); |
| 173 | + } |
| 174 | + if (time_offset != NULL) { |
| 175 | + flb_free(time_offset); |
| 176 | + } |
| 177 | + if (pregex != NULL) { |
| 178 | + flb_free(pregex); |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
0 commit comments