c2dfb7
From f79283a86531e3bbf0854b5f126b7b291fadfb43 Mon Sep 17 00:00:00 2001
c2dfb7
From: Lennart Poettering <lennart@poettering.net>
c2dfb7
Date: Wed, 20 Mar 2019 19:09:09 +0100
c2dfb7
Subject: [PATCH] core: expose SUID/SGID restriction as new unit setting
c2dfb7
 RestrictSUIDSGID=
c2dfb7
c2dfb7
(cherry picked from commit f69567cbe26d09eac9d387c0be0fc32c65a83ada)
c2dfb7
Related: #1687512
c2dfb7
---
c2dfb7
 src/core/dbus-execute.c                     |  4 ++++
c2dfb7
 src/core/execute.c                          | 22 +++++++++++++++++++++
c2dfb7
 src/core/execute.h                          |  1 +
c2dfb7
 src/core/load-fragment-gperf.gperf.m4       |  2 ++
c2dfb7
 src/shared/bus-unit-util.c                  |  2 +-
c2dfb7
 test/fuzz/fuzz-unit-file/directives.service |  1 +
c2dfb7
 6 files changed, 31 insertions(+), 1 deletion(-)
c2dfb7
c2dfb7
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
c2dfb7
index 198f149210..e7c0b893d1 100644
c2dfb7
--- a/src/core/dbus-execute.c
c2dfb7
+++ b/src/core/dbus-execute.c
c2dfb7
@@ -815,6 +815,7 @@ const sd_bus_vtable bus_exec_vtable[] = {
c2dfb7
         SD_BUS_PROPERTY("ConfigurationDirectory", "as", NULL, offsetof(ExecContext, directories[EXEC_DIRECTORY_CONFIGURATION].paths), SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
         SD_BUS_PROPERTY("MemoryDenyWriteExecute", "b", bus_property_get_bool, offsetof(ExecContext, memory_deny_write_execute), SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
         SD_BUS_PROPERTY("RestrictRealtime", "b", bus_property_get_bool, offsetof(ExecContext, restrict_realtime), SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
+        SD_BUS_PROPERTY("RestrictSUIDSGID", "b", bus_property_get_bool, offsetof(ExecContext, restrict_suid_sgid), SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
         SD_BUS_PROPERTY("RestrictNamespaces", "t", bus_property_get_ulong, offsetof(ExecContext, restrict_namespaces), SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
         SD_BUS_PROPERTY("BindPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
         SD_BUS_PROPERTY("BindReadOnlyPaths", "a(ssbt)", property_get_bind_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
c2dfb7
@@ -1179,6 +1180,9 @@ int bus_exec_context_set_transient_property(
c2dfb7
         if (streq(name, "RestrictRealtime"))
c2dfb7
                 return bus_set_transient_bool(u, name, &c->restrict_realtime, message, flags, error);
c2dfb7
 
c2dfb7
+        if (streq(name, "RestrictSUIDSGID"))
c2dfb7
+                return bus_set_transient_bool(u, name, &c->restrict_suid_sgid, message, flags, error);
c2dfb7
+
c2dfb7
         if (streq(name, "DynamicUser"))
c2dfb7
                 return bus_set_transient_bool(u, name, &c->dynamic_user, message, flags, error);
c2dfb7
 
c2dfb7
diff --git a/src/core/execute.c b/src/core/execute.c
c2dfb7
index 56aa89e1ec..f012023224 100644
c2dfb7
--- a/src/core/execute.c
c2dfb7
+++ b/src/core/execute.c
c2dfb7
@@ -1366,6 +1366,7 @@ static bool context_has_no_new_privileges(const ExecContext *c) {
c2dfb7
         return context_has_address_families(c) ||
c2dfb7
                 c->memory_deny_write_execute ||
c2dfb7
                 c->restrict_realtime ||
c2dfb7
+                c->restrict_suid_sgid ||
c2dfb7
                 exec_context_restrict_namespaces_set(c) ||
c2dfb7
                 c->protect_kernel_tunables ||
c2dfb7
                 c->protect_kernel_modules ||
c2dfb7
@@ -1470,6 +1471,19 @@ static int apply_restrict_realtime(const Unit* u, const ExecContext *c) {
c2dfb7
         return seccomp_restrict_realtime();
c2dfb7
 }
c2dfb7
 
c2dfb7
+static int apply_restrict_suid_sgid(const Unit* u, const ExecContext *c) {
c2dfb7
+        assert(u);
c2dfb7
+        assert(c);
c2dfb7
+
c2dfb7
+        if (!c->restrict_suid_sgid)
c2dfb7
+                return 0;
c2dfb7
+
c2dfb7
+        if (skip_seccomp_unavailable(u, "RestrictSUIDSGID="))
c2dfb7
+                return 0;
c2dfb7
+
c2dfb7
+        return seccomp_restrict_suid_sgid();
c2dfb7
+}
c2dfb7
+
c2dfb7
 static int apply_protect_sysctl(const Unit *u, const ExecContext *c) {
c2dfb7
         assert(u);
c2dfb7
         assert(c);
c2dfb7
@@ -3404,6 +3418,12 @@ static int exec_child(
c2dfb7
                         return log_unit_error_errno(unit, r, "Failed to apply realtime restrictions: %m");
c2dfb7
                 }
c2dfb7
 
c2dfb7
+                r = apply_restrict_suid_sgid(unit, context);
c2dfb7
+                if (r < 0) {
c2dfb7
+                        *exit_status = EXIT_SECCOMP;
c2dfb7
+                        return log_unit_error_errno(unit, r, "Failed to apply SUID/SGID restrictions: %m");
c2dfb7
+                }
c2dfb7
+
c2dfb7
                 r = apply_restrict_namespaces(unit, context);
c2dfb7
                 if (r < 0) {
c2dfb7
                         *exit_status = EXIT_SECCOMP;
c2dfb7
@@ -4023,6 +4043,7 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
c2dfb7
                 "%sIgnoreSIGPIPE: %s\n"
c2dfb7
                 "%sMemoryDenyWriteExecute: %s\n"
c2dfb7
                 "%sRestrictRealtime: %s\n"
c2dfb7
+                "%sRestrictSUIDSGID: %s\n"
c2dfb7
                 "%sKeyringMode: %s\n",
c2dfb7
                 prefix, c->umask,
c2dfb7
                 prefix, c->working_directory ? c->working_directory : "/",
c2dfb7
@@ -4041,6 +4062,7 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
c2dfb7
                 prefix, yes_no(c->ignore_sigpipe),
c2dfb7
                 prefix, yes_no(c->memory_deny_write_execute),
c2dfb7
                 prefix, yes_no(c->restrict_realtime),
c2dfb7
+                prefix, yes_no(c->restrict_suid_sgid),
c2dfb7
                 prefix, exec_keyring_mode_to_string(c->keyring_mode));
c2dfb7
 
c2dfb7
         if (c->root_image)
c2dfb7
diff --git a/src/core/execute.h b/src/core/execute.h
c2dfb7
index b2eb55f8f5..2266355962 100644
c2dfb7
--- a/src/core/execute.h
c2dfb7
+++ b/src/core/execute.h
c2dfb7
@@ -245,6 +245,7 @@ struct ExecContext {
c2dfb7
          * that the autofs logic detects that it belongs to us and we
c2dfb7
          * don't enter a trigger loop. */
c2dfb7
         bool same_pgrp;
c2dfb7
+        bool restrict_suid_sgid;
c2dfb7
 
c2dfb7
         unsigned long personality;
c2dfb7
         bool lock_personality;
c2dfb7
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
c2dfb7
index cdf4d14c4e..49e938d0ce 100644
c2dfb7
--- a/src/core/load-fragment-gperf.gperf.m4
c2dfb7
+++ b/src/core/load-fragment-gperf.gperf.m4
c2dfb7
@@ -76,6 +76,7 @@ $1.SystemCallErrorNumber,        config_parse_syscall_errno,         0,
c2dfb7
 $1.MemoryDenyWriteExecute,       config_parse_bool,                  0,                             offsetof($1, exec_context.memory_deny_write_execute)
c2dfb7
 $1.RestrictNamespaces,           config_parse_restrict_namespaces,   0,                             offsetof($1, exec_context)
c2dfb7
 $1.RestrictRealtime,             config_parse_bool,                  0,                             offsetof($1, exec_context.restrict_realtime)
c2dfb7
+$1.RestrictSUIDSGID,             config_parse_bool,                  0,                             offsetof($1, exec_context.restrict_suid_sgid)
c2dfb7
 $1.RestrictAddressFamilies,      config_parse_address_families,      0,                             offsetof($1, exec_context)
c2dfb7
 $1.LockPersonality,              config_parse_bool,                  0,                             offsetof($1, exec_context.lock_personality)',
c2dfb7
 `$1.SystemCallFilter,            config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
c2dfb7
@@ -84,6 +85,7 @@ $1.SystemCallErrorNumber,        config_parse_warn_compat,           DISABLED_CO
c2dfb7
 $1.MemoryDenyWriteExecute,       config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
c2dfb7
 $1.RestrictNamespaces,           config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
c2dfb7
 $1.RestrictRealtime,             config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
c2dfb7
+$1.RestrictSUIDSGID,             config_parse_warn_compat,           DISABLED_CONFIGURATION         0
c2dfb7
 $1.RestrictAddressFamilies,      config_parse_warn_compat,           DISABLED_CONFIGURATION,        0
c2dfb7
 $1.LockPersonality,              config_parse_warn_compat,           DISABLED_CONFIGURATION,        0')
c2dfb7
 $1.LimitCPU,                     config_parse_rlimit,                RLIMIT_CPU,                    offsetof($1, exec_context.rlimit)
c2dfb7
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
c2dfb7
index 055edd6e22..3c42e97b7a 100644
c2dfb7
--- a/src/shared/bus-unit-util.c
c2dfb7
+++ b/src/shared/bus-unit-util.c
c2dfb7
@@ -697,7 +697,7 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
c2dfb7
                        "PrivateMounts", "NoNewPrivileges", "SyslogLevelPrefix",
c2dfb7
                        "MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
c2dfb7
                        "ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups",
c2dfb7
-                       "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality"))
c2dfb7
+                       "MountAPIVFS", "CPUSchedulingResetOnFork", "LockPersonality" "RestrictSUIDSGID"))
c2dfb7
 
c2dfb7
                 return bus_append_parse_boolean(m, field, eq);
c2dfb7
 
c2dfb7
diff --git a/test/fuzz/fuzz-unit-file/directives.service b/test/fuzz/fuzz-unit-file/directives.service
c2dfb7
index d8d1fc68b8..eab1820e20 100644
c2dfb7
--- a/test/fuzz/fuzz-unit-file/directives.service
c2dfb7
+++ b/test/fuzz/fuzz-unit-file/directives.service
c2dfb7
@@ -849,6 +849,7 @@ ReserveVT=
c2dfb7
 RestrictAddressFamilies=
c2dfb7
 RestrictNamespaces=
c2dfb7
 RestrictRealtime=
c2dfb7
+RestrictSUIDSGID=
c2dfb7
 RuntimeDirectory=
c2dfb7
 RuntimeDirectoryMode=
c2dfb7
 RuntimeDirectoryPreserve=