firmware: coreboot: Check size of table entry and split memcpy
PATCH 1/1 firmware: coreboot: Check size of table entry and split memcpy
The memcpy() of the data following a coreboot_table_entry couldn't be evaluated by the compiler under CONFIG_FORTIFY_SOURCE. To make it easier to reason about, add an explicit flexible array member to struct coreboot_table_entry, and validate the sizes before copying. Avoids this run-time false positive warning:
memcpy: detected field-spanning write (size 168) of single field "&device->entry" at drivers/firmware/google/coreboot_table.c:103 (size 8)
--- drivers/firmware/google/coreboot_table.c | 8 +++++++- drivers/firmware/google/coreboot_table.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/firmware/google/coreboot_table.c +7 -1
@@ -93,6 +93,11 @@ static int coreboot_table_populate(struct device *dev, void *ptr)for (i =0 ; i < header->table_entries; i++) { entry = ptr_entry; +if (entry->size <sizeof (*entry)) { +dev_warn (dev,"coreboot table entry too small!\n" ); +return -EINVAL ; + } + device =kzalloc (sizeof (struct device ) + entry->size,GFP_KERNEL );if (!device)return -ENOMEM ;
@@ -100,7 +105,8 @@ static int coreboot_table_populate(struct device *dev, void *ptr) device->dev.parent = dev; device->dev.bus = &coreboot_bus_type; device->dev.release = coreboot_device_release; -memcpy (&device->entry, ptr_entry, entry->size); + device->entry = *entry; +memcpy (device->entry.data, entry->data, entry->size -sizeof (*entry));switch (device->entry.tag) {case LB_TAG_CBMEM_ENTRY :
drivers/firmware/google/coreboot_table.h +1 -0
@@ -29,6 +29,7 @@ struct coreboot_table_header {struct coreboot_table_entry {u32 tag;u32 size; +u8 data[];/* Size here is: "size - (sizeof(u32) * 2)" */ };/* Points to a CBMEM entry */
Signature · 1 line
2.34.1
Have you considered adding the flexible array member directly to the union in struct coreboot_device instead? I think that would make this a bit simpler by not having to copy header and data portion separately.
Quoted 4 lines · Julius Werner
Have you considered adding the flexible array member directly to the union in struct
coreboot_deviceinstead? I think that would make this a bit simpler by not having to copy header and data portion separately.
Are you thinking something like this?
drivers/firmware/google/coreboot_table.c +7 -2
@@ -93,14 +93,19 @@ static int coreboot_table_populate(struct device *dev, void *ptr)for (i =0 ; i < header->table_entries; i++) { entry = ptr_entry; - device =kzalloc (sizeof (struct device ) + entry->size,GFP_KERNEL ); +if (entry->size <sizeof (*entry)) { +dev_warn (dev,"coreboot table entry too small!\n" ); +return -EINVAL ; + } + + device =kzalloc (sizeof (device->dev) + entry->size,GFP_KERNEL );if (!device)return -ENOMEM ; device->dev.parent = dev; device->dev.bus = &coreboot_bus_type; device->dev.release = coreboot_device_release; -memcpy (&device->entry, ptr_entry, entry->size); +memcpy (device->raw, entry, entry->size);switch (device->entry.tag) {case LB_TAG_CBMEM_ENTRY :
drivers/firmware/google/coreboot_table.h +1 -0
@@ -79,6 +79,7 @@ struct coreboot_device {struct lb_cbmem_ref cbmem_ref;struct lb_cbmem_entry cbmem_entry;struct lb_framebuffer framebuffer; +DECLARE_FLEX_ARRAY (u8, raw); }; };
Signature · 1 line
Kees Cook
Yeah, exactly. Isn't that a bit simpler? (I wasn't aware of the flexible array member in union issue, but it sounds like we have a neat solution for that already.)
Quoted 51 lines · Kees Cook
Quoted 4 lines · Julius Werner
Have you considered adding the flexible array member directly to the union in struct
coreboot_deviceinstead? I think that would make this a bit simpler by not having to copy header and data portion separately.Are you thinking something like this?
drivers/firmware/google/coreboot_table.c +7
-2@@ -93,14 +93,19 @@ static int coreboot_table_populate(struct device *dev, void *ptr)for (i =0 ; i < header->table_entries; i++) { entry = ptr_entry; - device =kzalloc (sizeof (struct device ) + entry->size,GFP_KERNEL ); +if (entry->size <sizeof (*entry)) { +dev_warn (dev,"coreboot table entry too small!\n" ); +return -EINVAL ; + } + + device =kzalloc (sizeof (device->dev) + entry->size,GFP_KERNEL );if (!device)return -ENOMEM ; device->dev.parent = dev; device->dev.bus = &coreboot_bus_type; device->dev.release = coreboot_device_release; -memcpy (&device->entry, ptr_entry, entry->size); +memcpy (device->raw, entry, entry->size);switch (device->entry.tag) {case LB_TAG_CBMEM_ENTRY :drivers/firmware/google/coreboot_table.h +1
-1@@ -79,6 +79,7 @@ struct coreboot_device {struct lb_cbmem_ref cbmem_ref;struct lb_cbmem_entry cbmem_entry;struct lb_framebuffer framebuffer; +DECLARE_FLEX_ARRAY (u8, raw); }; }; --Kees Cook