nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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