]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
3c81b5af513d6b55e1cb7399daa421c7e4998480
[mirror_edk2.git] / SecurityPkg / Library / DxeTpm2MeasureBootLib / DxeTpm2MeasureBootLib.c
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 TrEEMeasurePeImage() function will accept untrusted PE/COFF image and validate its
13 data structure within this image buffer before use.
14
15 TrEEMeasureGptTable() function will receive untrusted GPT partition table, and parse
16 partition data carefully.
17
18 Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
23
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26
27 **/
28
29 #include <PiDxe.h>
30
31 #include <Protocol/TrEEProtocol.h>
32 #include <Protocol/BlockIo.h>
33 #include <Protocol/DiskIo.h>
34 #include <Protocol/DevicePathToText.h>
35 #include <Protocol/FirmwareVolumeBlock.h>
36
37 #include <Guid/MeasuredFvHob.h>
38
39 #include <Library/BaseLib.h>
40 #include <Library/DebugLib.h>
41 #include <Library/BaseMemoryLib.h>
42 #include <Library/MemoryAllocationLib.h>
43 #include <Library/DevicePathLib.h>
44 #include <Library/UefiBootServicesTableLib.h>
45 #include <Library/BaseCryptLib.h>
46 #include <Library/PeCoffLib.h>
47 #include <Library/SecurityManagementLib.h>
48 #include <Library/HobLib.h>
49
50 //
51 // Flag to check GPT partition. It only need be measured once.
52 //
53 BOOLEAN mTrEEMeasureGptTableFlag = FALSE;
54 EFI_GUID mTrEEZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
55 UINTN mTrEEMeasureGptCount = 0;
56 VOID *mTrEEFileBuffer;
57 UINTN mTrEEImageSize;
58 //
59 // Measured FV handle cache
60 //
61 EFI_HANDLE mTrEECacheMeasuredHandle = NULL;
62 MEASURED_HOB_DATA *mTrEEMeasuredHobData = 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 > mTrEEImageSize) {
100 *ReadSize = (UINT32)(mTrEEImageSize - FileOffset);
101 }
102
103 if (FileOffset >= mTrEEImageSize) {
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 TreeProtocol Pointer to the located TREE 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 TrEEMeasureGptTable (
130 IN EFI_TREE_PROTOCOL *TreeProtocol,
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 TrEE_EVENT *TreeEvent;
143 EFI_GPT_DATA *GptData;
144 UINT32 EventSize;
145
146 if (mTrEEMeasureGptCount > 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 (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mTrEEZeroGuid)) {
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 TreeEvent = (TrEE_EVENT *) AllocateZeroPool (EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event));
216 if (TreeEvent == NULL) {
217 FreePool (PrimaryHeader);
218 FreePool (EntryPtr);
219 return EFI_OUT_OF_RESOURCES;
220 }
221
222 TreeEvent->Size = EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event);
223 TreeEvent->Header.HeaderSize = sizeof(TrEE_EVENT_HEADER);
224 TreeEvent->Header.HeaderVersion = TREE_EVENT_HEADER_VERSION;
225 TreeEvent->Header.PCRIndex = 5;
226 TreeEvent->Header.EventType = EV_EFI_GPT_EVENT;
227 GptData = (EFI_GPT_DATA *) TreeEvent->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 (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mTrEEZeroGuid)) {
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 = TreeProtocol->HashLogExtendEvent (
255 TreeProtocol,
256 0,
257 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData,
258 (UINT64) EventSize,
259 TreeEvent
260 );
261 if (!EFI_ERROR (Status)) {
262 mTrEEMeasureGptCount++;
263 }
264
265 FreePool (PrimaryHeader);
266 FreePool (EntryPtr);
267 FreePool (TreeEvent);
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] TreeProtocol Pointer to the located TREE 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 TrEEMeasurePeImage (
296 IN EFI_TREE_PROTOCOL *TreeProtocol,
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 TrEE_EVENT *TreeEvent;
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 TreeEvent = AllocateZeroPool (EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event));
319 if (TreeEvent == NULL) {
320 return EFI_OUT_OF_RESOURCES;
321 }
322
323 TreeEvent->Size = EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event);
324 TreeEvent->Header.HeaderSize = sizeof(TrEE_EVENT_HEADER);
325 TreeEvent->Header.HeaderVersion = TREE_EVENT_HEADER_VERSION;
326 ImageLoad = (EFI_IMAGE_LOAD_EVENT *) TreeEvent->Event;
327
328 switch (ImageType) {
329 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
330 TreeEvent->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION;
331 TreeEvent->Header.PCRIndex = 4;
332 break;
333 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
334 TreeEvent->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER;
335 TreeEvent->Header.PCRIndex = 2;
336 break;
337 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
338 TreeEvent->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;
339 TreeEvent->Header.PCRIndex = 2;
340 break;
341 default:
342 DEBUG ((
343 EFI_D_ERROR,
344 "TrEEMeasurePeImage: 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 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);
355
356 //
357 // Log the PE data
358 //
359 Status = TreeProtocol->HashLogExtendEvent (
360 TreeProtocol,
361 PE_COFF_IMAGE,
362 ImageAddress,
363 ImageSize,
364 TreeEvent
365 );
366 if (Status == EFI_VOLUME_FULL) {
367 //
368 // Volume full here means the image is hashed and its result is extended to PCR.
369 // But the event log cann't be saved since log area is full.
370 // Just return EFI_SUCCESS in order not to block the image load.
371 //
372 Status = EFI_SUCCESS;
373 }
374
375 Finish:
376 FreePool (TreeEvent);
377
378 return Status;
379 }
380
381 /**
382 The security handler is used to abstract platform-specific policy
383 from the DXE core response to an attempt to use a file that returns a
384 given status for the authentication check from the section extraction protocol.
385
386 The possible responses in a given SAP implementation may include locking
387 flash upon failure to authenticate, attestation logging for all signed drivers,
388 and other exception operations. The File parameter allows for possible logging
389 within the SAP of the driver.
390
391 If File is NULL, then EFI_INVALID_PARAMETER is returned.
392
393 If the file specified by File with an authentication status specified by
394 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
395
396 If the file specified by File with an authentication status specified by
397 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
398 then EFI_ACCESS_DENIED is returned.
399
400 If the file specified by File with an authentication status specified by
401 AuthenticationStatus is not safe for the DXE Core to use right now, but it
402 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
403 returned.
404
405 @param[in] AuthenticationStatus This is the authentication status returned
406 from the securitymeasurement services for the
407 input file.
408 @param[in] File This is a pointer to the device path of the file that is
409 being dispatched. This will optionally be used for logging.
410 @param[in] FileBuffer File buffer matches the input file device path.
411 @param[in] FileSize Size of File buffer matches the input file device path.
412 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.
413
414 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL
415 FileBuffer did authenticate, and the platform policy dictates
416 that the DXE Foundation may use the file.
417 @retval other error value
418 **/
419 EFI_STATUS
420 EFIAPI
421 DxeTpm2MeasureBootHandler (
422 IN UINT32 AuthenticationStatus,
423 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
424 IN VOID *FileBuffer,
425 IN UINTN FileSize,
426 IN BOOLEAN BootPolicy
427 )
428 {
429 EFI_TREE_PROTOCOL *TreeProtocol;
430 EFI_STATUS Status;
431 TREE_BOOT_SERVICE_CAPABILITY ProtocolCapability;
432 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
433 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;
434 EFI_HANDLE Handle;
435 EFI_HANDLE TempHandle;
436 BOOLEAN ApplicationRequired;
437 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
438 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
439 EFI_PHYSICAL_ADDRESS FvAddress;
440 UINT32 Index;
441
442 Status = gBS->LocateProtocol (&gEfiTrEEProtocolGuid, NULL, (VOID **) &TreeProtocol);
443 if (EFI_ERROR (Status)) {
444 //
445 // TrEE protocol is not installed. So, TPM2 is not present.
446 // Don't do any measurement, and directly return EFI_SUCCESS.
447 //
448 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler - TrEE - %r\n", Status));
449 return EFI_SUCCESS;
450 }
451
452 ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);
453 Status = TreeProtocol->GetCapability (
454 TreeProtocol,
455 &ProtocolCapability
456 );
457 if (EFI_ERROR (Status) || !ProtocolCapability.TrEEPresentFlag) {
458 //
459 // TPM device doesn't work or activate.
460 //
461 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler (%r) - TrEEPresentFlag - %x\n", Status, ProtocolCapability.TrEEPresentFlag));
462 return EFI_SUCCESS;
463 }
464
465 //
466 // Copy File Device Path
467 //
468 OrigDevicePathNode = DuplicateDevicePath (File);
469
470 //
471 // 1. Check whether this device path support BlockIo protocol.
472 // Is so, this device path may be a GPT device path.
473 //
474 DevicePathNode = OrigDevicePathNode;
475 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);
476 if (!EFI_ERROR (Status) && !mTrEEMeasureGptTableFlag) {
477 //
478 // Find the gpt partion on the given devicepath
479 //
480 DevicePathNode = OrigDevicePathNode;
481 ASSERT (DevicePathNode != NULL);
482 while (!IsDevicePathEnd (DevicePathNode)) {
483 //
484 // Find the Gpt partition
485 //
486 if (DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH &&
487 DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {
488 //
489 // Check whether it is a gpt partition or not
490 //
491 if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER &&
492 ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {
493
494 //
495 // Change the partition device path to its parent device path (disk) and get the handle.
496 //
497 DevicePathNode->Type = END_DEVICE_PATH_TYPE;
498 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
499 DevicePathNode = OrigDevicePathNode;
500 Status = gBS->LocateDevicePath (
501 &gEfiDiskIoProtocolGuid,
502 &DevicePathNode,
503 &Handle
504 );
505 if (!EFI_ERROR (Status)) {
506 //
507 // Measure GPT disk.
508 //
509 Status = TrEEMeasureGptTable (TreeProtocol, Handle);
510 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler - TrEEMeasureGptTable - %r\n", Status));
511 if (!EFI_ERROR (Status)) {
512 //
513 // GPT disk check done.
514 //
515 mTrEEMeasureGptTableFlag = TRUE;
516 }
517 }
518 FreePool (OrigDevicePathNode);
519 OrigDevicePathNode = DuplicateDevicePath (File);
520 ASSERT (OrigDevicePathNode != NULL);
521 break;
522 }
523 }
524 DevicePathNode = NextDevicePathNode (DevicePathNode);
525 }
526 }
527
528 //
529 // 2. Measure PE image.
530 //
531 ApplicationRequired = FALSE;
532
533 //
534 // Check whether this device path support FVB protocol.
535 //
536 DevicePathNode = OrigDevicePathNode;
537 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);
538 if (!EFI_ERROR (Status)) {
539 //
540 // Don't check FV image, and directly return EFI_SUCCESS.
541 // It can be extended to the specific FV authentication according to the different requirement.
542 //
543 if (IsDevicePathEnd (DevicePathNode)) {
544 return EFI_SUCCESS;
545 }
546 //
547 // The PE image from unmeasured Firmware volume need be measured
548 // The PE image from measured Firmware volume will be mearsured according to policy below.
549 // If it is driver, do not measure
550 // If it is application, still measure.
551 //
552 ApplicationRequired = TRUE;
553
554 if (mTrEECacheMeasuredHandle != Handle && mTrEEMeasuredHobData != NULL) {
555 //
556 // Search for Root FV of this PE image
557 //
558 TempHandle = Handle;
559 do {
560 Status = gBS->HandleProtocol(
561 TempHandle,
562 &gEfiFirmwareVolumeBlockProtocolGuid,
563 (VOID**)&FvbProtocol
564 );
565 TempHandle = FvbProtocol->ParentHandle;
566 } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);
567
568 //
569 // Search in measured FV Hob
570 //
571 Status = FvbProtocol->GetPhysicalAddress(FvbProtocol, &FvAddress);
572 if (EFI_ERROR(Status)){
573 return Status;
574 }
575
576 ApplicationRequired = FALSE;
577
578 for (Index = 0; Index < mTrEEMeasuredHobData->Num; Index++) {
579 if(mTrEEMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {
580 //
581 // Cache measured FV for next measurement
582 //
583 mTrEECacheMeasuredHandle = Handle;
584 ApplicationRequired = TRUE;
585 break;
586 }
587 }
588 }
589 }
590
591 //
592 // File is not found.
593 //
594 if (FileBuffer == NULL) {
595 Status = EFI_SECURITY_VIOLATION;
596 goto Finish;
597 }
598
599 mTrEEImageSize = FileSize;
600 mTrEEFileBuffer = FileBuffer;
601
602 //
603 // Measure PE Image
604 //
605 DevicePathNode = OrigDevicePathNode;
606 ZeroMem (&ImageContext, sizeof (ImageContext));
607 ImageContext.Handle = (VOID *) FileBuffer;
608 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeTpm2MeasureBootLibImageRead;
609
610 //
611 // Get information about the image being loaded
612 //
613 Status = PeCoffLoaderGetImageInfo (&ImageContext);
614 if (EFI_ERROR (Status)) {
615 //
616 // The information can't be got from the invalid PeImage
617 //
618 goto Finish;
619 }
620
621 //
622 // Measure only application if Application flag is set
623 // Measure drivers and applications if Application flag is not set
624 //
625 if ((!ApplicationRequired) ||
626 (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {
627 //
628 // Print the image path to be measured.
629 //
630 DEBUG_CODE_BEGIN ();
631 CHAR16 *ToText;
632 ToText = ConvertDevicePathToText (
633 DevicePathNode,
634 FALSE,
635 TRUE
636 );
637 if (ToText != NULL) {
638 DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));
639 FreePool (ToText);
640 }
641 DEBUG_CODE_END ();
642
643 //
644 // Measure PE image into TPM log.
645 //
646 Status = TrEEMeasurePeImage (
647 TreeProtocol,
648 (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer,
649 FileSize,
650 (UINTN) ImageContext.ImageAddress,
651 ImageContext.ImageType,
652 DevicePathNode
653 );
654 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler - TrEEMeasurePeImage - %r\n", Status));
655 }
656
657 //
658 // Done, free the allocated resource.
659 //
660 Finish:
661 if (OrigDevicePathNode != NULL) {
662 FreePool (OrigDevicePathNode);
663 }
664
665 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler - %r\n", Status));
666
667 return Status;
668 }
669
670 /**
671 Register the security handler to provide TPM measure boot service.
672
673 @param ImageHandle ImageHandle of the loaded driver.
674 @param SystemTable Pointer to the EFI System Table.
675
676 @retval EFI_SUCCESS Register successfully.
677 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
678 **/
679 EFI_STATUS
680 EFIAPI
681 DxeTpm2MeasureBootLibConstructor (
682 IN EFI_HANDLE ImageHandle,
683 IN EFI_SYSTEM_TABLE *SystemTable
684 )
685 {
686 EFI_HOB_GUID_TYPE *GuidHob;
687
688 GuidHob = NULL;
689
690 GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);
691
692 if (GuidHob != NULL) {
693 mTrEEMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);
694 }
695
696 return RegisterSecurity2Handler (
697 DxeTpm2MeasureBootHandler,
698 EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED
699 );
700 }