diff options
-rw-r--r-- | Makefile.am | 4 | ||||
-rw-r--r-- | configure.seed | 33 | ||||
-rw-r--r-- | fuzz/Makefile.am | 13 | ||||
-rw-r--r-- | fuzz/fuzz_process_packet.c | 28 |
4 files changed, 77 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am index 4090817b3..e4d8c58c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,10 @@ ACLOCAL_AMFLAGS = -I m4 SUBDIRS = src/lib example tests +if BUILD_FUZZTARGETS +SUBDIRS += fuzz +endif + pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libndpi.pc diff --git a/configure.seed b/configure.seed index 15c922f08..d67978b10 100644 --- a/configure.seed +++ b/configure.seed @@ -145,9 +145,40 @@ AC_ARG_ENABLE([debug-messages], AS_HELP_STRING([--enable-debug-messages], [Define NDPI_ENABLE_DEBUG_MESSAGES=1]), [ AC_DEFINE(NDPI_ENABLE_DEBUG_MESSAGES, 1, [Enable ndpi_debug_messages]) ]) +AC_ARG_ENABLE(fuzztargets, + AS_HELP_STRING([--enable-fuzztargets], [Enable fuzz targets]),[enable_fuzztargets=$enableval],[enable_fuzztargets=no]) +AM_CONDITIONAL([BUILD_FUZZTARGETS], [test "x$enable_fuzztargets" = "xyes"]) +AS_IF([test "x$enable_fuzztargets" = "xyes"], [ + AC_PROG_CXX + AC_LANG_PUSH(C++) + tmp_saved_flags=$[]_AC_LANG_PREFIX[]FLAGS + AS_IF([test "x$LIB_FUZZING_ENGINE" = "x"], [ + LIB_FUZZING_ENGINE=-fsanitize=fuzzer + AC_SUBST(LIB_FUZZING_ENGINE) + ]) + _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $LIB_FUZZING_ENGINE" + AC_MSG_CHECKING([whether $CXX accepts $LIB_FUZZING_ENGINE]) + AC_LINK_IFELSE([AC_LANG_SOURCE([[ +#include <sys/types.h> +extern "C" int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size); +extern "C" int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size) { +(void)Data; +(void)Size; +return 0; +} + ]])], + [ AC_MSG_RESULT(yes) + has_sanitizefuzzer=yes], + [ AC_MSG_RESULT(no) ] + ) + _AC_LANG_PREFIX[]FLAGS=$tmp_saved_flags + AC_LANG_POP() +]) +AM_CONDITIONAL([HAS_FUZZLDFLAGS], [test "x$has_sanitizefuzzer" = "xyes"]) + AC_CHECK_LIB(pthread, pthread_setaffinity_np, AC_DEFINE_UNQUOTED(HAVE_PTHREAD_SETAFFINITY_NP, 1, [libc has pthread_setaffinity_np])) -AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile]) +AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile fuzz/Makefile]) AC_CONFIG_HEADERS(src/include/ndpi_config.h) AC_SUBST(GIT_RELEASE) AC_SUBST(NDPI_MAJOR) diff --git a/fuzz/Makefile.am b/fuzz/Makefile.am new file mode 100644 index 000000000..4693d4076 --- /dev/null +++ b/fuzz/Makefile.am @@ -0,0 +1,13 @@ +bin_PROGRAMS = fuzz_process_packet + +fuzz_process_packet_SOURCES = fuzz_process_packet.c +fuzz_process_packet_LDFLAGS = ../src/lib/libndpi.a +if HAS_FUZZLDFLAGS + fuzz_process_packet_LDFLAGS += $(LIB_FUZZING_ENGINE) +else + fuzz_process_packet_SOURCES += onefile.c +endif +# force usage of CXX for linker +fuzz_process_packet_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXX) $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(fuzz_process_packet_LDFLAGS) $(LDFLAGS) -o $@ diff --git a/fuzz/fuzz_process_packet.c b/fuzz/fuzz_process_packet.c new file mode 100644 index 000000000..88319b419 --- /dev/null +++ b/fuzz/fuzz_process_packet.c @@ -0,0 +1,28 @@ +#include "ndpi_api.h" + +#include <stdint.h> +#include <stdio.h> + +struct ndpi_detection_module_struct *ndpi_info_mod = NULL; +struct ndpi_flow_struct *ndpi_flow; +struct ndpi_id_struct *src; +struct ndpi_id_struct *dst; + +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + if (ndpi_info_mod == NULL) { + ndpi_info_mod = ndpi_init_detection_module(); + NDPI_PROTOCOL_BITMASK all; + NDPI_BITMASK_SET_ALL(all); + ndpi_set_protocol_detection_bitmask2(ndpi_info_mod, &all); + ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT); + src = ndpi_malloc(SIZEOF_ID_STRUCT); + dst = ndpi_malloc(SIZEOF_ID_STRUCT); + } + + memset(ndpi_flow, 0, SIZEOF_FLOW_STRUCT); + memset(src, 0, SIZEOF_ID_STRUCT); + memset(dst, 0, SIZEOF_ID_STRUCT); + ndpi_detection_process_packet(ndpi_info_mod, ndpi_flow, Data, Size, 0, src, dst); + + return 0; +} |