summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>2012-08-21 08:38:58 +0000
committerydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>2012-08-21 08:38:58 +0000
commit3acb1985676d6d8d379126389b9cbcfd4544667e (patch)
treefa8e7bc7330044f9e919a74a6e34e8cf8bf53d0e
parent05bf16e04d5eabe4c0fd31a6e0e639c096a74ebe (diff)
downloadedk2-platforms-3acb1985676d6d8d379126389b9cbcfd4544667e.tar.xz
Enhance the check when ImageRead function return.
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
-rw-r--r--MdePkg/Include/Library/PeCoffLib.h4
-rw-r--r--MdePkg/Library/BasePeCoffLib/BasePeCoff.c46
2 files changed, 32 insertions, 18 deletions
diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/PeCoffLib.h
index 7df9969979..09c7b835ab 100644
--- a/MdePkg/Include/Library/PeCoffLib.h
+++ b/MdePkg/Include/Library/PeCoffLib.h
@@ -5,7 +5,7 @@
IA-32, x86, IPF, and EBC processor types. The library functions are memory-based
and can be ported easily to any environment.
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
@@ -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.
@retval RETURN_SUCCESS The specified portion of the PE/COFF image was
- read and the size
+ read and the size return in ReadSize.
@retval RETURN_DEVICE_ERROR The specified portion of the PE/COFF image
could not be read due to a device error.
diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 71089bbb0d..014eb22dc8 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -83,6 +83,7 @@ PeCoffLoaderGetPeHeader (
RETURN_STATUS Status;
EFI_IMAGE_DOS_HEADER DosHdr;
UINTN Size;
+ UINTN ReadSize;
UINT16 Magic;
UINT32 SectionHeaderOffset;
UINT32 Index;
@@ -94,13 +95,14 @@ PeCoffLoaderGetPeHeader (
// Read the DOS image header to check for its existence
//
Size = sizeof (EFI_IMAGE_DOS_HEADER);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
0,
&Size,
&DosHdr
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
@@ -121,13 +123,14 @@ PeCoffLoaderGetPeHeader (
// location in both images.
//
Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
ImageContext->PeCoffHeaderOffset,
&Size,
Hdr.Pe32
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
@@ -173,16 +176,17 @@ PeCoffLoaderGetPeHeader (
}
//
- // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file
+ // 2.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the file.
//
Size = 1;
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,
&Size,
&BufferData
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
@@ -202,9 +206,10 @@ PeCoffLoaderGetPeHeader (
}
//
- // Read section header from file
+ // Read last byte of section header from file
//
Size = 1;
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
@@ -212,7 +217,7 @@ PeCoffLoaderGetPeHeader (
&Size,
&BufferData
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
}
@@ -246,16 +251,17 @@ PeCoffLoaderGetPeHeader (
}
//
- // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file
+ // 2.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from the file.
//
Size = 1;
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,
&Size,
&BufferData
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
@@ -275,9 +281,10 @@ PeCoffLoaderGetPeHeader (
}
//
- // Read section header from file
+ // Read last byte of section header from file
//
Size = 1;
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
@@ -285,7 +292,7 @@ PeCoffLoaderGetPeHeader (
&Size,
&BufferData
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
}
@@ -333,13 +340,14 @@ PeCoffLoaderGetPeHeader (
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
SectionHeaderOffset,
&Size,
&SectionHeader
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
@@ -356,13 +364,14 @@ PeCoffLoaderGetPeHeader (
// Read the last byte to make sure the data is in the image region.
//
Size = 1;
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,
&Size,
&BufferData
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status;
}
}
@@ -416,6 +425,7 @@ PeCoffLoaderGetImageInfo (
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;
UINTN Size;
+ UINTN ReadSize;
UINTN Index;
UINTN DebugDirectoryEntryRva;
UINTN DebugDirectoryEntryFileOffset;
@@ -538,13 +548,14 @@ PeCoffLoaderGetImageInfo (
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
SectionHeaderOffset,
&Size,
&SectionHeader
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
@@ -565,13 +576,14 @@ PeCoffLoaderGetImageInfo (
// Read next debug directory entry
//
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
DebugDirectoryEntryFileOffset + Index,
&Size,
&DebugEntry
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
@@ -599,13 +611,14 @@ PeCoffLoaderGetImageInfo (
// Read section header from file
//
Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
SectionHeaderOffset,
&Size,
&SectionHeader
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
@@ -652,13 +665,14 @@ PeCoffLoaderGetImageInfo (
// Read next debug directory entry
//
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
+ ReadSize = Size;
Status = ImageContext->ImageRead (
ImageContext->Handle,
DebugDirectoryEntryFileOffset + Index,
&Size,
&DebugEntry
);
- if (RETURN_ERROR (Status)) {
+ if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}