Skip to content

Commit fbb9700

Browse files
authored
fuzz: purge old sessions (ntop#1451)
At every fuzz iteration (i.e for every trace file): * keep the same ndpi context (`ndpi_init_detection_module` is very slow); * reset the flow table, otherwise it grows indefinitely. This change should fix the "out-of-memory" errors reported by oss-fuzz.
1 parent 6c1accd commit fbb9700

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

example/ndpiReader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
20622062
prefs.ignore_vlanid = ignore_vlanid;
20632063

20642064
memset(&ndpi_thread_info[thread_id], 0, sizeof(ndpi_thread_info[thread_id]));
2065-
ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle);
2065+
ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle, 1);
20662066

20672067
/* Preferences */
20682068
ndpi_workflow_set_flow_detected_callback(ndpi_thread_info[thread_id].workflow,

example/reader_util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ extern char *_debug_protocols;
391391
static int _debug_protocols_ok = 0;
392392

393393
struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs,
394-
pcap_t * pcap_handle) {
394+
pcap_t * pcap_handle, int do_init_flows_root) {
395395
struct ndpi_detection_module_struct * module;
396396
struct ndpi_workflow * workflow;
397397

@@ -427,7 +427,8 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref
427427
if(_debug_protocols_ok)
428428
ndpi_set_debug_bitmask(module, debug_bitmask);
429429

430-
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
430+
if(do_init_flows_root)
431+
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
431432

432433
return workflow;
433434
}

example/reader_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ typedef struct ndpi_workflow {
306306

307307

308308
/* TODO: remove wrappers parameters and use ndpi global, when their initialization will be fixed... */
309-
struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle);
309+
struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle, int do_init_flows_root);
310310

311311

312312
/* workflow main free function */

fuzz/fuzz_ndpi_reader.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
4747
char errbuf[PCAP_ERRBUF_SIZE];
4848
NDPI_PROTOCOL_BITMASK all;
4949
char * pcap_path = tempnam("/tmp", "fuzz-ndpi-reader");
50+
u_int i;
5051

5152
if (prefs == NULL) {
5253
prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1);
@@ -59,7 +60,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
5960
prefs->max_ndpi_flows = 1024 * 1024;
6061
prefs->quiet_mode = 0;
6162

62-
workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */);
63+
workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */, 0);
6364
// enable all protocols
6465
NDPI_BITMASK_SET_ALL(all);
6566
ndpi_set_protocol_detection_bitmask2(workflow->ndpi_struct, &all);
@@ -90,6 +91,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
9091
}
9192

9293
workflow->pcap_handle = pkts;
94+
/* Init flow tree */
95+
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
9396

9497
header = NULL;
9598
r = pcap_next_ex(pkts, &header, &pkt);
@@ -109,6 +112,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
109112
}
110113
pcap_close(pkts);
111114

115+
/* Free flow trees */
116+
for(i = 0; i < workflow->prefs.num_roots; i++)
117+
ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer);
118+
ndpi_free(workflow->ndpi_flows_root);
119+
112120
remove(pcap_path);
113121
free(pcap_path);
114122

0 commit comments

Comments
 (0)