Blame SOURCES/dbus-1.12.8-fix-fd-limit-change.patch

73dd34
From 94bacc6955e563a7e698e53151a75323279a9f45 Mon Sep 17 00:00:00 2001
73dd34
From: Simon McVittie <smcv@collabora.com>
73dd34
Date: Mon, 11 Mar 2019 09:03:39 +0000
73dd34
Subject: [PATCH] bus: Try to raise soft fd limit to match hard limit
73dd34
73dd34
Linux systems have traditionally set the soft limit to 1024 and the hard
73dd34
limit to 4096. Recent versions of systemd keep the soft fd limit at
73dd34
1024 to avoid breaking programs that still use select(), but raise the
73dd34
hard limit to 512*1024, while in recent Debian versions a complicated
73dd34
interaction between components gives a soft limit of 1024 and a hard
73dd34
limit of 1024*1024. If we can, we might as well elevate our soft limit
73dd34
to match the hard limit, minimizing the chance that we will run out of
73dd34
file descriptor slots.
73dd34
73dd34
Unlike the previous code to raise the hard and soft limits to at least
73dd34
65536, we do this even if we don't have privileges: privileges are
73dd34
unnecessary to raise the soft limit up to the hard limit.
73dd34
73dd34
If we *do* have privileges, we also continue to raise the hard and soft
73dd34
limits to at least 65536 if they weren't already that high, making
73dd34
it harder to carry out a denial of service attack on the system bus on
73dd34
systems that use the traditional limit (CVE-2014-7824).
73dd34
73dd34
As was previously the case on the system bus, we'll drop the limits back
73dd34
to our initial limits before we execute a subprocess for traditional
73dd34
(non-systemd) activation, if enabled.
73dd34
73dd34
systemd activation doesn't involve us starting subprocesses at all,
73dd34
so in both cases activated services will still inherit the same limits
73dd34
they did previously.
73dd34
73dd34
This change also fixes a bug when the hard limit is very large but
73dd34
the soft limit is not, for example seen as a regression when upgrading
73dd34
to systemd >= 240 (Debian #928877). In such environments, dbus-daemon
73dd34
would previously have changed its fd limit to 64K soft/64K hard. Because
73dd34
this hard limit is less than its original hard limit, it was unable to
73dd34
restore its original hard limit as intended when carrying out traditional
73dd34
activation, leaving activated subprocesses with unintended limits (while
73dd34
logging a warning).
73dd34
73dd34
Reviewed-by: Lennart Poettering <lennart@poettering.net>
73dd34
[smcv: Correct a comment based on Lennart's review, reword commit message]
73dd34
Signed-off-by: Simon McVittie <smcv@collabora.com>
73dd34
(cherry picked from commit 7eacbfece70f16bb54d0f3ac51f87ae398759ef5)
73dd34
[smcv: Mention that this also fixes Debian #928877]
73dd34
---
73dd34
 bus/bus.c                     |  8 ++---
73dd34
 dbus/dbus-sysdeps-util-unix.c | 64 +++++++++++++++++++++--------------
73dd34
 dbus/dbus-sysdeps-util-win.c  |  3 +-
73dd34
 dbus/dbus-sysdeps.h           |  3 +-
73dd34
 4 files changed, 44 insertions(+), 34 deletions(-)
73dd34
73dd34
diff --git a/bus/bus.c b/bus/bus.c
73dd34
index 30ce4e10..2ad8e789 100644
73dd34
--- a/bus/bus.c
73dd34
+++ b/bus/bus.c
73dd34
@@ -693,11 +693,11 @@ raise_file_descriptor_limit (BusContext      *context)
73dd34
   /* We used to compute a suitable rlimit based on the configured number
73dd34
    * of connections, but that breaks down as soon as we allow fd-passing,
73dd34
    * because each connection is allowed to pass 64 fds to us, and if
73dd34
-   * they all did, we'd hit kernel limits. We now hard-code 64k as a
73dd34
-   * good limit, like systemd does: that's enough to avoid DoS from
73dd34
-   * anything short of multiple uids conspiring against us.
73dd34
+   * they all did, we'd hit kernel limits. We now hard-code a good
73dd34
+   * limit that is enough to avoid DoS from anything short of multiple
73dd34
+   * uids conspiring against us, much like systemd does.
73dd34
    */
73dd34
-  if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
73dd34
+  if (!_dbus_rlimit_raise_fd_limit (&error))
73dd34
     {
73dd34
       bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
73dd34
                        "%s: %s", error.name, error.message);
73dd34
diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c
73dd34
index 2be5b779..7c4c3604 100644
73dd34
--- a/dbus/dbus-sysdeps-util-unix.c
73dd34
+++ b/dbus/dbus-sysdeps-util-unix.c
73dd34
@@ -406,23 +406,15 @@ _dbus_rlimit_save_fd_limit (DBusError *error)
73dd34
   return self;
73dd34
 }
73dd34
 
73dd34
+/* Enough fds that we shouldn't run out, even if several uids work
73dd34
+ * together to carry out a denial-of-service attack. This happens to be
73dd34
+ * the same number that systemd < 234 would normally use. */
73dd34
+#define ENOUGH_FDS 65536
73dd34
+
73dd34
 dbus_bool_t
73dd34
-_dbus_rlimit_raise_fd_limit_if_privileged (unsigned int  desired,
73dd34
-                                           DBusError    *error)
73dd34
+_dbus_rlimit_raise_fd_limit (DBusError *error)
73dd34
 {
73dd34
-  struct rlimit lim;
73dd34
-
73dd34
-  /* No point to doing this practically speaking
73dd34
-   * if we're not uid 0.  We expect the system
73dd34
-   * bus to use this before we change UID, and
73dd34
-   * the session bus takes the Linux default,
73dd34
-   * currently 1024 for cur and 4096 for max.
73dd34
-   */
73dd34
-  if (getuid () != 0)
73dd34
-    {
73dd34
-      /* not an error, we're probably the session bus */
73dd34
-      return TRUE;
73dd34
-    }
73dd34
+  struct rlimit old, lim;
73dd34
 
73dd34
   if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
73dd34
     {
73dd34
@@ -431,22 +423,43 @@ _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int  desired,
73dd34
       return FALSE;
73dd34
     }
73dd34
 
73dd34
-  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
73dd34
+  old = lim;
73dd34
+
73dd34
+  if (getuid () == 0)
73dd34
     {
73dd34
-      /* not an error, everything is fine */
73dd34
-      return TRUE;
73dd34
+      /* We are privileged, so raise the soft limit to at least
73dd34
+       * ENOUGH_FDS, and the hard limit to at least the desired soft
73dd34
+       * limit. This assumes we can exercise CAP_SYS_RESOURCE on Linux,
73dd34
+       * or other OSs' equivalents. */
73dd34
+      if (lim.rlim_cur != RLIM_INFINITY &&
73dd34
+          lim.rlim_cur < ENOUGH_FDS)
73dd34
+        lim.rlim_cur = ENOUGH_FDS;
73dd34
+
73dd34
+      if (lim.rlim_max != RLIM_INFINITY &&
73dd34
+          lim.rlim_max < lim.rlim_cur)
73dd34
+        lim.rlim_max = lim.rlim_cur;
73dd34
     }
73dd34
 
73dd34
-  /* Ignore "maximum limit", assume we have the "superuser"
73dd34
-   * privileges.  On Linux this is CAP_SYS_RESOURCE.
73dd34
-   */
73dd34
-  lim.rlim_cur = lim.rlim_max = desired;
73dd34
+  /* Raise the soft limit to match the hard limit, which we can do even
73dd34
+   * if we are unprivileged. In particular, systemd >= 240 will normally
73dd34
+   * set rlim_cur to 1024 and rlim_max to 512*1024, recent Debian
73dd34
+   * versions end up setting rlim_cur to 1024 and rlim_max to 1024*1024,
73dd34
+   * and older and non-systemd Linux systems would typically set rlim_cur
73dd34
+   * to 1024 and rlim_max to 4096. */
73dd34
+  if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
73dd34
+    lim.rlim_cur = lim.rlim_max;
73dd34
+
73dd34
+  /* Early-return if there is nothing to do. */
73dd34
+  if (lim.rlim_max == old.rlim_max &&
73dd34
+      lim.rlim_cur == old.rlim_cur)
73dd34
+    return TRUE;
73dd34
 
73dd34
   if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
73dd34
     {
73dd34
       dbus_set_error (error, _dbus_error_from_errno (errno),
73dd34
-                      "Failed to set fd limit to %u: %s",
73dd34
-                      desired, _dbus_strerror (errno));
73dd34
+                      "Failed to set fd limit to %lu: %s",
73dd34
+                      (unsigned long) lim.rlim_cur,
73dd34
+                      _dbus_strerror (errno));
73dd34
       return FALSE;
73dd34
     }
73dd34
 
73dd34
@@ -485,8 +498,7 @@ _dbus_rlimit_save_fd_limit (DBusError *error)
73dd34
 }
73dd34
 
73dd34
 dbus_bool_t
73dd34
-_dbus_rlimit_raise_fd_limit_if_privileged (unsigned int  desired,
73dd34
-                                           DBusError    *error)
73dd34
+_dbus_rlimit_raise_fd_limit (DBusError *error)
73dd34
 {
73dd34
   fd_limit_not_supported (error);
73dd34
   return FALSE;
73dd34
diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c
73dd34
index 1ef4ae6c..1c1d9f7d 100644
73dd34
--- a/dbus/dbus-sysdeps-util-win.c
73dd34
+++ b/dbus/dbus-sysdeps-util-win.c
73dd34
@@ -273,8 +273,7 @@ _dbus_rlimit_save_fd_limit (DBusError *error)
73dd34
 }
73dd34
 
73dd34
 dbus_bool_t
73dd34
-_dbus_rlimit_raise_fd_limit_if_privileged (unsigned int  desired,
73dd34
-                                           DBusError    *error)
73dd34
+_dbus_rlimit_raise_fd_limit (DBusError *error)
73dd34
 {
73dd34
   fd_limit_not_supported (error);
73dd34
   return FALSE;
73dd34
diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h
73dd34
index ef786ecc..0b9d7696 100644
73dd34
--- a/dbus/dbus-sysdeps.h
73dd34
+++ b/dbus/dbus-sysdeps.h
73dd34
@@ -698,8 +698,7 @@ dbus_bool_t _dbus_replace_install_prefix (DBusString *path);
73dd34
 typedef struct DBusRLimit DBusRLimit;
73dd34
 
73dd34
 DBusRLimit     *_dbus_rlimit_save_fd_limit                 (DBusError    *error);
73dd34
-dbus_bool_t     _dbus_rlimit_raise_fd_limit_if_privileged  (unsigned int  desired,
73dd34
-                                                            DBusError    *error);
73dd34
+dbus_bool_t     _dbus_rlimit_raise_fd_limit                (DBusError    *error);
73dd34
 dbus_bool_t     _dbus_rlimit_restore_fd_limit              (DBusRLimit   *saved,
73dd34
                                                             DBusError    *error);
73dd34
 void            _dbus_rlimit_free                          (DBusRLimit   *lim);
73dd34
-- 
73dd34
GitLab
73dd34