Blame SOURCES/0548-EFI-allocate-kernel-in-EFI_RUNTIME_SERVICES_CODE-ins.patch

b9d01e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9d01e
From: Peter Jones <pjones@redhat.com>
b9d01e
Date: Wed, 9 Feb 2022 16:08:20 -0500
b9d01e
Subject: [PATCH] EFI: allocate kernel in EFI_RUNTIME_SERVICES_CODE instead of
b9d01e
 EFI_LOADER_DATA.
b9d01e
b9d01e
On some of the firmwares with more security mitigations, EFI_LOADER_DATA
b9d01e
doesn't get you executable memory, and we take a fault and reboot when
b9d01e
we enter kernel.
b9d01e
b9d01e
This patch correctly allocates the kernel code as EFI_RUNTIME_SERVICES_CODE
b9d01e
rather than EFI_LOADER_DATA.
b9d01e
b9d01e
Signed-off-by: Peter Jones <pjones@redhat.com>
b9d01e
[rharwood: use kernel_size]
b9d01e
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
b9d01e
(cherry picked from commit 8b31058a12d3e85f0f0180ac90b98d6465fccbb7)
b9d01e
(cherry picked from commit 460df66aab9b3a57fc0d14a21a595cd467c4b13e)
b9d01e
---
b9d01e
 grub-core/loader/i386/efi/linux.c | 19 +++++++++++++------
b9d01e
 1 file changed, 13 insertions(+), 6 deletions(-)
b9d01e
b9d01e
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
b9d01e
index 8337191921..3d4069e4c6 100644
b9d01e
--- a/grub-core/loader/i386/efi/linux.c
b9d01e
+++ b/grub-core/loader/i386/efi/linux.c
b9d01e
@@ -86,7 +86,9 @@ kernel_free(void *addr, grub_efi_uintn_t size)
b9d01e
 }
b9d01e
 
b9d01e
 static void *
b9d01e
-kernel_alloc(grub_efi_uintn_t size, const char * const errmsg)
b9d01e
+kernel_alloc(grub_efi_uintn_t size,
b9d01e
+	     grub_efi_memory_type_t memtype,
b9d01e
+	     const char * const errmsg)
b9d01e
 {
b9d01e
   void *addr = 0;
b9d01e
   unsigned int i;
b9d01e
@@ -112,7 +114,7 @@ kernel_alloc(grub_efi_uintn_t size, const char * const errmsg)
b9d01e
       prev_max = max;
b9d01e
       addr = grub_efi_allocate_pages_real (max, pages,
b9d01e
 					   max_addresses[i].alloc_type,
b9d01e
-					   GRUB_EFI_LOADER_DATA);
b9d01e
+					   memtype);
b9d01e
       if (addr)
b9d01e
 	grub_dprintf ("linux", "Allocated at %p\n", addr);
b9d01e
     }
b9d01e
@@ -243,7 +245,8 @@ grub_cmd_initrd (grub_command_t cmd, int argc, char *argv[])
b9d01e
 	}
b9d01e
     }
b9d01e
 
b9d01e
-  initrd_mem = kernel_alloc(size, N_("can't allocate initrd"));
b9d01e
+  initrd_mem = kernel_alloc(size, GRUB_EFI_RUNTIME_SERVICES_DATA,
b9d01e
+			    N_("can't allocate initrd"));
b9d01e
   if (initrd_mem == NULL)
b9d01e
     goto fail;
b9d01e
   grub_dprintf ("linux", "initrd_mem = %p\n", initrd_mem);
b9d01e
@@ -411,7 +414,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
     }
b9d01e
 #endif
b9d01e
 
b9d01e
-  params = kernel_alloc (sizeof(*params), "cannot allocate kernel parameters");
b9d01e
+  params = kernel_alloc (sizeof(*params), GRUB_EFI_RUNTIME_SERVICES_DATA,
b9d01e
+			 "cannot allocate kernel parameters");
b9d01e
   if (!params)
b9d01e
     goto fail;
b9d01e
   grub_dprintf ("linux", "params = %p\n", params);
b9d01e
@@ -432,7 +436,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
   grub_dprintf ("linux", "new lh is at %p\n", lh);
b9d01e
 
b9d01e
   grub_dprintf ("linux", "setting up cmdline\n");
b9d01e
-  cmdline = kernel_alloc (lh->cmdline_size + 1, N_("can't allocate cmdline"));
b9d01e
+  cmdline = kernel_alloc (lh->cmdline_size + 1,
b9d01e
+			  GRUB_EFI_RUNTIME_SERVICES_DATA,
b9d01e
+			  N_("can't allocate cmdline"));
b9d01e
   if (!cmdline)
b9d01e
     goto fail;
b9d01e
   grub_dprintf ("linux", "cmdline = %p\n", cmdline);
b9d01e
@@ -478,7 +484,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
   max_addresses[1].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
b9d01e
   max_addresses[2].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
b9d01e
   kernel_size = lh->init_size;
b9d01e
-  kernel_mem = kernel_alloc (kernel_size, N_("can't allocate kernel"));
b9d01e
+  kernel_mem = kernel_alloc (kernel_size, GRUB_EFI_RUNTIME_SERVICES_CODE,
b9d01e
+			     N_("can't allocate kernel"));
b9d01e
   restore_addresses();
b9d01e
   if (!kernel_mem)
b9d01e
     goto fail;