dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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