9ab0c5
From 625b30e09d0090f81722aa8f02e7057839dfcf4f Mon Sep 17 00:00:00 2001
9ab0c5
From: Lennart Poettering <lennart@poettering.net>
9ab0c5
Date: Fri, 9 Feb 2018 17:05:17 +0100
9ab0c5
Subject: [PATCH] service: relax PID file symlink chain checks a bit (#8133)
9ab0c5
9ab0c5
Let's read the PID file after all if there's a potentially unsafe
9ab0c5
symlink chain in place. But if we do, then refuse taking the PID if its
9ab0c5
outside of the cgroup.
9ab0c5
9ab0c5
Fixes: #8085
9ab0c5
9ab0c5
(cherry picked from commit 73969ab61c39357e6892747e43307fbf07cafbed)
9ab0c5
(cherry picked from commit ce87ed7b47c61e649a0f9da39d272631b9524740)
9ab0c5
9ab0c5
Resolves: #1729414
9ab0c5
---
9ab0c5
 src/core/service.c | 15 +++++++++++++--
9ab0c5
 1 file changed, 13 insertions(+), 2 deletions(-)
9ab0c5
9ab0c5
diff --git a/src/core/service.c b/src/core/service.c
9ab0c5
index eaa588863f..6b61ccac18 100644
9ab0c5
--- a/src/core/service.c
9ab0c5
+++ b/src/core/service.c
9ab0c5
@@ -736,6 +736,7 @@ static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) {
9ab0c5
 
9ab0c5
 static int service_load_pid_file(Service *s, bool may_warn) {
9ab0c5
         char procfs[sizeof("/proc/self/fd/") - 1 + DECIMAL_STR_MAX(int)];
9ab0c5
+        bool questionable_pid_file = false;
9ab0c5
         _cleanup_free_ char *k = NULL;
9ab0c5
         _cleanup_close_ int fd = -1;
9ab0c5
         int r, prio;
9ab0c5
@@ -749,8 +750,13 @@ static int service_load_pid_file(Service *s, bool may_warn) {
9ab0c5
         prio = may_warn ? LOG_INFO : LOG_DEBUG;
9ab0c5
 
9ab0c5
         fd = chase_symlinks(s->pid_file, NULL, CHASE_OPEN|CHASE_SAFE, NULL);
9ab0c5
-        if (fd == -EPERM)
9ab0c5
-                return log_unit_full_errno(UNIT(s)->id, prio, fd, "Permission denied while opening PID file or unsafe symlink chain: %s", s->pid_file);
9ab0c5
+        if (fd == -EPERM) {
9ab0c5
+                log_unit_full(UNIT(s)->id, LOG_DEBUG, "Permission denied while opening PID file or potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file);
9ab0c5
+
9ab0c5
+                questionable_pid_file = true;
9ab0c5
+
9ab0c5
+                fd = chase_symlinks(s->pid_file, NULL, CHASE_OPEN, NULL);
9ab0c5
+        }
9ab0c5
         if (fd < 0)
9ab0c5
                 return log_unit_full_errno(UNIT(s)->id, prio, fd, "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state));
9ab0c5
 
9ab0c5
@@ -773,6 +779,11 @@ static int service_load_pid_file(Service *s, bool may_warn) {
9ab0c5
         if (r == 0) {
9ab0c5
                 struct stat st;
9ab0c5
 
9ab0c5
+                if (questionable_pid_file) {
9ab0c5
+                        log_unit_error(UNIT(s)->id, "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file);
9ab0c5
+                        return -EPERM;
9ab0c5
+                }
9ab0c5
+
9ab0c5
                 /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */
9ab0c5
 
9ab0c5
                 if (fstat(fd, &st) < 0)