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

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