Blame SOURCES/0006-Bug-1614820-Crash-in-vslapd_log_emergency_error.patch

edc5f2
From 59071a77774c530f0ab570dda27e23a021d23972 Mon Sep 17 00:00:00 2001
6e8815
From: Mark Reynolds <mreynolds@redhat.com>
edc5f2
Date: Thu, 23 Aug 2018 10:09:58 -0400
edc5f2
Subject: [PATCH] Bug 1614820 - Crash in vslapd_log_emergency_error
6e8815
edc5f2
Description: We were not locking the error log fd before closing and reopening
edc5f2
             the log file.  This could cause a crash when multiple threads are
edc5f2
             trying to log tothe errors log.
6e8815
---
6e8815
 ldap/servers/slapd/log.c | 22 ++++++++++++++++------
6e8815
 1 file changed, 16 insertions(+), 6 deletions(-)
6e8815
6e8815
diff --git a/ldap/servers/slapd/log.c b/ldap/servers/slapd/log.c
edc5f2
index 2e4ee03a8..7dd71541b 100644
6e8815
--- a/ldap/servers/slapd/log.c
6e8815
+++ b/ldap/servers/slapd/log.c
6e8815
@@ -2231,11 +2231,11 @@ vslapd_log_emergency_error(LOGFD fp, const char *msg, int locked)
6e8815
     if (logging_hr_timestamps_enabled == 1) {
6e8815
         struct timespec tsnow;
6e8815
         if (clock_gettime(CLOCK_REALTIME, &tsnow) != 0) {
6e8815
-            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to determine system time for message :: %s", msg);
6e8815
+            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to determine system time for message :: %s\n", msg);
6e8815
             return;
6e8815
         }
6e8815
         if (format_localTime_hr_log(tsnow.tv_sec, tsnow.tv_nsec, sizeof(tbuf), tbuf, &size) != 0) {
6e8815
-            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to format system time for message :: %s", msg);
6e8815
+            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to format system time for message :: %s\n", msg);
6e8815
             return;
6e8815
         }
6e8815
     } else {
6e8815
@@ -2243,14 +2243,14 @@ vslapd_log_emergency_error(LOGFD fp, const char *msg, int locked)
6e8815
         time_t tnl;
6e8815
         tnl = slapi_current_utc_time();
6e8815
         if (format_localTime_log(tnl, sizeof(tbuf), tbuf, &size) != 0) {
6e8815
-            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to format system time for message :: %s", msg);
6e8815
+            syslog(LOG_EMERG, "vslapd_log_emergency_error, Unable to format system time for message :: %s\n", msg);
6e8815
             return;
6e8815
         }
6e8815
 #ifdef HAVE_CLOCK_GETTIME
6e8815
     }
6e8815
 #endif
6e8815
 
6e8815
-    PR_snprintf(buffer, sizeof(buffer), "%s - EMERG - %s", tbuf, msg);
6e8815
+    PR_snprintf(buffer, sizeof(buffer), "%s - EMERG - %s\n", tbuf, msg);
6e8815
     size = strlen(buffer);
6e8815
 
6e8815
     if (!locked) {
6e8815
@@ -2531,7 +2531,7 @@ vslapd_log_access(char *fmt, va_list ap)
6e8815
 
6e8815
     if (SLAPI_LOG_BUFSIZ - blen < vlen) {
6e8815
         /* We won't be able to fit the message in! Uh-oh! */
6e8815
-        /* Should we actually just do the snprintf, and warn that message was trunced? */
6e8815
+        /* Should we actually just do the snprintf, and warn that message was truncated? */
6e8815
         log__error_emergency("Insufficent buffer capacity to fit timestamp and message!", 1, 0);
6e8815
         return -1;
6e8815
     }
edc5f2
@@ -4486,6 +4486,13 @@ log__error_emergency(const char *errstr, int reopen, int locked)
6e8815
     if (!reopen) {
6e8815
         return;
6e8815
     }
6e8815
+    if (!locked) {
6e8815
+        /*
6e8815
+         * Take the lock because we are closing and reopening the error log (fd),
6e8815
+         * and we don't want any other threads trying to use this fd
6e8815
+         */
6e8815
+        LOG_ERROR_LOCK_WRITE();
6e8815
+    }
6e8815
     if (NULL != loginfo.log_error_fdes) {
6e8815
         LOG_CLOSE(loginfo.log_error_fdes);
6e8815
     }
edc5f2
@@ -4494,7 +4501,10 @@ log__error_emergency(const char *errstr, int reopen, int locked)
6e8815
         PRErrorCode prerr = PR_GetError();
6e8815
         syslog(LOG_ERR, "Failed to reopen errors log file, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n", prerr, slapd_pr_strerror(prerr));
6e8815
     } else {
6e8815
-        vslapd_log_emergency_error(loginfo.log_error_fdes, errstr, locked);
6e8815
+        vslapd_log_emergency_error(loginfo.log_error_fdes, errstr, 1 /* locked */);
6e8815
+    }
6e8815
+    if (!locked) {
6e8815
+        LOG_ERROR_UNLOCK_WRITE();
6e8815
     }
6e8815
     return;
6e8815
 }
6e8815
-- 
6e8815
2.17.1
6e8815