Blame SOURCES/0224-Re-work-some-intricacies-of-PE-loading.patch

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