diff --git a/test/fuzzing/Makefile.am b/test/fuzzing/Makefile.am index 6c4737fa4090c21bc7545de120b47ef783041897..f127c1c7d7870d7cb8be3d2e1fe102732afe231b 100644 --- a/test/fuzzing/Makefile.am +++ b/test/fuzzing/Makefile.am @@ -7,5 +7,5 @@ check_PROGRAMS = lib_LTLIBRARIES = libfuzz.la -libfuzz_la_SOURCES = lib/utils.cpp lib/supervisor.cpp lib/gnutls.cpp +libfuzz_la_SOURCES = lib/utils.cpp lib/supervisor.cpp lib/gnutls.cpp lib/rand.cpp lib/syslog.cpp endif diff --git a/test/fuzzing/lib/rand.cpp b/test/fuzzing/lib/rand.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f62f465a8c7824fa53a14a78e7bac8897b5cbe1b --- /dev/null +++ b/test/fuzzing/lib/rand.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2021 Savoir-faire Linux Inc. + * + * Author: Olivier Dion <olivier.dion>@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdlib.h> + +/* + * No random! + */ +void +srand(unsigned int seed) +{ + (void) seed; +} diff --git a/test/fuzzing/lib/syslog.cpp b/test/fuzzing/lib/syslog.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d68d58a09156c7d66000521f26be103bc199d89 --- /dev/null +++ b/test/fuzzing/lib/syslog.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021 Savoir-faire Linux Inc. + * + * Author: Olivier Dion <olivier.dion>@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <syslog.h> +#include <stdio.h> +#include <cstdlib> + +#include "lib/supervisor.h" +#include "lib/syslog.h" + +/* Jami */ +#include "logger.h" + +__weak +bool +syslog_handler(int priority, const char *fmt, va_list ap) +{ + (void)priority; + (void)fmt; + (void)ap; + + fflush(NULL); + fprintf(stderr, "libfuzz: stop by supervisor log\n"); + + return false; +} + +BEGIN_WRAPPER(void, vsyslog, int priority, const char* format, va_list ap) +{ + static int priority_threshold = -2; + + if (-2 == priority_threshold) { + + const char* str_to_int[] = { + [LOG_EMERG] = "EMERG", + [LOG_ALERT] = "ALERT", + [LOG_CRIT] = "CRIT", + [LOG_ERR] = "ERR", + [LOG_WARNING] = "WARNING", + [LOG_NOTICE] = "NOTICE", + [LOG_INFO] = "INFO", + [LOG_DEBUG] = "DEBUG" + }; + + const char* supervisor_says = std::getenv(supervisor::env::log); + + if (nullptr == supervisor_says) { + priority_threshold = 0; + goto no_threshold; + } + + for (size_t i = 0; i < array_size(str_to_int); ++i) { + if (streq(str_to_int[i], supervisor_says)) { + priority_threshold = i; + break; + } + } + + if (priority_threshold < 0) { + fprintf(stderr, "libfuzz: Invalid value of SUPERVISOR_LOG `%s`\n", supervisor_says); + priority_threshold = -1; + } + } + +no_threshold: + this_func(priority, format, ap); + + if (priority <= priority_threshold) { + if (not syslog_handler(priority, format, ap)) { + exit(supervisor::signal::exit::log); + } + } +} +END_WRAPPER(); diff --git a/test/fuzzing/lib/syslog.h b/test/fuzzing/lib/syslog.h new file mode 100644 index 0000000000000000000000000000000000000000..52a1ca28a02ff14ba86c2b81ff241141850983f5 --- /dev/null +++ b/test/fuzzing/lib/syslog.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2021 Savoir-faire Linux Inc. + * + * Author: Olivier Dion <olivier.dion>@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include <stdarg.h> + +extern "C" { + extern bool syslog_handler(int priority, const char *fmt, va_list ap); +};