From: Ana Bobillo <anabobillo3@gmail.com>
To: u-boot@lists.u-boot-project.org
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>
Subject: [PATCH v3 05/11] efi_loader: make room for the PE headers in the loaded image
Date: Fri, 06 Nov 2026 21:56:50 +0000 [thread overview]
Message-ID: <f459c343-e217-4152-84b7-e7d907d03ae2@gmail.com> (raw)
In-Reply-To: <dacef858-56d7-4708-b784-16b0133bb6aa@gmail.com>
From: Ana Bobillo <anabobillo3@gmail.com>
efi_load_pe() copies the PE headers to the start of the allocated image.
The size it copies is not the size of the headers: it adds the size of a
whole 32bit NT header, which already holds an optional header, to
SizeOfOptionalHeader, and assumes that the NT header follows the DOS
header directly. Nor is it checked against the size of the allocation,
which only covers the sections. An image with many empty section
headers but little section data makes the copy write past the
allocation, and the copy reads past the end of a small image.
Copy the headers up to the end of the section table, which is already
checked to lie within the image, and allocate at least that much.
This runs before the image is rejected when Secure Boot fails to verify
it.
Fixes: 8458bf648fd7 ("efi_loader: set image_base and image_size to correct values")
Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
---
lib/efi_loader/efi_image_loader.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index c6e7d5fc599..a9e7fa0a74f 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -996,7 +996,6 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
struct efi_loaded_image *loaded_image_info)
{
IMAGE_NT_HEADERS32 *nt;
- IMAGE_DOS_HEADER *dos;
IMAGE_SECTION_HEADER *sections;
int num_sections;
void *efi_reloc;
@@ -1006,6 +1005,7 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
uint64_t image_base;
unsigned long virt_size = 0;
+ size_t hdr_size;
int supported = 0;
efi_status_t ret;
@@ -1072,6 +1072,13 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
virt_size = max_t(unsigned long, virt_size, end);
}
+ /*
+ * The PE headers, up to the end of the section table, are copied to
+ * the start of the loaded image: make room for them.
+ */
+ hdr_size = (void *)(sections + num_sections) - efi;
+ virt_size = max_t(unsigned long, virt_size, hdr_size);
+
/* Read 32/64bit specific header bits */
if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
@@ -1128,11 +1135,7 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
#endif
/* Copy PE headers */
- memcpy(efi_reloc, efi,
- sizeof(*dos)
- + sizeof(*nt)
- + nt->FileHeader.SizeOfOptionalHeader
- + num_sections * sizeof(IMAGE_SECTION_HEADER));
+ memcpy(efi_reloc, efi, hdr_size);
/* Load sections into RAM */
for (i = num_sections - 1; i >= 0; i--) {
next prev parent reply other threads:[~2026-11-06 21:56 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 21:56 [PATCH v3 00/11] efi_loader: check PE images before using them Ana Bobillo
2026-11-06 21:56 ` [PATCH v3 01/11] efi_loader: check the NT header size in efi_check_pe() Ana Bobillo
2026-11-06 21:56 ` [PATCH v3 02/11] efi_loader: check the PE image ranges in efi_image_parse() Ana Bobillo
2026-11-06 21:56 ` [PATCH v3 03/11] efi_loader: reject a PE section whose end wraps around Ana Bobillo
2026-11-06 21:56 ` [PATCH v3 04/11] efi_loader: check PE section raw data against the image size Ana Bobillo
2026-11-06 21:56 ` Ana Bobillo [this message]
2026-11-06 21:56 ` [PATCH v3 06/11] efi_loader: check the relocation table " Ana Bobillo
2026-11-06 21:57 ` [PATCH v3 07/11] efi_loader: check certificate lengths in efi_image_authenticate() Ana Bobillo
2026-11-06 21:57 ` [PATCH v3 08/11] efi_loader: refuse a PE image of an unknown subsystem Ana Bobillo
2026-11-06 21:57 ` [PATCH v3 09/11] efi_loader: do not start an image that cannot be measured Ana Bobillo
2026-11-06 21:57 ` [PATCH v3 10/11] test: lib: add tests for the PE image checks Ana Bobillo
2026-11-06 21:57 ` [PATCH v3 11/11] test: py: test invalid certificate lengths in signed images Ana Bobillo
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=f459c343-e217-4152-84b7-e7d907d03ae2@gmail.com \
--to=anabobillo3@gmail.com \
--cc=ilias.apalodimas@linaro.org \
--cc=u-boot@lists.u-boot-project.org \
--cc=xypron.glpk@gmx.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.