From nobody Sun Apr 28 14:39:39 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+62903+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+62903+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1595288325; cv=none; d=zohomail.com; s=zohoarc; b=NUZBkceod0v9fd7uQwRQU7PU63gbcV0wrhXMqeZrX20cvi/BM/Faem6FVoTbMd2GKiiwsnpTTowwgzXp2OI5GdOMuk+8qemUOiXplV+N+QJ7FsXtY7pAzFTsjr4tuFTg/Lomf0SF3uSdYsibRdq7xEbWo+rUAmAnBsXN30fe13o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595288325; h=Content-Transfer-Encoding:Cc:Date:From:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=5OPGUOZ2gnPmXaEks6n25AIdZfNSmAbGr8KSGInbflQ=; b=AsSLdfvPmZ1PxuZXN0PRllIViD8sASmV3PzzBMGVDOsPyfFfiA56YPCENPJTNqfo2xQyQtAqrXdtsoh7RzJ8+5OvO02R59ocg1UtuT0Fg5KPN41TQo+28gRdSJWHy7l4ufJnwyzrK4MPmxOO04Z8OyhjBFnjZWZBrVqRlpRHWAk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+62903+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 159528832577721.217752064030833; Mon, 20 Jul 2020 16:38:45 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id bh79YY1788612x3dzKaFNBFg; Mon, 20 Jul 2020 16:38:45 -0700 X-Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web10.7100.1595284118736963106 for ; Mon, 20 Jul 2020 15:28:39 -0700 IronPort-SDR: Qj0LTp8v4UovKsWJuDbtWML3BJ8BNYYxkxVbzl8/QwAeKcJbwtFrtRfxgdw9vDcIXj8/FU8Nt7 VXQAGEa4z4MQ== X-IronPort-AV: E=McAfee;i="6000,8403,9688"; a="150005441" X-IronPort-AV: E=Sophos;i="5.75,375,1589266800"; d="scan'208";a="150005441" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2020 15:28:37 -0700 IronPort-SDR: Iji+F16qwSDxCRPPLdldkX3rSIHtscqh16c/nv3a75XTeGoJSeRkCV79d550Tu4ioMPhk/31Ol BiFnrHF3fO3w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,375,1589266800"; d="scan'208";a="392164753" X-Received: from fm73lab177-1.amr.corp.intel.com ([10.80.209.189]) by fmsmga001.fm.intel.com with ESMTP; 20 Jul 2020 15:28:36 -0700 From: "Rodrigo Gonzalez del Cueto" To: devel@edk2.groups.io Cc: Rodrigo Gonzalez del Cueto , Jiewen Yao , Jian J Wang , Qi Zhang Subject: [edk2-devel] [PATCH] SecurityPkg: Debug code to audit BIOS TPM extend operations. Date: Mon, 20 Jul 2020 15:28:32 -0700 Message-Id: MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,rodrigo.gonzalez.del.cueto@intel.com X-Gm-Message-State: XNeNWrujSkWtffCxb0iW56uOx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1595288325; bh=EmDKvYheFX50IuGlSxzAV16Tm9MLESig7NgdaKtSreY=; h=Cc:Date:From:Reply-To:Subject:To; b=IIsKDKqQDWeW28obgDcbGaF17XGD7mx8PQtBl7Ke9rmay6wVfLM5HH2zm1BWal/U/P9 5wXZA+V6IV10tYzSFsiZWZuuIBD8xukVECemKsirtkFcsUEYIbmELOzBRHuZJnXUjqxm7 TICo9y4l+r7VzWvyTOkuFLljPDUIAe7+R8c= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2858 Add debug functionality to examine TPM extend operations performed by BIOS and inspect the PCR 00 value prior to any BIOS measurements. Replaced usage of EFI_D_* for DEBUG_* definitions in debug messages. Cc: Jiewen Yao Cc: Jian J Wang Cc: Qi Zhang Signed-off-by: Rodrigo Gonzalez del Cueto --- SecurityPkg/Include/Library/Tpm2CommandLib.h | 25 +- .../Library/Tpm2CommandLib/Tpm2Integrity.c | 468 ++++++++++++------ SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c | 32 +- 3 files changed, 364 insertions(+), 161 deletions(-) diff --git a/SecurityPkg/Include/Library/Tpm2CommandLib.h b/SecurityPkg/Inc= lude/Library/Tpm2CommandLib.h index ce381e786b..bfa5bd82f4 100644 --- a/SecurityPkg/Include/Library/Tpm2CommandLib.h +++ b/SecurityPkg/Include/Library/Tpm2CommandLib.h @@ -505,7 +505,7 @@ EFIAPI Tpm2PcrEvent ( IN TPMI_DH_PCR PcrHandle, IN TPM2B_EVENT *EventData, - OUT TPML_DIGEST_VALUES *Digests + OUT TPML_DIGEST_VALUES *Digests ); =20 /** @@ -523,9 +523,26 @@ EFI_STATUS EFIAPI Tpm2PcrRead ( IN TPML_PCR_SELECTION *PcrSelectionIn, - OUT UINT32 *PcrUpdateCounter, - OUT TPML_PCR_SELECTION *PcrSelectionOut, - OUT TPML_DIGEST *PcrValues + OUT UINT32 *PcrUpdateCounter, + OUT TPML_PCR_SELECTION *PcrSelectionOut, + OUT TPML_DIGEST *PcrValues + ); + +/** + This function will query the TPM to determine which hashing algorithms = and + get the digests of all active and supported PCR banks of a specific PCR= register. + + @param[in] PcrHandle The index of the PCR register to be read. + @param[out] HashList List of digests from PCR register being re= ad. + + @retval EFI_SUCCESS The Pcr was read successfully. + @retval EFI_DEVICE_ERROR The command was unsuccessful. +**/ +EFI_STATUS +EFIAPI +Tpm2ActivePcrRegisterRead ( + IN TPMI_DH_PCR PcrHandle, + OUT TPML_DIGEST *HashList ); =20 /** diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Integrity.c b/SecurityP= kg/Library/Tpm2CommandLib/Tpm2Integrity.c index ddb15178fb..229fc44139 100644 --- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Integrity.c +++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Integrity.c @@ -76,6 +76,297 @@ typedef struct { =20 #pragma pack() =20 +/** + This command returns the values of all PCR specified in pcrSelect. + + @param[in] PcrSelectionIn The selection of PCR to read. + @param[out] PcrUpdateCounter The current value of the PCR update count= er. + @param[out] PcrSelectionOut The PCR in the returned list. + @param[out] PcrValues The contents of the PCR indicated in pcrS= elect. + + @retval EFI_SUCCESS Operation completed successfully. + @retval EFI_DEVICE_ERROR The command was unsuccessful. +**/ +EFI_STATUS +EFIAPI +Tpm2PcrRead ( + IN TPML_PCR_SELECTION *PcrSelectionIn, + OUT UINT32 *PcrUpdateCounter, + OUT TPML_PCR_SELECTION *PcrSelectionOut, + OUT TPML_DIGEST *PcrValues + ) +{ + EFI_STATUS Status; + TPM2_PCR_READ_COMMAND SendBuffer; + TPM2_PCR_READ_RESPONSE RecvBuffer; + UINT32 SendBufferSize; + UINT32 RecvBufferSize; + UINTN Index; + TPML_DIGEST *PcrValuesOut; + TPM2B_DIGEST *Digests; + + // + // Construct command + // + SendBuffer.Header.tag =3D SwapBytes16(TPM_ST_NO_SESSIONS); + SendBuffer.Header.commandCode =3D SwapBytes32(TPM_CC_PCR_Read); + + SendBuffer.PcrSelectionIn.count =3D SwapBytes32(PcrSelectionIn->count); + for (Index =3D 0; Index < PcrSelectionIn->count; Index++) { + SendBuffer.PcrSelectionIn.pcrSelections[Index].hash =3D SwapBytes16(Pc= rSelectionIn->pcrSelections[Index].hash); + SendBuffer.PcrSelectionIn.pcrSelections[Index].sizeofSelect =3D PcrSel= ectionIn->pcrSelections[Index].sizeofSelect; + CopyMem (&SendBuffer.PcrSelectionIn.pcrSelections[Index].pcrSelect, &P= crSelectionIn->pcrSelections[Index].pcrSelect, SendBuffer.PcrSelectionIn.pc= rSelections[Index].sizeofSelect); + } + + SendBufferSize =3D sizeof(SendBuffer.Header) + sizeof(SendBuffer.PcrSele= ctionIn.count) + sizeof(SendBuffer.PcrSelectionIn.pcrSelections[0]) * PcrSe= lectionIn->count; + SendBuffer.Header.paramSize =3D SwapBytes32 (SendBufferSize); + + // + // send Tpm command + // + RecvBufferSize =3D sizeof (RecvBuffer); + Status =3D Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &Rec= vBufferSize, (UINT8 *)&RecvBuffer); + if (EFI_ERROR (Status)) { + return Status; + } + + if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); + return EFI_DEVICE_ERROR; + } + if (SwapBytes32(RecvBuffer.Header.responseCode) !=3D TPM_RC_SUCCESS) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - responseCode - %x\n", SwapBytes32(= RecvBuffer.Header.responseCode))); + return EFI_NOT_FOUND; + } + + // + // Return the response + // + + // + // PcrUpdateCounter + // + if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter)) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); + return EFI_DEVICE_ERROR; + } + *PcrUpdateCounter =3D SwapBytes32(RecvBuffer.PcrUpdateCounter); + + // + // PcrSelectionOut + // + if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count)) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); + return EFI_DEVICE_ERROR; + } + PcrSelectionOut->count =3D SwapBytes32(RecvBuffer.PcrSelectionOut.count); + if (PcrSelectionOut->count > HASH_COUNT) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrSelectionOut->count error %x\n"= , PcrSelectionOut->count)); + return EFI_DEVICE_ERROR; + } + + if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBu= ffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); + return EFI_DEVICE_ERROR; + } + for (Index =3D 0; Index < PcrSelectionOut->count; Index++) { + PcrSelectionOut->pcrSelections[Index].hash =3D SwapBytes16(RecvBuffer.= PcrSelectionOut.pcrSelections[Index].hash); + PcrSelectionOut->pcrSelections[Index].sizeofSelect =3D RecvBuffer.PcrS= electionOut.pcrSelections[Index].sizeofSelect; + if (PcrSelectionOut->pcrSelections[Index].sizeofSelect > PCR_SELECT_MA= X) { + return EFI_DEVICE_ERROR; + } + CopyMem (&PcrSelectionOut->pcrSelections[Index].pcrSelect, &RecvBuffer= .PcrSelectionOut.pcrSelections[Index].pcrSelect, PcrSelectionOut->pcrSelect= ions[Index].sizeofSelect); + } + + // + // PcrValues + // + PcrValuesOut =3D (TPML_DIGEST *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RES= PONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.Pcr= SelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) *= PcrSelectionOut->count); + PcrValues->count =3D SwapBytes32(PcrValuesOut->count); + // + // The number of digests in list is not greater than 8 per TPML_DIGEST d= efinition + // + if (PcrValues->count > 8) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrValues->count error %x\n", PcrV= alues->count)); + return EFI_DEVICE_ERROR; + } + Digests =3D PcrValuesOut->digests; + for (Index =3D 0; Index < PcrValues->count; Index++) { + PcrValues->digests[Index].size =3D SwapBytes16(Digests->size); + if (PcrValues->digests[Index].size > sizeof(TPMU_HA)) { + DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - Digest.size error %x\n", PcrValu= es->digests[Index].size)); + return EFI_DEVICE_ERROR; + } + CopyMem (&PcrValues->digests[Index].buffer, &Digests->buffer, PcrValue= s->digests[Index].size); + Digests =3D (TPM2B_DIGEST *)((UINT8 *)Digests + sizeof(Digests->size) = + PcrValues->digests[Index].size); + } + + return EFI_SUCCESS; +} + +/** + This function will query the TPM to determine which hashing algorithms = and + get the digests of all active and supported PCR banks of a specific PCR= register. + + @param[in] PcrHandle The index of the PCR register to be read. + @param[out] HashList List of digests from PCR register being re= ad. + + @retval EFI_SUCCESS The Pcr was read successfully. + @retval EFI_DEVICE_ERROR The command was unsuccessful. +**/ +EFI_STATUS +EFIAPI +Tpm2ActivePcrRegisterRead ( + IN TPMI_DH_PCR PcrHandle, + OUT TPML_DIGEST *HashList +) +{ + EFI_STATUS Status; + TPML_PCR_SELECTION Pcrs; + TPML_PCR_SELECTION PcrSelectionIn; + TPML_PCR_SELECTION PcrSelectionOut; + TPML_DIGEST PcrValues; + UINT32 PcrUpdateCounter; + UINT32 PcrIndex; + UINT32 TpmHashAlgorithmBitmap; + TPMI_ALG_HASH CurrentPcrBankHash; + UINT32 ActivePcrBanks; + UINT32 TcgRegistryHashAlg; + UINT32 Index; + UINT32 Index2; + + PcrIndex =3D (UINT8)PcrHandle; + + if ((PcrIndex < 0) || + (PcrIndex >=3D IMPLEMENTATION_PCR)) { + return EFI_INVALID_PARAMETER; + } + + ZeroMem (&PcrSelectionIn, sizeof (PcrSelectionIn)); + ZeroMem (&PcrUpdateCounter, sizeof (UINT32)); + ZeroMem (&PcrSelectionOut, sizeof (PcrSelectionOut)); + ZeroMem (&PcrValues, sizeof (PcrValues)); + ZeroMem (&Pcrs, sizeof (TPML_PCR_SELECTION)); + + DEBUG ((DEBUG_INFO, "ReadPcr - %02d\n", PcrIndex)); + + // + // Read TPM capabilities + // + Status =3D Tpm2GetCapabilityPcrs (&Pcrs); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "ReadPcr: Unable to read TPM capabilities\n")); + return EFI_DEVICE_ERROR; + } + + // + // Get Active Pcrs + // + Status =3D Tpm2GetCapabilitySupportedAndActivePcrs ( + &TpmHashAlgorithmBitmap, + &ActivePcrBanks + ); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "ReadPcr: Unable to read TPM capabilities and act= ive PCRs\n")); + return EFI_DEVICE_ERROR; + } + + // + // Select from Active PCRs + // + for (Index =3D 0; Index < Pcrs.count; Index++) { + CurrentPcrBankHash =3D Pcrs.pcrSelections[Index].hash; + + switch (CurrentPcrBankHash) { + case TPM_ALG_SHA1: + DEBUG ((DEBUG_VERBOSE, "HASH_ALG_SHA1 Present\n")); + TcgRegistryHashAlg =3D HASH_ALG_SHA1; + break; + case TPM_ALG_SHA256: + DEBUG ((DEBUG_VERBOSE, "HASH_ALG_SHA256 Present\n")); + TcgRegistryHashAlg =3D HASH_ALG_SHA256; + break; + case TPM_ALG_SHA384: + DEBUG ((DEBUG_VERBOSE, "HASH_ALG_SHA384 Present\n")); + TcgRegistryHashAlg =3D HASH_ALG_SHA384; + break; + case TPM_ALG_SHA512: + DEBUG ((DEBUG_VERBOSE, "HASH_ALG_SHA512 Present\n")); + TcgRegistryHashAlg =3D HASH_ALG_SHA512; + break; + case TPM_ALG_SM3_256: + DEBUG ((DEBUG_VERBOSE, "HASH_ALG_SM3 Present\n")); + TcgRegistryHashAlg =3D HASH_ALG_SM3_256; + break; + default: + // + // Unsupported algorithm + // + DEBUG ((DEBUG_VERBOSE, "Unknown algorithm present\n")); + TcgRegistryHashAlg =3D 0; + break; + } + // + // Skip unsupported and inactive PCR banks + // + if ((TcgRegistryHashAlg & ActivePcrBanks) =3D=3D 0) { + DEBUG ((DEBUG_VERBOSE, "Skipping unsupported or inactive bank: 0x%04= x\n", CurrentPcrBankHash)); + continue; + } + + // + // Select PCR from current active bank + // + PcrSelectionIn.pcrSelections[PcrSelectionIn.count].hash =3D Pcrs.pcrSe= lections[Index].hash; + PcrSelectionIn.pcrSelections[PcrSelectionIn.count].sizeofSelect =3D PC= R_SELECT_MAX; + PcrSelectionIn.pcrSelections[PcrSelectionIn.count].pcrSelect[0] =3D (P= crIndex < 8) ? 1 << PcrIndex : 0; + PcrSelectionIn.pcrSelections[PcrSelectionIn.count].pcrSelect[1] =3D (P= crIndex > 7) && (PcrIndex < 16) ? 1 << (PcrIndex - 8) : 0; + PcrSelectionIn.pcrSelections[PcrSelectionIn.count].pcrSelect[2] =3D (P= crIndex > 15) ? 1 << (PcrIndex - 16) : 0; + PcrSelectionIn.count++; + } + + // + // Read PCRs + // + Status =3D Tpm2PcrRead ( + &PcrSelectionIn, + &PcrUpdateCounter, + &PcrSelectionOut, + &PcrValues + ); + + if (EFI_ERROR (Status)) { + DEBUG((DEBUG_ERROR, "Tpm2PcrRead failed Status =3D %r \n", Status)); + return EFI_DEVICE_ERROR; + } + + for (Index =3D 0; Index < PcrValues.count; Index++) { + DEBUG (( + DEBUG_INFO, + "ReadPcr - HashAlg =3D 0x%04x, Pcr[%02d], digest =3D ", + PcrSelectionOut.pcrSelections[Index].hash, + PcrIndex + )); + + for(Index2 =3D 0; Index2 < PcrValues.digests[Index].size; Index2++) { + DEBUG ((DEBUG_INFO, "%02x ", PcrValues.digests[Index].buffer[Index2]= )); + } + DEBUG ((DEBUG_INFO, "\n")); + } + + if (HashList !=3D NULL) { + CopyMem ( + HashList, + &PcrValues, + sizeof (TPML_DIGEST) + ); + } + + return EFI_SUCCESS; +} + /** This command is used to cause an update to the indicated PCR. The digests parameter contains one or more tagged digest value identifie= d by an algorithm ID. @@ -130,14 +421,26 @@ Tpm2PcrExtend ( Buffer +=3D sizeof(UINT16); DigestSize =3D GetHashSizeFromAlgo (Digests->digests[Index].hashAlg); if (DigestSize =3D=3D 0) { - DEBUG ((EFI_D_ERROR, "Unknown hash algorithm %d\r\n", Digests->diges= ts[Index].hashAlg)); + DEBUG ((DEBUG_ERROR, "Unknown hash algorithm %d\r\n", Digests->diges= ts[Index].hashAlg)); return EFI_DEVICE_ERROR; } + CopyMem( Buffer, &Digests->digests[Index].digest, DigestSize ); + + DEBUG_CODE_BEGIN (); + UINTN Index2; + DEBUG ((DEBUG_INFO, "Tpm2PcrExtend - Hash =3D 0x%04x, Pcr[%02d], diges= t =3D ", Digests->digests[Index].hashAlg, (UINT8) PcrHandle)); + + for (Index2 =3D 0; Index2 < DigestSize; Index2++) { + DEBUG ((DEBUG_INFO, "%02x ", Buffer[Index2])); + } + DEBUG ((DEBUG_INFO, "\n")); + DEBUG_CODE_END (); + Buffer +=3D DigestSize; } =20 @@ -151,7 +454,7 @@ Tpm2PcrExtend ( } =20 if (ResultBufSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Failed ExecuteCommand: Buffer Too= Small\r\n")); + DEBUG ((DEBUG_ERROR, "Tpm2PcrExtend: Failed ExecuteCommand: Buffer Too= Small\r\n")); return EFI_BUFFER_TOO_SMALL; } =20 @@ -160,7 +463,7 @@ Tpm2PcrExtend ( // RespSize =3D SwapBytes32(Res.Header.paramSize); if (RespSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Response size too large! %d\r\n",= RespSize)); + DEBUG ((DEBUG_ERROR, "Tpm2PcrExtend: Response size too large! %d\r\n",= RespSize)); return EFI_BUFFER_TOO_SMALL; } =20 @@ -168,10 +471,15 @@ Tpm2PcrExtend ( // Fail if command failed // if (SwapBytes32(Res.Header.responseCode) !=3D TPM_RC_SUCCESS) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrExtend: Response Code error! 0x%08x\r\n",= SwapBytes32(Res.Header.responseCode))); + DEBUG ((DEBUG_ERROR, "Tpm2PcrExtend: Response Code error! 0x%08x\r\n",= SwapBytes32(Res.Header.responseCode))); return EFI_DEVICE_ERROR; } =20 + DEBUG_CODE_BEGIN (); + DEBUG ((DEBUG_INFO, "Tpm2PcrExtend: PCR read after extend...\n")); + Tpm2ActivePcrRegisterRead (PcrHandle, NULL); + DEBUG_CODE_END (); + // // Unmarshal the response // @@ -246,7 +554,7 @@ Tpm2PcrEvent ( } =20 if (ResultBufSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrEvent: Failed ExecuteCommand: Buffer Too = Small\r\n")); + DEBUG ((DEBUG_ERROR, "Tpm2PcrEvent: Failed ExecuteCommand: Buffer Too = Small\r\n")); return EFI_BUFFER_TOO_SMALL; } =20 @@ -255,7 +563,7 @@ Tpm2PcrEvent ( // RespSize =3D SwapBytes32(Res.Header.paramSize); if (RespSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrEvent: Response size too large! %d\r\n", = RespSize)); + DEBUG ((DEBUG_ERROR, "Tpm2PcrEvent: Response size too large! %d\r\n", = RespSize)); return EFI_BUFFER_TOO_SMALL; } =20 @@ -263,7 +571,7 @@ Tpm2PcrEvent ( // Fail if command failed // if (SwapBytes32(Res.Header.responseCode) !=3D TPM_RC_SUCCESS) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrEvent: Response Code error! 0x%08x\r\n", = SwapBytes32(Res.Header.responseCode))); + DEBUG ((DEBUG_ERROR, "Tpm2PcrEvent: Response Code error! 0x%08x\r\n", = SwapBytes32(Res.Header.responseCode))); return EFI_DEVICE_ERROR; } =20 @@ -284,7 +592,7 @@ Tpm2PcrEvent ( Buffer +=3D sizeof(UINT16); DigestSize =3D GetHashSizeFromAlgo (Digests->digests[Index].hashAlg); if (DigestSize =3D=3D 0) { - DEBUG ((EFI_D_ERROR, "Unknown hash algorithm %d\r\n", Digests->diges= ts[Index].hashAlg)); + DEBUG ((DEBUG_ERROR, "Unknown hash algorithm %d\r\n", Digests->diges= ts[Index].hashAlg)); return EFI_DEVICE_ERROR; } CopyMem( @@ -298,134 +606,6 @@ Tpm2PcrEvent ( return EFI_SUCCESS; } =20 -/** - This command returns the values of all PCR specified in pcrSelect. - - @param[in] PcrSelectionIn The selection of PCR to read. - @param[out] PcrUpdateCounter The current value of the PCR update count= er. - @param[out] PcrSelectionOut The PCR in the returned list. - @param[out] PcrValues The contents of the PCR indicated in pcrS= elect. - - @retval EFI_SUCCESS Operation completed successfully. - @retval EFI_DEVICE_ERROR The command was unsuccessful. -**/ -EFI_STATUS -EFIAPI -Tpm2PcrRead ( - IN TPML_PCR_SELECTION *PcrSelectionIn, - OUT UINT32 *PcrUpdateCounter, - OUT TPML_PCR_SELECTION *PcrSelectionOut, - OUT TPML_DIGEST *PcrValues - ) -{ - EFI_STATUS Status; - TPM2_PCR_READ_COMMAND SendBuffer; - TPM2_PCR_READ_RESPONSE RecvBuffer; - UINT32 SendBufferSize; - UINT32 RecvBufferSize; - UINTN Index; - TPML_DIGEST *PcrValuesOut; - TPM2B_DIGEST *Digests; - - // - // Construct command - // - SendBuffer.Header.tag =3D SwapBytes16(TPM_ST_NO_SESSIONS); - SendBuffer.Header.commandCode =3D SwapBytes32(TPM_CC_PCR_Read); - - SendBuffer.PcrSelectionIn.count =3D SwapBytes32(PcrSelectionIn->count); - for (Index =3D 0; Index < PcrSelectionIn->count; Index++) { - SendBuffer.PcrSelectionIn.pcrSelections[Index].hash =3D SwapBytes16(Pc= rSelectionIn->pcrSelections[Index].hash); - SendBuffer.PcrSelectionIn.pcrSelections[Index].sizeofSelect =3D PcrSel= ectionIn->pcrSelections[Index].sizeofSelect; - CopyMem (&SendBuffer.PcrSelectionIn.pcrSelections[Index].pcrSelect, &P= crSelectionIn->pcrSelections[Index].pcrSelect, SendBuffer.PcrSelectionIn.pc= rSelections[Index].sizeofSelect); - } - - SendBufferSize =3D sizeof(SendBuffer.Header) + sizeof(SendBuffer.PcrSele= ctionIn.count) + sizeof(SendBuffer.PcrSelectionIn.pcrSelections[0]) * PcrSe= lectionIn->count; - SendBuffer.Header.paramSize =3D SwapBytes32 (SendBufferSize); - - // - // send Tpm command - // - RecvBufferSize =3D sizeof (RecvBuffer); - Status =3D Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &Rec= vBufferSize, (UINT8 *)&RecvBuffer); - if (EFI_ERROR (Status)) { - return Status; - } - - if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); - return EFI_DEVICE_ERROR; - } - if (SwapBytes32(RecvBuffer.Header.responseCode) !=3D TPM_RC_SUCCESS) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - responseCode - %x\n", SwapBytes32(= RecvBuffer.Header.responseCode))); - return EFI_NOT_FOUND; - } - - // - // Return the response - // - - // - // PcrUpdateCounter - // - if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); - return EFI_DEVICE_ERROR; - } - *PcrUpdateCounter =3D SwapBytes32(RecvBuffer.PcrUpdateCounter); - - // - // PcrSelectionOut - // - if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); - return EFI_DEVICE_ERROR; - } - PcrSelectionOut->count =3D SwapBytes32(RecvBuffer.PcrSelectionOut.count); - if (PcrSelectionOut->count > HASH_COUNT) { - DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrSelectionOut->count error %x\n"= , PcrSelectionOut->count)); - return EFI_DEVICE_ERROR; - } - - if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.P= crUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBu= ffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", Recv= BufferSize)); - return EFI_DEVICE_ERROR; - } - for (Index =3D 0; Index < PcrSelectionOut->count; Index++) { - PcrSelectionOut->pcrSelections[Index].hash =3D SwapBytes16(RecvBuffer.= PcrSelectionOut.pcrSelections[Index].hash); - PcrSelectionOut->pcrSelections[Index].sizeofSelect =3D RecvBuffer.PcrS= electionOut.pcrSelections[Index].sizeofSelect; - if (PcrSelectionOut->pcrSelections[Index].sizeofSelect > PCR_SELECT_MA= X) { - return EFI_DEVICE_ERROR; - } - CopyMem (&PcrSelectionOut->pcrSelections[Index].pcrSelect, &RecvBuffer= .PcrSelectionOut.pcrSelections[Index].pcrSelect, PcrSelectionOut->pcrSelect= ions[Index].sizeofSelect); - } - - // - // PcrValues - // - PcrValuesOut =3D (TPML_DIGEST *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RES= PONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.Pcr= SelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) *= PcrSelectionOut->count); - PcrValues->count =3D SwapBytes32(PcrValuesOut->count); - // - // The number of digests in list is not greater than 8 per TPML_DIGEST d= efinition - // - if (PcrValues->count > 8) { - DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrValues->count error %x\n", PcrV= alues->count)); - return EFI_DEVICE_ERROR; - } - Digests =3D PcrValuesOut->digests; - for (Index =3D 0; Index < PcrValues->count; Index++) { - PcrValues->digests[Index].size =3D SwapBytes16(Digests->size); - if (PcrValues->digests[Index].size > sizeof(TPMU_HA)) { - DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - Digest.size error %x\n", PcrValu= es->digests[Index].size)); - return EFI_DEVICE_ERROR; - } - CopyMem (&PcrValues->digests[Index].buffer, &Digests->buffer, PcrValue= s->digests[Index].size); - Digests =3D (TPM2B_DIGEST *)((UINT8 *)Digests + sizeof(Digests->size) = + PcrValues->digests[Index].size); - } - - return EFI_SUCCESS; -} - /** This command is used to set the desired PCR allocation of PCR and algori= thms. =20 @@ -513,7 +693,7 @@ Tpm2PcrAllocate ( } =20 if (ResultBufSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrAllocate: Failed ExecuteCommand: Buffer T= oo Small\r\n")); + DEBUG ((DEBUG_ERROR, "Tpm2PcrAllocate: Failed ExecuteCommand: Buffer T= oo Small\r\n")); Status =3D EFI_BUFFER_TOO_SMALL; goto Done; } @@ -523,7 +703,7 @@ Tpm2PcrAllocate ( // RespSize =3D SwapBytes32(Res.Header.paramSize); if (RespSize > sizeof(Res)) { - DEBUG ((EFI_D_ERROR, "Tpm2PcrAllocate: Response size too large! %d\r\n= ", RespSize)); + DEBUG ((DEBUG_ERROR, "Tpm2PcrAllocate: Response size too large! %d\r\n= ", RespSize)); Status =3D EFI_BUFFER_TOO_SMALL; goto Done; } @@ -532,7 +712,7 @@ Tpm2PcrAllocate ( // Fail if command failed // if (SwapBytes32(Res.Header.responseCode) !=3D TPM_RC_SUCCESS) { - DEBUG((EFI_D_ERROR,"Tpm2PcrAllocate: Response Code error! 0x%08x\r\n",= SwapBytes32(Res.Header.responseCode))); + DEBUG((DEBUG_ERROR,"Tpm2PcrAllocate: Response Code error! 0x%08x\r\n",= SwapBytes32(Res.Header.responseCode))); Status =3D EFI_DEVICE_ERROR; goto Done; } @@ -673,15 +853,15 @@ Tpm2PcrAllocateBanks ( &SizeNeeded, &SizeAvailable ); - DEBUG ((EFI_D_INFO, "Tpm2PcrAllocateBanks call Tpm2PcrAllocate - %r\n", = Status)); + DEBUG ((DEBUG_INFO, "Tpm2PcrAllocateBanks call Tpm2PcrAllocate - %r\n", = Status)); if (EFI_ERROR (Status)) { goto Done; } =20 - DEBUG ((EFI_D_INFO, "AllocationSuccess - %02x\n", AllocationSuccess)); - DEBUG ((EFI_D_INFO, "MaxPCR - %08x\n", MaxPCR)); - DEBUG ((EFI_D_INFO, "SizeNeeded - %08x\n", SizeNeeded)); - DEBUG ((EFI_D_INFO, "SizeAvailable - %08x\n", SizeAvailable)); + DEBUG ((DEBUG_INFO, "AllocationSuccess - %02x\n", AllocationSuccess)); + DEBUG ((DEBUG_INFO, "MaxPCR - %08x\n", MaxPCR)); + DEBUG ((DEBUG_INFO, "SizeNeeded - %08x\n", SizeNeeded)); + DEBUG ((DEBUG_INFO, "SizeAvailable - %08x\n", SizeAvailable)); =20 Done: ZeroMem(&LocalAuthSession.hmac, sizeof(LocalAuthSession.hmac)); diff --git a/SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c b/SecurityPkg/Tcg/Tcg2Pei/Tc= g2Pei.c index 19b8e4b318..678826f8a5 100644 --- a/SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c +++ b/SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c @@ -147,7 +147,6 @@ EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] =3D { } }; =20 - /** Record all measured Firmware Volume Information into a Guid Hob Guid Hob payload layout is @@ -223,7 +222,7 @@ SyncPcrAllocationsAndPcrMask ( UINT32 Tpm2PcrMask; UINT32 NewTpm2PcrMask; =20 - DEBUG ((EFI_D_ERROR, "SyncPcrAllocationsAndPcrMask!\n")); + DEBUG ((DEBUG_ERROR, "SyncPcrAllocationsAndPcrMask!\n")); =20 // // Determine the current TPM support and the Platform PCR mask. @@ -234,7 +233,7 @@ SyncPcrAllocationsAndPcrMask ( Tpm2PcrMask =3D PcdGet32 (PcdTpm2HashMask); if (Tpm2PcrMask =3D=3D 0) { // - // if PcdTPm2HashMask is zero, use ActivePcr setting + // if PcdTpm2HashMask is zero, use ActivePcr setting // PcdSet32S (PcdTpm2HashMask, TpmActivePcrBanks); Tpm2PcrMask =3D TpmActivePcrBanks; @@ -253,9 +252,9 @@ SyncPcrAllocationsAndPcrMask ( if ((TpmActivePcrBanks & Tpm2PcrMask) !=3D TpmActivePcrBanks) { NewTpmActivePcrBanks =3D TpmActivePcrBanks & Tpm2PcrMask; =20 - DEBUG ((EFI_D_INFO, "%a - Reallocating PCR banks from 0x%X to 0x%X.\n"= , __FUNCTION__, TpmActivePcrBanks, NewTpmActivePcrBanks)); + DEBUG ((DEBUG_INFO, "%a - Reallocating PCR banks from 0x%X to 0x%X.\n"= , __FUNCTION__, TpmActivePcrBanks, NewTpmActivePcrBanks)); if (NewTpmActivePcrBanks =3D=3D 0) { - DEBUG ((EFI_D_ERROR, "%a - No viable PCRs active! Please set a less = restrictive value for PcdTpm2HashMask!\n", __FUNCTION__)); + DEBUG ((DEBUG_ERROR, "%a - No viable PCRs active! Please set a less = restrictive value for PcdTpm2HashMask!\n", __FUNCTION__)); ASSERT (FALSE); } else { Status =3D Tpm2PcrAllocateBanks (NULL, (UINT32)TpmHashAlgorithmBitma= p, NewTpmActivePcrBanks); @@ -263,7 +262,7 @@ SyncPcrAllocationsAndPcrMask ( // // We can't do much here, but we hope that this doesn't happen. // - DEBUG ((EFI_D_ERROR, "%a - Failed to reallocate PCRs!\n", __FUNCTI= ON__)); + DEBUG ((DEBUG_ERROR, "%a - Failed to reallocate PCRs!\n", __FUNCTI= ON__)); ASSERT_EFI_ERROR (Status); } // @@ -280,9 +279,9 @@ SyncPcrAllocationsAndPcrMask ( if ((Tpm2PcrMask & TpmHashAlgorithmBitmap) !=3D Tpm2PcrMask) { NewTpm2PcrMask =3D Tpm2PcrMask & TpmHashAlgorithmBitmap; =20 - DEBUG ((EFI_D_INFO, "%a - Updating PcdTpm2HashMask from 0x%X to 0x%X.\= n", __FUNCTION__, Tpm2PcrMask, NewTpm2PcrMask)); + DEBUG ((DEBUG_INFO, "%a - Updating PcdTpm2HashMask from 0x%X to 0x%X.\= n", __FUNCTION__, Tpm2PcrMask, NewTpm2PcrMask)); if (NewTpm2PcrMask =3D=3D 0) { - DEBUG ((EFI_D_ERROR, "%a - No viable PCRs supported! Please set a le= ss restrictive value for PcdTpm2HashMask!\n", __FUNCTION__)); + DEBUG ((DEBUG_ERROR, "%a - No viable PCRs supported! Please set a le= ss restrictive value for PcdTpm2HashMask!\n", __FUNCTION__)); ASSERT (FALSE); } =20 @@ -321,7 +320,7 @@ LogHashEvent ( RetStatus =3D EFI_SUCCESS; for (Index =3D 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0= ]); Index++) { if ((SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) !=3D 0) { - DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].= LogFormat)); + DEBUG ((DEBUG_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].= LogFormat)); switch (mTcg2EventInfo[Index].LogFormat) { case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2: Status =3D GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &New= EventHdr->Digest); @@ -416,7 +415,7 @@ HashLogExtendEvent ( } =20 if (Status =3D=3D EFI_DEVICE_ERROR) { - DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status= )); + DEBUG ((DEBUG_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status= )); BuildGuidHob (&gTpmErrorHobGuid,0); REPORT_STATUS_CODE ( EFI_ERROR_CODE | EFI_ERROR_MINOR, @@ -925,7 +924,7 @@ PeimEntryMA ( } =20 if (GetFirstGuidHob (&gTpmErrorHobGuid) !=3D NULL) { - DEBUG ((EFI_D_ERROR, "TPM2 error!\n")); + DEBUG ((DEBUG_ERROR, "TPM2 error!\n")); return EFI_DEVICE_ERROR; } =20 @@ -989,7 +988,7 @@ PeimEntryMA ( for (PcrIndex =3D 0; PcrIndex < 8; PcrIndex++) { Status =3D MeasureSeparatorEventWithError (PcrIndex); if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "Separator Event with Error not Measured. E= rror!\n")); + DEBUG ((DEBUG_ERROR, "Separator Event with Error not Measured. E= rror!\n")); } } } @@ -1006,6 +1005,13 @@ PeimEntryMA ( } } =20 + DEBUG_CODE_BEGIN (); + // + // Peek into TPM PCR 00 before any BIOS measurement. + // + Tpm2ActivePcrRegisterRead (00, NULL); + DEBUG_CODE_END (); + // // Only install TpmInitializedPpi on success // @@ -1020,7 +1026,7 @@ PeimEntryMA ( =20 Done: if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "TPM2 error! Build Hob\n")); + DEBUG ((DEBUG_ERROR, "TPM2 error! Build Hob\n")); BuildGuidHob (&gTpmErrorHobGuid,0); REPORT_STATUS_CODE ( EFI_ERROR_CODE | EFI_ERROR_MINOR, --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#62903): https://edk2.groups.io/g/devel/message/62903 Mute This Topic: https://groups.io/mt/75694164/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-