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 <evvers@ya.ru>
This commit is contained in:
Evgeny Vereshchagin 2021-04-14 18:37:08 +00:00
parent 1667e14e07
commit 4a6af91855
4 changed files with 47 additions and 3 deletions

View File

@ -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

View File

@ -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)
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)
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;
} }

View File

@ -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,
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 */

View File

@ -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[])
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);
} }