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