Skip to contentSkip to conversation

libsepol: Use calloc when initializing bool_val_to_struct array

PATCH In review James Carter 4 participants 9 messages Mar 14, 2022 – Mar 30, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: Use calloc when initializing bool_val_to_struct array

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
 	if (p->bool_val_to_struct)
 		free(p->bool_val_to_struct);
 	p->bool_val_to_struct = (cond_bool_datum_t **)
-	    mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+	    calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
 	if (!p->bool_val_to_struct)
 		return -1;
 	return 0;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 26 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -2
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--
Quoted 2 lines

2.34.1

Can this be merged? I think it might hurt the fuzzer, e.g. cause the flakiness in issue #45327.

On a technical note:
In src/policydb.c::policydb_index_others() the return value of cond_init_bool_indexes() is not checked.

libsepol/src/policydb.c +2 -1
index fc71463e..e29cbd51 100644
@@ -1252,7 +1252,8 @@ int policydb_index_others(sepol_handle_t * handle,
       if (!p->type_val_to_struct)
               return -1;
 
-       cond_init_bool_indexes(p);
+       if (cond_init_bool_indexes(p) == -1)
+               return -1;
 
       for (i = SYM_ROLES; i < SYM_NUM; i++) {
               free(p->sym_val_to_name[i]);
#2 lore raw
Quoted 25 lines · Christian Göttsche
Quoted 23 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
        if (!p->bool_val_to_struct)
Quoted 9 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
                return -1;
        return 0;
--

2.34.1

Can this be merged? I think it might hurt the fuzzer, e.g. cause the flakiness in issue #45327.

This has been merged.
Jim

Quoted 18 lines

On a technical note:
In src/policydb.c::policydb_index_others() the return value of cond_init_bool_indexes() is not checked.

libsepol/src/policydb.c +2 -1
index fc71463e..e29cbd51 100644
@@ -1252,7 +1252,8 @@ int policydb_index_others(sepol_handle_t * handle,
       if (!p->type_val_to_struct)
               return -1;
 
-       cond_init_bool_indexes(p);
+       if (cond_init_bool_indexes(p) == -1)
+               return -1;
 
       for (i = SYM_ROLES; i < SYM_NUM; i++) {
               free(p->sym_val_to_name[i]);
#3 lore raw
In reply to James Carter
Quoted 26 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -2
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--
Quoted 1 line

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

#4 lore raw
Quoted 24 lines · Joshua Brindle
Quoted 22 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
Quoted 8 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

Many of the calls to mallocarray() should be replaced by calloc() because the array is initialized to zero right after the mallocarray() call. I guess all of the calls can be replaced if you don't mind having the memory set to zero and then immediately setting the array to different values.

I will merge this patch and send another patch replacing mallocarray() where appropriate.

Jim

Jim

#5 lore raw
Quoted 36 lines · James Carter
Quoted 24 lines · Joshua Brindle
Quoted 22 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
Quoted 8 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

Quoted 9 lines

Many of the calls to mallocarray() should be replaced by calloc() because the array is initialized to zero right after the mallocarray() call. I guess all of the calls can be replaced if you don't mind having the memory set to zero and then immediately setting the array to different values.

I will merge this patch and send another patch replacing mallocarray() where appropriate.

Hrm, I meant something like:

libsepol/src/private.h +1 -1
index a8cc1472..9a51fb34 100644
@@ -90,7 +90,7 @@ static inline void* mallocarray(size_t nmemb, size_t size) {
                return NULL;
        }
 
-       return malloc(nmemb * size);
+       return calloc(nmemb, size);
 }
 
 #ifndef HAVE_REALLOCARRAY

Since mallocarray has additional length checking logic that you lose if you just switch from mallocarray to calloc.

#6 lore raw
Quoted 65 lines · Joshua Brindle
Quoted 36 lines · James Carter
Quoted 24 lines · Joshua Brindle
Quoted 22 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
Quoted 8 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

Quoted 9 lines

Many of the calls to mallocarray() should be replaced by calloc() because the array is initialized to zero right after the mallocarray() call. I guess all of the calls can be replaced if you don't mind having the memory set to zero and then immediately setting the array to different values.

I will merge this patch and send another patch replacing mallocarray() where appropriate.

Hrm, I meant something like:

libsepol/src/private.h +1 -1
index a8cc1472..9a51fb34 100644
@@ -90,7 +90,7 @@ static inline void* mallocarray(size_t nmemb, size_t size) {
                return NULL;
        }
 
-       return malloc(nmemb * size);
+       return calloc(nmemb, size);
 }
 
 #ifndef HAVE_REALLOCARRAY
Quoted 2 lines

Since mallocarray has additional length checking logic that you lose if you just switch from mallocarray to calloc.

Does it? The man page for calloc says that it will return an error if nmemb * size will overflow.

Jim

#7 lore raw
Quoted 65 lines · James Carter
Quoted 62 lines · Joshua Brindle
Quoted 36 lines · James Carter
Quoted 24 lines · Joshua Brindle
Quoted 22 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
Quoted 8 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

Quoted 9 lines

Many of the calls to mallocarray() should be replaced by calloc() because the array is initialized to zero right after the mallocarray() call. I guess all of the calls can be replaced if you don't mind having the memory set to zero and then immediately setting the array to different values.

I will merge this patch and send another patch replacing mallocarray() where appropriate.

Hrm, I meant something like:

libsepol/src/private.h +1 -1
index a8cc1472..9a51fb34 100644
@@ -90,7 +90,7 @@ static inline void* mallocarray(size_t nmemb, size_t size) {
                return NULL;
        }
 
-       return malloc(nmemb * size);
+       return calloc(nmemb, size);
 }
 
Quoted 8 lines
Quoted 5 lines
libsepol/src/private.h +0 -0
 #ifndef HAVE_REALLOCARRAY

Since mallocarray has additional length checking logic that you lose if you just switch from mallocarray to calloc.

Does it? The man page for calloc says that it will return an error if nmemb * size will overflow.

Alright, +1

#8 lore raw
In reply to Joshua Brindle
Quoted 24 lines · Joshua Brindle
Quoted 22 lines · James Carter

Use calloc() instead of mallocarray() so that everything is initialized to zero to prevent the use of unitialized memory when validating malformed binary policies.

Found by oss-fuzz (#45493)

---
 libsepol/src/conditional.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/conditional.c +1 -1
index f78b38a2..a620451d 100644
@@ -522,7 +522,7 @@ int cond_init_bool_indexes(policydb_t * p)
        if (p->bool_val_to_struct)
                free(p->bool_val_to_struct);
        p->bool_val_to_struct = (cond_bool_datum_t **)
-           mallocarray(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
+           calloc(p->p_bools.nprim, sizeof(cond_bool_datum_t *));
Quoted 8 lines
Quoted 5 lines
libsepol/src/conditional.c +0 -1
        if (!p->bool_val_to_struct)
                return -1;
        return 0;
--

2.34.1

Why not change the mallocarray macro to use calloc? I see a number of mallocarray calls that should be audited if this approach is taken.

+1, it also gets rid of initialization code like this (note the loop setting to NULL):
int sepol_sidtab_init(sidtab_t * s)

{
        int i;

        s->htable = mallocarray(SIDTAB_SIZE, sizeof(sidtab_ptr_t));
        if (!s->htable)
                return -ENOMEM;
        for (i = 0; i < SIDTAB_SIZE; i++)
                s->htable[i] = (sidtab_ptr_t) NULL;
        s->nel = 0;
        s->next_sid = 1;
        s->shutdown = 0;
        INIT_SIDTAB_LOCK(s);
        return 0;
}