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