From nobody Fri Oct 3 11:45:44 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1579804433; cv=none; d=zohomail.com; s=zohoarc; b=SyRmHTGW8aBuWQUaAiIUB59054Znx5hz6KXo8Af/+ucvepcuYQskH6hwyLoXNQBWBDqLvg5uX8/LAxxQ9b7AU8yEYP1pf015gBje9jOcGfhHpt676uTjqy6qRsB+7Sl2/1JyLdc7THYX131+l5K1oX4xOYktiUHobm8yCnXH9jc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579804433; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/QogBgT1TKryRsR11pDKQb3Rpjj6FmtIWwzSwj1Qh94=; b=QAIQAbmpwGv2+pLUGI1CpUgQXrhQuSpjRTyCVlaNhMzIPa+CYP02iFz+kb+GY/rNDoc0MOVp1MLjiFM8IsjIRMntirWg2LF3/cBuIPwkNx0bs9L4l2AGdccPH0rXuYTrSrvtA+VVdl7BC0cYrrFKqdo6oo9/rrBwzFZmRG3D5kk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157980443360733.61011782683636; Thu, 23 Jan 2020 10:33:53 -0800 (PST) Received: from localhost ([::1]:34832 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuhId-0001zd-RQ for importer@patchew.org; Thu, 23 Jan 2020 13:33:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41521) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iufeE-0005IY-8M for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:48:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iufeC-0006lJ-Bo for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:48:01 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:48465 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iufeC-0006l5-8u for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:48:00 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-199-AoQBnM1hOci7z1TEVHr7Gg-1; Thu, 23 Jan 2020 11:47:58 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AB6E9477 for ; Thu, 23 Jan 2020 16:47:57 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-116-110.ams2.redhat.com [10.36.116.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id 04C9B19C69; Thu, 23 Jan 2020 16:47:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579798080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/QogBgT1TKryRsR11pDKQb3Rpjj6FmtIWwzSwj1Qh94=; b=Sz9sk2AZvpEy+ktW1lq0we0MhUfGvpniamDbh10PSsKi52txossEj4KX/qjyNwlCeblSzu 2j5QXOclPbLY2981OUQ5y+IglqGUvWxjSbQXE6Vj6vNnj8aeaaVSDS/2Fgmn1Z0nqEY2q4 kV7tPHITk9fDvcQ3of82h9Awz1RRszY= From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com Subject: [PULL 058/108] virtiofsd: add --syslog command-line option Date: Thu, 23 Jan 2020 16:45:40 +0000 Message-Id: <20200123164630.91498-59-dgilbert@redhat.com> In-Reply-To: <20200123164630.91498-1-dgilbert@redhat.com> References: <20200123164630.91498-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: AoQBnM1hOci7z1TEVHr7Gg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Stefan Hajnoczi 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 dgilbert: Reworked as a logging function Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Dr. David Alan Gilbert --- 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_lowleve= l.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; }; =20 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[] =3D { FUSE_HELPER_OPT("subtype=3D", nodefault_subtype), FUSE_OPT_KEY("subtype=3D", FUSE_OPT_KEY_KEEP), FUSE_HELPER_OPT("max_idle_threads=3D%u", max_idle_threads), + FUSE_HELPER_OPT("--syslog", syslog), FUSE_OPT_END }; =20 @@ -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 work= er " 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 #include #include +#include #include =20 #include "passthrough_helpers.h" @@ -138,6 +139,7 @@ static const struct fuse_opt lo_opts[] =3D { { "norace", offsetof(struct lo_data, norace), 1 }, FUSE_OPT_END }; +static bool use_syslog =3D false; =20 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64= _t n); =20 @@ -2262,11 +2264,12 @@ static void setup_mounts(const char *source) * Lock down this process to prevent access to other processes or files ou= tside * 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); } =20 /* Raise the maximum number of open file descriptors */ @@ -2298,6 +2301,42 @@ static void setup_nofile_rlimit(void) } } =20 +static void log_func(enum fuse_log_level level, const char *fmt, va_list a= p) +{ + if (use_syslog) { + int priority =3D LOG_ERR; + switch (level) { + case FUSE_LOG_EMERG: + priority =3D LOG_EMERG; + break; + case FUSE_LOG_ALERT: + priority =3D LOG_ALERT; + break; + case FUSE_LOG_CRIT: + priority =3D LOG_CRIT; + break; + case FUSE_LOG_ERR: + priority =3D LOG_ERR; + break; + case FUSE_LOG_WARNING: + priority =3D LOG_WARNING; + break; + case FUSE_LOG_NOTICE: + priority =3D LOG_NOTICE; + break; + case FUSE_LOG_INFO: + priority =3D LOG_INFO; + break; + case FUSE_LOG_DEBUG: + priority =3D LOG_DEBUG; + break; + } + vsyslog(priority, fmt, ap); + } else { + vfprintf(stderr, fmt, ap); + } +} + int main(int argc, char *argv[]) { struct fuse_args args =3D FUSE_ARGS_INIT(argc, argv); @@ -2336,6 +2375,11 @@ int main(int argc, char *argv[]) if (fuse_parse_cmdline(&args, &opts) !=3D 0) { return 1; } + fuse_set_log_func(log_func); + use_syslog =3D 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(); =20 - setup_sandbox(&lo, se); + setup_sandbox(&lo, se, opts.syslog); =20 /* Block until ctrl+c or fusermount -u */ ret =3D 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[] =3D { SCMP_SYS(writev), }; =20 -void setup_seccomp(void) +/* Syscalls used when --syslog is enabled */ +static const int syscall_whitelist_syslog[] =3D { + SCMP_SYS(sendto), +}; + +static void add_whitelist(scmp_filter_ctx ctx, const int syscalls[], size_= t len) { - scmp_filter_ctx ctx; size_t i; =20 + for (i =3D 0; i < len; i++) { + if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, syscalls[i], 0) !=3D 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 =3D seccomp_init(SCMP_ACT_KILL_PROCESS); /* Handle a newer libseccomp but an older kernel */ @@ -126,13 +143,10 @@ void setup_seccomp(void) exit(1); } =20 - for (i =3D 0; i < G_N_ELEMENTS(syscall_whitelist); i++) { - if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, - syscall_whitelist[i], 0) !=3D 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)); } =20 /* 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 =20 -void setup_seccomp(void); +#include + +void setup_seccomp(bool enable_syslog); =20 #endif /* VIRTIOFSD_SECCOMP_H */ --=20 2.24.1