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:20:58 -0700
d9d99f
Subject: [PATCH] Rework linux16 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 int it
d9d99f
before pulling out the individual blocks later on.
d9d99f
---
d9d99f
 grub-core/loader/i386/pc/linux.c | 34 +++++++++++++++++++++-------------
d9d99f
 1 file changed, 21 insertions(+), 13 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
b71686
index a3c87cf2f..caa76bee8 100644
d9d99f
--- a/grub-core/loader/i386/pc/linux.c
d9d99f
+++ b/grub-core/loader/i386/pc/linux.c
d9d99f
@@ -123,13 +123,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
d9d99f
   grub_file_t file = 0;
d9d99f
   struct linux_i386_kernel_header lh;
d9d99f
   grub_uint8_t setup_sects;
d9d99f
-  grub_size_t real_size;
d9d99f
+  grub_size_t real_size, kernel_offset = 0;
d9d99f
   grub_ssize_t len;
d9d99f
   int i;
d9d99f
   char *grub_linux_prot_chunk;
d9d99f
   int grub_linux_is_bzimage;
d9d99f
   grub_addr_t grub_linux_prot_target;
d9d99f
   grub_err_t err;
d9d99f
+  grub_uint8_t *kernel = NULL;
d9d99f
 
d9d99f
   grub_dl_ref (my_mod);
d9d99f
 
d9d99f
@@ -143,7 +144,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
@@ -151,6 +160,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
@@ -314,13 +326,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
d9d99f
   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
d9d99f
 
d9d99f
   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
d9d99f
-  if (grub_file_read (file, grub_linux_real_chunk + 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
+  grub_memcpy (grub_linux_real_chunk + sizeof (lh), kernel + kernel_offset,
d9d99f
+	       len);
d9d99f
+  kernel_offset += len;
d9d99f
 
d9d99f
   if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
d9d99f
       || grub_le_to_cpu16 (lh.version) < 0x0200)
d9d99f
@@ -355,10 +363,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
d9d99f
   }
d9d99f
 
d9d99f
   len = grub_linux16_prot_size;
d9d99f
-  if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
d9d99f
-      != (grub_ssize_t) grub_linux16_prot_size && !grub_errno)
d9d99f
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
d9d99f
-		argv[0]);
d9d99f
+  grub_memcpy (grub_linux_prot_chunk, kernel + kernel_offset, len);
d9d99f
+  kernel_offset += len;
d9d99f
 
d9d99f
   if (grub_errno == GRUB_ERR_NONE)
d9d99f
     {
d9d99f
@@ -368,6 +374,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