diff mbox series

libselinux: fix memory leaks on the audit2why module init

Message ID 20221115115536.2647075-1-lujie54@huawei.com (mailing list archive)
State Accepted
Commit 42f7f2fdcf1a
Headers show
Series libselinux: fix memory leaks on the audit2why module init | expand

Commit Message

Jie Lu Nov. 15, 2022, 11:55 a.m. UTC
Signed-off-by: Jie Lu <lujie54@huawei.com>
---
 libselinux/src/audit2why.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

Comments

James Carter Nov. 16, 2022, 8:33 p.m. UTC | #1
On Tue, Nov 15, 2022 at 9:31 AM Jie Lu <lujie54@huawei.com> wrote:
>
> 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/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
> index 44a9a341..ba1a66eb 100644
> --- a/libselinux/src/audit2why.c
> +++ b/libselinux/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
>
James Carter Nov. 21, 2022, 8:56 p.m. UTC | #2
On Wed, Nov 16, 2022 at 3:33 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Tue, Nov 15, 2022 at 9:31 AM Jie Lu <lujie54@huawei.com> wrote:
> >
> > Signed-off-by: Jie Lu <lujie54@huawei.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Merged.
Thanks,
Jim

> > ---
> >  libselinux/src/audit2why.c | 32 +++++++++++++++++++++++---------
> >  1 file changed, 23 insertions(+), 9 deletions(-)
> >
> > diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
> > index 44a9a341..ba1a66eb 100644
> > --- a/libselinux/src/audit2why.c
> > +++ b/libselinux/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
> >
diff mbox series

Patch

diff --git a/libselinux/src/audit2why.c b/libselinux/src/audit2why.c
index 44a9a341..ba1a66eb 100644
--- a/libselinux/src/audit2why.c
+++ b/libselinux/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) {