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