6b2dd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9efc5
From: Matthew Garrett <mjg59@coreos.com>
b9efc5
Date: Sun, 9 Aug 2015 16:12:39 -0700
31cddd
Subject: [PATCH] Rework linux command
b9efc5
b9efc5
We want a single buffer that contains the entire kernel image in order to
b9efc5
perform a TPM measurement. Allocate one and copy the entire kernel into it
b9efc5
before pulling out the individual blocks later on.
b9efc5
---
ad4aff
 grub-core/loader/i386/linux.c | 37 ++++++++++++++++++++++++-------------
ad4aff
 1 file changed, 24 insertions(+), 13 deletions(-)
b9efc5
b9efc5
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
15a207
index 9b53d3168f9..f7186be4002 100644
b9efc5
--- a/grub-core/loader/i386/linux.c
b9efc5
+++ b/grub-core/loader/i386/linux.c
15a207
@@ -685,13 +685,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
ad4aff
 {
b9efc5
   grub_file_t file = 0;
752ceb
   struct linux_i386_kernel_header lh;
ad4aff
+  grub_uint8_t *linux_params_ptr;
b9efc5
   grub_uint8_t setup_sects;
b9efc5
-  grub_size_t real_size, prot_size, prot_file_size;
b9efc5
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
b9efc5
   grub_ssize_t len;
b9efc5
   int i;
b9efc5
   grub_size_t align, min_align;
b9efc5
   int relocatable;
b9efc5
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
b9efc5
+  grub_uint8_t *kernel = NULL;
b9efc5
 
b9efc5
   grub_dl_ref (my_mod);
b9efc5
 
15a207
@@ -705,7 +707,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
   if (! file)
b9efc5
     goto fail;
b9efc5
 
b9efc5
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
b9efc5
+  len = grub_file_size (file);
b9efc5
+  kernel = grub_malloc (len);
b9efc5
+  if (!kernel)
b9efc5
+    {
b9efc5
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
b9efc5
+      goto fail;
b9efc5
+    }
b9efc5
+
b9efc5
+  if (grub_file_read (file, kernel, len) != len)
b9efc5
     {
b9efc5
       if (!grub_errno)
b9efc5
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
15a207
@@ -713,6 +723,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
       goto fail;
b9efc5
     }
b9efc5
 
b9efc5
+  grub_memcpy (&lh, kernel, sizeof (lh));
b9efc5
+  kernel_offset = sizeof (lh);
b9efc5
+
b9efc5
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
b9efc5
     {
b9efc5
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
15a207
@@ -804,6 +817,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
ad4aff
 		      preferred_address))
ad4aff
     goto fail;
ad4aff
 
ad4aff
+
ad4aff
   grub_memset (&linux_params, 0, sizeof (linux_params));
ad4aff
   grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
ad4aff
 
15a207
@@ -812,13 +826,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
   linux_params.ps_mouse = linux_params.padding10 =  0;
b9efc5
 
b9efc5
   len = sizeof (linux_params) - sizeof (lh);
b9efc5
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
b9efc5
-    {
b9efc5
-      if (!grub_errno)
b9efc5
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
b9efc5
-		    argv[0]);
b9efc5
-      goto fail;
b9efc5
-    }
b9efc5
+
ad4aff
+  linux_params_ptr = (void *)&linux_params;
ad4aff
+  grub_memcpy (linux_params_ptr + sizeof (lh), kernel + kernel_offset, len);
b9efc5
+  kernel_offset += len;
b9efc5
 
b9efc5
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
b9efc5
 
15a207
@@ -877,7 +888,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
 
b9efc5
   /* The other parameters are filled when booting.  */
b9efc5
 
b9efc5
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
b9efc5
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
b9efc5
 
b9efc5
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
b9efc5
 		(unsigned) real_size, (unsigned) prot_size);
15a207
@@ -1025,9 +1036,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
 			      - (sizeof (LINUX_IMAGE) - 1));
b9efc5
 
b9efc5
   len = prot_file_size;
b9efc5
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
b9efc5
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
b9efc5
-		argv[0]);
b9efc5
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
b9efc5
 
b9efc5
   if (grub_errno == GRUB_ERR_NONE)
b9efc5
     {
15a207
@@ -1038,6 +1047,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9efc5
 
b9efc5
  fail:
b9efc5
 
b9efc5
+  grub_free (kernel);
b9efc5
+
b9efc5
   if (file)
b9efc5
     grub_file_close (file);
b9efc5