All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] libsepol: reorder calloc(3) arguments
@ 2024-01-05 18:35 Christian Göttsche
  2024-01-05 18:35 ` [PATCH 2/4] libselinux: " Christian Göttsche
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Christian Göttsche @ 2024-01-05 18:35 UTC (permalink / raw)
  To: selinux

The canonical order of calloc(3) parameters is the number of elements
first and the size of each element second.

Reported by GCC 14:

    kernel_to_conf.c:814:47: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
    kernel_to_conf.c:945:46: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
    kernel_to_conf.c:2109:35: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
    kernel_to_common.c:578:29: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsepol/src/kernel_to_common.c | 2 +-
 libsepol/src/kernel_to_conf.c   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libsepol/src/kernel_to_common.c b/libsepol/src/kernel_to_common.c
index 4612eef3..2422eed0 100644
--- a/libsepol/src/kernel_to_common.c
+++ b/libsepol/src/kernel_to_common.c
@@ -575,7 +575,7 @@ static int sort_ocontext_data(struct ocontext **ocons, int (*cmp)(const void *,
 		return 0;
 	}
 
-	data = calloc(sizeof(*data), num);
+	data = calloc(num, sizeof(*data));
 	if (!data) {
 		ERR(NULL, "Out of memory");
 		return -1;
diff --git a/libsepol/src/kernel_to_conf.c b/libsepol/src/kernel_to_conf.c
index 83f46e0f..e6b449b4 100644
--- a/libsepol/src/kernel_to_conf.c
+++ b/libsepol/src/kernel_to_conf.c
@@ -811,7 +811,7 @@ static int write_sensitivity_rules_to_conf(FILE *out, struct policydb *pdb)
 	num = strs_num_items(strs);
 
 	if (num > 0) {
-		sens_alias_map = calloc(sizeof(*sens_alias_map), pdb->p_levels.nprim);
+		sens_alias_map = calloc(pdb->p_levels.nprim, sizeof(*sens_alias_map));
 		if (!sens_alias_map) {
 			rc = -1;
 			goto exit;
@@ -942,7 +942,7 @@ static int write_category_rules_to_conf(FILE *out, struct policydb *pdb)
 	num = strs_num_items(strs);
 
 	if (num > 0) {
-		cat_alias_map = calloc(sizeof(*cat_alias_map), pdb->p_cats.nprim);
+		cat_alias_map = calloc(pdb->p_cats.nprim, sizeof(*cat_alias_map));
 		if (!cat_alias_map) {
 			rc = -1;
 			goto exit;
@@ -2106,7 +2106,7 @@ static int write_cond_nodes_to_conf(FILE *out, struct policydb *pdb)
 		return 0;
 	}
 
-	cond_data = calloc(sizeof(struct cond_data), num);
+	cond_data = calloc(num, sizeof(struct cond_data));
 	if (!cond_data) {
 		rc = -1;
 		goto exit;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] libselinux: reorder calloc(3) arguments
  2024-01-05 18:35 [PATCH 1/4] libsepol: reorder calloc(3) arguments Christian Göttsche
@ 2024-01-05 18:35 ` Christian Göttsche
  2024-01-05 18:35 ` [PATCH 3/4] sandbox: do not override warning CFLAGS Christian Göttsche
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Christian Göttsche @ 2024-01-05 18:35 UTC (permalink / raw)
  To: selinux

The canonical order of calloc(3) parameters is the number of elements
first and the size of each element second.

Reported by GCC 14:

    is_customizable_type.c:43:45: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/is_customizable_type.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c
index da301c60..0ae92c8d 100644
--- a/libselinux/src/is_customizable_type.c
+++ b/libselinux/src/is_customizable_type.c
@@ -39,9 +39,7 @@ static void customizable_init(void)
 	}
 
 	if (ctr) {
-		list =
-		    (char **) calloc(sizeof(char *),
-						  ctr + 1);
+		list = calloc(ctr + 1, sizeof(char *));
 		if (list) {
 			i = 0;
 			while (fgets_unlocked(buf, selinux_page_size, fp)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] sandbox: do not override warning CFLAGS
  2024-01-05 18:35 [PATCH 1/4] libsepol: reorder calloc(3) arguments Christian Göttsche
  2024-01-05 18:35 ` [PATCH 2/4] libselinux: " Christian Göttsche
@ 2024-01-05 18:35 ` Christian Göttsche
  2024-01-05 18:35 ` [PATCH 4/4] mcstrans: check memory allocations Christian Göttsche
  2024-01-05 19:56 ` [PATCH 1/4] libsepol: reorder calloc(3) arguments James Carter
  3 siblings, 0 replies; 6+ messages in thread
From: Christian Göttsche @ 2024-01-05 18:35 UTC (permalink / raw)
  To: selinux

Do not unconditionally add warning flags and especially -Werror to
CFLAGS, only when CFLAGS is empty.
This helps when building with noisy warning flags, like -Weverything.

Also drop -W, since it is an alias for -Wextra.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 sandbox/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sandbox/Makefile b/sandbox/Makefile
index 360a8bc5..0799ab7c 100644
--- a/sandbox/Makefile
+++ b/sandbox/Makefile
@@ -8,7 +8,8 @@ BINDIR ?= $(PREFIX)/bin
 SBINDIR ?= $(PREFIX)/sbin
 MANDIR ?= $(PREFIX)/share/man
 SHAREDIR ?= $(PREFIX)/share/sandbox
-override CFLAGS += -DPACKAGE="\"policycoreutils\"" -Wall -Werror -Wextra -W
+CFLAGS ?= -Werror -Wall -Wextra
+override CFLAGS += -DPACKAGE="\"policycoreutils\""
 override LDLIBS += -lselinux -lcap-ng
 SEUNSHARE_OBJS = seunshare.o
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] mcstrans: check memory allocations
  2024-01-05 18:35 [PATCH 1/4] libsepol: reorder calloc(3) arguments Christian Göttsche
  2024-01-05 18:35 ` [PATCH 2/4] libselinux: " Christian Göttsche
  2024-01-05 18:35 ` [PATCH 3/4] sandbox: do not override warning CFLAGS Christian Göttsche
@ 2024-01-05 18:35 ` Christian Göttsche
  2024-01-05 19:56 ` [PATCH 1/4] libsepol: reorder calloc(3) arguments James Carter
  3 siblings, 0 replies; 6+ messages in thread
From: Christian Göttsche @ 2024-01-05 18:35 UTC (permalink / raw)
  To: selinux

Avoid NULL dereferences on allocation failure.

Reported by GCC analyzer.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 mcstrans/src/mls_level.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mcstrans/src/mls_level.c b/mcstrans/src/mls_level.c
index 2017f117..2ee1a231 100644
--- a/mcstrans/src/mls_level.c
+++ b/mcstrans/src/mls_level.c
@@ -13,6 +13,8 @@ mls_level_t *mls_level_from_string(char *mls_context)
 	}
 
 	l = (mls_level_t *) calloc(1, sizeof(mls_level_t));
+	if (!l)
+		return NULL;
 
 	/* Extract low sensitivity. */
 	scontextp = p = mls_context;
@@ -124,6 +126,9 @@ char *mls_level_to_string(mls_level_t *l)
 	if (len == 0)
 		return NULL;
 	char *result = (char *)malloc(len + 1);
+	if (!result)
+		return NULL;
+
 	char *p = result;
 
 	p += sprintf(p, "s%d", l->sens);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/4] libsepol: reorder calloc(3) arguments
  2024-01-05 18:35 [PATCH 1/4] libsepol: reorder calloc(3) arguments Christian Göttsche
                   ` (2 preceding siblings ...)
  2024-01-05 18:35 ` [PATCH 4/4] mcstrans: check memory allocations Christian Göttsche
@ 2024-01-05 19:56 ` James Carter
  2024-01-25 19:56   ` James Carter
  3 siblings, 1 reply; 6+ messages in thread
From: James Carter @ 2024-01-05 19:56 UTC (permalink / raw)
  To: Christian Göttsche; +Cc: selinux

On Fri, Jan 5, 2024 at 1:36 PM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> The canonical order of calloc(3) parameters is the number of elements
> first and the size of each element second.
>
> Reported by GCC 14:
>
>     kernel_to_conf.c:814:47: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
>     kernel_to_conf.c:945:46: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
>     kernel_to_conf.c:2109:35: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
>     kernel_to_common.c:578:29: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

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

> ---
>  libsepol/src/kernel_to_common.c | 2 +-
>  libsepol/src/kernel_to_conf.c   | 6 +++---
>  2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/libsepol/src/kernel_to_common.c b/libsepol/src/kernel_to_common.c
> index 4612eef3..2422eed0 100644
> --- a/libsepol/src/kernel_to_common.c
> +++ b/libsepol/src/kernel_to_common.c
> @@ -575,7 +575,7 @@ static int sort_ocontext_data(struct ocontext **ocons, int (*cmp)(const void *,
>                 return 0;
>         }
>
> -       data = calloc(sizeof(*data), num);
> +       data = calloc(num, sizeof(*data));
>         if (!data) {
>                 ERR(NULL, "Out of memory");
>                 return -1;
> diff --git a/libsepol/src/kernel_to_conf.c b/libsepol/src/kernel_to_conf.c
> index 83f46e0f..e6b449b4 100644
> --- a/libsepol/src/kernel_to_conf.c
> +++ b/libsepol/src/kernel_to_conf.c
> @@ -811,7 +811,7 @@ static int write_sensitivity_rules_to_conf(FILE *out, struct policydb *pdb)
>         num = strs_num_items(strs);
>
>         if (num > 0) {
> -               sens_alias_map = calloc(sizeof(*sens_alias_map), pdb->p_levels.nprim);
> +               sens_alias_map = calloc(pdb->p_levels.nprim, sizeof(*sens_alias_map));
>                 if (!sens_alias_map) {
>                         rc = -1;
>                         goto exit;
> @@ -942,7 +942,7 @@ static int write_category_rules_to_conf(FILE *out, struct policydb *pdb)
>         num = strs_num_items(strs);
>
>         if (num > 0) {
> -               cat_alias_map = calloc(sizeof(*cat_alias_map), pdb->p_cats.nprim);
> +               cat_alias_map = calloc(pdb->p_cats.nprim, sizeof(*cat_alias_map));
>                 if (!cat_alias_map) {
>                         rc = -1;
>                         goto exit;
> @@ -2106,7 +2106,7 @@ static int write_cond_nodes_to_conf(FILE *out, struct policydb *pdb)
>                 return 0;
>         }
>
> -       cond_data = calloc(sizeof(struct cond_data), num);
> +       cond_data = calloc(num, sizeof(struct cond_data));
>         if (!cond_data) {
>                 rc = -1;
>                 goto exit;
> --
> 2.43.0
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/4] libsepol: reorder calloc(3) arguments
  2024-01-05 19:56 ` [PATCH 1/4] libsepol: reorder calloc(3) arguments James Carter
@ 2024-01-25 19:56   ` James Carter
  0 siblings, 0 replies; 6+ messages in thread
From: James Carter @ 2024-01-25 19:56 UTC (permalink / raw)
  To: Christian Göttsche; +Cc: selinux

On Fri, Jan 5, 2024 at 2:56 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Fri, Jan 5, 2024 at 1:36 PM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > The canonical order of calloc(3) parameters is the number of elements
> > first and the size of each element second.
> >
> > Reported by GCC 14:
> >
> >     kernel_to_conf.c:814:47: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
> >     kernel_to_conf.c:945:46: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
> >     kernel_to_conf.c:2109:35: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
> >     kernel_to_common.c:578:29: warning: 'calloc' sizes specified with 'sizeof' in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> For these four patches:
> Acked-by: James Carter <jwcart2@gmail.com>
>

These four patches have been merged.
Thanks,
Jim

> > ---
> >  libsepol/src/kernel_to_common.c | 2 +-
> >  libsepol/src/kernel_to_conf.c   | 6 +++---
> >  2 files changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/libsepol/src/kernel_to_common.c b/libsepol/src/kernel_to_common.c
> > index 4612eef3..2422eed0 100644
> > --- a/libsepol/src/kernel_to_common.c
> > +++ b/libsepol/src/kernel_to_common.c
> > @@ -575,7 +575,7 @@ static int sort_ocontext_data(struct ocontext **ocons, int (*cmp)(const void *,
> >                 return 0;
> >         }
> >
> > -       data = calloc(sizeof(*data), num);
> > +       data = calloc(num, sizeof(*data));
> >         if (!data) {
> >                 ERR(NULL, "Out of memory");
> >                 return -1;
> > diff --git a/libsepol/src/kernel_to_conf.c b/libsepol/src/kernel_to_conf.c
> > index 83f46e0f..e6b449b4 100644
> > --- a/libsepol/src/kernel_to_conf.c
> > +++ b/libsepol/src/kernel_to_conf.c
> > @@ -811,7 +811,7 @@ static int write_sensitivity_rules_to_conf(FILE *out, struct policydb *pdb)
> >         num = strs_num_items(strs);
> >
> >         if (num > 0) {
> > -               sens_alias_map = calloc(sizeof(*sens_alias_map), pdb->p_levels.nprim);
> > +               sens_alias_map = calloc(pdb->p_levels.nprim, sizeof(*sens_alias_map));
> >                 if (!sens_alias_map) {
> >                         rc = -1;
> >                         goto exit;
> > @@ -942,7 +942,7 @@ static int write_category_rules_to_conf(FILE *out, struct policydb *pdb)
> >         num = strs_num_items(strs);
> >
> >         if (num > 0) {
> > -               cat_alias_map = calloc(sizeof(*cat_alias_map), pdb->p_cats.nprim);
> > +               cat_alias_map = calloc(pdb->p_cats.nprim, sizeof(*cat_alias_map));
> >                 if (!cat_alias_map) {
> >                         rc = -1;
> >                         goto exit;
> > @@ -2106,7 +2106,7 @@ static int write_cond_nodes_to_conf(FILE *out, struct policydb *pdb)
> >                 return 0;
> >         }
> >
> > -       cond_data = calloc(sizeof(struct cond_data), num);
> > +       cond_data = calloc(num, sizeof(struct cond_data));
> >         if (!cond_data) {
> >                 rc = -1;
> >                 goto exit;
> > --
> > 2.43.0
> >
> >

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-25 19:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-05 18:35 [PATCH 1/4] libsepol: reorder calloc(3) arguments Christian Göttsche
2024-01-05 18:35 ` [PATCH 2/4] libselinux: " Christian Göttsche
2024-01-05 18:35 ` [PATCH 3/4] sandbox: do not override warning CFLAGS Christian Göttsche
2024-01-05 18:35 ` [PATCH 4/4] mcstrans: check memory allocations Christian Göttsche
2024-01-05 19:56 ` [PATCH 1/4] libsepol: reorder calloc(3) arguments James Carter
2024-01-25 19:56   ` James Carter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.