From: Stefan Hajnoczi <stefa...@redhat.com> Sometimes collecting output from stderr is inconvenient or does not fit within the overall logging architecture. Add syslog(3) support for cases where stderr cannot be used.
Signed-off-by: Stefan Hajnoczi <stefa...@redhat.com> dgilbert: Reworked as a logging function Signed-off-by: Dr. David Alan Gilbert <dgilb...@redhat.com> --- contrib/virtiofsd/fuse_lowlevel.h | 1 + contrib/virtiofsd/helper.c | 2 ++ contrib/virtiofsd/passthrough_ll.c | 34 +++++++++++++++++++++++++++--- contrib/virtiofsd/seccomp.c | 32 ++++++++++++++++++++-------- contrib/virtiofsd/seccomp.h | 4 +++- 5 files changed, 60 insertions(+), 13 deletions(-) diff --git a/contrib/virtiofsd/fuse_lowlevel.h b/contrib/virtiofsd/fuse_lowlevel.h index 13fd9791d5..112596caaf 100644 --- a/contrib/virtiofsd/fuse_lowlevel.h +++ b/contrib/virtiofsd/fuse_lowlevel.h @@ -1798,6 +1798,7 @@ struct fuse_cmdline_opts { int show_help; int print_capabilities; int clone_fd; + int syslog; unsigned int max_idle_threads; }; diff --git a/contrib/virtiofsd/helper.c b/contrib/virtiofsd/helper.c index 08e00c0d13..b1e45aee05 100644 --- a/contrib/virtiofsd/helper.c +++ b/contrib/virtiofsd/helper.c @@ -53,6 +53,7 @@ static const struct fuse_opt fuse_helper_opts[] = { #endif FUSE_HELPER_OPT("clone_fd", clone_fd), FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads), + FUSE_HELPER_OPT("--syslog", syslog), FUSE_OPT_END }; @@ -135,6 +136,7 @@ void fuse_cmdline_help(void) " -V --version print version\n" " --print-capabilities print vhost-user.json\n" " -d -o debug enable debug output (implies -f)\n" + " --syslog log to syslog (default stderr)\n" " -f foreground operation\n" " --daemonize run in background\n" " -s disable multi-threaded operation\n" diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthrough_ll.c index 25f7ad854a..b413687720 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -42,6 +42,7 @@ #include <stddef.h> #include <stdbool.h> #include <string.h> +#include <syslog.h> #include <limits.h> #include <dirent.h> #include <assert.h> @@ -151,6 +152,7 @@ static const struct fuse_opt lo_opts[] = { offsetof(struct lo_data, norace), 1 }, FUSE_OPT_END }; +static bool use_syslog = false; static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n); @@ -2103,13 +2105,13 @@ static void setup_mount_namespace(const char *source) * Lock down this process to prevent access to other processes or files outside * source directory. This reduces the impact of arbitrary code execution bugs. */ -static void setup_sandbox(struct lo_data *lo) +static void setup_sandbox(struct lo_data *lo, bool enable_syslog) { setup_pid_namespace(); setup_proc_self_fd(lo); setup_net_namespace(); setup_mount_namespace(lo->source); - setup_seccomp(); + setup_seccomp(enable_syslog); } /* Raise the maximum number of open file descriptors to the system limit */ @@ -2142,6 +2144,27 @@ static void setup_nofile_rlimit(void) g_free(nr_open); } +static void log_func(enum fuse_log_level level, + const char *fmt, va_list ap) +{ + if (use_syslog) { + int priority = LOG_ERR; + switch (level) { + case FUSE_LOG_EMERG: priority = LOG_EMERG; break; + case FUSE_LOG_ALERT: priority = LOG_ALERT; break; + case FUSE_LOG_CRIT: priority = LOG_CRIT; break; + case FUSE_LOG_ERR: priority = LOG_ERR; break; + case FUSE_LOG_WARNING: priority = LOG_WARNING; break; + case FUSE_LOG_NOTICE: priority = LOG_NOTICE; break; + case FUSE_LOG_INFO: priority = LOG_INFO; break; + case FUSE_LOG_DEBUG: priority = LOG_DEBUG; break; + } + vsyslog(priority, fmt, ap); + } else { + vfprintf(stderr, fmt, ap); + } +} + int main(int argc, char *argv[]) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); @@ -2179,6 +2202,11 @@ int main(int argc, char *argv[]) if (fuse_parse_cmdline(&args, &opts) != 0) return 1; + fuse_set_log_func(log_func); + use_syslog = opts.syslog; + if (use_syslog) { + openlog("virtiofsd", LOG_PID, LOG_DAEMON); + } if (opts.show_help) { printf("usage: %s [options]\n\n", argv[0]); fuse_cmdline_help(); @@ -2260,7 +2288,7 @@ int main(int argc, char *argv[]) fuse_daemonize(opts.foreground); - setup_sandbox(&lo); + setup_sandbox(&lo, opts.syslog); /* Block until ctrl+c or fusermount -u */ ret = virtio_loop(se); diff --git a/contrib/virtiofsd/seccomp.c b/contrib/virtiofsd/seccomp.c index df1390d6be..77bc405bdc 100644 --- a/contrib/virtiofsd/seccomp.c +++ b/contrib/virtiofsd/seccomp.c @@ -88,11 +88,28 @@ static const int syscall_whitelist[] = { SCMP_SYS(writev), }; -void setup_seccomp(void) +/* Syscalls used when --syslog is enabled */ +static const int syscall_whitelist_syslog[] = { + SCMP_SYS(sendto), +}; + +static void add_whitelist(scmp_filter_ctx ctx, const int syscalls[], size_t len) { - scmp_filter_ctx ctx; size_t i; + for (i = 0; i < len; i++) { + if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscalls[i], 0) != 0) { + fuse_log(FUSE_LOG_ERR, "seccomp_rule_add syscall %d failed\n", + syscalls[i]); + exit(1); + } + } +} + +void setup_seccomp(bool enable_syslog) +{ + scmp_filter_ctx ctx; + #ifdef SCMP_ACT_KILL_PROCESS ctx = seccomp_init(SCMP_ACT_KILL_PROCESS); /* Handle a newer libseccomp but an older kernel */ @@ -107,13 +124,10 @@ void setup_seccomp(void) exit(1); } - for (i = 0; i < G_N_ELEMENTS(syscall_whitelist); i++) { - if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, - syscall_whitelist[i], 0) != 0) { - fuse_log(FUSE_LOG_ERR, "seccomp_rule_add syscall %d", - syscall_whitelist[i]); - exit(1); - } + add_whitelist(ctx, syscall_whitelist, G_N_ELEMENTS(syscall_whitelist)); + if (enable_syslog) { + add_whitelist(ctx, syscall_whitelist_syslog, + G_N_ELEMENTS(syscall_whitelist_syslog)); } /* libvhost-user calls this for post-copy migration, we don't need it */ diff --git a/contrib/virtiofsd/seccomp.h b/contrib/virtiofsd/seccomp.h index 86bce72652..d47c8eade6 100644 --- a/contrib/virtiofsd/seccomp.h +++ b/contrib/virtiofsd/seccomp.h @@ -9,6 +9,8 @@ #ifndef VIRTIOFSD_SECCOMP_H #define VIRTIOFSD_SECCOMP_H -void setup_seccomp(void); +#include <stdbool.h> + +void setup_seccomp(bool enable_syslog); #endif /* VIRTIOFSD_SECCOMP_H */ -- 2.23.0