calloc: Use calloc() at most places

This modifies most of the places we do some form of:

  X = malloc(Y * Z);

to use calloc(Y, Z) instead.

Among other issues, this fixes:
  - allocation of integer overflow in grub_png_decode_image_header()
    reported by Chris Coulson,
  - allocation of integer overflow in luks_recover_key()
    reported by Chris Coulson,
  - allocation of integer overflow in grub_lvm_detect()
    reported by Chris Coulson.

Fixes: CVE-2020-14308

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Peter Jones 2020-06-15 12:26:01 -04:00 committed by Daniel Kiper
parent 64e26162eb
commit f725fa7cb2
87 changed files with 179 additions and 178 deletions

View file

@ -1135,7 +1135,7 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
array->lvs->segments->node_count = nmemb;
array->lvs->segments->raid_member_size = disk_size;
array->lvs->segments->nodes
= grub_zalloc (nmemb * sizeof (array->lvs->segments->nodes[0]));
= grub_calloc (nmemb, sizeof (array->lvs->segments->nodes[0]));
array->lvs->segments->stripe_size = stripe_size;
for (i = 0; i < nmemb; i++)
{
@ -1227,7 +1227,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
grub_partition_t p;
for (p = disk->partition; p; p = p->parent)
s++;
pv->partmaps = xmalloc (s * sizeof (pv->partmaps[0]));
pv->partmaps = xcalloc (s, sizeof (pv->partmaps[0]));
s = 0;
for (p = disk->partition; p; p = p->parent)
pv->partmaps[s++] = xstrdup (p->partmap->name);

View file

@ -297,7 +297,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
/* Power machines documentation specify 672 as maximum SAS disks in
one system. Using a slightly larger value to be safe. */
table_size = 768;
table = grub_malloc (table_size * sizeof (grub_uint64_t));
table = grub_calloc (table_size, sizeof (grub_uint64_t));
if (!table)
{

View file

@ -323,8 +323,8 @@ make_vg (grub_disk_t disk,
lv->segments->type = GRUB_DISKFILTER_MIRROR;
lv->segments->node_count = 0;
lv->segments->node_alloc = 8;
lv->segments->nodes = grub_zalloc (sizeof (*lv->segments->nodes)
* lv->segments->node_alloc);
lv->segments->nodes = grub_calloc (lv->segments->node_alloc,
sizeof (*lv->segments->nodes));
if (!lv->segments->nodes)
goto fail2;
ptr = vblk[i].dynamic;
@ -543,8 +543,8 @@ make_vg (grub_disk_t disk,
{
comp->segment_alloc = 8;
comp->segment_count = 0;
comp->segments = grub_malloc (sizeof (*comp->segments)
* comp->segment_alloc);
comp->segments = grub_calloc (comp->segment_alloc,
sizeof (*comp->segments));
if (!comp->segments)
goto fail2;
}
@ -590,8 +590,8 @@ make_vg (grub_disk_t disk,
}
comp->segments->node_count = read_int (ptr + 1, *ptr);
comp->segments->node_alloc = comp->segments->node_count;
comp->segments->nodes = grub_zalloc (sizeof (*comp->segments->nodes)
* comp->segments->node_alloc);
comp->segments->nodes = grub_calloc (comp->segments->node_alloc,
sizeof (*comp->segments->nodes));
if (!lv->segments->nodes)
goto fail2;
}
@ -1017,7 +1017,7 @@ grub_util_ldm_embed (struct grub_disk *disk, unsigned int *nsectors,
*nsectors = lv->size;
if (*nsectors > max_nsectors)
*nsectors = max_nsectors;
*sectors = grub_malloc (*nsectors * sizeof (**sectors));
*sectors = grub_calloc (*nsectors, sizeof (**sectors));
if (!*sectors)
return grub_errno;
for (i = 0; i < *nsectors; i++)

View file

@ -178,7 +178,7 @@ luks_recover_key (grub_disk_t source,
&& grub_be_to_cpu32 (header.keyblock[i].stripes) > max_stripes)
max_stripes = grub_be_to_cpu32 (header.keyblock[i].stripes);
split_key = grub_malloc (keysize * max_stripes);
split_key = grub_calloc (keysize, max_stripes);
if (!split_key)
return grub_errno;

View file

@ -210,7 +210,7 @@ grub_lvm_detect (grub_disk_t disk,
first one. */
/* Allocate buffer space for the circular worst-case scenario. */
metadatabuf = grub_malloc (2 * mda_size);
metadatabuf = grub_calloc (2, mda_size);
if (! metadatabuf)
goto fail;
@ -465,7 +465,7 @@ grub_lvm_detect (grub_disk_t disk,
#endif
goto lvs_fail;
}
lv->segments = grub_zalloc (sizeof (*seg) * lv->segment_count);
lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
seg = lv->segments;
for (i = 0; i < lv->segment_count; i++)
@ -522,8 +522,8 @@ grub_lvm_detect (grub_disk_t disk,
if (seg->node_count != 1)
seg->stripe_size = grub_lvm_getvalue (&p, "stripe_size = ");
seg->nodes = grub_zalloc (sizeof (*stripe)
* seg->node_count);
seg->nodes = grub_calloc (seg->node_count,
sizeof (*stripe));
stripe = seg->nodes;
p = grub_strstr (p, "stripes = [");
@ -899,7 +899,7 @@ grub_lvm_detect (grub_disk_t disk,
break;
if (lv)
{
cache->lv->segments = grub_malloc (lv->segment_count * sizeof (*lv->segments));
cache->lv->segments = grub_calloc (lv->segment_count, sizeof (*lv->segments));
if (!cache->lv->segments)
{
grub_lvm_free_cache_lvs (cache_lvs);
@ -912,7 +912,7 @@ grub_lvm_detect (grub_disk_t disk,
struct grub_diskfilter_node *nodes = lv->segments[i].nodes;
grub_size_t node_count = lv->segments[i].node_count;
cache->lv->segments[i].nodes = grub_malloc (node_count * sizeof (*nodes));
cache->lv->segments[i].nodes = grub_calloc (node_count, sizeof (*nodes));
if (!cache->lv->segments[i].nodes)
{
for (j = 0; j < i; ++j)

View file

@ -426,7 +426,7 @@ grub_xendisk_init (void)
if (!ctr)
return;
virtdisks = grub_malloc (ctr * sizeof (virtdisks[0]));
virtdisks = grub_calloc (ctr, sizeof (virtdisks[0]));
if (!virtdisks)
return;
if (grub_xenstore_dir ("device/vbd", fill, &ctr))