Skip to content

Commit f725fa7

Browse files
vathpelaDaniel Kiper
authored and
Daniel Kiper
committed
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 <[email protected]> Reviewed-by: Daniel Kiper <[email protected]>
1 parent 64e2616 commit f725fa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+179
-178
lines changed

grub-core/bus/usb/usbhub.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ grub_usb_add_hub (grub_usb_device_t dev)
149149
grub_usb_set_configuration (dev, 1);
150150

151151
dev->nports = hubdesc.portcnt;
152-
dev->children = grub_zalloc (hubdesc.portcnt * sizeof (dev->children[0]));
153-
dev->ports = grub_zalloc (dev->nports * sizeof (dev->ports[0]));
152+
dev->children = grub_calloc (hubdesc.portcnt, sizeof (dev->children[0]));
153+
dev->ports = grub_calloc (dev->nports, sizeof (dev->ports[0]));
154154
if (!dev->children || !dev->ports)
155155
{
156156
grub_free (dev->children);
@@ -268,8 +268,8 @@ grub_usb_controller_dev_register_iter (grub_usb_controller_t controller, void *d
268268

269269
/* Query the number of ports the root Hub has. */
270270
hub->nports = controller->dev->hubports (controller);
271-
hub->devices = grub_zalloc (sizeof (hub->devices[0]) * hub->nports);
272-
hub->ports = grub_zalloc (sizeof (hub->ports[0]) * hub->nports);
271+
hub->devices = grub_calloc (hub->nports, sizeof (hub->devices[0]));
272+
hub->ports = grub_calloc (hub->nports, sizeof (hub->ports[0]));
273273
if (!hub->devices || !hub->ports)
274274
{
275275
grub_free (hub->devices);

grub-core/commands/efi/lsefisystab.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ grub_cmd_lsefisystab (struct grub_command *cmd __attribute__ ((unused)),
7373
grub_printf ("Vendor: ");
7474

7575
for (vendor_utf16 = st->firmware_vendor; *vendor_utf16; vendor_utf16++);
76-
vendor = grub_malloc (4 * (vendor_utf16 - st->firmware_vendor) + 1);
76+
/* Allocate extra 3 bytes to simplify math. */
77+
vendor = grub_calloc (4, vendor_utf16 - st->firmware_vendor + 1);
7778
if (!vendor)
7879
return grub_errno;
7980
*grub_utf16_to_utf8 ((grub_uint8_t *) vendor, st->firmware_vendor,

grub-core/commands/legacycfg.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)),
314314
if (argc < 2)
315315
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
316316

317-
cutargs = grub_malloc (sizeof (cutargs[0]) * (argc - 1));
317+
cutargs = grub_calloc (argc - 1, sizeof (cutargs[0]));
318318
if (!cutargs)
319319
return grub_errno;
320320
cutargc = argc - 1;
@@ -436,7 +436,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)),
436436
{
437437
char rbuf[3] = "-r";
438438
bsdargc = cutargc + 2;
439-
bsdargs = grub_malloc (sizeof (bsdargs[0]) * bsdargc);
439+
bsdargs = grub_calloc (bsdargc, sizeof (bsdargs[0]));
440440
if (!bsdargs)
441441
{
442442
err = grub_errno;
@@ -559,7 +559,7 @@ grub_cmd_legacy_initrdnounzip (struct grub_command *mycmd __attribute__ ((unused
559559
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't find command `%s'"),
560560
"module");
561561

562-
newargs = grub_malloc ((argc + 1) * sizeof (newargs[0]));
562+
newargs = grub_calloc (argc + 1, sizeof (newargs[0]));
563563
if (!newargs)
564564
return grub_errno;
565565
grub_memcpy (newargs + 1, args, argc * sizeof (newargs[0]));

grub-core/commands/menuentry.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ grub_normal_add_menu_entry (int argc, const char **args,
154154
goto fail;
155155

156156
/* Save argc, args to pass as parameters to block arg later. */
157-
menu_args = grub_malloc (sizeof (char*) * (argc + 1));
157+
menu_args = grub_calloc (argc + 1, sizeof (char *));
158158
if (! menu_args)
159159
goto fail;
160160

grub-core/commands/nativedisk.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ grub_cmd_nativedisk (grub_command_t cmd __attribute__ ((unused)),
195195
else
196196
path_prefix = prefix;
197197

198-
mods = grub_malloc (argc * sizeof (mods[0]));
198+
mods = grub_calloc (argc, sizeof (mods[0]));
199199
if (!mods)
200200
return grub_errno;
201201

grub-core/commands/parttool.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ grub_parttool_register(const char *part_name,
5959
for (nargs = 0; args[nargs].name != 0; nargs++);
6060
cur->nargs = nargs;
6161
cur->args = (struct grub_parttool_argdesc *)
62-
grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
62+
grub_calloc (nargs + 1, sizeof (struct grub_parttool_argdesc));
63+
if (!cur->args)
64+
{
65+
grub_free (cur);
66+
curhandle--;
67+
return -1;
68+
}
6369
grub_memcpy (cur->args, args,
6470
(nargs + 1) * sizeof (struct grub_parttool_argdesc));
6571

@@ -257,7 +263,7 @@ grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
257263
return err;
258264
}
259265

260-
parsed = (int *) grub_zalloc (argc * sizeof (int));
266+
parsed = (int *) grub_calloc (argc, sizeof (int));
261267

262268
for (i = 1; i < argc; i++)
263269
if (! parsed[i])
@@ -290,7 +296,7 @@ grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
290296
}
291297
ptool = cur;
292298
pargs = (struct grub_parttool_args *)
293-
grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
299+
grub_calloc (ptool->nargs, sizeof (struct grub_parttool_args));
294300
for (j = i; j < argc; j++)
295301
if (! parsed[j])
296302
{

grub-core/commands/regexp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ grub_cmd_regexp (grub_extcmd_context_t ctxt, int argc, char **args)
116116
if (ret)
117117
goto fail;
118118

119-
matches = grub_zalloc (sizeof (*matches) * (regex.re_nsub + 1));
119+
matches = grub_calloc (regex.re_nsub + 1, sizeof (*matches));
120120
if (! matches)
121121
goto fail;
122122

grub-core/commands/search_wrap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
122122
for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
123123
nhints++;
124124

125-
hints = grub_malloc (sizeof (hints[0]) * nhints);
125+
hints = grub_calloc (nhints, sizeof (hints[0]));
126126
if (!hints)
127127
return grub_errno;
128128
j = 0;

grub-core/disk/diskfilter.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
11351135
array->lvs->segments->node_count = nmemb;
11361136
array->lvs->segments->raid_member_size = disk_size;
11371137
array->lvs->segments->nodes
1138-
= grub_zalloc (nmemb * sizeof (array->lvs->segments->nodes[0]));
1138+
= grub_calloc (nmemb, sizeof (array->lvs->segments->nodes[0]));
11391139
array->lvs->segments->stripe_size = stripe_size;
11401140
for (i = 0; i < nmemb; i++)
11411141
{
@@ -1227,7 +1227,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
12271227
grub_partition_t p;
12281228
for (p = disk->partition; p; p = p->parent)
12291229
s++;
1230-
pv->partmaps = xmalloc (s * sizeof (pv->partmaps[0]));
1230+
pv->partmaps = xcalloc (s, sizeof (pv->partmaps[0]));
12311231
s = 0;
12321232
for (p = disk->partition; p; p = p->parent)
12331233
pv->partmaps[s++] = xstrdup (p->partmap->name);

grub-core/disk/ieee1275/ofdisk.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
297297
/* Power machines documentation specify 672 as maximum SAS disks in
298298
one system. Using a slightly larger value to be safe. */
299299
table_size = 768;
300-
table = grub_malloc (table_size * sizeof (grub_uint64_t));
300+
table = grub_calloc (table_size, sizeof (grub_uint64_t));
301301

302302
if (!table)
303303
{

grub-core/disk/ldm.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ make_vg (grub_disk_t disk,
323323
lv->segments->type = GRUB_DISKFILTER_MIRROR;
324324
lv->segments->node_count = 0;
325325
lv->segments->node_alloc = 8;
326-
lv->segments->nodes = grub_zalloc (sizeof (*lv->segments->nodes)
327-
* lv->segments->node_alloc);
326+
lv->segments->nodes = grub_calloc (lv->segments->node_alloc,
327+
sizeof (*lv->segments->nodes));
328328
if (!lv->segments->nodes)
329329
goto fail2;
330330
ptr = vblk[i].dynamic;
@@ -543,8 +543,8 @@ make_vg (grub_disk_t disk,
543543
{
544544
comp->segment_alloc = 8;
545545
comp->segment_count = 0;
546-
comp->segments = grub_malloc (sizeof (*comp->segments)
547-
* comp->segment_alloc);
546+
comp->segments = grub_calloc (comp->segment_alloc,
547+
sizeof (*comp->segments));
548548
if (!comp->segments)
549549
goto fail2;
550550
}
@@ -590,8 +590,8 @@ make_vg (grub_disk_t disk,
590590
}
591591
comp->segments->node_count = read_int (ptr + 1, *ptr);
592592
comp->segments->node_alloc = comp->segments->node_count;
593-
comp->segments->nodes = grub_zalloc (sizeof (*comp->segments->nodes)
594-
* comp->segments->node_alloc);
593+
comp->segments->nodes = grub_calloc (comp->segments->node_alloc,
594+
sizeof (*comp->segments->nodes));
595595
if (!lv->segments->nodes)
596596
goto fail2;
597597
}
@@ -1017,7 +1017,7 @@ grub_util_ldm_embed (struct grub_disk *disk, unsigned int *nsectors,
10171017
*nsectors = lv->size;
10181018
if (*nsectors > max_nsectors)
10191019
*nsectors = max_nsectors;
1020-
*sectors = grub_malloc (*nsectors * sizeof (**sectors));
1020+
*sectors = grub_calloc (*nsectors, sizeof (**sectors));
10211021
if (!*sectors)
10221022
return grub_errno;
10231023
for (i = 0; i < *nsectors; i++)

grub-core/disk/luks.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ luks_recover_key (grub_disk_t source,
178178
&& grub_be_to_cpu32 (header.keyblock[i].stripes) > max_stripes)
179179
max_stripes = grub_be_to_cpu32 (header.keyblock[i].stripes);
180180

181-
split_key = grub_malloc (keysize * max_stripes);
181+
split_key = grub_calloc (keysize, max_stripes);
182182
if (!split_key)
183183
return grub_errno;
184184

grub-core/disk/lvm.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ grub_lvm_detect (grub_disk_t disk,
210210
first one. */
211211

212212
/* Allocate buffer space for the circular worst-case scenario. */
213-
metadatabuf = grub_malloc (2 * mda_size);
213+
metadatabuf = grub_calloc (2, mda_size);
214214
if (! metadatabuf)
215215
goto fail;
216216

@@ -465,7 +465,7 @@ grub_lvm_detect (grub_disk_t disk,
465465
#endif
466466
goto lvs_fail;
467467
}
468-
lv->segments = grub_zalloc (sizeof (*seg) * lv->segment_count);
468+
lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
469469
seg = lv->segments;
470470

471471
for (i = 0; i < lv->segment_count; i++)
@@ -522,8 +522,8 @@ grub_lvm_detect (grub_disk_t disk,
522522
if (seg->node_count != 1)
523523
seg->stripe_size = grub_lvm_getvalue (&p, "stripe_size = ");
524524

525-
seg->nodes = grub_zalloc (sizeof (*stripe)
526-
* seg->node_count);
525+
seg->nodes = grub_calloc (seg->node_count,
526+
sizeof (*stripe));
527527
stripe = seg->nodes;
528528

529529
p = grub_strstr (p, "stripes = [");
@@ -899,7 +899,7 @@ grub_lvm_detect (grub_disk_t disk,
899899
break;
900900
if (lv)
901901
{
902-
cache->lv->segments = grub_malloc (lv->segment_count * sizeof (*lv->segments));
902+
cache->lv->segments = grub_calloc (lv->segment_count, sizeof (*lv->segments));
903903
if (!cache->lv->segments)
904904
{
905905
grub_lvm_free_cache_lvs (cache_lvs);
@@ -912,7 +912,7 @@ grub_lvm_detect (grub_disk_t disk,
912912
struct grub_diskfilter_node *nodes = lv->segments[i].nodes;
913913
grub_size_t node_count = lv->segments[i].node_count;
914914

915-
cache->lv->segments[i].nodes = grub_malloc (node_count * sizeof (*nodes));
915+
cache->lv->segments[i].nodes = grub_calloc (node_count, sizeof (*nodes));
916916
if (!cache->lv->segments[i].nodes)
917917
{
918918
for (j = 0; j < i; ++j)

grub-core/disk/xen/xendisk.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ grub_xendisk_init (void)
426426
if (!ctr)
427427
return;
428428

429-
virtdisks = grub_malloc (ctr * sizeof (virtdisks[0]));
429+
virtdisks = grub_calloc (ctr, sizeof (virtdisks[0]));
430430
if (!virtdisks)
431431
return;
432432
if (grub_xenstore_dir ("device/vbd", fill, &ctr))

grub-core/efiemu/loadcore.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ grub_efiemu_count_symbols (const Elf_Ehdr *e)
201201

202202
grub_efiemu_nelfsyms = (unsigned) s->sh_size / (unsigned) s->sh_entsize;
203203
grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
204-
grub_malloc (sizeof (struct grub_efiemu_elf_sym) * grub_efiemu_nelfsyms);
204+
grub_calloc (grub_efiemu_nelfsyms, sizeof (struct grub_efiemu_elf_sym));
205205

206206
/* Relocators */
207207
for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);

grub-core/efiemu/mm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,11 @@ grub_efiemu_mmap_sort_and_uniq (void)
554554
/* Initialize variables*/
555555
grub_memset (present, 0, sizeof (int) * GRUB_EFI_MAX_MEMORY_TYPE);
556556
scanline_events = (struct grub_efiemu_mmap_scan *)
557-
grub_malloc (sizeof (struct grub_efiemu_mmap_scan) * 2 * mmap_num);
557+
grub_calloc (mmap_num, sizeof (struct grub_efiemu_mmap_scan) * 2);
558558

559559
/* Number of chunks can't increase more than by factor of 2 */
560560
result = (grub_efi_memory_descriptor_t *)
561-
grub_malloc (sizeof (grub_efi_memory_descriptor_t) * 2 * mmap_num);
561+
grub_calloc (mmap_num, sizeof (grub_efi_memory_descriptor_t) * 2);
562562
if (!result || !scanline_events)
563563
{
564564
grub_free (result);
@@ -660,7 +660,7 @@ grub_efiemu_mm_do_alloc (void)
660660

661661
/* Preallocate mmap */
662662
efiemu_mmap = (grub_efi_memory_descriptor_t *)
663-
grub_malloc (mmap_reserved_size * sizeof (grub_efi_memory_descriptor_t));
663+
grub_calloc (mmap_reserved_size, sizeof (grub_efi_memory_descriptor_t));
664664
if (!efiemu_mmap)
665665
{
666666
grub_efiemu_unload ();

grub-core/font/font.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ load_font_index (grub_file_t file, grub_uint32_t sect_length, struct
293293
font->num_chars = sect_length / FONT_CHAR_INDEX_ENTRY_SIZE;
294294

295295
/* Allocate the character index array. */
296-
font->char_index = grub_malloc (font->num_chars
297-
* sizeof (struct char_index_entry));
296+
font->char_index = grub_calloc (font->num_chars, sizeof (struct char_index_entry));
298297
if (!font->char_index)
299298
return 1;
300299
font->bmp_idx = grub_malloc (0x10000 * sizeof (grub_uint16_t));

grub-core/fs/affs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ grub_affs_read_symlink (grub_fshelp_node_t node)
301301
return 0;
302302
}
303303
latin1[symlink_size] = 0;
304-
utf8 = grub_malloc (symlink_size * GRUB_MAX_UTF8_PER_LATIN1 + 1);
304+
utf8 = grub_calloc (GRUB_MAX_UTF8_PER_LATIN1 + 1, symlink_size);
305305
if (!utf8)
306306
{
307307
grub_free (latin1);
@@ -422,7 +422,7 @@ grub_affs_iterate_dir (grub_fshelp_node_t dir,
422422
return 1;
423423
}
424424

425-
hashtable = grub_zalloc (data->htsize * sizeof (*hashtable));
425+
hashtable = grub_calloc (data->htsize, sizeof (*hashtable));
426426
if (!hashtable)
427427
return 1;
428428

@@ -628,7 +628,7 @@ grub_affs_label (grub_device_t device, char **label)
628628
len = file.namelen;
629629
if (len > sizeof (file.name))
630630
len = sizeof (file.name);
631-
*label = grub_malloc (len * GRUB_MAX_UTF8_PER_LATIN1 + 1);
631+
*label = grub_calloc (GRUB_MAX_UTF8_PER_LATIN1 + 1, len);
632632
if (*label)
633633
*grub_latin1_to_utf8 ((grub_uint8_t *) *label, file.name, len) = '\0';
634634
}

grub-core/fs/btrfs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ lower_bound (struct grub_btrfs_data *data,
415415
{
416416
desc->allocated = 16;
417417
desc->depth = 0;
418-
desc->data = grub_malloc (sizeof (desc->data[0]) * desc->allocated);
418+
desc->data = grub_calloc (desc->allocated, sizeof (desc->data[0]));
419419
if (!desc->data)
420420
return grub_errno;
421421
}
@@ -754,7 +754,7 @@ raid56_read_retry (struct grub_btrfs_data *data,
754754
grub_err_t ret = GRUB_ERR_OUT_OF_MEMORY;
755755
grub_uint64_t i, failed_devices;
756756

757-
buffers = grub_zalloc (sizeof(*buffers) * nstripes);
757+
buffers = grub_calloc (nstripes, sizeof (*buffers));
758758
if (!buffers)
759759
goto cleanup;
760760

@@ -2167,7 +2167,7 @@ grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
21672167
*nsectors = 64 * 2 - 1;
21682168
if (*nsectors > max_nsectors)
21692169
*nsectors = max_nsectors;
2170-
*sectors = grub_malloc (*nsectors * sizeof (**sectors));
2170+
*sectors = grub_calloc (*nsectors, sizeof (**sectors));
21712171
if (!*sectors)
21722172
return grub_errno;
21732173
for (i = 0; i < *nsectors; i++)

grub-core/fs/hfs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ grub_hfs_label (grub_device_t device, char **label)
13601360
grub_size_t len = data->sblock.volname[0];
13611361
if (len > sizeof (data->sblock.volname) - 1)
13621362
len = sizeof (data->sblock.volname) - 1;
1363-
*label = grub_malloc (len * MAX_UTF8_PER_MAC_ROMAN + 1);
1363+
*label = grub_calloc (MAX_UTF8_PER_MAC_ROMAN + 1, len);
13641364
if (*label)
13651365
macroman_to_utf8 (*label, data->sblock.volname + 1,
13661366
len + 1, 0);

grub-core/fs/hfsplus.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ list_nodes (void *record, void *hook_arg)
720720
if (! filename)
721721
return 0;
722722

723-
keyname = grub_malloc (grub_be_to_cpu16 (catkey->namelen) * sizeof (*keyname));
723+
keyname = grub_calloc (grub_be_to_cpu16 (catkey->namelen), sizeof (*keyname));
724724
if (!keyname)
725725
{
726726
grub_free (filename);
@@ -1007,7 +1007,7 @@ grub_hfsplus_label (grub_device_t device, char **label)
10071007
grub_hfsplus_btree_recptr (&data->catalog_tree, node, ptr);
10081008

10091009
label_len = grub_be_to_cpu16 (catkey->namelen);
1010-
label_name = grub_malloc (label_len * sizeof (*label_name));
1010+
label_name = grub_calloc (label_len, sizeof (*label_name));
10111011
if (!label_name)
10121012
{
10131013
grub_free (node);
@@ -1029,7 +1029,7 @@ grub_hfsplus_label (grub_device_t device, char **label)
10291029
}
10301030
}
10311031

1032-
*label = grub_malloc (label_len * GRUB_MAX_UTF8_PER_UTF16 + 1);
1032+
*label = grub_calloc (label_len, GRUB_MAX_UTF8_PER_UTF16 + 1);
10331033
if (! *label)
10341034
{
10351035
grub_free (label_name);

0 commit comments

Comments
 (0)