public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zhiguang Liu" <zhiguang.liu@intel.com>
To: devel@edk2.groups.io
Cc: Jian J Wang <jian.j.wang@intel.com>, Hao A Wu <hao.a.wu@intel.com>
Subject: [PATCH] MdePkg: PE loader should zero out dest buffer on allocation
Date: Mon, 13 Jan 2020 16:18:53 +0800	[thread overview]
Message-ID: <20200113081854.9732-1-zhiguang.liu@intel.com> (raw)

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.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Signed-off-by: Zhiguang Liu <zhiguang.liu@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


             reply	other threads:[~2020-01-13  8:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-13  8:18 Zhiguang Liu [this message]
2020-01-13  9:09 ` [edk2-devel] [PATCH] MdePkg: PE loader should zero out dest buffer on allocation Marvin Häuser
2020-01-13 17:58 ` Laszlo Ersek

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-list 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=20200113081854.9732-1-zhiguang.liu@intel.com \
    --to=devel@edk2.groups.io \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox