nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0547-pe-add-the-DOS-header-struct-and-fix-some-bad-naming.patch

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