DxeTpm2MeasureBootLib.c 24 KB

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