/src/ndpi/fuzz/fuzz_ds_domain_classify.cpp
Line | Count | Source |
1 | | #include "ndpi_api.h" |
2 | | #include "ndpi_private.h" |
3 | | #include "fuzz_common_code.h" |
4 | | |
5 | | #include <stdint.h> |
6 | | #include <stdio.h> |
7 | | #include <assert.h> |
8 | | #include "fuzzer/FuzzedDataProvider.h" |
9 | | |
10 | | static struct ndpi_detection_module_struct *ndpi_struct = NULL; |
11 | | |
12 | | extern "C" { |
13 | | |
14 | | #ifdef NDPI_ENABLE_DEBUG_MESSAGES |
15 | | void ndpi_debug_printf(uint16_t proto, struct ndpi_detection_module_struct *ndpi_str, ndpi_log_level_t log_level, |
16 | | const char *file_name, const char *func_name, unsigned int line_number, const char *format, ...); |
17 | | #endif |
18 | | |
19 | 1.31k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
20 | 1.31k | FuzzedDataProvider fuzzed_data(data, size); |
21 | 1.31k | u_int16_t i, num_iteration, is_added = 0; |
22 | 1.31k | bool rc; |
23 | 1.31k | ndpi_domain_classify *d; |
24 | 1.31k | u_int64_t class_id; |
25 | 1.31k | std::string value, value_added; |
26 | | |
27 | | /* We don't need a complete (and costly to set up) context! |
28 | | Setting up manually only what is really needed is complex (and error prone!) |
29 | | but allow us to be significant faster and to have better coverage */ |
30 | 1.31k | if (ndpi_struct == NULL) { |
31 | 1 | ndpi_struct = (struct ndpi_detection_module_struct *)ndpi_calloc(1, sizeof(struct ndpi_detection_module_struct)); |
32 | | #ifdef NDPI_ENABLE_DEBUG_MESSAGES |
33 | | set_ndpi_debug_function(ndpi_struct, (ndpi_debug_function_ptr)ndpi_debug_printf); |
34 | | #endif |
35 | 1 | if (ndpi_struct) { |
36 | 1 | ndpi_struct->cfg.log_level = NDPI_LOG_DEBUG_EXTRA; |
37 | 1 | ndpi_load_domain_suffixes(ndpi_struct, (char *)"public_suffix_list.dat"); |
38 | 1 | } |
39 | 1 | } |
40 | | |
41 | | /* To allow memory allocation failures */ |
42 | 1.31k | fuzz_set_alloc_callbacks_and_seed(size); |
43 | | |
44 | 1.31k | d = ndpi_domain_classify_alloc(); |
45 | | |
46 | 1.31k | num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
47 | | |
48 | 119k | for (i = 0; i < num_iteration; i++) { |
49 | 118k | value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>()); |
50 | 118k | class_id = fuzzed_data.ConsumeIntegral<u_int64_t>(); |
51 | 118k | rc = ndpi_domain_classify_add(fuzzed_data.ConsumeBool() ? ndpi_struct : NULL, |
52 | 118k | d, class_id, (char*)value.c_str()); |
53 | | |
54 | | /* Keep one random entry really added */ |
55 | 118k | if (rc == true && is_added == 0 && fuzzed_data.ConsumeBool()) { |
56 | 681 | value_added = value; |
57 | 681 | is_added = 1; |
58 | 681 | } |
59 | 118k | } |
60 | | |
61 | 1.31k | ndpi_domain_classify_add_domains(ndpi_struct, d, |
62 | 1.31k | fuzzed_data.ConsumeIntegralInRange(0, 512), |
63 | 1.31k | fuzzed_data.ConsumeBool() ? (char *)"random_list.list" : (char *)"wrong_path"); |
64 | | |
65 | | /* "Random" search */ |
66 | 1.31k | num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
67 | 73.2k | for (i = 0; i < num_iteration; i++) { |
68 | 71.9k | value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>()); |
69 | 71.9k | ndpi_domain_classify_hostname(fuzzed_data.ConsumeBool() ? ndpi_struct : NULL, d, &class_id, (char *)value.c_str()); |
70 | 71.9k | } |
71 | | |
72 | | /* Search of an added entry */ |
73 | 1.31k | if (is_added) { |
74 | 681 | ndpi_domain_classify_hostname(ndpi_struct, d, &class_id, (char *)value_added.c_str()); |
75 | 681 | } |
76 | | |
77 | 1.31k | ndpi_domain_classify_size(d); |
78 | 1.31k | ndpi_domain_classify_free(d); |
79 | | |
80 | 1.31k | return 0; |
81 | 1.31k | } |
82 | | |
83 | | } |