Unverified Commit 94363265 by Christian Brauner Committed by GitHub

Merge pull request #3802 from evverx/build-system-fuzzers

oss-fuzz: switch to --enable-fuzzers
parents 8f7b7b8d a10327e7
...@@ -20,8 +20,13 @@ apt-get install --yes --no-install-recommends \ ...@@ -20,8 +20,13 @@ apt-get install --yes --no-install-recommends \
python3-setuptools rsync squashfs-tools uidmap unzip uuid-runtime \ python3-setuptools rsync squashfs-tools uidmap unzip uuid-runtime \
wget xz-utils wget xz-utils
ARGS="--enable-sanitizers --enable-tests --prefix=/usr/ --sysconfdir=/etc/ --localstatedir=/var/ --disable-no-undefined"
case "$CC" in clang*)
ARGS="$ARGS --enable-fuzzers"
esac
./autogen.sh ./autogen.sh
CFLAGS="-Wall -Werror" ./configure --enable-sanitizers --enable-tests --prefix=/usr/ --sysconfdir=/etc/ --localstatedir=/var/ --disable-no-undefined CFLAGS="-Wall -Werror" ./configure $ARGS
make make
make install make install
......
...@@ -17,4 +17,4 @@ jobs: ...@@ -17,4 +17,4 @@ jobs:
- name: Build - name: Build
run: | run: |
sudo CC=${{ matrix.compiler }} .github/workflows/sanitizers.sh sudo CC=${{ matrix.compiler }} CXX=${{ matrix.compiler }}++ .github/workflows/sanitizers.sh
...@@ -62,6 +62,7 @@ if test "x$valid_compiler" = "xno"; then ...@@ -62,6 +62,7 @@ if test "x$valid_compiler" = "xno"; then
fi fi
AC_PROG_GCC_TRADITIONAL AC_PROG_GCC_TRADITIONAL
AC_PROG_CXX
AC_ENABLE_SHARED AC_ENABLE_SHARED
AC_ENABLE_STATIC AC_ENABLE_STATIC
# Check binaries # Check binaries
...@@ -205,6 +206,13 @@ AC_ARG_ENABLE([no_undefined], ...@@ -205,6 +206,13 @@ AC_ARG_ENABLE([no_undefined],
[enable_no_undefined=$enableval], [enable_no_undefined=yes]) [enable_no_undefined=$enableval], [enable_no_undefined=yes])
AM_CONDITIONAL([ENABLE_NO_UNDEFINED], [test "x$enable_no_undefined" = "xyes"]) AM_CONDITIONAL([ENABLE_NO_UNDEFINED], [test "x$enable_no_undefined" = "xyes"])
AC_ARG_ENABLE([fuzzers],
[AS_HELP_STRING([--enable-fuzzers], [compile with fuzzers])],
[enable_fuzzers=$enableval], [enable_fuzzers=no])
AM_CONDITIONAL([ENABLE_FUZZERS], [test "x$enable_fuzzers" = "xyes"])
AM_CONDITIONAL([OSS_FUZZ], [test "x$LIB_FUZZING_ENGINE" != x])
# Allow disabling rpath # Allow disabling rpath
AC_ARG_ENABLE([rpath], AC_ARG_ENABLE([rpath],
[AS_HELP_STRING([--enable-rpath], [set rpath in executables [default=no]])], [AS_HELP_STRING([--enable-rpath], [set rpath in executables [default=no]])],
...@@ -476,6 +484,17 @@ else ...@@ -476,6 +484,17 @@ else
AC_MSG_RESULT([no]) AC_MSG_RESULT([no])
fi fi
if test "x$enable_fuzzers" = "xyes"; then
if test "x$LIB_FUZZING_ENGINE" = x; then
CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \
-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
-fsanitize=fuzzer-no-link])
fi
else
CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[-flto=thin])
fi
AC_SUBST(AM_CFLAGS)
# Optional test binaries # Optional test binaries
AC_ARG_ENABLE([tests], AC_ARG_ENABLE([tests],
[AS_HELP_STRING([--enable-tests], [build test/example binaries [default=no]])], [AS_HELP_STRING([--enable-tests], [build test/example binaries [default=no]])],
...@@ -800,7 +819,6 @@ CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \ ...@@ -800,7 +819,6 @@ CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \
-Warray-bounds \ -Warray-bounds \
-Wrestrict \ -Wrestrict \
-Wreturn-local-addr \ -Wreturn-local-addr \
-flto=thin \
-fsanitize=cfi \ -fsanitize=cfi \
-Wstringop-overflow]) -Wstringop-overflow])
AC_SUBST(AM_CFLAGS) AC_SUBST(AM_CFLAGS)
...@@ -1124,6 +1142,7 @@ Debugging: ...@@ -1124,6 +1142,7 @@ Debugging:
- Coverity: $enable_coverity_build - Coverity: $enable_coverity_build
- mutex debugging: $enable_mutex_debugging - mutex debugging: $enable_mutex_debugging
- tests: $enable_tests - tests: $enable_tests
- fuzzers: $enable_fuzzers
Paths: Paths:
- Logs in configpath: $enable_configpath_log - Logs in configpath: $enable_configpath_log
......
...@@ -1932,6 +1932,9 @@ init_lxc_static_CFLAGS = $(AM_CFLAGS) -DNO_LXC_CONF ...@@ -1932,6 +1932,9 @@ init_lxc_static_CFLAGS = $(AM_CFLAGS) -DNO_LXC_CONF
if ENABLE_SANITIZERS if ENABLE_SANITIZERS
init_lxc_static_CFLAGS += -fno-sanitize=address,undefined init_lxc_static_CFLAGS += -fno-sanitize=address,undefined
endif endif
if ENABLE_FUZZERS
init_lxc_static_CFLAGS += -fno-sanitize=fuzzer-no-link
endif
endif endif
endif endif
......
...@@ -508,7 +508,10 @@ static int build_dir(const char *name) ...@@ -508,7 +508,10 @@ static int build_dir(const char *name)
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
ret = lxc_unpriv(mkdir(n, 0755)); ret = lxc_unpriv(mkdir(n, 0755));
#else #else
ret = errno = EEXIST; if (is_in_comm("fuzz-lxc-") > 0)
ret = errno = EEXIST;
else
ret = lxc_unpriv(mkdir(n, 0755));
#endif /*!FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ #endif /*!FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
*p = '/'; *p = '/';
if (ret && errno != EEXIST) if (ret && errno != EEXIST)
...@@ -521,10 +524,14 @@ static int build_dir(const char *name) ...@@ -521,10 +524,14 @@ static int build_dir(const char *name)
static int log_open(const char *name) static int log_open(const char *name)
{ {
int newfd = -EBADF; int newfd = -EBADF;
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
__do_close int fd = -EBADF; __do_close int fd = -EBADF;
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660)); fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660));
#else
if (is_in_comm("fuzz-lxc-") <= 0)
fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660));
#endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
if (fd < 0) if (fd < 0)
return log_error_errno(-errno, errno, "Failed to open log file \"%s\"", name); return log_error_errno(-errno, errno, "Failed to open log file \"%s\"", name);
...@@ -534,7 +541,6 @@ static int log_open(const char *name) ...@@ -534,7 +541,6 @@ static int log_open(const char *name)
newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO); newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO);
if (newfd < 0) if (newfd < 0)
return log_error_errno(-errno, errno, "Failed to dup log fd %d", fd); return log_error_errno(-errno, errno, "Failed to dup log fd %d", fd);
#endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
return newfd; return newfd;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/vfs.h> #include <sys/vfs.h>
...@@ -271,4 +272,28 @@ static inline __u32 copy_struct_to_client(__u32 client_size, void *dst, ...@@ -271,4 +272,28 @@ static inline __u32 copy_struct_to_client(__u32 client_size, void *dst,
return size; return size;
} }
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
static inline int is_in_comm(const char *s)
{
__do_free char *buf = NULL;
__do_free char *comm = NULL;
size_t buf_size;
buf = file_to_buf("/proc/self/comm", &buf_size);
if (!buf)
return -1;
if (buf_size == 0)
return -1;
comm = malloc(buf_size + 1);
if (!comm)
return -1;
memcpy(comm, buf, buf_size);
comm[buf_size] = '\0';
return strstr(comm, s) != NULL;
}
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
#endif /* __LXC_UTILS_H */ #endif /* __LXC_UTILS_H */
...@@ -791,6 +791,29 @@ bin_SCRIPTS += lxc-test-lxc-attach \ ...@@ -791,6 +791,29 @@ bin_SCRIPTS += lxc-test-lxc-attach \
endif endif
endif endif
if ENABLE_FUZZERS
LIB_FUZZING_ENGINE ?= -fsanitize=fuzzer
# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements
nodist_EXTRA_fuzz_lxc_config_read_SOURCES = dummy.cxx
fuzz_lxc_config_read_SOURCES = fuzz-lxc-config-read.c
fuzz_lxc_config_read_CFLAGS = $(AM_CFLAGS)
fuzz_lxc_config_read_CXXFLAGS = $(AM_CFLAGS)
fuzz_lxc_config_read_LDFLAGS = $(AM_LDFLAGS) -static
fuzz_lxc_config_read_LDADD = $(LDADD) $(LIB_FUZZING_ENGINE)
nodist_EXTRA_fuzz_lxc_define_load_SOURCES = dummy.cxx
fuzz_lxc_define_load_SOURCES = fuzz-lxc-define-load.c
fuzz_lxc_define_load_CFLAGS = $(AM_CFLAGS)
fuzz_lxc_define_load_CXXFLAGS = $(AM_CFLAGS)
fuzz_lxc_define_load_LDFLAGS = $(AM_LDFLAGS) -static
fuzz_lxc_define_load_LDADD = $(LDADD) $(LIB_FUZZING_ENGINE)
bin_PROGRAMS += fuzz-lxc-config-read \
fuzz-lxc-define-load
bin_SCRIPTS += lxc-test-fuzzers
endif
endif endif
EXTRA_DIST = basic.c \ EXTRA_DIST = basic.c \
......
#!/bin/bash
set -eux
set -o pipefail
TMP_DIR=`mktemp -d`
export ASAN_OPTIONS=${ASAN_OPTIONS:-detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:strict_string_checks=1}
export UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup exit
for fuzzer in /usr/bin/fuzz-lxc-*; do
name=$(basename "$fuzzer")
corpus_dir="$TMP_DIR/$name"
mkdir -p "$corpus_dir"
if wget --directory-prefix="$TMP_DIR" https://storage.googleapis.com/lxc-backup.clusterfuzz-external.appspot.com/corpus/libFuzzer/lxc_$name/public.zip; then
unzip -q -d "$corpus_dir" "$TMP_DIR/public.zip"
fi
"$fuzzer" -max_total_time=120 "$corpus_dir"
done
...@@ -594,6 +594,15 @@ void test_task_blocks_signal(void) ...@@ -594,6 +594,15 @@ void test_task_blocks_signal(void)
return; return;
} }
void test_is_in_comm(void)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
lxc_test_assert_abort(is_in_comm("fuzz-lxc-") == 0);
lxc_test_assert_abort(is_in_comm("lxc-test") == 1);
lxc_test_assert_abort(is_in_comm("") == 1);
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
test_lxc_string_replace(); test_lxc_string_replace();
...@@ -606,6 +615,7 @@ int main(int argc, char *argv[]) ...@@ -606,6 +615,7 @@ int main(int argc, char *argv[])
test_parse_byte_size_string(); test_parse_byte_size_string();
test_lxc_config_net_is_hwaddr(); test_lxc_config_net_is_hwaddr();
test_task_blocks_signal(); test_task_blocks_signal();
test_is_in_comm();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
...@@ -24,9 +24,6 @@ mkdir -p $OUT ...@@ -24,9 +24,6 @@ mkdir -p $OUT
export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer} export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer}
# AFL++ and hoggfuzz are both incompatible with lto=thin apparently
sed -i '/-flto=thin/d' configure.ac
# turn off the libutil dependency # turn off the libutil dependency
sed -i 's/^AC_CHECK_LIB(util/#/' configure.ac sed -i 's/^AC_CHECK_LIB(util/#/' configure.ac
...@@ -39,14 +36,15 @@ sed -i 's/^AC_CHECK_LIB(util/#/' configure.ac ...@@ -39,14 +36,15 @@ sed -i 's/^AC_CHECK_LIB(util/#/' configure.ac
--disable-selinux \ --disable-selinux \
--disable-seccomp \ --disable-seccomp \
--disable-capabilities \ --disable-capabilities \
--disable-no-undefined --disable-no-undefined \
--enable-tests \
--enable-fuzzers
make -j$(nproc) make -j$(nproc)
for fuzz_target_source in src/tests/fuzz-lxc*.c; do for fuzz_target_source in src/tests/fuzz-lxc*.c; do
fuzz_target_name=$(basename "$fuzz_target_source" ".c") fuzz_target_name=$(basename "$fuzz_target_source" ".c")
$CC -c -o "$fuzz_target_name.o" $CFLAGS -Isrc -Isrc/lxc "$fuzz_target_source" cp "src/tests/$fuzz_target_name" "$OUT"
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE "$fuzz_target_name.o" src/lxc/.libs/liblxc.a -o "$OUT/$fuzz_target_name"
done done
perl -lne 'if (/config_jump_table\[\]\s*=/../^}/) { /"([^"]+)"/ && print "$1=" }' src/lxc/confile.c >doc/examples/keys.conf perl -lne 'if (/config_jump_table\[\]\s*=/../^}/) { /"([^"]+)"/ && print "$1=" }' src/lxc/confile.c >doc/examples/keys.conf
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment