]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/Image.c
Validate some fields in PE image to make sure not access violation for later code.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
index 04f8368a1c2c51449b24559c863290dee28295ce..e51a9fe1743b8391e2d7bbf5c0341f84ffec7e4a 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Core image handling services to load and unload PeImage.\r
 \r
-Copyright (c) 2006 - 2010, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -77,6 +77,46 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage  = {
 //\r
 GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mDxeCodeMemoryRangeUsageBitMap=NULL;\r
 \r
+typedef struct {\r
+  UINT16  MachineType;\r
+  CHAR16  *MachineTypeName;\r
+} MACHINE_TYPE_INFO;\r
+\r
+//\r
+// EBC machine is not listed in this table, because EBC is in the default supported scopes of other machine type.\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED MACHINE_TYPE_INFO  mMachineTypeInfo[] = {\r
+  {EFI_IMAGE_MACHINE_IA32,           L"IA32"},\r
+  {EFI_IMAGE_MACHINE_IA64,           L"IA64"},\r
+  {EFI_IMAGE_MACHINE_X64,            L"X64"},\r
+  {EFI_IMAGE_MACHINE_ARMTHUMB_MIXED, L"ARM"}\r
+};\r
+\r
+UINT16 mDxeCoreImageMachineType = 0;\r
+\r
+/**\r
+ Return machine type name.\r
+\r
+ @param MachineType The machine type\r
+\r
+ @return machine type name\r
+**/\r
+CHAR16 *\r
+GetMachineTypeName (\r
+  UINT16 MachineType\r
+  )\r
+{\r
+  UINTN  Index;\r
+  \r
+  for (Index = 0; Index < sizeof(mMachineTypeInfo)/sizeof(mMachineTypeInfo[0]); Index++) {\r
+    if (mMachineTypeInfo[Index].MachineType == MachineType) {\r
+      return mMachineTypeInfo[Index].MachineTypeName;\r
+    }\r
+  }\r
+\r
+  return L"<Unknown>";\r
+}\r
+\r
 /**\r
   Add the Image Services to EFI Boot Services Table and install the protocol\r
   interfaces for this image.\r
@@ -97,6 +137,7 @@ CoreInitializeImageServices (
   UINT64                            DxeCoreImageLength;\r
   VOID                              *DxeCoreEntryPoint;\r
   EFI_PEI_HOB_POINTERS              DxeCoreHob;\r
\r
   //\r
   // Searching for image hob\r
   //\r
@@ -116,6 +157,7 @@ CoreInitializeImageServices (
   DxeCoreImageLength      = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength;\r
   DxeCoreEntryPoint       = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint;\r
   gDxeCoreFileName        = &DxeCoreHob.MemoryAllocationModule->ModuleName;\r
+  \r
   //\r
   // Initialize the fields for an internal driver\r
   //\r
@@ -145,6 +187,7 @@ CoreInitializeImageServices (
   //\r
   // Fill in DXE globals\r
   //\r
+  mDxeCoreImageMachineType = PeCoffLoaderGetMachineType (Image->Info.ImageBase);\r
   gDxeCoreImageHandle = Image->Handle;\r
   gDxeCoreLoadedImage = &Image->Info;\r
 \r
@@ -153,11 +196,11 @@ CoreInitializeImageServices (
     // Export DXE Core PE Loader functionality for backward compatibility.\r
     //\r
     Status = CoreInstallProtocolInterface (\r
-      &mLoadPe32PrivateData.Handle,\r
-      &gEfiLoadPeImageProtocolGuid,\r
-      EFI_NATIVE_INTERFACE,\r
-      &mLoadPe32PrivateData.Pe32Image\r
-      );\r
+               &mLoadPe32PrivateData.Handle,\r
+               &gEfiLoadPeImageProtocolGuid,\r
+               EFI_NATIVE_INTERFACE,\r
+               &mLoadPe32PrivateData.Pe32Image\r
+               );\r
   }\r
 \r
   return Status;\r
@@ -189,6 +232,14 @@ CoreReadImageFile (
   UINTN               EndPosition;\r
   IMAGE_FILE_HANDLE  *FHand;\r
 \r
+  if (UserHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (MAX_ADDRESS - Offset < *ReadSize) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   FHand = (IMAGE_FILE_HANDLE  *)UserHandle;\r
   ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);\r
 \r
@@ -356,7 +407,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
          // relative to top address\r
          //\r
          if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) < 0) {\r
-                ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)ImageContext->ImageAddress;\r
+                ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress;\r
          }\r
          //\r
          // Check if the memory range is avaliable.\r
@@ -424,6 +475,8 @@ CoreLoadPeImage (
       // The PE/COFF loader can support loading image types that can be executed.\r
       // If we loaded an image type that we can not execute return EFI_UNSUPORTED.\r
       //\r
+      DEBUG ((EFI_D_ERROR, "Image type %s can't be loaded ", GetMachineTypeName(Image->ImageContext.Machine)));\r
+      DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));\r
       return EFI_UNSUPPORTED;\r
     }\r
   }\r
@@ -957,6 +1010,14 @@ CoreUnloadAndCloseImage (
                                   protocol for loading the file.\r
   @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
+  @retval EFI_LOAD_ERROR          Image was not loaded because the image format was corrupt or not\r
+                                  understood.\r
+  @retval EFI_DEVICE_ERROR        Image was not loaded because the device returned a read error.\r
+  @retval EFI_ACCESS_DENIED       Image was not loaded because the platform policy prohibits the \r
+                                  image from being loaded. NULL is returned in *ImageHandle.\r
+  @retval EFI_SECURITY_VIOLATION  Image was loaded and an ImageHandle was created with a \r
+                                  valid EFI_LOADED_IMAGE_PROTOCOL. However, the current \r
+                                  platform policy specifies that the image should not be started.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1076,6 +1137,13 @@ CoreLoadImageCommon (
                                   OriginalFilePath\r
                                   );\r
     if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {\r
+      if (SecurityStatus == EFI_ACCESS_DENIED) {\r
+        //\r
+        // Image was not loaded because the platform policy prohibits the image from being loaded.\r
+        // It's the only place we could meet EFI_ACCESS_DENIED.\r
+        //\r
+        *ImageHandle = NULL;\r
+      }\r
       Status = SecurityStatus;\r
       Image = NULL;\r
       goto Done;\r
@@ -1227,7 +1295,6 @@ Done:
   if (EFI_ERROR (Status)) {\r
     if (Image != NULL) {\r
       CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer == 0));\r
-      *ImageHandle = NULL;\r
     }\r
   } else if (EFI_ERROR (SecurityStatus)) {\r
     Status = SecurityStatus;\r
@@ -1263,6 +1330,14 @@ Done:
                                   protocol for loading the file.\r
   @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
+  @retval EFI_LOAD_ERROR          Image was not loaded because the image format was corrupt or not\r
+                                  understood.\r
+  @retval EFI_DEVICE_ERROR        Image was not loaded because the device returned a read error.\r
+  @retval EFI_ACCESS_DENIED       Image was not loaded because the platform policy prohibits the \r
+                                  image from being loaded. NULL is returned in *ImageHandle.\r
+  @retval EFI_SECURITY_VIOLATION  Image was loaded and an ImageHandle was created with a \r
+                                  valid EFI_LOADED_IMAGE_PROTOCOL. However, the current \r
+                                  platform policy specifies that the image should not be started.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1332,6 +1407,14 @@ CoreLoadImage (
                                   protocol for loading the file.\r
   @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
+  @retval EFI_LOAD_ERROR          Image was not loaded because the image format was corrupt or not\r
+                                  understood.\r
+  @retval EFI_DEVICE_ERROR        Image was not loaded because the device returned a read error.\r
+  @retval EFI_ACCESS_DENIED       Image was not loaded because the platform policy prohibits the \r
+                                  image from being loaded. NULL is returned in *ImageHandle.\r
+  @retval EFI_SECURITY_VIOLATION  Image was loaded and an ImageHandle was created with a \r
+                                  valid EFI_LOADED_IMAGE_PROTOCOL. However, the current \r
+                                  platform policy specifies that the image should not be started.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1370,7 +1453,7 @@ CoreLoadImageEx (
   @param  ImageHandle             Handle of image to be started.\r
   @param  ExitDataSize            Pointer of the size to ExitData\r
   @param  ExitData                Pointer to a pointer to a data buffer that\r
-                                  includes a Null-terminated Unicode string,\r
+                                  includes a Null-terminated string,\r
                                   optionally followed by additional binary data.\r
                                   The string is a description that the caller may\r
                                   use to further indicate the reason for the\r
@@ -1404,8 +1487,13 @@ CoreStartImage (
   //\r
   // The image to be started must have the machine type supported by DxeCore.\r
   //\r
-  ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine));\r
   if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {\r
+    //\r
+    // Do not ASSERT here, because image might be loaded via EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED\r
+    // But it can not be started.\r
+    //\r
+    DEBUG ((EFI_D_ERROR, "Image type %s can't be started ", GetMachineTypeName(Image->Machine)));\r
+    DEBUG ((EFI_D_ERROR, "on %s UEFI system.\n", GetMachineTypeName(mDxeCoreImageMachineType)));\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r