1 /** @file 2 The library instance provides security service of TPM2 measure boot. 3 4 Caution: This file requires additional review when modified. 5 This library will have external input - PE/COFF image and GPT partition. 6 This external input must be validated carefully to avoid security issue like 7 buffer overflow, integer overflow. 8 9 DxeTpm2MeasureBootLibImageRead() function will make sure the PE/COFF image content 10 read is within the image buffer. 11 12 Tcg2MeasurePeImage() function will accept untrusted PE/COFF image and validate its 13 data structure within this image buffer before use. 14 15 Tcg2MeasureGptTable() function will receive untrusted GPT partition table, and parse 16 partition data carefully. 17 18 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR> 19 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR> 20 This program and the accompanying materials 21 are licensed and made available under the terms and conditions of the BSD License 22 which accompanies this distribution. The full text of the license may be found at 23 http://opensource.org/licenses/bsd-license.php 24 25 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 26 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 27 28 **/ 29 30 #include <PiDxe.h> 31 32 #include <Protocol/Tcg2Protocol.h> 33 #include <Protocol/BlockIo.h> 34 #include <Protocol/DiskIo.h> 35 #include <Protocol/DevicePathToText.h> 36 #include <Protocol/FirmwareVolumeBlock.h> 37 38 #include <Guid/MeasuredFvHob.h> 39 40 #include <Library/BaseLib.h> 41 #include <Library/DebugLib.h> 42 #include <Library/BaseMemoryLib.h> 43 #include <Library/MemoryAllocationLib.h> 44 #include <Library/DevicePathLib.h> 45 #include <Library/UefiBootServicesTableLib.h> 46 #include <Library/BaseCryptLib.h> 47 #include <Library/PeCoffLib.h> 48 #include <Library/SecurityManagementLib.h> 49 #include <Library/HobLib.h> 50 51 // 52 // Flag to check GPT partition. It only need be measured once. 53 // 54 BOOLEAN mTcg2MeasureGptTableFlag = FALSE; 55 UINTN mTcg2MeasureGptCount = 0; 56 VOID *mTcg2FileBuffer; 57 UINTN mTcg2ImageSize; 58 // 59 // Measured FV handle cache 60 // 61 EFI_HANDLE mTcg2CacheMeasuredHandle = NULL; 62 MEASURED_HOB_DATA *mTcg2MeasuredHobData = NULL; 63 64 /** 65 Reads contents of a PE/COFF image in memory buffer. 66 67 Caution: This function may receive untrusted input. 68 PE/COFF image is external input, so this function will make sure the PE/COFF image content 69 read is within the image buffer. 70 71 @param FileHandle Pointer to the file handle to read the PE/COFF image. 72 @param FileOffset Offset into the PE/COFF image to begin the read operation. 73 @param ReadSize On input, the size in bytes of the requested read operation. 74 On output, the number of bytes actually read. 75 @param Buffer Output buffer that contains the data read from the PE/COFF image. 76 77 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size 78 **/ 79 EFI_STATUS 80 EFIAPI 81 DxeTpm2MeasureBootLibImageRead ( 82 IN VOID *FileHandle, 83 IN UINTN FileOffset, 84 IN OUT UINTN *ReadSize, 85 OUT VOID *Buffer 86 ) 87 { 88 UINTN EndPosition; 89 90 if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) { 91 return EFI_INVALID_PARAMETER; 92 } 93 94 if (MAX_ADDRESS - FileOffset < *ReadSize) { 95 return EFI_INVALID_PARAMETER; 96 } 97 98 EndPosition = FileOffset + *ReadSize; 99 if (EndPosition > mTcg2ImageSize) { 100 *ReadSize = (UINT32)(mTcg2ImageSize - FileOffset); 101 } 102 103 if (FileOffset >= mTcg2ImageSize) { 104 *ReadSize = 0; 105 } 106 107 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize); 108 109 return EFI_SUCCESS; 110 } 111 112 /** 113 Measure GPT table data into TPM log. 114 115 Caution: This function may receive untrusted input. 116 The GPT partition table is external input, so this function should parse partition data carefully. 117 118 @param Tcg2Protocol Pointer to the located TCG2 protocol instance. 119 @param GptHandle Handle that GPT partition was installed. 120 121 @retval EFI_SUCCESS Successfully measure GPT table. 122 @retval EFI_UNSUPPORTED Not support GPT table on the given handle. 123 @retval EFI_DEVICE_ERROR Can't get GPT table because device error. 124 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table. 125 @retval other error value 126 **/ 127 EFI_STATUS 128 EFIAPI 129 Tcg2MeasureGptTable ( 130 IN EFI_TCG2_PROTOCOL *Tcg2Protocol, 131 IN EFI_HANDLE GptHandle 132 ) 133 { 134 EFI_STATUS Status; 135 EFI_BLOCK_IO_PROTOCOL *BlockIo; 136 EFI_DISK_IO_PROTOCOL *DiskIo; 137 EFI_PARTITION_TABLE_HEADER *PrimaryHeader; 138 EFI_PARTITION_ENTRY *PartitionEntry; 139 UINT8 *EntryPtr; 140 UINTN NumberOfPartition; 141 UINT32 Index; 142 EFI_TCG2_EVENT *Tcg2Event; 143 EFI_GPT_DATA *GptData; 144 UINT32 EventSize; 145 146 if (mTcg2MeasureGptCount > 0) { 147 return EFI_SUCCESS; 148 } 149 150 Status = gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo); 151 if (EFI_ERROR (Status)) { 152 return EFI_UNSUPPORTED; 153 } 154 Status = gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID**)&DiskIo); 155 if (EFI_ERROR (Status)) { 156 return EFI_UNSUPPORTED; 157 } 158 // 159 // Read the EFI Partition Table Header 160 // 161 PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize); 162 if (PrimaryHeader == NULL) { 163 return EFI_OUT_OF_RESOURCES; 164 } 165 Status = DiskIo->ReadDisk ( 166 DiskIo, 167 BlockIo->Media->MediaId, 168 1 * BlockIo->Media->BlockSize, 169 BlockIo->Media->BlockSize, 170 (UINT8 *)PrimaryHeader 171 ); 172 if (EFI_ERROR (Status)) { 173 DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n")); 174 FreePool (PrimaryHeader); 175 return EFI_DEVICE_ERROR; 176 } 177 // 178 // Read the partition entry. 179 // 180 EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry); 181 if (EntryPtr == NULL) { 182 FreePool (PrimaryHeader); 183 return EFI_OUT_OF_RESOURCES; 184 } 185 Status = DiskIo->ReadDisk ( 186 DiskIo, 187 BlockIo->Media->MediaId, 188 MultU64x32(PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize), 189 PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry, 190 EntryPtr 191 ); 192 if (EFI_ERROR (Status)) { 193 FreePool (PrimaryHeader); 194 FreePool (EntryPtr); 195 return EFI_DEVICE_ERROR; 196 } 197 198 // 199 // Count the valid partition 200 // 201 PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr; 202 NumberOfPartition = 0; 203 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) { 204 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) { 205 NumberOfPartition++; 206 } 207 PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry); 208 } 209 210 // 211 // Prepare Data for Measurement 212 // 213 EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions) 214 + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry); 215 Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event)); 216 if (Tcg2Event == NULL) { 217 FreePool (PrimaryHeader); 218 FreePool (EntryPtr); 219 return EFI_OUT_OF_RESOURCES; 220 } 221 222 Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event); 223 Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER); 224 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION; 225 Tcg2Event->Header.PCRIndex = 5; 226 Tcg2Event->Header.EventType = EV_EFI_GPT_EVENT; 227 GptData = (EFI_GPT_DATA *) Tcg2Event->Event; 228 229 // 230 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition 231 // 232 CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER)); 233 GptData->NumberOfPartitions = NumberOfPartition; 234 // 235 // Copy the valid partition entry 236 // 237 PartitionEntry = (EFI_PARTITION_ENTRY*)EntryPtr; 238 NumberOfPartition = 0; 239 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) { 240 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) { 241 CopyMem ( 242 (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry, 243 (UINT8 *)PartitionEntry, 244 PrimaryHeader->SizeOfPartitionEntry 245 ); 246 NumberOfPartition++; 247 } 248 PartitionEntry =(EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry); 249 } 250 251 // 252 // Measure the GPT data 253 // 254 Status = Tcg2Protocol->HashLogExtendEvent ( 255 Tcg2Protocol, 256 0, 257 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData, 258 (UINT64) EventSize, 259 Tcg2Event 260 ); 261 if (!EFI_ERROR (Status)) { 262 mTcg2MeasureGptCount++; 263 } 264 265 FreePool (PrimaryHeader); 266 FreePool (EntryPtr); 267 FreePool (Tcg2Event); 268 269 return Status; 270 } 271 272 /** 273 Measure PE image into TPM log based on the authenticode image hashing in 274 PE/COFF Specification 8.0 Appendix A. 275 276 Caution: This function may receive untrusted input. 277 PE/COFF image is external input, so this function will validate its data structure 278 within this image buffer before use. 279 280 @param[in] Tcg2Protocol Pointer to the located TCG2 protocol instance. 281 @param[in] ImageAddress Start address of image buffer. 282 @param[in] ImageSize Image size 283 @param[in] LinkTimeBase Address that the image is loaded into memory. 284 @param[in] ImageType Image subsystem type. 285 @param[in] FilePath File path is corresponding to the input image. 286 287 @retval EFI_SUCCESS Successfully measure image. 288 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image. 289 @retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format. 290 @retval other error value 291 292 **/ 293 EFI_STATUS 294 EFIAPI 295 Tcg2MeasurePeImage ( 296 IN EFI_TCG2_PROTOCOL *Tcg2Protocol, 297 IN EFI_PHYSICAL_ADDRESS ImageAddress, 298 IN UINTN ImageSize, 299 IN UINTN LinkTimeBase, 300 IN UINT16 ImageType, 301 IN EFI_DEVICE_PATH_PROTOCOL *FilePath 302 ) 303 { 304 EFI_STATUS Status; 305 EFI_TCG2_EVENT *Tcg2Event; 306 EFI_IMAGE_LOAD_EVENT *ImageLoad; 307 UINT32 FilePathSize; 308 UINT32 EventSize; 309 310 Status = EFI_UNSUPPORTED; 311 ImageLoad = NULL; 312 FilePathSize = (UINT32) GetDevicePathSize (FilePath); 313 314 // 315 // Determine destination PCR by BootPolicy 316 // 317 EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize; 318 Tcg2Event = AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event)); 319 if (Tcg2Event == NULL) { 320 return EFI_OUT_OF_RESOURCES; 321 } 322 323 Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event); 324 Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER); 325 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION; 326 ImageLoad = (EFI_IMAGE_LOAD_EVENT *) Tcg2Event->Event; 327 328 switch (ImageType) { 329 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION: 330 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION; 331 Tcg2Event->Header.PCRIndex = 4; 332 break; 333 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: 334 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER; 335 Tcg2Event->Header.PCRIndex = 2; 336 break; 337 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: 338 Tcg2Event->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER; 339 Tcg2Event->Header.PCRIndex = 2; 340 break; 341 default: 342 DEBUG (( 343 EFI_D_ERROR, 344 "Tcg2MeasurePeImage: Unknown subsystem type %d", 345 ImageType 346 )); 347 goto Finish; 348 } 349 350 ImageLoad->ImageLocationInMemory = ImageAddress; 351 ImageLoad->ImageLengthInMemory = ImageSize; 352 ImageLoad->ImageLinkTimeAddress = LinkTimeBase; 353 ImageLoad->LengthOfDevicePath = FilePathSize; 354 if ((FilePath != NULL) && (FilePathSize != 0)) { 355 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize); 356 } 357 358 // 359 // Log the PE data 360 // 361 Status = Tcg2Protocol->HashLogExtendEvent ( 362 Tcg2Protocol, 363 PE_COFF_IMAGE, 364 ImageAddress, 365 ImageSize, 366 Tcg2Event 367 ); 368 if (Status == EFI_VOLUME_FULL) { 369 // 370 // Volume full here means the image is hashed and its result is extended to PCR. 371 // But the event log cann't be saved since log area is full. 372 // Just return EFI_SUCCESS in order not to block the image load. 373 // 374 Status = EFI_SUCCESS; 375 } 376 377 Finish: 378 FreePool (Tcg2Event); 379 380 return Status; 381 } 382 383 /** 384 The security handler is used to abstract platform-specific policy 385 from the DXE core response to an attempt to use a file that returns a 386 given status for the authentication check from the section extraction protocol. 387 388 The possible responses in a given SAP implementation may include locking 389 flash upon failure to authenticate, attestation logging for all signed drivers, 390 and other exception operations. The File parameter allows for possible logging 391 within the SAP of the driver. 392 393 If File is NULL, then EFI_INVALID_PARAMETER is returned. 394 395 If the file specified by File with an authentication status specified by 396 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned. 397 398 If the file specified by File with an authentication status specified by 399 AuthenticationStatus is not safe for the DXE Core to use under any circumstances, 400 then EFI_ACCESS_DENIED is returned. 401 402 If the file specified by File with an authentication status specified by 403 AuthenticationStatus is not safe for the DXE Core to use right now, but it 404 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is 405 returned. 406 407 @param[in] AuthenticationStatus This is the authentication status returned 408 from the securitymeasurement services for the 409 input file. 410 @param[in] File This is a pointer to the device path of the file that is 411 being dispatched. This will optionally be used for logging. 412 @param[in] FileBuffer File buffer matches the input file device path. 413 @param[in] FileSize Size of File buffer matches the input file device path. 414 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service. 415 416 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL 417 FileBuffer did authenticate, and the platform policy dictates 418 that the DXE Foundation may use the file. 419 @retval other error value 420 **/ 421 EFI_STATUS 422 EFIAPI 423 DxeTpm2MeasureBootHandler ( 424 IN UINT32 AuthenticationStatus, 425 IN CONST EFI_DEVICE_PATH_PROTOCOL *File, 426 IN VOID *FileBuffer, 427 IN UINTN FileSize, 428 IN BOOLEAN BootPolicy 429 ) 430 { 431 EFI_TCG2_PROTOCOL *Tcg2Protocol; 432 EFI_STATUS Status; 433 EFI_TCG2_BOOT_SERVICE_CAPABILITY ProtocolCapability; 434 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode; 435 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode; 436 EFI_HANDLE Handle; 437 EFI_HANDLE TempHandle; 438 BOOLEAN ApplicationRequired; 439 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; 440 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol; 441 EFI_PHYSICAL_ADDRESS FvAddress; 442 UINT32 Index; 443 444 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol); 445 if (EFI_ERROR (Status)) { 446 // 447 // Tcg2 protocol is not installed. So, TPM2 is not present. 448 // Don't do any measurement, and directly return EFI_SUCCESS. 449 // 450 DEBUG ((EFI_D_VERBOSE, "DxeTpm2MeasureBootHandler - Tcg2 - %r\n", Status)); 451 return EFI_SUCCESS; 452 } 453 454 ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability); 455 Status = Tcg2Protocol->GetCapability ( 456 Tcg2Protocol, 457 &ProtocolCapability 458 ); 459 if (EFI_ERROR (Status) || (!ProtocolCapability.TPMPresentFlag)) { 460 // 461 // TPM device doesn't work or activate. 462 // 463 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler (%r) - TPMPresentFlag - %x\n", Status, ProtocolCapability.TPMPresentFlag)); 464 return EFI_SUCCESS; 465 } 466 467 // 468 // Copy File Device Path 469 // 470 OrigDevicePathNode = DuplicateDevicePath (File); 471 472 // 473 // 1. Check whether this device path support BlockIo protocol. 474 // Is so, this device path may be a GPT device path. 475 // 476 DevicePathNode = OrigDevicePathNode; 477 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle); 478 if (!EFI_ERROR (Status) && !mTcg2MeasureGptTableFlag) { 479 // 480 // Find the gpt partion on the given devicepath 481 // 482 DevicePathNode = OrigDevicePathNode; 483 ASSERT (DevicePathNode != NULL); 484 while (!IsDevicePathEnd (DevicePathNode)) { 485 // 486 // Find the Gpt partition 487 // 488 if (DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH && 489 DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) { 490 // 491 // Check whether it is a gpt partition or not 492 // 493 if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER && 494 ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) { 495 496 // 497 // Change the partition device path to its parent device path (disk) and get the handle. 498 // 499 DevicePathNode->Type = END_DEVICE_PATH_TYPE; 500 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE; 501 DevicePathNode = OrigDevicePathNode; 502 Status = gBS->LocateDevicePath ( 503 &gEfiDiskIoProtocolGuid, 504 &DevicePathNode, 505 &Handle 506 ); 507 if (!EFI_ERROR (Status)) { 508 // 509 // Measure GPT disk. 510 // 511 Status = Tcg2MeasureGptTable (Tcg2Protocol, Handle); 512 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasureGptTable - %r\n", Status)); 513 if (!EFI_ERROR (Status)) { 514 // 515 // GPT disk check done. 516 // 517 mTcg2MeasureGptTableFlag = TRUE; 518 } 519 } 520 FreePool (OrigDevicePathNode); 521 OrigDevicePathNode = DuplicateDevicePath (File); 522 ASSERT (OrigDevicePathNode != NULL); 523 break; 524 } 525 } 526 DevicePathNode = NextDevicePathNode (DevicePathNode); 527 } 528 } 529 530 // 531 // 2. Measure PE image. 532 // 533 ApplicationRequired = FALSE; 534 535 // 536 // Check whether this device path support FVB protocol. 537 // 538 DevicePathNode = OrigDevicePathNode; 539 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle); 540 if (!EFI_ERROR (Status)) { 541 // 542 // Don't check FV image, and directly return EFI_SUCCESS. 543 // It can be extended to the specific FV authentication according to the different requirement. 544 // 545 if (IsDevicePathEnd (DevicePathNode)) { 546 return EFI_SUCCESS; 547 } 548 // 549 // The PE image from unmeasured Firmware volume need be measured 550 // The PE image from measured Firmware volume will be mearsured according to policy below. 551 // If it is driver, do not measure 552 // If it is application, still measure. 553 // 554 ApplicationRequired = TRUE; 555 556 if (mTcg2CacheMeasuredHandle != Handle && mTcg2MeasuredHobData != NULL) { 557 // 558 // Search for Root FV of this PE image 559 // 560 TempHandle = Handle; 561 do { 562 Status = gBS->HandleProtocol( 563 TempHandle, 564 &gEfiFirmwareVolumeBlockProtocolGuid, 565 (VOID**)&FvbProtocol 566 ); 567 TempHandle = FvbProtocol->ParentHandle; 568 } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL); 569 570 // 571 // Search in measured FV Hob 572 // 573 Status = FvbProtocol->GetPhysicalAddress(FvbProtocol, &FvAddress); 574 if (EFI_ERROR(Status)){ 575 return Status; 576 } 577 578 ApplicationRequired = FALSE; 579 580 for (Index = 0; Index < mTcg2MeasuredHobData->Num; Index++) { 581 if(mTcg2MeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) { 582 // 583 // Cache measured FV for next measurement 584 // 585 mTcg2CacheMeasuredHandle = Handle; 586 ApplicationRequired = TRUE; 587 break; 588 } 589 } 590 } 591 } 592 593 // 594 // File is not found. 595 // 596 if (FileBuffer == NULL) { 597 Status = EFI_SECURITY_VIOLATION; 598 goto Finish; 599 } 600 601 mTcg2ImageSize = FileSize; 602 mTcg2FileBuffer = FileBuffer; 603 604 // 605 // Measure PE Image 606 // 607 DevicePathNode = OrigDevicePathNode; 608 ZeroMem (&ImageContext, sizeof (ImageContext)); 609 ImageContext.Handle = (VOID *) FileBuffer; 610 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeTpm2MeasureBootLibImageRead; 611 612 // 613 // Get information about the image being loaded 614 // 615 Status = PeCoffLoaderGetImageInfo (&ImageContext); 616 if (EFI_ERROR (Status)) { 617 // 618 // The information can't be got from the invalid PeImage 619 // 620 goto Finish; 621 } 622 623 // 624 // Measure only application if Application flag is set 625 // Measure drivers and applications if Application flag is not set 626 // 627 if ((!ApplicationRequired) || 628 (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) { 629 // 630 // Print the image path to be measured. 631 // 632 DEBUG_CODE_BEGIN (); 633 CHAR16 *ToText; 634 ToText = ConvertDevicePathToText ( 635 DevicePathNode, 636 FALSE, 637 TRUE 638 ); 639 if (ToText != NULL) { 640 DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText)); 641 FreePool (ToText); 642 } 643 DEBUG_CODE_END (); 644 645 // 646 // Measure PE image into TPM log. 647 // 648 Status = Tcg2MeasurePeImage ( 649 Tcg2Protocol, 650 (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer, 651 FileSize, 652 (UINTN) ImageContext.ImageAddress, 653 ImageContext.ImageType, 654 DevicePathNode 655 ); 656 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasurePeImage - %r\n", Status)); 657 } 658 659 // 660 // Done, free the allocated resource. 661 // 662 Finish: 663 if (OrigDevicePathNode != NULL) { 664 FreePool (OrigDevicePathNode); 665 } 666 667 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - %r\n", Status)); 668 669 return Status; 670 } 671 672 /** 673 Register the security handler to provide TPM measure boot service. 674 675 @param ImageHandle ImageHandle of the loaded driver. 676 @param SystemTable Pointer to the EFI System Table. 677 678 @retval EFI_SUCCESS Register successfully. 679 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler. 680 **/ 681 EFI_STATUS 682 EFIAPI 683 DxeTpm2MeasureBootLibConstructor ( 684 IN EFI_HANDLE ImageHandle, 685 IN EFI_SYSTEM_TABLE *SystemTable 686 ) 687 { 688 EFI_HOB_GUID_TYPE *GuidHob; 689 690 GuidHob = NULL; 691 692 GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid); 693 694 if (GuidHob != NULL) { 695 mTcg2MeasuredHobData = GET_GUID_HOB_DATA (GuidHob); 696 } 697 698 return RegisterSecurity2Handler ( 699 DxeTpm2MeasureBootHandler, 700 EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED 701 ); 702 } 703