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