Zbigniew Jędrzejewski-Szmek 6384ab
From 0e557eef37c9ebcc8f5c19fc6fc44b6fd617cc5d Mon Sep 17 00:00:00 2001
Zbigniew Jędrzejewski-Szmek 6384ab
From: Sergey Bugaev <bugaevc@gmail.com>
Zbigniew Jędrzejewski-Szmek 6384ab
Date: Mon, 22 Mar 2021 18:31:12 +0300
Zbigniew Jędrzejewski-Szmek 6384ab
Subject: [PATCH] log: protect errno in log_open()
Zbigniew Jędrzejewski-Szmek 6384ab
Zbigniew Jędrzejewski-Szmek 6384ab
Commit 0b1f3c768ce1bd1490a5e53f539976dcef8ca765 has introduced log_open()
Zbigniew Jędrzejewski-Szmek 6384ab
calls after exec fails post-fork. However, the log_open() call itself could
Zbigniew Jędrzejewski-Szmek 6384ab
change the value of errno, which, for me, manifested in:
Zbigniew Jędrzejewski-Szmek 6384ab
Zbigniew Jędrzejewski-Szmek 6384ab
$ coredumpctl gdb
Zbigniew Jędrzejewski-Szmek 6384ab
...
Zbigniew Jędrzejewski-Szmek 6384ab
Failed to invoke gdb: Success
Zbigniew Jędrzejewski-Szmek 6384ab
Zbigniew Jędrzejewski-Szmek 6384ab
Fix this by using PROTECT_ERRNO in log_open().
Zbigniew Jędrzejewski-Szmek 6384ab
---
Zbigniew Jędrzejewski-Szmek 6384ab
 src/basic/log.c | 7 +++++++
Zbigniew Jędrzejewski-Szmek 6384ab
 1 file changed, 7 insertions(+)
Zbigniew Jędrzejewski-Szmek 6384ab
Zbigniew Jędrzejewski-Szmek 6384ab
diff --git a/src/basic/log.c b/src/basic/log.c
Zbigniew Jędrzejewski-Szmek 6384ab
index c8cca96bca4..0e6023cff22 100644
Zbigniew Jędrzejewski-Szmek 6384ab
--- a/src/basic/log.c
Zbigniew Jędrzejewski-Szmek 6384ab
+++ b/src/basic/log.c
Zbigniew Jędrzejewski-Szmek 6384ab
@@ -252,6 +252,13 @@ int log_open(void) {
Zbigniew Jędrzejewski-Szmek 6384ab
 
Zbigniew Jędrzejewski-Szmek 6384ab
         /* Do not call from library code. */
Zbigniew Jędrzejewski-Szmek 6384ab
 
Zbigniew Jędrzejewski-Szmek 6384ab
+        /* This function is often called in preparation for being able
Zbigniew Jędrzejewski-Szmek 6384ab
+         * to log. Let's make sure we don't clobber errno, so that a call
Zbigniew Jędrzejewski-Szmek 6384ab
+         * to a logging function immediately following a log_open() call
Zbigniew Jędrzejewski-Szmek 6384ab
+         * can still easily reference an error that happened immediately
Zbigniew Jędrzejewski-Szmek 6384ab
+         * before the log_open() call. */
Zbigniew Jędrzejewski-Szmek 6384ab
+        PROTECT_ERRNO;
Zbigniew Jędrzejewski-Szmek 6384ab
+
Zbigniew Jędrzejewski-Szmek 6384ab
         /* If we don't use the console we close it here, to not get
Zbigniew Jędrzejewski-Szmek 6384ab
          * killed by SAK. If we don't use syslog we close it here so
Zbigniew Jędrzejewski-Szmek 6384ab
          * that we are not confused by somebody deleting the socket in