8d86bd
From d6ffd324cc933efec946a3ffbed6fccfe7077203 Mon Sep 17 00:00:00 2001
8d86bd
From: Lennart Poettering <lennart@poettering.net>
8d86bd
Date: Mon, 26 Nov 2018 21:07:48 +0100
8d86bd
Subject: [PATCH] core: be more careful when inheriting stdout fds to stderr
8d86bd
8d86bd
We need to compare the fd name/file name if we inherit an fd from stdout
8d86bd
to stderr. Let's do that.
8d86bd
8d86bd
Fixes: #10875
8d86bd
(cherry picked from commit 41fc585a7a3b8ae857cad5fdad1bc70cdacfa8e5)
8d86bd
8d86bd
Related: #2093479
8d86bd
---
8d86bd
 src/core/execute.c | 27 +++++++++++++++++++++++++--
8d86bd
 1 file changed, 25 insertions(+), 2 deletions(-)
8d86bd
8d86bd
diff --git a/src/core/execute.c b/src/core/execute.c
8d86bd
index 9cbb678ac4..b1d8dceb32 100644
8d86bd
--- a/src/core/execute.c
8d86bd
+++ b/src/core/execute.c
8d86bd
@@ -545,6 +545,30 @@ static int setup_input(
8d86bd
         }
8d86bd
 }
8d86bd
 
8d86bd
+static bool can_inherit_stderr_from_stdout(
8d86bd
+                const ExecContext *context,
8d86bd
+                ExecOutput o,
8d86bd
+                ExecOutput e) {
8d86bd
+
8d86bd
+        assert(context);
8d86bd
+
8d86bd
+        /* Returns true, if given the specified STDERR and STDOUT output we can directly dup() the stdout fd to the
8d86bd
+         * stderr fd */
8d86bd
+
8d86bd
+        if (e == EXEC_OUTPUT_INHERIT)
8d86bd
+                return true;
8d86bd
+        if (e != o)
8d86bd
+                return false;
8d86bd
+
8d86bd
+        if (e == EXEC_OUTPUT_NAMED_FD)
8d86bd
+                return streq_ptr(context->stdio_fdname[STDOUT_FILENO], context->stdio_fdname[STDERR_FILENO]);
8d86bd
+
8d86bd
+        if (IN_SET(e, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND))
8d86bd
+                return streq_ptr(context->stdio_file[STDOUT_FILENO], context->stdio_file[STDERR_FILENO]);
8d86bd
+
8d86bd
+        return true;
8d86bd
+}
8d86bd
+
8d86bd
 static int setup_output(
8d86bd
                 const Unit *unit,
8d86bd
                 const ExecContext *context,
8d86bd
@@ -603,7 +627,7 @@ static int setup_output(
8d86bd
                         return fileno;
8d86bd
 
8d86bd
                 /* Duplicate from stdout if possible */
8d86bd
-                if ((e == o && e != EXEC_OUTPUT_NAMED_FD) || e == EXEC_OUTPUT_INHERIT)
8d86bd
+                if (can_inherit_stderr_from_stdout(context, o, e))
8d86bd
                         return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno;
8d86bd
 
8d86bd
                 o = e;
8d86bd
@@ -694,7 +718,6 @@ static int setup_output(
8d86bd
                         flags |= O_APPEND;
8d86bd
 
8d86bd
                 fd = acquire_path(context->stdio_file[fileno], flags, 0666 & ~context->umask);
8d86bd
-
8d86bd
                 if (fd < 0)
8d86bd
                         return fd;
8d86bd