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>,
Tom Rini <trini@konsulko.com>
Subject: [PATCH 06/10] efi_loader: check the relocation table against the image size
Date: Fri, 30 Oct 2026 06:14:11 +0000 [thread overview]
Message-ID: <22a9b067-d08c-4fb2-a4de-46dfc6226ada@gmail.com> (raw)
In-Reply-To: <633dddd6-ee22-4cd6-a8b6-cb7df0650fae@gmail.com>
From: Ana Bobillo <anabobillo3@gmail.com>
efi_loader_relocate() checks each relocation block and entry against the
end of the relocation table and each target against the image size, but
the table itself is taken from the base relocation data directory as it
is: its address and size are not checked against the image, so the
blocks are read from wherever they point, up to 4 GiB past the image.
The directory is also read when NumberOfRvaAndSizes says the image has
none, in which case its bytes belong to the section table.
Use the base relocation directory only if the image has one, and reject
a relocation table that does not lie within the image, as EDK2's
PeCoffLoaderRelocateImage() does.
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>
---
lib/efi_loader/efi_image_loader.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index f9cd2cb274c..c0677b440e8 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -117,9 +117,17 @@ static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
unsigned long virt_size)
{
unsigned long delta = (unsigned long)efi_reloc - pref_address;
+ unsigned long rel_off = (const char *)rel - (const char *)efi_reloc;
const IMAGE_BASE_RELOCATION *end;
int i;
+ /* The relocation table must lie within the image */
+ if (rel_size &&
+ (rel_off > virt_size || rel_size > virt_size - rel_off)) {
+ log_debug("Relocation table exceeds image size\n");
+ return EFI_LOAD_ERROR;
+ }
+
if (delta == 0)
return EFI_SUCCESS;
@@ -1111,7 +1119,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
goto err;
}
handle->entry = efi_reloc + opt->AddressOfEntryPoint;
- rel_size = opt->DataDirectory[rel_idx].Size;
+ rel_size = opt->NumberOfRvaAndSizes > rel_idx ?
+ opt->DataDirectory[rel_idx].Size : 0;
rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
} else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
@@ -1127,7 +1136,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
goto err;
}
handle->entry = efi_reloc + opt->AddressOfEntryPoint;
- rel_size = opt->DataDirectory[rel_idx].Size;
+ rel_size = opt->NumberOfRvaAndSizes > rel_idx ?
+ opt->DataDirectory[rel_idx].Size : 0;
rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
} else {
log_err("Invalid optional header magic %x\n",
next prev parent reply other threads:[~2026-10-30 6:14 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 6:13 [PATCH 00/10] efi_loader: check PE images before using them Ana Bobillo
2026-10-30 6:13 ` [PATCH 01/10] efi_loader: check the NT header size in efi_check_pe() Ana Bobillo
2026-10-30 7:23 ` Ilias Apalodimas
2026-10-30 11:15 ` Heinrich Schuchardt
2026-10-30 11:40 ` Ana Bobillo
2026-10-30 6:13 ` [PATCH 02/10] efi_loader: check the PE image ranges in efi_image_parse() Ana Bobillo
2026-10-30 12:14 ` Heinrich Schuchardt
2026-10-30 6:13 ` [PATCH 03/10] efi_loader: reject a PE section whose end wraps around Ana Bobillo
2026-10-30 7:41 ` Ilias Apalodimas
2026-10-30 12:35 ` Heinrich Schuchardt
2026-10-30 6:14 ` [PATCH 04/10] efi_loader: check PE section raw data against the image size Ana Bobillo
2026-10-30 6:14 ` [PATCH 05/10] efi_loader: check that the PE headers fit into the loaded image Ana Bobillo
2026-10-30 6:14 ` Ana Bobillo [this message]
2026-10-30 6:14 ` [PATCH 07/10] efi_loader: check certificate lengths in efi_image_authenticate() Ana Bobillo
2026-10-30 6:14 ` [PATCH 08/10] efi_loader: do not load an image that cannot be measured Ana Bobillo
2026-10-30 7:22 ` Ilias Apalodimas
2026-10-30 7:54 ` Ana Bobillo
2026-10-30 9:23 ` Ilias Apalodimas
2026-10-30 10:04 ` Ana Bobillo
2026-10-30 6:14 ` [PATCH 09/10] test: lib: add tests for the PE image checks Ana Bobillo
2026-10-30 6:14 ` [PATCH 10/10] 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=22a9b067-d08c-4fb2-a4de-46dfc6226ada@gmail.com \
--to=anabobillo3@gmail.com \
--cc=ilias.apalodimas@linaro.org \
--cc=trini@konsulko.com \
--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.