public inbox for linux-efi@vger.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-efi@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>
Subject: [PATCH] efi/zboot: Avoid CopyMem/SetMem EFI services after ExitBootServices
Date: Wed, 26 Feb 2025 13:52:16 +0100	[thread overview]
Message-ID: <20250226125215.2545828-2-ardb+git@google.com> (raw)

From: Ard Biesheuvel <ardb@kernel.org>

Given that memset/memcpy are intrinsics, the compiler might insert calls
to these routines unpredictably, including in code that executes after
ExitBootServices(). In this case, the respective boot services are no
longer accessible, and calling them will cause a crash.

So detect this case, and fall back to a bytewise copy/store if it occurs.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/intrinsics.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/efi/libstub/intrinsics.c b/drivers/firmware/efi/libstub/intrinsics.c
index 965e734f6f98..f26c19cf65fb 100644
--- a/drivers/firmware/efi/libstub/intrinsics.c
+++ b/drivers/firmware/efi/libstub/intrinsics.c
@@ -17,7 +17,12 @@ void *__memset(void *s, int c, size_t count) __alias(memset);
 
 void *memcpy(void *dst, const void *src, size_t len)
 {
-	efi_bs_call(copy_mem, dst, src, len);
+	if (efi_table_attr(efi_system_table, boottime) != NULL)
+		efi_bs_call(copy_mem, dst, src, len);
+	else
+		for (size_t i = 0; i < len; i++)
+			((u8 *)dst)[i] = ((u8 *)src)[i];
+
 	return dst;
 }
 
@@ -25,7 +30,12 @@ extern void *memmove(void *dst, const void *src, size_t len) __alias(memcpy);
 
 void *memset(void *dst, int c, size_t len)
 {
-	efi_bs_call(set_mem, dst, len, c & U8_MAX);
+	if (efi_table_attr(efi_system_table, boottime) != NULL)
+		efi_bs_call(set_mem, dst, len, c & U8_MAX);
+	else
+		for (u8 *d = dst; len--; d++)
+			*d = c;
+
 	return dst;
 }
 
-- 
2.48.1.658.g4767266eb4-goog


                 reply	other threads:[~2025-02-26 12:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20250226125215.2545828-2-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=linux-efi@vger.kernel.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