3d407d2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3d407d2
From: Peter Jones <pjones@redhat.com>
3d407d2
Date: Fri, 25 Mar 2022 15:40:12 -0400
3d407d2
Subject: [PATCH] pe: add the DOS header struct and fix some bad naming.
3d407d2
3d407d2
In order to properly validate a loaded kernel's support for being loaded
3d407d2
without a writable stack or executable, we need to be able to properly
3d407d2
parse arbitrary PE headers.
3d407d2
3d407d2
Currently, pe32.h is written in such a way that the MS-DOS header that
3d407d2
tells us where to find the PE header in the binary can't be accessed.
3d407d2
Further, for some reason it calls the DOS MZ magic "GRUB_PE32_MAGIC".
3d407d2
3d407d2
This patch adds the structure for the DOS header, renames the DOS magic
3d407d2
define, and adds defines for the actual PE magic.
3d407d2
3d407d2
Signed-off-by: Peter Jones <pjones@redhat.com>
3d407d2
---
3d407d2
 grub-core/loader/arm64/linux.c |  2 +-
3d407d2
 include/grub/efi/pe32.h        | 28 ++++++++++++++++++++++++++--
3d407d2
 2 files changed, 27 insertions(+), 3 deletions(-)
3d407d2
3d407d2
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
3d407d2
index d2af47c2c0..cc67f43906 100644
3d407d2
--- a/grub-core/loader/arm64/linux.c
3d407d2
+++ b/grub-core/loader/arm64/linux.c
3d407d2
@@ -58,7 +58,7 @@ grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
3d407d2
   if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
3d407d2
     return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
3d407d2
 
3d407d2
-  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
3d407d2
+  if ((lh->code0 & 0xffff) != GRUB_DOS_MAGIC)
3d407d2
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
3d407d2
 		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
3d407d2
 
3d407d2
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
3d407d2
index a43adf2746..2a5e1ee003 100644
3d407d2
--- a/include/grub/efi/pe32.h
3d407d2
+++ b/include/grub/efi/pe32.h
3d407d2
@@ -46,7 +46,30 @@
3d407d2
 
3d407d2
 #define GRUB_PE32_MSDOS_STUB_SIZE	0x80
3d407d2
 
3d407d2
-#define GRUB_PE32_MAGIC			0x5a4d
3d407d2
+#define GRUB_DOS_MAGIC			0x5a4d
3d407d2
+
3d407d2
+struct grub_dos_header
3d407d2
+{
3d407d2
+  grub_uint16_t magic;
3d407d2
+  grub_uint16_t cblp;
3d407d2
+  grub_uint16_t cp;
3d407d2
+  grub_uint16_t crlc;
3d407d2
+  grub_uint16_t cparhdr;
3d407d2
+  grub_uint16_t minalloc;
3d407d2
+  grub_uint16_t maxalloc;
3d407d2
+  grub_uint16_t ss;
3d407d2
+  grub_uint16_t sp;
3d407d2
+  grub_uint16_t csum;
3d407d2
+  grub_uint16_t ip;
3d407d2
+  grub_uint16_t cs;
3d407d2
+  grub_uint16_t lfarlc;
3d407d2
+  grub_uint16_t ovno;
3d407d2
+  grub_uint16_t res0[4];
3d407d2
+  grub_uint16_t oemid;
3d407d2
+  grub_uint16_t oeminfo;
3d407d2
+  grub_uint16_t res1[10];
3d407d2
+  grub_uint32_t lfanew;
3d407d2
+};
3d407d2
 
3d407d2
 /* According to the spec, the minimal alignment is 512 bytes...
3d407d2
    But some examples (such as EFI drivers in the Intel
3d407d2
@@ -280,7 +303,8 @@ struct grub_pe32_section_table
3d407d2
 
3d407d2
 
3d407d2
 
3d407d2
-#define GRUB_PE32_SIGNATURE_SIZE 4
3d407d2
+#define GRUB_PE32_SIGNATURE_SIZE		4
3d407d2
+#define GRUB_PE32_SIGNATURE			"PE\0\0"
3d407d2
 
3d407d2
 struct grub_pe32_header
3d407d2
 {