Blame SOURCES/0223-Rework-linux16-command.patch

d41074
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a85e8e
From: Matthew Garrett <mjg59@coreos.com>
a85e8e
Date: Sun, 9 Aug 2015 16:20:58 -0700
d41074
Subject: [PATCH] Rework linux16 command
a85e8e
a85e8e
We want a single buffer that contains the entire kernel image in order to
a85e8e
perform a TPM measurement. Allocate one and copy the entire kernel int it
a85e8e
before pulling out the individual blocks later on.
a85e8e
---
a85e8e
 grub-core/loader/i386/pc/linux.c | 54 +++++++++++++++++++++++-----------------
a85e8e
 1 file changed, 31 insertions(+), 23 deletions(-)
a85e8e
a85e8e
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
d41074
index b19527e8e17..60bb31fbf0d 100644
a85e8e
--- a/grub-core/loader/i386/pc/linux.c
a85e8e
+++ b/grub-core/loader/i386/pc/linux.c
a85e8e
@@ -124,13 +124,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   grub_file_t file = 0;
a85e8e
   struct linux_kernel_header lh;
a85e8e
   grub_uint8_t setup_sects;
a85e8e
-  grub_size_t real_size;
a85e8e
+  grub_size_t real_size, kernel_offset = 0;
a85e8e
   grub_ssize_t len;
a85e8e
   int i;
a85e8e
   char *grub_linux_prot_chunk;
a85e8e
   int grub_linux_is_bzimage;
a85e8e
   grub_addr_t grub_linux_prot_target;
a85e8e
   grub_err_t err;
a85e8e
+  grub_uint8_t *kernel = NULL;
a85e8e
 
a85e8e
   grub_dl_ref (my_mod);
a85e8e
 
a85e8e
@@ -144,7 +145,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   if (! file)
a85e8e
     goto fail;
a85e8e
 
a85e8e
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
a85e8e
+  len = grub_file_size (file);
a85e8e
+  kernel = grub_malloc (len);
a85e8e
+  if (!kernel)
a85e8e
+    {
a85e8e
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
a85e8e
+      goto fail;
a85e8e
+    }
a85e8e
+
a85e8e
+  if (grub_file_read (file, kernel, len) != len)
a85e8e
     {
a85e8e
       if (!grub_errno)
a85e8e
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
@@ -152,7 +161,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
       goto fail;
a85e8e
     }
a85e8e
 
a85e8e
-  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
a85e8e
+  grub_memcpy (&lh, kernel, sizeof (lh));
a85e8e
+  kernel_offset = sizeof (lh);
a85e8e
+
a85e8e
+  if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
a85e8e
     {
a85e8e
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
a85e8e
       goto fail;
a85e8e
@@ -170,7 +182,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
   maximal_cmdline_size = 256;
a85e8e
 
a85e8e
-  if (lh.header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
+  if (lh.header == grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
       && grub_le_to_cpu16 (lh.version) >= 0x0200)
a85e8e
     {
a85e8e
       grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
a85e8e
@@ -189,7 +201,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
       if (grub_le_to_cpu16 (lh.version) >= 0x0201)
a85e8e
 	{
a85e8e
-	  lh.heap_end_ptr = grub_cpu_to_le16 (GRUB_LINUX_HEAP_END_OFFSET);
a85e8e
+	  lh.heap_end_ptr = grub_cpu_to_le32_compile_time (GRUB_LINUX_HEAP_END_OFFSET);
a85e8e
 	  lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
a85e8e
 	}
a85e8e
 
a85e8e
@@ -197,17 +209,17 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 	lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
a85e8e
       else
a85e8e
 	{
a85e8e
-	  lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
a85e8e
-	  lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
a85e8e
-	  lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET
a85e8e
+	  lh.cl_magic = grub_cpu_to_le32_compile_time (GRUB_LINUX_CL_MAGIC);
a85e8e
+	  lh.cl_offset = grub_cpu_to_le32_compile_time (GRUB_LINUX_CL_OFFSET);
a85e8e
+	  lh.setup_move_size = grub_cpu_to_le32_compile_time (GRUB_LINUX_CL_OFFSET
a85e8e
 						 + maximal_cmdline_size);
a85e8e
 	}
a85e8e
     }
a85e8e
   else
a85e8e
     {
a85e8e
       /* Your kernel is quite old...  */
a85e8e
-      lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
a85e8e
-      lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
a85e8e
+      lh.cl_magic = grub_cpu_to_le32_compile_time (GRUB_LINUX_CL_MAGIC);
a85e8e
+      lh.cl_offset = grub_cpu_to_le32_compile_time (GRUB_LINUX_CL_OFFSET);
a85e8e
 
a85e8e
       setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
a85e8e
 
a85e8e
@@ -312,15 +324,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
a85e8e
 
a85e8e
   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
a85e8e
-  if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
a85e8e
-    {
a85e8e
-      if (!grub_errno)
a85e8e
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
-		    argv[0]);
a85e8e
-      goto fail;
a85e8e
-    }
a85e8e
+  grub_memcpy (grub_linux_real_chunk + sizeof (lh), kernel + kernel_offset,
a85e8e
+	       len);
a85e8e
+  kernel_offset += len;
a85e8e
 
a85e8e
-  if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
+  if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
       || grub_le_to_cpu16 (lh.version) < 0x0200)
a85e8e
     /* Clear the heap space.  */
a85e8e
     grub_memset (grub_linux_real_chunk
a85e8e
@@ -353,10 +361,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   }
a85e8e
 
a85e8e
   len = grub_linux16_prot_size;
a85e8e
-  if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
a85e8e
-      != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
a85e8e
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
-		argv[0]);
a85e8e
+  grub_memcpy (grub_linux_prot_chunk, kernel + kernel_offset, len);
a85e8e
+  kernel_offset += len;
a85e8e
 
a85e8e
   if (grub_errno == GRUB_ERR_NONE)
a85e8e
     {
a85e8e
@@ -366,6 +372,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
  fail:
a85e8e
 
a85e8e
+  grub_free (kernel);
a85e8e
+
a85e8e
   if (file)
a85e8e
     grub_file_close (file);
a85e8e
 
a85e8e
@@ -405,7 +413,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
   lh = (struct linux_kernel_header *) grub_linux_real_chunk;
a85e8e
 
a85e8e
-  if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
+  if (!(lh->header == grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
a85e8e
 	&& grub_le_to_cpu16 (lh->version) >= 0x0200))
a85e8e
     {
a85e8e
       grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");