dcavalca / rpms / systemd

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