]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Enhance the check when ImageRead function return.
authorydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 21 Aug 2012 08:38:58 +0000 (08:38 +0000)
committerydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 21 Aug 2012 08:38:58 +0000 (08:38 +0000)
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ye Ting <ting.ye@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13657 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Include/Library/PeCoffLib.h
MdePkg/Library/BasePeCoffLib/BasePeCoff.c

index 7df9969979412495fb618d7111520c1f0d08d288..09c7b835abd6a7c9772a7775b4ec94929c80a7b4 100644 (file)
@@ -5,7 +5,7 @@
   IA-32, x86, IPF, and EBC processor types. The library functions are memory-based \r
   and can be ported easily to any environment.\r
   \r
   IA-32, x86, IPF, and EBC processor types. The library functions are memory-based \r
   and can be ported easily to any environment.\r
   \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
@@ -60,7 +60,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
   @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
   \r
   @retval RETURN_SUCCESS            The specified portion of the PE/COFF image was \r
   @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
   \r
   @retval RETURN_SUCCESS            The specified portion of the PE/COFF image was \r
-                                    read and the size \r
+                                    read and the size return in ReadSize.\r
   @retval RETURN_DEVICE_ERROR       The specified portion of the PE/COFF image \r
                                     could not be read due to a device error.\r
 \r
   @retval RETURN_DEVICE_ERROR       The specified portion of the PE/COFF image \r
                                     could not be read due to a device error.\r
 \r
index 71089bbb0de6afda69c87e9657edba70841a88f1..014eb22dc812db0935f41283e7c36c4570f1ca3e 100644 (file)
@@ -83,6 +83,7 @@ PeCoffLoaderGetPeHeader (
   RETURN_STATUS         Status;\r
   EFI_IMAGE_DOS_HEADER  DosHdr;\r
   UINTN                 Size;\r
   RETURN_STATUS         Status;\r
   EFI_IMAGE_DOS_HEADER  DosHdr;\r
   UINTN                 Size;\r
+  UINTN                 ReadSize;\r
   UINT16                Magic;\r
   UINT32                SectionHeaderOffset;\r
   UINT32                Index;\r
   UINT16                Magic;\r
   UINT32                SectionHeaderOffset;\r
   UINT32                Index;\r
@@ -94,13 +95,14 @@ PeCoffLoaderGetPeHeader (
   // Read the DOS image header to check for its existence\r
   //\r
   Size = sizeof (EFI_IMAGE_DOS_HEADER);\r
   // Read the DOS image header to check for its existence\r
   //\r
   Size = sizeof (EFI_IMAGE_DOS_HEADER);\r
+  ReadSize = Size;\r
   Status = ImageContext->ImageRead (\r
                            ImageContext->Handle,\r
                            0,\r
                            &Size,\r
                            &DosHdr\r
                            );\r
   Status = ImageContext->ImageRead (\r
                            ImageContext->Handle,\r
                            0,\r
                            &Size,\r
                            &DosHdr\r
                            );\r
-  if (RETURN_ERROR (Status)) {\r
+  if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
     ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
     return Status;\r
   }\r
     ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
     return Status;\r
   }\r
@@ -121,13 +123,14 @@ PeCoffLoaderGetPeHeader (
   // location in both images.\r
   //\r
   Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
   // location in both images.\r
   //\r
   Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
+  ReadSize = Size;\r
   Status = ImageContext->ImageRead (\r
                            ImageContext->Handle,\r
                            ImageContext->PeCoffHeaderOffset,\r
                            &Size,\r
                            Hdr.Pe32\r
                            );\r
   Status = ImageContext->ImageRead (\r
                            ImageContext->Handle,\r
                            ImageContext->PeCoffHeaderOffset,\r
                            &Size,\r
                            Hdr.Pe32\r
                            );\r
-  if (RETURN_ERROR (Status)) {\r
+  if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
     ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
     return Status;\r
   }\r
     ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
     return Status;\r
   }\r
@@ -173,16 +176,17 @@ PeCoffLoaderGetPeHeader (
       }\r
 \r
       //\r
       }\r
 \r
       //\r
-      // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file\r
+      // 2.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the file.\r
       //\r
       Size = 1;\r
       //\r
       Size = 1;\r
+      ReadSize = Size;\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
-      if (RETURN_ERROR (Status)) {\r
+      if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
         return Status;\r
       }\r
 \r
         return Status;\r
       }\r
 \r
@@ -202,9 +206,10 @@ PeCoffLoaderGetPeHeader (
           }\r
 \r
           //\r
           }\r
 \r
           //\r
-          // Read section header from file\r
+          // Read last byte of section header from file\r
           //\r
           Size = 1;\r
           //\r
           Size = 1;\r
+          ReadSize = Size;\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r
@@ -212,7 +217,7 @@ PeCoffLoaderGetPeHeader (
                                    &Size,\r
                                    &BufferData\r
                                    );\r
                                    &Size,\r
                                    &BufferData\r
                                    );\r
-          if (RETURN_ERROR (Status)) {\r
+          if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
             return Status;\r
           }\r
         }\r
             return Status;\r
           }\r
         }\r
@@ -246,16 +251,17 @@ PeCoffLoaderGetPeHeader (
       }\r
 \r
       //\r
       }\r
 \r
       //\r
-      // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file\r
+      // 2.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from the file.\r
       //\r
       Size = 1;\r
       //\r
       Size = 1;\r
+      ReadSize = Size;\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
-      if (RETURN_ERROR (Status)) {\r
+      if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
         return Status;\r
       }\r
 \r
         return Status;\r
       }\r
 \r
@@ -275,9 +281,10 @@ PeCoffLoaderGetPeHeader (
           }\r
 \r
           //\r
           }\r
 \r
           //\r
-          // Read section header from file\r
+          // Read last byte of section header from file\r
           //\r
           Size = 1;\r
           //\r
           Size = 1;\r
+          ReadSize = Size;\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r
@@ -285,7 +292,7 @@ PeCoffLoaderGetPeHeader (
                                    &Size,\r
                                    &BufferData\r
                                    );\r
                                    &Size,\r
                                    &BufferData\r
                                    );\r
-          if (RETURN_ERROR (Status)) {\r
+          if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
             return Status;\r
           }\r
         }\r
             return Status;\r
           }\r
         }\r
@@ -333,13 +340,14 @@ PeCoffLoaderGetPeHeader (
     // Read section header from file\r
     //\r
     Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
     // Read section header from file\r
     //\r
     Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
+    ReadSize = Size;\r
     Status = ImageContext->ImageRead (\r
                              ImageContext->Handle,\r
                              SectionHeaderOffset,\r
                              &Size,\r
                              &SectionHeader\r
                              );\r
     Status = ImageContext->ImageRead (\r
                              ImageContext->Handle,\r
                              SectionHeaderOffset,\r
                              &Size,\r
                              &SectionHeader\r
                              );\r
-    if (RETURN_ERROR (Status)) {\r
+    if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
       return Status;\r
     }\r
 \r
       return Status;\r
     }\r
 \r
@@ -356,13 +364,14 @@ PeCoffLoaderGetPeHeader (
       // Read the last byte to make sure the data is in the image region.\r
       //\r
       Size = 1;\r
       // Read the last byte to make sure the data is in the image region.\r
       //\r
       Size = 1;\r
+      ReadSize = Size;\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,\r
                                &Size,\r
                                &BufferData\r
                                );\r
-      if (RETURN_ERROR (Status)) {\r
+      if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
         return Status;\r
       }\r
     }\r
         return Status;\r
       }\r
     }\r
@@ -416,6 +425,7 @@ PeCoffLoaderGetImageInfo (
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
   EFI_IMAGE_DATA_DIRECTORY              *DebugDirectoryEntry;\r
   UINTN                                 Size;\r
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
   EFI_IMAGE_DATA_DIRECTORY              *DebugDirectoryEntry;\r
   UINTN                                 Size;\r
+  UINTN                                 ReadSize;\r
   UINTN                                 Index;\r
   UINTN                                 DebugDirectoryEntryRva;\r
   UINTN                                 DebugDirectoryEntryFileOffset;\r
   UINTN                                 Index;\r
   UINTN                                 DebugDirectoryEntryRva;\r
   UINTN                                 DebugDirectoryEntryFileOffset;\r
@@ -538,13 +548,14 @@ PeCoffLoaderGetImageInfo (
         // Read section header from file\r
         //\r
         Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
         // Read section header from file\r
         //\r
         Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
+        ReadSize = Size;\r
         Status = ImageContext->ImageRead (\r
                                  ImageContext->Handle,\r
                                  SectionHeaderOffset,\r
                                  &Size,\r
                                  &SectionHeader\r
                                  );\r
         Status = ImageContext->ImageRead (\r
                                  ImageContext->Handle,\r
                                  SectionHeaderOffset,\r
                                  &Size,\r
                                  &SectionHeader\r
                                  );\r
-        if (RETURN_ERROR (Status)) {\r
+        if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
           ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
           return Status;\r
         }\r
           ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
           return Status;\r
         }\r
@@ -565,13 +576,14 @@ PeCoffLoaderGetImageInfo (
           // Read next debug directory entry\r
           //\r
           Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
           // Read next debug directory entry\r
           //\r
           Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
+          ReadSize = Size;\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    DebugDirectoryEntryFileOffset + Index,\r
                                    &Size,\r
                                    &DebugEntry\r
                                    );\r
           Status = ImageContext->ImageRead (\r
                                    ImageContext->Handle,\r
                                    DebugDirectoryEntryFileOffset + Index,\r
                                    &Size,\r
                                    &DebugEntry\r
                                    );\r
-          if (RETURN_ERROR (Status)) {\r
+          if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
             ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
             return Status;\r
           }\r
             ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
             return Status;\r
           }\r
@@ -599,13 +611,14 @@ PeCoffLoaderGetImageInfo (
       // Read section header from file\r
       //\r
       Size   = sizeof (EFI_IMAGE_SECTION_HEADER);\r
       // Read section header from file\r
       //\r
       Size   = sizeof (EFI_IMAGE_SECTION_HEADER);\r
+      ReadSize = Size;\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                SectionHeaderOffset,\r
                                &Size,\r
                                &SectionHeader\r
                                );\r
       Status = ImageContext->ImageRead (\r
                                ImageContext->Handle,\r
                                SectionHeaderOffset,\r
                                &Size,\r
                                &SectionHeader\r
                                );\r
-      if (RETURN_ERROR (Status)) {\r
+      if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
         ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
         return Status;\r
       }\r
         ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
         return Status;\r
       }\r
@@ -652,13 +665,14 @@ PeCoffLoaderGetImageInfo (
         // Read next debug directory entry\r
         //\r
         Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
         // Read next debug directory entry\r
         //\r
         Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
+        ReadSize = Size;\r
         Status = ImageContext->ImageRead (\r
                                  ImageContext->Handle,\r
                                  DebugDirectoryEntryFileOffset + Index,\r
                                  &Size,\r
                                  &DebugEntry\r
                                  );\r
         Status = ImageContext->ImageRead (\r
                                  ImageContext->Handle,\r
                                  DebugDirectoryEntryFileOffset + Index,\r
                                  &Size,\r
                                  &DebugEntry\r
                                  );\r
-        if (RETURN_ERROR (Status)) {\r
+        if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
           ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
           return Status;\r
         }\r
           ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
           return Status;\r
         }\r