8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Matthew Garrett <mjg59@coreos.com>
8631a2
Date: Sun, 9 Aug 2015 16:20:58 -0700
8631a2
Subject: [PATCH] Rework linux16 command
8631a2
8631a2
We want a single buffer that contains the entire kernel image in order to
8631a2
perform a TPM measurement. Allocate one and copy the entire kernel int it
8631a2
before pulling out the individual blocks later on.
8631a2
---
8631a2
 grub-core/loader/i386/pc/linux.c | 34 +++++++++++++++++++++-------------
8631a2
 1 file changed, 21 insertions(+), 13 deletions(-)
8631a2
8631a2
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
b32e65
index a3c87cf2f..caa76bee8 100644
8631a2
--- a/grub-core/loader/i386/pc/linux.c
8631a2
+++ b/grub-core/loader/i386/pc/linux.c
8631a2
@@ -123,13 +123,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
   grub_file_t file = 0;
8631a2
   struct linux_i386_kernel_header lh;
8631a2
   grub_uint8_t setup_sects;
8631a2
-  grub_size_t real_size;
8631a2
+  grub_size_t real_size, kernel_offset = 0;
8631a2
   grub_ssize_t len;
8631a2
   int i;
8631a2
   char *grub_linux_prot_chunk;
8631a2
   int grub_linux_is_bzimage;
8631a2
   grub_addr_t grub_linux_prot_target;
8631a2
   grub_err_t err;
8631a2
+  grub_uint8_t *kernel = NULL;
8631a2
 
8631a2
   grub_dl_ref (my_mod);
8631a2
 
8631a2
@@ -143,7 +144,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
   if (! file)
8631a2
     goto fail;
8631a2
 
8631a2
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
8631a2
+  len = grub_file_size (file);
8631a2
+  kernel = grub_malloc (len);
8631a2
+  if (!kernel)
8631a2
+    {
8631a2
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
8631a2
+      goto fail;
8631a2
+    }
8631a2
+
8631a2
+  if (grub_file_read (file, kernel, len) != len)
8631a2
     {
8631a2
       if (!grub_errno)
8631a2
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8631a2
@@ -151,6 +160,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
       goto fail;
8631a2
     }
8631a2
 
8631a2
+  grub_memcpy (&lh, kernel, sizeof (lh));
8631a2
+  kernel_offset = sizeof (lh);
8631a2
+
8631a2
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
8631a2
     {
8631a2
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
8631a2
@@ -314,13 +326,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
8631a2
 
8631a2
   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
8631a2
-  if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
8631a2
-    {
8631a2
-      if (!grub_errno)
8631a2
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8631a2
-		    argv[0]);
8631a2
-      goto fail;
8631a2
-    }
8631a2
+  grub_memcpy (grub_linux_real_chunk + sizeof (lh), kernel + kernel_offset,
8631a2
+	       len);
8631a2
+  kernel_offset += len;
8631a2
 
8631a2
   if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
8631a2
       || grub_le_to_cpu16 (lh.version) < 0x0200)
8631a2
@@ -355,10 +363,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
   }
8631a2
 
8631a2
   len = grub_linux16_prot_size;
8631a2
-  if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
8631a2
-      != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
8631a2
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8631a2
-		argv[0]);
8631a2
+  grub_memcpy (grub_linux_prot_chunk, kernel + kernel_offset, len);
8631a2
+  kernel_offset += len;
8631a2
 
8631a2
   if (grub_errno == GRUB_ERR_NONE)
8631a2
     {
8631a2
@@ -368,6 +374,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8631a2
 
8631a2
  fail:
8631a2
 
8631a2
+  grub_free (kernel);
8631a2
+
8631a2
   if (file)
8631a2
     grub_file_close (file);
8631a2