yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-stay-below-fs.file-max-sysctl-value-CVE-20.patch

ddf19c
From 301f19f2ebd617e43e3a8e7bdcf694de580fe689 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Tue, 5 May 2020 16:35:56 +0100
ddf19c
Subject: [PATCH 5/9] virtiofsd: stay below fs.file-max sysctl value
ddf19c
 (CVE-2020-10717)
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200505163600.22956-4-dgilbert@redhat.com>
ddf19c
Patchwork-id: 96271
ddf19c
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 3/7] virtiofsd: stay below fs.file-max sysctl value (CVE-2020-10717)
ddf19c
Bugzilla: 1817445
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ddf19c
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
The system-wide fs.file-max sysctl value determines how many files can
ddf19c
be open.  It defaults to a value calculated based on the machine's RAM
ddf19c
size.  Previously virtiofsd would try to set RLIMIT_NOFILE to 1,000,000
ddf19c
and this allowed the FUSE client to exhaust the number of open files
ddf19c
system-wide on Linux hosts with less than 10 GB of RAM!
ddf19c
ddf19c
Take fs.file-max into account when choosing the default RLIMIT_NOFILE
ddf19c
value.
ddf19c
ddf19c
Fixes: CVE-2020-10717
ddf19c
Reported-by: Yuval Avrahami <yavrahami@paloaltonetworks.com>
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-Id: <20200501140644.220940-3-stefanha@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 8c1d353d107b4fc344e27f2f08ea7fa25de2eea2)
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/helper.c | 26 +++++++++++++++++++++++++-
ddf19c
 1 file changed, 25 insertions(+), 1 deletion(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
ddf19c
index 9b3eddc..5b222ea 100644
ddf19c
--- a/tools/virtiofsd/helper.c
ddf19c
+++ b/tools/virtiofsd/helper.c
ddf19c
@@ -176,7 +176,8 @@ void fuse_cmdline_help(void)
ddf19c
            "                               default: no_xattr\n"
ddf19c
            "    --rlimit-nofile=<num>      set maximum number of file descriptors\n"
ddf19c
            "                               (0 leaves rlimit unchanged)\n"
ddf19c
-           "                               default: 1,000,000 if the current rlimit is lower\n"
ddf19c
+           "                               default: min(1000000, fs.file-max - 16384)\n"
ddf19c
+           "                                        if the current rlimit is lower\n"
ddf19c
            );
ddf19c
 }
ddf19c
 
ddf19c
@@ -199,9 +200,32 @@ static int fuse_helper_opt_proc(void *data, const char *arg, int key,
ddf19c
 
ddf19c
 static unsigned long get_default_rlimit_nofile(void)
ddf19c
 {
ddf19c
+    g_autofree gchar *file_max_str = NULL;
ddf19c
+    const rlim_t reserved_fds = 16384; /* leave at least this many fds free */
ddf19c
     rlim_t max_fds = 1000000; /* our default RLIMIT_NOFILE target */
ddf19c
+    rlim_t file_max;
ddf19c
     struct rlimit rlim;
ddf19c
 
ddf19c
+    /*
ddf19c
+     * Reduce max_fds below the system-wide maximum, if necessary.  This
ddf19c
+     * ensures there are fds available for other processes so we don't
ddf19c
+     * cause resource exhaustion.
ddf19c
+     */
ddf19c
+    if (!g_file_get_contents("/proc/sys/fs/file-max", &file_max_str,
ddf19c
+                             NULL, NULL)) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "can't read /proc/sys/fs/file-max\n");
ddf19c
+        exit(1);
ddf19c
+    }
ddf19c
+    file_max = g_ascii_strtoull(file_max_str, NULL, 10);
ddf19c
+    if (file_max < 2 * reserved_fds) {
ddf19c
+        fuse_log(FUSE_LOG_ERR,
ddf19c
+                 "The fs.file-max sysctl is too low (%lu) to allow a "
ddf19c
+                 "reasonable number of open files.\n",
ddf19c
+                 (unsigned long)file_max);
ddf19c
+        exit(1);
ddf19c
+    }
ddf19c
+    max_fds = MIN(file_max - reserved_fds, max_fds);
ddf19c
+
ddf19c
     if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
ddf19c
         fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n");
ddf19c
         exit(1);
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c