public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Neo Hsueh <hong-chihx.hsueh@intel.com>
To: edk2-devel@lists.01.org
Cc: Michael D Kinney <michael.d.kinney@intel.com>,
	Liming Gao <liming.gao@intel.com>,
	Dandan Bi <dandan.bi@intel.com>
Subject: [PATCH] MdePkg/BasePeCoffLib: skip runtime relocation if relocation info is invalid.
Date: Thu, 24 Jan 2019 15:18:19 -0800	[thread overview]
Message-ID: <20190124231819.2352-1-hong-chihx.hsueh@intel.com> (raw)

1.Skip runtime relocation for PE images that provide invalid relocation
  infomation (ex: RelocDir->Size = 0) to fix a hang observed while booting
  Windows.
2.Add a magic number check for PE32+ image.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Neo Hsueh <hong-chihx.hsueh@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
---
 MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 1bd079ad6a..6477ef0759 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -1725,11 +1725,18 @@ PeCoffLoaderRelocateImageForRuntime (
     NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
     DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);
   } else {
+    if (Hdr.Pe32Plus->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
+      //
+      // Use PE32+ offset
+      //
+      NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
+      DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);
+    } else {
     //
-    // Use PE32+ offset
+    // Not a valid PE image so Exit
     //
-    NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
-    DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);
+    return;
+    }
   }
 
   //
@@ -1746,6 +1753,12 @@ PeCoffLoaderRelocateImageForRuntime (
                                                                             RelocDir->VirtualAddress + RelocDir->Size - 1,
                                                                             0
                                                                             );
+    if (RelocBase == NULL || RelocBaseEnd == NULL || RelocBaseEnd < RelocBase) {
+      //
+      // relocation block is not valid, just return
+      //
+      return;
+    }
   } else {
     //
     // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
-- 
2.16.2.windows.1



             reply	other threads:[~2019-01-24 23:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-24 23:18 Neo Hsueh [this message]
2019-01-25  9:07 ` [PATCH] MdePkg/BasePeCoffLib: skip runtime relocation if relocation info is invalid Laszlo Ersek
2019-01-28 18:46   ` Hsueh, Hong-chihX
  -- strict thread matches above, loose matches on Subject: below --
2019-01-28 18:40 Neo Hsueh
2019-01-28 22:16 ` Laszlo Ersek
2019-01-28 23:40   ` Hsueh, Hong-chihX
2019-01-29 10:52     ` Laszlo Ersek
2019-01-29  5:13 ` Bi, Dandan
2019-01-29 10:55   ` Laszlo Ersek
2019-01-28 23:22 Neo Hsueh
2019-01-29 10:57 ` Laszlo Ersek
2019-01-30  1:05   ` Hsueh, Hong-chihX

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=20190124231819.2352-1-hong-chihx.hsueh@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