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

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