Skip to contentSkip to conversation

efi: Replace efi_memory_attributes_table_t 0-sized array with flexible array

PATCH In review Kees Cook 2 participants 4 messages Jul 11, 2024 – Jul 12, 2024 EFI

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

v2 cover efi: Replace efi_memory_attributes_table_t 0-sized array with flexible array

Hi,

Update efi_memory_attributes_table_t to use a proper flexible array. Renames efi_early_memdesc_ptr() to efi_memdesc_ptr() so it more accurately reflects its usage.

Thanks!

-Kees

v2:
- rename efi_early_memdesc_ptr() to efi_memdesc_ptr()
- add comment about reading efi_memdesc_ptr() to efi_memory_attributes_table_t
v1: https://lore.kernel.org/lkml/[email protected]/

Kees Cook (2):
efi: Rename efi_early_memdesc_ptr() to efi_memdesc_ptr()
efi: Replace efi_memory_attributes_table_t 0-sized array with flexible
array

 drivers/firmware/efi/libstub/relocate.c          |  2 +-
 drivers/firmware/efi/libstub/unaccepted_memory.c |  2 +-
 drivers/firmware/efi/libstub/x86-stub.c          |  2 +-
 drivers/firmware/efi/memattr.c                   |  2 +-
 include/linux/efi.h                              | 10 +++++++---
 5 files changed, 11 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 efi: Rename efi_early_memdesc_ptr() to efi_memdesc_ptr()

The "early" part of the helper's name isn't accurate[1]. Drop it in preparation for adding a new (not early) usage.

---
Cc: Ard Biesheuvel <[email protected]>
Cc: "Kirill A. Shutemov" <[email protected]>
Cc: "Borislav Petkov (AMD)" <[email protected]>
Cc: Baskov Evgeniy <[email protected]>
Cc: [email protected]

---
 drivers/firmware/efi/libstub/relocate.c          | 2 +-
 drivers/firmware/efi/libstub/unaccepted_memory.c | 2 +-
 drivers/firmware/efi/libstub/x86-stub.c          | 2 +-
 include/linux/efi.h                              | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/relocate.c +1 -1
index bf6fbd5d22a1..d694bcfa1074 100644
@@ -48,7 +48,7 @@ efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
 		unsigned long m = (unsigned long)map->map;
 		u64 start, end;
 
-		desc = efi_early_memdesc_ptr(m, map->desc_size, i);
+		desc = efi_memdesc_ptr(m, map->desc_size, i);
 
 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
 			continue;
drivers/firmware/efi/libstub/unaccepted_memory.c +1 -1
index 9a655f30ba47..c295ea3a6efc 100644
@@ -29,7 +29,7 @@ efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
 		efi_memory_desc_t *d;
 		unsigned long m = (unsigned long)map->map;
 
-		d = efi_early_memdesc_ptr(m, map->desc_size, i);
+		d = efi_memdesc_ptr(m, map->desc_size, i);
 		if (d->type != EFI_UNACCEPTED_MEMORY)
 			continue;
 
drivers/firmware/efi/libstub/x86-stub.c +1 -1
index 1983fd3bf392..8d94484f3a20 100644
@@ -555,7 +555,7 @@ setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_s
 		m |= (u64)efi->efi_memmap_hi << 32;
 #endif
 
-		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
+		d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i);
 		switch (d->type) {
 		case EFI_RESERVED_TYPE:
 		case EFI_RUNTIME_SERVICES_CODE:
include/linux/efi.h +2 -2
index 418e555459da..fbdfcc9a81e6 100644
@@ -783,7 +783,7 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
 					 efi_memattr_perm_setter fn);
 
 /*
- * efi_early_memdesc_ptr - get the n-th EFI memmap descriptor
+ * efi_memdesc_ptr - get the n-th EFI memmap descriptor
  * @map: the start of efi memmap
  * @desc_size: the size of space for each EFI memmap descriptor
  * @n: the index of efi memmap descriptor
@@ -801,7 +801,7 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
  * during bootup since for_each_efi_memory_desc_xxx() is available after the
  * kernel initializes the EFI subsystem to set up struct efi_memory_map.
  */
-#define efi_early_memdesc_ptr(map, desc_size, n)			\
+#define efi_memdesc_ptr(map, desc_size, n)			\
 	(efi_memory_desc_t *)((void *)(map) + ((n) * (desc_size)))
 
 /* Iterate through an efi_memory_map */
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/2 efi: Replace efi_memory_attributes_table_t 0-sized array with flexible array

In reply to Kees Cook

While efi_memory_attributes_table_t::entry isn't used directly as an array, it is used as a base for pointer arithmetic. The type is wrong as it's not technically an array of efi_memory_desc_t's; they could be larger. Regardless, leave the type unchanged and remove the old style "0" array size. Additionally replace the open-coded entry offset code with the existing efi_memdesc_ptr() helper.

---
Cc: Ard Biesheuvel <[email protected]>
Cc: [email protected]

---
 drivers/firmware/efi/memattr.c | 2 +-
 include/linux/efi.h            | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/firmware/efi/memattr.c +1 -1
index ab85bf8e165a..164203429fa7 100644
@@ -164,7 +164,7 @@ int __init efi_memattr_apply_permissions(struct mm_struct *mm,
 		bool valid;
 		char buf[64];
 
-		valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
+		valid = entry_is_valid(efi_memdesc_ptr(tbl->entry, tbl->desc_size, i),
 				       &md);
 		size = md.num_pages << EFI_PAGE_SHIFT;
 		if (efi_enabled(EFI_DBG) || !valid)
include/linux/efi.h +5 -1
index fbdfcc9a81e6..40475d77231c 100644
@@ -607,7 +607,11 @@ typedef struct {
 	u32 num_entries;
 	u32 desc_size;
 	u32 flags;
-	efi_memory_desc_t entry[0];
+	/*
+	 * There are @num_entries following, each of size @desc_size bytes,
+	 * including an efi_memory_desc_t header. See efi_memdesc_ptr().
+	 */
+	efi_memory_desc_t entry[];
 } efi_memory_attributes_table_t;
 
 typedef struct {
Signature · 1 line
2.34.1
maintainer
#3 lore raw
In reply to Kees Cook
Quoted 16 lines · Kees Cook

Hi,

Update efi_memory_attributes_table_t to use a proper flexible array. Renames efi_early_memdesc_ptr() to efi_memdesc_ptr() so it more accurately reflects its usage.

Thanks!

-Kees

v2:
- rename efi_early_memdesc_ptr() to efi_memdesc_ptr()
- add comment about reading efi_memdesc_ptr() to efi_memory_attributes_table_t
v1: https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

Kees Cook (2):
efi: Rename efi_early_memdesc_ptr() to efi_memdesc_ptr()
efi: Replace efi_memory_attributes_table_t 0-sized array with flexible
array

Thanks, I've queued this up now.