REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1999

When PE loader loads image to memory, the first section of image may
not locate right next to the image header, which causes some memory
space remaining uninitialized. This is a security issue.
This patch compares the ending address of image header and the beginning
address of the first section. If there is a gap, zero out this gap.

Cc: Jian J Wang <jian.j.w...@intel.com>
Cc: Hao A Wu <hao.a...@intel.com>
Signed-off-by: Zhiguang Liu <zhiguang....@intel.com>
---
 MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c 
b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 07bb62f860..2cdfb4a082 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -1306,6 +1306,14 @@ PeCoffLoaderLoadImage (
   // Load each section of the image
   //
   Section = FirstSection;
+  //
+  // Zero out the memory space between image header and the first section
+  //
+  End  = (CHAR8 *)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders);
+  Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress, 
TeStrippedOffset);
+  if (End < Base) {
+    ZeroMem (End, Base - End);
+  }
   for (Index = 0; Index < NumberOfSections; Index++) {
     //
     // Read the section
-- 
2.16.2.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#53185): https://edk2.groups.io/g/devel/message/53185
Mute This Topic: https://groups.io/mt/69664451/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to