Revision: 15387
          http://sourceforge.net/p/edk2/code/15387
Author:   ydong10
Date:     2014-03-25 05:04:21 +0000 (Tue, 25 Mar 2014)
Log Message:
-----------
Support load 64 bit image from 32 bit core.
Add more enhancement to check invalid PE format.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Eric Dong <[email protected]>
Reviewed-by: Jiewen, Yao <[email protected]>
Reviewed-by: Liming, Gao <[email protected]>

Modified Paths:
--------------
    trunk/edk2/MdePkg/Library/BasePeCoffLib/BasePeCoff.c

Modified: trunk/edk2/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
===================================================================
--- trunk/edk2/MdePkg/Library/BasePeCoffLib/BasePeCoff.c        2014-03-25 
03:54:34 UTC (rev 15386)
+++ trunk/edk2/MdePkg/Library/BasePeCoffLib/BasePeCoff.c        2014-03-25 
05:04:21 UTC (rev 15387)
@@ -941,6 +941,7 @@
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
   EFI_IMAGE_DATA_DIRECTORY              *RelocDir;
   UINT64                                Adjust;
+  EFI_IMAGE_BASE_RELOCATION             *RelocBaseOrg;
   EFI_IMAGE_BASE_RELOCATION             *RelocBase;
   EFI_IMAGE_BASE_RELOCATION             *RelocBaseEnd;
   UINT16                                *Reloc;
@@ -1041,7 +1042,8 @@
                                                                             
RelocDir->VirtualAddress + RelocDir->Size - 1,
                                                                             
TeStrippedOffset
                                                                             );
-    if (RelocBase == NULL || RelocBaseEnd == NULL) {
+    if (RelocBase == NULL || RelocBaseEnd == NULL || RelocBaseEnd < RelocBase) 
{
+      ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
       return RETURN_LOAD_ERROR;
     }
   } else {
@@ -1050,6 +1052,7 @@
     //
     RelocBase = RelocBaseEnd = NULL;    
   }
+  RelocBaseOrg = RelocBase;
 
   //
   // If Adjust is not zero, then apply fix ups to the image
@@ -1065,14 +1068,23 @@
       //
       // Add check for RelocBase->SizeOfBlock field.
       //
-      if ((RelocBase->SizeOfBlock == 0) || (RelocBase->SizeOfBlock > 
RelocDir->Size)) {
+      if (RelocBase->SizeOfBlock == 0) {
         ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
         return RETURN_LOAD_ERROR;
       }
+      if ((UINTN)RelocBase > MAX_ADDRESS - RelocBase->SizeOfBlock) {
+        ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
+        return RETURN_LOAD_ERROR;
+      }
 
       RelocEnd  = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);
+      if ((UINTN)RelocEnd > (UINTN)RelocBaseOrg + RelocDir->Size) {
+        ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
+        return RETURN_LOAD_ERROR;
+      }
       FixupBase = PeCoffLoaderImageAddress (ImageContext, 
RelocBase->VirtualAddress, TeStrippedOffset);
       if (FixupBase == NULL) {
+        ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
         return RETURN_LOAD_ERROR;
       }  
 
@@ -1080,8 +1092,11 @@
       // Run this relocation record
       //
       while (Reloc < RelocEnd) {
-
-        Fixup = FixupBase + (*Reloc & 0xFFF);
+        Fixup = PeCoffLoaderImageAddress (ImageContext, 
RelocBase->VirtualAddress + (*Reloc & 0xFFF), TeStrippedOffset);
+        if (Fixup == NULL) {
+          ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
+          return RETURN_LOAD_ERROR;
+        }
         switch ((*Reloc) >> 12) {
         case EFI_IMAGE_REL_BASED_ABSOLUTE:
           break;
@@ -1148,6 +1163,7 @@
       //
       RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
     }
+    ASSERT ((UINTN)FixupData <= (UINTN)ImageContext->FixupData + 
ImageContext->FixupDataSize);
 
     //
     // Adjust the EntryPoint to match the linked-to address
@@ -1444,14 +1460,17 @@
       DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY 
*)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
     }
 
+    //
+    // Must use UINT64 here, because there might a case that 32bit loader to 
load 64bit image.
+    //
     if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
-      ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * 
sizeof (UINTN);
+      ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * 
sizeof (UINT64);
     } else {
       ImageContext->FixupDataSize = 0;
     }
   } else {
     DirectoryEntry              = &Hdr.Te->DataDirectory[0];
-    ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * 
sizeof (UINTN);
+    ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * 
sizeof (UINT64);
   }
   //
   // Consumer must allocate a buffer for the relocation fixup log.

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
edk2-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-commits

Reply via email to