Blame SOURCES/0727-core-be-stricter-when-handling-PID-files-and-MAINPID.patch

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