From nobody Sat May 18 09:01:26 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1588342183; cv=none; d=zohomail.com; s=zohoarc; b=eOdLQi6zRAaTfuk/ldc6gDQhX2ahSdVVzYotplMxxi4MKYm4qc/6D0rr6N9EMHRn5MxQpgeu0FmuBzy9/BF0Adw4NrB+Wxq1iiUvBeJqwCwkd/QwDd6dI24Brenl3CWgJDArJYO3Tqtt1EQE3d8f9qPn+inHcGxZ2/UIfY/AXr0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588342183; h=Content-Type:Content-Transfer-Encoding:Cc: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=kbG/ivlaYfH0BCJK951opl/KBNJa3i2O840z7dUnfdo=; b=JPJMnIQ4aobFp/tN0qfowxO3tVtXrN0jy2KAxID0i99ryb/uoL8++J0exUJlyvA4W9WFqaRxvjc8nBeR5OUbNhVFPJUFZsSqNl/7fJeibTE2LYuNz1eZeRjgY2GydoonRXnI3YzZmBtUaHItBQLkhACQ2gb2l5OkMLxmGHVDU4I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail 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 1588342183093609.57847509377; Fri, 1 May 2020 07:09:43 -0700 (PDT) Received: from localhost ([::1]:50554 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUWMH-0006Zn-PX for importer@patchew.org; Fri, 01 May 2020 10:09:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37260) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUWK2-0002e8-1s for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jUWJu-00060W-NY for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:21 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:58228 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jUWJu-00060I-5w for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:14 -0400 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-383-1OitSWMcMraLIyp0Aczm1Q-1; Fri, 01 May 2020 10:07:10 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E23C31899527 for ; Fri, 1 May 2020 14:07:09 +0000 (UTC) Received: from localhost (ovpn-112-36.ams2.redhat.com [10.36.112.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2BD865EDF1; Fri, 1 May 2020 14:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588342032; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kbG/ivlaYfH0BCJK951opl/KBNJa3i2O840z7dUnfdo=; b=gI99JH0nJaddgsNG2Z1w9OyrBB+nif1grv/RGV1cyui8czAfjUMf/LQEm+g5YQ0a6PNwIj OFia/KlX0KW5M9VYwNEl/CZzj4OCM2GlTk59rZwpyZi8Kd4xCOR7X8QTGx4vzb2/B+bdPy fna2NxKZnT5MrN8kYIMWUv+HNDiiLIY= X-MC-Unique: 1OitSWMcMraLIyp0Aczm1Q-1 From: Stefan Hajnoczi To: Subject: [PATCH v2 1/2] virtiofsd: add --rlimit-nofile=NUM option Date: Fri, 1 May 2020 15:06:43 +0100 Message-Id: <20200501140644.220940-2-stefanha@redhat.com> In-Reply-To: <20200501140644.220940-1-stefanha@redhat.com> References: <20200501140644.220940-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=207.211.31.81; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/01 03:09:43 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: , Cc: virtio-fs@redhat.com, Stefan Hajnoczi , "Dr. David Alan Gilbert" , vgoyal@redhat.com, pjp@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Make it possible to specify the RLIMIT_NOFILE on the command-line. Users running multiple virtiofsd processes should allocate a certain number to each process so that the system-wide limit can never be exhausted. When this option is set to 0 the rlimit is left at its current value. This is useful when a management tool wants to configure the rlimit itself. The default behavior remains unchanged: try to set the limit to 1,000,000 file descriptors if the current rlimit is lower. Signed-off-by: Stefan Hajnoczi Reviewed-by: Dr. David Alan Gilbert --- tools/virtiofsd/fuse_lowlevel.h | 1 + tools/virtiofsd/helper.c | 23 +++++++++++++++++++++++ tools/virtiofsd/passthrough_ll.c | 22 ++++++++-------------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/virtiofsd/fuse_lowlevel.h b/tools/virtiofsd/fuse_lowleve= l.h index 8f6d705b5c..562fd5241e 100644 --- a/tools/virtiofsd/fuse_lowlevel.h +++ b/tools/virtiofsd/fuse_lowlevel.h @@ -1777,6 +1777,7 @@ struct fuse_cmdline_opts { int syslog; int log_level; unsigned int max_idle_threads; + unsigned long rlimit_nofile; }; =20 /** diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c index 819c2bc13c..dc59f38af0 100644 --- a/tools/virtiofsd/helper.c +++ b/tools/virtiofsd/helper.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include =20 #define FUSE_HELPER_OPT(t, p) \ @@ -53,6 +55,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("--rlimit-nofile=3D%lu", rlimit_nofile), FUSE_HELPER_OPT("--syslog", syslog), FUSE_HELPER_OPT_VALUE("log_level=3Ddebug", log_level, FUSE_LOG_DEBUG), FUSE_HELPER_OPT_VALUE("log_level=3Dinfo", log_level, FUSE_LOG_INFO), @@ -171,6 +174,9 @@ void fuse_cmdline_help(void) " default: no_writeback\n" " -o xattr|no_xattr enable/disable xattr\n" " default: no_xattr\n" + " --rlimit-nofile=3D set maximum number of file de= scriptors\n" + " (0 leaves rlimit unchanged)\n" + " default: 1,000,000 if the curre= nt rlimit is lower\n" ); } =20 @@ -191,11 +197,28 @@ static int fuse_helper_opt_proc(void *data, const cha= r *arg, int key, } } =20 +static unsigned long get_default_rlimit_nofile(void) +{ + rlim_t max_fds =3D 1000000; /* our default RLIMIT_NOFILE target */ + struct rlimit rlim; + + if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { + fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n"); + exit(1); + } + + if (rlim.rlim_cur >=3D max_fds) { + return 0; /* we have more fds available than required! */ + } + return max_fds; +} + int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *o= pts) { memset(opts, 0, sizeof(struct fuse_cmdline_opts)); =20 opts->max_idle_threads =3D 10; + opts->rlimit_nofile =3D get_default_rlimit_nofile(); opts->foreground =3D 1; =20 if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc)= =3D=3D diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough= _ll.c index 4c35c95b25..f7b9c1d20c 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -2707,24 +2707,18 @@ static void setup_sandbox(struct lo_data *lo, struc= t fuse_session *se, setup_seccomp(enable_syslog); } =20 -/* Raise the maximum number of open file descriptors */ -static void setup_nofile_rlimit(void) +/* Set the maximum number of open file descriptors */ +static void setup_nofile_rlimit(unsigned long rlimit_nofile) { - const rlim_t max_fds =3D 1000000; - struct rlimit rlim; + struct rlimit rlim =3D { + .rlim_cur =3D rlimit_nofile, + .rlim_max =3D rlimit_nofile, + }; =20 - if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { - fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n"); - exit(1); - } - - if (rlim.rlim_cur >=3D max_fds) { + if (rlimit_nofile =3D=3D 0) { return; /* nothing to do */ } =20 - rlim.rlim_cur =3D max_fds; - rlim.rlim_max =3D max_fds; - if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) { /* Ignore SELinux denials */ if (errno =3D=3D EPERM) { @@ -2977,7 +2971,7 @@ int main(int argc, char *argv[]) =20 fuse_daemonize(opts.foreground); =20 - setup_nofile_rlimit(); + setup_nofile_rlimit(opts.rlimit_nofile); =20 /* Must be before sandbox since it wants /proc */ setup_capng(); --=20 2.25.3 From nobody Sat May 18 09:01:26 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1588342099; cv=none; d=zohomail.com; s=zohoarc; b=JvjfJbgjB9BEiRPMIRLNEf3e+/0xKKAFB2DPZI1MHnYpFfe91vfIDvqGzGKOsSHz6J2vqCFblOWHAIW1iUlWsKduo+spB8EJ3usrvTES88H3uqU1y6J2Honf4MuhVknZ5omPTtsmim/r4/MLW3AUEh88cPjjn9uoGb6oud1oEyM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588342099; h=Content-Type:Content-Transfer-Encoding:Cc: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=rkvF79RrRX5S1GUW/qd4mrdcGIbSvsNKlW8sbMYtD+k=; b=RHQxBvcvsuomGnW2apaauoRNUhBGD7vSmEHWA+sRhsAIKEb6G6Io5ag07OxjPXm6g9hZzVCRI68d3aTWsb5yXCstCZNOtc2Ogv7RB7tQxKuLR7b0EMhQaxtUpdkiEasmv2XVTHwNtVYNhgQA8NbM+jGkwgYRTqHGi1WPOFb3kjE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail 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 1588342099295521.5780344315305; Fri, 1 May 2020 07:08:19 -0700 (PDT) Received: from localhost ([::1]:47248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUWKv-00041I-TD for importer@patchew.org; Fri, 01 May 2020 10:08:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37290) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUWK6-0002hj-6i for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jUWK4-0006Iw-5l for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:25 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:56530 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jUWK3-0006FY-L1 for qemu-devel@nongnu.org; Fri, 01 May 2020 10:07:23 -0400 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-50-SzHfc6M1NuuzTT3pXhi7Jw-1; Fri, 01 May 2020 10:07:20 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 865628015D1; Fri, 1 May 2020 14:07:19 +0000 (UTC) Received: from localhost (ovpn-112-36.ams2.redhat.com [10.36.112.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 32E7E5D9CC; Fri, 1 May 2020 14:07:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588342042; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rkvF79RrRX5S1GUW/qd4mrdcGIbSvsNKlW8sbMYtD+k=; b=Z1M+fThtkel0fCn249tWQh32GlYLhIyolPynoVZO/JYTZjumR0wNdvlAzpuU2iZK+9AGJw WlO66XUu8a3P0cIdU+zJrMNnogCbkfRHGWoNOXzxShj0lZ7U9fgnpowugoN0KpsTqL8sIu KCXdSlCy/Fbpk4C6ZALl6hpaY/e4K3c= X-MC-Unique: SzHfc6M1NuuzTT3pXhi7Jw-1 From: Stefan Hajnoczi To: Subject: [PATCH v2 2/2] virtiofsd: stay below fs.file-max sysctl value (CVE-2020-10717) Date: Fri, 1 May 2020 15:06:44 +0100 Message-Id: <20200501140644.220940-3-stefanha@redhat.com> In-Reply-To: <20200501140644.220940-1-stefanha@redhat.com> References: <20200501140644.220940-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=205.139.110.120; envelope-from=stefanha@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/01 08:22:51 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.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: , Cc: "Dr. David Alan Gilbert" , virtio-fs@redhat.com, Stefan Hajnoczi , Yuval Avrahami , pjp@redhat.com, vgoyal@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The system-wide fs.file-max sysctl value determines how many files can be open. It defaults to a value calculated based on the machine's RAM size. Previously virtiofsd would try to set RLIMIT_NOFILE to 1,000,000 and this allowed the FUSE client to exhaust the number of open files system-wide on Linux hosts with less than 10 GB of RAM! Take fs.file-max into account when choosing the default RLIMIT_NOFILE value. Fixes: CVE-2020-10717 Reported-by: Yuval Avrahami Signed-off-by: Stefan Hajnoczi Reviewed-by: Dr. David Alan Gilbert --- tools/virtiofsd/helper.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c index dc59f38af0..00a1ef666a 100644 --- a/tools/virtiofsd/helper.c +++ b/tools/virtiofsd/helper.c @@ -176,7 +176,8 @@ void fuse_cmdline_help(void) " default: no_xattr\n" " --rlimit-nofile=3D set maximum number of file de= scriptors\n" " (0 leaves rlimit unchanged)\n" - " default: 1,000,000 if the curre= nt rlimit is lower\n" + " default: min(1000000, fs.file-m= ax - 16384)\n" + " if the current rlimit = is lower\n" ); } =20 @@ -199,9 +200,32 @@ static int fuse_helper_opt_proc(void *data, const char= *arg, int key, =20 static unsigned long get_default_rlimit_nofile(void) { + g_autofree gchar *file_max_str =3D NULL; + const rlim_t reserved_fds =3D 16384; /* leave at least this many fds f= ree */ rlim_t max_fds =3D 1000000; /* our default RLIMIT_NOFILE target */ + rlim_t file_max; struct rlimit rlim; =20 + /* + * Reduce max_fds below the system-wide maximum, if necessary. This + * ensures there are fds available for other processes so we don't + * cause resource exhaustion. + */ + if (!g_file_get_contents("/proc/sys/fs/file-max", &file_max_str, + NULL, NULL)) { + fuse_log(FUSE_LOG_ERR, "can't read /proc/sys/fs/file-max\n"); + exit(1); + } + file_max =3D g_ascii_strtoull(file_max_str, NULL, 10); + if (file_max < 2 * reserved_fds) { + fuse_log(FUSE_LOG_ERR, + "The fs.file-max sysctl is too low (%lu) to allow a " + "reasonable number of open files.\n", + (unsigned long)file_max); + exit(1); + } + max_fds =3D MIN(file_max - reserved_fds, max_fds); + if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n"); exit(1); --=20 2.25.3