]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
disk: Check if returned pointer for allocated memory is NULL
authorAlec Brown <alec.r.brown@oracle.com>
Wed, 22 Jan 2025 02:55:11 +0000 (02:55 +0000)
committerDaniel Kiper <daniel.kiper@oracle.com>
Thu, 13 Feb 2025 14:45:56 +0000 (15:45 +0100)
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>
grub-core/disk/ata.c
grub-core/disk/ieee1275/obdisk.c
grub-core/disk/ldm.c
grub-core/disk/lvm.c
grub-core/disk/memdisk.c

index 7b6ac7bfc708fb1216120d5f62ddcff3acc5f838..a2433e29e283aa66f470999e962047c6bfa6f798 100644 (file)
@@ -112,10 +112,10 @@ grub_ata_identify (struct grub_ata *dev)
     return grub_atapi_identify (dev);
 
   info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE);
+  if (info64 == NULL)
+    return grub_errno;
   info32 = (grub_uint32_t *) info64;
   info16 = (grub_uint16_t *) info64;
-  if (! info16)
-    return grub_errno;
 
   grub_memset (&parms, 0, sizeof (parms));
   parms.buffer = info16;
index 9d4c4266557e8a6d7039076b4300708842919614..fcc39e0a238eb311b7111a0a8f9ebd2486657de4 100644 (file)
@@ -423,6 +423,12 @@ canonicalise_disk (const char *devname)
        }
 
       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",
                      op->name, real_unit_address);
index 4101b15d8bc25cb6ad0451393923a379e44e0543..048e29cd09547048dabe2af4886061edf63cb027 100644 (file)
@@ -292,6 +292,12 @@ make_vg (grub_disk_t disk,
            }
 
          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);
          pv->id.uuid[pv->id.uuidlen] = 0;
 
index b53c3b75e2bfca9e0836ea39dacea177802f2c00..d5af854825bed825247a34bb41fb5487a9366ff0 100644 (file)
@@ -370,6 +370,8 @@ grub_lvm_detect (grub_disk_t disk,
                break;
 
              pv = grub_zalloc (sizeof (*pv));
+             if (pv == NULL)
+               goto fail4;
              q = p;
              while (*q != ' ' && q < mda_end)
                q++;
@@ -379,6 +381,8 @@ grub_lvm_detect (grub_disk_t disk,
 
              s = q - p;
              pv->name = grub_malloc (s + 1);
+             if (pv->name == NULL)
+               goto pvs_fail_noname;
              grub_memcpy (pv->name, p, s);
              pv->name[s] = '\0';
 
@@ -451,6 +455,8 @@ grub_lvm_detect (grub_disk_t disk,
                break;
 
              lv = grub_zalloc (sizeof (*lv));
+             if (lv == NULL)
+               goto fail4;
 
              q = p;
              while (*q != ' ' && q < mda_end)
@@ -545,6 +551,8 @@ grub_lvm_detect (grub_disk_t disk,
                  goto lvs_fail;
                }
              lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
+             if (lv->segments == NULL)
+               goto lvs_fail;
              seg = lv->segments;
 
              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,
                                                sizeof (*stripe));
+                     if (seg->nodes == NULL)
+                       goto lvs_segment_fail;
                      stripe = seg->nodes;
 
                      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]));
+                     if (seg->nodes == NULL)
+                       goto lvs_segment_fail;
 
                      p = grub_strstr (p, "mirrors = [");
                      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]));
+                     if (seg->nodes == NULL)
+                       goto lvs_segment_fail;
 
                      p = grub_strstr (p, "raids = [");
                      if (p == NULL)
index 36de3bfabf44bc8f7618e54a993bc352769139bb..2d7afaea3f09da5ec2f37b029a026991e00f5671 100644 (file)
@@ -103,6 +103,8 @@ GRUB_MOD_INIT(memdisk)
            return;
          }
        memdisk_addr = grub_malloc (memdisk_size);
+       if (memdisk_addr == NULL)
+         return;
 
        grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
        grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);