Blame SOURCES/0552-nx-set-attrs-in-our-kernel-loaders.patch

0ccc47
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0ccc47
From: Peter Jones <pjones@redhat.com>
0ccc47
Date: Tue, 22 Mar 2022 10:57:07 -0400
0ccc47
Subject: [PATCH] nx: set attrs in our kernel loaders
0ccc47
0ccc47
For NX, our kernel loaders need to set write and execute page
0ccc47
permissions on allocated pages and the stack.
0ccc47
0ccc47
This patch adds those calls.
0ccc47
0ccc47
Signed-off-by: Peter Jones <pjones@redhat.com>
0ccc47
[rharwood: fix aarch64 callsites]
0ccc47
(cherry-picked from commit a9f79a997f01a83b36cdfa89ef2e72ac2a17c06c)
0ccc47
[rharwood: double verification]
0ccc47
(cherry picked from commit daba852bd3e4d7b7784b19cf7acf107dc3c0dce4)
0ccc47
[rharwood: stack_attrs initialization, no risc-v, arm renames, arm age]
0ccc47
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
0ccc47
---
0ccc47
 grub-core/kern/efi/mm.c            |  78 ++++++++++++++++++
0ccc47
 grub-core/loader/arm64/linux.c     |  16 +++-
0ccc47
 grub-core/loader/arm64/xen_boot.c  |   4 +-
0ccc47
 grub-core/loader/efi/chainloader.c |  11 +++
0ccc47
 grub-core/loader/efi/linux.c       | 162 ++++++++++++++++++++++++++++++++++++-
0ccc47
 grub-core/loader/i386/efi/linux.c  |  26 +++++-
0ccc47
 grub-core/loader/i386/linux.c      |   5 ++
0ccc47
 include/grub/efi/efi.h             |   6 +-
0ccc47
 include/grub/efi/linux.h           |  17 +++-
0ccc47
 include/grub/efi/pe32.h            |   2 +
0ccc47
 10 files changed, 312 insertions(+), 15 deletions(-)
0ccc47
0ccc47
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
0ccc47
index 2cf4a4883a..8a896144df 100644
0ccc47
--- a/grub-core/kern/efi/mm.c
0ccc47
+++ b/grub-core/kern/efi/mm.c
0ccc47
@@ -602,6 +602,82 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map,
0ccc47
 }
0ccc47
 #endif
0ccc47
 
0ccc47
+grub_addr_t grub_stack_addr = (grub_addr_t)-1ll;
0ccc47
+grub_size_t grub_stack_size = 0;
0ccc47
+
0ccc47
+static void
0ccc47
+grub_nx_init (void)
0ccc47
+{
0ccc47
+  grub_uint64_t attrs, stack_attrs;
0ccc47
+  grub_err_t err;
0ccc47
+  grub_addr_t stack_current, stack_end;
0ccc47
+  const grub_uint64_t page_size = 4096;
0ccc47
+  const grub_uint64_t page_mask = ~(page_size - 1);
0ccc47
+
0ccc47
+  /*
0ccc47
+   * These are to confirm that the flags are working as expected when
0ccc47
+   * debugging.
0ccc47
+   */
0ccc47
+  attrs = 0;
0ccc47
+  stack_current = (grub_addr_t)grub_nx_init & page_mask;
0ccc47
+  err = grub_get_mem_attrs (stack_current, page_size, &attrs);
0ccc47
+  if (err)
0ccc47
+    {
0ccc47
+      grub_dprintf ("nx",
0ccc47
+		    "grub_get_mem_attrs(0x%"PRIxGRUB_UINT64_T", ...) -> 0x%x\n",
0ccc47
+		    stack_current, err);
0ccc47
+      grub_error_pop ();
0ccc47
+    }
0ccc47
+  else
0ccc47
+    grub_dprintf ("nx", "page attrs for grub_nx_init (%p) are %c%c%c\n",
0ccc47
+		  grub_dl_load_core,
0ccc47
+		  (attrs & GRUB_MEM_ATTR_R) ? 'r' : '-',
0ccc47
+		  (attrs & GRUB_MEM_ATTR_R) ? 'w' : '-',
0ccc47
+		  (attrs & GRUB_MEM_ATTR_R) ? 'x' : '-');
0ccc47
+
0ccc47
+  stack_current = (grub_addr_t)&stack_current & page_mask;
0ccc47
+  err = grub_get_mem_attrs (stack_current, page_size, &stack_attrs);
0ccc47
+  if (err)
0ccc47
+    {
0ccc47
+      grub_dprintf ("nx",
0ccc47
+		    "grub_get_mem_attrs(0x%"PRIxGRUB_UINT64_T", ...) -> 0x%x\n",
0ccc47
+		    stack_current, err);
0ccc47
+      grub_error_pop ();
0ccc47
+    }
0ccc47
+  else
0ccc47
+    {
0ccc47
+      attrs = stack_attrs;
0ccc47
+      grub_dprintf ("nx", "page attrs for stack (%p) are %c%c%c\n",
0ccc47
+                    &attrs,
0ccc47
+                    (attrs & GRUB_MEM_ATTR_R) ? 'r' : '-',
0ccc47
+                    (attrs & GRUB_MEM_ATTR_R) ? 'w' : '-',
0ccc47
+                    (attrs & GRUB_MEM_ATTR_R) ? 'x' : '-');
0ccc47
+    }
0ccc47
+
0ccc47
+  for (stack_end = stack_current + page_size ;
0ccc47
+       !(attrs & GRUB_MEM_ATTR_R);
0ccc47
+       stack_end += page_size)
0ccc47
+    {
0ccc47
+      err = grub_get_mem_attrs (stack_current, page_size, &attrs);
0ccc47
+      if (err)
0ccc47
+	{
0ccc47
+	  grub_dprintf ("nx",
0ccc47
+			"grub_get_mem_attrs(0x%"PRIxGRUB_UINT64_T", ...) -> 0x%x\n",
0ccc47
+			stack_current, err);
0ccc47
+	  grub_error_pop ();
0ccc47
+	  break;
0ccc47
+	}
0ccc47
+    }
0ccc47
+  if (stack_end > stack_current)
0ccc47
+    {
0ccc47
+      grub_stack_addr = stack_current;
0ccc47
+      grub_stack_size = stack_end - stack_current;
0ccc47
+      grub_dprintf ("nx",
0ccc47
+		    "detected stack from 0x%"PRIxGRUB_ADDR" to 0x%"PRIxGRUB_ADDR"\n",
0ccc47
+		    grub_stack_addr, grub_stack_addr + grub_stack_size - 1);
0ccc47
+    }
0ccc47
+}
0ccc47
+
0ccc47
 void
0ccc47
 grub_efi_mm_init (void)
0ccc47
 {
0ccc47
@@ -615,6 +691,8 @@ grub_efi_mm_init (void)
0ccc47
   grub_efi_uint64_t required_pages;
0ccc47
   int mm_status;
0ccc47
 
0ccc47
+  grub_nx_init ();
0ccc47
+
0ccc47
   /* Prepare a memory region to store two memory maps.  */
0ccc47
   memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
0ccc47
   if (! memory_map)
0ccc47
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
0ccc47
index 24ab0f0074..37f5d0c7eb 100644
0ccc47
--- a/grub-core/loader/arm64/linux.c
0ccc47
+++ b/grub-core/loader/arm64/linux.c
0ccc47
@@ -191,7 +191,8 @@ free_params (void)
0ccc47
 }
0ccc47
 
0ccc47
 grub_err_t
0ccc47
-grub_armxx_efi_linux_boot_image (grub_addr_t addr, char *args)
0ccc47
+grub_armxx_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args,
0ccc47
+				int nx_supported)
0ccc47
 {
0ccc47
   grub_err_t retval;
0ccc47
 
0ccc47
@@ -201,7 +202,8 @@ grub_armxx_efi_linux_boot_image (grub_addr_t addr, char *args)
0ccc47
 
0ccc47
   grub_dprintf ("linux", "linux command line: '%s'\n", args);
0ccc47
 
0ccc47
-  retval = grub_efi_linux_boot ((char *)addr, handover_offset, (void *)addr);
0ccc47
+  retval = grub_efi_linux_boot (addr, size, handover_offset,
0ccc47
+				(void *)addr, nx_supported);
0ccc47
 
0ccc47
   /* Never reached... */
0ccc47
   free_params();
0ccc47
@@ -211,7 +213,10 @@ grub_armxx_efi_linux_boot_image (grub_addr_t addr, char *args)
0ccc47
 static grub_err_t
0ccc47
 grub_linux_boot (void)
0ccc47
 {
0ccc47
-  return grub_armxx_efi_linux_boot_image((grub_addr_t)kernel_addr, linux_args);
0ccc47
+  return grub_armxx_efi_linux_boot_image((grub_addr_t)kernel_addr,
0ccc47
+					(grub_size_t)kernel_size,
0ccc47
+					linux_args,
0ccc47
+					0);
0ccc47
 }
0ccc47
 
0ccc47
 static grub_err_t
0ccc47
@@ -340,6 +345,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   struct grub_armxx_linux_pe_header *pe;
0ccc47
   int rc;
0ccc47
   grub_err_t err;
0ccc47
+  int nx_supported = 1;
0ccc47
 
0ccc47
   grub_dl_ref (my_mod);
0ccc47
 
0ccc47
@@ -395,6 +401,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
 	}
0ccc47
     }
0ccc47
 
0ccc47
+  err = grub_efi_check_nx_image_support((grub_addr_t) kernel_addr, kernel_size, &nx_supported);
0ccc47
+  if (err != GRUB_ERR_NONE)
0ccc47
+    goto fail;
0ccc47
+
0ccc47
   pe = (void *)((unsigned long)kernel_addr + lh.hdr_offset);
0ccc47
   handover_offset = pe->opt.entry_addr;
0ccc47
 
0ccc47
diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c
0ccc47
index 1a337866f0..1fd1bbb4bd 100644
0ccc47
--- a/grub-core/loader/arm64/xen_boot.c
0ccc47
+++ b/grub-core/loader/arm64/xen_boot.c
0ccc47
@@ -266,7 +266,9 @@ xen_boot (void)
0ccc47
     return err;
0ccc47
 
0ccc47
   return grub_armxx_efi_linux_boot_image (xen_hypervisor->start,
0ccc47
-					  xen_hypervisor->cmdline);
0ccc47
+                                         xen_hypervisor->size,
0ccc47
+                                         xen_hypervisor->cmdline,
0ccc47
+                                         0);
0ccc47
 }
0ccc47
 
0ccc47
 static void
0ccc47
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
0ccc47
index 8e658f713e..b72e6bd5e3 100644
0ccc47
--- a/grub-core/loader/efi/chainloader.c
0ccc47
+++ b/grub-core/loader/efi/chainloader.c
0ccc47
@@ -1055,6 +1055,17 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
0ccc47
       goto fail;
0ccc47
     }
0ccc47
 
0ccc47
+  /*
0ccc47
+   * The OS kernel is going to set its own permissions when it takes over
0ccc47
+   * paging a few million instructions from now, and load_image() will set up
0ccc47
+   * anything that's needed based on the section headers, so there's no point
0ccc47
+   * in doing anything but clearing the protection bits here.
0ccc47
+   */
0ccc47
+  grub_dprintf("nx", "setting attributes for %p (%lu bytes) to %llx\n",
0ccc47
+	       (void *)(grub_addr_t)address, fsize, 0llu);
0ccc47
+  grub_update_mem_attrs (address, fsize,
0ccc47
+			 GRUB_MEM_ATTR_R|GRUB_MEM_ATTR_W|GRUB_MEM_ATTR_X, 0);
0ccc47
+
0ccc47
 #if defined (__i386__) || defined (__x86_64__)
0ccc47
   if (fsize >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
0ccc47
     {
0ccc47
diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c
0ccc47
index 927d89a90d..421502bd25 100644
0ccc47
--- a/grub-core/loader/efi/linux.c
0ccc47
+++ b/grub-core/loader/efi/linux.c
0ccc47
@@ -66,16 +66,125 @@ grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
0ccc47
 
0ccc47
 #pragma GCC diagnostic push
0ccc47
 #pragma GCC diagnostic ignored "-Wcast-align"
0ccc47
+#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
0ccc47
+
0ccc47
+grub_err_t
0ccc47
+grub_efi_check_nx_image_support (grub_addr_t kernel_addr,
0ccc47
+				 grub_size_t kernel_size,
0ccc47
+				 int *nx_supported)
0ccc47
+{
0ccc47
+  struct grub_dos_header *doshdr;
0ccc47
+  grub_size_t sz = sizeof (*doshdr);
0ccc47
+
0ccc47
+  struct grub_pe32_header_32 *pe32;
0ccc47
+  struct grub_pe32_header_64 *pe64;
0ccc47
+
0ccc47
+  int image_is_compatible = 0;
0ccc47
+  int is_64_bit;
0ccc47
+
0ccc47
+  if (kernel_size < sz)
0ccc47
+    return grub_error (GRUB_ERR_BAD_OS, N_("kernel is too small"));
0ccc47
+
0ccc47
+  doshdr = (void *)kernel_addr;
0ccc47
+
0ccc47
+  if ((doshdr->magic & 0xffff) != GRUB_DOS_MAGIC)
0ccc47
+    return grub_error (GRUB_ERR_BAD_OS, N_("kernel DOS magic is invalid"));
0ccc47
+
0ccc47
+  sz = doshdr->lfanew + sizeof (*pe32);
0ccc47
+  if (kernel_size < sz)
0ccc47
+    return grub_error (GRUB_ERR_BAD_OS, N_("kernel is too small"));
0ccc47
+
0ccc47
+  pe32 = (struct grub_pe32_header_32 *)(kernel_addr + doshdr->lfanew);
0ccc47
+  pe64 = (struct grub_pe32_header_64 *)pe32;
0ccc47
+
0ccc47
+  if (grub_memcmp (pe32->signature, GRUB_PE32_SIGNATURE,
0ccc47
+		   GRUB_PE32_SIGNATURE_SIZE) != 0)
0ccc47
+    return grub_error (GRUB_ERR_BAD_OS, N_("kernel PE magic is invalid"));
0ccc47
+
0ccc47
+  switch (pe32->coff_header.machine)
0ccc47
+    {
0ccc47
+    case GRUB_PE32_MACHINE_ARMTHUMB_MIXED:
0ccc47
+    case GRUB_PE32_MACHINE_I386:
0ccc47
+      is_64_bit = 0;
0ccc47
+      break;
0ccc47
+    case GRUB_PE32_MACHINE_ARM64:
0ccc47
+    case GRUB_PE32_MACHINE_IA64:
0ccc47
+    case GRUB_PE32_MACHINE_X86_64:
0ccc47
+      is_64_bit = 1;
0ccc47
+      break;
0ccc47
+    default:
0ccc47
+      return grub_error (GRUB_ERR_BAD_OS, N_("PE machine type 0x%04hx unknown"),
0ccc47
+			 pe32->coff_header.machine);
0ccc47
+    }
0ccc47
+
0ccc47
+  if (is_64_bit)
0ccc47
+    {
0ccc47
+      sz = doshdr->lfanew + sizeof (*pe64);
0ccc47
+      if (kernel_size < sz)
0ccc47
+	return grub_error (GRUB_ERR_BAD_OS, N_("kernel is too small"));
0ccc47
+
0ccc47
+      if (pe64->optional_header.dll_characteristics & GRUB_PE32_NX_COMPAT)
0ccc47
+	image_is_compatible = 1;
0ccc47
+    }
0ccc47
+  else
0ccc47
+    {
0ccc47
+      if (pe32->optional_header.dll_characteristics & GRUB_PE32_NX_COMPAT)
0ccc47
+	image_is_compatible = 1;
0ccc47
+    }
0ccc47
+
0ccc47
+  *nx_supported = image_is_compatible;
0ccc47
+  return GRUB_ERR_NONE;
0ccc47
+}
0ccc47
+
0ccc47
+grub_err_t
0ccc47
+grub_efi_check_nx_required (int *nx_required)
0ccc47
+{
0ccc47
+  grub_efi_status_t status;
0ccc47
+  grub_efi_guid_t guid = GRUB_EFI_SHIM_LOCK_GUID;
0ccc47
+  grub_size_t mok_policy_sz = 0;
0ccc47
+  char *mok_policy = NULL;
0ccc47
+  grub_uint32_t mok_policy_attrs = 0;
0ccc47
+
0ccc47
+  status = grub_efi_get_variable_with_attributes ("MokPolicy", &guid,
0ccc47
+						  &mok_policy_sz,
0ccc47
+						  (void **)&mok_policy,
0ccc47
+						  &mok_policy_attrs);
0ccc47
+  if (status == GRUB_EFI_NOT_FOUND ||
0ccc47
+      mok_policy_sz == 0 ||
0ccc47
+      mok_policy == NULL)
0ccc47
+    {
0ccc47
+      *nx_required = 0;
0ccc47
+      return GRUB_ERR_NONE;
0ccc47
+    }
0ccc47
+
0ccc47
+  *nx_required = 0;
0ccc47
+  if (mok_policy_sz < 1 ||
0ccc47
+      mok_policy_attrs != (GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
0ccc47
+			   GRUB_EFI_VARIABLE_RUNTIME_ACCESS) ||
0ccc47
+      (mok_policy[mok_policy_sz-1] & GRUB_MOK_POLICY_NX_REQUIRED))
0ccc47
+    *nx_required = 1;
0ccc47
+
0ccc47
+  return GRUB_ERR_NONE;
0ccc47
+}
0ccc47
 
0ccc47
 typedef void (*handover_func) (void *, grub_efi_system_table_t *, void *);
0ccc47
 
0ccc47
 grub_err_t
0ccc47
-grub_efi_linux_boot (void *kernel_addr, grub_off_t handover_offset,
0ccc47
-		     void *kernel_params)
0ccc47
+grub_efi_linux_boot (grub_addr_t kernel_addr, grub_size_t kernel_size,
0ccc47
+		     grub_off_t handover_offset, void *kernel_params,
0ccc47
+		     int nx_supported)
0ccc47
 {
0ccc47
   grub_efi_loaded_image_t *loaded_image = NULL;
0ccc47
   handover_func hf;
0ccc47
   int offset = 0;
0ccc47
+  grub_uint64_t stack_set_attrs = GRUB_MEM_ATTR_R |
0ccc47
+				  GRUB_MEM_ATTR_W |
0ccc47
+				  GRUB_MEM_ATTR_X;
0ccc47
+  grub_uint64_t stack_clear_attrs = 0;
0ccc47
+  grub_uint64_t kernel_set_attrs = stack_set_attrs;
0ccc47
+  grub_uint64_t kernel_clear_attrs = stack_clear_attrs;
0ccc47
+  grub_uint64_t attrs;
0ccc47
+  int nx_required = 0;
0ccc47
 
0ccc47
 #ifdef __x86_64__
0ccc47
   offset = 512;
0ccc47
@@ -88,12 +197,57 @@ grub_efi_linux_boot (void *kernel_addr, grub_off_t handover_offset,
0ccc47
    */
0ccc47
   loaded_image = grub_efi_get_loaded_image (grub_efi_image_handle);
0ccc47
   if (loaded_image)
0ccc47
-    loaded_image->image_base = kernel_addr;
0ccc47
+    loaded_image->image_base = (void *)kernel_addr;
0ccc47
   else
0ccc47
     grub_dprintf ("linux", "Loaded Image base address could not be set\n");
0ccc47
 
0ccc47
   grub_dprintf ("linux", "kernel_addr: %p handover_offset: %p params: %p\n",
0ccc47
-		kernel_addr, (void *)(grub_efi_uintn_t)handover_offset, kernel_params);
0ccc47
+		(void *)kernel_addr, (void *)handover_offset, kernel_params);
0ccc47
+
0ccc47
+
0ccc47
+  if (nx_required && !nx_supported)
0ccc47
+    return grub_error (GRUB_ERR_BAD_OS, N_("kernel does not support NX loading required by policy"));
0ccc47
+
0ccc47
+  if (nx_supported)
0ccc47
+    {
0ccc47
+      kernel_set_attrs &= ~GRUB_MEM_ATTR_W;
0ccc47
+      kernel_clear_attrs |= GRUB_MEM_ATTR_W;
0ccc47
+      stack_set_attrs &= ~GRUB_MEM_ATTR_X;
0ccc47
+      stack_clear_attrs |= GRUB_MEM_ATTR_X;
0ccc47
+    }
0ccc47
+
0ccc47
+  grub_dprintf ("nx", "Setting attributes for 0x%"PRIxGRUB_ADDR"-0x%"PRIxGRUB_ADDR" to r%cx\n",
0ccc47
+		    kernel_addr, kernel_addr + kernel_size - 1,
0ccc47
+		    (kernel_set_attrs & GRUB_MEM_ATTR_W) ? 'w' : '-');
0ccc47
+  grub_update_mem_attrs (kernel_addr, kernel_size,
0ccc47
+			 kernel_set_attrs, kernel_clear_attrs);
0ccc47
+
0ccc47
+  grub_get_mem_attrs (kernel_addr, 4096, &attrs);
0ccc47
+  grub_dprintf ("nx", "permissions for 0x%"PRIxGRUB_ADDR" are %s%s%s\n",
0ccc47
+		(grub_addr_t)kernel_addr,
0ccc47
+		(attrs & GRUB_MEM_ATTR_R) ? "r" : "-",
0ccc47
+		(attrs & GRUB_MEM_ATTR_W) ? "w" : "-",
0ccc47
+		(attrs & GRUB_MEM_ATTR_X) ? "x" : "-");
0ccc47
+  if (grub_stack_addr != (grub_addr_t)-1ll)
0ccc47
+    {
0ccc47
+      grub_dprintf ("nx", "Setting attributes for stack at 0x%"PRIxGRUB_ADDR"-0x%"PRIxGRUB_ADDR" to rw%c\n",
0ccc47
+		    grub_stack_addr, grub_stack_addr + grub_stack_size - 1,
0ccc47
+		    (stack_set_attrs & GRUB_MEM_ATTR_X) ? 'x' : '-');
0ccc47
+      grub_update_mem_attrs (grub_stack_addr, grub_stack_size,
0ccc47
+			     stack_set_attrs, stack_clear_attrs);
0ccc47
+
0ccc47
+      grub_get_mem_attrs (grub_stack_addr, 4096, &attrs);
0ccc47
+      grub_dprintf ("nx", "permissions for 0x%"PRIxGRUB_ADDR" are %s%s%s\n",
0ccc47
+		    grub_stack_addr,
0ccc47
+		    (attrs & GRUB_MEM_ATTR_R) ? "r" : "-",
0ccc47
+		    (attrs & GRUB_MEM_ATTR_W) ? "w" : "-",
0ccc47
+		    (attrs & GRUB_MEM_ATTR_X) ? "x" : "-");
0ccc47
+    }
0ccc47
+
0ccc47
+#if defined(__i386__) || defined(__x86_64__)
0ccc47
+  asm volatile ("cli");
0ccc47
+#endif
0ccc47
+
0ccc47
   hf = (handover_func)((char *)kernel_addr + handover_offset + offset);
0ccc47
   grub_dprintf ("linux", "handover_func() = %p\n", hf);
0ccc47
   hf (grub_efi_image_handle, grub_efi_system_table, kernel_params);
0ccc47
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
0ccc47
index 3d4069e4c6..d80d6ec312 100644
0ccc47
--- a/grub-core/loader/i386/efi/linux.c
0ccc47
+++ b/grub-core/loader/i386/efi/linux.c
0ccc47
@@ -44,7 +44,7 @@ struct grub_linuxefi_context {
0ccc47
   grub_uint32_t handover_offset;
0ccc47
   struct linux_kernel_params *params;
0ccc47
   char *cmdline;
0ccc47
-
0ccc47
+  int nx_supported;
0ccc47
   void *initrd_mem;
0ccc47
 };
0ccc47
 
0ccc47
@@ -110,13 +110,19 @@ kernel_alloc(grub_efi_uintn_t size,
0ccc47
       pages = BYTES_TO_PAGES(size);
0ccc47
       grub_dprintf ("linux", "Trying to allocate %lu pages from %p\n",
0ccc47
 		    pages, (void *)max);
0ccc47
+      size = pages * GRUB_EFI_PAGE_SIZE;
0ccc47
 
0ccc47
       prev_max = max;
0ccc47
       addr = grub_efi_allocate_pages_real (max, pages,
0ccc47
 					   max_addresses[i].alloc_type,
0ccc47
 					   memtype);
0ccc47
       if (addr)
0ccc47
-	grub_dprintf ("linux", "Allocated at %p\n", addr);
0ccc47
+	{
0ccc47
+	  grub_dprintf ("linux", "Allocated at %p\n", addr);
0ccc47
+	  grub_update_mem_attrs ((grub_addr_t)addr, size,
0ccc47
+				 GRUB_MEM_ATTR_R|GRUB_MEM_ATTR_W,
0ccc47
+				 GRUB_MEM_ATTR_X);
0ccc47
+	}
0ccc47
     }
0ccc47
 
0ccc47
   while (grub_error_pop ())
0ccc47
@@ -137,9 +143,11 @@ grub_linuxefi_boot (void *data)
0ccc47
 
0ccc47
   asm volatile ("cli");
0ccc47
 
0ccc47
-  return grub_efi_linux_boot ((char *)context->kernel_mem,
0ccc47
+  return grub_efi_linux_boot ((grub_addr_t)context->kernel_mem,
0ccc47
+			      context->kernel_size,
0ccc47
 			      context->handover_offset,
0ccc47
-			      context->params);
0ccc47
+			      context->params,
0ccc47
+			      context->nx_supported);
0ccc47
 }
0ccc47
 
0ccc47
 static grub_err_t
0ccc47
@@ -308,7 +316,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   grub_uint32_t handover_offset;
0ccc47
   struct linux_kernel_params *params = 0;
0ccc47
   char *cmdline = 0;
0ccc47
+  int nx_supported = 1;
0ccc47
   struct grub_linuxefi_context *context = 0;
0ccc47
+  grub_err_t err;
0ccc47
 
0ccc47
   grub_dl_ref (my_mod);
0ccc47
 
0ccc47
@@ -352,6 +362,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
 	}
0ccc47
     }
0ccc47
 
0ccc47
+  err = grub_efi_check_nx_image_support ((grub_addr_t)kernel, filelen,
0ccc47
+					 &nx_supported);
0ccc47
+  if (err != GRUB_ERR_NONE)
0ccc47
+    return err;
0ccc47
+  grub_dprintf ("linux", "nx is%s supported by this kernel\n",
0ccc47
+		nx_supported ? "" : " not");
0ccc47
+
0ccc47
   lh = (struct linux_i386_kernel_header *)kernel;
0ccc47
   grub_dprintf ("linux", "original lh is at %p\n", kernel);
0ccc47
 
0ccc47
@@ -515,6 +532,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   context->handover_offset = handover_offset;
0ccc47
   context->params = params;
0ccc47
   context->cmdline = cmdline;
0ccc47
+  context->nx_supported = nx_supported;
0ccc47
 
0ccc47
   grub_loader_set_ex (grub_linuxefi_boot, grub_linuxefi_unload, context, 0);
0ccc47
 
0ccc47
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
0ccc47
index ef8fcb9e1b..c160ddb0ea 100644
0ccc47
--- a/grub-core/loader/i386/linux.c
0ccc47
+++ b/grub-core/loader/i386/linux.c
0ccc47
@@ -831,6 +831,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   grub_memset (&linux_params, 0, sizeof (linux_params));
0ccc47
   grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
0ccc47
 
0ccc47
+  grub_dprintf("efi", "setting attributes for %p (%zu bytes) to +rw-x\n",
0ccc47
+	       &linux_params, sizeof (lh) + len);
0ccc47
+  grub_update_mem_attrs ((grub_addr_t)&linux_params, sizeof (lh) + len,
0ccc47
+			 GRUB_MEM_ATTR_R|GRUB_MEM_ATTR_W, GRUB_MEM_ATTR_X);
0ccc47
+
0ccc47
   linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
0ccc47
   linux_params.kernel_alignment = (1 << align);
0ccc47
   linux_params.ps_mouse = linux_params.padding10 =  0;
0ccc47
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
0ccc47
index a635bcb0a9..8ca8c38f9a 100644
0ccc47
--- a/include/grub/efi/efi.h
0ccc47
+++ b/include/grub/efi/efi.h
0ccc47
@@ -135,12 +135,16 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
0ccc47
 						char **device,
0ccc47
 						char **path);
0ccc47
 
0ccc47
+extern grub_addr_t EXPORT_VAR(grub_stack_addr);
0ccc47
+extern grub_size_t EXPORT_VAR(grub_stack_size);
0ccc47
+
0ccc47
 #if defined(__arm__) || defined(__aarch64__)
0ccc47
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
0ccc47
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
0ccc47
 #include <grub/cpu/linux.h>
0ccc47
 grub_err_t grub_armxx_efi_linux_check_image(struct linux_armxx_kernel_header *lh);
0ccc47
-grub_err_t grub_armxx_efi_linux_boot_image(grub_addr_t addr, char *args);
0ccc47
+grub_err_t grub_armxx_efi_linux_boot_image(grub_addr_t addr, grub_size_t size,
0ccc47
+					  char *args, int nx_enabled);
0ccc47
 #endif
0ccc47
 
0ccc47
 grub_addr_t grub_efi_section_addr (const char *section);
0ccc47
diff --git a/include/grub/efi/linux.h b/include/grub/efi/linux.h
0ccc47
index 0033d9305a..8130b19590 100644
0ccc47
--- a/include/grub/efi/linux.h
0ccc47
+++ b/include/grub/efi/linux.h
0ccc47
@@ -22,10 +22,23 @@
0ccc47
 #include <grub/err.h>
0ccc47
 #include <grub/symbol.h>
0ccc47
 
0ccc47
+#define GRUB_MOK_POLICY_NX_REQUIRED   0x1
0ccc47
+
0ccc47
 int
0ccc47
 EXPORT_FUNC(grub_linuxefi_secure_validate) (void *data, grub_uint32_t size);
0ccc47
+
0ccc47
 grub_err_t
0ccc47
-EXPORT_FUNC(grub_efi_linux_boot) (void *kernel_address, grub_off_t offset,
0ccc47
-				  void *kernel_param);
0ccc47
+EXPORT_FUNC(grub_efi_linux_boot) (grub_addr_t kernel_address,
0ccc47
+				  grub_size_t kernel_size,
0ccc47
+				  grub_off_t handover_offset,
0ccc47
+				  void *kernel_param, int nx_enabled);
0ccc47
+
0ccc47
+grub_err_t
0ccc47
+EXPORT_FUNC(grub_efi_check_nx_image_support) (grub_addr_t kernel_addr,
0ccc47
+					      grub_size_t kernel_size,
0ccc47
+					      int *nx_supported);
0ccc47
+
0ccc47
+grub_err_t
0ccc47
+EXPORT_FUNC(grub_efi_check_nx_required) (int *nx_required);
0ccc47
 
0ccc47
 #endif /* ! GRUB_EFI_LINUX_HEADER */
0ccc47
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
0ccc47
index 2241f6317b..45c9f8b756 100644
0ccc47
--- a/include/grub/efi/pe32.h
0ccc47
+++ b/include/grub/efi/pe32.h
0ccc47
@@ -172,6 +172,8 @@ struct grub_pe32_optional_header
0ccc47
   struct grub_pe32_data_directory reserved_entry;
0ccc47
 };
0ccc47
 
0ccc47
+#define GRUB_PE32_NX_COMPAT 0x0100
0ccc47
+
0ccc47
 struct grub_pe64_optional_header
0ccc47
 {
0ccc47
   grub_uint16_t magic;