dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0220-Add-secureboot-support-on-efi-chainloader.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Peter Jones <pjones@redhat.com>
f725e3
Date: Tue, 6 Oct 2015 13:04:37 -0400
f725e3
Subject: [PATCH] Add secureboot support on efi chainloader
f725e3
f725e3
Expand the chainloader to be able to verify the image by means of shim
f725e3
lock protocol. The PE/COFF image is loaded and relocated by the
f725e3
chainloader instead of calling LoadImage and StartImage UEFI boot
f725e3
Service as they require positive verification result from keys enrolled
f725e3
in KEK or DB. The shim will use MOK in addition to firmware enrolled
f725e3
keys to verify the image.
f725e3
f725e3
The chainloader module could be used to load other UEFI bootloaders,
f725e3
such as xen.efi, and could be signed by any of MOK, KEK or DB.
f725e3
f725e3
Based on https://build.opensuse.org/package/view_file/openSUSE:Factory/grub2/grub2-secureboot-chainloader.patch
f725e3
f725e3
Signed-off-by: Peter Jones <pjones@redhat.com>
f725e3
---
f725e3
 grub-core/loader/efi/chainloader.c | 612 ++++++++++++++++++++++++++++++++++---
f725e3
 include/grub/efi/pe32.h            |  20 +-
f725e3
 2 files changed, 595 insertions(+), 37 deletions(-)
f725e3
f725e3
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
f725e3
index 14ce6ddd7ad..87a91e16f17 100644
f725e3
--- a/grub-core/loader/efi/chainloader.c
f725e3
+++ b/grub-core/loader/efi/chainloader.c
f725e3
@@ -32,6 +32,8 @@
f725e3
 #include <grub/efi/api.h>
f725e3
 #include <grub/efi/efi.h>
f725e3
 #include <grub/efi/disk.h>
f725e3
+#include <grub/efi/pe32.h>
f725e3
+#include <grub/efi/linux.h>
f725e3
 #include <grub/command.h>
f725e3
 #include <grub/i18n.h>
f725e3
 #include <grub/net.h>
f725e3
@@ -46,9 +48,14 @@ static grub_dl_t my_mod;
f725e3
 
f725e3
 static grub_efi_physical_address_t address;
f725e3
 static grub_efi_uintn_t pages;
f725e3
+static grub_ssize_t fsize;
f725e3
 static grub_efi_device_path_t *file_path;
f725e3
 static grub_efi_handle_t image_handle;
f725e3
 static grub_efi_char16_t *cmdline;
f725e3
+static grub_ssize_t cmdline_len;
f725e3
+static grub_efi_handle_t dev_handle;
f725e3
+
f725e3
+static grub_efi_status_t (*entry_point) (grub_efi_handle_t image_handle, grub_efi_system_table_t *system_table);
f725e3
 
f725e3
 static grub_err_t
f725e3
 grub_chainloader_unload (void)
f725e3
@@ -63,6 +70,7 @@ grub_chainloader_unload (void)
f725e3
   grub_free (cmdline);
f725e3
   cmdline = 0;
f725e3
   file_path = 0;
f725e3
+  dev_handle = 0;
f725e3
 
f725e3
   grub_dl_unref (my_mod);
f725e3
   return GRUB_ERR_NONE;
f725e3
@@ -191,12 +199,523 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
f725e3
   return file_path;
f725e3
 }
f725e3
 
f725e3
+#define SHIM_LOCK_GUID \
f725e3
+  { 0x605dab50, 0xe046, 0x4300, { 0xab,0xb6,0x3d,0xd8,0x10,0xdd,0x8b,0x23 } }
f725e3
+
f725e3
+typedef union
f725e3
+{
f725e3
+  struct grub_pe32_header_32 pe32;
f725e3
+  struct grub_pe32_header_64 pe32plus;
f725e3
+} grub_pe_header_t;
f725e3
+
f725e3
+struct pe_coff_loader_image_context
f725e3
+{
f725e3
+  grub_efi_uint64_t image_address;
f725e3
+  grub_efi_uint64_t image_size;
f725e3
+  grub_efi_uint64_t entry_point;
f725e3
+  grub_efi_uintn_t size_of_headers;
f725e3
+  grub_efi_uint16_t image_type;
f725e3
+  grub_efi_uint16_t number_of_sections;
f725e3
+  grub_efi_uint32_t section_alignment;
f725e3
+  struct grub_pe32_section_table *first_section;
f725e3
+  struct grub_pe32_data_directory *reloc_dir;
f725e3
+  struct grub_pe32_data_directory *sec_dir;
f725e3
+  grub_efi_uint64_t number_of_rva_and_sizes;
f725e3
+  grub_pe_header_t *pe_hdr;
f725e3
+};
f725e3
+
f725e3
+typedef struct pe_coff_loader_image_context pe_coff_loader_image_context_t;
f725e3
+
f725e3
+struct grub_efi_shim_lock
f725e3
+{
f725e3
+  grub_efi_status_t (*verify)(void *buffer,
f725e3
+                              grub_efi_uint32_t size);
f725e3
+  grub_efi_status_t (*hash)(void *data,
f725e3
+                            grub_efi_int32_t datasize,
f725e3
+                            pe_coff_loader_image_context_t *context,
f725e3
+                            grub_efi_uint8_t *sha256hash,
f725e3
+                            grub_efi_uint8_t *sha1hash);
f725e3
+  grub_efi_status_t (*context)(void *data,
f725e3
+                               grub_efi_uint32_t size,
f725e3
+                               pe_coff_loader_image_context_t *context);
f725e3
+};
f725e3
+
f725e3
+typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
f725e3
+
f725e3
+static grub_efi_boolean_t
f725e3
+read_header (void *data, grub_efi_uint32_t size,
f725e3
+	     pe_coff_loader_image_context_t *context)
f725e3
+{
f725e3
+  grub_efi_guid_t guid = SHIM_LOCK_GUID;
f725e3
+  grub_efi_shim_lock_t *shim_lock;
f725e3
+  grub_efi_status_t status;
f725e3
+
f725e3
+  shim_lock = grub_efi_locate_protocol (&guid, NULL);
f725e3
+
f725e3
+  if (!shim_lock)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "no shim lock protocol");
f725e3
+      return 0;
f725e3
+    }
f725e3
+
f725e3
+  status = shim_lock->context (data, size, context);
f725e3
+
f725e3
+  if (status == GRUB_EFI_SUCCESS)
f725e3
+    {
f725e3
+      grub_dprintf ("chain", "context success\n");
f725e3
+      return 1;
f725e3
+    }
f725e3
+
f725e3
+  switch (status)
f725e3
+    {
f725e3
+      case GRUB_EFI_UNSUPPORTED:
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error unsupported");
f725e3
+      break;
f725e3
+      case GRUB_EFI_INVALID_PARAMETER:
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error invalid parameter");
f725e3
+      break;
f725e3
+      default:
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error code");
f725e3
+      break;
f725e3
+    }
f725e3
+
f725e3
+  return 0;
f725e3
+}
f725e3
+
f725e3
+static void*
f725e3
+image_address (void *image, grub_efi_uint64_t sz, grub_efi_uint64_t adr)
f725e3
+{
f725e3
+  if (adr > sz)
f725e3
+    return NULL;
f725e3
+
f725e3
+  return ((grub_uint8_t*)image + adr);
f725e3
+}
f725e3
+
f725e3
+static int
f725e3
+image_is_64_bit (grub_pe_header_t *pe_hdr)
f725e3
+{
f725e3
+  /* .Magic is the same offset in all cases */
f725e3
+  if (pe_hdr->pe32plus.optional_header.magic == GRUB_PE32_PE64_MAGIC)
f725e3
+    return 1;
f725e3
+  return 0;
f725e3
+}
f725e3
+
f725e3
+static const grub_uint16_t machine_type =
f725e3
+#if defined(__x86_64__)
f725e3
+  GRUB_PE32_MACHINE_X86_64;
f725e3
+#elif defined(__aarch64__)
f725e3
+  GRUB_PE32_MACHINE_ARM64;
f725e3
+#elif defined(__arm__)
f725e3
+  GRUB_PE32_MACHINE_ARMTHUMB_MIXED;
f725e3
+#elif defined(__i386__) || defined(__i486__) || defined(__i686__)
f725e3
+  GRUB_PE32_MACHINE_I386;
f725e3
+#elif defined(__ia64__)
f725e3
+  GRUB_PE32_MACHINE_IA64;
f725e3
+#else
f725e3
+#error this architecture is not supported by grub2
f725e3
+#endif
f725e3
+
f725e3
+static grub_efi_status_t
f725e3
+relocate_coff (pe_coff_loader_image_context_t *context,
f725e3
+	       struct grub_pe32_section_table *section,
f725e3
+	       void *orig, void *data)
f725e3
+{
f725e3
+  struct grub_pe32_data_directory *reloc_base, *reloc_base_end;
f725e3
+  grub_efi_uint64_t adjust;
f725e3
+  struct grub_pe32_fixup_block *reloc, *reloc_end;
f725e3
+  char *fixup, *fixup_base, *fixup_data = NULL;
f725e3
+  grub_efi_uint16_t *fixup_16;
f725e3
+  grub_efi_uint32_t *fixup_32;
f725e3
+  grub_efi_uint64_t *fixup_64;
f725e3
+  grub_efi_uint64_t size = context->image_size;
f725e3
+  void *image_end = (char *)orig + size;
f725e3
+  int n = 0;
f725e3
+
f725e3
+  if (image_is_64_bit (context->pe_hdr))
f725e3
+    context->pe_hdr->pe32plus.optional_header.image_base =
f725e3
+      (grub_uint64_t)(unsigned long)data;
f725e3
+  else
f725e3
+    context->pe_hdr->pe32.optional_header.image_base =
f725e3
+      (grub_uint32_t)(unsigned long)data;
f725e3
+
f725e3
+  /* Alright, so here's how this works:
f725e3
+   *
f725e3
+   * context->reloc_dir gives us two things:
f725e3
+   * - the VA the table of base relocation blocks are (maybe) to be
f725e3
+   *   mapped at (reloc_dir->rva)
f725e3
+   * - the virtual size (reloc_dir->size)
f725e3
+   *
f725e3
+   * The .reloc section (section here) gives us some other things:
f725e3
+   * - the name! kind of. (section->name)
f725e3
+   * - the virtual size (section->virtual_size), which should be the same
f725e3
+   *   as RelocDir->Size
f725e3
+   * - the virtual address (section->virtual_address)
f725e3
+   * - the file section size (section->raw_data_size), which is
f725e3
+   *   a multiple of optional_header->file_alignment.  Only useful for image
f725e3
+   *   validation, not really useful for iteration bounds.
f725e3
+   * - the file address (section->raw_data_offset)
f725e3
+   * - a bunch of stuff we don't use that's 0 in our binaries usually
f725e3
+   * - Flags (section->characteristics)
f725e3
+   *
f725e3
+   * and then the thing that's actually at the file address is an array
f725e3
+   * of struct grub_pe32_fixup_block structs with some values packed behind
f725e3
+   * them.  The block_size field of this structure includes the
f725e3
+   * structure itself, and adding it to that structure's address will
f725e3
+   * yield the next entry in the array.
f725e3
+   */
f725e3
+
f725e3
+  reloc_base = image_address (orig, size, section->raw_data_offset);
f725e3
+  reloc_base_end = image_address (orig, size, section->raw_data_offset
f725e3
+				  + section->virtual_size - 1);
f725e3
+
f725e3
+  grub_dprintf ("chain", "reloc_base %p reloc_base_end %p\n", reloc_base,
f725e3
+		reloc_base_end);
f725e3
+
f725e3
+  if (!reloc_base && !reloc_base_end)
f725e3
+    return GRUB_EFI_SUCCESS;
f725e3
+
f725e3
+  if (!reloc_base || !reloc_base_end)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc table overflows binary");
f725e3
+      return GRUB_EFI_UNSUPPORTED;
f725e3
+    }
f725e3
+
f725e3
+  adjust = (grub_uint64_t)data - context->image_address;
f725e3
+  if (adjust == 0)
f725e3
+    return GRUB_EFI_SUCCESS;
f725e3
+
f725e3
+  while (reloc_base < reloc_base_end)
f725e3
+    {
f725e3
+      grub_uint16_t *entry;
f725e3
+      reloc = (struct grub_pe32_fixup_block *)((char*)reloc_base);
f725e3
+
f725e3
+      if ((reloc_base->size == 0) ||
f725e3
+	  (reloc_base->size > context->reloc_dir->size))
f725e3
+	{
f725e3
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
f725e3
+		      "Reloc %d block size %d is invalid\n", n,
f725e3
+		      reloc_base->size);
f725e3
+	  return GRUB_EFI_UNSUPPORTED;
f725e3
+	}
f725e3
+
f725e3
+      entry = &reloc->entries[0];
f725e3
+      reloc_end = (struct grub_pe32_fixup_block *)
f725e3
+	((char *)reloc_base + reloc_base->size);
f725e3
+
f725e3
+      if ((void *)reloc_end < data || (void *)reloc_end > image_end)
f725e3
+        {
f725e3
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc entry %d overflows binary",
f725e3
+		      n);
f725e3
+          return GRUB_EFI_UNSUPPORTED;
f725e3
+        }
f725e3
+
f725e3
+      fixup_base = image_address(data, size, reloc_base->rva);
f725e3
+
f725e3
+      if (!fixup_base)
f725e3
+        {
f725e3
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc %d Invalid fixupbase", n);
f725e3
+          return GRUB_EFI_UNSUPPORTED;
f725e3
+        }
f725e3
+
f725e3
+      while ((void *)entry < (void *)reloc_end)
f725e3
+        {
f725e3
+          fixup = fixup_base + (*entry & 0xFFF);
f725e3
+          switch ((*entry) >> 12)
f725e3
+            {
f725e3
+              case GRUB_PE32_REL_BASED_ABSOLUTE:
f725e3
+                break;
f725e3
+              case GRUB_PE32_REL_BASED_HIGH:
f725e3
+                fixup_16 = (grub_uint16_t *)fixup;
f725e3
+                *fixup_16 = (grub_uint16_t)
f725e3
+		  (*fixup_16 + ((grub_uint16_t)((grub_uint32_t)adjust >> 16)));
f725e3
+                if (fixup_data != NULL)
f725e3
+                  {
f725e3
+                    *(grub_uint16_t *) fixup_data = *fixup_16;
f725e3
+                    fixup_data = fixup_data + sizeof (grub_uint16_t);
f725e3
+                  }
f725e3
+                break;
f725e3
+              case GRUB_PE32_REL_BASED_LOW:
f725e3
+                fixup_16 = (grub_uint16_t *)fixup;
f725e3
+                *fixup_16 = (grub_uint16_t) (*fixup_16 + (grub_uint16_t)adjust);
f725e3
+                if (fixup_data != NULL)
f725e3
+                  {
f725e3
+                    *(grub_uint16_t *) fixup_data = *fixup_16;
f725e3
+                    fixup_data = fixup_data + sizeof (grub_uint16_t);
f725e3
+                  }
f725e3
+                break;
f725e3
+              case GRUB_PE32_REL_BASED_HIGHLOW:
f725e3
+                fixup_32 = (grub_uint32_t *)fixup;
f725e3
+                *fixup_32 = *fixup_32 + (grub_uint32_t)adjust;
f725e3
+                if (fixup_data != NULL)
f725e3
+                  {
f725e3
+                    fixup_data = (char *)ALIGN_UP ((grub_addr_t)fixup_data, sizeof (grub_uint32_t));
f725e3
+                    *(grub_uint32_t *) fixup_data = *fixup_32;
f725e3
+                    fixup_data += sizeof (grub_uint32_t);
f725e3
+                  }
f725e3
+                break;
f725e3
+              case GRUB_PE32_REL_BASED_DIR64:
f725e3
+                fixup_64 = (grub_uint64_t *)fixup;
f725e3
+                *fixup_64 = *fixup_64 + (grub_uint64_t)adjust;
f725e3
+                if (fixup_data != NULL)
f725e3
+                  {
f725e3
+                    fixup_data = (char *)ALIGN_UP ((grub_addr_t)fixup_data, sizeof (grub_uint64_t));
f725e3
+                    *(grub_uint64_t *) fixup_data = *fixup_64;
f725e3
+                    fixup_data += sizeof (grub_uint64_t);
f725e3
+                  }
f725e3
+                break;
f725e3
+              default:
f725e3
+                grub_error (GRUB_ERR_BAD_ARGUMENT,
f725e3
+			    "Reloc %d unknown relocation type %d",
f725e3
+			    n, (*entry) >> 12);
f725e3
+                return GRUB_EFI_UNSUPPORTED;
f725e3
+            }
f725e3
+          entry += 1;
f725e3
+        }
f725e3
+      reloc_base = (struct grub_pe32_data_directory *)reloc_end;
f725e3
+      n++;
f725e3
+    }
f725e3
+
f725e3
+  return GRUB_EFI_SUCCESS;
f725e3
+}
f725e3
+
f725e3
+static grub_efi_device_path_t *
f725e3
+grub_efi_get_media_file_path (grub_efi_device_path_t *dp)
f725e3
+{
f725e3
+  while (1)
f725e3
+    {
f725e3
+      grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
f725e3
+      grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
f725e3
+
f725e3
+      if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
f725e3
+        break;
f725e3
+      else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
f725e3
+            && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
f725e3
+      return dp;
f725e3
+
f725e3
+      dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
f725e3
+    }
f725e3
+
f725e3
+    return NULL;
f725e3
+}
f725e3
+
f725e3
+static grub_efi_boolean_t
f725e3
+handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
+{
f725e3
+  grub_efi_boot_services_t *b;
f725e3
+  grub_efi_loaded_image_t *li, li_bak;
f725e3
+  grub_efi_status_t efi_status;
f725e3
+  char *buffer = NULL;
f725e3
+  char *buffer_aligned = NULL;
f725e3
+  grub_efi_uint32_t i, size;
f725e3
+  struct grub_pe32_section_table *section;
f725e3
+  char *base, *end;
f725e3
+  pe_coff_loader_image_context_t context;
f725e3
+  grub_uint32_t section_alignment;
f725e3
+  grub_uint32_t buffer_size;
f725e3
+
f725e3
+  b = grub_efi_system_table->boot_services;
f725e3
+
f725e3
+  if (read_header (data, datasize, &context))
f725e3
+    {
f725e3
+      grub_dprintf ("chain", "Succeed to read header\n");
f725e3
+    }
f725e3
+  else
f725e3
+    {
f725e3
+      grub_dprintf ("chain", "Failed to read header\n");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  section_alignment = context.section_alignment;
f725e3
+  buffer_size = context.image_size + section_alignment;
f725e3
+
f725e3
+  efi_status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA,
f725e3
+			   buffer_size, &buffer);
f725e3
+
f725e3
+  if (efi_status != GRUB_EFI_SUCCESS)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  buffer_aligned = (char *)ALIGN_UP ((grub_addr_t)buffer, section_alignment);
f725e3
+
f725e3
+  if (!buffer_aligned)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  grub_memcpy (buffer_aligned, data, context.size_of_headers);
f725e3
+
f725e3
+  char *reloc_base, *reloc_base_end;
f725e3
+  reloc_base = image_address (buffer_aligned, datasize,
f725e3
+			      context.reloc_dir->rva);
f725e3
+  /* RelocBaseEnd here is the address of the last byte of the table */
f725e3
+  reloc_base_end = image_address (buffer_aligned, datasize,
f725e3
+				  context.reloc_dir->rva
f725e3
+				  + context.reloc_dir->size - 1);
f725e3
+  struct grub_pe32_section_table *reloc_section = NULL;
f725e3
+
f725e3
+  section = context.first_section;
f725e3
+  for (i = 0; i < context.number_of_sections; i++, section++)
f725e3
+    {
f725e3
+      size = section->virtual_size;
f725e3
+      if (size > section->raw_data_size)
f725e3
+        size = section->raw_data_size;
f725e3
+
f725e3
+      base = image_address (buffer_aligned, context.image_size,
f725e3
+			    section->virtual_address);
f725e3
+      end = image_address (buffer_aligned, context.image_size,
f725e3
+			   section->virtual_address + size - 1);
f725e3
+
f725e3
+
f725e3
+      /* We do want to process .reloc, but it's often marked
f725e3
+       * discardable, so we don't want to memcpy it. */
f725e3
+      if (grub_memcmp (section->name, ".reloc\0\0", 8) == 0)
f725e3
+	{
f725e3
+	  if (reloc_section)
f725e3
+	    {
f725e3
+	      grub_error (GRUB_ERR_BAD_ARGUMENT,
f725e3
+			  "Image has multiple relocation sections");
f725e3
+	      goto error_exit;
f725e3
+	    }
f725e3
+
f725e3
+	  /* If it has nonzero sizes, and our bounds check
f725e3
+	   * made sense, and the VA and size match RelocDir's
f725e3
+	   * versions, then we believe in this section table. */
f725e3
+	  if (section->raw_data_size && section->virtual_size &&
f725e3
+	      base && end && reloc_base == base && reloc_base_end == end)
f725e3
+	    {
f725e3
+	      reloc_section = section;
f725e3
+	    }
f725e3
+	}
f725e3
+
f725e3
+      if (section->characteristics && GRUB_PE32_SCN_MEM_DISCARDABLE)
f725e3
+	continue;
f725e3
+
f725e3
+      if (!base || !end)
f725e3
+        {
f725e3
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid section size");
f725e3
+          goto error_exit;
f725e3
+        }
f725e3
+
f725e3
+      if (section->virtual_address < context.size_of_headers ||
f725e3
+	  section->raw_data_offset < context.size_of_headers)
f725e3
+	{
f725e3
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
f725e3
+		      "Section %d is inside image headers", i);
f725e3
+	  goto error_exit;
f725e3
+	}
f725e3
+
f725e3
+      if (section->raw_data_size > 0)
f725e3
+        grub_memcpy (base, (grub_efi_uint8_t*)data + section->raw_data_offset,
f725e3
+		     size);
f725e3
+
f725e3
+      if (size < section->virtual_size)
f725e3
+        grub_memset (base + size, 0, section->virtual_size - size);
f725e3
+
f725e3
+      grub_dprintf ("chain", "copied section %s\n", section->name);
f725e3
+    }
f725e3
+
f725e3
+  /* 5 == EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC */
f725e3
+  if (context.number_of_rva_and_sizes <= 5)
f725e3
+    {
f725e3
+      grub_dprintf ("chain", "image has no relocation entry\n");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  if (context.reloc_dir->size && reloc_section)
f725e3
+    {
f725e3
+      /* run the relocation fixups */
f725e3
+      efi_status = relocate_coff (&context, reloc_section, data,
f725e3
+				  buffer_aligned);
f725e3
+
f725e3
+      if (efi_status != GRUB_EFI_SUCCESS)
f725e3
+	{
f725e3
+	  grub_error (GRUB_ERR_BAD_ARGUMENT, "relocation failed");
f725e3
+	  goto error_exit;
f725e3
+	}
f725e3
+    }
f725e3
+
f725e3
+  entry_point = image_address (buffer_aligned, context.image_size,
f725e3
+			       context.entry_point);
f725e3
+
f725e3
+  if (!entry_point)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid entry point");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  li = grub_efi_get_loaded_image (grub_efi_image_handle);
f725e3
+  if (!li)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "no loaded image available");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  grub_memcpy (&li_bak, li, sizeof (grub_efi_loaded_image_t));
f725e3
+  li->image_base = buffer_aligned;
f725e3
+  li->image_size = context.image_size;
f725e3
+  li->load_options = cmdline;
f725e3
+  li->load_options_size = cmdline_len;
f725e3
+  li->file_path = grub_efi_get_media_file_path (file_path);
f725e3
+  li->device_handle = dev_handle;
f725e3
+  if (li->file_path)
f725e3
+    {
f725e3
+      grub_printf ("file path: ");
f725e3
+      grub_efi_print_device_path (li->file_path);
f725e3
+    }
f725e3
+  else
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching file path found");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
+  efi_status = efi_call_2 (entry_point, grub_efi_image_handle,
f725e3
+			   grub_efi_system_table);
f725e3
+
f725e3
+  grub_memcpy (li, &li_bak, sizeof (grub_efi_loaded_image_t));
f725e3
+  efi_status = efi_call_1 (b->free_pool, buffer);
f725e3
+
f725e3
+  return 1;
f725e3
+
f725e3
+error_exit:
f725e3
+  if (buffer)
f725e3
+      efi_call_1 (b->free_pool, buffer);
f725e3
+
f725e3
+  return 0;
f725e3
+}
f725e3
+
f725e3
+static grub_err_t
f725e3
+grub_secureboot_chainloader_unload (void)
f725e3
+{
f725e3
+  grub_efi_boot_services_t *b;
f725e3
+
f725e3
+  b = grub_efi_system_table->boot_services;
f725e3
+  efi_call_2 (b->free_pages, address, pages);
f725e3
+  grub_free (file_path);
f725e3
+  grub_free (cmdline);
f725e3
+  cmdline = 0;
f725e3
+  file_path = 0;
f725e3
+  dev_handle = 0;
f725e3
+
f725e3
+  grub_dl_unref (my_mod);
f725e3
+  return GRUB_ERR_NONE;
f725e3
+}
f725e3
+
f725e3
+static grub_err_t
f725e3
+grub_secureboot_chainloader_boot (void)
f725e3
+{
f725e3
+  handle_image ((void *)address, fsize);
f725e3
+  grub_loader_unset ();
f725e3
+  return grub_errno;
f725e3
+}
f725e3
+
f725e3
 static grub_err_t
f725e3
 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
 		      int argc, char *argv[])
f725e3
 {
f725e3
   grub_file_t file = 0;
f725e3
-  grub_ssize_t size;
f725e3
   grub_efi_status_t status;
f725e3
   grub_efi_boot_services_t *b;
f725e3
   grub_device_t dev = 0;
f725e3
@@ -204,7 +723,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
   grub_efi_loaded_image_t *loaded_image;
f725e3
   char *filename;
f725e3
   void *boot_image = 0;
f725e3
-  grub_efi_handle_t dev_handle = 0;
f725e3
 
f725e3
   if (argc == 0)
f725e3
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
f725e3
@@ -216,9 +734,36 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
   address = 0;
f725e3
   image_handle = 0;
f725e3
   file_path = 0;
f725e3
+  dev_handle = 0;
f725e3
 
f725e3
   b = grub_efi_system_table->boot_services;
f725e3
 
f725e3
+  if (argc > 1)
f725e3
+    {
f725e3
+      int i;
f725e3
+      grub_efi_char16_t *p16;
f725e3
+
f725e3
+      for (i = 1, cmdline_len = 0; i < argc; i++)
f725e3
+        cmdline_len += grub_strlen (argv[i]) + 1;
f725e3
+
f725e3
+      cmdline_len *= sizeof (grub_efi_char16_t);
f725e3
+      cmdline = p16 = grub_malloc (cmdline_len);
f725e3
+      if (! cmdline)
f725e3
+        goto fail;
f725e3
+
f725e3
+      for (i = 1; i < argc; i++)
f725e3
+        {
f725e3
+          char *p8;
f725e3
+
f725e3
+          p8 = argv[i];
f725e3
+          while (*p8)
f725e3
+            *(p16++) = *(p8++);
f725e3
+
f725e3
+          *(p16++) = ' ';
f725e3
+        }
f725e3
+      *(--p16) = 0;
f725e3
+    }
f725e3
+
f725e3
   file = grub_file_open (filename);
f725e3
   if (! file)
f725e3
     goto fail;
f725e3
@@ -267,14 +812,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
   grub_printf ("file path: ");
f725e3
   grub_efi_print_device_path (file_path);
f725e3
 
f725e3
-  size = grub_file_size (file);
f725e3
-  if (!size)
f725e3
+  fsize = grub_file_size (file);
f725e3
+  if (!fsize)
f725e3
     {
f725e3
       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
f725e3
 		  filename);
f725e3
       goto fail;
f725e3
     }
f725e3
-  pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
f725e3
+  pages = (((grub_efi_uintn_t) fsize + ((1 << 12) - 1)) >> 12);
f725e3
 
f725e3
   status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
f725e3
 			      GRUB_EFI_LOADER_CODE,
f725e3
@@ -288,7 +833,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
     }
f725e3
 
f725e3
   boot_image = (void *) ((grub_addr_t) address);
f725e3
-  if (grub_file_read (file, boot_image, size) != size)
f725e3
+  if (grub_file_read (file, boot_image, fsize) != fsize)
f725e3
     {
f725e3
       if (grub_errno == GRUB_ERR_NONE)
f725e3
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
f725e3
@@ -298,7 +843,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
     }
f725e3
 
f725e3
 #if defined (__i386__) || defined (__x86_64__)
f725e3
-  if (size >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
f725e3
+  if (fsize >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
f725e3
     {
f725e3
       struct grub_macho_fat_header *head = boot_image;
f725e3
       if (head->magic
f725e3
@@ -307,6 +852,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
 	  grub_uint32_t i;
f725e3
 	  struct grub_macho_fat_arch *archs
f725e3
 	    = (struct grub_macho_fat_arch *) (head + 1);
f725e3
+
f725e3
+	  if (grub_efi_secure_boot())
f725e3
+	    {
f725e3
+	      grub_error (GRUB_ERR_BAD_OS,
f725e3
+			  "MACHO binaries are forbidden with Secure Boot");
f725e3
+	      goto fail;
f725e3
+	    }
f725e3
+
f725e3
 	  for (i = 0; i < grub_cpu_to_le32 (head->nfat_arch); i++)
f725e3
 	    {
f725e3
 	      if (GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT (archs[i].cputype))
f725e3
@@ -321,21 +874,28 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
 	      > ~grub_cpu_to_le32 (archs[i].size)
f725e3
 	      || grub_cpu_to_le32 (archs[i].offset)
f725e3
 	      + grub_cpu_to_le32 (archs[i].size)
f725e3
-	      > (grub_size_t) size)
f725e3
+	      > (grub_size_t) fsize)
f725e3
 	    {
f725e3
 	      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
f725e3
 			  filename);
f725e3
 	      goto fail;
f725e3
 	    }
f725e3
 	  boot_image = (char *) boot_image + grub_cpu_to_le32 (archs[i].offset);
f725e3
-	  size = grub_cpu_to_le32 (archs[i].size);
f725e3
+	  fsize = grub_cpu_to_le32 (archs[i].size);
f725e3
 	}
f725e3
     }
f725e3
 #endif
f725e3
 
f725e3
+  if (grub_linuxefi_secure_validate((void *)address, fsize))
f725e3
+    {
f725e3
+      grub_file_close (file);
f725e3
+      grub_loader_set (grub_secureboot_chainloader_boot,
f725e3
+		       grub_secureboot_chainloader_unload, 0);
f725e3
+      return 0;
f725e3
+    }
f725e3
+
f725e3
   status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
f725e3
-		       boot_image, size,
f725e3
-		       &image_handle);
f725e3
+		       boot_image, fsize, &image_handle);
f725e3
   if (status != GRUB_EFI_SUCCESS)
f725e3
     {
f725e3
       if (status == GRUB_EFI_OUT_OF_RESOURCES)
f725e3
@@ -357,33 +917,10 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
     }
f725e3
   loaded_image->device_handle = dev_handle;
f725e3
 
f725e3
-  if (argc > 1)
f725e3
+  if (cmdline)
f725e3
     {
f725e3
-      int i, len;
f725e3
-      grub_efi_char16_t *p16;
f725e3
-
f725e3
-      for (i = 1, len = 0; i < argc; i++)
f725e3
-        len += grub_strlen (argv[i]) + 1;
f725e3
-
f725e3
-      len *= sizeof (grub_efi_char16_t);
f725e3
-      cmdline = p16 = grub_malloc (len);
f725e3
-      if (! cmdline)
f725e3
-        goto fail;
f725e3
-
f725e3
-      for (i = 1; i < argc; i++)
f725e3
-        {
f725e3
-          char *p8;
f725e3
-
f725e3
-          p8 = argv[i];
f725e3
-          while (*p8)
f725e3
-            *(p16++) = *(p8++);
f725e3
-
f725e3
-          *(p16++) = ' ';
f725e3
-        }
f725e3
-      *(--p16) = 0;
f725e3
-
f725e3
       loaded_image->load_options = cmdline;
f725e3
-      loaded_image->load_options_size = len;
f725e3
+      loaded_image->load_options_size = cmdline_len;
f725e3
     }
f725e3
 
f725e3
   grub_file_close (file);
f725e3
@@ -405,6 +942,9 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
f725e3
   if (address)
f725e3
     efi_call_2 (b->free_pages, address, pages);
f725e3
 
f725e3
+  if (cmdline)
f725e3
+    grub_free (cmdline);
f725e3
+
f725e3
   grub_dl_unref (my_mod);
f725e3
 
f725e3
   return grub_errno;
f725e3
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
f725e3
index f79c36c026e..f79782e1bde 100644
f725e3
--- a/include/grub/efi/pe32.h
f725e3
+++ b/include/grub/efi/pe32.h
f725e3
@@ -212,7 +212,11 @@ struct grub_pe64_optional_header
f725e3
 struct grub_pe32_section_table
f725e3
 {
f725e3
   char name[8];
f725e3
-  grub_uint32_t virtual_size;
f725e3
+  union
f725e3
+    {
f725e3
+      grub_uint32_t physical_address;
f725e3
+      grub_uint32_t virtual_size;
f725e3
+    };
f725e3
   grub_uint32_t virtual_address;
f725e3
   grub_uint32_t raw_data_size;
f725e3
   grub_uint32_t raw_data_offset;
f725e3
@@ -263,6 +267,20 @@ struct grub_pe32_header
f725e3
 #endif
f725e3
 };
f725e3
 
f725e3
+struct grub_pe32_header_32
f725e3
+{
f725e3
+  char signature[GRUB_PE32_SIGNATURE_SIZE];
f725e3
+  struct grub_pe32_coff_header coff_header;
f725e3
+  struct grub_pe32_optional_header optional_header;
f725e3
+};
f725e3
+
f725e3
+struct grub_pe32_header_64
f725e3
+{
f725e3
+  char signature[GRUB_PE32_SIGNATURE_SIZE];
f725e3
+  struct grub_pe32_coff_header coff_header;
f725e3
+  struct grub_pe64_optional_header optional_header;
f725e3
+};
f725e3
+
f725e3
 struct grub_pe32_fixup_block
f725e3
 {
f725e3
   grub_uint32_t page_rva;