61f723
From 97f09918ef370c3be5aa64dcfeb3bb21e762f90d Mon Sep 17 00:00:00 2001
61f723
From: Mark Reynolds <mreynolds@redhat.com>
61f723
Date: Tue, 14 Mar 2017 20:23:07 -0400
61f723
Subject: [PATCH 4/5] Issue 49169 - Fix covscan errors
61f723
61f723
src/libsds/bpt/map.c  - resource leak
61f723
ldap/servers/slapd/vattr.c - resource leak
61f723
ldap/servers/slapd/task.c:  resource leaks
61f723
ldap/servers/slapd/str2filter.c - resource leak
61f723
ldap/servers/slapd/pw.c - resource leak
61f723
ldap/servers/slapd/back-ldbm/import-threads.c - resource leak
61f723
ldap/servers/plugins/uiduniq/uid.c:536 - resource leak
61f723
ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c:164 - resource leak
61f723
ldap/servers/plugins/linkedattrs/linked_attrs.c:1672 - resource leak
61f723
ldap/servers/plugins/addn/addn.c:419
61f723
ldap/servers/slapd/ssl.c - dead code
61f723
ldap/servers/slapd/index_subsystem.c - null dereference
61f723
61f723
https://pagure.io/389-ds-base/issue/49169
61f723
61f723
Reviewed by: nkinder & wibrown(Thanks!!)
61f723
61f723
(cherry picked from commit c75126be1edece121826e336141f9b0b9c0bddfd)
61f723
---
61f723
 ldap/servers/plugins/addn/addn.c                |  4 +++-
61f723
 ldap/servers/plugins/linkedattrs/linked_attrs.c |  2 ++
61f723
 ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c    |  1 +
61f723
 ldap/servers/plugins/uiduniq/uid.c              |  6 +++++-
61f723
 ldap/servers/slapd/back-ldbm/import-threads.c   |  1 +
61f723
 ldap/servers/slapd/index_subsystem.c            | 27 +++++++++++++------------
61f723
 ldap/servers/slapd/pw.c                         |  1 +
61f723
 ldap/servers/slapd/pw_verify.c                  |  1 -
61f723
 ldap/servers/slapd/ssl.c                        |  8 +++-----
61f723
 ldap/servers/slapd/str2filter.c                 |  1 +
61f723
 ldap/servers/slapd/task.c                       |  3 +--
61f723
 ldap/servers/slapd/vattr.c                      |  6 +++---
61f723
 src/libsds/sds/bpt/map.c                        |  1 +
61f723
 13 files changed, 36 insertions(+), 26 deletions(-)
61f723
61f723
diff --git a/ldap/servers/plugins/addn/addn.c b/ldap/servers/plugins/addn/addn.c
61f723
index 3abc112..6ba7833 100644
61f723
--- a/ldap/servers/plugins/addn/addn.c
61f723
+++ b/ldap/servers/plugins/addn/addn.c
61f723
@@ -415,7 +415,9 @@ addn_start(Slapi_PBlock *pb)
61f723
     domain = slapi_entry_attr_get_charptr(plugin_entry, "addn_default_domain");
61f723
 
61f723
     if (domain == NULL) {
61f723
-        slapi_log_err(SLAPI_LOG_ERR, plugin_name, "addn_start: CRITICAL: No default domain in configuration, you must set addn_default_domain!\n");
61f723
+        slapi_log_err(SLAPI_LOG_ERR, plugin_name,
61f723
+                "addn_start: CRITICAL: No default domain in configuration, you must set addn_default_domain!\n");
61f723
+        slapi_ch_free((void**)&config);
61f723
         return SLAPI_PLUGIN_FAILURE;
61f723
     }
61f723
 
61f723
diff --git a/ldap/servers/plugins/linkedattrs/linked_attrs.c b/ldap/servers/plugins/linkedattrs/linked_attrs.c
61f723
index b5adb21..d046542 100644
61f723
--- a/ldap/servers/plugins/linkedattrs/linked_attrs.c
61f723
+++ b/ldap/servers/plugins/linkedattrs/linked_attrs.c
61f723
@@ -1669,6 +1669,8 @@ linked_attrs_mod_post_op(Slapi_PBlock *pb)
61f723
             /* Bail out if the plug-in close function was just called. */
61f723
             if (!slapi_plugin_running(pb)) {
61f723
                 linked_attrs_unlock();
61f723
+                slapi_mod_free(&next_mod);
61f723
+                slapi_mods_free(&smods);
61f723
                 return SLAPI_PLUGIN_SUCCESS;
61f723
             }
61f723
 
61f723
diff --git a/ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c b/ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c
61f723
index 1b3e555..b228700 100644
61f723
--- a/ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c
61f723
+++ b/ldap/servers/plugins/pwdstorage/pbkdf2_pwd.c
61f723
@@ -161,6 +161,7 @@ pbkdf2_sha256_pw_enc(const char *pwd)
61f723
      */
61f723
     if ( pbkdf2_sha256_hash(hash + PBKDF2_ITERATIONS_LENGTH + PBKDF2_SALT_LENGTH, PBKDF2_HASH_LENGTH, &passItem, &saltItem, PBKDF2_ITERATIONS) != SECSuccess ) {
61f723
         slapi_log_err(SLAPI_LOG_ERR, (char *)schemeName, "Could not generate pbkdf2_sha256_hash!\n");
61f723
+        slapi_ch_free_string(&enc;;
61f723
         return NULL;
61f723
     }
61f723
 
61f723
diff --git a/ldap/servers/plugins/uiduniq/uid.c b/ldap/servers/plugins/uiduniq/uid.c
61f723
index ae9320e..46554b2 100644
61f723
--- a/ldap/servers/plugins/uiduniq/uid.c
61f723
+++ b/ldap/servers/plugins/uiduniq/uid.c
61f723
@@ -533,7 +533,11 @@ create_filter(const char **attributes, const struct berval *value, const char *r
61f723
 
61f723
           /* Place value in filter */
61f723
           if (ldap_quote_filter_value(value->bv_val, value->bv_len,
61f723
-            fp, max-fp, &valueLen)) { slapi_ch_free((void**)&filter); return 0; }
61f723
+                                      fp, max-fp, &valueLen)) {
61f723
+              slapi_ch_free((void**)&filter);
61f723
+              slapi_ch_free((void**)&attrLen);
61f723
+              return 0;
61f723
+          }
61f723
           fp += valueLen;
61f723
 
61f723
           strcpy(fp, ")");
61f723
diff --git a/ldap/servers/slapd/back-ldbm/import-threads.c b/ldap/servers/slapd/back-ldbm/import-threads.c
61f723
index 5b81427..087103b 100644
61f723
--- a/ldap/servers/slapd/back-ldbm/import-threads.c
61f723
+++ b/ldap/servers/slapd/back-ldbm/import-threads.c
61f723
@@ -1647,6 +1647,7 @@ upgradedn_producer(void *param)
61f723
                 }
61f723
                 e = slapi_str2entry_ext(normdn, NULL, data.dptr, 
61f723
                                         SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT);
61f723
+                slapi_ch_free_string(&rdn;;
61f723
             }
61f723
         } else {
61f723
             e = 
61f723
diff --git a/ldap/servers/slapd/index_subsystem.c b/ldap/servers/slapd/index_subsystem.c
61f723
index 57d4f58..8f9fe6d 100644
61f723
--- a/ldap/servers/slapd/index_subsystem.c
61f723
+++ b/ldap/servers/slapd/index_subsystem.c
61f723
@@ -185,27 +185,28 @@ static int index_subsys_index_matches_filter(indexEntry *index, Slapi_Filter *f)
61f723
  */
61f723
 int index_subsys_assign_filter_decoders(Slapi_PBlock *pb)
61f723
 {
61f723
-	int				rc;
61f723
+	int				rc = 0;
61f723
 	Slapi_Filter	*f;
61f723
 	char			*subsystem = "index_subsys_assign_filter_decoders";
61f723
 	char			logbuf[ 1024 ];
61f723
 
61f723
 	/* extract the filter */
61f723
 	slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, &f);   
61f723
+	if (f) {
61f723
+		if ( loglevel_is_set( LDAP_DEBUG_FILTER )) {
61f723
+			logbuf[0] = '\0';
61f723
+			slapi_log_err(SLAPI_LOG_DEBUG, subsystem, "before: %s\n",
61f723
+					slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
61f723
+		}
61f723
 
61f723
-	if ( loglevel_is_set( LDAP_DEBUG_FILTER ) && NULL != f ) {
61f723
-		logbuf[0] = '\0';
61f723
-		slapi_log_err(SLAPI_LOG_DEBUG, subsystem, "before: %s\n",
61f723
-				slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
61f723
-	}
61f723
-
61f723
-	/* find decoders */
61f723
-	rc = index_subsys_assign_decoders(f);
61f723
+		/* find decoders */
61f723
+		rc = index_subsys_assign_decoders(f);
61f723
 
61f723
-	if ( loglevel_is_set( LDAP_DEBUG_FILTER ) && NULL != f ) {
61f723
-		logbuf[0] = '\0';
61f723
-		slapi_log_err(SLAPI_LOG_DEBUG, subsystem, " after: %s\n",
61f723
-				slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
61f723
+		if ( loglevel_is_set( LDAP_DEBUG_FILTER )) {
61f723
+			logbuf[0] = '\0';
61f723
+			slapi_log_err(SLAPI_LOG_DEBUG, subsystem, " after: %s\n",
61f723
+					slapi_filter_to_string(f, logbuf, sizeof(logbuf)));
61f723
+		}
61f723
 	}
61f723
 
61f723
 	return rc;
61f723
diff --git a/ldap/servers/slapd/pw.c b/ldap/servers/slapd/pw.c
61f723
index 215c9eb..378d148 100644
61f723
--- a/ldap/servers/slapd/pw.c
61f723
+++ b/ldap/servers/slapd/pw.c
61f723
@@ -1512,6 +1512,7 @@ check_trivial_words (Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Value **vals, char
61f723
 			ep = sp + strlen(sp);
61f723
 			ep = ldap_utf8prevn(sp, ep, toklen);
61f723
 			if (!ep || (sp >= ep)) {
61f723
+				slapi_ch_free_string(&sp);
61f723
 				continue;
61f723
 			}
61f723
 			/* See if the password contains the value */
61f723
diff --git a/ldap/servers/slapd/pw_verify.c b/ldap/servers/slapd/pw_verify.c
61f723
index 529bb83..a9fd9ec 100644
61f723
--- a/ldap/servers/slapd/pw_verify.c
61f723
+++ b/ldap/servers/slapd/pw_verify.c
61f723
@@ -103,7 +103,6 @@ pw_verify_be_dn(Slapi_PBlock *pb, Slapi_Entry **referral)
61f723
 int
61f723
 pw_validate_be_dn(Slapi_PBlock *pb, Slapi_Entry **referral)
61f723
 {
61f723
-    int rc = 0;
61f723
     Slapi_Backend *be = NULL;
61f723
     Slapi_DN *pb_sdn;
61f723
     struct berval *cred;
61f723
diff --git a/ldap/servers/slapd/ssl.c b/ldap/servers/slapd/ssl.c
61f723
index f35b3f1..050e7b5 100644
61f723
--- a/ldap/servers/slapd/ssl.c
61f723
+++ b/ldap/servers/slapd/ssl.c
61f723
@@ -1418,12 +1418,10 @@ slapd_ssl_init()
61f723
       errorCode = PR_GetError();
61f723
       slapd_SSL_error("Failed to retrieve SSL "
61f723
                      "configuration information ("
61f723
-                     SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
61f723
+                     SLAPI_COMPONENT_NAME_NSPR " error %d - not found): "
61f723
                      "nssslSessionTimeout: %s ",
61f723
-                     errorCode, slapd_pr_strerror(errorCode),
61f723
-             (val ? "found" : "not found"));
61f723
-      slapi_ch_free((void **) &val;;
61f723
-      slapi_ch_free((void **) &ciphers);
61f723
+                     errorCode, slapd_pr_strerror(errorCode));
61f723
+      slapi_ch_free((void **)&ciphers);
61f723
       freeConfigEntry( &entry );
61f723
       return -1;
61f723
     }
61f723
diff --git a/ldap/servers/slapd/str2filter.c b/ldap/servers/slapd/str2filter.c
61f723
index ebd5c5d..744c93f 100644
61f723
--- a/ldap/servers/slapd/str2filter.c
61f723
+++ b/ldap/servers/slapd/str2filter.c
61f723
@@ -344,6 +344,7 @@ str2simple( char *str , int unescape_filter)
61f723
 			*endp = '\0';
61f723
 			rc = _parse_ext_filter(str, extp, &f->f_mr_type, &f->f_mr_oid, &f->f_mr_dnAttrs);
61f723
 			if (rc) {
61f723
+				slapi_filter_free(f, 1);
61f723
 				return NULL; /* error */
61f723
 			} else {
61f723
 				f->f_choice = LDAP_FILTER_EXTENDED;
61f723
diff --git a/ldap/servers/slapd/task.c b/ldap/servers/slapd/task.c
61f723
index ad52e9d..eabd517 100644
61f723
--- a/ldap/servers/slapd/task.c
61f723
+++ b/ldap/servers/slapd/task.c
61f723
@@ -2389,7 +2389,6 @@ task_fixup_tombstones_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
61f723
         slapi_task_finish(task, *returncode);
61f723
         slapi_ch_array_free(base);
61f723
         slapi_ch_free((void **)&task_data);
61f723
-        return SLAPI_DSE_CALLBACK_ERROR;
61f723
     }
61f723
 
61f723
 done:
61f723
@@ -2507,9 +2506,9 @@ task_des2aes(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
61f723
 error:
61f723
     if (rc == SLAPI_DSE_CALLBACK_ERROR){
61f723
         slapi_ch_array_free(bases);
61f723
-        slapi_ch_array_free(suffix);
61f723
         slapi_ch_free((void **)&task_data);
61f723
     }
61f723
+    slapi_ch_array_free(suffix);
61f723
     return rc;
61f723
 }
61f723
 
61f723
diff --git a/ldap/servers/slapd/vattr.c b/ldap/servers/slapd/vattr.c
61f723
index 34665de..599b54e 100644
61f723
--- a/ldap/servers/slapd/vattr.c
61f723
+++ b/ldap/servers/slapd/vattr.c
61f723
@@ -753,10 +753,10 @@ slapi_vattr_values_get_sp(vattr_context *c,
61f723
   }
61f723
   if (use_local_ctx) {
61f723
     /* slapi_pblock_destroy cleans up pb_vattr_context, as well */
61f723
-    slapi_pblock_destroy(local_pb);
61f723
-  } else {
61f723
-    vattr_context_ungrok(&c);
61f723
+	slapi_pblock_destroy(local_pb);
61f723
+	ctx->pb = NULL;
61f723
   }
61f723
+  vattr_context_ungrok(&ctx;;
61f723
   return rc;
61f723
 }
61f723
 
61f723
diff --git a/src/libsds/sds/bpt/map.c b/src/libsds/sds/bpt/map.c
61f723
index 4205aa5..2c3468b 100644
61f723
--- a/src/libsds/sds/bpt/map.c
61f723
+++ b/src/libsds/sds/bpt/map.c
61f723
@@ -18,6 +18,7 @@ sds_bptree_map_nodes(sds_bptree_instance *binst, sds_bptree_node *root, sds_resu
61f723
     sds_bptree_node_list *tail = cur;
61f723
 
61f723
     if (binst == NULL) {
61f723
+        sds_free(cur);
61f723
         return SDS_NULL_POINTER;
61f723
     }
61f723
 
61f723
-- 
61f723
2.9.3
61f723