Coverage Report

Created: 2025-11-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/fuzz/fuzz_ndpi_reader_payload_analyzer.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
304k
static void node_cleanup_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
54
304k
  struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node;
55
56
304k
  (void)depth;
57
304k
  (void)user_data;
58
59
304k
  if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */
60
147k
    if((!flow->detection_completed) && flow->ndpi_flow) {
61
110k
      flow->detected_protocol = ndpi_detection_giveup(workflow->ndpi_struct,
62
110k
                                                      flow->ndpi_flow);
63
110k
    }
64
65
147k
    process_ndpi_collected_info(workflow, flow);
66
147k
  }
67
304k
}
68
69
static void fn_flow_callback(struct ndpi_workflow *w, struct ndpi_flow_info *f, void *d)
70
36.8k
{
71
36.8k
  (void)w;
72
36.8k
  (void)f;
73
36.8k
  (void)d;
74
36.8k
}
75
76
191k
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
77
191k
  pcap_t * pkts;
78
191k
  const u_char *pkt;
79
191k
  struct pcap_pkthdr *header;
80
191k
  int r;
81
191k
  char errbuf[PCAP_ERRBUF_SIZE];
82
191k
  u_int i;
83
191k
  FILE *fd;
84
191k
  char name[256];
85
86
191k
  if (prefs == NULL) {
87
9
    prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1); /* No failure here */
88
9
    prefs->decode_tunnels = 1;
89
9
    prefs->num_roots = 16;
90
9
    prefs->max_ndpi_flows = 16 * 1024 * 1024;
91
9
    prefs->quiet_mode = 0;
92
93
9
#ifdef ENABLE_MEM_ALLOC_FAILURES
94
9
    fuzz_set_alloc_callbacks();
95
9
#endif
96
97
9
    g_ctx = ndpi_global_init();
98
99
9
    workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */, 0, ndpi_serialization_format_json, g_ctx);
100
101
9
    ndpi_workflow_set_flow_callback(workflow, fn_flow_callback, NULL);
102
103
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "log.level", "3");
104
9
    ndpi_set_config(workflow->ndpi_struct, "all", "log", "1");
105
106
9
    sprintf(name, "%s/public_suffix_list.dat", path);
107
9
    assert(ndpi_load_domain_suffixes(workflow->ndpi_struct, name) >= 0);
108
9
    sprintf(name, "%s/lists/", path);
109
9
    assert(ndpi_load_categories_dir(workflow->ndpi_struct, name) >= 0);
110
9
    sprintf(name, "%s/lists/protocols/", path);
111
9
    assert(ndpi_load_protocols_dir(workflow->ndpi_struct, name) >= 0);
112
9
    sprintf(name, "%s/protos.txt", path);
113
9
    assert(ndpi_load_protocols_file(workflow->ndpi_struct, name) >= 0);
114
9
    sprintf(name, "%s/categories.txt", path);
115
9
    assert(ndpi_load_categories_file(workflow->ndpi_struct, name, NULL) >= 0);
116
9
    sprintf(name, "%s/risky_domains.txt", path);
117
9
    assert(ndpi_load_risk_domain_file(workflow->ndpi_struct, name) >= 0);
118
9
    sprintf(name, "%s/ja4_fingerprints.csv", path);
119
9
    assert(ndpi_load_malicious_ja4_file(workflow->ndpi_struct, name) >= 0);
120
9
    sprintf(name, "%s/tcp_fingerprints.csv", path);
121
9
    assert(ndpi_load_tcp_fingerprint_file(workflow->ndpi_struct, name) >= 0);
122
9
    sprintf(name, "%s/sha1_fingerprints.csv", path);
123
9
    assert(ndpi_load_malicious_sha1_file(workflow->ndpi_struct, name) >= 0);
124
125
#ifdef ENABLE_ONLY_SUBCLASSIFICATION
126
    sprintf(name, "%s/config_only_classification.txt", path);
127
    assert(ndpi_set_config(workflow->ndpi_struct, NULL, "filename.config", name) == 0);
128
#else
129
130
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "packets_limit_per_flow", "255");
131
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "flow.track_payload", "1");
132
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "tcp_ack_payload_heuristic", "1");
133
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "fully_encrypted_heuristic", "1");
134
9
    ndpi_set_config(workflow->ndpi_struct, "dns", "subclassification", "1");
135
9
    ndpi_set_config(workflow->ndpi_struct, "tls", "application_blocks_tracking", "1");
136
9
#ifndef ENABLE_CONFIG2
137
9
    ndpi_set_config(workflow->ndpi_struct, "stun", "max_packets_extra_dissection", "40");
138
9
    ndpi_set_config(workflow->ndpi_struct, "zoom", "max_packets_extra_dissection", "255");
139
9
    ndpi_set_config(workflow->ndpi_struct, "rtp", "search_for_stun", "1");
140
9
#endif
141
9
    ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics", "0x01");
142
9
    ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics.num_messages", "20");
143
9
    ndpi_set_config(workflow->ndpi_struct, "tls", "metadata.ja4r_fingerprint", "1");
144
9
    ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics", "0x07");
145
9
    ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics.max_packets_extra_dissection", "40");
146
9
    ndpi_set_config(workflow->ndpi_struct, "stun", "monitoring", "1");
147
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "dpi.address_cache_size", "8192");
148
9
    ndpi_set_config(workflow->ndpi_struct, NULL, "hostname_dns_check", "1");
149
150
#ifdef ENABLE_CONFIG2
151
    ndpi_set_config(workflow->ndpi_struct, NULL, "flow_risk.all.info", "0");
152
    ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.tcp_fingerprint_format", "1");
153
    ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.ndpi_fingerprint_format", "1");
154
    ndpi_set_config(workflow->ndpi_struct, "tls", "blocks_analysis", "1");
155
156
    addr_dump_path = "/tmp/";
157
#endif
158
159
9
#endif /* ENABLE_ONLY_SUBCLASSIFICATION */
160
161
9
    ndpi_finalize_initialization(workflow->ndpi_struct);
162
163
#ifdef ENABLE_FINGERPRINT_FP
164
    fingerprint_fp = stdout;
165
#endif
166
167
#ifdef CRYPT_FORCE_NO_AESNI
168
    force_no_aesni = 1;
169
#endif
170
171
9
#ifdef ENABLE_PAYLOAD_ANALYZER
172
9
   enable_payload_analyzer = 1;
173
9
#endif
174
9
  }
175
176
191k
#ifdef ENABLE_MEM_ALLOC_FAILURES
177
  /* Don't fail memory allocations until init phase is done */
178
191k
  fuzz_set_alloc_callbacks_and_seed(Size);
179
191k
#endif
180
181
191k
  fd = buffer_to_file(Data, Size);
182
191k
  if (fd == NULL)
183
0
    return 0;
184
185
191k
  pkts = pcap_fopen_offline(fd, errbuf);
186
191k
  if (pkts == NULL) {
187
17
    fclose(fd);
188
17
    return 0;
189
17
  }
190
191k
  if (ndpi_is_datalink_supported(pcap_datalink(pkts)) == 0)
191
279
  {
192
    /* Do not fail if the datalink type is not supported (may happen often during fuzzing). */
193
279
    pcap_close(pkts);
194
279
    return 0;
195
279
  }
196
197
190k
  workflow->pcap_handle = pkts;
198
  /* Init flow tree */
199
190k
  workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
200
190k
  if(!workflow->ndpi_flows_root) {
201
2
    pcap_close(pkts);
202
2
    return 0;
203
2
  }
204
205
190k
  header = NULL;
206
190k
  r = pcap_next_ex(pkts, &header, &pkt);
207
12.4M
  while (r > 0) {
208
    /* allocate an exact size buffer to check overflows */
209
12.2M
    uint8_t *packet_checked = malloc(header->caplen);
210
211
12.2M
    if(packet_checked) {
212
12.2M
      ndpi_risk flow_risk;
213
12.2M
      struct ndpi_flow_info *flow = NULL; /* unused */
214
215
12.2M
      memcpy(packet_checked, pkt, header->caplen);
216
12.2M
      ndpi_workflow_process_packet(workflow, header, packet_checked, &flow_risk, &flow);
217
12.2M
      free(packet_checked);
218
12.2M
    }
219
220
12.2M
    r = pcap_next_ex(pkts, &header, &pkt);
221
12.2M
  }
222
190k
  pcap_close(pkts);
223
224
  /* Free flow trees */
225
3.24M
  for(i = 0; i < workflow->prefs.num_roots; i++) {
226
3.05M
    ndpi_twalk(workflow->ndpi_flows_root[i], node_cleanup_walker, NULL);
227
3.05M
    ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer);
228
3.05M
  }
229
190k
  ndpi_free(workflow->ndpi_flows_root);
230
  /* Free payload analyzer data */
231
190k
  if(enable_payload_analyzer)
232
16.8k
    ndpi_report_payload_stats(stdout);
233
234
190k
#ifdef ENABLE_PAYLOAD_ANALYZER
235
190k
  ndpi_update_params(SPLT_PARAM_TYPE, "splt_param.txt");
236
190k
  ndpi_update_params(BD_PARAM_TYPE, "bd_param.txt");
237
190k
  ndpi_update_params(2, ""); /* invalid */
238
190k
#endif
239
240
190k
  return 0;
241
190k
}