[PATCH 2/4] efi: loader: map code-type page allocations executable
Ahmad Fatoum
a.fatoum at pengutronix.de
Mon Aug 24 00:29:52 PDT 2026
From: Ahmad Fatoum <a.fatoum at barebox.org>
PE images are loaded into EFI_LOADER_CODE pages allocated from
conventional memory, which mmu_remap_memory_banks() would map
non-executable. The Linux arm64 EFI stub additionally relocates the
kernel into an EFI_LOADER_CODE allocation of its own and jumps to it,
so fixing this up at StartImage time only would not be enough.
Drop the erroneous comment and map executable pages RWX before handing
them out and revert them to regular cached memory when they are freed.
Assisted-by: Claude:fable-5
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
efi/loader/boot.c | 2 --
efi/loader/memory.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/efi/loader/boot.c b/efi/loader/boot.c
index 53c8fc5ff938..d361a71a4dae 100644
--- a/efi/loader/boot.c
+++ b/efi/loader/boot.c
@@ -3132,8 +3132,6 @@ efi_status_t __efi_start_image(efi_handle_t image_handle,
pr_info("Starting EFI payload at %p\n", entry);
- // FIXME: we need the below if we enable CONFIG_ARM_MMU_PERMISSIONS...
- // remap_range(info->image_base, info->image_size, MAP_ARCH(2));
sync_caches_for_execution();
efi_set_variable_usec("LoaderTimeExecUSec", &efi_systemd_vendor_guid,
diff --git a/efi/loader/memory.c b/efi/loader/memory.c
index 8931cd1bab52..d35f124d381e 100644
--- a/efi/loader/memory.c
+++ b/efi/loader/memory.c
@@ -7,14 +7,35 @@
#include <linux/sprintf.h>
#include <efi/loader.h>
#include <efi/error.h>
+#include <efi/mode.h>
#include <init.h>
#include <memory.h>
+#include <mmu.h>
#include <linux/list_sort.h>
#include <linux/sizes.h>
#include <dma.h>
efi_uintn_t efi_memory_map_key;
+/*
+ * EFI images and the code pages they allocate expect to be mapped RWX,
+ * matching the attributes we advertise in the memory map for the code
+ * memory types. The page tables set up from the ELF segments only cover
+ * the barebox image itself, so allocations from conventional memory are
+ * mapped non-executable when ARM_MMU_PERMISSIONS is enabled.
+ */
+static void efi_remap_pages(u64 addr, size_t size, maptype_t map_type)
+{
+ if (!arch_can_remap())
+ return;
+
+ /* EFI_ALLOCATE_ADDRESS may pass through an unaligned address */
+ if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(size, PAGE_SIZE))
+ return;
+
+ remap_range((void *)(uintptr_t)addr, size, map_type);
+}
+
static efi_status_t find_pages_max(struct list_head *banks, size_t npages, size_t *page)
{
struct memory_bank *bank;
@@ -199,6 +220,12 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type,
res->flags |= IORESOURCE_EFI_ALLOC;
+ if (memory_type == EFI_LOADER_CODE ||
+ memory_type == EFI_BOOT_SERVICES_CODE ||
+ memory_type == EFI_RUNTIME_SERVICES_CODE)
+ efi_remap_pages(new_addr, npages << EFI_PAGE_SHIFT,
+ MAP_CACHED_RWX);
+
*memory = new_addr;
return EFI_SUCCESS;
}
@@ -301,6 +328,9 @@ efi_status_t efi_free_pages(uint64_t memory, size_t pages)
if (nfreed <= 0)
return EFI_INVALID_PARAMETER;
+ /* Revert a possible executable mapping of code-type allocations */
+ efi_remap_pages(memory, size, MAP_CACHED);
+
++efi_memory_map_key;
return EFI_SUCCESS;
--
2.47.3
More information about the barebox
mailing list