]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
log: create log files in "fuzzing" mode if it's called outside fuzz targets
authorEvgeny Vereshchagin <evvers@ya.ru>
Wed, 14 Apr 2021 18:37:08 +0000 (18:37 +0000)
committerEvgeny Vereshchagin <evvers@ya.ru>
Mon, 26 Apr 2021 19:57:56 +0000 (19:57 +0000)
to make it possible to run the fuzzers along with the other tests

Signed-off-by: Evgeny Vereshchagin <evvers@ya.ru>
src/lxc/Makefile.am
src/lxc/log.c
src/lxc/utils.h
src/tests/lxc-test-utils.c

index 7ead4e27062fdff9ac0ed7482a4c427c37719b7d..da6806f8703aa97fd8830230cffa3d2f170b9cc8 100644 (file)
@@ -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
 
index b4877bca8dfcfcc0b3b1bae972abb2cc421267fa..844c1cefb73910cd0b58f3eeabe1020e7f712f0d 100644 (file)
@@ -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;
 }
 
index 49c0f3859f71b2008b2478f4ec1853dc5059791a..272e2dbef7e274d21f271f7bc9545ac697889bb3 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
 #include <sys/vfs.h>
@@ -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 */
index 3a26aecf306334ecd1930cddb1760d7b9b21b225..96d23f35c62b959011016f7e5b3a4a7607614b6e 100644 (file)
@@ -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);
 }