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