All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jie Lu <lujie54@huawei.com>
To: <selinux@vger.kernel.org>
Subject: [PATCH 1/3] libselinux: fix potential NULL reference and memory leak in audit2why
Date: Mon, 5 Dec 2022 17:36:42 +0800	[thread overview]
Message-ID: <20221205093644.703107-1-lujie54@huawei.com> (raw)

In audit2why.c add return check for memory allocation. And free every element
in the boollist when function fails.

Signed-off-by: Jie Lu <lujie54@huawei.com>
---
 libselinux/src/audit2why.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
index ba1a66eb..742b4ff5 100644
--- a/libselinux/src/audit2why.c
+++ b/libselinux/src/audit2why.c
@@ -55,7 +55,16 @@ static int load_booleans(const sepol_bool_t * boolean,
 			 void *arg __attribute__ ((__unused__)))
 {
 	boollist[boolcnt] = malloc(sizeof(struct boolean_t));
+	if (!boollist[boolcnt]) {
+		PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
+		return -1;
+	}
 	boollist[boolcnt]->name = strdup(sepol_bool_get_name(boolean));
+	if (!boollist[boolcnt]->name) {
+		PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
+		free(boollist[boolcnt]);
+		return -1;
+	}
 	boollist[boolcnt]->active = sepol_bool_get_value(boolean);
 	boolcnt++;
 	return 0;
@@ -149,6 +158,11 @@ static int check_booleans(struct boolean_t **bools)
 
 	if (fcnt > 0) {
 		*bools = calloc(sizeof(struct boolean_t), fcnt + 1);
+		if (!*bools) {
+			PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
+			free(foundlist);
+			return 0;
+		}
 		struct boolean_t *b = *bools;
 		for (i = 0; i < fcnt; i++) {
 			int ctr = foundlist[i];
@@ -278,14 +292,22 @@ static int __policy_init(const char *init_path)
 	return 0;
 
 err:
-	if (boollist)
-		free(boollist);
+	if (boollist) {
+		for (i = 0; i < boolcnt; i++) {
+                        free(boollist[i]->name);
+                        free(boollist[i]);
+                }
+                free(boollist);
+                boollist = NULL;
+                boolcnt = 0;
+	}
 	if (avc){
 		if (avc->handle)
 			sepol_handle_destroy(avc->handle);
 		if (avc->policydb)
 			sepol_policydb_free(avc->policydb);
 		free(avc);
+		avc = NULL;
 	}
 	if (pf)
 		sepol_policy_file_free(pf);
-- 
2.27.0


             reply	other threads:[~2022-12-05 12:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-05  9:36 Jie Lu [this message]
2022-12-05  9:36 ` [PATCH 2/3] libsepol: fix potential memory leak in common_copy_callback Jie Lu
2022-12-16 16:06   ` James Carter
2022-12-05  9:36 ` [PATCH 3/3] policycoreutils: fix potential NULL reference in load_checks Jie Lu
2022-12-16 16:07   ` James Carter
2023-01-11 15:51     ` James Carter
2022-12-16 16:03 ` [PATCH 1/3] libselinux: fix potential NULL reference and memory leak in audit2why James Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221205093644.703107-1-lujie54@huawei.com \
    --to=lujie54@huawei.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.