f8452f
From 11f5677752f9b78239214b3064e5a2c3712d71b1 Mon Sep 17 00:00:00 2001
f8452f
From: Lennart Poettering <lennart@poettering.net>
f8452f
Date: Wed, 20 Mar 2019 20:19:38 +0100
f8452f
Subject: [PATCH] core: imply NNP and SUID/SGID restriction for DynamicUser=yes
f8452f
 service
f8452f
f8452f
Let's be safe, rather than sorry. This way DynamicUser=yes services can
f8452f
neither take benefit of, nor create SUID/SGID binaries.
f8452f
f8452f
Given that DynamicUser= is a recent addition only we should be able to
f8452f
get away with turning this on, even though this is strictly speaking a
f8452f
binary compatibility breakage.
f8452f
f8452f
(cherry picked from commit bf65b7e0c9fc215897b676ab9a7c9d1c688143ba)
f8452f
Resolves: #1687512
f8452f
---
f8452f
 man/systemd.exec.xml | 16 ++++++++++------
f8452f
 src/core/unit.c      | 10 ++++++++--
f8452f
 2 files changed, 18 insertions(+), 8 deletions(-)
f8452f
f8452f
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
f8452f
index 45ed1864f8..bdaed68162 100644
f8452f
--- a/man/systemd.exec.xml
f8452f
+++ b/man/systemd.exec.xml
f8452f
@@ -229,7 +229,9 @@
f8452f
         created by the executed processes is bound to the runtime of the service, and hence the lifetime of the dynamic
f8452f
         user/group. Since <filename>/tmp</filename> and <filename>/var/tmp</filename> are usually the only
f8452f
         world-writable directories on a system this ensures that a unit making use of dynamic user/group allocation
f8452f
-        cannot leave files around after unit termination. Moreover <varname>ProtectSystem=strict</varname> and
f8452f
+        cannot leave files around after unit termination. Furthermore <varname>NoNewPrivileges=</varname> and
f8452f
+        <varname>RestrictSUIDSGID=</varname> are implicitly enabled to ensure that processes invoked cannot take benefit
f8452f
+        or create SUID/SGID files or directories. Moreover <varname>ProtectSystem=strict</varname> and
f8452f
         <varname>ProtectHome=read-only</varname> are implied, thus prohibiting the service to write to arbitrary file
f8452f
         system locations. In order to allow the service to write to certain directories, they have to be whitelisted
f8452f
         using <varname>ReadWritePaths=</varname>, but care must be taken so that UID/GID recycling doesn't create
f8452f
@@ -357,11 +359,12 @@ CapabilityBoundingSet=~CAP_B CAP_C</programlisting>
f8452f
         <varname>RestrictAddressFamilies=</varname>, <varname>RestrictNamespaces=</varname>,
f8452f
         <varname>PrivateDevices=</varname>, <varname>ProtectKernelTunables=</varname>,
f8452f
         <varname>ProtectKernelModules=</varname>, <varname>MemoryDenyWriteExecute=</varname>,
f8452f
-        <varname>RestrictRealtime=</varname>, <varname>RestrictSUIDSGID=</varname> or
f8452f
-        <varname>LockPersonality=</varname> are specified. Note that even if this setting is overridden by
f8452f
-        them, <command>systemctl show</command> shows the original value of this setting. Also see 
f8452f
+        <varname>RestrictRealtime=</varname>, <varname>RestrictSUIDSGID=</varname>,
f8452f
+        <varname>DynamicUser=</varname> or <varname>LockPersonality=</varname> are specified. Note that even
f8452f
+        if this setting is overridden by them, <command>systemctl show</command> shows the original value of
f8452f
+        this setting. Also see 
f8452f
         url="https://www.kernel.org/doc/html/latest/userspace-api/no_new_privs.html">No New Privileges
f8452f
-        Flag</ulink>.  </para></listitem>
f8452f
+        Flag</ulink>.</para></listitem>
f8452f
       </varlistentry>
f8452f
 
f8452f
       <varlistentry>
f8452f
@@ -1288,7 +1291,8 @@ RestrictNamespaces=~cgroup net</programlisting>
f8452f
         identity of other users, it is recommended to restrict creation of SUID/SGID files to the few
f8452f
         programs that actually require them. Note that this restricts marking of any type of file system
f8452f
         object with these bits, including both regular files and directories (where the SGID is a different
f8452f
-        meaning than for files, see documentation). Defaults to off.</para></listitem>
f8452f
+        meaning than for files, see documentation). This option is implied if <varname>DynamicUser=</varname>
f8452f
+        is enabled. Defaults to off.</para></listitem>
f8452f
       </varlistentry>
f8452f
 
f8452f
       <varlistentry>
f8452f
diff --git a/src/core/unit.c b/src/core/unit.c
f8452f
index 115739f4c6..e1f5e6f7bd 100644
f8452f
--- a/src/core/unit.c
f8452f
+++ b/src/core/unit.c
f8452f
@@ -4161,14 +4161,20 @@ int unit_patch_contexts(Unit *u) {
f8452f
                                         return -ENOMEM;
f8452f
                         }
f8452f
 
f8452f
-                        /* If the dynamic user option is on, let's make sure that the unit can't leave its UID/GID
f8452f
-                         * around in the file system or on IPC objects. Hence enforce a strict sandbox. */
f8452f
+                        /* If the dynamic user option is on, let's make sure that the unit can't leave its
f8452f
+                         * UID/GID around in the file system or on IPC objects. Hence enforce a strict
f8452f
+                         * sandbox. */
f8452f
 
f8452f
                         ec->private_tmp = true;
f8452f
                         ec->remove_ipc = true;
f8452f
                         ec->protect_system = PROTECT_SYSTEM_STRICT;
f8452f
                         if (ec->protect_home == PROTECT_HOME_NO)
f8452f
                                 ec->protect_home = PROTECT_HOME_READ_ONLY;
f8452f
+
f8452f
+                        /* Make sure this service can neither benefit from SUID/SGID binaries nor create
f8452f
+                         * them. */
f8452f
+                        ec->no_new_privileges = true;
f8452f
+                        ec->restrict_suid_sgid = true;
f8452f
                 }
f8452f
         }
f8452f