/src/ndpi/fuzz/fuzz_ndpi_reader_alloc_fail.c
Line | Count | Source |
1 | | #include "reader_util.h" |
2 | | #include "ndpi_api.h" |
3 | | #include "fuzz_common_code.h" |
4 | | |
5 | | #include <pcap/pcap.h> |
6 | | |
7 | | #include <errno.h> |
8 | | #include <stdint.h> |
9 | | #include <stdio.h> |
10 | | #include <assert.h> |
11 | | #include <libgen.h> |
12 | | |
13 | | #ifdef ENABLE_PCAP_L7_MUTATOR |
14 | | #include "pl7m.h" |
15 | | #endif |
16 | | |
17 | | struct ndpi_workflow_prefs *prefs = NULL; |
18 | | struct ndpi_workflow *workflow = NULL; |
19 | | struct ndpi_global_context *g_ctx; |
20 | | |
21 | | u_int8_t enable_payload_analyzer = 0; |
22 | | u_int8_t enable_flow_stats = 1; |
23 | | u_int8_t human_readeable_string_len = 5; |
24 | | u_int8_t max_num_udp_dissected_pkts = 0, max_num_tcp_dissected_pkts = 0; /* Disable limits at application layer */; |
25 | | int malloc_size_stats = 0; |
26 | | FILE *fingerprint_fp = NULL; |
27 | | char *addr_dump_path = NULL; |
28 | | int monitoring_enabled = 1; |
29 | | u_int8_t enable_doh_dot_detection = 0; |
30 | | |
31 | | static char *path = NULL; |
32 | | |
33 | | extern void ndpi_report_payload_stats(FILE *out); |
34 | | |
35 | | #ifdef CRYPT_FORCE_NO_AESNI |
36 | | extern int force_no_aesni; |
37 | | #endif |
38 | | |
39 | | #ifdef ENABLE_PCAP_L7_MUTATOR |
40 | | size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, |
41 | | size_t MaxSize, unsigned int Seed) { |
42 | | return pl7m_mutator(Data, Size, MaxSize, Seed); |
43 | | } |
44 | | #endif |
45 | | |
46 | 20 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
47 | 20 | (void)argc; |
48 | | |
49 | 20 | path = dirname(strdup(*argv[0])); /* No errors; no free! */ |
50 | 20 | return 0; |
51 | 20 | } |
52 | | |
53 | 600k | static void node_cleanup_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) { |
54 | 600k | struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node; |
55 | | |
56 | 600k | (void)depth; |
57 | 600k | (void)user_data; |
58 | | |
59 | 600k | if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */ |
60 | 270k | if((!flow->detection_completed) && flow->ndpi_flow) { |
61 | 224k | flow->detected_protocol = ndpi_detection_giveup(workflow->ndpi_struct, |
62 | 224k | flow->ndpi_flow); |
63 | 224k | } |
64 | | |
65 | 270k | process_ndpi_collected_info(workflow, flow); |
66 | 270k | } |
67 | 600k | } |
68 | | |
69 | | static void fn_flow_callback(struct ndpi_workflow *w, struct ndpi_flow_info *f, void *d) |
70 | 45.9k | { |
71 | 45.9k | (void)w; |
72 | 45.9k | (void)f; |
73 | 45.9k | (void)d; |
74 | 45.9k | } |
75 | | |
76 | 174k | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
77 | 174k | pcap_t * pkts; |
78 | 174k | const u_char *pkt; |
79 | 174k | struct pcap_pkthdr *header; |
80 | 174k | int r; |
81 | 174k | char errbuf[PCAP_ERRBUF_SIZE]; |
82 | 174k | u_int i; |
83 | 174k | FILE *fd; |
84 | 174k | char name[256]; |
85 | | |
86 | 174k | if (prefs == NULL) { |
87 | 8 | prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1); /* No failure here */ |
88 | 8 | prefs->decode_tunnels = 1; |
89 | 8 | prefs->num_roots = 16; |
90 | 8 | prefs->max_ndpi_flows = 16 * 1024 * 1024; |
91 | 8 | prefs->quiet_mode = 0; |
92 | | |
93 | 8 | #ifdef ENABLE_MEM_ALLOC_FAILURES |
94 | 8 | fuzz_set_alloc_callbacks(); |
95 | 8 | #endif |
96 | | |
97 | 8 | g_ctx = ndpi_global_init(); |
98 | | |
99 | 8 | workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */, 0, ndpi_serialization_format_json, g_ctx); |
100 | | |
101 | 8 | ndpi_workflow_set_flow_callback(workflow, fn_flow_callback, NULL); |
102 | | |
103 | 8 | ndpi_set_config(workflow->ndpi_struct, NULL, "log.level", "3"); |
104 | 8 | ndpi_set_config(workflow->ndpi_struct, "all", "log", "1"); |
105 | | |
106 | 8 | sprintf(name, "%s/public_suffix_list.dat", path); |
107 | 8 | assert(ndpi_load_domain_suffixes(workflow->ndpi_struct, name) >= 0); |
108 | 8 | sprintf(name, "%s/lists/", path); |
109 | 8 | assert(ndpi_load_categories_dir(workflow->ndpi_struct, name) >= 0); |
110 | 8 | sprintf(name, "%s/lists/protocols/", path); |
111 | 8 | assert(ndpi_load_protocols_dir(workflow->ndpi_struct, name) >= 0); |
112 | 8 | sprintf(name, "%s/protos.txt", path); |
113 | 8 | assert(ndpi_load_protocols_file(workflow->ndpi_struct, name) >= 0); |
114 | 8 | sprintf(name, "%s/categories.txt", path); |
115 | 8 | assert(ndpi_load_categories_file(workflow->ndpi_struct, name, NULL) >= 0); |
116 | 8 | sprintf(name, "%s/risky_domains.txt", path); |
117 | 8 | assert(ndpi_load_risk_domain_file(workflow->ndpi_struct, name) >= 0); |
118 | 8 | sprintf(name, "%s/ja4_fingerprints.csv", path); |
119 | 8 | assert(ndpi_load_malicious_ja4_file(workflow->ndpi_struct, name) >= 0); |
120 | 8 | sprintf(name, "%s/tcp_fingerprints.csv", path); |
121 | 8 | assert(ndpi_load_tcp_fingerprint_file(workflow->ndpi_struct, name) >= 0); |
122 | 8 | sprintf(name, "%s/sha1_fingerprints.csv", path); |
123 | 8 | assert(ndpi_load_malicious_sha1_file(workflow->ndpi_struct, name) >= 0); |
124 | 8 | sprintf(name, "%s", path); |
125 | 8 | assert(ndpi_load_protocol_plugins(workflow->ndpi_struct, name) >= 0); /* Plugins are not really used while fuzzing, yet */ |
126 | | |
127 | | #ifdef ENABLE_ONLY_SUBCLASSIFICATION |
128 | | sprintf(name, "%s/config_only_classification.txt", path); |
129 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "filename.config", name) == NDPI_CFG_OK); |
130 | | #else |
131 | | |
132 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "packets_limit_per_flow", "255") == NDPI_CFG_OK); |
133 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "flow.track_payload", "1") == NDPI_CFG_OK); |
134 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "tcp_ack_payload_heuristic", "1") == NDPI_CFG_OK); |
135 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "fully_encrypted_heuristic", "1") == NDPI_CFG_OK); |
136 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "dns", "subclassification", "1") == NDPI_CFG_OK); |
137 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "application_blocks_tracking", "1") == NDPI_CFG_OK); |
138 | 8 | #ifndef ENABLE_CONFIG2 |
139 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "stun", "max_packets_extra_dissection", "40") == NDPI_CFG_OK); |
140 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "zoom", "max_packets_extra_dissection", "255") == NDPI_CFG_OK); |
141 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "rtp", "search_for_stun", "1") == NDPI_CFG_OK); |
142 | 8 | #endif |
143 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics", "0x01") == NDPI_CFG_OK); |
144 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics.num_messages", "20") == NDPI_CFG_OK); |
145 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "metadata.ja4r_fingerprint", "1") == NDPI_CFG_OK); |
146 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics", "0x07") == NDPI_CFG_OK); |
147 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics.max_packets_extra_dissection", "40") == NDPI_CFG_OK); |
148 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "all", "monitoring", "1") == NDPI_CFG_OK); |
149 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "dpi.address_cache_size", "8192") == NDPI_CFG_OK); |
150 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "hostname_dns_check", "1") == NDPI_CFG_OK); |
151 | | |
152 | | #ifdef ENABLE_CONFIG2 |
153 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "flow_risk.all.info", "0") == NDPI_CFG_OK); |
154 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.tcp_fingerprint_format", "1") == NDPI_CFG_OK); |
155 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.ndpi_fingerprint_format", "1") == NDPI_CFG_OK); |
156 | | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "blocks_analysis", "1") == NDPI_CFG_OK); |
157 | | |
158 | | addr_dump_path = "/tmp/"; |
159 | | #endif |
160 | | |
161 | 8 | #endif /* ENABLE_ONLY_SUBCLASSIFICATION */ |
162 | | |
163 | 8 | ndpi_finalize_initialization(workflow->ndpi_struct); |
164 | | |
165 | 8 | #ifdef ENABLE_FINGERPRINT_FP |
166 | 8 | fingerprint_fp = stdout; |
167 | 8 | #endif |
168 | | |
169 | 8 | #ifdef CRYPT_FORCE_NO_AESNI |
170 | 8 | force_no_aesni = 1; |
171 | 8 | #endif |
172 | | |
173 | | #ifdef ENABLE_PAYLOAD_ANALYZER |
174 | | enable_payload_analyzer = 1; |
175 | | #endif |
176 | 8 | } |
177 | | |
178 | 174k | #ifdef ENABLE_MEM_ALLOC_FAILURES |
179 | | /* Don't fail memory allocations until init phase is done */ |
180 | 174k | fuzz_set_alloc_callbacks_and_seed(Size); |
181 | 174k | #endif |
182 | | |
183 | 174k | fd = buffer_to_file(Data, Size); |
184 | 174k | if (fd == NULL) |
185 | 0 | return 0; |
186 | | |
187 | 174k | pkts = pcap_fopen_offline(fd, errbuf); |
188 | 174k | if (pkts == NULL) { |
189 | 15 | fclose(fd); |
190 | 15 | return 0; |
191 | 15 | } |
192 | 174k | if (ndpi_is_datalink_supported(pcap_datalink(pkts)) == 0) |
193 | 242 | { |
194 | | /* Do not fail if the datalink type is not supported (may happen often during fuzzing). */ |
195 | 242 | pcap_close(pkts); |
196 | 242 | return 0; |
197 | 242 | } |
198 | | |
199 | 173k | workflow->pcap_handle = pkts; |
200 | | /* Init flow tree */ |
201 | 173k | workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *)); |
202 | 173k | if(!workflow->ndpi_flows_root) { |
203 | 2 | pcap_close(pkts); |
204 | 2 | return 0; |
205 | 2 | } |
206 | | |
207 | 173k | header = NULL; |
208 | 173k | r = pcap_next_ex(pkts, &header, &pkt); |
209 | 9.86M | while (r > 0) { |
210 | | /* allocate an exact size buffer to check overflows */ |
211 | 9.69M | uint8_t *packet_checked = malloc(header->caplen); |
212 | | |
213 | 9.69M | if(packet_checked) { |
214 | 9.69M | ndpi_risk flow_risk; |
215 | 9.69M | struct ndpi_flow_info *flow = NULL; /* unused */ |
216 | | |
217 | 9.69M | memcpy(packet_checked, pkt, header->caplen); |
218 | 9.69M | ndpi_workflow_process_packet(workflow, header, packet_checked, &flow_risk, &flow); |
219 | 9.69M | free(packet_checked); |
220 | 9.69M | } |
221 | | |
222 | 9.69M | r = pcap_next_ex(pkts, &header, &pkt); |
223 | 9.69M | } |
224 | 173k | pcap_close(pkts); |
225 | | |
226 | | /* Free flow trees */ |
227 | 2.95M | for(i = 0; i < workflow->prefs.num_roots; i++) { |
228 | 2.78M | ndpi_twalk(workflow->ndpi_flows_root[i], node_cleanup_walker, NULL); |
229 | 2.78M | ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer); |
230 | 2.78M | } |
231 | 173k | ndpi_free(workflow->ndpi_flows_root); |
232 | | /* Free payload analyzer data */ |
233 | 173k | if(enable_payload_analyzer) |
234 | 16.7k | ndpi_report_payload_stats(stdout); |
235 | | |
236 | | #ifdef ENABLE_PAYLOAD_ANALYZER |
237 | | ndpi_update_params(SPLT_PARAM_TYPE, "splt_param.txt"); |
238 | | ndpi_update_params(BD_PARAM_TYPE, "bd_param.txt"); |
239 | | ndpi_update_params(2, ""); /* invalid */ |
240 | | #endif |
241 | | |
242 | 173k | return 0; |
243 | 173k | } |