File 0004-pe-add-the-DOS-header-struct-and-fix-some-bad-naming.patch of Package grub2

From a19aeb0e4937b846f61da6c352b340fa446b4891 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Fri, 25 Mar 2022 15:40:12 -0400
Subject: [PATCH 04/28] pe: add the DOS header struct and fix some bad naming.

In order to properly validate a loaded kernel's support for being loaded
without a writable stack or executable, we need to be able to properly
parse arbitrary PE headers.

Currently, pe32.h is written in such a way that the MS-DOS header that
tells us where to find the PE header in the binary can't be accessed.
Further, for some reason it calls the DOS MZ magic "GRUB_PE32_MAGIC".

This patch adds the structure for the DOS header, renames the DOS magic
define, and adds defines for the actual PE magic.

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/loader/arm64/efi/linux.c |  2 +-
 grub-core/loader/arm64/xen_boot.c  |  2 +-
 grub-core/loader/efi/linux.c       |  2 +-
 include/grub/efi/pe32.h            | 28 ++++++++++++++++++++++++++--
 4 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/grub-core/loader/arm64/efi/linux.c b/grub-core/loader/arm64/efi/linux.c
index 33df0e1fd..c95f49e18 100644
--- a/grub-core/loader/arm64/efi/linux.c
+++ b/grub-core/loader/arm64/efi/linux.c
@@ -60,7 +60,7 @@ grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
   if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
     return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
 
-  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
+  if ((lh->code0 & 0xffff) != GRUB_DOS_MAGIC)
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
 
diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c
index b82a2db89..bd086670c 100644
--- a/grub-core/loader/arm64/xen_boot.c
+++ b/grub-core/loader/arm64/xen_boot.c
@@ -97,7 +97,7 @@ grub_arch_efi_linux_load_image_header (grub_file_t file,
   if (grub_file_read (file, lh, sizeof (*lh)) < (grub_ssize_t) sizeof (*lh))
     return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read Linux image header");
 
-  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
+  if ((lh->code0 & 0xffff) != GRUB_DOS_MAGIC)
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
 
diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c
index ab8fb35ad..5e12fea70 100644
--- a/grub-core/loader/efi/linux.c
+++ b/grub-core/loader/efi/linux.c
@@ -95,7 +95,7 @@ grub_arch_efi_linux_load_image_header (grub_file_t file,
   if (grub_file_read (file, lh, sizeof (*lh)) < (grub_ssize_t) sizeof (*lh))
     return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read Linux image header");
 
-  if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
+  if ((lh->code0 & 0xffff) != GRUB_DOS_MAGIC)
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
 		       N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
 
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
index 101859af1..c21e7a64d 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -46,7 +46,30 @@
 
 #define GRUB_PE32_MSDOS_STUB_SIZE	0x80
 
-#define GRUB_PE32_MAGIC			0x5a4d
+#define GRUB_DOS_MAGIC			0x5a4d
+
+struct grub_dos_header
+{
+  grub_uint16_t magic;
+  grub_uint16_t cblp;
+  grub_uint16_t cp;
+  grub_uint16_t crlc;
+  grub_uint16_t cparhdr;
+  grub_uint16_t minalloc;
+  grub_uint16_t maxalloc;
+  grub_uint16_t ss;
+  grub_uint16_t sp;
+  grub_uint16_t csum;
+  grub_uint16_t ip;
+  grub_uint16_t cs;
+  grub_uint16_t lfarlc;
+  grub_uint16_t ovno;
+  grub_uint16_t res0[4];
+  grub_uint16_t oemid;
+  grub_uint16_t oeminfo;
+  grub_uint16_t res1[10];
+  grub_uint32_t lfanew;
+};
 
 struct grub_msdos_image_header
 {
@@ -265,7 +288,8 @@ struct grub_pe32_section_table
 #define GRUB_PE32_SCN_ALIGN_SHIFT		20
 #define GRUB_PE32_SCN_ALIGN_MASK		7
 
-#define GRUB_PE32_SIGNATURE_SIZE 4
+#define GRUB_PE32_SIGNATURE_SIZE		4
+#define GRUB_PE32_SIGNATURE			"PE\0\0"
 
 struct grub_pe_image_header
 {
-- 
2.42.0

openSUSE Build Service is sponsored by