]>
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 | |
c411b485 MK |
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 | |
c411b485 MK |
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 | |
c411b485 MK |
76 | IN VOID *FileHandle,\r |
77 | IN UINTN FileOffset,\r | |
78 | IN OUT UINTN *ReadSize,\r | |
79 | OUT VOID *Buffer\r | |
c1d93242 JY |
80 | )\r |
81 | {\r | |
c411b485 | 82 | UINTN EndPosition;\r |
c1d93242 | 83 | \r |
c411b485 | 84 | if ((FileHandle == NULL) || (ReadSize == NULL) || (Buffer == NULL)) {\r |
c1d93242 JY |
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 | |
c411b485 | 101 | CopyMem (Buffer, (UINT8 *)((UINTN)FileHandle + FileOffset), *ReadSize);\r |
c1d93242 JY |
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 | |
c411b485 MK |
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 | |
136 | EFI_TCG2_EVENT *Tcg2Event;\r | |
137 | EFI_GPT_DATA *GptData;\r | |
138 | UINT32 EventSize;\r | |
c1d93242 | 139 | \r |
1abfa4ce | 140 | if (mTcg2MeasureGptCount > 0) {\r |
c1d93242 JY |
141 | return EFI_SUCCESS;\r |
142 | }\r | |
143 | \r | |
c411b485 | 144 | Status = gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r |
c1d93242 JY |
145 | if (EFI_ERROR (Status)) {\r |
146 | return EFI_UNSUPPORTED;\r | |
147 | }\r | |
c411b485 MK |
148 | \r |
149 | Status = gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo);\r | |
c1d93242 JY |
150 | if (EFI_ERROR (Status)) {\r |
151 | return EFI_UNSUPPORTED;\r | |
152 | }\r | |
c411b485 | 153 | \r |
c1d93242 JY |
154 | //\r |
155 | // Read the EFI Partition Table Header\r | |
b3548d32 | 156 | //\r |
c411b485 | 157 | PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *)AllocatePool (BlockIo->Media->BlockSize);\r |
c1d93242 JY |
158 | if (PrimaryHeader == NULL) {\r |
159 | return EFI_OUT_OF_RESOURCES;\r | |
b3548d32 | 160 | }\r |
c411b485 | 161 | \r |
c1d93242 JY |
162 | Status = DiskIo->ReadDisk (\r |
163 | DiskIo,\r | |
164 | BlockIo->Media->MediaId,\r | |
165 | 1 * BlockIo->Media->BlockSize,\r | |
166 | BlockIo->Media->BlockSize,\r | |
167 | (UINT8 *)PrimaryHeader\r | |
168 | );\r | |
169 | if (EFI_ERROR (Status)) {\r | |
e905fbb0 | 170 | DEBUG ((DEBUG_ERROR, "Failed to Read Partition Table Header!\n"));\r |
c1d93242 JY |
171 | FreePool (PrimaryHeader);\r |
172 | return EFI_DEVICE_ERROR;\r | |
b3548d32 | 173 | }\r |
c411b485 | 174 | \r |
c1d93242 JY |
175 | //\r |
176 | // Read the partition entry.\r | |
177 | //\r | |
178 | EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);\r | |
179 | if (EntryPtr == NULL) {\r | |
180 | FreePool (PrimaryHeader);\r | |
181 | return EFI_OUT_OF_RESOURCES;\r | |
182 | }\r | |
c411b485 | 183 | \r |
c1d93242 JY |
184 | Status = DiskIo->ReadDisk (\r |
185 | DiskIo,\r | |
186 | BlockIo->Media->MediaId,\r | |
c411b485 | 187 | MultU64x32 (PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),\r |
c1d93242 JY |
188 | PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry,\r |
189 | EntryPtr\r | |
190 | );\r | |
191 | if (EFI_ERROR (Status)) {\r | |
192 | FreePool (PrimaryHeader);\r | |
193 | FreePool (EntryPtr);\r | |
194 | return EFI_DEVICE_ERROR;\r | |
195 | }\r | |
b3548d32 | 196 | \r |
c1d93242 JY |
197 | //\r |
198 | // Count the valid partition\r | |
199 | //\r | |
200 | PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr;\r | |
201 | NumberOfPartition = 0;\r | |
202 | for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r | |
965268ea | 203 | if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r |
b3548d32 | 204 | NumberOfPartition++;\r |
c1d93242 | 205 | }\r |
c411b485 | 206 | \r |
c1d93242 JY |
207 | PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r |
208 | }\r | |
209 | \r | |
210 | //\r | |
211 | // Prepare Data for Measurement\r | |
b3548d32 LG |
212 | //\r |
213 | EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions)\r | |
c411b485 MK |
214 | + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r |
215 | Tcg2Event = (EFI_TCG2_EVENT *)AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event));\r | |
1abfa4ce | 216 | if (Tcg2Event == NULL) {\r |
c1d93242 JY |
217 | FreePool (PrimaryHeader);\r |
218 | FreePool (EntryPtr);\r | |
219 | return EFI_OUT_OF_RESOURCES;\r | |
220 | }\r | |
221 | \r | |
c411b485 MK |
222 | Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);\r |
223 | Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);\r | |
1abfa4ce JY |
224 | Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r |
225 | Tcg2Event->Header.PCRIndex = 5;\r | |
226 | Tcg2Event->Header.EventType = EV_EFI_GPT_EVENT;\r | |
c411b485 | 227 | GptData = (EFI_GPT_DATA *)Tcg2Event->Event;\r |
c1d93242 JY |
228 | \r |
229 | //\r | |
230 | // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r | |
b3548d32 | 231 | //\r |
c411b485 | 232 | CopyMem ((UINT8 *)GptData, (UINT8 *)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r |
c1d93242 JY |
233 | GptData->NumberOfPartitions = NumberOfPartition;\r |
234 | //\r | |
235 | // Copy the valid partition entry\r | |
236 | //\r | |
c411b485 | 237 | PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr;\r |
c1d93242 JY |
238 | NumberOfPartition = 0;\r |
239 | for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r | |
965268ea | 240 | if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r |
c1d93242 JY |
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 | |
c411b485 MK |
248 | \r |
249 | PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r | |
c1d93242 JY |
250 | }\r |
251 | \r | |
252 | //\r | |
253 | // Measure the GPT data\r | |
254 | //\r | |
1abfa4ce | 255 | Status = Tcg2Protocol->HashLogExtendEvent (\r |
c411b485 MK |
256 | Tcg2Protocol,\r |
257 | 0,\r | |
258 | (EFI_PHYSICAL_ADDRESS)(UINTN)(VOID *)GptData,\r | |
259 | (UINT64)EventSize,\r | |
260 | Tcg2Event\r | |
261 | );\r | |
c1d93242 | 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 | |
b3548d32 | 290 | @retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format.\r |
c1d93242 JY |
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 | |
c411b485 MK |
305 | EFI_STATUS Status;\r |
306 | EFI_TCG2_EVENT *Tcg2Event;\r | |
307 | EFI_IMAGE_LOAD_EVENT *ImageLoad;\r | |
308 | UINT32 FilePathSize;\r | |
309 | UINT32 EventSize;\r | |
c1d93242 | 310 | \r |
c411b485 MK |
311 | Status = EFI_UNSUPPORTED;\r |
312 | ImageLoad = NULL;\r | |
313 | FilePathSize = (UINT32)GetDevicePathSize (FilePath);\r | |
c1d93242 JY |
314 | \r |
315 | //\r | |
316 | // Determine destination PCR by BootPolicy\r | |
317 | //\r | |
318 | EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize;\r | |
c411b485 | 319 | Tcg2Event = AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event));\r |
1abfa4ce | 320 | if (Tcg2Event == NULL) {\r |
c1d93242 JY |
321 | return EFI_OUT_OF_RESOURCES;\r |
322 | }\r | |
323 | \r | |
c411b485 MK |
324 | Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);\r |
325 | Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);\r | |
1abfa4ce | 326 | Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r |
c411b485 | 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 | |
e905fbb0 | 344 | DEBUG_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 | 362 | Status = Tcg2Protocol->HashLogExtendEvent (\r |
c411b485 MK |
363 | Tcg2Protocol,\r |
364 | PE_COFF_IMAGE,\r | |
365 | ImageAddress,\r | |
366 | ImageSize,\r | |
367 | Tcg2Event\r | |
368 | );\r | |
c1d93242 JY |
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 | |
d6b926e7 | 372 | // But the event log can't be saved since log area is full.\r |
c1d93242 JY |
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 | |
b3548d32 LG |
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 | |
c1d93242 | 388 | \r |
b3548d32 LG |
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 | |
c1d93242 JY |
392 | within the SAP of the driver.\r |
393 | \r | |
b3548d32 | 394 | If the file specified by File with an authentication status specified by\r |
c1d93242 JY |
395 | AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r |
396 | \r | |
b3548d32 LG |
397 | If the file specified by File with an authentication status specified by\r |
398 | AuthenticationStatus is not safe for the DXE Core to use under any circumstances,\r | |
c1d93242 JY |
399 | then EFI_ACCESS_DENIED is returned.\r |
400 | \r | |
b3548d32 LG |
401 | If the file specified by File with an authentication status specified by\r |
402 | AuthenticationStatus is not safe for the DXE Core to use right now, but it\r | |
403 | might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is\r | |
c1d93242 JY |
404 | returned.\r |
405 | \r | |
1755932f GJ |
406 | If check image specified by FileBuffer and File is NULL meanwhile, return EFI_ACCESS_DENIED.\r |
407 | \r | |
c1d93242 JY |
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 | |
c411b485 MK |
425 | IN UINT32 AuthenticationStatus,\r |
426 | IN CONST EFI_DEVICE_PATH_PROTOCOL *File OPTIONAL,\r | |
427 | IN VOID *FileBuffer,\r | |
428 | IN UINTN FileSize,\r | |
429 | IN BOOLEAN BootPolicy\r | |
c1d93242 JY |
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 | |
4b026f0d | 444 | \r |
c411b485 | 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 | |
e905fbb0 | 451 | DEBUG ((DEBUG_VERBOSE, "DxeTpm2MeasureBootHandler - Tcg2 - %r\n", Status));\r |
c1d93242 JY |
452 | return EFI_SUCCESS;\r |
453 | }\r | |
454 | \r | |
c411b485 MK |
455 | ProtocolCapability.Size = (UINT8)sizeof (ProtocolCapability);\r |
456 | Status = Tcg2Protocol->GetCapability (\r | |
457 | Tcg2Protocol,\r | |
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 | |
e905fbb0 | 464 | DEBUG ((DEBUG_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 | |
b3548d32 | 472 | \r |
c1d93242 JY |
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 | |
c411b485 | 478 | Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);\r |
1abfa4ce | 479 | if (!EFI_ERROR (Status) && !mTcg2MeasureGptTableFlag) {\r |
c1d93242 | 480 | //\r |
fc70522f | 481 | // Find the gpt partition on the given devicepath\r |
c1d93242 JY |
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 | |
c411b485 MK |
489 | if ((DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH) &&\r |
490 | (DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP))\r | |
491 | {\r | |
c1d93242 JY |
492 | //\r |
493 | // Check whether it is a gpt partition or not\r | |
b3548d32 | 494 | //\r |
c411b485 MK |
495 | if ((((HARDDRIVE_DEVICE_PATH *)DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER) &&\r |
496 | (((HARDDRIVE_DEVICE_PATH *)DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID))\r | |
497 | {\r | |
c1d93242 JY |
498 | //\r |
499 | // Change the partition device path to its parent device path (disk) and get the handle.\r | |
500 | //\r | |
501 | DevicePathNode->Type = END_DEVICE_PATH_TYPE;\r | |
502 | DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r | |
503 | DevicePathNode = OrigDevicePathNode;\r | |
c411b485 MK |
504 | Status = gBS->LocateDevicePath (\r |
505 | &gEfiDiskIoProtocolGuid,\r | |
506 | &DevicePathNode,\r | |
507 | &Handle\r | |
508 | );\r | |
c1d93242 JY |
509 | if (!EFI_ERROR (Status)) {\r |
510 | //\r | |
511 | // Measure GPT disk.\r | |
512 | //\r | |
1abfa4ce | 513 | Status = Tcg2MeasureGptTable (Tcg2Protocol, Handle);\r |
e905fbb0 | 514 | DEBUG ((DEBUG_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasureGptTable - %r\n", Status));\r |
c1d93242 JY |
515 | if (!EFI_ERROR (Status)) {\r |
516 | //\r | |
517 | // GPT disk check done.\r | |
518 | //\r | |
1abfa4ce | 519 | mTcg2MeasureGptTableFlag = TRUE;\r |
c1d93242 JY |
520 | }\r |
521 | }\r | |
c411b485 | 522 | \r |
c1d93242 JY |
523 | FreePool (OrigDevicePathNode);\r |
524 | OrigDevicePathNode = DuplicateDevicePath (File);\r | |
525 | ASSERT (OrigDevicePathNode != NULL);\r | |
526 | break;\r | |
527 | }\r | |
528 | }\r | |
c411b485 MK |
529 | \r |
530 | DevicePathNode = NextDevicePathNode (DevicePathNode);\r | |
c1d93242 JY |
531 | }\r |
532 | }\r | |
b3548d32 | 533 | \r |
c1d93242 JY |
534 | //\r |
535 | // 2. Measure PE image.\r | |
536 | //\r | |
537 | ApplicationRequired = FALSE;\r | |
538 | \r | |
539 | //\r | |
540 | // Check whether this device path support FVB protocol.\r | |
541 | //\r | |
542 | DevicePathNode = OrigDevicePathNode;\r | |
c411b485 | 543 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);\r |
c1d93242 JY |
544 | if (!EFI_ERROR (Status)) {\r |
545 | //\r | |
546 | // Don't check FV image, and directly return EFI_SUCCESS.\r | |
547 | // It can be extended to the specific FV authentication according to the different requirement.\r | |
548 | //\r | |
549 | if (IsDevicePathEnd (DevicePathNode)) {\r | |
550 | return EFI_SUCCESS;\r | |
551 | }\r | |
c411b485 | 552 | \r |
c1d93242 JY |
553 | //\r |
554 | // The PE image from unmeasured Firmware volume need be measured\r | |
d6b926e7 | 555 | // The PE image from measured Firmware volume will be measured according to policy below.\r |
c1d93242 JY |
556 | // If it is driver, do not measure\r |
557 | // If it is application, still measure.\r | |
558 | //\r | |
559 | ApplicationRequired = TRUE;\r | |
560 | \r | |
c411b485 | 561 | if ((mTcg2CacheMeasuredHandle != Handle) && (mTcg2MeasuredHobData != NULL)) {\r |
c1d93242 JY |
562 | //\r |
563 | // Search for Root FV of this PE image\r | |
564 | //\r | |
565 | TempHandle = Handle;\r | |
566 | do {\r | |
c411b485 | 567 | Status = gBS->HandleProtocol (\r |
b3548d32 | 568 | TempHandle,\r |
c1d93242 | 569 | &gEfiFirmwareVolumeBlockProtocolGuid,\r |
c411b485 | 570 | (VOID **)&FvbProtocol\r |
c1d93242 JY |
571 | );\r |
572 | TempHandle = FvbProtocol->ParentHandle;\r | |
c411b485 | 573 | } while (!EFI_ERROR (Status) && FvbProtocol->ParentHandle != NULL);\r |
c1d93242 JY |
574 | \r |
575 | //\r | |
576 | // Search in measured FV Hob\r | |
577 | //\r | |
c411b485 MK |
578 | Status = FvbProtocol->GetPhysicalAddress (FvbProtocol, &FvAddress);\r |
579 | if (EFI_ERROR (Status)) {\r | |
c1d93242 JY |
580 | return Status;\r |
581 | }\r | |
582 | \r | |
583 | ApplicationRequired = FALSE;\r | |
584 | \r | |
1abfa4ce | 585 | for (Index = 0; Index < mTcg2MeasuredHobData->Num; Index++) {\r |
c411b485 | 586 | if (mTcg2MeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r |
c1d93242 JY |
587 | //\r |
588 | // Cache measured FV for next measurement\r | |
589 | //\r | |
1abfa4ce | 590 | mTcg2CacheMeasuredHandle = Handle;\r |
c411b485 | 591 | ApplicationRequired = TRUE;\r |
c1d93242 JY |
592 | break;\r |
593 | }\r | |
594 | }\r | |
595 | }\r | |
596 | }\r | |
597 | \r | |
598 | //\r | |
599 | // File is not found.\r | |
600 | //\r | |
601 | if (FileBuffer == NULL) {\r | |
602 | Status = EFI_SECURITY_VIOLATION;\r | |
603 | goto Finish;\r | |
604 | }\r | |
605 | \r | |
1abfa4ce JY |
606 | mTcg2ImageSize = FileSize;\r |
607 | mTcg2FileBuffer = FileBuffer;\r | |
c1d93242 JY |
608 | \r |
609 | //\r | |
610 | // Measure PE Image\r | |
611 | //\r | |
612 | DevicePathNode = OrigDevicePathNode;\r | |
613 | ZeroMem (&ImageContext, sizeof (ImageContext));\r | |
c411b485 MK |
614 | ImageContext.Handle = (VOID *)FileBuffer;\r |
615 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)DxeTpm2MeasureBootLibImageRead;\r | |
c1d93242 JY |
616 | \r |
617 | //\r | |
618 | // Get information about the image being loaded\r | |
619 | //\r | |
620 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r | |
621 | if (EFI_ERROR (Status)) {\r | |
1755932f GJ |
622 | //\r |
623 | // Check for invalid parameters.\r | |
624 | //\r | |
625 | if (File == NULL) {\r | |
626 | Status = EFI_ACCESS_DENIED;\r | |
627 | }\r | |
628 | \r | |
c1d93242 JY |
629 | //\r |
630 | // The information can't be got from the invalid PeImage\r | |
631 | //\r | |
632 | goto Finish;\r | |
633 | }\r | |
b3548d32 | 634 | \r |
c1d93242 JY |
635 | //\r |
636 | // Measure only application if Application flag is set\r | |
637 | // Measure drivers and applications if Application flag is not set\r | |
638 | //\r | |
b3548d32 | 639 | if ((!ApplicationRequired) ||\r |
c411b485 MK |
640 | (ApplicationRequired && (ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)))\r |
641 | {\r | |
c1d93242 JY |
642 | //\r |
643 | // Print the image path to be measured.\r | |
b3548d32 | 644 | //\r |
c1d93242 | 645 | DEBUG_CODE_BEGIN ();\r |
c411b485 MK |
646 | CHAR16 *ToText;\r |
647 | ToText = ConvertDevicePathToText (\r | |
648 | DevicePathNode,\r | |
649 | FALSE,\r | |
650 | TRUE\r | |
651 | );\r | |
652 | if (ToText != NULL) {\r | |
653 | DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r | |
654 | FreePool (ToText);\r | |
655 | }\r | |
656 | \r | |
c1d93242 JY |
657 | DEBUG_CODE_END ();\r |
658 | \r | |
659 | //\r | |
660 | // Measure PE image into TPM log.\r | |
661 | //\r | |
1abfa4ce JY |
662 | Status = Tcg2MeasurePeImage (\r |
663 | Tcg2Protocol,\r | |
c411b485 | 664 | (EFI_PHYSICAL_ADDRESS)(UINTN)FileBuffer,\r |
b3548d32 | 665 | FileSize,\r |
c411b485 | 666 | (UINTN)ImageContext.ImageAddress,\r |
b3548d32 | 667 | ImageContext.ImageType,\r |
c1d93242 JY |
668 | DevicePathNode\r |
669 | );\r | |
e905fbb0 | 670 | DEBUG ((DEBUG_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasurePeImage - %r\n", Status));\r |
c1d93242 JY |
671 | }\r |
672 | \r | |
673 | //\r | |
674 | // Done, free the allocated resource.\r | |
675 | //\r | |
676 | Finish:\r | |
677 | if (OrigDevicePathNode != NULL) {\r | |
678 | FreePool (OrigDevicePathNode);\r | |
679 | }\r | |
680 | \r | |
e905fbb0 | 681 | DEBUG ((DEBUG_INFO, "DxeTpm2MeasureBootHandler - %r\n", Status));\r |
c1d93242 JY |
682 | \r |
683 | return Status;\r | |
684 | }\r | |
685 | \r | |
686 | /**\r | |
687 | Register the security handler to provide TPM measure boot service.\r | |
688 | \r | |
689 | @param ImageHandle ImageHandle of the loaded driver.\r | |
690 | @param SystemTable Pointer to the EFI System Table.\r | |
691 | \r | |
692 | @retval EFI_SUCCESS Register successfully.\r | |
693 | @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r | |
694 | **/\r | |
695 | EFI_STATUS\r | |
696 | EFIAPI\r | |
697 | DxeTpm2MeasureBootLibConstructor (\r | |
698 | IN EFI_HANDLE ImageHandle,\r | |
699 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
700 | )\r | |
701 | {\r | |
702 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
703 | \r | |
704 | GuidHob = NULL;\r | |
705 | \r | |
706 | GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);\r | |
707 | \r | |
708 | if (GuidHob != NULL) {\r | |
1abfa4ce | 709 | mTcg2MeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r |
c1d93242 JY |
710 | }\r |
711 | \r | |
712 | return RegisterSecurity2Handler (\r | |
c411b485 MK |
713 | DxeTpm2MeasureBootHandler,\r |
714 | EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r | |
715 | );\r | |
c1d93242 | 716 | }\r |