nalika / rpms / grub2

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