]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BasePeCoffLib: skip runtime relocation if reloc info is invalid
authorHsueh, Hong-chihX <hong-chihx.hsueh@intel.com>
Wed, 30 Jan 2019 01:19:36 +0000 (09:19 +0800)
committerLiming Gao <liming.gao@intel.com>
Thu, 31 Jan 2019 03:36:09 +0000 (11:36 +0800)
Skip runtime relocation for PE images that provide invalid relocation
infomation (ex: RelocDir->Size = 0) to fix a hang observed while booting
Windows.

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>
Cc: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Bi Dandan <dandan.bi@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
MdePkg/Library/BasePeCoffLib/BasePeCoff.c

index 1bd079ad6a87f46cc25f37484071ac3c19dd6b71..e2c62e1932a3b7226c4bee1ca177033a5587c4f0 100644 (file)
@@ -1002,7 +1002,7 @@ PeCoffLoaderRelocateImage (
                                                                             RelocDir->VirtualAddress + RelocDir->Size - 1,\r
                                                                             TeStrippedOffset\r
                                                                             );\r
-    if (RelocBase == NULL || RelocBaseEnd == NULL || RelocBaseEnd < RelocBase) {\r
+    if (RelocBase == NULL || RelocBaseEnd == NULL || (UINTN) RelocBaseEnd < (UINTN) RelocBase) {\r
       ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
       return RETURN_LOAD_ERROR;\r
     }\r
@@ -1022,7 +1022,7 @@ PeCoffLoaderRelocateImage (
     // Run the relocation information and apply the fixups\r
     //\r
     FixupData = ImageContext->FixupData;\r
-    while (RelocBase < RelocBaseEnd) {\r
+    while ((UINTN) RelocBase < (UINTN) RelocBaseEnd) {\r
 \r
       Reloc     = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
       //\r
@@ -1051,7 +1051,7 @@ PeCoffLoaderRelocateImage (
       //\r
       // Run this relocation record\r
       //\r
-      while (Reloc < RelocEnd) {\r
+      while ((UINTN) Reloc < (UINTN) RelocEnd) {\r
         Fixup = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress + (*Reloc & 0xFFF), TeStrippedOffset);\r
         if (Fixup == NULL) {\r
           ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
@@ -1739,13 +1739,23 @@ PeCoffLoaderRelocateImageForRuntime (
   // is present in the image. You have to check the NumberOfRvaAndSizes in\r
   // the optional header to verify a desired directory entry is there.\r
   //\r
+  RelocBase = NULL;\r
+  RelocBaseEnd = NULL;\r
   if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r
     RelocDir      = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;\r
-    RelocBase     = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext, RelocDir->VirtualAddress, 0);\r
-    RelocBaseEnd  = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext,\r
-                                                                            RelocDir->VirtualAddress + RelocDir->Size - 1,\r
-                                                                            0\r
-                                                                            );\r
+    if ((RelocDir != NULL) && (RelocDir->Size > 0)) {\r
+      RelocBase     = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext, RelocDir->VirtualAddress, 0);\r
+      RelocBaseEnd  = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext,\r
+                                                                              RelocDir->VirtualAddress + RelocDir->Size - 1,\r
+                                                                              0\r
+                                                                              );\r
+    }\r
+    if (RelocBase == NULL || RelocBaseEnd == NULL || (UINTN) RelocBaseEnd < (UINTN) RelocBase) {\r
+      //\r
+      // relocation block is not valid, just return\r
+      //\r
+      return;\r
+    }\r
   } else {\r
     //\r
     // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.\r
@@ -1769,7 +1779,7 @@ PeCoffLoaderRelocateImageForRuntime (
     //\r
     FixupData = RelocationData;\r
     RelocBaseOrig = RelocBase;\r
-    while (RelocBase < RelocBaseEnd) {\r
+    while ((UINTN) RelocBase < (UINTN) RelocBaseEnd) {\r
       //\r
       // Add check for RelocBase->SizeOfBlock field.\r
       //\r
@@ -1794,7 +1804,7 @@ PeCoffLoaderRelocateImageForRuntime (
       //\r
       // Run this relocation record\r
       //\r
-      while (Reloc < RelocEnd) {\r
+      while ((UINTN) Reloc < (UINTN) RelocEnd) {\r
 \r
         Fixup = PeCoffLoaderImageAddress (&ImageContext, RelocBase->VirtualAddress + (*Reloc & 0xFFF), 0);\r
         if (Fixup == NULL) {\r