From 635f3df5dc540892cca0b9d4ba42ab7215277f7c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 9 Aug 2017 16:15:07 +0200 Subject: [PATCH] units: make use of the new !! ExecStart= prefix in systemd-resolved.service Let's make use of !! to run resolved with ambient capabilities on systems supporting them. --- src/resolve/resolved.c | 19 ++++++++++++------- units/systemd-resolved.service.in | 9 ++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/resolve/resolved.c b/src/resolve/resolved.c index 74603f93112..8fd81956b83 100644 --- a/src/resolve/resolved.c +++ b/src/resolve/resolved.c @@ -67,13 +67,18 @@ int main(int argc, char *argv[]) { goto finish; } - /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */ - r = drop_privileges(uid, gid, - (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */ - (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */ - (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */); - if (r < 0) - goto finish; + /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all + * privileges are already dropped. */ + if (getuid() == 0) { + + /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */ + r = drop_privileges(uid, gid, + (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */ + (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */ + (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */); + if (r < 0) + goto finish; + } assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, -1) >= 0); diff --git a/units/systemd-resolved.service.in b/units/systemd-resolved.service.in index 9821e1cfee7..ba8d3f6bb16 100644 --- a/units/systemd-resolved.service.in +++ b/units/systemd-resolved.service.in @@ -19,9 +19,11 @@ Wants=nss-lookup.target Type=notify Restart=always RestartSec=0 -ExecStart=@rootlibexecdir@/systemd-resolved +ExecStart=!!@rootlibexecdir@/systemd-resolved WatchdogSec=3min -CapabilityBoundingSet=CAP_SETUID CAP_SETGID CAP_SETPCAP CAP_CHOWN CAP_DAC_OVERRIDE CAP_FOWNER CAP_NET_RAW CAP_NET_BIND_SERVICE +User=systemd-resolve +CapabilityBoundingSet=CAP_SETPCAP CAP_NET_RAW CAP_NET_BIND_SERVICE +AmbientCapabilities=CAP_SETPCAP CAP_NET_RAW CAP_NET_BIND_SERVICE PrivateTmp=yes PrivateDevices=yes ProtectSystem=strict @@ -34,7 +36,8 @@ RestrictRealtime=yes RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6 SystemCallFilter=~@clock @cpu-emulation @debug @keyring @module @mount @obsolete @raw-io @reboot @swap SystemCallArchitectures=native -ReadWritePaths=/run/systemd +RuntimeDirectory=systemd/resolve +RuntimeDirectoryPreserve=yes [Install] WantedBy=multi-user.target -- 2.47.3