ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
923a60
From 5a7f49bb38bc1d7965d497e775b7cc8053b0c465 Mon Sep 17 00:00:00 2001
923a60
From: Jan Synacek <jsynacek@redhat.com>
923a60
Date: Fri, 18 Aug 2017 10:17:22 +0200
923a60
Subject: [PATCH] log: never log into foreign fd #2 in PID 1 or its
923a60
 pre-execve() children
923a60
923a60
(cherry picked from commit 48a601fe5de8aa0d89ba6dadde168769fa7ce992)
923a60
Resolves: #1420505
923a60
---
923a60
 src/core/main.c  | 11 +++++++++--
923a60
 src/shared/log.c |  7 ++++++-
923a60
 src/shared/log.h |  1 +
923a60
 3 files changed, 16 insertions(+), 3 deletions(-)
923a60
923a60
diff --git a/src/core/main.c b/src/core/main.c
923a60
index 37e3ea0ced..66393ed6ad 100644
923a60
--- a/src/core/main.c
923a60
+++ b/src/core/main.c
923a60
@@ -1310,10 +1310,17 @@ int main(int argc, char *argv[]) {
923a60
         log_show_color(isatty(STDERR_FILENO) > 0);
923a60
         log_set_upgrade_syslog_to_journal(true);
923a60
 
923a60
-        /* Disable the umask logic */
923a60
-        if (getpid() == 1)
923a60
+        if (getpid() == 1) {
923a60
+                /* Disable the umask logic */
923a60
                 umask(0);
923a60
 
923a60
+                /* Always reopen /dev/console when running as PID 1 or one of its pre-execve() children. This is
923a60
+                 * important so that we never end up logging to any foreign stderr, for example if we have to log in a
923a60
+                 * child process right before execve()'ing the actual binary, at a point in time where socket
923a60
+                 * activation stderr/stdout area already set up. */
923a60
+                log_set_always_reopen_console(true);
923a60
+        }
923a60
+
923a60
         if (getpid() == 1 && detect_container(NULL) <= 0) {
923a60
 
923a60
                 /* Running outside of a container as PID 1 */
923a60
diff --git a/src/shared/log.c b/src/shared/log.c
923a60
index 646a1d6389..3491420308 100644
923a60
--- a/src/shared/log.c
923a60
+++ b/src/shared/log.c
923a60
@@ -52,6 +52,7 @@ static bool show_color = false;
923a60
 static bool show_location = false;
923a60
 
923a60
 static bool upgrade_syslog_to_journal = false;
923a60
+static bool always_reopen_console = false;
923a60
 
923a60
 /* Akin to glibc's __abort_msg; which is private and we hence cannot
923a60
  * use here. */
923a60
@@ -75,7 +76,7 @@ static int log_open_console(void) {
923a60
         if (console_fd >= 0)
923a60
                 return 0;
923a60
 
923a60
-        if (getpid() == 1) {
923a60
+        if (always_reopen_console) {
923a60
                 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
923a60
                 if (console_fd < 0)
923a60
                         return console_fd;
923a60
@@ -1061,3 +1062,7 @@ void log_received_signal(int level, const struct signalfd_siginfo *si) {
923a60
 void log_set_upgrade_syslog_to_journal(bool b) {
923a60
         upgrade_syslog_to_journal = b;
923a60
 }
923a60
+
923a60
+void log_set_always_reopen_console(bool b) {
923a60
+        always_reopen_console = b;
923a60
+}
923a60
diff --git a/src/shared/log.h b/src/shared/log.h
923a60
index 2889e1e77f..3c9448f1a7 100644
923a60
--- a/src/shared/log.h
923a60
+++ b/src/shared/log.h
923a60
@@ -210,3 +210,4 @@ LogTarget log_target_from_string(const char *s) _pure_;
923a60
 void log_received_signal(int level, const struct signalfd_siginfo *si);
923a60
 
923a60
 void log_set_upgrade_syslog_to_journal(bool b);
923a60
+void log_set_always_reopen_console(bool b);