DxeTpmMeasureBootLib.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
The library instance provides security service of TPM measure boot.
Copyright (c) 2009 - 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
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiDxe.h>
#include <Protocol/TcgService.h>
#include <Protocol/FirmwareVolume2.h>
#include <Protocol/DevicePathToText.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DevicePathLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/PeCoffLib.h>
#include <Library/SecurityManagementLib.h>
//
// Flag to check GPT partition. It only need be measured once.
//
UINTN mMeasureGptCount = 0;
/**
@param ReadSize On input, the size in bytes of the requested read operation.
On output, the number of bytes actually read.
**/
)
{
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
if (EndPosition > mImageSize) {
}
if (FileOffset >= mImageSize) {
*ReadSize = 0;
}
return EFI_SUCCESS;
}
/**
Measure GPT table data into TPM log.
@param TcgProtocol Pointer to the located TCG protocol instance.
@param GptHandle Handle that GPT partition was installed.
@retval EFI_SUCCESS Successfully measure GPT table.
@retval EFI_UNSUPPORTED Not support GPT table on the given handle.
@retval EFI_DEVICE_ERROR Can't get GPT table because device error.
@retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.
@retval other error value
**/
)
{
if (mMeasureGptCount > 0) {
return EFI_SUCCESS;
}
return EFI_UNSUPPORTED;
}
return EFI_UNSUPPORTED;
}
//
// Read the EFI Partition Table Header
//
if (PrimaryHeader == NULL) {
return EFI_OUT_OF_RESOURCES;
}
);
return EFI_DEVICE_ERROR;
}
//
// Read the partition entry.
//
EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);
return EFI_OUT_OF_RESOURCES;
}
);
return EFI_DEVICE_ERROR;
}
//
// Count the valid partition
//
NumberOfPartition = 0;
}
}
//
// Parepare Data for Measurement
//
return EFI_OUT_OF_RESOURCES;
}
//
// Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition
//
//
// Copy the valid partition entry
//
NumberOfPartition = 0;
CopyMem (
(UINT8 *)PartitionEntry,
sizeof (EFI_PARTITION_ENTRY)
);
}
}
//
// Measure the GPT data
//
EventNumber = 1;
);
}
return Status;
}
/**
Measure PE image into TPM log based on the authenticode image hashing in
@param[in] TcgProtocol Pointer to the located TCG protocol instance.
@param[in] ImageAddress Start address of image buffer.
@param[in] ImageSize Image size
@param[in] LinkTimeBase Address that the image is loaded into memory.
@param[in] ImageType Image subsystem type.
@param[in] FilePath File path is corresponding to the input image.
@retval EFI_SUCCESS Successfully measure image.
@retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
@retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format.
@retval other error value
**/
)
{
//
// Determine destination PCR by BootPolicy
//
return EFI_OUT_OF_RESOURCES;
}
switch (ImageType) {
break;
break;
break;
default:
DEBUG ((
"TcgMeasurePeImage: Unknown subsystem type %d",
));
goto Finish;
}
//
//
PeCoffHeaderOffset = 0;
}
goto Finish;
}
//
//
//
//
// 1. Load the image header into memory.
// 2. Initialize a SHA hash context.
CtxSize = Sha1GetContextSize ();
goto Finish;
}
if (!HashStatus) {
goto Finish;
}
//
// But CheckSum field and SECURITY data directory (certificate) are excluded
//
//
// 3. Calculate the distance from the base of the image header to the image checksum address.
// 4. Hash the image header from its base to beginning of the image checksum.
//
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset
//
} else {
//
// Use PE32+ offset
//
}
if (!HashStatus) {
goto Finish;
}
//
// 5. Skip over the image checksum (it occupies a single ULONG).
//
//
// 6. Since there is no Cert Directory in optional header, hash everything
// from the end of the checksum to the end of image header.
//
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset.
//
} else {
//
// Use PE32+ offset.
//
}
if (HashSize != 0) {
if (!HashStatus) {
goto Finish;
}
}
} else {
//
// 7. Hash everything from the end of the checksum to the start of the Cert Directory.
//
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset
//
HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
} else {
//
// Use PE32+ offset
//
HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);
}
if (HashSize != 0) {
if (!HashStatus) {
goto Finish;
}
}
//
// 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
// 9. Hash everything from the end of the Cert Directory to the end of image header.
//
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset
//
HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
} else {
//
// Use PE32+ offset
//
HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
}
if (HashSize != 0) {
if (!HashStatus) {
goto Finish;
}
}
}
//
// 10. Set the SUM_OF_BYTES_HASHED to the size of the header
//
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset
//
} else {
//
// Use PE32+ offset
//
}
//
// 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
// structures in the image. The 'NumberOfSections' field of the image
// header indicates how big the table should be. Do not include any
// IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
//
SectionHeader = (EFI_IMAGE_SECTION_HEADER *) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER) * Hdr.Pe32->FileHeader.NumberOfSections);
if (SectionHeader == NULL) {
goto Finish;
}
//
// 12. Using the 'PointerToRawData' in the referenced section headers as
// a key, arrange the elements in the table in ascending order. In other
// words, sort the section headers according to the disk-file offset of
// the section.
//
Section = (EFI_IMAGE_SECTION_HEADER *) (
sizeof(UINT32) +
sizeof(EFI_IMAGE_FILE_HEADER) +
);
Pos--;
}
Section += 1;
}
//
// 13. Walk through the sorted table, bring the corresponding section
// into memory, and hash the entire section (using the 'SizeOfRawData'
// field in the section header to determine the amount of data to hash).
// 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
// 15. Repeat steps 13 and 14 for all the sections in the sorted table.
//
if (Section->SizeOfRawData == 0) {
continue;
}
if (!HashStatus) {
goto Finish;
}
}
//
// 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
// data in the file that needs to be added to the hash. This data begins
// at file offset SUM_OF_BYTES_HASHED and its length is:
// FileSize - (CertDirectory->Size)
//
if (ImageSize > SumOfBytesHashed) {
CertSize = 0;
} else {
if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
//
// Use PE32 offset.
//
} else {
//
// Use PE32+ offset.
//
}
}
if (!HashStatus) {
goto Finish;
}
goto Finish;
}
}
//
// 17. Finalize the SHA hash.
//
if (!HashStatus) {
goto Finish;
}
//
// Log the PE data
//
EventNumber = 1;
0,
);
if (SectionHeader != NULL) {
}
}
return Status;
}
/**
The security handler is used to abstract platform-specific policy
from the DXE core response to an attempt to use a file that returns a
given status for the authentication check from the section extraction protocol.
The possible responses in a given SAP implementation may include locking
flash upon failure to authenticate, attestation logging for all signed drivers,
and other exception operations. The File parameter allows for possible logging
within the SAP of the driver.
If File is NULL, then EFI_INVALID_PARAMETER is returned.
If the file specified by File with an authentication status specified by
AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
If the file specified by File with an authentication status specified by
AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
then EFI_ACCESS_DENIED is returned.
If the file specified by File with an authentication status specified by
AuthenticationStatus is not safe for the DXE Core to use right now, but it
might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
returned.
@param[in, out] AuthenticationStatus This is the authentication status returned
from the securitymeasurement services for the
input file.
@param[in] File This is a pointer to the device path of the file that is
being dispatched. This will optionally be used for logging.
@param[in] FileBuffer File buffer matches the input file device path.
@param[in] FileSize Size of File buffer matches the input file device path.
@retval EFI_SUCCESS The file specified by File did authenticate, and the
platform policy dictates that the DXE Core may use File.
@retval EFI_INVALID_PARAMETER File is NULL.
@retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
the platform policy dictates that File should be placed
in the untrusted state. A file may be promoted from
the untrusted to the trusted state at a future time
with a call to the Trust() DXE Service.
@retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and
the platform policy dictates that File should not be
used for any purpose.
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// TCG protocol is not installed. So, TPM is not present.
// Don't do any measurement, and directly return EFI_SUCCESS.
//
return EFI_SUCCESS;
}
);
//
// TPM device doesn't work or activate.
//
return EFI_SUCCESS;
}
//
// Copy File Device Path
//
//
// 1. Check whether this device path support BlockIo protocol.
// Is so, this device path may be a GPT device path.
//
//
// Find the gpt partion on the given devicepath
//
while (!IsDevicePathEnd (DevicePathNode)) {
//
// Find the Gpt partition
//
//
// Check whether it is a gpt partition or not
//
//
// Change the partition device path to its parent device path (disk) and get the handle.
//
);
//
// Measure GPT disk.
//
//
// GPT disk check done.
//
}
}
break;
}
}
}
}
//
// 2. Measure PE image.
//
//
// Check whether this device path support FV2 protocol.
//
//
// Don't check FV image, and directly return EFI_SUCCESS.
// It can be extended to the specific FV authentication according to the different requirement.
//
if (IsDevicePathEnd (DevicePathNode)) {
return EFI_SUCCESS;
}
//
// The image from Firmware image will not be mearsured.
// Current policy doesn't measure PeImage from Firmware if it is driver
// If the got PeImage is application, it will be still be measured.
//
}
//
// File is not found.
//
if (FileBuffer == NULL) {
goto Finish;
}
//
// Measure PE Image
//
//
// Get information about the image being loaded
//
//
// The information can't be got from the invalid PeImage
//
goto Finish;
}
//
// Measure only application if Application flag is set
// Measure drivers and applications if Application flag is not set
//
if ((!ApplicationRequired) ||
//
// Print the image path to be measured.
//
DEBUG_CODE_BEGIN ();
NULL,
(VOID **) &DevPathToText
);
);
}
}
DEBUG_CODE_END ();
//
// Measure PE image into TPM log.
//
);
}
//
// Done, free the allocated resource.
//
return Status;
}
/**
Register the security handler to provide TPM measure boot service.
@param ImageHandle ImageHandle of the loaded driver.
@param SystemTable Pointer to the EFI System Table.
@retval EFI_SUCCESS Register successfully.
@retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
**/
)
{
return RegisterSecurityHandler (
);
}