fuzz: add fuzzer testing nDPI (initial) configurations (#1830)

The goal of this fuzzer is to test init and deinit of the library, with
different configurations. In details:
* random memory allocation failures, even during init phase
* random `ndpi_init_prefs` parameter of `ndpi_init_detection_module()`
* random LRU caches sizes
* random bitmask of enabled protocols
* random parameters of `ndpi_set_detection_preferences()`
* random initialization of opportunistic TLS
* random load/don't load of configuration files

This new fuzzer is a C++ file, because it uses `FuzzedDataProvider`
class (see
https://github.com/google/fuzzing/blob/master/docs/split-inputs.md).
Note that the (existing) fuzzers need to be linked with C++ compiler
anyway, so this new fuzzer doesn't add any new requirements.
This commit is contained in:
Ivan Nardi
2022-12-23 19:07:13 +01:00
committed by GitHub
parent 3de76812d9
commit 560280e6f0
13 changed files with 310 additions and 60 deletions
+4 -28
View File
@@ -1,5 +1,6 @@
#include "reader_util.h"
#include "ndpi_api.h"
#include "fuzz_common_code.h"
#include <pcap/pcap.h>
@@ -23,27 +24,6 @@ int malloc_size_stats = 0;
int max_malloc_bins = 0;
struct ndpi_bin malloc_bins; /* unused */
#ifdef ENABLE_MEM_ALLOC_FAILURES
static int mem_alloc_state = 0;
__attribute__((no_sanitize("integer")))
static int fastrand ()
{
if(!mem_alloc_state) return 1; /* No failures */
mem_alloc_state = (214013 * mem_alloc_state + 2531011);
return (mem_alloc_state >> 16) & 0x7FFF;
}
static void *malloc_wrapper(size_t size) {
return (fastrand () % 16) ? malloc (size) : NULL;
}
static void free_wrapper(void *freeable) {
free(freeable);
}
#endif
FILE *bufferToFile(const uint8_t *Data, size_t Size) {
FILE *fd;
fd = tmpfile();
@@ -99,17 +79,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
memset(workflow->stats.protocol_flows, 0,
sizeof(workflow->stats.protocol_flows));
ndpi_finalize_initialization(workflow->ndpi_struct);
#ifdef ENABLE_MEM_ALLOC_FAILURES
set_ndpi_malloc(malloc_wrapper);
set_ndpi_free(free_wrapper);
/* Don't fail memory allocations until init phase is done */
#endif
}
#ifdef ENABLE_MEM_ALLOC_FAILURES
mem_alloc_state = Size;
/* Don't fail memory allocations until init phase is done */
set_mem_alloc_state(Size);
#endif
fd = bufferToFile(Data, Size);
if (fd == NULL)
return 0;