From 1f0a3f1ad198ed78ecf5875e0da60c71ec61fcce Mon Sep 17 00:00:00 2001 From: Jordy Zomer Date: Thu, 10 Jun 2021 16:04:05 +0000 Subject: [PATCH] Add basic fuzzing support for libstrophe --- Makefile.am | 10 ++++++++++ configure.ac | 10 ++++++++++ tests/test_fuzz.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/test_fuzz.c diff --git a/Makefile.am b/Makefile.am index 2409cb3..ad62fd4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -171,6 +171,16 @@ endif check_PROGRAMS = $(TESTS) +if FUZZ +check_PROGRAMS += tests/test_fuzz + +tests_test_fuzz_SOURCES = tests/test_fuzz.c +tests_test_fuzz_CFLAGS = -fsanitize=fuzzer,address $(PARSER_CFLAGS) $(STROPHE_FLAGS) \ + -I$(top_srcdir)/src +tests_test_fuzz_LDADD = $(STROPHE_LIBS) +tests_test_fuzz_LDFLAGS = -static +endif + tests_check_parser_SOURCES = tests/check_parser.c tests/test.h tests_check_parser_CFLAGS = $(PARSER_CFLAGS) $(STROPHE_FLAGS) \ -I$(top_srcdir)/src diff --git a/configure.ac b/configure.ac index ef6a903..c9302ce 100644 --- a/configure.ac +++ b/configure.ac @@ -33,6 +33,16 @@ AC_ARG_ENABLE([tls], AC_ARG_ENABLE([cares], [AS_HELP_STRING([--enable-cares], [use c-ares for DNS resolution])]) +AC_ARG_ENABLE([fuzzing], [--enable-fuzzing Turn on fuzzing], + [case "${enableval}" in yes) fuzzing=true ;; no) fuzzing=false ;; *) AC_MSG_ERROR([bad value ${enableval} for --enable-fuzzing]) ;; esac],[fuzzing=false]) +AM_CONDITIONAL([FUZZ], [test x$fuzzing = xtrue]) + +if test "x$enable_fuzzing" = "xyes" ; then + if ! test "x$CC" = "xclang" ; then + AC_MSG_ERROR(["You need to set CC=clang to use --enable-fuzzing, used $CC"]) + fi +fi + AC_SEARCH_LIBS([socket], [network socket]) AC_CHECK_FUNCS([snprintf vsnprintf]) AC_CHECK_DECLS([va_copy], [], [], [#include ]) diff --git a/tests/test_fuzz.c b/tests/test_fuzz.c new file mode 100644 index 0000000..92bdf48 --- /dev/null +++ b/tests/test_fuzz.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "strophe.h" +#include "parser.h" + +void xmpp_initialize(void); + +void cbtest_handle_start(char *name, char **attrs, void *userdata) +{ + (void)name; + (void)attrs; + (void)userdata; +} + +void cbtest_handle_end(char *name, void *userdata) +{ + (void)name; + (void)userdata; +} + +void cbtest_handle_stanza(xmpp_stanza_t *stanza, void *userdata) +{ + (void)stanza; + (void)userdata; +} + +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) +{ + xmpp_ctx_t *ctx; + parser_t *parser; + + char *dup = malloc(Size); + memcpy(dup, Data, Size); + + ctx = xmpp_ctx_new(NULL, NULL); + parser = parser_new(ctx, cbtest_handle_start, cbtest_handle_end, + cbtest_handle_stanza, NULL); + + parser_feed(parser, dup, Size); + + free(dup); + parser_free(parser); + xmpp_ctx_free(ctx); + + return 0; +}