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

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