libselinux/backport-libselinux-fix-memory-leaks-on-the-audit2why-module-init.patch
zgzxx 990a13251e backport upstream patches
(cherry picked from commit a7bf1057839c82d554d24c1220ddc1609098b96c)
2023-06-13 19:18:19 +08:00

95 lines
2.5 KiB
Diff

From 42f7f2fdcf1a38b6018933132ea6a35dc41bdeff Mon Sep 17 00:00:00 2001
From: Jie Lu <lujie54@huawei.com>
Date: Tue, 15 Nov 2022 19:55:36 +0800
Subject: [PATCH] libselinux: fix memory leaks on the audit2why module init
Signed-off-by: Jie Lu <lujie54@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
---
libselinux/src/audit2why.c | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/src/audit2why.c b/src/audit2why.c
index 44a9a341..ba1a66eb 100644
--- a/src/audit2why.c
+++ b/src/audit2why.c
@@ -191,7 +191,7 @@ static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args)
static int __policy_init(const char *init_path)
{
- FILE *fp;
+ FILE *fp = NULL;
const char *curpolicy;
char errormsg[PATH_MAX+1024+20];
struct sepol_policy_file *pf = NULL;
@@ -235,18 +235,17 @@ static int __policy_init(const char *init_path)
snprintf(errormsg, sizeof(errormsg),
"policydb_init failed: %m\n");
PyErr_SetString( PyExc_RuntimeError, errormsg);
- fclose(fp);
- return 1;
+ goto err;
}
sepol_policy_file_set_fp(pf, fp);
if (sepol_policydb_read(avc->policydb, pf)) {
snprintf(errormsg, sizeof(errormsg),
"invalid binary policy %s\n", curpolicy);
PyErr_SetString( PyExc_ValueError, errormsg);
- fclose(fp);
- return 1;
+ goto err;
}
fclose(fp);
+ fp = NULL;
sepol_set_policydb(&avc->policydb->p);
avc->handle = sepol_handle_create();
/* Turn off messages */
@@ -256,13 +255,13 @@ static int __policy_init(const char *init_path)
avc->policydb, &cnt);
if (rc < 0) {
PyErr_SetString( PyExc_RuntimeError, "unable to get bool count\n");
- return 1;
+ goto err;
}
boollist = calloc(cnt, sizeof(*boollist));
if (!boollist) {
PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
- return 1;
+ goto err;
}
sepol_bool_iterate(avc->handle, avc->policydb,
@@ -273,11 +272,26 @@ static int __policy_init(const char *init_path)
rc = sepol_sidtab_init(&sidtab);
if (rc < 0) {
PyErr_SetString( PyExc_RuntimeError, "unable to init sidtab\n");
- free(boollist);
- return 1;
+ goto err;
}
sepol_set_sidtab(&sidtab);
return 0;
+
+err:
+ if (boollist)
+ free(boollist);
+ if (avc){
+ if (avc->handle)
+ sepol_handle_destroy(avc->handle);
+ if (avc->policydb)
+ sepol_policydb_free(avc->policydb);
+ free(avc);
+ }
+ if (pf)
+ sepol_policy_file_free(pf);
+ if (fp)
+ fclose(fp);
+ return 1;
}
static PyObject *init(PyObject *self __attribute__((unused)), PyObject *args) {
--
2.27.0