ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
65878a
From 6744ef720b34a87ff69acb8535cf5e83db6f5e8b Mon Sep 17 00:00:00 2001
65878a
From: Michal Sekletar <msekleta@redhat.com>
65878a
Date: Tue, 24 Jun 2014 14:50:26 +0200
65878a
Subject: [PATCH] socket: add SocketUser= and SocketGroup= for chown()ing
65878a
 sockets in the file system
65878a
65878a
This is relatively complex, as we cannot invoke NSS from PID 1, and thus
65878a
need to fork a helper process temporarily.
65878a
65878a
(cherry picked from commit 3900e5fdff688dc3c273f177d9d913b7389d5561)
65878a
65878a
Resolves: #1111761
65878a
---
65878a
 man/systemd.socket.xml                |  38 ++++--
65878a
 src/core/dbus-socket.c                |   8 +-
65878a
 src/core/load-fragment-gperf.gperf.m4 |   4 +-
65878a
 src/core/socket.c                     | 220 +++++++++++++++++++++++++++-------
65878a
 src/core/socket.h                     |   5 +
65878a
 src/shared/exit-status.c              |   4 +
65878a
 src/shared/exit-status.h              |   3 +-
65878a
 7 files changed, 224 insertions(+), 58 deletions(-)
65878a
65878a
diff --git a/man/systemd.socket.xml b/man/systemd.socket.xml
65878a
index 570a6fb..d146b3b 100644
65878a
--- a/man/systemd.socket.xml
65878a
+++ b/man/systemd.socket.xml
65878a
@@ -372,16 +372,21 @@
65878a
                         </varlistentry>
65878a
 
65878a
                         <varlistentry>
65878a
-                                <term><varname>DirectoryMode=</varname></term>
65878a
-                                <listitem><para>If listening on a file
65878a
-                                system socket or FIFO, the parent
65878a
-                                directories are automatically created
65878a
-                                if needed. This option specifies the
65878a
-                                file system access mode used when
65878a
-                                creating these directories. Takes an
65878a
-                                access mode in octal
65878a
-                                notation. Defaults to
65878a
-                                0755.</para></listitem>
65878a
+                                <term><varname>SocketUser=</varname></term>
65878a
+                                <term><varname>SocketGroup=</varname></term>
65878a
+
65878a
+                                <listitem><para>Takes a UNIX
65878a
+                                user/group name. When specified
65878a
+                                all AF_UNIX sockets and FIFO nodes in
65878a
+                                the file system are owned by the
65878a
+                                specified user and group. If unset
65878a
+                                (the default), the nodes are owned by
65878a
+                                the root user/group (if run in system
65878a
+                                context) or the invoking user/group
65878a
+                                (if run in user context). If only a
65878a
+                                user is specified but no group, then
65878a
+                                the group is derived from the user's
65878a
+                                default group.</para></listitem>
65878a
                         </varlistentry>
65878a
 
65878a
                         <varlistentry>
65878a
@@ -396,6 +401,19 @@
65878a
                         </varlistentry>
65878a
 
65878a
                         <varlistentry>
65878a
+                                <term><varname>DirectoryMode=</varname></term>
65878a
+                                <listitem><para>If listening on a file
65878a
+                                system socket or FIFO, the parent
65878a
+                                directories are automatically created
65878a
+                                if needed. This option specifies the
65878a
+                                file system access mode used when
65878a
+                                creating these directories. Takes an
65878a
+                                access mode in octal
65878a
+                                notation. Defaults to
65878a
+                                0755.</para></listitem>
65878a
+                        </varlistentry>
65878a
+
65878a
+                        <varlistentry>
65878a
                                 <term><varname>Accept=</varname></term>
65878a
                                 <listitem><para>Takes a boolean
65878a
                                 argument. If true, a service instance
65878a
diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c
65878a
index 30c4b63..870bcdc 100644
65878a
--- a/src/core/dbus-socket.c
65878a
+++ b/src/core/dbus-socket.c
65878a
@@ -44,8 +44,10 @@
65878a
         BUS_CGROUP_CONTEXT_INTERFACE                                    \
65878a
         "  <property name=\"ControlPID\" type=\"u\" access=\"read\"/>\n" \
65878a
         "  <property name=\"BindToDevice\" type=\"s\" access=\"read\"/>\n" \
65878a
-        "  <property name=\"DirectoryMode\" type=\"u\" access=\"read\"/>\n" \
65878a
+        "  <property name=\"SocketUser\" type=\"s\" access=\"read\"/>\n" \
65878a
+        "  <property name=\"SocketGroup\" type=\"s\" access=\"read\"/>\n" \
65878a
         "  <property name=\"SocketMode\" type=\"u\" access=\"read\"/>\n" \
65878a
+        "  <property name=\"DirectoryMode\" type=\"u\" access=\"read\"/>\n" \
65878a
         "  <property name=\"Accept\" type=\"b\" access=\"read\"/>\n"    \
65878a
         "  <property name=\"KeepAlive\" type=\"b\" access=\"read\"/>\n" \
65878a
         "  <property name=\"Priority\" type=\"i\" access=\"read\"/>\n"  \
65878a
@@ -172,8 +174,10 @@ static const BusProperty bus_socket_properties[] = {
65878a
         BUS_EXEC_COMMAND_PROPERTY("ExecStopPost",  offsetof(Socket, exec_command[SOCKET_EXEC_STOP_POST]),  true ),
65878a
         { "ControlPID",     bus_property_append_pid,           "u", offsetof(Socket, control_pid)     },
65878a
         { "BindToDevice",   bus_property_append_string,        "s", offsetof(Socket, bind_to_device), true },
65878a
-        { "DirectoryMode",  bus_property_append_mode,          "u", offsetof(Socket, directory_mode)  },
65878a
+        { "SocketUser",     bus_property_append_string,        "s", offsetof(Socket, user),           true },
65878a
+        { "SocketGroup",    bus_property_append_string,        "s", offsetof(Socket, group),          true },
65878a
         { "SocketMode",     bus_property_append_mode,          "u", offsetof(Socket, socket_mode)     },
65878a
+        { "DirectoryMode",  bus_property_append_mode,          "u", offsetof(Socket, directory_mode)  },
65878a
         { "Accept",         bus_property_append_bool,          "b", offsetof(Socket, accept)          },
65878a
         { "KeepAlive",      bus_property_append_bool,          "b", offsetof(Socket, keep_alive)      },
65878a
         { "Priority",       bus_property_append_int,           "i", offsetof(Socket, priority)        },
65878a
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
65878a
index d65bcc1..0991cb9 100644
65878a
--- a/src/core/load-fragment-gperf.gperf.m4
65878a
+++ b/src/core/load-fragment-gperf.gperf.m4
65878a
@@ -193,8 +193,10 @@ Socket.ExecStartPost,            config_parse_exec,                  SOCKET_EXEC
65878a
 Socket.ExecStopPre,              config_parse_exec,                  SOCKET_EXEC_STOP_PRE,          offsetof(Socket, exec_command)
65878a
 Socket.ExecStopPost,             config_parse_exec,                  SOCKET_EXEC_STOP_POST,         offsetof(Socket, exec_command)
65878a
 Socket.TimeoutSec,               config_parse_sec,                   0,                             offsetof(Socket, timeout_usec)
65878a
-Socket.DirectoryMode,            config_parse_mode,                  0,                             offsetof(Socket, directory_mode)
65878a
+Socket.SocketUser,               config_parse_unit_string_printf,    0,                             offsetof(Socket, user)
65878a
+Socket.SocketGroup,              config_parse_unit_string_printf,    0,                             offsetof(Socket, group)
65878a
 Socket.SocketMode,               config_parse_mode,                  0,                             offsetof(Socket, socket_mode)
65878a
+Socket.DirectoryMode,            config_parse_mode,                  0,                             offsetof(Socket, directory_mode)
65878a
 Socket.Accept,                   config_parse_bool,                  0,                             offsetof(Socket, accept)
65878a
 Socket.MaxConnections,           config_parse_unsigned,              0,                             offsetof(Socket, max_connections)
65878a
 Socket.KeepAlive,                config_parse_bool,                  0,                             offsetof(Socket, keep_alive)
65878a
diff --git a/src/core/socket.c b/src/core/socket.c
65878a
index e673f38..32e0d35 100644
65878a
--- a/src/core/socket.c
65878a
+++ b/src/core/socket.c
65878a
@@ -54,6 +54,7 @@
65878a
 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
65878a
         [SOCKET_DEAD] = UNIT_INACTIVE,
65878a
         [SOCKET_START_PRE] = UNIT_ACTIVATING,
65878a
+        [SOCKET_START_CHOWN] = UNIT_ACTIVATING,
65878a
         [SOCKET_START_POST] = UNIT_ACTIVATING,
65878a
         [SOCKET_LISTENING] = UNIT_ACTIVE,
65878a
         [SOCKET_RUNNING] = UNIT_ACTIVE,
65878a
@@ -147,6 +148,8 @@ static void socket_done(Unit *u) {
65878a
         free(s->smack);
65878a
         free(s->smack_ip_in);
65878a
         free(s->smack_ip_out);
65878a
+        free(s->user);
65878a
+        free(s->group);
65878a
 
65878a
         unit_unwatch_timer(u, &s->timer_watch);
65878a
 }
65878a
@@ -527,6 +530,16 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
65878a
                         "%sSmackLabelIPOut: %s\n",
65878a
                         prefix, s->smack_ip_out);
65878a
 
65878a
+        if (s->user)
65878a
+                fprintf(f,
65878a
+                        "%sOwnerUser: %s\n",
65878a
+                        prefix, s->user);
65878a
+
65878a
+        if (s->group)
65878a
+                fprintf(f,
65878a
+                        "%sOwnerGroup: %s\n",
65878a
+                        prefix, s->user);
65878a
+
65878a
         LIST_FOREACH(port, p, s->ports) {
65878a
 
65878a
                 if (p->type == SOCKET_SOCKET) {
65878a
@@ -1100,6 +1113,7 @@ static void socket_set_state(Socket *s, SocketState state) {
65878a
         s->state = state;
65878a
 
65878a
         if (state != SOCKET_START_PRE &&
65878a
+            state != SOCKET_START_CHOWN &&
65878a
             state != SOCKET_START_POST &&
65878a
             state != SOCKET_STOP_PRE &&
65878a
             state != SOCKET_STOP_PRE_SIGTERM &&
65878a
@@ -1116,7 +1130,8 @@ static void socket_set_state(Socket *s, SocketState state) {
65878a
         if (state != SOCKET_LISTENING)
65878a
                 socket_unwatch_fds(s);
65878a
 
65878a
-        if (state != SOCKET_START_POST &&
65878a
+        if (state != SOCKET_START_CHOWN &&
65878a
+            state != SOCKET_START_POST &&
65878a
             state != SOCKET_LISTENING &&
65878a
             state != SOCKET_RUNNING &&
65878a
             state != SOCKET_STOP_PRE &&
65878a
@@ -1143,6 +1158,7 @@ static int socket_coldplug(Unit *u) {
65878a
         if (s->deserialized_state != s->state) {
65878a
 
65878a
                 if (s->deserialized_state == SOCKET_START_PRE ||
65878a
+                    s->deserialized_state == SOCKET_START_CHOWN ||
65878a
                     s->deserialized_state == SOCKET_START_POST ||
65878a
                     s->deserialized_state == SOCKET_STOP_PRE ||
65878a
                     s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
65878a
@@ -1163,7 +1179,8 @@ static int socket_coldplug(Unit *u) {
65878a
                                 return r;
65878a
                 }
65878a
 
65878a
-                if (s->deserialized_state == SOCKET_START_POST ||
65878a
+                if (s->deserialized_state == SOCKET_START_CHOWN ||
65878a
+                    s->deserialized_state == SOCKET_START_POST ||
65878a
                     s->deserialized_state == SOCKET_LISTENING ||
65878a
                     s->deserialized_state == SOCKET_RUNNING ||
65878a
                     s->deserialized_state == SOCKET_STOP_PRE ||
65878a
@@ -1185,7 +1202,7 @@ static int socket_coldplug(Unit *u) {
65878a
 static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
65878a
         pid_t pid;
65878a
         int r;
65878a
-        char **argv;
65878a
+        _cleanup_strv_free_ char **argv = NULL;
65878a
 
65878a
         assert(s);
65878a
         assert(c);
65878a
@@ -1216,11 +1233,11 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
65878a
                        NULL,
65878a
                        &pid;;
65878a
 
65878a
-        strv_free(argv);
65878a
         if (r < 0)
65878a
                 goto fail;
65878a
 
65878a
-        if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
65878a
+        r = unit_watch_pid(UNIT(s), pid);
65878a
+        if (r < 0)
65878a
                 /* FIXME: we need to do something here */
65878a
                 goto fail;
65878a
 
65878a
@@ -1234,6 +1251,88 @@ fail:
65878a
         return r;
65878a
 }
65878a
 
65878a
+static int socket_chown(Socket *s, pid_t *_pid) {
65878a
+        pid_t pid;
65878a
+        int r;
65878a
+
65878a
+        r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
65878a
+        if (r < 0)
65878a
+                goto fail;
65878a
+
65878a
+        pid = fork();
65878a
+        if (pid < 0)
65878a
+                return -errno;
65878a
+
65878a
+        if (pid == 0) {
65878a
+                SocketPort *p;
65878a
+                uid_t uid = (uid_t) -1;
65878a
+                gid_t gid = (gid_t) -1;
65878a
+                int ret;
65878a
+
65878a
+                default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
65878a
+                ignore_signals(SIGPIPE, -1);
65878a
+                log_forget_fds();
65878a
+
65878a
+                if (!isempty(s->user)) {
65878a
+                        const char *user = s->user;
65878a
+
65878a
+                        r = get_user_creds(&user, &uid, &gid, NULL, NULL);
65878a
+                        if (r < 0) {
65878a
+                                ret = EXIT_USER;
65878a
+                                goto fail_child;
65878a
+                        }
65878a
+                }
65878a
+
65878a
+                if (!isempty(s->group)) {
65878a
+                        const char *group = s->group;
65878a
+
65878a
+                        r = get_group_creds(&group, &gid;;
65878a
+                        if (r < 0) {
65878a
+                                ret = EXIT_GROUP;
65878a
+                                goto fail_child;
65878a
+                        }
65878a
+                }
65878a
+
65878a
+                LIST_FOREACH(port, p, s->ports) {
65878a
+                        const char *path = NULL;
65878a
+
65878a
+                        if (p->type == SOCKET_SOCKET)
65878a
+                                path = socket_address_get_path(&p->address);
65878a
+                        else if (p->type == SOCKET_FIFO)
65878a
+                                path = p->path;
65878a
+
65878a
+                        if (!path)
65878a
+                                continue;
65878a
+
65878a
+                        if (chown(path, uid, gid) < 0) {
65878a
+                                r = -errno;
65878a
+                                ret = EXIT_CHOWN;
65878a
+                                goto fail_child;
65878a
+                        }
65878a
+                }
65878a
+
65878a
+                _exit(0);
65878a
+
65878a
+        fail_child:
65878a
+                log_open();
65878a
+                log_error("Failed to chown socket at step %s: %s", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD), strerror(-r));
65878a
+
65878a
+                _exit(ret);
65878a
+        }
65878a
+
65878a
+        r = unit_watch_pid(UNIT(s), pid);
65878a
+        if (r < 0)
65878a
+                goto fail;
65878a
+
65878a
+        *_pid = pid;
65878a
+
65878a
+        return 0;
65878a
+
65878a
+fail:
65878a
+        unit_unwatch_timer(UNIT(s), &s->timer_watch);
65878a
+        return r;
65878a
+}
65878a
+
65878a
 static void socket_enter_dead(Socket *s, SocketResult f) {
65878a
         assert(s);
65878a
 
65878a
@@ -1256,9 +1355,11 @@ static void socket_enter_stop_post(Socket *s, SocketResult f) {
65878a
         socket_unwatch_control_pid(s);
65878a
 
65878a
         s->control_command_id = SOCKET_EXEC_STOP_POST;
65878a
+        s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
65878a
 
65878a
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
65878a
-                if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
65878a
+        if (s->control_command) {
65878a
+                r = socket_spawn(s, s->control_command, &s->control_pid);
65878a
+                if (r < 0)
65878a
                         goto fail;
65878a
 
65878a
                 socket_set_state(s, SOCKET_STOP_POST);
65878a
@@ -1326,9 +1427,11 @@ static void socket_enter_stop_pre(Socket *s, SocketResult f) {
65878a
         socket_unwatch_control_pid(s);
65878a
 
65878a
         s->control_command_id = SOCKET_EXEC_STOP_PRE;
65878a
+        s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
65878a
 
65878a
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
65878a
-                if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
65878a
+        if (s->control_command) {
65878a
+                r = socket_spawn(s, s->control_command, &s->control_pid);
65878a
+                if (r < 0)
65878a
                         goto fail;
65878a
 
65878a
                 socket_set_state(s, SOCKET_STOP_PRE);
65878a
@@ -1376,15 +1479,13 @@ static void socket_enter_start_post(Socket *s) {
65878a
         }
65878a
 
65878a
         socket_unwatch_control_pid(s);
65878a
-
65878a
         s->control_command_id = SOCKET_EXEC_START_POST;
65878a
+        s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
65878a
 
65878a
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
65878a
+        if (s->control_command) {
65878a
                 r = socket_spawn(s, s->control_command, &s->control_pid);
65878a
                 if (r < 0) {
65878a
-                        log_warning_unit(UNIT(s)->id,
65878a
-                                         "%s failed to run 'start-post' task: %s",
65878a
-                                         UNIT(s)->id, strerror(-r));
65878a
+                        log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
65878a
                         goto fail;
65878a
                 }
65878a
 
65878a
@@ -1398,6 +1499,38 @@ fail:
65878a
         socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
65878a
 }
65878a
 
65878a
+static void socket_enter_start_chown(Socket *s) {
65878a
+        int r;
65878a
+
65878a
+        assert(s);
65878a
+
65878a
+        r = socket_open_fds(s);
65878a
+        if (r < 0) {
65878a
+                log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r));
65878a
+                goto fail;
65878a
+        }
65878a
+
65878a
+        if (!isempty(s->user) || !isempty(s->group)) {
65878a
+                socket_unwatch_control_pid(s);
65878a
+                s->control_command_id = SOCKET_EXEC_START_CHOWN;
65878a
+                s->control_command = NULL;
65878a
+
65878a
+                r = socket_chown(s, &s->control_pid);
65878a
+                if (r < 0) {
65878a
+                        log_warning_unit(UNIT(s)->id, "%s failed to fork 'start-chown' task: %s", UNIT(s)->id, strerror(-r));
65878a
+                        goto fail;
65878a
+                }
65878a
+
65878a
+                socket_set_state(s, SOCKET_START_CHOWN);
65878a
+        } else
65878a
+                socket_enter_start_post(s);
65878a
+
65878a
+        return;
65878a
+
65878a
+fail:
65878a
+        socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
65878a
+}
65878a
+
65878a
 static void socket_enter_start_pre(Socket *s) {
65878a
         int r;
65878a
         assert(s);
65878a
@@ -1405,21 +1538,21 @@ static void socket_enter_start_pre(Socket *s) {
65878a
         socket_unwatch_control_pid(s);
65878a
 
65878a
         s->control_command_id = SOCKET_EXEC_START_PRE;
65878a
+        s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
65878a
 
65878a
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
65878a
-                if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
65878a
+        if (s->control_command) {
65878a
+                r = socket_spawn(s, s->control_command, &s->control_pid);
65878a
+                if (r < 0)
65878a
                         goto fail;
65878a
 
65878a
                 socket_set_state(s, SOCKET_START_PRE);
65878a
         } else
65878a
-                socket_enter_start_post(s);
65878a
+                socket_enter_start_chown(s);
65878a
 
65878a
         return;
65878a
 
65878a
 fail:
65878a
-        log_warning_unit(UNIT(s)->id,
65878a
-                         "%s failed to run 'start-pre' task: %s",
65878a
-                         UNIT(s)->id, strerror(-r));
65878a
+        log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
65878a
         socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
65878a
 }
65878a
 
65878a
@@ -1609,6 +1742,7 @@ static int socket_start(Unit *u) {
65878a
                 return -EAGAIN;
65878a
 
65878a
         if (s->state == SOCKET_START_PRE ||
65878a
+            s->state == SOCKET_START_CHOWN ||
65878a
             s->state == SOCKET_START_POST)
65878a
                 return 0;
65878a
 
65878a
@@ -1669,6 +1803,7 @@ static int socket_stop(Unit *u) {
65878a
         /* If there's already something running we go directly into
65878a
          * kill mode. */
65878a
         if (s->state == SOCKET_START_PRE ||
65878a
+            s->state == SOCKET_START_CHOWN ||
65878a
             s->state == SOCKET_START_POST) {
65878a
                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
65878a
                 return -EAGAIN;
65878a
@@ -2098,11 +2233,18 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
65878a
 
65878a
                 case SOCKET_START_PRE:
65878a
                         if (f == SOCKET_SUCCESS)
65878a
-                                socket_enter_start_post(s);
65878a
+                                socket_enter_start_chown(s);
65878a
                         else
65878a
                                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f);
65878a
                         break;
65878a
 
65878a
+                case SOCKET_START_CHOWN:
65878a
+                        if (f == SOCKET_SUCCESS)
65878a
+                                socket_enter_start_post(s);
65878a
+                        else
65878a
+                                socket_enter_stop_pre(s, f);
65878a
+                        break;
65878a
+
65878a
                 case SOCKET_START_POST:
65878a
                         if (f == SOCKET_SUCCESS)
65878a
                                 socket_enter_listening(s);
65878a
@@ -2141,65 +2283,53 @@ static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
65878a
         switch (s->state) {
65878a
 
65878a
         case SOCKET_START_PRE:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s starting timed out. Terminating.", u->id);
65878a
+                log_warning_unit(u->id, "%s starting timed out. Terminating.", u->id);
65878a
                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
+        case SOCKET_START_CHOWN:
65878a
         case SOCKET_START_POST:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s starting timed out. Stopping.", u->id);
65878a
+                log_warning_unit(u->id, "%s starting timed out. Stopping.", u->id);
65878a
                 socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
         case SOCKET_STOP_PRE:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s stopping timed out. Terminating.", u->id);
65878a
+                log_warning_unit(u->id, "%s stopping timed out. Terminating.", u->id);
65878a
                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
         case SOCKET_STOP_PRE_SIGTERM:
65878a
                 if (s->kill_context.send_sigkill) {
65878a
-                        log_warning_unit(u->id,
65878a
-                                         "%s stopping timed out. Killing.", u->id);
65878a
+                        log_warning_unit(u->id, "%s stopping timed out. Killing.", u->id);
65878a
                         socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT);
65878a
                 } else {
65878a
-                        log_warning_unit(u->id,
65878a
-                                         "%s stopping timed out. Skipping SIGKILL. Ignoring.",
65878a
-                                         u->id);
65878a
+                        log_warning_unit(u->id, "%s stopping timed out. Skipping SIGKILL. Ignoring.", u->id);
65878a
                         socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
65878a
                 }
65878a
                 break;
65878a
 
65878a
         case SOCKET_STOP_PRE_SIGKILL:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s still around after SIGKILL. Ignoring.", u->id);
65878a
+                log_warning_unit(u->id, "%s still around after SIGKILL. Ignoring.", u->id);
65878a
                 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
         case SOCKET_STOP_POST:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s stopping timed out (2). Terminating.", u->id);
65878a
+                log_warning_unit(u->id, "%s stopping timed out (2). Terminating.", u->id);
65878a
                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
         case SOCKET_FINAL_SIGTERM:
65878a
                 if (s->kill_context.send_sigkill) {
65878a
-                        log_warning_unit(u->id,
65878a
-                                         "%s stopping timed out (2). Killing.", u->id);
65878a
+                        log_warning_unit(u->id, "%s stopping timed out (2). Killing.", u->id);
65878a
                         socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT);
65878a
                 } else {
65878a
-                        log_warning_unit(u->id,
65878a
-                                         "%s stopping timed out (2). Skipping SIGKILL. Ignoring.",
65878a
-                                         u->id);
65878a
+                        log_warning_unit(u->id, "%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->id);
65878a
                         socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
65878a
                 }
65878a
                 break;
65878a
 
65878a
         case SOCKET_FINAL_SIGKILL:
65878a
-                log_warning_unit(u->id,
65878a
-                                 "%s still around after SIGKILL (2). Entering failed mode.",
65878a
-                                 u->id);
65878a
+                log_warning_unit(u->id, "%s still around after SIGKILL (2). Entering failed mode.", u->id);
65878a
                 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
65878a
                 break;
65878a
 
65878a
@@ -2333,6 +2463,7 @@ static int socket_kill(Unit *u, KillWho who, int signo, DBusError *error) {
65878a
 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
65878a
         [SOCKET_DEAD] = "dead",
65878a
         [SOCKET_START_PRE] = "start-pre",
65878a
+        [SOCKET_START_CHOWN] = "start-chown",
65878a
         [SOCKET_START_POST] = "start-post",
65878a
         [SOCKET_LISTENING] = "listening",
65878a
         [SOCKET_RUNNING] = "running",
65878a
@@ -2349,6 +2480,7 @@ DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
65878a
 
65878a
 static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
65878a
         [SOCKET_EXEC_START_PRE] = "StartPre",
65878a
+        [SOCKET_EXEC_START_CHOWN] = "StartChown",
65878a
         [SOCKET_EXEC_START_POST] = "StartPost",
65878a
         [SOCKET_EXEC_STOP_PRE] = "StopPre",
65878a
         [SOCKET_EXEC_STOP_POST] = "StopPost"
65878a
diff --git a/src/core/socket.h b/src/core/socket.h
65878a
index 3d7eadc..6a24883 100644
65878a
--- a/src/core/socket.h
65878a
+++ b/src/core/socket.h
65878a
@@ -32,6 +32,7 @@ typedef struct Socket Socket;
65878a
 typedef enum SocketState {
65878a
         SOCKET_DEAD,
65878a
         SOCKET_START_PRE,
65878a
+        SOCKET_START_CHOWN,
65878a
         SOCKET_START_POST,
65878a
         SOCKET_LISTENING,
65878a
         SOCKET_RUNNING,
65878a
@@ -48,6 +49,7 @@ typedef enum SocketState {
65878a
 
65878a
 typedef enum SocketExecCommand {
65878a
         SOCKET_EXEC_START_PRE,
65878a
+        SOCKET_EXEC_START_CHOWN,
65878a
         SOCKET_EXEC_START_POST,
65878a
         SOCKET_EXEC_STOP_PRE,
65878a
         SOCKET_EXEC_STOP_POST,
65878a
@@ -151,6 +153,9 @@ struct Socket {
65878a
         char *smack;
65878a
         char *smack_ip_in;
65878a
         char *smack_ip_out;
65878a
+
65878a
+        char *user;
65878a
+        char *group;
65878a
 };
65878a
 
65878a
 /* Called from the service code when collecting fds */
65878a
diff --git a/src/shared/exit-status.c b/src/shared/exit-status.c
65878a
index 45131f2..f15fdba 100644
65878a
--- a/src/shared/exit-status.c
65878a
+++ b/src/shared/exit-status.c
65878a
@@ -130,6 +130,10 @@ const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) {
65878a
 
65878a
                 case EXIT_SECCOMP:
65878a
                         return "SECCOMP";
65878a
+
65878a
+                case EXIT_CHOWN:
65878a
+                        return "CHOWN";
65878a
+
65878a
                 }
65878a
         }
65878a
 
65878a
diff --git a/src/shared/exit-status.h b/src/shared/exit-status.h
65878a
index 1f035a3..52dd3d0 100644
65878a
--- a/src/shared/exit-status.h
65878a
+++ b/src/shared/exit-status.h
65878a
@@ -67,7 +67,8 @@ typedef enum ExitStatus {
65878a
         EXIT_NETWORK,
65878a
         EXIT_NAMESPACE,
65878a
         EXIT_NO_NEW_PRIVILEGES,
65878a
-        EXIT_SECCOMP
65878a
+        EXIT_SECCOMP,
65878a
+        EXIT_CHOWN
65878a
 } ExitStatus;
65878a
 
65878a
 typedef enum ExitStatusLevel {