Blame SOURCES/0311-efilinux-Fix-integer-overflows-in-grub_cmd_initrd.patch

c294fc
From 0a9a828e88a2e14be684761afe582e7bb286676b Mon Sep 17 00:00:00 2001
c294fc
From: Colin Watson <cjwatson@debian.org>
c294fc
Date: Fri, 24 Jul 2020 17:18:09 +0100
c294fc
Subject: [PATCH 311/314] efilinux: Fix integer overflows in grub_cmd_initrd
c294fc
c294fc
These could be triggered by an extremely large number of arguments to
c294fc
the initrd command on 32-bit architectures, or a crafted filesystem with
c294fc
very large files on any architecture.
c294fc
c294fc
Signed-off-by: Colin Watson <cjwatson@debian.org>
c294fc
---
c294fc
 grub-core/loader/i386/efi/linux.c | 9 +++++++--
c294fc
 1 file changed, 7 insertions(+), 2 deletions(-)
c294fc
c294fc
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
c294fc
index ea9f5134e67..ade7ab8f573 100644
c294fc
--- a/grub-core/loader/i386/efi/linux.c
c294fc
+++ b/grub-core/loader/i386/efi/linux.c
c294fc
@@ -28,6 +28,7 @@
c294fc
 #include <grub/efi/efi.h>
c294fc
 #include <grub/efi/linux.h>
c294fc
 #include <grub/tpm.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -94,7 +95,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
c294fc
       goto fail;
c294fc
     }
c294fc
 
c294fc
-  files = grub_zalloc (argc * sizeof (files[0]));
c294fc
+  files = grub_calloc (argc, sizeof (files[0]));
c294fc
   if (!files)
c294fc
     goto fail;
c294fc
 
c294fc
@@ -105,7 +106,11 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
c294fc
       if (! files[i])
c294fc
         goto fail;
c294fc
       nfiles++;
c294fc
-      size += ALIGN_UP (grub_file_size (files[i]), 4);
c294fc
+      if (grub_add (size, ALIGN_UP (grub_file_size (files[i]), 4), &size))
c294fc
+	{
c294fc
+	  grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+	  goto fail;
c294fc
+	}
c294fc
     }
c294fc
 
c294fc
   initrd_mem = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(size));
c294fc
-- 
c294fc
2.26.2
c294fc