Skip to contentSkip to conversation

libsemanage: fix resource leaks

PATCH In review [email protected] 2 participants 3 messages Aug 22, 2005 – Aug 24, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsemanage: fix resource leaks

This is the last of three patches against libsemanage. Just needed to hit these on my way to checking policycoreutils.

The following fixes a bug found by Coverity in libsemanage:

In both semod_get_active_modules_list() and semod_get_sandbox_modules_list() it is possible to return -1 after names and versions have been defined (if asprintf fails). In addition it is obviously possible for names to be malloced and the versions malloc to fail.

module_direct_api.c +8 -0
Index: src/module_direct_api.c
===================================================================
@@ -260,6 +260,10 @@ int semod_direct_list_modules(char **res
         char **names, **versions;
         *results = NULL;
         if ((retval = semod_get_active_modules_list (&names, &versions, &len)) < 0) {
+		if (names)
+			free(names);
+		if (versions)
+			free(versions);
                 return retval;
         }
         return list_modules_common (results, names, versions, len);
@@ -278,6 +282,10 @@ int semod_direct_list_sandbox(char *sand
         *results = NULL;
         if ((retval = semod_get_sandbox_modules_list (sandbox,
                                                       &names, &versions, &len)) < 0) {
+		if (names)
+			free(names);
+		if (versions)
+			free(versions);
                 return retval;
         }
         return list_modules_common (results, names, versions, len);

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 27 lines · On Mon, 2005-08-22 at 06:46 -0500, [email protected] wrote:

This is the last of three patches against libsemanage. Just needed to hit these on my way to checking policycoreutils.

The following fixes a bug found by Coverity in libsemanage:

In both semod_get_active_modules_list() and semod_get_sandbox_modules_list() it is possible to return -1 after names and versions have been defined (if asprintf fails). In addition it is obviously possible for names to be malloced and the versions malloc to fail.

Index: src/module_direct_api.c
===================================================================

module_direct_api.c +4 -0
@@ -260,6 +260,10 @@ int semod_direct_list_modules(char **res
         char **names, **versions;
         *results = NULL;
         if ((retval = semod_get_active_modules_list (&names, &versions, &len)) < 0) {
+		if (names)
+			free(names);
+		if (versions)
+			free(versions);
                 return retval;
         }
         return list_modules_common (results, names, versions, len);

module_direct_api.c int semod_direct_list_sandbox(char *sand · line 291

Quoted 10 lines
module_direct_api.c +4 -0
@@ -278,6 +282,10 @@ int semod_direct_list_sandbox(char *sand
         *results = NULL;
         if ((retval = semod_get_sandbox_modules_list (sandbox,
                                                       &names, &versions, &len)) < 0) {
+		if (names)
+			free(names);
+		if (versions)
+			free(versions);
                 return retval;
         }
         return list_modules_common (results, names, versions, len);

Looks like we need to free the individual elements as well. Any reason this isn't handled internally by semod_build_modules_list upon an error?

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw
Quoted 3 lines · Stephen Smalley

On Mon, 2005-08-22 at 06:46 -0500, [email protected] wrote:
Looks like we need to free the individual elements as well. Any reason this isn't handled internally by semod_build_modules_list upon an error?

How about the following patch?

thanks,
-serge

module_store.c renamed from module_store.c.orig +22 -2
@@ -721,13 +721,15 @@ static int semod_build_modules_list (con
         }	
 	if ((*names = (char **) calloc (*len, sizeof (**names))) == NULL ||
             (*versions = (char **) calloc (*len, sizeof (**versions))) == NULL) {
-                return -1;
+                goto err;
 	}
+	memset(names, 0, *len * sizeof(**names));
+	memset(versions, 0, *len * sizeof(**versions));
 	for (i = 0; i < *len; i++) {
                 char *filename, *name, *version;
                 int type;
                 if (asprintf (&filename, "%s/%s", path, namelist[i]->d_name) == -1) {
-                        return -1;
+                        goto err;
                 }
 		if ((pf.fp = fopen (filename, "r")) == NULL) {
                         free (filename);
@@ -748,6 +750,24 @@ static int semod_build_modules_list (con
 	}
         free(namelist);
         return 0;
+
+err:
+	if (*names) {
+		for (i=0; i< *len; i++) {
+			if ((*names)[i])
+				free((*names)[i]);
+		}
+		free(*names);
+	}
+	if (*versions) {
+		for (i=0; i< *len; i++) {
+			if ((*versions)[i])
+				free((*versions)[i]);
+		}
+		free(*versions);
+	}
+
+	return -1;
 }
 
 /********************* accessor functions below *********************/

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.