From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=hong-chihx.hsueh@intel.com; receiver=edk2-devel@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EEF90211B85F1 for ; Thu, 24 Jan 2019 15:18:42 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jan 2019 15:18:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,518,1539673200"; d="scan'208";a="109573805" Received: from hhsuehx-desk.gar.corp.intel.com ([10.9.67.173]) by orsmga007.jf.intel.com with ESMTP; 24 Jan 2019 15:18:42 -0800 From: Neo Hsueh To: edk2-devel@lists.01.org Cc: Michael D Kinney , Liming Gao , Dandan Bi Date: Thu, 24 Jan 2019 15:18:19 -0800 Message-Id: <20190124231819.2352-1-hong-chihx.hsueh@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 Subject: [PATCH] MdePkg/BasePeCoffLib: skip runtime relocation if relocation info is invalid. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jan 2019 23:18:43 -0000 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 Cc: Michael D Kinney Cc: Liming Gao Cc: Dandan Bi --- 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