daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
c62b8e
From c0f32feb77768aa76d8c813471b3484c93bc2651 Mon Sep 17 00:00:00 2001
c62b8e
From: Lennart Poettering <lennart@poettering.net>
c62b8e
Date: Fri, 5 Jan 2018 12:20:22 +0100
c62b8e
Subject: [PATCH] core: be stricter when handling PID files and MAINPID
c62b8e
 sd_notify() messages
c62b8e
c62b8e
Let's be more restrictive when validating PID files and MAINPID=
c62b8e
messages: don't accept PIDs that make no sense, and if the configuration
c62b8e
source is not trusted, don't accept out-of-cgroup PIDs. A configuratin
c62b8e
source is considered trusted when the PID file is owned by root, or the
c62b8e
message was received from root.
c62b8e
c62b8e
This should lock things down a bit, in case service authors write out
c62b8e
PID files from unprivileged code or use NotifyAccess=all with
c62b8e
unprivileged code. Note that doing so was always problematic, just now
c62b8e
it's a bit less problematic.
c62b8e
c62b8e
When we open the PID file we'll now use the CHASE_SAFE chase_symlinks()
c62b8e
logic, to ensure that we won't follow an unpriviled-owned symlink to a
c62b8e
privileged-owned file thinking this was a valid privileged PID file,
c62b8e
even though it really isn't.
c62b8e
c62b8e
Fixes: #6632
c62b8e
(cherry picked from commit db256aab13d8a89d583ecd2bacf0aca87c66effc)
c62b8e
c62b8e
Resolves: #1663143
c62b8e
---
c62b8e
 man/systemd.service.xml                |  18 ++-
c62b8e
 src/core/manager.c                     |  17 ++-
c62b8e
 src/core/service.c                     | 166 ++++++++++++++++------
c62b8e
 src/core/unit.h                        |   2 +-
c62b8e
 test/TEST-20-MAINPIDGAMES/Makefile     |   1 +
c62b8e
 test/TEST-20-MAINPIDGAMES/test.sh      |  81 +++++++++++
c62b8e
 test/TEST-20-MAINPIDGAMES/testsuite.sh | 189 +++++++++++++++++++++++++
c62b8e
 test/test-functions                    |   2 +-
c62b8e
 8 files changed, 418 insertions(+), 58 deletions(-)
c62b8e
 create mode 120000 test/TEST-20-MAINPIDGAMES/Makefile
c62b8e
 create mode 100755 test/TEST-20-MAINPIDGAMES/test.sh
c62b8e
 create mode 100755 test/TEST-20-MAINPIDGAMES/testsuite.sh
c62b8e
c62b8e
diff --git a/man/systemd.service.xml b/man/systemd.service.xml
c62b8e
index d147e449a6..565a783f72 100644
c62b8e
--- a/man/systemd.service.xml
c62b8e
+++ b/man/systemd.service.xml
c62b8e
@@ -221,16 +221,14 @@
c62b8e
       <varlistentry>
c62b8e
         <term><varname>PIDFile=</varname></term>
c62b8e
 
c62b8e
-        <listitem><para>Takes an absolute file name pointing to the
c62b8e
-        PID file of this daemon. Use of this option is recommended for
c62b8e
-        services where <varname>Type=</varname> is set to
c62b8e
-        <option>forking</option>. systemd will read the PID of the
c62b8e
-        main process of the daemon after start-up of the service.
c62b8e
-        systemd will not write to the file configured here, although
c62b8e
-        it will remove the file after the service has shut down if it
c62b8e
-        still exists.
c62b8e
-        </para>
c62b8e
-        </listitem>
c62b8e
+        <listitem><para>Takes an absolute path referring to the PID file of the service. Usage of this option is
c62b8e
+        recommended for services where <varname>Type=</varname> is set to <option>forking</option>. The service manager
c62b8e
+        will read the PID of the main process of the service from this file after start-up of the service. The service
c62b8e
+        manager will not write to the file configured here, although it will remove the file after the service has shut
c62b8e
+        down if it still exists. The PID file does not need to be owned by a privileged user, but if it is owned by an
c62b8e
+        unprivileged user additional safety restrictions are enforced: the file may not be a symlink to a file owned by
c62b8e
+        a different user (neither directly nor indirectly), and the PID file must refer to a process already belonging
c62b8e
+        to the service.</para></listitem>
c62b8e
       </varlistentry>
c62b8e
 
c62b8e
       <varlistentry>
c62b8e
diff --git a/src/core/manager.c b/src/core/manager.c
c62b8e
index 73d6c81fdb..3bca61d0b1 100644
c62b8e
--- a/src/core/manager.c
c62b8e
+++ b/src/core/manager.c
c62b8e
@@ -1658,11 +1658,18 @@ static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, ui
c62b8e
         return 0;
c62b8e
 }
c62b8e
 
c62b8e
-static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, const char *buf, FDSet *fds) {
c62b8e
+static void manager_invoke_notify_message(
c62b8e
+                Manager *m,
c62b8e
+                Unit *u,
c62b8e
+                const struct ucred *ucred,
c62b8e
+                const char *buf,
c62b8e
+                FDSet *fds) {
c62b8e
+
c62b8e
         _cleanup_strv_free_ char **tags = NULL;
c62b8e
 
c62b8e
         assert(m);
c62b8e
         assert(u);
c62b8e
+        assert(ucred);
c62b8e
         assert(buf);
c62b8e
 
c62b8e
         tags = strv_split(buf, "\n\r");
c62b8e
@@ -1674,7 +1681,7 @@ static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, const
c62b8e
         log_unit_debug(u->id, "Got notification message for unit %s", u->id);
c62b8e
 
c62b8e
         if (UNIT_VTABLE(u)->notify_message)
c62b8e
-                UNIT_VTABLE(u)->notify_message(u, pid, tags, fds);
c62b8e
+                UNIT_VTABLE(u)->notify_message(u, ucred, tags, fds);
c62b8e
         else if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) {
c62b8e
                 _cleanup_free_ char *x = NULL, *y = NULL;
c62b8e
 
c62b8e
@@ -1777,19 +1784,19 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
c62b8e
          * to avoid notifying the same one multiple times. */
c62b8e
         u1 = manager_get_unit_by_pid(m, ucred->pid);
c62b8e
         if (u1) {
c62b8e
-                manager_invoke_notify_message(m, u1, ucred->pid, buf, fds);
c62b8e
+                manager_invoke_notify_message(m, u1, ucred, buf, fds);
c62b8e
                 found = true;
c62b8e
         }
c62b8e
 
c62b8e
         u2 = hashmap_get(m->watch_pids1, LONG_TO_PTR(ucred->pid));
c62b8e
         if (u2 && u2 != u1) {
c62b8e
-                manager_invoke_notify_message(m, u2, ucred->pid, buf, fds);
c62b8e
+                manager_invoke_notify_message(m, u2, ucred, buf, fds);
c62b8e
                 found = true;
c62b8e
         }
c62b8e
 
c62b8e
         u3 = hashmap_get(m->watch_pids2, LONG_TO_PTR(ucred->pid));
c62b8e
         if (u3 && u3 != u2 && u3 != u1) {
c62b8e
-                manager_invoke_notify_message(m, u3, ucred->pid, buf, fds);
c62b8e
+                manager_invoke_notify_message(m, u3, ucred, buf, fds);
c62b8e
                 found = true;
c62b8e
         }
c62b8e
 
c62b8e
diff --git a/src/core/service.c b/src/core/service.c
c62b8e
index fe6e2ff17c..06b39e3a5a 100644
c62b8e
--- a/src/core/service.c
c62b8e
+++ b/src/core/service.c
c62b8e
@@ -700,9 +700,45 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
c62b8e
         }
c62b8e
 }
c62b8e
 
c62b8e
+static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) {
c62b8e
+        Unit *owner;
c62b8e
+
c62b8e
+        assert(s);
c62b8e
+        assert(pid > 0);
c62b8e
+
c62b8e
+        /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the
c62b8e
+         * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is
c62b8e
+         * good */
c62b8e
+
c62b8e
+        if (pid == getpid() || pid == 1) {
c62b8e
+                log_unit_full(UNIT(s)->id, prio, "New main PID "PID_FMT" is the manager, refusing.", pid);
c62b8e
+                return -EPERM;
c62b8e
+        }
c62b8e
+
c62b8e
+        if (pid == s->control_pid) {
c62b8e
+                log_unit_full(UNIT(s)->id, prio, "New main PID "PID_FMT" is the control process, refusing.", pid);
c62b8e
+                return -EPERM;
c62b8e
+        }
c62b8e
+
c62b8e
+        if (!pid_is_alive(pid)) {
c62b8e
+                log_unit_full(UNIT(s)->id, prio, "New main PID "PID_FMT" does not exist or is a zombie.", pid);
c62b8e
+                return -ESRCH;
c62b8e
+        }
c62b8e
+
c62b8e
+        owner = manager_get_unit_by_pid(UNIT(s)->manager, pid);
c62b8e
+        if (owner == UNIT(s)) {
c62b8e
+                log_unit_debug(UNIT(s)->id, "New main PID "PID_FMT" belongs to service, we are happy.", pid);
c62b8e
+                return 1; /* Yay, it's definitely a good PID */
c62b8e
+        }
c62b8e
+
c62b8e
+        return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */
c62b8e
+}
c62b8e
+
c62b8e
 static int service_load_pid_file(Service *s, bool may_warn) {
c62b8e
+        char procfs[sizeof("/proc/self/fd/") - 1 + DECIMAL_STR_MAX(int)];
c62b8e
         _cleanup_free_ char *k = NULL;
c62b8e
-        int r;
c62b8e
+        _cleanup_close_ int fd = -1;
c62b8e
+        int r, prio;
c62b8e
         pid_t pid;
c62b8e
 
c62b8e
         assert(s);
c62b8e
@@ -710,30 +746,47 @@ static int service_load_pid_file(Service *s, bool may_warn) {
c62b8e
         if (!s->pid_file)
c62b8e
                 return -ENOENT;
c62b8e
 
c62b8e
-        r = read_one_line_file(s->pid_file, &k);
c62b8e
-        if (r < 0) {
c62b8e
-                if (may_warn)
c62b8e
-                        log_unit_info(UNIT(s)->id, "PID file %s not readable (yet?) after %s.", s->pid_file, service_state_to_string(s->state));
c62b8e
-                return r;
c62b8e
-        }
c62b8e
+        prio = may_warn ? LOG_INFO : LOG_DEBUG;
c62b8e
+
c62b8e
+        fd = chase_symlinks(s->pid_file, NULL, CHASE_OPEN|CHASE_SAFE, NULL);
c62b8e
+        if (fd == -EPERM)
c62b8e
+                return log_unit_full(UNIT(s)->id, prio, "Permission denied while opening PID file or unsafe symlink chain: %s", s->pid_file);
c62b8e
+        if (fd < 0)
c62b8e
+                return log_unit_full(UNIT(s)->id, prio, "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state));
c62b8e
+
c62b8e
+        /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd chase_symlinks() returned us into a proper fd first. */
c62b8e
+        xsprintf(procfs, "/proc/self/fd/%i", fd);
c62b8e
+        r = read_one_line_file(procfs, &k);
c62b8e
+        if (r < 0)
c62b8e
+                return log_unit_error_errno(UNIT(s)->id, r, "Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m", s->pid_file);
c62b8e
 
c62b8e
         r = parse_pid(k, &pid;;
c62b8e
-        if (r < 0) {
c62b8e
-                if (may_warn)
c62b8e
-                        log_unit_info_errno(UNIT(s)->id, r, "Failed to read PID from file %s: %m", s->pid_file);
c62b8e
+        if (r < 0)
c62b8e
+                return log_unit_full(UNIT(s)->id, prio, "Failed to parse PID from file %s: %m", s->pid_file);
c62b8e
+
c62b8e
+        if (s->main_pid_known && pid == s->main_pid)
c62b8e
+                return 0;
c62b8e
+
c62b8e
+        r = service_is_suitable_main_pid(s, pid, prio);
c62b8e
+        if (r < 0)
c62b8e
                 return r;
c62b8e
-        }
c62b8e
+        if (r == 0) {
c62b8e
+                struct stat st;
c62b8e
 
c62b8e
-        if (!pid_is_alive(pid)) {
c62b8e
-                if (may_warn)
c62b8e
-                        log_unit_info(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
c62b8e
-                return -ESRCH;
c62b8e
+                /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */
c62b8e
+
c62b8e
+                if (fstat(fd, &st) < 0)
c62b8e
+                        return log_unit_error_errno(UNIT(s)->id, errno, "Failed to fstat() PID file O_PATH fd: %m");
c62b8e
+
c62b8e
+                if (st.st_uid != 0) {
c62b8e
+                        log_unit_error(UNIT(s)->id, "New main PID "PID_FMT" does not belong to service, and PID file is not owned by root. Refusing.", pid);
c62b8e
+                        return -EPERM;
c62b8e
+                }
c62b8e
+
c62b8e
+                log_unit_debug(UNIT(s)->id, "New main PID "PID_FMT" does not belong to service, but we'll accept it since PID file is owned by root.", pid);
c62b8e
         }
c62b8e
 
c62b8e
         if (s->main_pid_known) {
c62b8e
-                if (pid == s->main_pid)
c62b8e
-                        return 0;
c62b8e
-
c62b8e
                 log_unit_debug(UNIT(s)->id, "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
c62b8e
 
c62b8e
                 service_unwatch_main_pid(s);
c62b8e
@@ -752,7 +805,7 @@ static int service_load_pid_file(Service *s, bool may_warn) {
c62b8e
                 return r;
c62b8e
         }
c62b8e
 
c62b8e
-        return 0;
c62b8e
+        return 1;
c62b8e
 }
c62b8e
 
c62b8e
 static int service_search_main_pid(Service *s) {
c62b8e
@@ -2584,7 +2637,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
c62b8e
                 /* Forking services may occasionally move to a new PID.
c62b8e
                  * As long as they update the PID file before exiting the old
c62b8e
                  * PID, they're fine. */
c62b8e
-                if (service_load_pid_file(s, false) == 0)
c62b8e
+                if (service_load_pid_file(s, false) > 0)
c62b8e
                         return;
c62b8e
 
c62b8e
                 s->main_pid = 0;
c62b8e
@@ -2957,42 +3010,73 @@ static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void
c62b8e
         return 0;
c62b8e
 }
c62b8e
 
c62b8e
-static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
c62b8e
+static bool service_notify_message_authorized(Service *s, pid_t pid, char **tags, FDSet *fds) {
c62b8e
+        assert(s);
c62b8e
+
c62b8e
+        if (s->notify_access == NOTIFY_NONE) {
c62b8e
+                log_unit_warning(UNIT(s)->id, "Got notification message from PID "PID_FMT", but reception is disabled.", pid);
c62b8e
+                return false;
c62b8e
+        }
c62b8e
+
c62b8e
+        if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
c62b8e
+                if (s->main_pid != 0)
c62b8e
+                        log_unit_warning(UNIT(s)->id, "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid);
c62b8e
+                else
c62b8e
+                        log_unit_warning(UNIT(s)->id, "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid);
c62b8e
+
c62b8e
+                return false;
c62b8e
+        }
c62b8e
+
c62b8e
+        return true;
c62b8e
+}
c62b8e
+
c62b8e
+static void service_notify_message(
c62b8e
+                Unit *u,
c62b8e
+                const struct ucred *ucred,
c62b8e
+                char **tags,
c62b8e
+                FDSet *fds) {
c62b8e
         Service *s = SERVICE(u);
c62b8e
-        _cleanup_free_ char *cc = NULL;
c62b8e
         bool notify_dbus = false;
c62b8e
         const char *e;
c62b8e
+        int r;
c62b8e
 
c62b8e
         assert(u);
c62b8e
+        assert(ucred);
c62b8e
 
c62b8e
-        cc = strv_join(tags, ", ");
c62b8e
-        log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT" (%s)",
c62b8e
-                       u->id, pid, isempty(cc) ? "n/a" : cc);
c62b8e
+        if (!service_notify_message_authorized(SERVICE(u), ucred->pid, tags, fds))
c62b8e
+                return;
c62b8e
 
c62b8e
         if (s->notify_access == NOTIFY_NONE) {
c62b8e
-                log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
c62b8e
-                return;
c62b8e
-        }
c62b8e
+                _cleanup_free_ char *cc = NULL;
c62b8e
 
c62b8e
-        if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
c62b8e
-                if (s->main_pid != 0)
c62b8e
-                        log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid);
c62b8e
-                else
c62b8e
-                        log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid);
c62b8e
-                return;
c62b8e
+                cc = strv_join(tags, ", ");
c62b8e
+                log_unit_debug(u->id, "Got notification message from PID "PID_FMT" (%s)", ucred->pid, isempty(cc) ? "n/a" : cc);
c62b8e
         }
c62b8e
 
c62b8e
         /* Interpret MAINPID= */
c62b8e
         e = strv_find_startswith(tags, "MAINPID=");
c62b8e
         if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
c62b8e
-                if (parse_pid(e, &pid) < 0)
c62b8e
-                        log_unit_warning(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
c62b8e
-                else {
c62b8e
-                        log_unit_debug(u->id, "%s: got MAINPID=%s", u->id, e);
c62b8e
+                pid_t new_main_pid;
c62b8e
 
c62b8e
-                        service_set_main_pid(s, pid);
c62b8e
-                        unit_watch_pid(UNIT(s), pid);
c62b8e
-                        notify_dbus = true;
c62b8e
+                if (parse_pid(e, &new_main_pid) < 0)
c62b8e
+                        log_unit_warning(u->id, "Failed to parse MAINPID= field in notification message, ignoring: %s", e);
c62b8e
+                else if (!s->main_pid_known || new_main_pid != s->main_pid) {
c62b8e
+
c62b8e
+                        r = service_is_suitable_main_pid(s, new_main_pid, LOG_WARNING);
c62b8e
+                        if (r == 0) {
c62b8e
+                                /* The new main PID is a bit suspicous, which is OK if the sender is privileged. */
c62b8e
+
c62b8e
+                                if (ucred->uid == 0) {
c62b8e
+                                        log_unit_debug(u->id, "New main PID "PID_FMT" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid);
c62b8e
+                                        r = 1;
c62b8e
+                                } else
c62b8e
+                                        log_unit_debug(u->id, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid);
c62b8e
+                        }
c62b8e
+                        if (r > 0) {
c62b8e
+                                service_set_main_pid(s, new_main_pid);
c62b8e
+                                unit_watch_pid(UNIT(s), new_main_pid);
c62b8e
+                                notify_dbus = true;
c62b8e
+                        }
c62b8e
                 }
c62b8e
         }
c62b8e
 
c62b8e
diff --git a/src/core/unit.h b/src/core/unit.h
c62b8e
index dfec9cea01..091ef7596e 100644
c62b8e
--- a/src/core/unit.h
c62b8e
+++ b/src/core/unit.h
c62b8e
@@ -376,7 +376,7 @@ struct UnitVTable {
c62b8e
         void (*notify_cgroup_empty)(Unit *u);
c62b8e
 
c62b8e
         /* Called whenever a process of this unit sends us a message */
c62b8e
-        void (*notify_message)(Unit *u, pid_t pid, char **tags, FDSet *fds);
c62b8e
+        void (*notify_message)(Unit *u, const struct ucred *ucred, char **tags, FDSet *fds);
c62b8e
 
c62b8e
         /* Called whenever a name this Unit registered for comes or
c62b8e
          * goes away. */
c62b8e
diff --git a/test/TEST-20-MAINPIDGAMES/Makefile b/test/TEST-20-MAINPIDGAMES/Makefile
c62b8e
new file mode 120000
c62b8e
index 0000000000..e9f93b1104
c62b8e
--- /dev/null
c62b8e
+++ b/test/TEST-20-MAINPIDGAMES/Makefile
c62b8e
@@ -0,0 +1 @@
c62b8e
+../TEST-01-BASIC/Makefile
c62b8e
\ No newline at end of file
c62b8e
diff --git a/test/TEST-20-MAINPIDGAMES/test.sh b/test/TEST-20-MAINPIDGAMES/test.sh
c62b8e
new file mode 100755
c62b8e
index 0000000000..733532b718
c62b8e
--- /dev/null
c62b8e
+++ b/test/TEST-20-MAINPIDGAMES/test.sh
c62b8e
@@ -0,0 +1,81 @@
c62b8e
+#!/bin/bash
c62b8e
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
c62b8e
+# ex: ts=8 sw=4 sts=4 et filetype=sh
c62b8e
+TEST_DESCRIPTION="test changing main PID"
c62b8e
+
c62b8e
+. $TEST_BASE_DIR/test-functions
c62b8e
+
c62b8e
+check_result_qemu() {
c62b8e
+    ret=1
c62b8e
+    mkdir -p $TESTDIR/root
c62b8e
+    mount ${LOOPDEV}p1 $TESTDIR/root
c62b8e
+    [[ -e $TESTDIR/root/testok ]] && ret=0
c62b8e
+    [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
c62b8e
+    [[ -f $TESTDIR/root/var/log/journal ]] && cp -a $TESTDIR/root/var/log/journal $TESTDIR
c62b8e
+    umount $TESTDIR/root
c62b8e
+    [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
c62b8e
+    ls -l $TESTDIR/journal/*/*.journal
c62b8e
+    test -s $TESTDIR/failed && ret=$(($ret+1))
c62b8e
+    return $ret
c62b8e
+}
c62b8e
+
c62b8e
+test_run() {
c62b8e
+    if run_qemu; then
c62b8e
+        check_result_qemu || return 1
c62b8e
+    else
c62b8e
+        dwarn "can't run QEMU, skipping"
c62b8e
+    fi
c62b8e
+    if check_nspawn; then
c62b8e
+        run_nspawn
c62b8e
+        check_result_nspawn || return 1
c62b8e
+    else
c62b8e
+        dwarn "can't run systemd-nspawn, skipping"
c62b8e
+    fi
c62b8e
+    return 0
c62b8e
+}
c62b8e
+
c62b8e
+test_setup() {
c62b8e
+    create_empty_image
c62b8e
+    mkdir -p $TESTDIR/root
c62b8e
+    mount ${LOOPDEV}p1 $TESTDIR/root
c62b8e
+
c62b8e
+    (
c62b8e
+        LOG_LEVEL=5
c62b8e
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
c62b8e
+
c62b8e
+        setup_basic_environment
c62b8e
+        inst_binary cut
c62b8e
+        inst_binary useradd
c62b8e
+        inst /etc/login.defs
c62b8e
+
c62b8e
+        # setup the testsuite service
c62b8e
+        cat >$initdir/etc/systemd/system/testsuite.service <
c62b8e
+[Unit]
c62b8e
+Description=Testsuite service
c62b8e
+
c62b8e
+[Service]
c62b8e
+ExecStart=/bin/bash -x /testsuite.sh
c62b8e
+Type=oneshot
c62b8e
+StandardOutput=tty
c62b8e
+StandardError=tty
c62b8e
+NotifyAccess=all
c62b8e
+EOF
c62b8e
+        cp testsuite.sh $initdir/
c62b8e
+
c62b8e
+        useradd -R $initdir -U -u 1234 test
c62b8e
+
c62b8e
+        setup_testsuite
c62b8e
+    )
c62b8e
+    setup_nspawn_root
c62b8e
+
c62b8e
+    ddebug "umount $TESTDIR/root"
c62b8e
+    umount $TESTDIR/root
c62b8e
+}
c62b8e
+
c62b8e
+test_cleanup() {
c62b8e
+    umount $TESTDIR/root 2>/dev/null
c62b8e
+    [[ $LOOPDEV ]] && losetup -d $LOOPDEV
c62b8e
+    return 0
c62b8e
+}
c62b8e
+
c62b8e
+do_test "$@"
c62b8e
diff --git a/test/TEST-20-MAINPIDGAMES/testsuite.sh b/test/TEST-20-MAINPIDGAMES/testsuite.sh
c62b8e
new file mode 100755
c62b8e
index 0000000000..d4ad63865c
c62b8e
--- /dev/null
c62b8e
+++ b/test/TEST-20-MAINPIDGAMES/testsuite.sh
c62b8e
@@ -0,0 +1,189 @@
c62b8e
+#!/bin/bash
c62b8e
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
c62b8e
+# ex: ts=8 sw=4 sts=4 et filetype=sh
c62b8e
+set -ex
c62b8e
+set -o pipefail
c62b8e
+
c62b8e
+systemctl_show_value() {
c62b8e
+    systemctl show "$@" | cut -d = -f 2-
c62b8e
+}
c62b8e
+
c62b8e
+systemd-analyze set-log-level debug
c62b8e
+
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Start a test process inside of our own cgroup
c62b8e
+sleep infinity &
c62b8e
+INTERNALPID=$!
c62b8e
+disown
c62b8e
+
c62b8e
+# Start a test process outside of our own cgroup
c62b8e
+systemd-run -p User=test --unit=sleep.service /bin/sleep infinity
c62b8e
+EXTERNALPID=`systemctl_show_value -p MainPID sleep.service`
c62b8e
+
c62b8e
+# Update our own main PID to the external test PID, this should work
c62b8e
+systemd-notify MAINPID=$EXTERNALPID
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $EXTERNALPID
c62b8e
+
c62b8e
+# Update our own main PID to the internal test PID, this should work, too
c62b8e
+systemd-notify MAINPID=$INTERNALPID
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $INTERNALPID
c62b8e
+
c62b8e
+# Update it back to our own PID, this should also work
c62b8e
+systemd-notify MAINPID=$$
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Try to set it to PID 1, which it should ignore, because that's the manager
c62b8e
+systemd-notify MAINPID=1
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Try to set it to PID 0, which is invalid and should be ignored
c62b8e
+systemd-notify MAINPID=0
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Try to set it to a valid but non-existing PID, which should be ignored. (Note
c62b8e
+# that we set the PID to a value well above any known /proc/sys/kernel/pid_max,
c62b8e
+# which means we can be pretty sure it doesn't exist by coincidence)
c62b8e
+systemd-notify MAINPID=1073741824
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Change it again to the external PID, without priviliges this time. This should be ignored, because the PID is from outside of our cgroup and we lack privileges.
c62b8e
+systemd-notify --uid=1000 MAINPID=$EXTERNALPID
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+# Change it again to the internal PID, without priviliges this time. This should work, as the process is on our cgroup, and that's enough even if we lack privileges.
c62b8e
+systemd-notify --uid=1000 MAINPID=$INTERNALPID
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $INTERNALPID
c62b8e
+
c62b8e
+# Update it back to our own PID, this should also work
c62b8e
+systemd-notify --uid=1000 MAINPID=$$
c62b8e
+test `systemctl_show_value -p MainPID testsuite.service` -eq $$
c62b8e
+
c62b8e
+cat >/tmp/mainpid.sh <
c62b8e
+#!/bin/bash
c62b8e
+
c62b8e
+set -eux
c62b8e
+set -o pipefail
c62b8e
+
c62b8e
+# Create a number of children, and make one the main one
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+MAINPID=\$!
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+echo \$MAINPID > /run/mainpidsh/pid
c62b8e
+EOF
c62b8e
+chmod +x /tmp/mainpid.sh
c62b8e
+
c62b8e
+cat > /etc/systemd/system/mainpidsh.service <
c62b8e
+[Unit]
c62b8e
+Description=MainPID test 1 service
c62b8e
+
c62b8e
+[Service]
c62b8e
+StandardOutput=tty
c62b8e
+StandardError=tty
c62b8e
+Type=forking
c62b8e
+RuntimeDirectory=mainpidsh
c62b8e
+PIDFile=/run/mainpidsh/pid
c62b8e
+ExecStart=/tmp/mainpid.sh
c62b8e
+EOF
c62b8e
+
c62b8e
+systemctl daemon-reload
c62b8e
+systemctl start mainpidsh.service
c62b8e
+test `systemctl_show_value -p MainPID mainpidsh.service` -eq `cat /run/mainpidsh/pid`
c62b8e
+
c62b8e
+cat >/tmp/mainpid2.sh <
c62b8e
+#!/bin/bash
c62b8e
+
c62b8e
+set -eux
c62b8e
+set -o pipefail
c62b8e
+
c62b8e
+# Create a number of children, and make one the main one
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+MAINPID=\$!
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+echo \$MAINPID > /run/mainpidsh2/pid
c62b8e
+chown 1001:1001 /run/mainpidsh2/pid
c62b8e
+EOF
c62b8e
+chmod +x /tmp/mainpid2.sh
c62b8e
+
c62b8e
+cat > /etc/systemd/system/mainpidsh2.service <
c62b8e
+[Unit]
c62b8e
+Description=MainPID test 2 service
c62b8e
+
c62b8e
+[Service]
c62b8e
+StandardOutput=tty
c62b8e
+StandardError=tty
c62b8e
+Type=forking
c62b8e
+RuntimeDirectory=mainpidsh2
c62b8e
+PIDFile=/run/mainpidsh2/pid
c62b8e
+ExecStart=/tmp/mainpid2.sh
c62b8e
+EOF
c62b8e
+
c62b8e
+systemctl daemon-reload
c62b8e
+systemctl start mainpidsh2.service
c62b8e
+test `systemctl_show_value -p MainPID mainpidsh2.service` -eq `cat /run/mainpidsh2/pid`
c62b8e
+
c62b8e
+cat >/dev/shm/mainpid3.sh <
c62b8e
+#!/bin/bash
c62b8e
+
c62b8e
+set -eux
c62b8e
+set -o pipefail
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+sleep infinity &
c62b8e
+disown
c62b8e
+
c62b8e
+# Let's try to play games, and link up a privileged PID file
c62b8e
+ln -s ../mainpidsh/pid /run/mainpidsh3/pid
c62b8e
+
c62b8e
+# Quick assertion that the link isn't dead
c62b8e
+test -f /run/mainpidsh3/pid
c62b8e
+EOF
c62b8e
+chmod 755 /dev/shm/mainpid3.sh
c62b8e
+
c62b8e
+cat > /etc/systemd/system/mainpidsh3.service <
c62b8e
+[Unit]
c62b8e
+Description=MainPID test 3 service
c62b8e
+
c62b8e
+[Service]
c62b8e
+StandardOutput=tty
c62b8e
+StandardError=tty
c62b8e
+Type=forking
c62b8e
+RuntimeDirectory=mainpidsh3
c62b8e
+PIDFile=/run/mainpidsh3/pid
c62b8e
+User=test
c62b8e
+TimeoutStartSec=2s
c62b8e
+ExecStart=/dev/shm/mainpid3.sh
c62b8e
+EOF
c62b8e
+
c62b8e
+systemctl daemon-reload
c62b8e
+systemctl start mainpidsh3.service
c62b8e
+
c62b8e
+# Test that this failed due to timeout, and not some other error
c62b8e
+# test `systemctl_show_value -p Result mainpidsh3.service` = timeout
c62b8e
+# Just check that there is no MainPID => the pid file was ignored
c62b8e
+test `systemctl_show_value -p MainPID mainpidsh3.service` -eq 0
c62b8e
+
c62b8e
+systemd-analyze set-log-level info
c62b8e
+
c62b8e
+echo OK > /testok
c62b8e
+
c62b8e
+exit 0
c62b8e
diff --git a/test/test-functions b/test/test-functions
c62b8e
index 78e725d5b9..e50ce556fd 100644
c62b8e
--- a/test/test-functions
c62b8e
+++ b/test/test-functions
c62b8e
@@ -12,7 +12,7 @@ if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
c62b8e
     ROOTLIBDIR=/usr/lib/systemd
c62b8e
 fi
c62b8e
 
c62b8e
-BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
c62b8e
+BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe chmod chown ln"
c62b8e
 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
c62b8e
 
c62b8e
 function find_qemu_bin() {