Re: [PATCH] libselinux: fix memory leaks on the audit2why module init

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Nov 15, 2022 at 9:31 AM Jie Lu <lujie54@xxxxxxxxxx> wrote:
>
> Signed-off-by: Jie Lu <lujie54@xxxxxxxxxx>

Acked-by: James Carter <jwcart2@xxxxxxxxx>

> ---
>  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
>



[Index of Archives]     [Selinux Refpolicy]     [Linux SGX]     [Fedora Users]     [Fedora Desktop]     [Yosemite Photos]     [Yosemite Camping]     [Yosemite Campsites]     [KDE Users]     [Gnome Users]

  Powered by Linux