diff mbox series

[1/5] semodule_link: avoid NULL dereference on OOM

Message ID 20231109135315.44095-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 8547846ecdb7
Delegated to: Petr Lautrbach
Headers show
Series [1/5] semodule_link: avoid NULL dereference on OOM | expand

Commit Message

Christian Göttsche Nov. 9, 2023, 1:53 p.m. UTC
In case the initial calloc(3) call fails the variable mods is still NULL
while its size hint num_mods is set.

Reported by Clang Analyzer:

    semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
      182 |                 sepol_module_package_free(mods[i]);
          |                                           ^~~~~~~

Fixes: 63e798a2034a ("semodule_link: update")

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 semodule-utils/semodule_link/semodule_link.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

James Carter Nov. 13, 2023, 9:43 p.m. UTC | #1
On Thu, Nov 9, 2023 at 8:53 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> In case the initial calloc(3) call fails the variable mods is still NULL
> while its size hint num_mods is set.
>
> Reported by Clang Analyzer:
>
>     semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
>       182 |                 sepol_module_package_free(mods[i]);
>           |                                           ^~~~~~~
>
> Fixes: 63e798a2034a ("semodule_link: update")
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

For these five patches:
Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  semodule-utils/semodule_link/semodule_link.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/semodule-utils/semodule_link/semodule_link.c b/semodule-utils/semodule_link/semodule_link.c
> index 0f157bd9..58fca34d 100644
> --- a/semodule-utils/semodule_link/semodule_link.c
> +++ b/semodule-utils/semodule_link/semodule_link.c
> @@ -178,9 +178,11 @@ failure:
>         ret = EXIT_FAILURE;
>
>  cleanup:
> -       for (i = 0; i < num_mods; i++)
> -               sepol_module_package_free(mods[i]);
> -       free(mods);
> +       if (mods) {
> +               for (i = 0; i < num_mods; i++)
> +                       sepol_module_package_free(mods[i]);
> +               free(mods);
> +       }
>         sepol_module_package_free(base);
>
>         return ret;
> --
> 2.42.0
>
James Carter Nov. 16, 2023, 2:56 p.m. UTC | #2
On Mon, Nov 13, 2023 at 4:43 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Thu, Nov 9, 2023 at 8:53 AM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > In case the initial calloc(3) call fails the variable mods is still NULL
> > while its size hint num_mods is set.
> >
> > Reported by Clang Analyzer:
> >
> >     semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
> >       182 |                 sepol_module_package_free(mods[i]);
> >           |                                           ^~~~~~~
> >
> > Fixes: 63e798a2034a ("semodule_link: update")
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> For these five patches:
> Acked-by: James Carter <jwcart2@gmail.com>
>

These five patches have been merged.
Thanks,
Jim

> > ---
> >  semodule-utils/semodule_link/semodule_link.c | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/semodule-utils/semodule_link/semodule_link.c b/semodule-utils/semodule_link/semodule_link.c
> > index 0f157bd9..58fca34d 100644
> > --- a/semodule-utils/semodule_link/semodule_link.c
> > +++ b/semodule-utils/semodule_link/semodule_link.c
> > @@ -178,9 +178,11 @@ failure:
> >         ret = EXIT_FAILURE;
> >
> >  cleanup:
> > -       for (i = 0; i < num_mods; i++)
> > -               sepol_module_package_free(mods[i]);
> > -       free(mods);
> > +       if (mods) {
> > +               for (i = 0; i < num_mods; i++)
> > +                       sepol_module_package_free(mods[i]);
> > +               free(mods);
> > +       }
> >         sepol_module_package_free(base);
> >
> >         return ret;
> > --
> > 2.42.0
> >
diff mbox series

Patch

diff --git a/semodule-utils/semodule_link/semodule_link.c b/semodule-utils/semodule_link/semodule_link.c
index 0f157bd9..58fca34d 100644
--- a/semodule-utils/semodule_link/semodule_link.c
+++ b/semodule-utils/semodule_link/semodule_link.c
@@ -178,9 +178,11 @@  failure:
 	ret = EXIT_FAILURE;
 
 cleanup:
-	for (i = 0; i < num_mods; i++)
-		sepol_module_package_free(mods[i]);
-	free(mods);
+	if (mods) {
+		for (i = 0; i < num_mods; i++)
+			sepol_module_package_free(mods[i]);
+		free(mods);
+	}
 	sepol_module_package_free(base);
 
 	return ret;