From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 2/4] efi: loader: map code-type page allocations executable
Date: Mon, 24 Aug 2026 09:29:52 +0200 [thread overview]
Message-ID: <20260824073005.3267576-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260824073005.3267576-1-a.fatoum@pengutronix.de>
From: Ahmad Fatoum <a.fatoum@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@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
next prev parent reply other threads:[~2026-08-24 7:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 7:29 [PATCH 1/4] ARM: lds: place EFI runtime code and data in separate PT_LOAD segments Ahmad Fatoum
2026-08-24 7:29 ` Ahmad Fatoum [this message]
2026-08-24 7:29 ` [PATCH 3/4] ARM: allow CONFIG_ARM_MMU_PERMISSIONS together with EFI_RUNTIME Ahmad Fatoum
2026-08-24 7:29 ` [PATCH 4/4] test: py: efiloader: test EFI ResetSystem from booted Linux Ahmad Fatoum
2026-08-24 12:28 ` [PATCH 1/4] ARM: lds: place EFI runtime code and data in separate PT_LOAD segments Sascha Hauer
2026-08-25 13:49 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824073005.3267576-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox