File openssh-7.6p1-blocksigalrm.patch of Package openssh.10835

# HG changeset patch
# Parent  724c9ea86fe2c4a1f0e0d3aba168357ab1b2c3aa
block SIGALRM while logging through syslog to prevent deadlocks
(through grace_alarm_handler())

bnc#57354

Index: openssh-7.6p1/log.c
===================================================================
--- openssh-7.6p1.orig/log.c	2019-03-12 14:34:00.379390930 +0100
+++ openssh-7.6p1/log.c	2019-03-12 14:34:18.999494650 +0100
@@ -51,6 +51,7 @@
 #endif
 
 #include "log.h"
+#include <signal.h>
 
 static LogLevel log_level = SYSLOG_LEVEL_INFO;
 static int log_on_stderr = 1;
@@ -401,6 +402,7 @@ do_log(LogLevel level, const char *fmt,
 	char fmtbuf[MSGBUFSIZ];
 	char *txt = NULL;
 	int pri = LOG_INFO;
+	sigset_t nset, oset;
 	int saved_errno = errno;
 	log_handler_fn *tmp_handler;
 
@@ -460,6 +462,13 @@ do_log(LogLevel level, const char *fmt,
 		    (int)sizeof msgbuf - 3, fmtbuf);
 		(void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
 	} else {
+		/* Prevent a race between the grace_alarm which writes a
+		 * log message and terminates and main sshd code that leads
+		 * to deadlock as syslog is not async safe.
+		 */ 
+		sigemptyset(&nset);
+		sigaddset(&nset, SIGALRM);
+		sigprocmask(SIG_BLOCK, &nset, &oset);
 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
 		openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
 		syslog_r(pri, &sdata, "%.500s", fmtbuf);
@@ -469,6 +478,7 @@ do_log(LogLevel level, const char *fmt,
 		syslog(pri, "%.500s", fmtbuf);
 		closelog();
 #endif
+		sigprocmask(SIG_SETMASK, &oset, NULL);
 	}
 	errno = saved_errno;
 }