Skip to contentSkip to conversation

Xenstored: eliminate double free and fixes for memory leak

PATCH Peter Teoh 2 participants 5 messages Sep 17, 2007

lore
Share & tools
lore

Peter Teoh

author
#0 lore raw

Please review. This patch fixes a few problems:

a. In handle_input(), which is called from main() in
xenstored_core.c, the conn->in is freed when there is no error, but
conn is freed when there is error. This is inconsistent. Moreover,
the conn is freed again upon exit from handle_input(), inside the main.

b. Another problem is that perms_to_strings() returned a allocated
memory (via realloc()). The only time the value is returned is in
do_get_perms(), but immediately after send_reply(), the pointer is immediately thrown away, resulting in a memory leak condition, as send_reply() does not free any memory.

c. Many of the functions like read_node(), get_parent(),
talloc_asprintf() etc all will allocate memory and return a pointer when memory is allocated, but many times these are not freed.

Another problem identified but which I cannot fix, is that
create_hashtable() can return NULL in low memory condition. But this
is not checked in the recursive function check_store_(). There is no
value to specify error or not in the return value of check_store_()
(declared as void). May be we should change this to unsigned int or
something like that, so that error condition can be returned?

I am still making changes to the same file, but was hesitant to incorporate so many changes as it will make the review more difficult.

I will submit these further changes in the next posting.

xenstore/xenstored_core.c renamed from xenstore/xenstored_core.c.orig +33 -7
@@ -347,6 +347,11 @@ int *talloc_open(const char *pathname, i
 	int *fd;
 
 	fd = talloc(pathname, int);
+	if (!fd) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	*fd = open(pathname, flags, mode);
 	if (*fd < 0) {
 		int saved_errno = errno;
@@ -397,6 +402,11 @@ static struct node *read_node(struct con
 	}
 
 	node = talloc(name, struct node);
+	if (!node) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	node->name = talloc_strdup(node, name);
 	node->parent = NULL;
 	node->tdb = tdb_context(conn);
@@ -493,8 +503,9 @@ static char *get_parent(const char *node
 /* What do parents say? */
 static enum xs_perm_type ask_parents(struct connection *conn, const char *name)
 {
-	struct node *node;
-
+	struct node *node = NULL;
+	enum xs_perm_type status;
+	
 	do {
 		name = get_parent(name);
 		node = read_node(conn, name);
@@ -506,7 +517,10 @@ static enum xs_perm_type ask_parents(str
 	if (!node)
 		corrupt(conn, "No permissions file at root");
 
-	return perm_for_conn(conn, node->perms, node->num_perms);
+	status = perm_for_conn(conn, node->perms, node->num_perms);
+	if (node)
+		talloc_free(node);
+	return status;
 }
 
 /* We have a weird permissions system.  You can allow someone into a
@@ -793,6 +807,11 @@ static struct node *construct_node(struc
 
 	/* Allocate node */
 	node = talloc(name, struct node);
+	if (!node) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	node->tdb = tdb_context(conn);
 	node->name = talloc_strdup(node, name);
 
@@ -808,6 +827,8 @@ static struct node *construct_node(struc
 	node->childlen = node->datalen = 0;
 	node->parent = parent;
 	domain_entry_inc(conn, node);
+	talloc_free(parentname);
+
 	return node;
 }
 
@@ -937,17 +958,19 @@ static void delete_node(struct connectio
 	for (i = 0; i < node->childlen; i += strlen(node->children+i) + 1) {
 		struct node *child;
 
-		child = read_node(conn, 
-				  talloc_asprintf(node, "%s/%s", node->name,
-						  node->children + i));
+		/* talloc_asprintf() will return an allocated memory pointer */
+		char *pnode = talloc_asprintf(node, "%s/%s", node->name, node->children + i));
+		child = read_node(conn, pnode);
 		if (child) {
 			delete_node(conn, child);
+			talloc_free(child);
 		}
 		else {
 			trace("delete_node: No child '%s/%s' found!\n",
 			      node->name, node->children + i);
 			/* Skip it, we've already deleted the parent. */
 		}
+		talloc_free(pnode);
 	}
 }
 
@@ -1067,7 +1090,10 @@ static void do_get_perms(struct connecti
 	if (!strings)
 		send_error(conn, errno);
 	else
+	{
 		send_reply(conn, XS_GET_PERMS, strings, len);
+		talloc_free(strings);
+	}
 }
 
 static void do_set_perms(struct connection *conn, struct buffered_data *in)
@@ -1301,7 +1327,7 @@ static void handle_input(struct connecti
 
 bad_client:
 	/* Kill it. */
-	talloc_free(conn);
+	talloc_free(conn->in);
 }
 
 static void handle_output(struct connection *conn)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

author
#1 lore raw

The changes are too interdependent, and therefore, I am now resubmitting it as a complete patch, instead of splitting it up into
multiple patches. All goes to the same file - xenstored_core.c.

On top of the comments given above, here are the additional comments:

Two FIXME comments added - for which a problem has been identified, but fixing it will incurred a lot of changes in design etc.

First problem is the returned value from get_parent() - it can be either a static value, or a dynamically allocated value (from talloc_asprintf()), and therefore it is very difficult to judge whether to free the memory or not.

Second (minor) problem is during destroy_node(), the allocated memory
within the node structure is not free. These are allocated in
construct_node() function.

Please comments, thanks.

xenstore/xenstored_core.c.orig renamed from xenstore/xenstored_core.c +9 -66
@@ -347,11 +347,6 @@ int *talloc_open(const char *pathname, i
 	int *fd;
 
 	fd = talloc(pathname, int);
-	if (!fd) 
-	{
-		errno = ENOMEM;
-		return NULL;
-	}
 	*fd = open(pathname, flags, mode);
 	if (*fd < 0) {
 		int saved_errno = errno;
@@ -402,11 +397,6 @@ static struct node *read_node(struct con
 	}
 
 	node = talloc(name, struct node);
-	if (!node) 
-	{
-		errno = ENOMEM;
-		return NULL;
-	}
 	node->name = talloc_strdup(node, name);
 	node->parent = NULL;
 	node->tdb = tdb_context(conn);
@@ -503,9 +493,8 @@ static char *get_parent(const char *node
 /* What do parents say? */
 static enum xs_perm_type ask_parents(struct connection *conn, const char *name)
 {
-	struct node *node = NULL;
-	enum xs_perm_type status;
-	
+	struct node *node;
+
 	do {
 		name = get_parent(name);
 		node = read_node(conn, name);
@@ -517,12 +506,7 @@ static enum xs_perm_type ask_parents(str
 	if (!node)
 		corrupt(conn, "No permissions file at root");
 
-	status = perm_for_conn(conn, node->perms, node->num_perms);
-	if (node)
-		talloc_free(node);
-
-	/* FIXME: name as returned from get_parent() can sometimes be allocated memory and sometimes otherwise, and if it is allocated it has to be deallocated here */
-	return status;
+	return perm_for_conn(conn, node->perms, node->num_perms);
 }
 
 /* We have a weird permissions system.  You can allow someone into a
@@ -745,7 +729,6 @@ static void send_directory(struct connec
 	}
 
 	send_reply(conn, XS_DIRECTORY, node->children, node->childlen);
-	talloc_free(node);
 }
 
 static void do_read(struct connection *conn, const char *name)
@@ -760,7 +743,6 @@ static void do_read(struct connection *c
 	}
 
 	send_reply(conn, XS_READ, node->data, node->datalen);
-	talloc_free(node);
 }
 
 static void delete_node_single(struct connection *conn, struct node *node)
@@ -811,11 +793,6 @@ static struct node *construct_node(struc
 
 	/* Allocate node */
 	node = talloc(name, struct node);
-	if (!node) 
-	{
-		errno = ENOMEM;
-		return NULL;
-	}
 	node->tdb = tdb_context(conn);
 	node->name = talloc_strdup(node, name);
 
@@ -831,15 +808,9 @@ static struct node *construct_node(struc
 	node->childlen = node->datalen = 0;
 	node->parent = parent;
 	domain_entry_inc(conn, node);
-	talloc_free(parentname);
-
 	return node;
 }
 
-/* FIXME:  construct_node() as above has a lot of allocated memory in its   */
-/* structure, these therefore have to be deallocated inside destroy_node()  */
-/* is not deallocated */
-
 static int destroy_node(void *_node)
 {
 	struct node *node = _node;
@@ -966,19 +937,17 @@ static void delete_node(struct connectio
 	for (i = 0; i < node->childlen; i += strlen(node->children+i) + 1) {
 		struct node *child;
 
-		/* talloc_asprintf() will return an allocated memory pointer */
-		char *pnode = talloc_asprintf(node, "%s/%s", node->name, node->children + i);
-		child = read_node(conn, pnode);
+		child = read_node(conn, 
+				  talloc_asprintf(node, "%s/%s", node->name,
+						  node->children + i));
 		if (child) {
 			delete_node(conn, child);
-			talloc_free(child);
 		}
 		else {
 			trace("delete_node: No child '%s/%s' found!\n",
 			      node->name, node->children + i);
 			/* Skip it, we've already deleted the parent. */
 		}
-		talloc_free(pnode);
 	}
 }
 
@@ -1020,29 +989,18 @@ static int _rm(struct connection *conn, 
 	/* Delete from parent first, then if we crash, the worst that can
 	   happen is the child will continue to take up space, but will
 	   otherwise be unreachable. */
-	char *parentname = get_parent(name);
-	struct node *parent;
-
-	if (!parentname) 
-		return 0;
-
-	parent = read_node(conn, parentname);
+	struct node *parent = read_node(conn, get_parent(name));
 	if (!parent) {
 		send_error(conn, EINVAL);
-		talloc_free(parentname);
 		return 0;
 	}
 
 	if (!delete_child(conn, parent, basename(name))) {
 		send_error(conn, EINVAL);
-		talloc_free(parent);
-		talloc_free(parentname);
 		return 0;
 	}
 
 	delete_node(conn, node);
-	talloc_free(parent);
-	talloc_free(parentname);
 	return 1;
 }
 
@@ -1067,14 +1025,7 @@ static void do_rm(struct connection *con
 	if (!node) {
 		/* Didn't exist already?  Fine, if parent exists. */
 		if (errno == ENOENT) {
-			char *parentname = get_parent(name);
-
-			if (!parentname)
-			{
-				errno = ENOMEM;
-				return;
-			}
-			node = read_node(conn, parentname);
+			node = read_node(conn, get_parent(name));
 			if (node) {
 				send_ack(conn, XS_RM);
 				return;
@@ -1116,11 +1067,7 @@ static void do_get_perms(struct connecti
 	if (!strings)
 		send_error(conn, errno);
 	else
-	{
 		send_reply(conn, XS_GET_PERMS, strings, len);
-		talloc_free(strings);
-	}
-	talloc_free(node);
 }
 
 static void do_set_perms(struct connection *conn, struct buffered_data *in)
@@ -1151,7 +1098,6 @@ static void do_set_perms(struct connecti
 	perms = talloc_array(node, struct xs_permissions, num);
 	if (!xs_strings_to_perms(perms, num, permstr)) {
 		send_error(conn, errno);
-		talloc_free(node);
 		return;
 	}
 
@@ -1159,7 +1105,6 @@ static void do_set_perms(struct connecti
 	if (domain_is_unprivileged(conn) &&
 	    perms[0].id != node->perms[0].id) {
 		send_error(conn, EPERM);
-		talloc_free(node);
 		return;
 	}
 
@@ -1170,10 +1115,8 @@ static void do_set_perms(struct connecti
 
 	if (!write_node(conn, node)) {
 		send_error(conn, errno);
-		talloc_free(node);
 		return;
 	}
-	talloc_free(node);
 
 	add_change_node(conn->transaction, name, false);
 	fire_watches(conn, name, false);
@@ -1358,7 +1301,7 @@ static void handle_input(struct connecti
 
 bad_client:
 	/* Kill it. */
-	talloc_free(conn->in);
+	talloc_free(conn);
 }
 
 static void handle_output(struct connection *conn)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

author
#2 lore raw

Apologized, the patch file was generated in reverse :-).

xenstore/xenstored_core.c renamed from xenstore/xenstored_core.c.orig +66 -9
@@ -347,6 +347,11 @@ int *talloc_open(const char *pathname, i
 	int *fd;
 
 	fd = talloc(pathname, int);
+	if (!fd) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	*fd = open(pathname, flags, mode);
 	if (*fd < 0) {
 		int saved_errno = errno;
@@ -397,6 +402,11 @@ static struct node *read_node(struct con
 	}
 
 	node = talloc(name, struct node);
+	if (!node) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	node->name = talloc_strdup(node, name);
 	node->parent = NULL;
 	node->tdb = tdb_context(conn);
@@ -493,8 +503,9 @@ static char *get_parent(const char *node
 /* What do parents say? */
 static enum xs_perm_type ask_parents(struct connection *conn, const char *name)
 {
-	struct node *node;
-
+	struct node *node = NULL;
+	enum xs_perm_type status;
+	
 	do {
 		name = get_parent(name);
 		node = read_node(conn, name);
@@ -506,7 +517,12 @@ static enum xs_perm_type ask_parents(str
 	if (!node)
 		corrupt(conn, "No permissions file at root");
 
-	return perm_for_conn(conn, node->perms, node->num_perms);
+	status = perm_for_conn(conn, node->perms, node->num_perms);
+	if (node)
+		talloc_free(node);
+
+	/* FIXME: name as returned from get_parent() can sometimes be allocated memory and sometimes otherwise, and if it is allocated it has to be deallocated here */
+	return status;
 }
 
 /* We have a weird permissions system.  You can allow someone into a
@@ -729,6 +745,7 @@ static void send_directory(struct connec
 	}
 
 	send_reply(conn, XS_DIRECTORY, node->children, node->childlen);
+	talloc_free(node);
 }
 
 static void do_read(struct connection *conn, const char *name)
@@ -743,6 +760,7 @@ static void do_read(struct connection *c
 	}
 
 	send_reply(conn, XS_READ, node->data, node->datalen);
+	talloc_free(node);
 }
 
 static void delete_node_single(struct connection *conn, struct node *node)
@@ -793,6 +811,11 @@ static struct node *construct_node(struc
 
 	/* Allocate node */
 	node = talloc(name, struct node);
+	if (!node) 
+	{
+		errno = ENOMEM;
+		return NULL;
+	}
 	node->tdb = tdb_context(conn);
 	node->name = talloc_strdup(node, name);
 
@@ -808,9 +831,15 @@ static struct node *construct_node(struc
 	node->childlen = node->datalen = 0;
 	node->parent = parent;
 	domain_entry_inc(conn, node);
+	talloc_free(parentname);
+
 	return node;
 }
 
+/* FIXME:  construct_node() as above has a lot of allocated memory in its   */
+/* structure, these therefore have to be deallocated inside destroy_node()  */
+/* is not deallocated */
+
 static int destroy_node(void *_node)
 {
 	struct node *node = _node;
@@ -937,17 +966,19 @@ static void delete_node(struct connectio
 	for (i = 0; i < node->childlen; i += strlen(node->children+i) + 1) {
 		struct node *child;
 
-		child = read_node(conn, 
-				  talloc_asprintf(node, "%s/%s", node->name,
-						  node->children + i));
+		/* talloc_asprintf() will return an allocated memory pointer */
+		char *pnode = talloc_asprintf(node, "%s/%s", node->name, node->children + i);
+		child = read_node(conn, pnode);
 		if (child) {
 			delete_node(conn, child);
+			talloc_free(child);
 		}
 		else {
 			trace("delete_node: No child '%s/%s' found!\n",
 			      node->name, node->children + i);
 			/* Skip it, we've already deleted the parent. */
 		}
+		talloc_free(pnode);
 	}
 }
 
@@ -989,18 +1020,29 @@ static int _rm(struct connection *conn, 
 	/* Delete from parent first, then if we crash, the worst that can
 	   happen is the child will continue to take up space, but will
 	   otherwise be unreachable. */
-	struct node *parent = read_node(conn, get_parent(name));
+	char *parentname = get_parent(name);
+	struct node *parent;
+
+	if (!parentname) 
+		return 0;
+
+	parent = read_node(conn, parentname);
 	if (!parent) {
 		send_error(conn, EINVAL);
+		talloc_free(parentname);
 		return 0;
 	}
 
 	if (!delete_child(conn, parent, basename(name))) {
 		send_error(conn, EINVAL);
+		talloc_free(parent);
+		talloc_free(parentname);
 		return 0;
 	}
 
 	delete_node(conn, node);
+	talloc_free(parent);
+	talloc_free(parentname);
 	return 1;
 }
 
@@ -1025,7 +1067,14 @@ static void do_rm(struct connection *con
 	if (!node) {
 		/* Didn't exist already?  Fine, if parent exists. */
 		if (errno == ENOENT) {
-			node = read_node(conn, get_parent(name));
+			char *parentname = get_parent(name);
+
+			if (!parentname)
+			{
+				errno = ENOMEM;
+				return;
+			}
+			node = read_node(conn, parentname);
 			if (node) {
 				send_ack(conn, XS_RM);
 				return;
@@ -1067,7 +1116,11 @@ static void do_get_perms(struct connecti
 	if (!strings)
 		send_error(conn, errno);
 	else
+	{
 		send_reply(conn, XS_GET_PERMS, strings, len);
+		talloc_free(strings);
+	}
+	talloc_free(node);
 }
 
 static void do_set_perms(struct connection *conn, struct buffered_data *in)
@@ -1098,6 +1151,7 @@ static void do_set_perms(struct connecti
 	perms = talloc_array(node, struct xs_permissions, num);
 	if (!xs_strings_to_perms(perms, num, permstr)) {
 		send_error(conn, errno);
+		talloc_free(node);
 		return;
 	}
 
@@ -1105,6 +1159,7 @@ static void do_set_perms(struct connecti
 	if (domain_is_unprivileged(conn) &&
 	    perms[0].id != node->perms[0].id) {
 		send_error(conn, EPERM);
+		talloc_free(node);
 		return;
 	}
 
@@ -1115,8 +1170,10 @@ static void do_set_perms(struct connecti
 
 	if (!write_node(conn, node)) {
 		send_error(conn, errno);
+		talloc_free(node);
 		return;
 	}
+	talloc_free(node);
 
 	add_change_node(conn->transaction, name, false);
 	fire_watches(conn, name, false);
@@ -1301,7 +1358,7 @@ static void handle_input(struct connecti
 
 bad_client:
 	/* Kill it. */
-	talloc_free(conn);
+	talloc_free(conn->in);
 }
 
 static void handle_output(struct connection *conn)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

author
#3 lore raw

On top of abovementioned problems I missed out this one:

canonicalize() may return an existing pointer as a value - so no memory deallocation is needed, but it also may return a newly
allocated memory - for which deallocation is needed. And currently
all the results returned from canonicalize() are not freed.

Please let me know how to fix this.

Thanks.

#4 lore raw

I don't believe there are any memory leaks in xenstored -- talloc() works somewhat differently than malloc() in that each allocation can specify a parent 'context'. This creates a hierarchy of allocated memory blocks: any memory block freed also automatically frees its children. When making a talloc*() call, the first argument is usually the parent context or parent allocation.

So, in the case of canonicalize(), either it returns the passed-in node, or it returns a new string as a child of node. In the latter case, when the original node string is freed, any string allocated by canonicalize() will also be freed. If you trace any of your other leaks you should find that the memory is actually freed, probably by the talloc_free() in consider_message(). Take a look at xenstore/talloc_guide.txt for more info about talloc.

As for not checking allocations for returning NULL, this is consistently not done throughout xenstored. Mainly because there isn't much we can do in that case, except perhaps log the error and then exit (which basically takes out the host, as xenstored cannot be restarted). Rather than adding NULL checks throughout xenstored, it would be acceptable to add a NULL check in _talloc() which then logs and exits 'cleanly'.

-- Keir

Quoted 15 lines · Peter Teoh

On top of abovementioned problems I missed out this one:

canonicalize() may return an existing pointer as a value - so no memory deallocation is needed, but it also may return a newly
allocated memory - for which deallocation is needed. And currently
all the results returned from canonicalize() are not freed.

Please let me know how to fix this.

Thanks.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel