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