All of lore.kernel.org
 help / color / mirror / Atom feed
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 03/11] efi_loader: reject a PE section whose end wraps around
Date: Fri, 06 Nov 2026 21:56:38 +0000	[thread overview]
Message-ID: <40a76498-7b85-45f0-8ec0-ae3ec0d5043a@gmail.com> (raw)
In-Reply-To: <dacef858-56d7-4708-b784-16b0133bb6aa@gmail.com>

From: Ana Bobillo <anabobillo3@gmail.com>

efi_load_pe() computes the end of each section as the sum of its
VirtualAddress and its size, both u32, so the sum wraps around before it
is widened to unsigned long. A section near 4 GiB then yields a small
virt_size, and the memset() and memcpy() of the section write far past
the allocated image.

Relative virtual addresses are 32 bit, so reject a section whose end
does not fit into 32 bits. barebox, whose EFI loader is based on this
file, fixed the same in b6598389d46d ("efi: loader: fix integer overflow
in PE virt_size calculation").

This runs before the image is rejected when Secure Boot fails to verify
it.

Fixes: cb149c66342c ("efi_loader: Add PE image loader")
Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 lib/efi_loader/efi_image_loader.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index 82083cb73d9..b21947e979a 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -1048,9 +1048,21 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
 	/* Calculate upper virtual address boundary */
 	for (i = num_sections - 1; i >= 0; i--) {
 		IMAGE_SECTION_HEADER *sec = &sections[i];
+		u32 end;
 
-		virt_size = max_t(unsigned long, virt_size,
-				  sec->VirtualAddress + section_size(sec));
+		/*
+		 * VirtualAddress and section_size() are both u32, so their sum
+		 * wraps around at 4 GiB, on 64bit systems too. A wrapped end
+		 * would make virt_size too small for the memset() and memcpy()
+		 * below.
+		 */
+		if (__builtin_add_overflow(sec->VirtualAddress,
+					   section_size(sec), &end)) {
+			log_err("Section %d virtual address overflow\n", i);
+			return EFI_LOAD_ERROR;
+		}
+
+		virt_size = max_t(unsigned long, virt_size, end);
 	}
 
 	/* Read 32/64bit specific header bits */

  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 ` Ana Bobillo [this message]
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 ` [PATCH v3 05/11] efi_loader: make room for the PE headers in the loaded image Ana Bobillo
2026-11-06 21:56 ` [PATCH v3 06/11] efi_loader: check the relocation table against the image size 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=40a76498-7b85-45f0-8ec0-ae3ec0d5043a@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.