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

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