Skip to contentSkip to conversation

diskfilter: check calloc() result for NULL

PATCH In review Daniel Axtens 2 participants 5 messages Aug 21, 2022 – Oct 6, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 diskfilter: check calloc() result for NULL

With wildly corrupt inputs, we can end up trying to calloc a very large amount of memory, which will fail and give us a NULL pointer. We need to check that to avoid a crash. (And, even if we blocked such inputs, it is good practice to check the results of allocations anyway.)

---
 grub-core/disk/diskfilter.c | 3 +++
 1 file changed, 3 insertions(+)
grub-core/disk/diskfilter.c +3 -0
index 2edcff6e8987..4ac50320ef4e 100644
@@ -1163,6 +1163,9 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
   array->lvs->segments->raid_member_size = disk_size;
   array->lvs->segments->nodes
     = grub_calloc (nmemb, sizeof (array->lvs->segments->nodes[0]));
+  if (array->lvs->segments->nodes == NULL)
+    goto fail;
+
   array->lvs->segments->stripe_size = stripe_size;
   for (i = 0; i < nmemb; i++)
     {
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 diskfilter: don't make a RAID array with more than 1024 disks

This is 'belt and braces' with the last fix: we end up trying to use too much memory in situations like corrupted Linux software raid setups purporting to usew a huge number of disks. Simply refuse to permit such configurations.

1024 is a bit arbitrary, yes, and I feel a bit like I'm tempting fate here, but I think 1024 disks in an array (that grub has to read to boot!) should be enough for anyone.

---
 grub-core/disk/diskfilter.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/disk/diskfilter.c +7 -0
index 4ac50320ef4e..79c5f4db940a 100644
@@ -1046,6 +1046,13 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
   struct grub_diskfilter_pv *pv;
   grub_err_t err;
 
+  /* We choose not to support more than 1024 disks */
+  if (nmemb > 1024)
+    {
+      grub_free (uuid);
+      return NULL;
+    }
+
   switch (level)
     {
     case 1:
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/2] diskfilter: don't make a RAID array with more than 1024 disks

Quoted 19 lines · Daniel Axtens

This is 'belt and braces' with the last fix: we end up trying to use too much memory in situations like corrupted Linux software raid setups purporting to usew a huge number of disks. Simply refuse to permit such configurations.

1024 is a bit arbitrary, yes, and I feel a bit like I'm tempting fate here, but I think 1024 disks in an array (that grub has to read to boot!) should be enough for anyone.

---
 grub-core/disk/diskfilter.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/disk/diskfilter.c +0 -0
index 4ac50320ef4e..79c5f4db940a 100644

grub-core/disk/diskfilter.c grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb, · line 1050

Quoted 5 lines
grub-core/disk/diskfilter.c +2 -0
@@ -1046,6 +1046,13 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
   struct grub_diskfilter_pv *pv;
   grub_err_t err;
 
+  /* We choose not to support more than 1024 disks */
+  if (nmemb > 1024)

Ergh, nmemb is an int; I will do a v2 that also checks that it's greater than 0 (or 1, given that it's RAID? I will do some tests.)

-- d

#3 lore raw

Re: [PATCH 2/2] diskfilter: don't make a RAID array with more than 1024 disks

Quoted 24 lines · Daniel Axtens
Quoted 22 lines · Daniel Axtens

This is 'belt and braces' with the last fix: we end up trying to use too much memory in situations like corrupted Linux software raid setups purporting to usew a huge number of disks. Simply refuse to permit such configurations.

1024 is a bit arbitrary, yes, and I feel a bit like I'm tempting fate here, but I think 1024 disks in an array (that grub has to read to boot!) should be enough for anyone.

---
 grub-core/disk/diskfilter.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/disk/diskfilter.c +0 -0
index 4ac50320ef4e..79c5f4db940a 100644
@@ -1046,6 +1046,13 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb,
   struct grub_diskfilter_pv *pv;
   grub_err_t err;
 
Quoted 5 lines

grub-core/disk/diskfilter.c grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb, · line 1050

Quoted 2 lines
grub-core/disk/diskfilter.c +2 -0
+  /* We choose not to support more than 1024 disks */
+  if (nmemb > 1024)

Ergh, nmemb is an int; I will do a v2 that also checks that it's greater than 0 (or 1, given that it's RAID? I will do some tests.)

Could you repost this patch with the fix? Or both with my RB for first one...

Daniel

Reviewed-by on v1 1/2
#4 lore raw
In reply to Daniel Axtens
Quoted 7 lines · Daniel Axtens

With wildly corrupt inputs, we can end up trying to calloc a very large amount of memory, which will fail and give us a NULL pointer. We need to check that to avoid a crash. (And, even if we blocked such inputs, it is good practice to check the results of allocations anyway.)

Daniel