disk: Check if returned pointer for allocated memory is NULL

When using grub_malloc(), grub_zalloc() or grub_calloc(), these functions can
fail if we are out of memory. After allocating memory we should check if these
functions returned NULL and handle this error if they did.

On the occasion make a NULL check in ATA code more obvious.

Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Alec Brown authored and Daniel Kiper committed 2025-02-13 15:45:56 +01:00
1 parent d8151f9833
commit 33bd6b5ac5
5 files changed
+30 -2

No files matched your search

+2 -2
View File
@@ -112,10 +112,10 @@ grub_ata_identify (struct grub_ata *dev)
return grub_atapi_identify (dev); return grub_atapi_identify (dev);
info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE); info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE);
if (info64 == NULL)
return grub_errno;
info32 = (grub_uint32_t *) info64; info32 = (grub_uint32_t *) info64;
info16 = (grub_uint16_t *) info64; info16 = (grub_uint16_t *) info64;
if (! info16)
return grub_errno;
grub_memset (&parms, 0, sizeof (parms)); grub_memset (&parms, 0, sizeof (parms));
parms.buffer = info16; parms.buffer = info16;
+6
View File
@@ -423,6 +423,12 @@ canonicalise_disk (const char *devname)
} }
real_canon = grub_malloc (real_unit_str_len); real_canon = grub_malloc (real_unit_str_len);
if (real_canon == NULL)
{
grub_free (parent);
grub_print_error ();
return NULL;
}
grub_snprintf (real_canon, real_unit_str_len, "%s/disk@%s", grub_snprintf (real_canon, real_unit_str_len, "%s/disk@%s",
op->name, real_unit_address); op->name, real_unit_address);
+6
View File
@@ -292,6 +292,12 @@ make_vg (grub_disk_t disk,
} }
pv->id.uuid = grub_malloc (sz); pv->id.uuid = grub_malloc (sz);
if (pv->id.uuid == NULL)
{
grub_free (pv->internal_id);
grub_free (pv);
goto fail2;
}
grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen); grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen);
pv->id.uuid[pv->id.uuidlen] = 0; pv->id.uuid[pv->id.uuidlen] = 0;
+14
View File
@@ -370,6 +370,8 @@ grub_lvm_detect (grub_disk_t disk,
break; break;
pv = grub_zalloc (sizeof (*pv)); pv = grub_zalloc (sizeof (*pv));
if (pv == NULL)
goto fail4;
q = p; q = p;
while (*q != ' ' && q < mda_end) while (*q != ' ' && q < mda_end)
q++; q++;
@@ -379,6 +381,8 @@ grub_lvm_detect (grub_disk_t disk,
s = q - p; s = q - p;
pv->name = grub_malloc (s + 1); pv->name = grub_malloc (s + 1);
if (pv->name == NULL)
goto pvs_fail_noname;
grub_memcpy (pv->name, p, s); grub_memcpy (pv->name, p, s);
pv->name[s] = '\0'; pv->name[s] = '\0';
@@ -451,6 +455,8 @@ grub_lvm_detect (grub_disk_t disk,
break; break;
lv = grub_zalloc (sizeof (*lv)); lv = grub_zalloc (sizeof (*lv));
if (lv == NULL)
goto fail4;
q = p; q = p;
while (*q != ' ' && q < mda_end) while (*q != ' ' && q < mda_end)
@@ -545,6 +551,8 @@ grub_lvm_detect (grub_disk_t disk,
goto lvs_fail; goto lvs_fail;
} }
lv->segments = grub_calloc (lv->segment_count, sizeof (*seg)); lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
if (lv->segments == NULL)
goto lvs_fail;
seg = lv->segments; seg = lv->segments;
for (i = 0; i < lv->segment_count; i++) for (i = 0; i < lv->segment_count; i++)
@@ -612,6 +620,8 @@ grub_lvm_detect (grub_disk_t disk,
seg->nodes = grub_calloc (seg->node_count, seg->nodes = grub_calloc (seg->node_count,
sizeof (*stripe)); sizeof (*stripe));
if (seg->nodes == NULL)
goto lvs_segment_fail;
stripe = seg->nodes; stripe = seg->nodes;
p = grub_strstr (p, "stripes = ["); p = grub_strstr (p, "stripes = [");
@@ -672,6 +682,8 @@ grub_lvm_detect (grub_disk_t disk,
} }
seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "mirrors = ["); p = grub_strstr (p, "mirrors = [");
if (p == NULL) if (p == NULL)
@@ -760,6 +772,8 @@ grub_lvm_detect (grub_disk_t disk,
} }
seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "raids = ["); p = grub_strstr (p, "raids = [");
if (p == NULL) if (p == NULL)
+2
View File
@@ -103,6 +103,8 @@ GRUB_MOD_INIT(memdisk)
return; return;
} }
memdisk_addr = grub_malloc (memdisk_size); memdisk_addr = grub_malloc (memdisk_size);
if (memdisk_addr == NULL)
return;
grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n"); grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size); grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);