From: Kees Cook <keescook@chromium.org>
To: Jack Rosenthal <jrosenth@chromium.org>
Cc: Kees Cook <keescook@chromium.org>,
Paul Menzel <pmenzel@molgen.mpg.de>,
Guenter Roeck <groeck@chromium.org>,
Julius Werner <jwerner@chromium.org>,
Brian Norris <briannorris@chromium.org>,
Stephen Boyd <swboyd@chromium.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: [PATCH] firmware: coreboot: Check size of table entry and split memcpy
Date: Thu, 5 Jan 2023 20:53:31 -0800 [thread overview]
Message-ID: <20230106045327.never.413-kees@kernel.org> (raw)
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)
Reported-by: Paul Menzel <pmenzel@molgen.mpg.de>
Link: https://lore.kernel.org/all/03ae2704-8c30-f9f0-215b-7cdf4ad35a9a@molgen.mpg.de/
Cc: Jack Rosenthal <jrosenth@chromium.org>
Cc: Guenter Roeck <groeck@chromium.org>
Cc: Julius Werner <jwerner@chromium.org>
Cc: Brian Norris <briannorris@chromium.org>
Cc: Stephen Boyd <swboyd@chromium.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
drivers/firmware/google/coreboot_table.c | 8 +++++++-
drivers/firmware/google/coreboot_table.h | 1 +
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/google/coreboot_table.c b/drivers/firmware/google/coreboot_table.c
index 2652c396c423..102edfb4eb7b 100644
--- a/drivers/firmware/google/coreboot_table.c
+++ b/drivers/firmware/google/coreboot_table.c
@@ -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:
diff --git a/drivers/firmware/google/coreboot_table.h b/drivers/firmware/google/coreboot_table.h
index 37f4d335a606..2a2cea79204b 100644
--- a/drivers/firmware/google/coreboot_table.h
+++ b/drivers/firmware/google/coreboot_table.h
@@ -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 */
--
2.34.1
next reply other threads:[~2023-01-06 4:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-06 4:53 Kees Cook [this message]
2023-01-06 15:03 ` [PATCH] firmware: coreboot: Check size of table entry and split memcpy Julius Werner
2023-01-06 20:34 ` Kees Cook
2023-01-06 22:27 ` Julius Werner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230106045327.never.413-kees@kernel.org \
--to=keescook@chromium.org \
--cc=briannorris@chromium.org \
--cc=gregkh@linuxfoundation.org \
--cc=groeck@chromium.org \
--cc=jrosenth@chromium.org \
--cc=jwerner@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pmenzel@molgen.mpg.de \
--cc=swboyd@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox