libselinux: fix memory leaks on the audit2why module init

Signed-off-by: Jie Lu <lujie54@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Jie Lu 2022-11-15 19:55:36 +08:00 committed by James Carter
parent cd5de9230d
commit 42f7f2fdcf

View File

@ -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) {