b9d01e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9d01e
From: Peter Jones <pjones@redhat.com>
b9d01e
Date: Tue, 22 Mar 2022 10:56:21 -0400
b9d01e
Subject: [PATCH] nx: add memory attribute get/set API
b9d01e
b9d01e
For NX, we need to set the page access permission attributes for write
b9d01e
and execute permissions.
b9d01e
b9d01e
This patch adds two new primitives, grub_set_mem_attrs() and
b9d01e
grub_clear_mem_attrs(), and associated constant definitions, to be used
b9d01e
for that purpose.
b9d01e
b9d01e
For most platforms, it adds a dummy implementation that returns
b9d01e
GRUB_ERR_NONE.  On EFI platforms, it adds a common helper function,
b9d01e
grub_efi_status_to_err(), which translates EFI error codes to grub error
b9d01e
codes, adds headers for the EFI Memory Attribute Protocol (still pending
b9d01e
standardization), and an implementation of the grub nx primitives using
b9d01e
it.
b9d01e
b9d01e
Signed-off-by: Peter Jones <pjones@redhat.com>
b9d01e
[rharwood: add pjones's none/nyi fixup]
b9d01e
(cherry picked from commit 35de78a8d32b9fad5291ec96fd3cbb9cf2f4a80b)
b9d01e
(cherry picked from commit 46cb4f9557bdba1db0a17d012df705d94d81a9f6)
b9d01e
[rharwood: context fuzz, guids]
b9d01e
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
b9d01e
---
b9d01e
 grub-core/kern/efi/efi.c |  36 +++++++++++++
b9d01e
 grub-core/kern/efi/mm.c  | 131 +++++++++++++++++++++++++++++++++++++++++++++++
b9d01e
 include/grub/efi/api.h   |  25 +++++++++
b9d01e
 include/grub/efi/efi.h   |   2 +
b9d01e
 include/grub/mm.h        |  32 ++++++++++++
b9d01e
 5 files changed, 226 insertions(+)
b9d01e
b9d01e
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
b9d01e
index fccea20a01..09468dc5d5 100644
b9d01e
--- a/grub-core/kern/efi/efi.c
b9d01e
+++ b/grub-core/kern/efi/efi.c
b9d01e
@@ -1093,3 +1093,39 @@ grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
b9d01e
 
b9d01e
   return 0;
b9d01e
 }
b9d01e
+
b9d01e
+grub_err_t
b9d01e
+grub_efi_status_to_err (grub_efi_status_t status)
b9d01e
+{
b9d01e
+  grub_err_t err;
b9d01e
+  switch (status)
b9d01e
+    {
b9d01e
+    case GRUB_EFI_SUCCESS:
b9d01e
+      err = GRUB_ERR_NONE;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_INVALID_PARAMETER:
b9d01e
+    default:
b9d01e
+      err = GRUB_ERR_BAD_ARGUMENT;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_OUT_OF_RESOURCES:
b9d01e
+      err = GRUB_ERR_OUT_OF_MEMORY;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_DEVICE_ERROR:
b9d01e
+      err = GRUB_ERR_IO;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_WRITE_PROTECTED:
b9d01e
+      err = GRUB_ERR_WRITE_ERROR;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_SECURITY_VIOLATION:
b9d01e
+      err = GRUB_ERR_ACCESS_DENIED;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_NOT_FOUND:
b9d01e
+      err = GRUB_ERR_FILE_NOT_FOUND;
b9d01e
+      break;
b9d01e
+    case GRUB_EFI_ABORTED:
b9d01e
+      err = GRUB_ERR_WAIT;
b9d01e
+      break;
b9d01e
+    }
b9d01e
+
b9d01e
+  return err;
b9d01e
+}
b9d01e
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
b9d01e
index 9e76f23e5f..2cf4a4883a 100644
b9d01e
--- a/grub-core/kern/efi/mm.c
b9d01e
+++ b/grub-core/kern/efi/mm.c
b9d01e
@@ -730,3 +730,134 @@ grub_efi_get_ram_base(grub_addr_t *base_addr)
b9d01e
   return GRUB_ERR_NONE;
b9d01e
 }
b9d01e
 #endif
b9d01e
+
b9d01e
+static inline grub_uint64_t
b9d01e
+grub_mem_attrs_to_uefi_mem_attrs (grub_uint64_t attrs)
b9d01e
+{
b9d01e
+  grub_uint64_t ret = GRUB_EFI_MEMORY_RP |
b9d01e
+		      GRUB_EFI_MEMORY_RO |
b9d01e
+		      GRUB_EFI_MEMORY_XP;
b9d01e
+
b9d01e
+  if (attrs & GRUB_MEM_ATTR_R)
b9d01e
+    ret &= ~GRUB_EFI_MEMORY_RP;
b9d01e
+
b9d01e
+  if (attrs & GRUB_MEM_ATTR_W)
b9d01e
+    ret &= ~GRUB_EFI_MEMORY_RO;
b9d01e
+
b9d01e
+  if (attrs & GRUB_MEM_ATTR_X)
b9d01e
+    ret &= ~GRUB_EFI_MEMORY_XP;
b9d01e
+
b9d01e
+  return ret;
b9d01e
+}
b9d01e
+
b9d01e
+static inline grub_uint64_t
b9d01e
+uefi_mem_attrs_to_grub_mem_attrs (grub_uint64_t attrs)
b9d01e
+{
b9d01e
+  grub_uint64_t ret = GRUB_MEM_ATTR_R |
b9d01e
+		      GRUB_MEM_ATTR_W |
b9d01e
+		      GRUB_MEM_ATTR_X;
b9d01e
+
b9d01e
+  if (attrs & GRUB_EFI_MEMORY_RP)
b9d01e
+    ret &= ~GRUB_MEM_ATTR_R;
b9d01e
+
b9d01e
+  if (attrs & GRUB_EFI_MEMORY_RO)
b9d01e
+    ret &= ~GRUB_MEM_ATTR_W;
b9d01e
+
b9d01e
+  if (attrs & GRUB_EFI_MEMORY_XP)
b9d01e
+    ret &= ~GRUB_MEM_ATTR_X;
b9d01e
+
b9d01e
+  return ret;
b9d01e
+}
b9d01e
+
b9d01e
+grub_err_t
b9d01e
+grub_get_mem_attrs (grub_addr_t addr, grub_size_t size, grub_uint64_t *attrs)
b9d01e
+{
b9d01e
+  grub_efi_memory_attribute_protocol_t *proto;
b9d01e
+  grub_efi_physical_address_t physaddr = addr;
b9d01e
+  grub_efi_guid_t protocol_guid = GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
b9d01e
+  grub_efi_status_t efi_status;
b9d01e
+
b9d01e
+  proto = grub_efi_locate_protocol (&protocol_guid, 0);
b9d01e
+  if (!proto)
b9d01e
+    return GRUB_ERR_NOT_IMPLEMENTED_YET;
b9d01e
+
b9d01e
+  if (physaddr & 0xfff || size & 0xfff || size == 0 || attrs == NULL)
b9d01e
+    {
b9d01e
+      grub_dprintf ("nx", "%s called on 0x%"PRIxGRUB_ADDR"-0x%"PRIxGRUB_ADDR" and attrs %p\n",
b9d01e
+		    __func__, physaddr, physaddr+size-1, attrs);
b9d01e
+      return 0;
b9d01e
+    }
b9d01e
+
b9d01e
+  efi_status = efi_call_4(proto->get_memory_attributes,
b9d01e
+			  proto, physaddr, size, attrs);
b9d01e
+  *attrs = uefi_mem_attrs_to_grub_mem_attrs (*attrs);
b9d01e
+
b9d01e
+  return grub_efi_status_to_err (efi_status);
b9d01e
+}
b9d01e
+
b9d01e
+grub_err_t
b9d01e
+grub_update_mem_attrs (grub_addr_t addr, grub_size_t size,
b9d01e
+		       grub_uint64_t set_attrs, grub_uint64_t clear_attrs)
b9d01e
+{
b9d01e
+  grub_efi_memory_attribute_protocol_t *proto;
b9d01e
+  grub_efi_physical_address_t physaddr = addr;
b9d01e
+  grub_efi_guid_t protocol_guid = GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
b9d01e
+  grub_efi_status_t efi_status = GRUB_EFI_SUCCESS;
b9d01e
+  grub_uint64_t before = 0, after = 0, uefi_set_attrs, uefi_clear_attrs;
b9d01e
+  grub_err_t err;
b9d01e
+
b9d01e
+  proto = grub_efi_locate_protocol (&protocol_guid, 0);
b9d01e
+  if (!proto)
b9d01e
+    return GRUB_ERR_NONE;
b9d01e
+
b9d01e
+  err = grub_get_mem_attrs (addr, size, &before);
b9d01e
+  if (err)
b9d01e
+    grub_dprintf ("nx", "grub_get_mem_attrs(0x%"PRIxGRUB_ADDR", %"PRIuGRUB_SIZE", %p) -> 0x%x\n",
b9d01e
+		  addr, size, &before, err);
b9d01e
+
b9d01e
+  if (physaddr & 0xfff || size & 0xfff || size == 0)
b9d01e
+    {
b9d01e
+      grub_dprintf ("nx", "%s called on 0x%"PRIxGRUB_ADDR"-0x%"PRIxGRUB_ADDR" +%s%s%s -%s%s%s\n",
b9d01e
+		    __func__, physaddr, physaddr + size - 1,
b9d01e
+		    (set_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
b9d01e
+		    (set_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
b9d01e
+		    (set_attrs & GRUB_MEM_ATTR_X) ? "x" : "",
b9d01e
+		    (clear_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
b9d01e
+		    (clear_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
b9d01e
+		    (clear_attrs & GRUB_MEM_ATTR_X) ? "x" : "");
b9d01e
+      return 0;
b9d01e
+    }
b9d01e
+
b9d01e
+  uefi_set_attrs = grub_mem_attrs_to_uefi_mem_attrs (set_attrs);
b9d01e
+  grub_dprintf ("nx", "translating set_attrs from 0x%lx to 0x%lx\n", set_attrs, uefi_set_attrs);
b9d01e
+  uefi_clear_attrs = grub_mem_attrs_to_uefi_mem_attrs (clear_attrs);
b9d01e
+  grub_dprintf ("nx", "translating clear_attrs from 0x%lx to 0x%lx\n", clear_attrs, uefi_clear_attrs);
b9d01e
+  if (uefi_set_attrs)
b9d01e
+    efi_status = efi_call_4(proto->set_memory_attributes,
b9d01e
+			    proto, physaddr, size, uefi_set_attrs);
b9d01e
+  if (efi_status == GRUB_EFI_SUCCESS && uefi_clear_attrs)
b9d01e
+    efi_status = efi_call_4(proto->clear_memory_attributes,
b9d01e
+			    proto, physaddr, size, uefi_clear_attrs);
b9d01e
+
b9d01e
+  err = grub_get_mem_attrs (addr, size, &after);
b9d01e
+  if (err)
b9d01e
+    grub_dprintf ("nx", "grub_get_mem_attrs(0x%"PRIxGRUB_ADDR", %"PRIuGRUB_SIZE", %p) -> 0x%x\n",
b9d01e
+		  addr, size, &after, err);
b9d01e
+
b9d01e
+  grub_dprintf ("nx", "set +%s%s%s -%s%s%s on 0x%"PRIxGRUB_ADDR"-0x%"PRIxGRUB_ADDR" before:%c%c%c after:%c%c%c\n",
b9d01e
+		(set_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
b9d01e
+		(set_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
b9d01e
+		(set_attrs & GRUB_MEM_ATTR_X) ? "x" : "",
b9d01e
+		(clear_attrs & GRUB_MEM_ATTR_R) ? "r" : "",
b9d01e
+		(clear_attrs & GRUB_MEM_ATTR_W) ? "w" : "",
b9d01e
+		(clear_attrs & GRUB_MEM_ATTR_X) ? "x" : "",
b9d01e
+		addr, addr + size - 1,
b9d01e
+		(before & GRUB_MEM_ATTR_R) ? 'r' : '-',
b9d01e
+		(before & GRUB_MEM_ATTR_W) ? 'w' : '-',
b9d01e
+		(before & GRUB_MEM_ATTR_X) ? 'x' : '-',
b9d01e
+		(after & GRUB_MEM_ATTR_R) ? 'r' : '-',
b9d01e
+		(after & GRUB_MEM_ATTR_W) ? 'w' : '-',
b9d01e
+		(after & GRUB_MEM_ATTR_X) ? 'x' : '-');
b9d01e
+
b9d01e
+  return grub_efi_status_to_err (efi_status);
b9d01e
+}
b9d01e
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
b9d01e
index 2a243fd290..510a4030f5 100644
b9d01e
--- a/include/grub/efi/api.h
b9d01e
+++ b/include/grub/efi/api.h
b9d01e
@@ -354,6 +354,11 @@
b9d01e
     { 0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } \
b9d01e
    }
b9d01e
 
b9d01e
+#define GRUB_EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID \
b9d01e
+  { 0xf4560cf6, 0x40ec, 0x4b4a, \
b9d01e
+    { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \
b9d01e
+  }
b9d01e
+
b9d01e
 struct grub_efi_sal_system_table
b9d01e
 {
b9d01e
   grub_uint32_t signature;
b9d01e
@@ -2091,6 +2096,26 @@ struct grub_efi_rng_protocol
b9d01e
 };
b9d01e
 typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
b9d01e
 
b9d01e
+struct grub_efi_memory_attribute_protocol
b9d01e
+{
b9d01e
+  grub_efi_status_t (*get_memory_attributes) (
b9d01e
+			    struct grub_efi_memory_attribute_protocol *this,
b9d01e
+			    grub_efi_physical_address_t base_address,
b9d01e
+			    grub_efi_uint64_t length,
b9d01e
+			    grub_efi_uint64_t *attributes);
b9d01e
+  grub_efi_status_t (*set_memory_attributes) (
b9d01e
+			    struct grub_efi_memory_attribute_protocol *this,
b9d01e
+			    grub_efi_physical_address_t base_address,
b9d01e
+			    grub_efi_uint64_t length,
b9d01e
+			    grub_efi_uint64_t attributes);
b9d01e
+  grub_efi_status_t (*clear_memory_attributes) (
b9d01e
+			    struct grub_efi_memory_attribute_protocol *this,
b9d01e
+			    grub_efi_physical_address_t base_address,
b9d01e
+			    grub_efi_uint64_t length,
b9d01e
+			    grub_efi_uint64_t attributes);
b9d01e
+};
b9d01e
+typedef struct grub_efi_memory_attribute_protocol grub_efi_memory_attribute_protocol_t;
b9d01e
+
b9d01e
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
b9d01e
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__)
b9d01e
 
b9d01e
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
b9d01e
index 7af979b184..a635bcb0a9 100644
b9d01e
--- a/include/grub/efi/efi.h
b9d01e
+++ b/include/grub/efi/efi.h
b9d01e
@@ -159,4 +159,6 @@ struct grub_net_card;
b9d01e
 grub_efi_handle_t
b9d01e
 grub_efinet_get_device_handle (struct grub_net_card *card);
b9d01e
 
b9d01e
+grub_err_t EXPORT_FUNC(grub_efi_status_to_err) (grub_efi_status_t status);
b9d01e
+
b9d01e
 #endif /* ! GRUB_EFI_EFI_HEADER */
b9d01e
diff --git a/include/grub/mm.h b/include/grub/mm.h
b9d01e
index 9c38dd3ca5..d81623d226 100644
b9d01e
--- a/include/grub/mm.h
b9d01e
+++ b/include/grub/mm.h
b9d01e
@@ -22,6 +22,7 @@
b9d01e
 
b9d01e
 #include <grub/types.h>
b9d01e
 #include <grub/symbol.h>
b9d01e
+#include <grub/err.h>
b9d01e
 #include <config.h>
b9d01e
 
b9d01e
 #ifndef NULL
b9d01e
@@ -38,6 +39,37 @@ void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size);
b9d01e
 void *EXPORT_FUNC(grub_memalign) (grub_size_t align, grub_size_t size);
b9d01e
 #endif
b9d01e
 
b9d01e
+#define GRUB_MEM_ATTR_R	0x0000000000000004LLU
b9d01e
+#define GRUB_MEM_ATTR_W	0x0000000000000002LLU
b9d01e
+#define GRUB_MEM_ATTR_X	0x0000000000000001LLU
b9d01e
+
b9d01e
+#ifdef GRUB_MACHINE_EFI
b9d01e
+grub_err_t EXPORT_FUNC(grub_get_mem_attrs) (grub_addr_t addr,
b9d01e
+					    grub_size_t size,
b9d01e
+					    grub_uint64_t *attrs);
b9d01e
+grub_err_t EXPORT_FUNC(grub_update_mem_attrs) (grub_addr_t addr,
b9d01e
+					       grub_size_t size,
b9d01e
+					       grub_uint64_t set_attrs,
b9d01e
+					       grub_uint64_t clear_attrs);
b9d01e
+#else /* !GRUB_MACHINE_EFI */
b9d01e
+static inline grub_err_t
b9d01e
+grub_get_mem_attrs (grub_addr_t addr __attribute__((__unused__)),
b9d01e
+		    grub_size_t size __attribute__((__unused__)),
b9d01e
+		    grub_uint64_t *attrs __attribute__((__unused__)))
b9d01e
+{
b9d01e
+  return GRUB_ERR_NONE;
b9d01e
+}
b9d01e
+
b9d01e
+static inline grub_err_t
b9d01e
+grub_update_mem_attrs (grub_addr_t addr __attribute__((__unused__)),
b9d01e
+		       grub_size_t size __attribute__((__unused__)),
b9d01e
+		       grub_uint64_t set_attrs __attribute__((__unused__)),
b9d01e
+		       grub_uint64_t clear_attrs __attribute__((__unused__)))
b9d01e
+{
b9d01e
+  return GRUB_ERR_NONE;
b9d01e
+}
b9d01e
+#endif /* GRUB_MACHINE_EFI */
b9d01e
+
b9d01e
 void grub_mm_check_real (const char *file, int line);
b9d01e
 #define grub_mm_check() grub_mm_check_real (GRUB_FILE, __LINE__);
b9d01e