Blame SOURCES/0081-Ticket-49441-Import-crashes-with-large-indexed-binar.patch

61f723
From df5000efced2d00aa0fc6546fcf6fc7b02e27256 Mon Sep 17 00:00:00 2001
61f723
From: Mark Reynolds <mreynolds@redhat.com>
61f723
Date: Mon, 6 Nov 2017 22:30:55 -0500
61f723
Subject: [PATCH] Ticket 49441 - Import crashes with large indexed binary 
61f723
 attributes
61f723
61f723
Bug Description:  Importing an ldif file that contains entries with large
61f723
                  binary attributes that are indexed crashes the server.
61f723
                  The crash occurs when "encoding" the binary value to a
61f723
                  string for debug logging, where we "underflow" the buffer
61f723
                  space index which then allows the string buffer to overflow.
61f723
61f723
Fix Description:  While filling the string buffer with the encoded binary
61f723
                  value we need to make sure if the buffer space is greater
61f723
                  than zero before decrementing it.
61f723
61f723
                  Also check if trace logging is being used before we actually
61f723
                  call the logging function which calls the "encoded" function
61f723
                  first.  This way we avoid this costly "encoding" on every
61f723
                  index call we make.
61f723
61f723
https://pagure.io/389-ds-base/issue/49441
61f723
61f723
Reviewed by: firstyear(Thanks!)
61f723
---
61f723
 ldap/servers/slapd/back-ldbm/index.c | 21 ++++++++++-----------
61f723
 1 file changed, 10 insertions(+), 11 deletions(-)
61f723
61f723
diff --git a/ldap/servers/slapd/back-ldbm/index.c b/ldap/servers/slapd/back-ldbm/index.c
61f723
index d4de28ca3..d62052a22 100644
61f723
--- a/ldap/servers/slapd/back-ldbm/index.c
61f723
+++ b/ldap/servers/slapd/back-ldbm/index.c
61f723
@@ -808,7 +808,10 @@ encode (const struct berval* data, char buf[BUFSIZ])
61f723
 		    bufSpace -= (s - first);
61f723
 		}
61f723
 		do {
61f723
-		    *bufNext++ = '\\'; --bufSpace;
61f723
+		    if (bufSpace) {
61f723
+		        *bufNext++ = '\\';
61f723
+		        --bufSpace;
61f723
+		    }
61f723
 		    if (bufSpace < 2) {
61f723
 			memcpy (bufNext, "..", 2);
61f723
 			bufNext += 2;
61f723
@@ -903,8 +906,10 @@ index_read_ext_allids(
61f723
 		slapi_log_err(SLAPI_LOG_ERR, "index_read_ext_allids", "NULL prefix\n");
61f723
 		return NULL;
61f723
 	}
61f723
-	slapi_log_err(SLAPI_LOG_TRACE, "index_read_ext_allids", "=> ( \"%s\" %s \"%s\" )\n",
61f723
-		   type, prefix, encode (val, buf));
61f723
+	if (slapi_is_loglevel_set(LDAP_DEBUG_TRACE)) {
61f723
+	    slapi_log_err(SLAPI_LOG_TRACE, "index_read_ext_allids", "=> ( \"%s\" %s \"%s\" )\n",
61f723
+	                  type, prefix, encode (val, buf));
61f723
+	}
61f723
 
61f723
 	basetype = typebuf;
61f723
 	if ( (basetmp = slapi_attr_basetype( type, typebuf, sizeof(typebuf) ))
61f723
@@ -1737,16 +1742,13 @@ addordel_values(
61f723
                  */
61f723
 		key.flags = DB_DBT_USERMEM;
61f723
                 key.ulen = tmpbuflen;
61f723
-#ifdef LDAP_ERROR_LOGGING
61f723
-		/* XXX if ( slapd_ldap_debug & LDAP_DEBUG_TRACE )  XXX */
61f723
-		{
61f723
+        if (slapi_is_loglevel_set(LDAP_DEBUG_TRACE)) {
61f723
 			char encbuf[BUFSIZ];
61f723
 
61f723
 			slapi_log_err(SLAPI_LOG_TRACE, "addordel_values", "%s_value(\"%s\")\n",
61f723
 				   (flags & BE_INDEX_ADD) ? "add" : "del",
61f723
 				   encoded (&key, encbuf));
61f723
 		}
61f723
-#endif
61f723
 
61f723
 		if (NULL != txn) {
61f723
 			db_txn = txn->back_txn_txn;
61f723
@@ -1907,16 +1909,13 @@ addordel_values_sv(
61f723
          */
61f723
         key.flags = DB_DBT_USERMEM;
61f723
         key.ulen = tmpbuflen;
61f723
-#ifdef LDAP_ERROR_LOGGING
61f723
-        /* XXX if ( slapd_ldap_debug & LDAP_DEBUG_TRACE )  XXX */
61f723
-        {
61f723
+        if (slapi_is_loglevel_set(LDAP_DEBUG_TRACE)) {
61f723
             char encbuf[BUFSIZ];
61f723
 
61f723
             slapi_log_err(SLAPI_LOG_TRACE, "addordel_values_sv", "%s_value(\"%s\")\n",
61f723
                        (flags & BE_INDEX_ADD) ? "add" : "del",
61f723
                        encoded (&key, encbuf));
61f723
         }
61f723
-#endif
61f723
 
61f723
         if (NULL != txn) {
61f723
             db_txn = txn->back_txn_txn;
61f723
-- 
61f723
2.13.6
61f723