6b2dd0f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9efc54
From: Matthew Garrett <mjg59@coreos.com>
46968b6
Date: Sun, 9 Aug 2015 16:12:39 -0700
31cddd6
Subject: [PATCH] Rework linux command
b9efc54
b9efc54
We want a single buffer that contains the entire kernel image in order to
b9efc54
perform a TPM measurement. Allocate one and copy the entire kernel into it
b9efc54
before pulling out the individual blocks later on.
46968b6
46968b6
Signed-off-by: Matthew Garrett <mjg59@coreos.com>
b9efc54
---
46968b6
 grub-core/loader/i386/linux.c | 35 +++++++++++++++++++++++------------
46968b6
 1 file changed, 23 insertions(+), 12 deletions(-)
b9efc54
b9efc54
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
e622855
index 9f74a96b19..dccf3bb300 100644
b9efc54
--- a/grub-core/loader/i386/linux.c
b9efc54
+++ b/grub-core/loader/i386/linux.c
46968b6
@@ -649,13 +649,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
ad4aff0
 {
b9efc54
   grub_file_t file = 0;
752ceb1
   struct linux_i386_kernel_header lh;
ad4aff0
+  grub_uint8_t *linux_params_ptr;
b9efc54
   grub_uint8_t setup_sects;
b9efc54
-  grub_size_t real_size, prot_size, prot_file_size;
b9efc54
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
b9efc54
   grub_ssize_t len;
b9efc54
   int i;
b9efc54
   grub_size_t align, min_align;
b9efc54
   int relocatable;
b9efc54
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
b9efc54
+  grub_uint8_t *kernel = NULL;
b9efc54
 
b9efc54
   grub_dl_ref (my_mod);
b9efc54
 
46968b6
@@ -669,7 +671,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc54
   if (! file)
b9efc54
     goto fail;
b9efc54
 
b9efc54
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
b9efc54
+  len = grub_file_size (file);
b9efc54
+  kernel = grub_malloc (len);
b9efc54
+  if (!kernel)
b9efc54
+    {
b9efc54
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
b9efc54
+      goto fail;
b9efc54
+    }
b9efc54
+
b9efc54
+  if (grub_file_read (file, kernel, len) != len)
b9efc54
     {
b9efc54
       if (!grub_errno)
b9efc54
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
46968b6
@@ -677,6 +687,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc54
       goto fail;
b9efc54
     }
b9efc54
 
b9efc54
+  grub_memcpy (&lh, kernel, sizeof (lh));
b9efc54
+  kernel_offset = sizeof (lh);
b9efc54
+
b9efc54
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
b9efc54
     {
b9efc54
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
46968b6
@@ -784,13 +797,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
e153146
   /* We've already read lh so there is no need to read it second time. */
e153146
   len -= sizeof(lh);
b9efc54
 
46968b6
-  if ((len > 0) &&
46968b6
-      (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len))
46968b6
+  linux_params_ptr = (void *)&linux_params;
46968b6
+  if (len > 0)
46968b6
     {
b9efc54
-      if (!grub_errno)
b9efc54
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
b9efc54
-		    argv[0]);
b9efc54
-      goto fail;
46968b6
+      grub_memcpy (linux_params_ptr + sizeof (lh), kernel + kernel_offset, len);
46968b6
+      kernel_offset += len;
46968b6
     }
b9efc54
 
46968b6
   linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
46968b6
@@ -853,7 +864,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc54
 
b9efc54
   /* The other parameters are filled when booting.  */
b9efc54
 
b9efc54
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
b9efc54
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
b9efc54
 
b9efc54
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
b9efc54
 		(unsigned) real_size, (unsigned) prot_size);
46968b6
@@ -1007,9 +1018,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
e153146
   }
b9efc54
 
b9efc54
   len = prot_file_size;
b9efc54
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
b9efc54
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
b9efc54
-		argv[0]);
b9efc54
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
b9efc54
 
b9efc54
   if (grub_errno == GRUB_ERR_NONE)
b9efc54
     {
46968b6
@@ -1020,6 +1029,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc54
 
b9efc54
  fail:
b9efc54
 
b9efc54
+  grub_free (kernel);
b9efc54
+
b9efc54
   if (file)
b9efc54
     grub_file_close (file);
b9efc54