f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Peter Jones <pjones@redhat.com>
f725e3
Date: Thu, 9 Jun 2016 12:22:29 -0400
f725e3
Subject: [PATCH] Re-work some intricacies of PE loading.
f725e3
f725e3
The PE spec is not a well written document, and awesomely every place
f725e3
where there's an ambiguous way to read something, Windows' bootmgfw.efi
f725e3
takes a different read than either of them.
f725e3
---
f725e3
 grub-core/loader/efi/chainloader.c | 156 +++++++++++++++++++++++++++++--------
f725e3
 include/grub/efi/pe32.h            |  32 +++++++-
f725e3
 2 files changed, 152 insertions(+), 36 deletions(-)
f725e3
f725e3
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
f725e3
index aee8e6becf6..4b77a7d5adb 100644
f725e3
--- a/grub-core/loader/efi/chainloader.c
f725e3
+++ b/grub-core/loader/efi/chainloader.c
f725e3
@@ -301,7 +301,7 @@ image_is_64_bit (grub_pe_header_t *pe_hdr)
f725e3
   return 0;
f725e3
 }
f725e3
 
f725e3
-static const grub_uint16_t machine_type =
f725e3
+static const grub_uint16_t machine_type __attribute__((__unused__)) =
f725e3
 #if defined(__x86_64__)
f725e3
   GRUB_PE32_MACHINE_X86_64;
f725e3
 #elif defined(__aarch64__)
f725e3
@@ -367,10 +367,10 @@ relocate_coff (pe_coff_loader_image_context_t *context,
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
+				  + section->virtual_size);
f725e3
 
f725e3
-  grub_dprintf ("chain", "reloc_base %p reloc_base_end %p\n", reloc_base,
f725e3
-		reloc_base_end);
f725e3
+  grub_dprintf ("chain", "relocate_coff(): reloc_base %p reloc_base_end %p\n",
f725e3
+		reloc_base, reloc_base_end);
f725e3
 
f725e3
   if (!reloc_base && !reloc_base_end)
f725e3
     return GRUB_EFI_SUCCESS;
f725e3
@@ -507,12 +507,13 @@ handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
   grub_efi_status_t efi_status;
f725e3
   char *buffer = NULL;
f725e3
   char *buffer_aligned = NULL;
f725e3
-  grub_efi_uint32_t i, size;
f725e3
+  grub_efi_uint32_t i;
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
+  int found_entry_point = 0;
f725e3
 
f725e3
   b = grub_efi_system_table->boot_services;
f725e3
 
f725e3
@@ -526,8 +527,28 @@ handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
       goto error_exit;
f725e3
     }
f725e3
 
f725e3
+  /*
f725e3
+   * The spec says, uselessly, of SectionAlignment:
f725e3
+   * =====
f725e3
+   * The alignment (in bytes) of sections when they are loaded into
f725e3
+   * memory. It must be greater than or equal to FileAlignment. The
f725e3
+   * default is the page size for the architecture.
f725e3
+   * =====
f725e3
+   * Which doesn't tell you whose responsibility it is to enforce the
f725e3
+   * "default", or when.  It implies that the value in the field must
f725e3
+   * be > FileAlignment (also poorly defined), but it appears visual
f725e3
+   * studio will happily write 512 for FileAlignment (its default) and
f725e3
+   * 0 for SectionAlignment, intending to imply PAGE_SIZE.
f725e3
+   *
f725e3
+   * We only support one page size, so if it's zero, nerf it to 4096.
f725e3
+   */
f725e3
   section_alignment = context.section_alignment;
f725e3
+  if (section_alignment == 0)
f725e3
+    section_alignment = 4096;
f725e3
+
f725e3
   buffer_size = context.image_size + section_alignment;
f725e3
+  grub_dprintf ("chain", "image size is %08lx, datasize is %08x\n",
f725e3
+	       context.image_size, datasize);
f725e3
 
f725e3
   efi_status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA,
f725e3
 			   buffer_size, &buffer);
f725e3
@@ -539,7 +560,6 @@ handle_image (void *data, grub_efi_uint32_t datasize)
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
@@ -548,27 +568,62 @@ handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
 
f725e3
   grub_memcpy (buffer_aligned, data, context.size_of_headers);
f725e3
 
f725e3
+  entry_point = image_address (buffer_aligned, context.image_size,
f725e3
+			       context.entry_point);
f725e3
+
f725e3
+  grub_dprintf ("chain", "entry_point: %p\n", entry_point);
f725e3
+  if (!entry_point)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid entry point");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+
f725e3
   char *reloc_base, *reloc_base_end;
f725e3
-  reloc_base = image_address (buffer_aligned, datasize,
f725e3
+  grub_dprintf ("chain", "reloc_dir: %p reloc_size: 0x%08x\n",
f725e3
+		(void *)(unsigned long long)context.reloc_dir->rva,
f725e3
+		context.reloc_dir->size);
f725e3
+  reloc_base = image_address (buffer_aligned, context.image_size,
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
+  reloc_base_end = image_address (buffer_aligned, context.image_size,
f725e3
 				  context.reloc_dir->rva
f725e3
 				  + context.reloc_dir->size - 1);
f725e3
+  grub_dprintf ("chain", "reloc_base: %p reloc_base_end: %p\n",
f725e3
+		reloc_base, reloc_base_end);
f725e3
+
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
+      char name[9];
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
+			   section->virtual_address + section->virtual_size -1);
f725e3
 
f725e3
+      grub_strncpy(name, section->name, 9);
f725e3
+      name[8] = '\0';
f725e3
+      grub_dprintf ("chain", "Section %d \"%s\" at %p..%p\n", i,
f725e3
+		   name, base, end);
f725e3
+
f725e3
+      if (end < base)
f725e3
+	{
f725e3
+	  grub_dprintf ("chain", " base is %p but end is %p... bad.\n",
f725e3
+		       base, end);
f725e3
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
f725e3
+		      "Image has invalid negative size");
f725e3
+	  goto error_exit;
f725e3
+	}
f725e3
+
f725e3
+      if (section->virtual_address <= context.entry_point &&
f725e3
+	  (section->virtual_address + section->raw_data_size - 1)
f725e3
+	  > context.entry_point)
f725e3
+	{
f725e3
+	  found_entry_point++;
f725e3
+	  grub_dprintf ("chain", " section contains entry point\n");
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
@@ -587,21 +642,46 @@ handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
 	  if (section->raw_data_size && section->virtual_size &&
f725e3
 	      base && end && reloc_base == base && reloc_base_end == end)
f725e3
 	    {
f725e3
+	      grub_dprintf ("chain", " section is relocation section\n");
f725e3
 	      reloc_section = section;
f725e3
 	    }
f725e3
+	  else
f725e3
+	    {
f725e3
+	      grub_dprintf ("chain", " section is not reloc section?\n");
f725e3
+	      grub_dprintf ("chain", " rds: 0x%08x, vs: %08x\n",
f725e3
+			    section->raw_data_size, section->virtual_size);
f725e3
+	      grub_dprintf ("chain", " base: %p end: %p\n", base, end);
f725e3
+	      grub_dprintf ("chain", " reloc_base: %p reloc_base_end: %p\n",
f725e3
+			    reloc_base, reloc_base_end);
f725e3
+	    }
f725e3
 	}
f725e3
 
f725e3
-      if (section->characteristics && GRUB_PE32_SCN_MEM_DISCARDABLE)
f725e3
-	continue;
f725e3
+      grub_dprintf ("chain", " Section characteristics are %08x\n",
f725e3
+		   section->characteristics);
f725e3
+      grub_dprintf ("chain", " Section virtual size: %08x\n",
f725e3
+		   section->virtual_size);
f725e3
+      grub_dprintf ("chain", " Section raw_data size: %08x\n",
f725e3
+		   section->raw_data_size);
f725e3
+      if (section->characteristics & GRUB_PE32_SCN_MEM_DISCARDABLE)
f725e3
+	{
f725e3
+	  grub_dprintf ("chain", " Discarding section\n");
f725e3
+	  continue;
f725e3
+	}
f725e3
 
f725e3
       if (!base || !end)
f725e3
         {
f725e3
+	  grub_dprintf ("chain", " section is invalid\n");
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
+      if (section->characteristics & GRUB_PE32_SCN_CNT_UNINITIALIZED_DATA)
f725e3
+	{
f725e3
+	  if (section->raw_data_size != 0)
f725e3
+	    grub_dprintf ("chain", " UNINITIALIZED_DATA section has data?\n");
f725e3
+	}
f725e3
+      else 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
@@ -609,13 +689,24 @@ handle_image (void *data, grub_efi_uint32_t datasize)
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
+	  grub_dprintf ("chain", " copying 0x%08x bytes to %p\n",
f725e3
+			section->raw_data_size, base);
f725e3
+	  grub_memcpy (base,
f725e3
+		       (grub_efi_uint8_t*)data + section->raw_data_offset,
f725e3
+		       section->raw_data_size);
f725e3
+	}
f725e3
 
f725e3
-      if (size < section->virtual_size)
f725e3
-        grub_memset (base + size, 0, section->virtual_size - size);
f725e3
+      if (section->raw_data_size < section->virtual_size)
f725e3
+	{
f725e3
+	  grub_dprintf ("chain", " padding with 0x%08x bytes at %p\n",
f725e3
+			section->virtual_size - section->raw_data_size,
f725e3
+			base + section->raw_data_size);
f725e3
+	  grub_memset (base + section->raw_data_size, 0,
f725e3
+		       section->virtual_size - section->raw_data_size);
f725e3
+	}
f725e3
 
f725e3
-      grub_dprintf ("chain", "copied section %s\n", section->name);
f725e3
+      grub_dprintf ("chain", " finished section %s\n", name);
f725e3
     }
f725e3
 
f725e3
   /* 5 == EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC */
f725e3
@@ -638,12 +729,15 @@ handle_image (void *data, grub_efi_uint32_t datasize)
f725e3
 	}
f725e3
     }
f725e3
 
f725e3
-  entry_point = image_address (buffer_aligned, context.image_size,
f725e3
-			       context.entry_point);
f725e3
-
f725e3
-  if (!entry_point)
f725e3
+  if (!found_entry_point)
f725e3
     {
f725e3
-      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid entry point");
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "entry point is not within sections");
f725e3
+      goto error_exit;
f725e3
+    }
f725e3
+  if (found_entry_point > 1)
f725e3
+    {
f725e3
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "%d sections contain entry point",
f725e3
+		  found_entry_point);
f725e3
       goto error_exit;
f725e3
     }
f725e3
 
f725e3
@@ -661,26 +755,24 @@ handle_image (void *data, grub_efi_uint32_t datasize)
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
+  if (!li->file_path)
f725e3
     {
f725e3
       grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching file path found");
f725e3
       goto error_exit;
f725e3
     }
f725e3
 
f725e3
+  grub_dprintf ("chain", "booting via entry point\n");
f725e3
   efi_status = efi_call_2 (entry_point, grub_efi_image_handle,
f725e3
 			   grub_efi_system_table);
f725e3
 
f725e3
+  grub_dprintf ("chain", "entry_point returned %ld\n", efi_status);
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
+  grub_dprintf ("chain", "error_exit: grub_errno: %d\n", grub_errno);
f725e3
   if (buffer)
f725e3
       efi_call_1 (b->free_pool, buffer);
f725e3
 
f725e3
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
f725e3
index f79782e1bde..8396bde4917 100644
f725e3
--- a/include/grub/efi/pe32.h
f725e3
+++ b/include/grub/efi/pe32.h
f725e3
@@ -227,12 +227,18 @@ struct grub_pe32_section_table
f725e3
   grub_uint32_t characteristics;
f725e3
 };
f725e3
 
f725e3
+#define GRUB_PE32_SCN_TYPE_NO_PAD		0x00000008
f725e3
 #define GRUB_PE32_SCN_CNT_CODE			0x00000020
f725e3
 #define GRUB_PE32_SCN_CNT_INITIALIZED_DATA	0x00000040
f725e3
-#define GRUB_PE32_SCN_MEM_DISCARDABLE		0x02000000
f725e3
-#define GRUB_PE32_SCN_MEM_EXECUTE		0x20000000
f725e3
-#define GRUB_PE32_SCN_MEM_READ			0x40000000
f725e3
-#define GRUB_PE32_SCN_MEM_WRITE			0x80000000
f725e3
+#define GRUB_PE32_SCN_CNT_UNINITIALIZED_DATA	0x00000080
f725e3
+#define GRUB_PE32_SCN_LNK_OTHER			0x00000100
f725e3
+#define GRUB_PE32_SCN_LNK_INFO			0x00000200
f725e3
+#define GRUB_PE32_SCN_LNK_REMOVE		0x00000800
f725e3
+#define GRUB_PE32_SCN_LNK_COMDAT		0x00001000
f725e3
+#define GRUB_PE32_SCN_GPREL			0x00008000
f725e3
+#define GRUB_PE32_SCN_MEM_16BIT			0x00020000
f725e3
+#define GRUB_PE32_SCN_MEM_LOCKED		0x00040000
f725e3
+#define GRUB_PE32_SCN_MEM_PRELOAD		0x00080000
f725e3
 
f725e3
 #define GRUB_PE32_SCN_ALIGN_1BYTES		0x00100000
f725e3
 #define GRUB_PE32_SCN_ALIGN_2BYTES		0x00200000
f725e3
@@ -241,10 +247,28 @@ struct grub_pe32_section_table
f725e3
 #define GRUB_PE32_SCN_ALIGN_16BYTES		0x00500000
f725e3
 #define GRUB_PE32_SCN_ALIGN_32BYTES		0x00600000
f725e3
 #define GRUB_PE32_SCN_ALIGN_64BYTES		0x00700000
f725e3
+#define GRUB_PE32_SCN_ALIGN_128BYTES		0x00800000
f725e3
+#define GRUB_PE32_SCN_ALIGN_256BYTES		0x00900000
f725e3
+#define GRUB_PE32_SCN_ALIGN_512BYTES		0x00A00000
f725e3
+#define GRUB_PE32_SCN_ALIGN_1024BYTES		0x00B00000
f725e3
+#define GRUB_PE32_SCN_ALIGN_2048BYTES		0x00C00000
f725e3
+#define GRUB_PE32_SCN_ALIGN_4096BYTES		0x00D00000
f725e3
+#define GRUB_PE32_SCN_ALIGN_8192BYTES		0x00E00000
f725e3
 
f725e3
 #define GRUB_PE32_SCN_ALIGN_SHIFT		20
f725e3
 #define GRUB_PE32_SCN_ALIGN_MASK		7
f725e3
 
f725e3
+#define GRUB_PE32_SCN_LNK_NRELOC_OVFL		0x01000000
f725e3
+#define GRUB_PE32_SCN_MEM_DISCARDABLE		0x02000000
f725e3
+#define GRUB_PE32_SCN_MEM_NOT_CACHED		0x04000000
f725e3
+#define GRUB_PE32_SCN_MEM_NOT_PAGED		0x08000000
f725e3
+#define GRUB_PE32_SCN_MEM_SHARED		0x10000000
f725e3
+#define GRUB_PE32_SCN_MEM_EXECUTE		0x20000000
f725e3
+#define GRUB_PE32_SCN_MEM_READ			0x40000000
f725e3
+#define GRUB_PE32_SCN_MEM_WRITE			0x80000000
f725e3
+
f725e3
+
f725e3
+
f725e3
 #define GRUB_PE32_SIGNATURE_SIZE 4
f725e3
 
f725e3
 struct grub_pe32_header