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