[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 060/111] virtiofsd: add --syslog command-line option
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[PULL 060/111] virtiofsd: add --syslog command-line option |
Date: |
Thu, 23 Jan 2020 11:57:50 +0000 |
From: Stefan Hajnoczi <address@hidden>
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 <address@hidden>
dgilbert: Reworked as a logging function
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Reviewed-by: Daniel P. Berrangé <address@hidden>
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
---
tools/virtiofsd/fuse_lowlevel.h | 1 +
tools/virtiofsd/helper.c | 2 ++
tools/virtiofsd/passthrough_ll.c | 50 ++++++++++++++++++++++++++++++--
tools/virtiofsd/seccomp.c | 32 ++++++++++++++------
tools/virtiofsd/seccomp.h | 4 ++-
5 files changed, 76 insertions(+), 13 deletions(-)
diff --git a/tools/virtiofsd/fuse_lowlevel.h b/tools/virtiofsd/fuse_lowlevel.h
index 0d61df8110..f2750bc189 100644
--- a/tools/virtiofsd/fuse_lowlevel.h
+++ b/tools/virtiofsd/fuse_lowlevel.h
@@ -1795,6 +1795,7 @@ struct fuse_cmdline_opts {
int show_version;
int show_help;
int print_capabilities;
+ int syslog;
unsigned int max_idle_threads;
};
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
index 5531425223..9692ef9f1f 100644
--- a/tools/virtiofsd/helper.c
+++ b/tools/virtiofsd/helper.c
@@ -54,6 +54,7 @@ static const struct fuse_opt fuse_helper_opts[] = {
FUSE_HELPER_OPT("subtype=", nodefault_subtype),
FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
+ FUSE_HELPER_OPT("--syslog", syslog),
FUSE_OPT_END
};
@@ -138,6 +139,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"
" -o max_idle_threads the maximum number of idle worker "
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
index c281d817af..0372aca143 100644
--- a/tools/virtiofsd/passthrough_ll.c
+++ b/tools/virtiofsd/passthrough_ll.c
@@ -58,6 +58,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/xattr.h>
+#include <syslog.h>
#include <unistd.h>
#include "passthrough_helpers.h"
@@ -138,6 +139,7 @@ static const struct fuse_opt lo_opts[] = {
{ "norace", 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);
@@ -2262,11 +2264,12 @@ static void setup_mounts(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, struct fuse_session *se)
+static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
+ bool enable_syslog)
{
setup_namespaces(lo, se);
setup_mounts(lo->source);
- setup_seccomp();
+ setup_seccomp(enable_syslog);
}
/* Raise the maximum number of open file descriptors */
@@ -2298,6 +2301,42 @@ static void setup_nofile_rlimit(void)
}
}
+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);
@@ -2336,6 +2375,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();
@@ -2424,7 +2468,7 @@ int main(int argc, char *argv[])
/* Must be before sandbox since it wants /proc */
setup_capng();
- setup_sandbox(&lo, se);
+ setup_sandbox(&lo, se, opts.syslog);
/* Block until ctrl+c or fusermount -u */
ret = virtio_loop(se);
diff --git a/tools/virtiofsd/seccomp.c b/tools/virtiofsd/seccomp.c
index 691fb63dea..2d9d4a7ec0 100644
--- a/tools/virtiofsd/seccomp.c
+++ b/tools/virtiofsd/seccomp.c
@@ -107,11 +107,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 */
@@ -126,13 +143,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/tools/virtiofsd/seccomp.h b/tools/virtiofsd/seccomp.h
index 86bce72652..d47c8eade6 100644
--- a/tools/virtiofsd/seccomp.h
+++ b/tools/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.24.1
- [PULL 054/111] virtiofsd: cap-ng helpers, (continued)
- [PULL 054/111] virtiofsd: cap-ng helpers, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 050/111] virtiofsd: move to an empty network namespace, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 055/111] virtiofsd: Drop CAP_FSETID if client asked for it, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 057/111] virtiofsd: fix libfuse information leaks, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 058/111] docs: Add docs/tools, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 059/111] virtiofsd: add security guide document, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 056/111] virtiofsd: set maximum RLIMIT_NOFILE limit, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 063/111] virtiofsd: Add timestamp to the log with FUSE_LOG_DEBUG level, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 061/111] virtiofsd: print log only when priority is high enough, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 062/111] virtiofsd: Add ID to the log with FUSE_LOG_DEBUG level, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 060/111] virtiofsd: add --syslog command-line option,
Dr. David Alan Gilbert (git) <=
- [PULL 064/111] virtiofsd: Handle reinit, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 065/111] virtiofsd: Handle hard reboot, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 066/111] virtiofsd: Kill threads when queues are stopped, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 070/111] virtiofsd: passthrough_ll: disable readdirplus on cache=never, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 069/111] virtiofsd: passthrough_ll: add renameat2 support, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 068/111] contrib/libvhost-user: Protect slave fd with mutex, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 072/111] virtiofsd: rename unref_inode() to unref_inode_lolocked(), Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 067/111] vhost-user: Print unexpected slave message types, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 076/111] virtiofsd: passthrough_ll: use hashtable, Dr. David Alan Gilbert (git), 2020/01/23
- [PULL 073/111] virtiofsd: fail when parent inode isn't known in lo_do_lookup(), Dr. David Alan Gilbert (git), 2020/01/23