Skip to contentSkip to conversation

libsepol: reorder calloc(3) arguments

PATCH In review Christian Göttsche 2 participants 6 messages Jan 5, 2024 – Jan 25, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libsepol: reorder calloc(3) arguments

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]

---
 libsepol/src/kernel_to_common.c | 2 +-
 libsepol/src/kernel_to_conf.c   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
libsepol/src/kernel_to_common.c +1 -1
index 4612eef3..2422eed0 100644
@@ -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;
libsepol/src/kernel_to_conf.c +3 -3
index 83f46e0f..e6b449b4 100644
@@ -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;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/4 libselinux: reorder calloc(3) arguments

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]

---
 libselinux/src/is_customizable_type.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
libselinux/src/is_customizable_type.c +1 -3
index da301c60..0ae92c8d 100644
@@ -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)
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/4 sandbox: do not override warning CFLAGS

In reply to Christian Göttsche

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.

---
 sandbox/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sandbox/Makefile +2 -1
index 360a8bc5..0799ab7c 100644
@@ -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
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/4 mcstrans: check memory allocations

In reply to Christian Göttsche

Avoid NULL dereferences on allocation failure.

Reported by GCC analyzer.

---
 mcstrans/src/mls_level.c | 5 +++++
 1 file changed, 5 insertions(+)
mcstrans/src/mls_level.c +5 -0
index 2017f117..2ee1a231 100644
@@ -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);
Signature · 1 line
2.43.0
Acked-by on v1 1/4
#4 lore raw
In reply to Christian Göttsche
Quoted 4 lines · Christian Göttsche

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

Quoted 8 lines

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]

For these four patches:

Quoted 53 lines
---
 libsepol/src/kernel_to_common.c | 2 +-
 libsepol/src/kernel_to_conf.c   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
libsepol/src/kernel_to_common.c +1 -1
index 4612eef3..2422eed0 100644
@@ -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;
libsepol/src/kernel_to_conf.c +3 -4
index 83f46e0f..e6b449b4 100644
@@ -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

#5 lore raw
Quoted 14 lines · James Carter
Quoted 11 lines · Christian Göttsche

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]

Quoted 5 lines
Quoted 1 line

For these four patches:

These four patches have been merged.
Thanks,
Jim

Quoted 53 lines
Quoted 53 lines
---
 libsepol/src/kernel_to_common.c | 2 +-
 libsepol/src/kernel_to_conf.c   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
libsepol/src/kernel_to_common.c +1 -1
index 4612eef3..2422eed0 100644
@@ -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;
libsepol/src/kernel_to_conf.c +3 -4
index 83f46e0f..e6b449b4 100644
@@ -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