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

@ -168,7 +168,7 @@ grub_util_raid_getmembers (const char *name, int bootable)
if (ret != 0)
grub_util_error (_("ioctl GET_ARRAY_INFO error: %s"), strerror (errno));
devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
devicelist = xcalloc (info.nr_disks + 1, sizeof (char *));
for (i = 0, j = 0; j < info.nr_disks; i++)
{
@ -241,7 +241,7 @@ grub_find_root_devices_from_btrfs (const char *dir)
return NULL;
}
ret = xmalloc ((fsi.num_devices + 1) * sizeof (ret[0]));
ret = xcalloc (fsi.num_devices + 1, sizeof (ret[0]));
for (i = 1; i <= fsi.max_id && j < fsi.num_devices; i++)
{
@ -396,7 +396,7 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
if (relroot)
*relroot = NULL;
entries = xmalloc (entry_max * sizeof (*entries));
entries = xcalloc (entry_max, sizeof (*entries));
again:
fp = grub_util_fopen ("/proc/self/mountinfo", "r");