changeset 6022:1eb39df1e9cb

6652170 iSCSI Target leaks memory in the current SCF implementation
author jdunham
date Fri, 15 Feb 2008 07:46:38 -0800
parents 667d5d93f842
children 9adf47ae28c4
files usr/src/cmd/iscsi/iscsitgtd/mgmt_scf.c usr/src/cmd/iscsi/iscsitgtd/util.c
diffstat 2 files changed, 43 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/iscsi/iscsitgtd/mgmt_scf.c	Thu Feb 14 19:07:51 2008 -0800
+++ b/usr/src/cmd/iscsi/iscsitgtd/mgmt_scf.c	Fri Feb 15 07:46:38 2008 -0800
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -98,17 +98,28 @@
 		if (h->t_scope != NULL) {
 			unbind = 1;
 			scf_scope_destroy(h->t_scope);
+			h->t_scope = NULL;
 		}
-		if (h->t_service != NULL)
+		if (h->t_instance != NULL) {
+			scf_instance_destroy(h->t_instance);
+			h->t_instance = NULL;
+		}
+		if (h->t_service != NULL) {
 			scf_service_destroy(h->t_service);
-		if (h->t_pg != NULL)
+			h->t_service = NULL;
+		}
+		if (h->t_pg != NULL) {
 			scf_pg_destroy(h->t_pg);
+			h->t_pg = NULL;
+		}
 		if (h->t_handle != NULL) {
 			if (unbind)
 				(void) scf_handle_unbind(h->t_handle);
 			scf_handle_destroy(h->t_handle);
+			h->t_handle = NULL;
 		}
 		free(h);
+		h = NULL;
 	}
 }
 
@@ -238,6 +249,8 @@
 	scf_property_t *prop = NULL;
 	scf_value_t *value = NULL;
 	scf_iter_t *iter = NULL;
+	scf_iter_t *iter_v = NULL;
+	scf_iter_t *iter_pv = NULL;
 	char pname[32];
 	char valuebuf[MAXPATHLEN];
 	char passcode[32];
@@ -245,11 +258,12 @@
 	tgt_node_t	*n;
 	tgt_node_t	*pn;
 	tgt_node_t	*vn;
+	Boolean_t	status = False;
 
 	h = mgmt_handle_init();
 
 	if (h == NULL)
-		return (False);
+		return (status);
 
 	prop = scf_property_create(h->t_handle);
 	value = scf_value_create(h->t_handle);
@@ -306,7 +320,6 @@
 	}
 
 	while (scf_iter_next_pg(iter, h->t_pg) > 0) {
-		scf_iter_t *iter_v = scf_iter_create(h->t_handle);
 		char *iname;
 
 		scf_pg_get_name(h->t_pg, pname, sizeof (pname));
@@ -325,13 +338,13 @@
 		n = tgt_node_alloc(pname, String, iname);
 		if (n == NULL)
 			goto error;
+
+		iter_v = scf_iter_create(h->t_handle);
 		if (scf_iter_pg_properties(iter_v, h->t_pg) == -1) {
-			scf_iter_destroy(iter_v);
 			goto error;
 		}
 		while (scf_iter_next_property(iter_v, prop) > 0) {
 			/* there may be many values in one property */
-			scf_iter_t *iter_pv = scf_iter_create(h->t_handle);
 			char *vname;
 
 			scf_property_get_name(prop, pname, sizeof (pname));
@@ -357,6 +370,8 @@
 					goto error;
 				tgt_node_add(n, pn);
 				*vname = '\0';
+
+				iter_pv = scf_iter_create(h->t_handle);
 				scf_iter_property_values(iter_pv, prop);
 				while (scf_iter_next_value(iter_pv, value)
 				    > 0) {
@@ -368,10 +383,13 @@
 						goto error;
 					tgt_node_add(pn, vn);
 				}
+				scf_iter_destroy(iter_pv);
+				iter_pv = NULL;
 			}
 		}
 		tgt_node_add(*node, n);
 		scf_iter_destroy(iter_v);
+		iter_v = NULL;
 	}
 
 	/* chap-secrets are stored in "passwords" pgroup as "application" */
@@ -418,21 +436,21 @@
 			}
 		}
 	}
-	(void) pthread_mutex_unlock(&scf_conf_mutex);
-	scf_iter_destroy(iter);
-	scf_value_destroy(value);
-	scf_property_destroy(prop);
-	return (True);
 
+	status = True;
 error:
-	if (*node != NULL)
+	if ((status != True) && (*node != NULL))
 		tgt_node_free(*node);
 	(void) pthread_mutex_unlock(&scf_conf_mutex);
+	if (iter_pv != NULL)
+		scf_iter_destroy(iter_pv);
+	if (iter_v != NULL)
+		scf_iter_destroy(iter_v);
 	scf_iter_destroy(iter);
 	scf_value_destroy(value);
 	scf_property_destroy(prop);
 	mgmt_handle_fini(h);
-	return (False);
+	return (status);
 }
 
 static int
@@ -771,11 +789,12 @@
 	char pgname[64];
 	char valuebuf[MAXPATHLEN];
 	tgt_node_t	*n;
+	Boolean_t status = False;
 
 	h = mgmt_handle_init();
 
 	if (h == NULL)
-		return (False);
+		return (status);
 
 	prop = scf_property_create(h->t_handle);
 	value = scf_value_create(h->t_handle);
@@ -821,19 +840,14 @@
 		}
 	}
 
-	(void) pthread_mutex_unlock(&scf_param_mutex);
-	scf_iter_destroy(iter);
-	scf_value_destroy(value);
-	scf_property_destroy(prop);
-	return (True);
-
+	status = True;
 error:
 	(void) pthread_mutex_unlock(&scf_param_mutex);
 	scf_iter_destroy(iter);
 	scf_value_destroy(value);
 	scf_property_destroy(prop);
 	mgmt_handle_fini(h);
-	return (False);
+	return (status);
 }
 
 Boolean_t
@@ -935,7 +949,7 @@
 convert_ret_t
 mgmt_convert_conf()
 {
-	targ_scf_t *h = NULL;
+	targ_scf_t		*h = NULL;
 	xmlTextReaderPtr	r;
 	convert_ret_t		ret = CONVERT_FAIL;
 	int			xml_fd = -1;
--- a/usr/src/cmd/iscsi/iscsitgtd/util.c	Thu Feb 14 19:07:51 2008 -0800
+++ b/usr/src/cmd/iscsi/iscsitgtd/util.c	Fri Feb 15 07:46:38 2008 -0800
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -1649,11 +1649,12 @@
 
 	while ((targ = tgt_node_next(targets_config, XML_ELEMENT_TARG, targ))
 	    != NULL) {
-		if ((tgt_find_value_str(targ, XML_ELEMENT_INAME, &str) ==
-		    True) && (strcmp(str, iname) == 0)) {
+		if (tgt_find_value_str(targ, XML_ELEMENT_INAME, &str) == True) {
+			if (strcmp(str, iname) == 0)
+				ret = strdup(targ->x_value);
 			free(str);
-			ret = strdup(targ->x_value);
-			break;
+			if (ret != NULL)
+				break;
 		}
 	}
 	return (ret);