SecurityPkg TrEEDxe: Add check for the PE/COFF image.
Use BasePeCoffLib PeCoffLoaderGetImageInfo() to check the PE/COFF image. In V2, add specific ImageRead() to make sure the PE/COFF image content read is within the image buffer. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Liming Gao <liming.gao@intel.com> Reviewed-by: Jiewen Yao <jiewen.yao@intel.com> Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>
This commit is contained in:
parent
5e9dfc6782
commit
cad19cd3f2
@ -6,7 +6,7 @@
|
|||||||
This external input must be validated carefully to avoid security issue like
|
This external input must be validated carefully to avoid security issue like
|
||||||
buffer overflow, integer overflow.
|
buffer overflow, integer overflow.
|
||||||
|
|
||||||
Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
|
||||||
This program and the accompanying materials
|
This program and the accompanying materials
|
||||||
are licensed and made available under the terms and conditions of the BSD License
|
are licensed and made available under the terms and conditions of the BSD License
|
||||||
which accompanies this distribution. The full text of the license may be found at
|
which accompanies this distribution. The full text of the license may be found at
|
||||||
@ -29,6 +29,56 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||||||
#include <Library/Tpm2CommandLib.h>
|
#include <Library/Tpm2CommandLib.h>
|
||||||
#include <Library/HashLib.h>
|
#include <Library/HashLib.h>
|
||||||
|
|
||||||
|
UINTN mTrEEDxeImageSize = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reads contents of a PE/COFF image in memory buffer.
|
||||||
|
|
||||||
|
Caution: This function may receive untrusted input.
|
||||||
|
PE/COFF image is external input, so this function will make sure the PE/COFF image content
|
||||||
|
read is within the image buffer.
|
||||||
|
|
||||||
|
@param FileHandle Pointer to the file handle to read the PE/COFF image.
|
||||||
|
@param FileOffset Offset into the PE/COFF image to begin the read operation.
|
||||||
|
@param ReadSize On input, the size in bytes of the requested read operation.
|
||||||
|
On output, the number of bytes actually read.
|
||||||
|
@param Buffer Output buffer that contains the data read from the PE/COFF image.
|
||||||
|
|
||||||
|
@retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
|
||||||
|
**/
|
||||||
|
EFI_STATUS
|
||||||
|
EFIAPI
|
||||||
|
TrEEDxeImageRead (
|
||||||
|
IN VOID *FileHandle,
|
||||||
|
IN UINTN FileOffset,
|
||||||
|
IN OUT UINTN *ReadSize,
|
||||||
|
OUT VOID *Buffer
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINTN EndPosition;
|
||||||
|
|
||||||
|
if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {
|
||||||
|
return EFI_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MAX_ADDRESS - FileOffset < *ReadSize) {
|
||||||
|
return EFI_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
EndPosition = FileOffset + *ReadSize;
|
||||||
|
if (EndPosition > mTrEEDxeImageSize) {
|
||||||
|
*ReadSize = (UINT32)(mTrEEDxeImageSize - FileOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FileOffset >= mTrEEDxeImageSize) {
|
||||||
|
*ReadSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);
|
||||||
|
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Measure PE image into TPM log based on the authenticode image hashing in
|
Measure PE image into TPM log based on the authenticode image hashing in
|
||||||
PE/COFF Specification 8.0 Appendix A.
|
PE/COFF Specification 8.0 Appendix A.
|
||||||
@ -37,6 +87,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||||||
PE/COFF image is external input, so this function will validate its data structure
|
PE/COFF image is external input, so this function will validate its data structure
|
||||||
within this image buffer before use.
|
within this image buffer before use.
|
||||||
|
|
||||||
|
Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
|
||||||
|
|
||||||
@param[in] PCRIndex TPM PCR index
|
@param[in] PCRIndex TPM PCR index
|
||||||
@param[in] ImageAddress Start address of image buffer.
|
@param[in] ImageAddress Start address of image buffer.
|
||||||
@param[in] ImageSize Image size
|
@param[in] ImageSize Image size
|
||||||
@ -69,6 +121,7 @@ MeasurePeImageAndExtend (
|
|||||||
UINT32 NumberOfRvaAndSizes;
|
UINT32 NumberOfRvaAndSizes;
|
||||||
UINT32 CertSize;
|
UINT32 CertSize;
|
||||||
HASH_HANDLE HashHandle;
|
HASH_HANDLE HashHandle;
|
||||||
|
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
|
||||||
|
|
||||||
HashHandle = 0xFFFFFFFF; // Know bad value
|
HashHandle = 0xFFFFFFFF; // Know bad value
|
||||||
|
|
||||||
@ -78,6 +131,23 @@ MeasurePeImageAndExtend (
|
|||||||
//
|
//
|
||||||
// Check PE/COFF image
|
// Check PE/COFF image
|
||||||
//
|
//
|
||||||
|
ZeroMem (&ImageContext, sizeof (ImageContext));
|
||||||
|
ImageContext.Handle = (VOID *) (UINTN) ImageAddress;
|
||||||
|
mTrEEDxeImageSize = ImageSize;
|
||||||
|
ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) TrEEDxeImageRead;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Get information about the image being loaded
|
||||||
|
//
|
||||||
|
Status = PeCoffLoaderGetImageInfo (&ImageContext);
|
||||||
|
if (EFI_ERROR (Status)) {
|
||||||
|
//
|
||||||
|
// The information can't be got from the invalid PeImage
|
||||||
|
//
|
||||||
|
DEBUG ((DEBUG_INFO, "TreeDxe: PeImage invalid. Cannot retrieve image information.\n"));
|
||||||
|
goto Finish;
|
||||||
|
}
|
||||||
|
|
||||||
DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;
|
DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;
|
||||||
PeCoffHeaderOffset = 0;
|
PeCoffHeaderOffset = 0;
|
||||||
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
|
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
|
||||||
|
@ -179,6 +179,8 @@ EFI_HANDLE mImageHandle;
|
|||||||
PE/COFF image is external input, so this function will validate its data structure
|
PE/COFF image is external input, so this function will validate its data structure
|
||||||
within this image buffer before use.
|
within this image buffer before use.
|
||||||
|
|
||||||
|
Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
|
||||||
|
|
||||||
@param[in] PCRIndex TPM PCR index
|
@param[in] PCRIndex TPM PCR index
|
||||||
@param[in] ImageAddress Start address of image buffer.
|
@param[in] ImageAddress Start address of image buffer.
|
||||||
@param[in] ImageSize Image size
|
@param[in] ImageSize Image size
|
||||||
|
@ -58,6 +58,7 @@
|
|||||||
HashLib
|
HashLib
|
||||||
PerformanceLib
|
PerformanceLib
|
||||||
ReportStatusCodeLib
|
ReportStatusCodeLib
|
||||||
|
PeCoffLib
|
||||||
|
|
||||||
[Guids]
|
[Guids]
|
||||||
## SOMETIMES_CONSUMES ## Variable:L"SecureBoot"
|
## SOMETIMES_CONSUMES ## Variable:L"SecureBoot"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user