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

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