libselinux: fix memory leaks on the audit2why module init
PATCH 1/1 libselinux: fix memory leaks on the audit2why module init
Signed-off-by Jie Lu <[email protected]>
--- libselinux/src/audit2why.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)
libselinux/src/audit2why.c +23 -9
@@ -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) {
Signature · 1 line
2.27.0
Quoted 2 lines · Jie Lu
Signed-off-by Jie Lu <[email protected]>
Acked-by James Carter <[email protected]>
Quoted 87 lines
--- libselinux/src/audit2why.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)libselinux/src/audit2why.c +23
-10@@ -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
Quoted 7 lines · James Carter
Quoted 2 lines · Jie Lu
Signed-off-by Jie Lu <[email protected]>Acked-by James Carter <[email protected]>
Merged.
Thanks,
Jim
Quoted 87 lines
Quoted 87 lines
--- libselinux/src/audit2why.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-)libselinux/src/audit2why.c +23
-10@@ -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