[U-Boot] [PATCH 4/4] efi_loader: check map_key in ExitBootServices

Heinrich Schuchardt xypron.glpk at gmx.de
Mon Jul 2 10:53:55 UTC 2018


The UEFI spec requires that the memory map key is checked in
ExitBootServices().

Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
---
 include/efi_loader.h          | 3 +++
 lib/efi_loader/efi_boottime.c | 4 ++++
 lib/efi_loader/efi_memory.c   | 8 +++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 628dd801a8..0ad22eefa7 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -85,6 +85,9 @@ const char *__efi_nesting_dec(void);
 #define EFI_CACHELINE_SIZE 128
 #endif
 
+/* Key identifying current memory map */
+extern efi_uintn_t efi_memory_map_key;
+
 extern struct efi_runtime_services efi_runtime_services;
 extern struct efi_system_table systab;
 
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 396f28c570..105e80bb52 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -1837,6 +1837,10 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 
 	EFI_ENTRY("%p, %ld", image_handle, map_key);
 
+	/* Check that the caller has read the current memory map */
+	if (map_key != efi_memory_map_key)
+		return EFI_INVALID_PARAMETER;
+
 	/* Make sure that notification functions are not called anymore */
 	efi_tpl = TPL_HIGH_LEVEL;
 
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index bad8704269..967c3f733e 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -15,6 +15,8 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+efi_uintn_t efi_memory_map_key;
+
 struct efi_mem_list {
 	struct list_head link;
 	struct efi_mem_desc desc;
@@ -160,9 +162,13 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
 	debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
 	      start, pages, memory_type, overlap_only_ram ? "yes" : "no");
 
+	if (memory_type >= EFI_MAX_MEMORY_TYPE)
+		return EFI_INVALID_PARAMETER;
+
 	if (!pages)
 		return start;
 
+	++efi_memory_map_key;
 	newlist = calloc(1, sizeof(*newlist));
 	newlist->desc.type = memory_type;
 	newlist->desc.physical_start = start;
@@ -487,7 +493,7 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
 	}
 
 	if (map_key)
-		*map_key = 0;
+		*map_key = efi_memory_map_key;
 
 	return EFI_SUCCESS;
 }
-- 
2.18.0



More information about the U-Boot mailing list