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

@ -78,7 +78,7 @@ linux_prepare_atag (void *target_atag)
/* some place for cmdline, initrd and terminator. */
tmp_size = get_atag_size (atag_orig) + 20 + (arg_size) / 4;
tmp_atag = grub_malloc (tmp_size * sizeof (grub_uint32_t));
tmp_atag = grub_calloc (tmp_size, sizeof (grub_uint32_t));
if (!tmp_atag)
return grub_errno;

View file

@ -116,7 +116,7 @@ copy_file_path (grub_efi_file_path_device_path_t *fp,
fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
path_name = grub_malloc (len * GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
path_name = grub_calloc (len, GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
if (!path_name)
return;

View file

@ -48,7 +48,7 @@ read_headers (grub_file_t file, const char *filename, Elf_Ehdr *e, char **shdr)
if (e->e_ident[EI_CLASS] != SUFFIX (ELFCLASS))
return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
*shdr = grub_malloc ((grub_uint32_t) e->e_shnum * e->e_shentsize);
*shdr = grub_calloc (e->e_shnum, e->e_shentsize);
if (! *shdr)
return grub_errno;

View file

@ -295,7 +295,7 @@ grub_xnu_devprop_add_property_utf8 (struct grub_xnu_devprop_device_descriptor *d
return grub_errno;
len = grub_strlen (name);
utf16 = grub_malloc (sizeof (grub_uint16_t) * len);
utf16 = grub_calloc (len, sizeof (grub_uint16_t));
if (!utf16)
{
grub_free (utf8);
@ -331,7 +331,7 @@ grub_xnu_devprop_add_property_utf16 (struct grub_xnu_devprop_device_descriptor *
grub_uint16_t *utf16;
grub_err_t err;
utf16 = grub_malloc (sizeof (grub_uint16_t) * namelen);
utf16 = grub_calloc (namelen, sizeof (grub_uint16_t));
if (!utf16)
return grub_errno;
grub_memcpy (utf16, name, sizeof (grub_uint16_t) * namelen);

View file

@ -97,7 +97,7 @@ grub_macho_file (grub_file_t file, const char *filename, int is_64bit)
if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
== (grub_off_t) -1)
goto fail;
archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
archs = grub_calloc (narchs, sizeof (struct grub_macho_fat_arch));
if (!archs)
goto fail;
if (grub_file_read (macho->file, archs,

View file

@ -217,7 +217,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
{
grub_uint8_t *shdr, *shdrptr;
shdr = grub_malloc ((grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize);
shdr = grub_calloc (ehdr->e_shnum, ehdr->e_shentsize);
if (!shdr)
return grub_errno;

View file

@ -800,7 +800,7 @@ grub_cmd_xnu_mkext (grub_command_t cmd __attribute__ ((unused)),
if (grub_be_to_cpu32 (head.magic) == GRUB_MACHO_FAT_MAGIC)
{
narchs = grub_be_to_cpu32 (head.nfat_arch);
archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
archs = grub_calloc (narchs, sizeof (struct grub_macho_fat_arch));
if (! archs)
{
grub_file_close (file);