Blame SOURCES/0096-Bug-1623247-Crash-in-vslapd_log_emergency_error.patch

6e8815
From 9d2aa18fb5c48a11300d2309df89213bbdb614e1 Mon Sep 17 00:00:00 2001
6e8815
From: Mark Reynolds <mreynolds@redhat.com>
6e8815
Date: Thu, 23 Aug 2018 13:43:36 -0400
6e8815
Subject: [PATCH 1/2] Bug 1623247 - Crash in vslapd_log_emergency_error
6e8815
6e8815
    Description: We were not locking the error log fd before closing and reopening
6e8815
                 the log file.  This could cause a crash when multiple threads are
6e8815
                 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
6e8815
index 998efaef3..90ce6ac0a 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
     }
6e8815
@@ -4447,6 +4447,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
     }
6e8815
@@ -4455,7 +4462,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