Skip to contentSkip to conversation

efi/zboot: Avoid CopyMem/SetMem EFI services after ExitBootServices

PATCH Superseded Ard Biesheuvel 1 participant 1 message Feb 26, 2025 EFI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi/zboot: Avoid CopyMem/SetMem EFI services after ExitBootServices

From: Ard Biesheuvel <[email protected]>

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.

---
 drivers/firmware/efi/libstub/intrinsics.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/firmware/efi/libstub/intrinsics.c +12 -2
index 965e734f6f98..f26c19cf65fb 100644
@@ -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;
 }
 
Signature · 1 line
2.48.1.658.g4767266eb4-goog