27a4da
From b0c5ad48da4ee57aa56755bf0a38671de7e0dfc1 Mon Sep 17 00:00:00 2001
27a4da
From: Matthew Garrett <mjg59@coreos.com>
27a4da
Date: Tue, 14 Jul 2015 16:58:51 -0700
27a4da
Subject: [PATCH 215/260] Fix race in EFI validation
27a4da
27a4da
---
27a4da
 grub-core/loader/i386/efi/linux.c | 44 ++++++++++-----------------------------
27a4da
 1 file changed, 11 insertions(+), 33 deletions(-)
27a4da
27a4da
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
27a4da
index e5b778577..7ccf32d9d 100644
27a4da
--- a/grub-core/loader/i386/efi/linux.c
27a4da
+++ b/grub-core/loader/i386/efi/linux.c
27a4da
@@ -154,7 +154,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
   grub_file_t file = 0;
27a4da
   struct linux_kernel_header lh;
27a4da
   grub_ssize_t len, start, filelen;
27a4da
-  void *kernel;
27a4da
+  void *kernel = NULL;
27a4da
 
27a4da
   grub_dl_ref (my_mod);
27a4da
 
27a4da
@@ -191,10 +191,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
       goto fail;
27a4da
     }
27a4da
 
27a4da
-  grub_file_seek (file, 0);
27a4da
-
27a4da
-  grub_free(kernel);
27a4da
-
27a4da
   params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
27a4da
 
27a4da
   if (! params)
27a4da
@@ -203,15 +199,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
       goto fail;
27a4da
     }
27a4da
 
27a4da
-  memset (params, 0, 16384);
27a4da
+  grub_memset (params, 0, 16384);
27a4da
 
27a4da
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
27a4da
-    {
27a4da
-      if (!grub_errno)
27a4da
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
-		    argv[0]);
27a4da
-      goto fail;
27a4da
-    }
27a4da
+  grub_memcpy (&lh, kernel, sizeof (lh));
27a4da
 
27a4da
   if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
27a4da
     {
27a4da
@@ -271,27 +261,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
       goto fail;
27a4da
     }
27a4da
 
27a4da
-  if (grub_file_seek (file, start) == (grub_off_t) -1)
27a4da
-    {
27a4da
-      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
-		  argv[0]);
27a4da
-      goto fail;
27a4da
-    }
27a4da
+  grub_memcpy (kernel_mem, (char *)kernel + start, len);
27a4da
+  grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
27a4da
+  loaded=1;
27a4da
 
27a4da
-  if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
27a4da
-    {
27a4da
-      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
-		  argv[0]);
27a4da
-    }
27a4da
-
27a4da
-  if (grub_errno == GRUB_ERR_NONE)
27a4da
-    {
27a4da
-      grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
27a4da
-      loaded = 1;
27a4da
-      lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
27a4da
-    }
27a4da
-
27a4da
-  memcpy(params, &lh, 2 * 512);
27a4da
+  lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
27a4da
+  grub_memcpy (params, &lh, 2 * 512);
27a4da
 
27a4da
   params->type_of_loader = 0x21;
27a4da
 
27a4da
@@ -300,6 +275,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
   if (file)
27a4da
     grub_file_close (file);
27a4da
 
27a4da
+  if (kernel)
27a4da
+    grub_free (kernel);
27a4da
+
27a4da
   if (grub_errno != GRUB_ERR_NONE)
27a4da
     {
27a4da
       grub_dl_unref (my_mod);
27a4da
-- 
27a4da
2.13.0
27a4da