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

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