From: Evgeny Vereshchagin Date: Wed, 14 Apr 2021 18:37:08 +0000 (+0000) Subject: log: create log files in "fuzzing" mode if it's called outside fuzz targets X-Git-Tag: lxc-5.0.0~189^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a6af918557b40b7d03880dcf14a221e965f2b67;p=thirdparty%2Flxc.git log: create log files in "fuzzing" mode if it's called outside fuzz targets to make it possible to run the fuzzers along with the other tests Signed-off-by: Evgeny Vereshchagin --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 7ead4e270..da6806f87 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -1932,6 +1932,9 @@ init_lxc_static_CFLAGS = $(AM_CFLAGS) -DNO_LXC_CONF if ENABLE_SANITIZERS init_lxc_static_CFLAGS += -fno-sanitize=address,undefined endif +if ENABLE_FUZZERS +init_lxc_static_CFLAGS += -fno-sanitize=fuzzer-no-link +endif endif endif diff --git a/src/lxc/log.c b/src/lxc/log.c index b4877bca8..844c1cefb 100644 --- a/src/lxc/log.c +++ b/src/lxc/log.c @@ -508,7 +508,10 @@ static int build_dir(const char *name) #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION ret = lxc_unpriv(mkdir(n, 0755)); #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 */ *p = '/'; if (ret && errno != EEXIST) @@ -521,10 +524,14 @@ static int build_dir(const char *name) static int log_open(const char *name) { int newfd = -EBADF; -#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION __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)); +#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) return log_error_errno(-errno, errno, "Failed to open log file \"%s\"", name); @@ -534,7 +541,6 @@ static int log_open(const char *name) newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO); if (newfd < 0) return log_error_errno(-errno, errno, "Failed to dup log fd %d", fd); -#endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ return newfd; } diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 49c0f3859..272e2dbef 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -271,4 +272,28 @@ static inline __u32 copy_struct_to_client(__u32 client_size, void *dst, 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 */ diff --git a/src/tests/lxc-test-utils.c b/src/tests/lxc-test-utils.c index 3a26aecf3..96d23f35c 100644 --- a/src/tests/lxc-test-utils.c +++ b/src/tests/lxc-test-utils.c @@ -594,6 +594,15 @@ void test_task_blocks_signal(void) 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[]) { test_lxc_string_replace(); @@ -606,6 +615,7 @@ int main(int argc, char *argv[]) test_parse_byte_size_string(); test_lxc_config_net_is_hwaddr(); test_task_blocks_signal(); + test_is_in_comm(); exit(EXIT_SUCCESS); }