libsepol: Replace calls to mallocarray() with calls to calloc()

Since calloc() will return an error if nmemb * size would overflow,
just use it instead of mallocarray(). This also allows code that
initializes the array to zero to be removed.

Signed-off-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
James Carter 2022-03-30 15:54:27 -04:00
parent fed78faaa3
commit 20187dbfe9
12 changed files with 20 additions and 52 deletions

View File

@ -3153,17 +3153,15 @@ int expand_module(sepol_handle_t * handle,
goto cleanup; goto cleanup;
/* Build the type<->attribute maps and remove attributes. */ /* Build the type<->attribute maps and remove attributes. */
state.out->attr_type_map = mallocarray(state.out->p_types.nprim, state.out->attr_type_map = calloc(state.out->p_types.nprim,
sizeof(ebitmap_t)); sizeof(ebitmap_t));
state.out->type_attr_map = mallocarray(state.out->p_types.nprim, state.out->type_attr_map = calloc(state.out->p_types.nprim,
sizeof(ebitmap_t)); sizeof(ebitmap_t));
if (!state.out->attr_type_map || !state.out->type_attr_map) { if (!state.out->attr_type_map || !state.out->type_attr_map) {
ERR(handle, "Out of memory!"); ERR(handle, "Out of memory!");
goto cleanup; goto cleanup;
} }
for (i = 0; i < state.out->p_types.nprim; i++) { for (i = 0; i < state.out->p_types.nprim; i++) {
ebitmap_init(&state.out->type_attr_map[i]);
ebitmap_init(&state.out->attr_type_map[i]);
/* add the type itself as the degenerate case */ /* add the type itself as the degenerate case */
if (ebitmap_set_bit(&state.out->type_attr_map[i], i, 1)) { if (ebitmap_set_bit(&state.out->type_attr_map[i], i, 1)) {
ERR(handle, "Out of memory!"); ERR(handle, "Out of memory!");

View File

@ -43,7 +43,6 @@ hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
{ {
hashtab_t p; hashtab_t p;
unsigned int i;
p = (hashtab_t) malloc(sizeof(hashtab_val_t)); p = (hashtab_t) malloc(sizeof(hashtab_val_t));
if (p == NULL) if (p == NULL)
@ -54,13 +53,11 @@ hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
p->nel = 0; p->nel = 0;
p->hash_value = hash_value; p->hash_value = hash_value;
p->keycmp = keycmp; p->keycmp = keycmp;
p->htable = (hashtab_ptr_t *) mallocarray(size, sizeof(hashtab_ptr_t)); p->htable = (hashtab_ptr_t *) calloc(size, sizeof(hashtab_ptr_t));
if (p->htable == NULL) { if (p->htable == NULL) {
free(p); free(p);
return NULL; return NULL;
} }
for (i = 0; i < size; i++)
p->htable[i] = (hashtab_ptr_t) NULL;
return p; return p;
} }

View File

@ -1681,14 +1681,10 @@ static int copy_scope_index(scope_index_t * src, scope_index_t * dest,
} }
/* next copy the enabled permissions data */ /* next copy the enabled permissions data */
if ((dest->class_perms_map = mallocarray(largest_mapped_class_value, if ((dest->class_perms_map = calloc(largest_mapped_class_value,
sizeof(*dest->class_perms_map))) == sizeof(*dest->class_perms_map))) == NULL) {
NULL) {
goto cleanup; goto cleanup;
} }
for (i = 0; i < largest_mapped_class_value; i++) {
ebitmap_init(dest->class_perms_map + i);
}
dest->class_perms_len = largest_mapped_class_value; dest->class_perms_len = largest_mapped_class_value;
for (i = 0; i < src->class_perms_len; i++) { for (i = 0; i < src->class_perms_len; i++) {
ebitmap_t *srcmap = src->class_perms_map + i; ebitmap_t *srcmap = src->class_perms_map + i;

View File

@ -409,14 +409,14 @@ static int module_package_read_offsets(sepol_module_package_t * mod,
goto err; goto err;
} }
off = (size_t *) mallocarray(nsec + 1, sizeof(size_t)); off = (size_t *) calloc(nsec + 1, sizeof(size_t));
if (!off) { if (!off) {
ERR(file->handle, "out of memory"); ERR(file->handle, "out of memory");
goto err; goto err;
} }
free(buf); free(buf);
buf = mallocarray(nsec, sizeof(uint32_t)); buf = calloc(nsec, sizeof(uint32_t));
if (!buf) { if (!buf) {
ERR(file->handle, "out of memory"); ERR(file->handle, "out of memory");
goto err; goto err;

View File

@ -432,7 +432,7 @@ static int stack_init(struct stack **stack)
goto exit; goto exit;
} }
s->stack = mallocarray(STACK_SIZE, sizeof(*s->stack)); s->stack = calloc(STACK_SIZE, sizeof(*s->stack));
if (s->stack == NULL) { if (s->stack == NULL) {
goto exit; goto exit;
} }
@ -1010,7 +1010,7 @@ static int ebitmap_to_names(struct ebitmap *map, char **vals_to_names, char ***n
goto exit; goto exit;
} }
name_arr = mallocarray(num, sizeof(*name_arr)); name_arr = calloc(num, sizeof(*name_arr));
if (name_arr == NULL) { if (name_arr == NULL) {
log_err("Out of memory"); log_err("Out of memory");
rc = -1; rc = -1;

View File

@ -45,7 +45,7 @@ static int type_vec_init(struct type_vec *v)
{ {
v->capacity = TYPE_VEC_INIT_SIZE; v->capacity = TYPE_VEC_INIT_SIZE;
v->count = 0; v->count = 0;
v->types = mallocarray(v->capacity, sizeof(*v->types)); v->types = calloc(v->capacity, sizeof(*v->types));
if (!v->types) if (!v->types)
return -1; return -1;
return 0; return 0;
@ -97,7 +97,7 @@ static struct type_vec *build_type_map(const policydb_t *p)
{ {
unsigned int i, k; unsigned int i, k;
ebitmap_node_t *n; ebitmap_node_t *n;
struct type_vec *map = mallocarray(p->p_types.nprim, sizeof(*map)); struct type_vec *map = calloc(p->p_types.nprim, sizeof(*map));
if (!map) if (!map)
return NULL; return NULL;

View File

@ -4127,7 +4127,7 @@ static int scope_read(policydb_t * p, int symnum, struct policy_file *fp)
goto cleanup; goto cleanup;
} }
if ((scope->decl_ids = if ((scope->decl_ids =
mallocarray(scope->decl_ids_len, sizeof(uint32_t))) == NULL) { calloc(scope->decl_ids_len, sizeof(uint32_t))) == NULL) {
goto cleanup; goto cleanup;
} }
rc = next_entry(scope->decl_ids, fp, sizeof(uint32_t) * scope->decl_ids_len); rc = next_entry(scope->decl_ids, fp, sizeof(uint32_t) * scope->decl_ids_len);
@ -4518,14 +4518,10 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
} }
if (policy_type == POLICY_KERN) { if (policy_type == POLICY_KERN) {
p->type_attr_map = mallocarray(p->p_types.nprim, sizeof(ebitmap_t)); p->type_attr_map = calloc(p->p_types.nprim, sizeof(ebitmap_t));
p->attr_type_map = mallocarray(p->p_types.nprim, sizeof(ebitmap_t)); p->attr_type_map = calloc(p->p_types.nprim, sizeof(ebitmap_t));
if (!p->type_attr_map || !p->attr_type_map) if (!p->type_attr_map || !p->attr_type_map)
goto bad; goto bad;
for (i = 0; i < p->p_types.nprim; i++) {
ebitmap_init(&p->type_attr_map[i]);
ebitmap_init(&p->attr_type_map[i]);
}
for (i = 0; i < p->p_types.nprim; i++) { for (i = 0; i < p->p_types.nprim; i++) {
if (r_policyvers >= POLICYDB_VERSION_AVTAB) { if (r_policyvers >= POLICYDB_VERSION_AVTAB) {
if (ebitmap_read(&p->type_attr_map[i], fp)) if (ebitmap_read(&p->type_attr_map[i], fp))

View File

@ -84,15 +84,6 @@ extern size_t put_entry(const void *ptr, size_t size, size_t n,
struct policy_file *fp); struct policy_file *fp);
extern int str_read(char **strp, struct policy_file *fp, size_t len); extern int str_read(char **strp, struct policy_file *fp, size_t len);
static inline void* mallocarray(size_t nmemb, size_t size) {
if (size && nmemb > (size_t)-1 / size) {
errno = ENOMEM;
return NULL;
}
return malloc(nmemb * size);
}
#ifndef HAVE_REALLOCARRAY #ifndef HAVE_REALLOCARRAY
static inline void* reallocarray(void *ptr, size_t nmemb, size_t size) { static inline void* reallocarray(void *ptr, size_t nmemb, size_t size) {
if (size && nmemb > (size_t)-1 / size) { if (size && nmemb > (size_t)-1 / size) {

View File

@ -712,7 +712,7 @@ mls_ops:
* Generate the same number of answer buffer entries as expression * Generate the same number of answer buffer entries as expression
* buffers (as there will never be more). * buffers (as there will never be more).
*/ */
answer_list = mallocarray(expr_count, sizeof(*answer_list)); answer_list = calloc(expr_count, sizeof(*answer_list));
if (!answer_list) { if (!answer_list) {
ERR(NULL, "failed to allocate answer stack"); ERR(NULL, "failed to allocate answer stack");
rc = -ENOMEM; rc = -ENOMEM;
@ -2169,12 +2169,11 @@ int sepol_get_user_sids(sepol_security_id_t fromsid,
} }
usercon.user = user->s.value; usercon.user = user->s.value;
mysids = mallocarray(maxnel, sizeof(sepol_security_id_t)); mysids = calloc(maxnel, sizeof(sepol_security_id_t));
if (!mysids) { if (!mysids) {
rc = -ENOMEM; rc = -ENOMEM;
goto out; goto out;
} }
memset(mysids, 0, maxnel * sizeof(sepol_security_id_t));
ebitmap_for_each_positive_bit(&user->roles.roles, rnode, i) { ebitmap_for_each_positive_bit(&user->roles.roles, rnode, i) {
role = policydb->role_val_to_struct[i]; role = policydb->role_val_to_struct[i];
@ -2204,17 +2203,12 @@ int sepol_get_user_sids(sepol_security_id_t fromsid,
mysids[mynel++] = sid; mysids[mynel++] = sid;
} else { } else {
maxnel += SIDS_NEL; maxnel += SIDS_NEL;
mysids2 = mysids2 = calloc(maxnel, sizeof(sepol_security_id_t));
mallocarray(maxnel,
sizeof(sepol_security_id_t));
if (!mysids2) { if (!mysids2) {
rc = -ENOMEM; rc = -ENOMEM;
free(mysids); free(mysids);
goto out; goto out;
} }
memset(mysids2, 0,
maxnel * sizeof(sepol_security_id_t));
memcpy(mysids2, mysids, memcpy(mysids2, mysids,
mynel * sizeof(sepol_security_id_t)); mynel * sizeof(sepol_security_id_t));
free(mysids); free(mysids);

View File

@ -26,13 +26,9 @@
int sepol_sidtab_init(sidtab_t * s) int sepol_sidtab_init(sidtab_t * s)
{ {
int i; s->htable = calloc(SIDTAB_SIZE, sizeof(sidtab_ptr_t));
s->htable = mallocarray(SIDTAB_SIZE, sizeof(sidtab_ptr_t));
if (!s->htable) if (!s->htable)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < SIDTAB_SIZE; i++)
s->htable[i] = (sidtab_ptr_t) NULL;
s->nel = 0; s->nel = 0;
s->next_sid = 1; s->next_sid = 1;
s->shutdown = 0; s->shutdown = 0;

View File

@ -267,7 +267,7 @@ int sepol_user_get_roles(sepol_handle_t * handle,
unsigned int i; unsigned int i;
const char **tmp_roles = const char **tmp_roles =
(const char **)mallocarray(user->num_roles, sizeof(char *)); (const char **)calloc(user->num_roles, sizeof(char *));
if (!tmp_roles) if (!tmp_roles)
goto omem; goto omem;

View File

@ -2117,7 +2117,7 @@ static int scope_write(hashtab_key_t key, hashtab_datum_t datum, void *ptr)
* buffer. this would have been easier with C99's * buffer. this would have been easier with C99's
* dynamic arrays... */ * dynamic arrays... */
rc = POLICYDB_ERROR; rc = POLICYDB_ERROR;
dyn_buf = mallocarray(items, sizeof(*dyn_buf)); dyn_buf = calloc(items, sizeof(*dyn_buf));
if (!dyn_buf) if (!dyn_buf)
goto err; goto err;
buf = dyn_buf; buf = dyn_buf;