All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] efi_loader: sections with zero VirtualSize
@ 2021-08-29 11:13 Heinrich Schuchardt
  2021-08-29 22:56 ` Asherah Connor
  0 siblings, 1 reply; 2+ messages in thread
From: Heinrich Schuchardt @ 2021-08-29 11:13 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Alexander Graf, Asherah Connor, u-boot, Heinrich Schuchardt

In a section header VirtualSize may be zero. This is for instance seen in
the .sbat section of shim. In this case use SizeOfRawData as section size.

Fixes: 9d30a941cce5 ("efi_loader: don't load beyond VirtualSize")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 lib/efi_loader/efi_image_loader.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index a0eb63fceb..838e3a7f02 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -800,6 +800,23 @@ efi_status_t efi_check_pe(void *buffer, size_t size, void **nt_header)
 	return EFI_SUCCESS;
 }
 
+/**
+ * section_size() - determine size of section
+ *
+ * The size of a section in memory if normally given by VirtualSize.
+ * If VirtualSize is not provided, use SizeOfRawData.
+ *
+ * @sec:	section header
+ * Return:	size of section in memory
+ */
+static u32 section_size(IMAGE_SECTION_HEADER *sec)
+{
+	if (sec->Misc.VirtualSize)
+		return sec->Misc.VirtualSize;
+	else
+		return sec->SizeOfRawData;
+}
+
 /**
  * efi_load_pe() - relocate EFI binary
  *
@@ -869,8 +886,9 @@ 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];
+
 		virt_size = max_t(unsigned long, virt_size,
-				  sec->VirtualAddress + sec->Misc.VirtualSize);
+				  sec->VirtualAddress + section_size(sec));
 	}
 
 	/* Read 32/64bit specific header bits */
@@ -931,11 +949,16 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
 	/* Load sections into RAM */
 	for (i = num_sections - 1; i >= 0; i--) {
 		IMAGE_SECTION_HEADER *sec = &sections[i];
-		memset(efi_reloc + sec->VirtualAddress, 0,
-		       sec->Misc.VirtualSize);
+		u32 copy_size = section_size(sec);
+
+		if (copy_size > sec->SizeOfRawData) {
+			copy_size = sec->SizeOfRawData;
+			memset(efi_reloc + sec->VirtualAddress, 0,
+			       sec->Misc.VirtualSize);
+		}
 		memcpy(efi_reloc + sec->VirtualAddress,
 		       efi + sec->PointerToRawData,
-		       min(sec->Misc.VirtualSize, sec->SizeOfRawData));
+		       copy_size);
 	}
 
 	/* Run through relocations */
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] efi_loader: sections with zero VirtualSize
  2021-08-29 11:13 [PATCH] efi_loader: sections with zero VirtualSize Heinrich Schuchardt
@ 2021-08-29 22:56 ` Asherah Connor
  0 siblings, 0 replies; 2+ messages in thread
From: Asherah Connor @ 2021-08-29 22:56 UTC (permalink / raw)
  To: Heinrich Schuchardt; +Cc: Heinrich Schuchardt, Alexander Graf, u-boot

On 21/08/29 01:08:p, Heinrich Schuchardt wrote:
> In a section header VirtualSize may be zero. This is for instance seen in
> the .sbat section of shim. In this case use SizeOfRawData as section size.
> 
> Fixes: 9d30a941cce5 ("efi_loader: don't load beyond VirtualSize")
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---
>  lib/efi_loader/efi_image_loader.c | 31 +++++++++++++++++++++++++++----
>  1 file changed, 27 insertions(+), 4 deletions(-)

Reviewed-by: Asherah Connor <ashe@kivikakk.ee>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-29 22:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-29 11:13 [PATCH] efi_loader: sections with zero VirtualSize Heinrich Schuchardt
2021-08-29 22:56 ` Asherah Connor

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.