]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
ArmVirtPkg/ArmVirtQemu: Revert "limit ACPI support to v5.0 and higher"
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasureBootLib / DxeTpmMeasureBootLib.c
... / ...
CommitLineData
1/** @file\r
2 The library instance provides security service of TPM measure boot.\r
3\r
4 Caution: This file requires additional review when modified.\r
5 This library will have external input - PE/COFF image and GPT partition.\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8\r
9 DxeTpmMeasureBootLibImageRead() function will make sure the PE/COFF image content\r
10 read is within the image buffer.\r
11\r
12 TcgMeasurePeImage() function will accept untrusted PE/COFF image and validate its\r
13 data structure within this image buffer before use.\r
14\r
15 TcgMeasureGptTable() function will receive untrusted GPT partition table, and parse\r
16 partition data carefully.\r
17\r
18Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
19SPDX-License-Identifier: BSD-2-Clause-Patent\r
20\r
21**/\r
22\r
23#include <PiDxe.h>\r
24\r
25#include <Protocol/TcgService.h>\r
26#include <Protocol/BlockIo.h>\r
27#include <Protocol/DiskIo.h>\r
28#include <Protocol/FirmwareVolumeBlock.h>\r
29\r
30#include <Guid/MeasuredFvHob.h>\r
31\r
32#include <Library/BaseLib.h>\r
33#include <Library/DebugLib.h>\r
34#include <Library/BaseMemoryLib.h>\r
35#include <Library/MemoryAllocationLib.h>\r
36#include <Library/DevicePathLib.h>\r
37#include <Library/UefiBootServicesTableLib.h>\r
38#include <Library/BaseCryptLib.h>\r
39#include <Library/PeCoffLib.h>\r
40#include <Library/SecurityManagementLib.h>\r
41#include <Library/HobLib.h>\r
42\r
43//\r
44// Flag to check GPT partition. It only need be measured once.\r
45//\r
46BOOLEAN mMeasureGptTableFlag = FALSE;\r
47UINTN mMeasureGptCount = 0;\r
48VOID *mFileBuffer;\r
49UINTN mTpmImageSize;\r
50//\r
51// Measured FV handle cache\r
52//\r
53EFI_HANDLE mCacheMeasuredHandle = NULL;\r
54MEASURED_HOB_DATA *mMeasuredHobData = NULL;\r
55\r
56/**\r
57 Reads contents of a PE/COFF image in memory buffer.\r
58\r
59 Caution: This function may receive untrusted input.\r
60 PE/COFF image is external input, so this function will make sure the PE/COFF image content\r
61 read is within the image buffer.\r
62\r
63 @param FileHandle Pointer to the file handle to read the PE/COFF image.\r
64 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
65 @param ReadSize On input, the size in bytes of the requested read operation.\r
66 On output, the number of bytes actually read.\r
67 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
68\r
69 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73DxeTpmMeasureBootLibImageRead (\r
74 IN VOID *FileHandle,\r
75 IN UINTN FileOffset,\r
76 IN OUT UINTN *ReadSize,\r
77 OUT VOID *Buffer\r
78 )\r
79{\r
80 UINTN EndPosition;\r
81\r
82 if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
83 return EFI_INVALID_PARAMETER;\r
84 }\r
85\r
86 if (MAX_ADDRESS - FileOffset < *ReadSize) {\r
87 return EFI_INVALID_PARAMETER;\r
88 }\r
89\r
90 EndPosition = FileOffset + *ReadSize;\r
91 if (EndPosition > mTpmImageSize) {\r
92 *ReadSize = (UINT32)(mTpmImageSize - FileOffset);\r
93 }\r
94\r
95 if (FileOffset >= mTpmImageSize) {\r
96 *ReadSize = 0;\r
97 }\r
98\r
99 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);\r
100\r
101 return EFI_SUCCESS;\r
102}\r
103\r
104/**\r
105 Measure GPT table data into TPM log.\r
106\r
107 Caution: This function may receive untrusted input.\r
108 The GPT partition table is external input, so this function should parse partition data carefully.\r
109\r
110 @param TcgProtocol Pointer to the located TCG protocol instance.\r
111 @param GptHandle Handle that GPT partition was installed.\r
112\r
113 @retval EFI_SUCCESS Successfully measure GPT table.\r
114 @retval EFI_UNSUPPORTED Not support GPT table on the given handle.\r
115 @retval EFI_DEVICE_ERROR Can't get GPT table because device error.\r
116 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.\r
117 @retval other error value\r
118**/\r
119EFI_STATUS\r
120EFIAPI\r
121TcgMeasureGptTable (\r
122 IN EFI_TCG_PROTOCOL *TcgProtocol,\r
123 IN EFI_HANDLE GptHandle\r
124 )\r
125{\r
126 EFI_STATUS Status;\r
127 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
128 EFI_DISK_IO_PROTOCOL *DiskIo;\r
129 EFI_PARTITION_TABLE_HEADER *PrimaryHeader;\r
130 EFI_PARTITION_ENTRY *PartitionEntry;\r
131 UINT8 *EntryPtr;\r
132 UINTN NumberOfPartition;\r
133 UINT32 Index;\r
134 TCG_PCR_EVENT *TcgEvent;\r
135 EFI_GPT_DATA *GptData;\r
136 UINT32 EventSize;\r
137 UINT32 EventNumber;\r
138 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
139\r
140 if (mMeasureGptCount > 0) {\r
141 return EFI_SUCCESS;\r
142 }\r
143\r
144 Status = gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo);\r
145 if (EFI_ERROR (Status)) {\r
146 return EFI_UNSUPPORTED;\r
147 }\r
148 Status = gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID**)&DiskIo);\r
149 if (EFI_ERROR (Status)) {\r
150 return EFI_UNSUPPORTED;\r
151 }\r
152 //\r
153 // Read the EFI Partition Table Header\r
154 //\r
155 PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize);\r
156 if (PrimaryHeader == NULL) {\r
157 return EFI_OUT_OF_RESOURCES;\r
158 }\r
159 Status = DiskIo->ReadDisk (\r
160 DiskIo,\r
161 BlockIo->Media->MediaId,\r
162 1 * BlockIo->Media->BlockSize,\r
163 BlockIo->Media->BlockSize,\r
164 (UINT8 *)PrimaryHeader\r
165 );\r
166 if (EFI_ERROR (Status)) {\r
167 DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n"));\r
168 FreePool (PrimaryHeader);\r
169 return EFI_DEVICE_ERROR;\r
170 }\r
171 //\r
172 // Read the partition entry.\r
173 //\r
174 EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);\r
175 if (EntryPtr == NULL) {\r
176 FreePool (PrimaryHeader);\r
177 return EFI_OUT_OF_RESOURCES;\r
178 }\r
179 Status = DiskIo->ReadDisk (\r
180 DiskIo,\r
181 BlockIo->Media->MediaId,\r
182 MultU64x32(PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),\r
183 PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry,\r
184 EntryPtr\r
185 );\r
186 if (EFI_ERROR (Status)) {\r
187 FreePool (PrimaryHeader);\r
188 FreePool (EntryPtr);\r
189 return EFI_DEVICE_ERROR;\r
190 }\r
191\r
192 //\r
193 // Count the valid partition\r
194 //\r
195 PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr;\r
196 NumberOfPartition = 0;\r
197 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
198 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
199 NumberOfPartition++;\r
200 }\r
201 PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
202 }\r
203\r
204 //\r
205 // Prepare Data for Measurement\r
206 //\r
207 EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions)\r
208 + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
209 TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT_HDR));\r
210 if (TcgEvent == NULL) {\r
211 FreePool (PrimaryHeader);\r
212 FreePool (EntryPtr);\r
213 return EFI_OUT_OF_RESOURCES;\r
214 }\r
215\r
216 TcgEvent->PCRIndex = 5;\r
217 TcgEvent->EventType = EV_EFI_GPT_EVENT;\r
218 TcgEvent->EventSize = EventSize;\r
219 GptData = (EFI_GPT_DATA *) TcgEvent->Event;\r
220\r
221 //\r
222 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
223 //\r
224 CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r
225 GptData->NumberOfPartitions = NumberOfPartition;\r
226 //\r
227 // Copy the valid partition entry\r
228 //\r
229 PartitionEntry = (EFI_PARTITION_ENTRY*)EntryPtr;\r
230 NumberOfPartition = 0;\r
231 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
232 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
233 CopyMem (\r
234 (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,\r
235 (UINT8 *)PartitionEntry,\r
236 PrimaryHeader->SizeOfPartitionEntry\r
237 );\r
238 NumberOfPartition++;\r
239 }\r
240 PartitionEntry =(EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
241 }\r
242\r
243 //\r
244 // Measure the GPT data\r
245 //\r
246 EventNumber = 1;\r
247 Status = TcgProtocol->HashLogExtendEvent (\r
248 TcgProtocol,\r
249 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData,\r
250 (UINT64) TcgEvent->EventSize,\r
251 TPM_ALG_SHA,\r
252 TcgEvent,\r
253 &EventNumber,\r
254 &EventLogLastEntry\r
255 );\r
256 if (!EFI_ERROR (Status)) {\r
257 mMeasureGptCount++;\r
258 }\r
259\r
260 FreePool (PrimaryHeader);\r
261 FreePool (EntryPtr);\r
262 FreePool (TcgEvent);\r
263\r
264 return Status;\r
265}\r
266\r
267/**\r
268 Measure PE image into TPM log based on the authenticode image hashing in\r
269 PE/COFF Specification 8.0 Appendix A.\r
270\r
271 Caution: This function may receive untrusted input.\r
272 PE/COFF image is external input, so this function will validate its data structure\r
273 within this image buffer before use.\r
274\r
275 Notes: PE/COFF image has been checked by BasePeCoffLib PeCoffLoaderGetImageInfo() in\r
276 its caller function DxeTpmMeasureBootHandler().\r
277\r
278 @param[in] TcgProtocol Pointer to the located TCG protocol instance.\r
279 @param[in] ImageAddress Start address of image buffer.\r
280 @param[in] ImageSize Image size\r
281 @param[in] LinkTimeBase Address that the image is loaded into memory.\r
282 @param[in] ImageType Image subsystem type.\r
283 @param[in] FilePath File path is corresponding to the input image.\r
284\r
285 @retval EFI_SUCCESS Successfully measure image.\r
286 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.\r
287 @retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format.\r
288 @retval other error value\r
289\r
290**/\r
291EFI_STATUS\r
292EFIAPI\r
293TcgMeasurePeImage (\r
294 IN EFI_TCG_PROTOCOL *TcgProtocol,\r
295 IN EFI_PHYSICAL_ADDRESS ImageAddress,\r
296 IN UINTN ImageSize,\r
297 IN UINTN LinkTimeBase,\r
298 IN UINT16 ImageType,\r
299 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
300 )\r
301{\r
302 EFI_STATUS Status;\r
303 TCG_PCR_EVENT *TcgEvent;\r
304 EFI_IMAGE_LOAD_EVENT *ImageLoad;\r
305 UINT32 FilePathSize;\r
306 VOID *Sha1Ctx;\r
307 UINTN CtxSize;\r
308 EFI_IMAGE_DOS_HEADER *DosHdr;\r
309 UINT32 PeCoffHeaderOffset;\r
310 EFI_IMAGE_SECTION_HEADER *Section;\r
311 UINT8 *HashBase;\r
312 UINTN HashSize;\r
313 UINTN SumOfBytesHashed;\r
314 EFI_IMAGE_SECTION_HEADER *SectionHeader;\r
315 UINTN Index;\r
316 UINTN Pos;\r
317 UINT32 EventSize;\r
318 UINT32 EventNumber;\r
319 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
320 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
321 UINT32 NumberOfRvaAndSizes;\r
322 BOOLEAN HashStatus;\r
323 UINT32 CertSize;\r
324\r
325 Status = EFI_UNSUPPORTED;\r
326 ImageLoad = NULL;\r
327 SectionHeader = NULL;\r
328 Sha1Ctx = NULL;\r
329 FilePathSize = (UINT32) GetDevicePathSize (FilePath);\r
330\r
331 //\r
332 // Determine destination PCR by BootPolicy\r
333 //\r
334 EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize;\r
335 TcgEvent = AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT));\r
336 if (TcgEvent == NULL) {\r
337 return EFI_OUT_OF_RESOURCES;\r
338 }\r
339\r
340 TcgEvent->EventSize = EventSize;\r
341 ImageLoad = (EFI_IMAGE_LOAD_EVENT *) TcgEvent->Event;\r
342\r
343 switch (ImageType) {\r
344 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
345 TcgEvent->EventType = EV_EFI_BOOT_SERVICES_APPLICATION;\r
346 TcgEvent->PCRIndex = 4;\r
347 break;\r
348 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
349 TcgEvent->EventType = EV_EFI_BOOT_SERVICES_DRIVER;\r
350 TcgEvent->PCRIndex = 2;\r
351 break;\r
352 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
353 TcgEvent->EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;\r
354 TcgEvent->PCRIndex = 2;\r
355 break;\r
356 default:\r
357 DEBUG ((\r
358 EFI_D_ERROR,\r
359 "TcgMeasurePeImage: Unknown subsystem type %d",\r
360 ImageType\r
361 ));\r
362 goto Finish;\r
363 }\r
364\r
365 ImageLoad->ImageLocationInMemory = ImageAddress;\r
366 ImageLoad->ImageLengthInMemory = ImageSize;\r
367 ImageLoad->ImageLinkTimeAddress = LinkTimeBase;\r
368 ImageLoad->LengthOfDevicePath = FilePathSize;\r
369 if ((FilePath != NULL) && (FilePathSize != 0)) {\r
370 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);\r
371 }\r
372\r
373 //\r
374 // Check PE/COFF image\r
375 //\r
376 DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;\r
377 PeCoffHeaderOffset = 0;\r
378 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
379 PeCoffHeaderOffset = DosHdr->e_lfanew;\r
380 }\r
381\r
382 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress + PeCoffHeaderOffset);\r
383 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
384 goto Finish;\r
385 }\r
386\r
387 //\r
388 // PE/COFF Image Measurement\r
389 //\r
390 // NOTE: The following codes/steps are based upon the authenticode image hashing in\r
391 // PE/COFF Specification 8.0 Appendix A.\r
392 //\r
393 //\r
394\r
395 // 1. Load the image header into memory.\r
396\r
397 // 2. Initialize a SHA hash context.\r
398 CtxSize = Sha1GetContextSize ();\r
399 Sha1Ctx = AllocatePool (CtxSize);\r
400 if (Sha1Ctx == NULL) {\r
401 Status = EFI_OUT_OF_RESOURCES;\r
402 goto Finish;\r
403 }\r
404\r
405 HashStatus = Sha1Init (Sha1Ctx);\r
406 if (!HashStatus) {\r
407 goto Finish;\r
408 }\r
409\r
410 //\r
411 // Measuring PE/COFF Image Header;\r
412 // But CheckSum field and SECURITY data directory (certificate) are excluded\r
413 //\r
414\r
415 //\r
416 // 3. Calculate the distance from the base of the image header to the image checksum address.\r
417 // 4. Hash the image header from its base to beginning of the image checksum.\r
418 //\r
419 HashBase = (UINT8 *) (UINTN) ImageAddress;\r
420 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
421 //\r
422 // Use PE32 offset\r
423 //\r
424 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
425 HashSize = (UINTN) (&Hdr.Pe32->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
426 } else {\r
427 //\r
428 // Use PE32+ offset\r
429 //\r
430 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
431 HashSize = (UINTN) (&Hdr.Pe32Plus->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
432 }\r
433\r
434 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
435 if (!HashStatus) {\r
436 goto Finish;\r
437 }\r
438\r
439 //\r
440 // 5. Skip over the image checksum (it occupies a single ULONG).\r
441 //\r
442 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
443 //\r
444 // 6. Since there is no Cert Directory in optional header, hash everything\r
445 // from the end of the checksum to the end of image header.\r
446 //\r
447 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
448 //\r
449 // Use PE32 offset.\r
450 //\r
451 HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
452 HashSize = Hdr.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
453 } else {\r
454 //\r
455 // Use PE32+ offset.\r
456 //\r
457 HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
458 HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
459 }\r
460\r
461 if (HashSize != 0) {\r
462 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
463 if (!HashStatus) {\r
464 goto Finish;\r
465 }\r
466 }\r
467 } else {\r
468 //\r
469 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.\r
470 //\r
471 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
472 //\r
473 // Use PE32 offset\r
474 //\r
475 HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
476 HashSize = (UINTN) (&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
477 } else {\r
478 //\r
479 // Use PE32+ offset\r
480 //\r
481 HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
482 HashSize = (UINTN) (&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
483 }\r
484\r
485 if (HashSize != 0) {\r
486 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
487 if (!HashStatus) {\r
488 goto Finish;\r
489 }\r
490 }\r
491\r
492 //\r
493 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)\r
494 // 9. Hash everything from the end of the Cert Directory to the end of image header.\r
495 //\r
496 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
497 //\r
498 // Use PE32 offset\r
499 //\r
500 HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
501 HashSize = Hdr.Pe32->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
502 } else {\r
503 //\r
504 // Use PE32+ offset\r
505 //\r
506 HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
507 HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
508 }\r
509\r
510 if (HashSize != 0) {\r
511 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
512 if (!HashStatus) {\r
513 goto Finish;\r
514 }\r
515 }\r
516 }\r
517\r
518 //\r
519 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header\r
520 //\r
521 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
522 //\r
523 // Use PE32 offset\r
524 //\r
525 SumOfBytesHashed = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r
526 } else {\r
527 //\r
528 // Use PE32+ offset\r
529 //\r
530 SumOfBytesHashed = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;\r
531 }\r
532\r
533 //\r
534 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER\r
535 // structures in the image. The 'NumberOfSections' field of the image\r
536 // header indicates how big the table should be. Do not include any\r
537 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.\r
538 //\r
539 SectionHeader = (EFI_IMAGE_SECTION_HEADER *) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER) * Hdr.Pe32->FileHeader.NumberOfSections);\r
540 if (SectionHeader == NULL) {\r
541 Status = EFI_OUT_OF_RESOURCES;\r
542 goto Finish;\r
543 }\r
544\r
545 //\r
546 // 12. Using the 'PointerToRawData' in the referenced section headers as\r
547 // a key, arrange the elements in the table in ascending order. In other\r
548 // words, sort the section headers according to the disk-file offset of\r
549 // the section.\r
550 //\r
551 Section = (EFI_IMAGE_SECTION_HEADER *) (\r
552 (UINT8 *) (UINTN) ImageAddress +\r
553 PeCoffHeaderOffset +\r
554 sizeof(UINT32) +\r
555 sizeof(EFI_IMAGE_FILE_HEADER) +\r
556 Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r
557 );\r
558 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r
559 Pos = Index;\r
560 while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {\r
561 CopyMem (&SectionHeader[Pos], &SectionHeader[Pos - 1], sizeof(EFI_IMAGE_SECTION_HEADER));\r
562 Pos--;\r
563 }\r
564 CopyMem (&SectionHeader[Pos], Section, sizeof(EFI_IMAGE_SECTION_HEADER));\r
565 Section += 1;\r
566 }\r
567\r
568 //\r
569 // 13. Walk through the sorted table, bring the corresponding section\r
570 // into memory, and hash the entire section (using the 'SizeOfRawData'\r
571 // field in the section header to determine the amount of data to hash).\r
572 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .\r
573 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.\r
574 //\r
575 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r
576 Section = (EFI_IMAGE_SECTION_HEADER *) &SectionHeader[Index];\r
577 if (Section->SizeOfRawData == 0) {\r
578 continue;\r
579 }\r
580 HashBase = (UINT8 *) (UINTN) ImageAddress + Section->PointerToRawData;\r
581 HashSize = (UINTN) Section->SizeOfRawData;\r
582\r
583 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
584 if (!HashStatus) {\r
585 goto Finish;\r
586 }\r
587\r
588 SumOfBytesHashed += HashSize;\r
589 }\r
590\r
591 //\r
592 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra\r
593 // data in the file that needs to be added to the hash. This data begins\r
594 // at file offset SUM_OF_BYTES_HASHED and its length is:\r
595 // FileSize - (CertDirectory->Size)\r
596 //\r
597 if (ImageSize > SumOfBytesHashed) {\r
598 HashBase = (UINT8 *) (UINTN) ImageAddress + SumOfBytesHashed;\r
599\r
600 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
601 CertSize = 0;\r
602 } else {\r
603 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
604 //\r
605 // Use PE32 offset.\r
606 //\r
607 CertSize = Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;\r
608 } else {\r
609 //\r
610 // Use PE32+ offset.\r
611 //\r
612 CertSize = Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;\r
613 }\r
614 }\r
615\r
616 if (ImageSize > CertSize + SumOfBytesHashed) {\r
617 HashSize = (UINTN) (ImageSize - CertSize - SumOfBytesHashed);\r
618\r
619 HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
620 if (!HashStatus) {\r
621 goto Finish;\r
622 }\r
623 } else if (ImageSize < CertSize + SumOfBytesHashed) {\r
624 goto Finish;\r
625 }\r
626 }\r
627\r
628 //\r
629 // 17. Finalize the SHA hash.\r
630 //\r
631 HashStatus = Sha1Final (Sha1Ctx, (UINT8 *) &TcgEvent->Digest);\r
632 if (!HashStatus) {\r
633 goto Finish;\r
634 }\r
635\r
636 //\r
637 // Log the PE data\r
638 //\r
639 EventNumber = 1;\r
640 Status = TcgProtocol->HashLogExtendEvent (\r
641 TcgProtocol,\r
642 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) NULL,\r
643 0,\r
644 TPM_ALG_SHA,\r
645 TcgEvent,\r
646 &EventNumber,\r
647 &EventLogLastEntry\r
648 );\r
649 if (Status == EFI_OUT_OF_RESOURCES) {\r
650 //\r
651 // Out of resource here means the image is hashed and its result is extended to PCR.\r
652 // But the event log can't be saved since log area is full.\r
653 // Just return EFI_SUCCESS in order not to block the image load.\r
654 //\r
655 Status = EFI_SUCCESS;\r
656 }\r
657\r
658Finish:\r
659 FreePool (TcgEvent);\r
660\r
661 if (SectionHeader != NULL) {\r
662 FreePool (SectionHeader);\r
663 }\r
664\r
665 if (Sha1Ctx != NULL ) {\r
666 FreePool (Sha1Ctx);\r
667 }\r
668 return Status;\r
669}\r
670\r
671/**\r
672 The security handler is used to abstract platform-specific policy\r
673 from the DXE core response to an attempt to use a file that returns a\r
674 given status for the authentication check from the section extraction protocol.\r
675\r
676 The possible responses in a given SAP implementation may include locking\r
677 flash upon failure to authenticate, attestation logging for all signed drivers,\r
678 and other exception operations. The File parameter allows for possible logging\r
679 within the SAP of the driver.\r
680\r
681 If File is NULL, then EFI_ACCESS_DENIED is returned.\r
682\r
683 If the file specified by File with an authentication status specified by\r
684 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r
685\r
686 If the file specified by File with an authentication status specified by\r
687 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,\r
688 then EFI_ACCESS_DENIED is returned.\r
689\r
690 If the file specified by File with an authentication status specified by\r
691 AuthenticationStatus is not safe for the DXE Core to use right now, but it\r
692 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is\r
693 returned.\r
694\r
695 @param[in] AuthenticationStatus This is the authentication status returned\r
696 from the securitymeasurement services for the\r
697 input file.\r
698 @param[in] File This is a pointer to the device path of the file that is\r
699 being dispatched. This will optionally be used for logging.\r
700 @param[in] FileBuffer File buffer matches the input file device path.\r
701 @param[in] FileSize Size of File buffer matches the input file device path.\r
702 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
703\r
704 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL\r
705 FileBuffer did authenticate, and the platform policy dictates\r
706 that the DXE Foundation may use the file.\r
707 @retval other error value\r
708**/\r
709EFI_STATUS\r
710EFIAPI\r
711DxeTpmMeasureBootHandler (\r
712 IN UINT32 AuthenticationStatus,\r
713 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
714 IN VOID *FileBuffer,\r
715 IN UINTN FileSize,\r
716 IN BOOLEAN BootPolicy\r
717 )\r
718{\r
719 EFI_TCG_PROTOCOL *TcgProtocol;\r
720 EFI_STATUS Status;\r
721 TCG_EFI_BOOT_SERVICE_CAPABILITY ProtocolCapability;\r
722 UINT32 TCGFeatureFlags;\r
723 EFI_PHYSICAL_ADDRESS EventLogLocation;\r
724 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
725 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
726 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
727 EFI_HANDLE Handle;\r
728 EFI_HANDLE TempHandle;\r
729 BOOLEAN ApplicationRequired;\r
730 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
731 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
732 EFI_PHYSICAL_ADDRESS FvAddress;\r
733 UINT32 Index;\r
734\r
735 //\r
736 // Check for invalid parameters.\r
737 //\r
738 if (File == NULL) {\r
739 return EFI_ACCESS_DENIED;\r
740 }\r
741\r
742 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
743 if (EFI_ERROR (Status)) {\r
744 //\r
745 // TCG protocol is not installed. So, TPM is not present.\r
746 // Don't do any measurement, and directly return EFI_SUCCESS.\r
747 //\r
748 return EFI_SUCCESS;\r
749 }\r
750\r
751 ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
752 Status = TcgProtocol->StatusCheck (\r
753 TcgProtocol,\r
754 &ProtocolCapability,\r
755 &TCGFeatureFlags,\r
756 &EventLogLocation,\r
757 &EventLogLastEntry\r
758 );\r
759 if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {\r
760 //\r
761 // TPM device doesn't work or activate.\r
762 //\r
763 return EFI_SUCCESS;\r
764 }\r
765\r
766 //\r
767 // Copy File Device Path\r
768 //\r
769 OrigDevicePathNode = DuplicateDevicePath (File);\r
770\r
771 //\r
772 // 1. Check whether this device path support BlockIo protocol.\r
773 // Is so, this device path may be a GPT device path.\r
774 //\r
775 DevicePathNode = OrigDevicePathNode;\r
776 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);\r
777 if (!EFI_ERROR (Status) && !mMeasureGptTableFlag) {\r
778 //\r
779 // Find the gpt partition on the given devicepath\r
780 //\r
781 DevicePathNode = OrigDevicePathNode;\r
782 ASSERT (DevicePathNode != NULL);\r
783 while (!IsDevicePathEnd (DevicePathNode)) {\r
784 //\r
785 // Find the Gpt partition\r
786 //\r
787 if (DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH &&\r
788 DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {\r
789 //\r
790 // Check whether it is a gpt partition or not\r
791 //\r
792 if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER &&\r
793 ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {\r
794\r
795 //\r
796 // Change the partition device path to its parent device path (disk) and get the handle.\r
797 //\r
798 DevicePathNode->Type = END_DEVICE_PATH_TYPE;\r
799 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
800 DevicePathNode = OrigDevicePathNode;\r
801 Status = gBS->LocateDevicePath (\r
802 &gEfiDiskIoProtocolGuid,\r
803 &DevicePathNode,\r
804 &Handle\r
805 );\r
806 if (!EFI_ERROR (Status)) {\r
807 //\r
808 // Measure GPT disk.\r
809 //\r
810 Status = TcgMeasureGptTable (TcgProtocol, Handle);\r
811 if (!EFI_ERROR (Status)) {\r
812 //\r
813 // GPT disk check done.\r
814 //\r
815 mMeasureGptTableFlag = TRUE;\r
816 }\r
817 }\r
818 FreePool (OrigDevicePathNode);\r
819 OrigDevicePathNode = DuplicateDevicePath (File);\r
820 ASSERT (OrigDevicePathNode != NULL);\r
821 break;\r
822 }\r
823 }\r
824 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
825 }\r
826 }\r
827\r
828 //\r
829 // 2. Measure PE image.\r
830 //\r
831 ApplicationRequired = FALSE;\r
832\r
833 //\r
834 // Check whether this device path support FVB protocol.\r
835 //\r
836 DevicePathNode = OrigDevicePathNode;\r
837 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);\r
838 if (!EFI_ERROR (Status)) {\r
839 //\r
840 // Don't check FV image, and directly return EFI_SUCCESS.\r
841 // It can be extended to the specific FV authentication according to the different requirement.\r
842 //\r
843 if (IsDevicePathEnd (DevicePathNode)) {\r
844 return EFI_SUCCESS;\r
845 }\r
846 //\r
847 // The PE image from unmeasured Firmware volume need be measured\r
848 // The PE image from measured Firmware volume will be measured according to policy below.\r
849 // If it is driver, do not measure\r
850 // If it is application, still measure.\r
851 //\r
852 ApplicationRequired = TRUE;\r
853\r
854 if (mCacheMeasuredHandle != Handle && mMeasuredHobData != NULL) {\r
855 //\r
856 // Search for Root FV of this PE image\r
857 //\r
858 TempHandle = Handle;\r
859 do {\r
860 Status = gBS->HandleProtocol(\r
861 TempHandle,\r
862 &gEfiFirmwareVolumeBlockProtocolGuid,\r
863 (VOID**)&FvbProtocol\r
864 );\r
865 TempHandle = FvbProtocol->ParentHandle;\r
866 } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);\r
867\r
868 //\r
869 // Search in measured FV Hob\r
870 //\r
871 Status = FvbProtocol->GetPhysicalAddress(FvbProtocol, &FvAddress);\r
872 if (EFI_ERROR(Status)){\r
873 return Status;\r
874 }\r
875\r
876 ApplicationRequired = FALSE;\r
877\r
878 for (Index = 0; Index < mMeasuredHobData->Num; Index++) {\r
879 if(mMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r
880 //\r
881 // Cache measured FV for next measurement\r
882 //\r
883 mCacheMeasuredHandle = Handle;\r
884 ApplicationRequired = TRUE;\r
885 break;\r
886 }\r
887 }\r
888 }\r
889 }\r
890\r
891 //\r
892 // File is not found.\r
893 //\r
894 if (FileBuffer == NULL) {\r
895 Status = EFI_SECURITY_VIOLATION;\r
896 goto Finish;\r
897 }\r
898\r
899 mTpmImageSize = FileSize;\r
900 mFileBuffer = FileBuffer;\r
901\r
902 //\r
903 // Measure PE Image\r
904 //\r
905 DevicePathNode = OrigDevicePathNode;\r
906 ZeroMem (&ImageContext, sizeof (ImageContext));\r
907 ImageContext.Handle = (VOID *) FileBuffer;\r
908 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeTpmMeasureBootLibImageRead;\r
909\r
910 //\r
911 // Get information about the image being loaded\r
912 //\r
913 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
914 if (EFI_ERROR (Status)) {\r
915 //\r
916 // The information can't be got from the invalid PeImage\r
917 //\r
918 goto Finish;\r
919 }\r
920\r
921 //\r
922 // Measure only application if Application flag is set\r
923 // Measure drivers and applications if Application flag is not set\r
924 //\r
925 if ((!ApplicationRequired) ||\r
926 (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {\r
927 //\r
928 // Print the image path to be measured.\r
929 //\r
930 DEBUG_CODE_BEGIN ();\r
931 CHAR16 *ToText;\r
932 ToText = ConvertDevicePathToText (\r
933 DevicePathNode,\r
934 FALSE,\r
935 TRUE\r
936 );\r
937 if (ToText != NULL) {\r
938 DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r
939 FreePool (ToText);\r
940 }\r
941 DEBUG_CODE_END ();\r
942\r
943 //\r
944 // Measure PE image into TPM log.\r
945 //\r
946 Status = TcgMeasurePeImage (\r
947 TcgProtocol,\r
948 (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer,\r
949 FileSize,\r
950 (UINTN) ImageContext.ImageAddress,\r
951 ImageContext.ImageType,\r
952 DevicePathNode\r
953 );\r
954 }\r
955\r
956 //\r
957 // Done, free the allocated resource.\r
958 //\r
959Finish:\r
960 if (OrigDevicePathNode != NULL) {\r
961 FreePool (OrigDevicePathNode);\r
962 }\r
963\r
964 return Status;\r
965}\r
966\r
967/**\r
968 Register the security handler to provide TPM measure boot service.\r
969\r
970 @param ImageHandle ImageHandle of the loaded driver.\r
971 @param SystemTable Pointer to the EFI System Table.\r
972\r
973 @retval EFI_SUCCESS Register successfully.\r
974 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
975**/\r
976EFI_STATUS\r
977EFIAPI\r
978DxeTpmMeasureBootLibConstructor (\r
979 IN EFI_HANDLE ImageHandle,\r
980 IN EFI_SYSTEM_TABLE *SystemTable\r
981 )\r
982{\r
983 EFI_HOB_GUID_TYPE *GuidHob;\r
984\r
985 GuidHob = NULL;\r
986\r
987 GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);\r
988\r
989 if (GuidHob != NULL) {\r
990 mMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r
991 }\r
992\r
993 return RegisterSecurity2Handler (\r
994 DxeTpmMeasureBootHandler,\r
995 EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r
996 );\r
997}\r