dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone
4fe85b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4fe85b
From: Matthew Garrett <mjg59@coreos.com>
4fe85b
Date: Sun, 9 Aug 2015 16:12:39 -0700
4fe85b
Subject: [PATCH] Rework linux command
4fe85b
4fe85b
We want a single buffer that contains the entire kernel image in order to
4fe85b
perform a TPM measurement. Allocate one and copy the entire kernel into it
4fe85b
before pulling out the individual blocks later on.
4fe85b
---
4fe85b
 grub-core/loader/i386/linux.c | 34 +++++++++++++++++++++-------------
4fe85b
 1 file changed, 21 insertions(+), 13 deletions(-)
4fe85b
4fe85b
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
4fe85b
index bd37c69b5d0..53f74ae0685 100644
4fe85b
--- a/grub-core/loader/i386/linux.c
4fe85b
+++ b/grub-core/loader/i386/linux.c
4fe85b
@@ -682,12 +682,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
   grub_file_t file = 0;
4fe85b
   struct linux_kernel_header lh;
4fe85b
   grub_uint8_t setup_sects;
4fe85b
-  grub_size_t real_size, prot_size, prot_file_size;
4fe85b
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
4fe85b
   grub_ssize_t len;
4fe85b
   int i;
4fe85b
   grub_size_t align, min_align;
4fe85b
   int relocatable;
4fe85b
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
4fe85b
+  grub_uint8_t *kernel = NULL;
4fe85b
 
4fe85b
   grub_dl_ref (my_mod);
4fe85b
 
4fe85b
@@ -701,7 +702,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
   if (! file)
4fe85b
     goto fail;
4fe85b
 
4fe85b
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
4fe85b
+  len = grub_file_size (file);
4fe85b
+  kernel = grub_malloc (len);
4fe85b
+  if (!kernel)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (grub_file_read (file, kernel, len) != len)
4fe85b
     {
4fe85b
       if (!grub_errno)
4fe85b
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
@@ -709,6 +718,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
       goto fail;
4fe85b
     }
4fe85b
 
4fe85b
+  grub_memcpy (&lh, kernel, sizeof (lh));
4fe85b
+  kernel_offset = sizeof (lh);
4fe85b
+
4fe85b
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
4fe85b
     {
4fe85b
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
4fe85b
@@ -808,13 +820,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
   linux_params.ps_mouse = linux_params.padding10 =  0;
4fe85b
 
4fe85b
   len = sizeof (linux_params) - sizeof (lh);
4fe85b
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
4fe85b
-    {
4fe85b
-      if (!grub_errno)
4fe85b
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
-		    argv[0]);
4fe85b
-      goto fail;
4fe85b
-    }
4fe85b
+
4fe85b
+  grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
4fe85b
+  kernel_offset += len;
4fe85b
 
4fe85b
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
4fe85b
 
4fe85b
@@ -873,7 +881,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
 
4fe85b
   /* The other parameters are filled when booting.  */
4fe85b
 
4fe85b
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
4fe85b
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
4fe85b
 
4fe85b
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
4fe85b
 		(unsigned) real_size, (unsigned) prot_size);
4fe85b
@@ -1018,9 +1026,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
 			      - (sizeof (LINUX_IMAGE) - 1));
4fe85b
 
4fe85b
   len = prot_file_size;
4fe85b
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
4fe85b
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
-		argv[0]);
4fe85b
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
4fe85b
 
4fe85b
   if (grub_errno == GRUB_ERR_NONE)
4fe85b
     {
4fe85b
@@ -1031,6 +1037,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
 
4fe85b
  fail:
4fe85b
 
4fe85b
+  grub_free (kernel);
4fe85b
+
4fe85b
   if (file)
4fe85b
     grub_file_close (file);
4fe85b