Skip to contentSkip to conversation

efistub/x86: Use struct_size() for setup_data sizes

PATCH New Thorsten Blum 1 participant 2 messages Sep 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 efistub/x86: Use struct_size() for setup_data sizes

Use struct_size(), which provides additional compile-time checks for structures with flexible array members (e.g., __must_be_array()), to calculate struct setup_data sizes.

---
 drivers/firmware/efi/libstub/x86-stub.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/x86-stub.c +5 -5
index dbfbe8a74ab2..d399ab9aa8f3 100644
@@ -7,6 +7,7 @@
  * ----------------------------------------------------------------------- */
 
 #include <linux/efi.h>
+#include <linux/overflow.h>
 #include <linux/pci.h>
 #include <linux/stddef.h>
 
@@ -176,7 +177,7 @@ static void retrieve_apple_device_properties(struct boot_params *boot_params)
 
 	do {
 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
-				     size + sizeof(struct setup_data),
+				     struct_size(new, data, size),
 				     (void **)&new);
 		if (status != EFI_SUCCESS) {
 			efi_err("Failed to allocate memory for 'properties'\n");
@@ -649,8 +650,8 @@ setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_s
 		}
 
 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
-			u32 need = (nr_desc - i) * sizeof(struct boot_e820_entry) +
-				   sizeof(struct setup_data);
+			u32 need = struct_size(e820ext, data,
+				(nr_desc - i) * sizeof(struct boot_e820_entry));
 
 			if (!e820ext || e820ext_size < need)
 				return EFI_BUFFER_TOO_SMALL;
@@ -684,8 +685,7 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
 	efi_status_t status;
 	unsigned long size;
 
-	size = sizeof(struct setup_data) +
-		sizeof(struct boot_e820_entry) * nr_desc;
+	size = struct_size(*e820ext, data, nr_desc * sizeof(struct boot_e820_entry));
 
 	if (*e820ext) {
 		efi_bs_call(free_pool, *e820ext);
#1 lore raw

PATCH 2/2 x86/setup: Use struct_size() for setup_data sizes

Use struct_size(), which provides additional compile-time checks for structures with flexible array members (e.g., __must_be_array()), to calculate struct setup_data sizes.

---
 arch/x86/kernel/setup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +3 -2
index cda6adb9f69c..20facddee922 100644
@@ -19,6 +19,7 @@
 #include <linux/iscsi_ibft.h>
 #include <linux/kexec_handover.h>
 #include <linux/memblock.h>
+#include <linux/overflow.h>
 #include <linux/panic_notifier.h>
 #include <linux/pci.h>
 #include <linux/random.h>
@@ -487,7 +488,7 @@ static void __init parse_setup_data(void)
 		u32 data_len, data_type;
 
 		data = early_memremap(pa_data, sizeof(*data));
-		data_len = data->len + sizeof(struct setup_data);
+		data_len = struct_size(data, data, data->len);
 		data_type = data->type;
 		pa_next = data->next;
 		early_memunmap(data, sizeof(*data));
@@ -584,7 +585,7 @@ static void __init memblock_x86_reserve_range_setup_data(void)
 		len = sizeof(*data);
 		pa_next = data->next;
 
-		memblock_reserve_kern(pa_data, sizeof(*data) + data->len);
+		memblock_reserve_kern(pa_data, struct_size(data, data, data->len));
 
 		if (data->type == SETUP_INDIRECT) {
 			len += data->len;