Skip to content

Commit 48e3529

Browse files
committed
UefiPayloadPkg,SecurityPkg: import coreboot's TPM log
Find the log using UefiPayloadPkg/CbParseLib in UefiPayloadPkg/BlSupportPei and create HOBs like those produced by TcgPei and Tcg2Pei all of which will be picked up by TcgDxe and Tcg2Dxe. TPM1 case is quite simple: - use coreboot's Spec ID Event as EDK doesn't seem to add one of its own TPM2 case is more advanced and is more complicated: - don't create a HOB for coreboot's Spec ID Event (the first entry) because TPM2 can have multiple digests and coreboot produces at most one - when importing HOBs in Tcg2Dxe add missing hashes of OneDigest kind from TXT spec (0x01 followed by 0x00 bytes) just to not come up with some custom placeholder Signed-off-by: Sergii Dmytruk <[email protected]>
1 parent ea90d54 commit 48e3529

File tree

7 files changed

+404
-46
lines changed

7 files changed

+404
-46
lines changed

SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,55 +1057,105 @@ GetDigestListBinSize (
10571057
return TotalSize;
10581058
}
10591059

1060+
STATIC VOID *
1061+
FindHashInDigestListBin (
1062+
IN VOID *DigestListBin,
1063+
TPMI_ALG_HASH HashAlg
1064+
)
1065+
{
1066+
UINTN Index;
1067+
UINT32 Count;
1068+
TPMI_ALG_HASH Alg;
1069+
1070+
Count = ReadUnaligned32 (DigestListBin);
1071+
DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
1072+
for (Index = 0; Index < Count; Index++) {
1073+
Alg = ReadUnaligned16 (DigestListBin);
1074+
DigestListBin = (UINT8 *)DigestListBin + sizeof(Alg);
1075+
1076+
if (Alg == HashAlg)
1077+
return DigestListBin;
1078+
1079+
DigestListBin = (UINT8 *)DigestListBin + GetHashSizeFromAlgo (Alg);
1080+
}
1081+
1082+
return NULL;
1083+
}
1084+
10601085
/**
10611086
Copy TPML_DIGEST_VALUES compact binary into a buffer
10621087
10631088
@param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.
10641089
@param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
10651090
@param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.
1066-
@param[out] HashAlgorithmMaskCopied Pointer to HASH bits corresponding to the digests copied.
10671091
10681092
@return The end of buffer to hold TPML_DIGEST_VALUES compact binary.
10691093
**/
10701094
VOID *
10711095
CopyDigestListBinToBuffer (
10721096
IN OUT VOID *Buffer,
10731097
IN VOID *DigestListBin,
1074-
IN UINT32 HashAlgorithmMask,
1075-
OUT UINT32 *HashAlgorithmMaskCopied
1098+
IN UINT32 HashAlgorithmMask
10761099
)
10771100
{
10781101
UINTN Index;
10791102
UINT16 DigestSize;
1080-
UINT32 Count;
10811103
TPMI_ALG_HASH HashAlg;
10821104
UINT32 DigestListCount;
10831105
UINT32 *DigestListCountPtr;
1106+
TPMI_ALG_HASH HashAlgs[5];
1107+
VOID *Digest;
1108+
1109+
HashAlgs[0] = TPM_ALG_SHA1;
1110+
HashAlgs[1] = TPM_ALG_SHA256;
1111+
HashAlgs[2] = TPM_ALG_SM3_256;
1112+
HashAlgs[3] = TPM_ALG_SHA384;
1113+
HashAlgs[4] = TPM_ALG_SHA512;
10841114

10851115
DigestListCountPtr = (UINT32 *) Buffer;
1116+
Buffer = (UINT8 *)Buffer + sizeof(UINT32);
1117+
10861118
DigestListCount = 0;
1087-
(*HashAlgorithmMaskCopied) = 0;
10881119

1089-
Count = ReadUnaligned32 (DigestListBin);
1090-
Buffer = (UINT8 *)Buffer + sizeof(Count);
1091-
DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
1092-
for (Index = 0; Index < Count; Index++) {
1093-
HashAlg = ReadUnaligned16 (DigestListBin);
1094-
DigestListBin = (UINT8 *)DigestListBin + sizeof(HashAlg);
1120+
//
1121+
// Make sure output buffer conforms to HashAlgorithmMask.
1122+
//
1123+
// Copy digests from the entry if they are present, otherwise add missing
1124+
// digests filled as what's called "OneDigest" in TXT Software
1125+
// Development Guide (not really related, but alternatives are zeroes or
1126+
// 0xFFs, might as well use a value documented somewhere).
1127+
//
1128+
for (Index = 0; Index < ARRAY_SIZE (HashAlgs); Index++) {
1129+
HashAlg = HashAlgs[Index];
1130+
Digest = FindHashInDigestListBin (DigestListBin, HashAlg);
10951131
DigestSize = GetHashSizeFromAlgo (HashAlg);
10961132

1097-
if (IsHashAlgSupportedInHashAlgorithmMask(HashAlg, HashAlgorithmMask)) {
1098-
CopyMem (Buffer, &HashAlg, sizeof(HashAlg));
1099-
Buffer = (UINT8 *)Buffer + sizeof(HashAlg);
1100-
CopyMem (Buffer, DigestListBin, DigestSize);
1101-
Buffer = (UINT8 *)Buffer + DigestSize;
1102-
DigestListCount++;
1103-
(*HashAlgorithmMaskCopied) |= GetHashMaskFromAlgo (HashAlg);
1133+
if (!(HashAlgorithmMask & GetHashMaskFromAlgo (HashAlg))) {
1134+
// Not active.
1135+
if (Digest != NULL)
1136+
DEBUG ((DEBUG_WARN, "%a(): Event log entry includes HashAlg (0x%x) unsupported by PCR bank\n",
1137+
__FUNCTION__, HashAlg));
1138+
continue;
1139+
}
1140+
1141+
CopyMem (Buffer, &HashAlg, sizeof(HashAlg));
1142+
Buffer = (UINT8 *)Buffer + sizeof(HashAlg);
1143+
1144+
if (Digest == NULL) {
1145+
// Missing, use "OneDigest".
1146+
ZeroMem (Buffer, DigestSize);
1147+
*(UINT8 *)Buffer = 1;
1148+
DEBUG ((DEBUG_WARN, "%a(): Event log entry is missing HashAlg (0x%x) supported by PCR bank\n",
1149+
__FUNCTION__, HashAlg));
11041150
} else {
1105-
DEBUG ((DEBUG_ERROR, "WARNING: CopyDigestListBinToBuffer Event log has HashAlg unsupported by PCR bank (0x%x)\n", HashAlg));
1151+
CopyMem (Buffer, Digest, DigestSize);
11061152
}
1107-
DigestListBin = (UINT8 *)DigestListBin + DigestSize;
1153+
1154+
Buffer = (UINT8 *)Buffer + DigestSize;
1155+
1156+
DigestListCount++;
11081157
}
1158+
11091159
WriteUnaligned32 (DigestListCountPtr, DigestListCount);
11101160

11111161
return Buffer;
@@ -1552,12 +1602,10 @@ SetupEventLog (
15521602
EFI_PHYSICAL_ADDRESS Lasa;
15531603
UINTN Index;
15541604
VOID *DigestListBin;
1555-
TPML_DIGEST_VALUES TempDigestListBin;
15561605
UINT32 DigestListBinSize;
15571606
UINT8 *Event;
15581607
UINT32 EventSize;
15591608
UINT32 *EventSizePtr;
1560-
UINT32 HashAlgorithmMaskCopied;
15611609
TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
15621610
UINT8 TempBuf[sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8)];
15631611
TCG_PCR_EVENT_HDR SpecIdEvent;
@@ -1814,11 +1862,11 @@ SetupEventLog (
18141862
Status = EFI_SUCCESS;
18151863
while (!EFI_ERROR (Status) &&
18161864
(GuidHob.Raw = GetNextGuidHob (mTcg2EventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {
1817-
TcgEvent = AllocateCopyPool (GET_GUID_HOB_DATA_SIZE (GuidHob.Guid), GET_GUID_HOB_DATA (GuidHob.Guid));
1818-
ASSERT (TcgEvent != NULL);
1819-
GuidHob.Raw = GET_NEXT_HOB (GuidHob);
18201865
switch (mTcg2EventInfo[Index].LogFormat) {
18211866
case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
1867+
TcgEvent = AllocateCopyPool (GET_GUID_HOB_DATA_SIZE (GuidHob.Guid), GET_GUID_HOB_DATA (GuidHob.Guid));
1868+
ASSERT (TcgEvent != NULL);
1869+
18221870
Status = TcgDxeLogEvent (
18231871
mTcg2EventInfo[Index].LogFormat,
18241872
TcgEvent,
@@ -1828,8 +1876,15 @@ SetupEventLog (
18281876
);
18291877
break;
18301878
case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1831-
DigestListBin = (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE);
1879+
//
1880+
// This is a storage for new header.
1881+
//
1882+
TcgEvent = AllocatePool (sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + sizeof(TPML_DIGEST_VALUES) + sizeof(UINT32));
1883+
ASSERT (TcgEvent != NULL);
1884+
1885+
DigestListBin = (UINT8 *)GET_GUID_HOB_DATA (GuidHob.Guid) + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE);
18321886
DigestListBinSize = GetDigestListBinSize (DigestListBin);
1887+
CopyMem (TcgEvent, GET_GUID_HOB_DATA (GuidHob.Guid), sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE));
18331888
//
18341889
// Save event size.
18351890
//
@@ -1838,26 +1893,18 @@ SetupEventLog (
18381893
//
18391894
// Filter inactive digest in the event2 log from PEI HOB.
18401895
//
1841-
CopyMem (&TempDigestListBin, DigestListBin, GetDigestListBinSize (DigestListBin));
18421896
EventSizePtr = CopyDigestListBinToBuffer (
1897+
TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE),
18431898
DigestListBin,
1844-
&TempDigestListBin,
1845-
mTcgDxeData.BsCap.ActivePcrBanks,
1846-
&HashAlgorithmMaskCopied
1899+
mTcgDxeData.BsCap.ActivePcrBanks
18471900
);
1848-
if (HashAlgorithmMaskCopied != mTcgDxeData.BsCap.ActivePcrBanks) {
1849-
DEBUG ((
1850-
DEBUG_ERROR,
1851-
"ERROR: The event2 log includes digest hash mask 0x%x, but required digest hash mask is 0x%x\n",
1852-
HashAlgorithmMaskCopied,
1853-
mTcgDxeData.BsCap.ActivePcrBanks
1854-
));
1855-
}
18561901
//
18571902
// Restore event size.
18581903
//
18591904
CopyMem (EventSizePtr, &EventSize, sizeof(UINT32));
1860-
DigestListBinSize = GetDigestListBinSize (DigestListBin);
1905+
DigestListBinSize = GetDigestListBinSize (TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE));
1906+
1907+
DEBUG ((DEBUG_INFO, "%a: DigestListBinSize = %d\n", __FUNCTION__, DigestListBinSize));
18611908

18621909
Status = TcgDxeLogEvent (
18631910
mTcg2EventInfo[Index].LogFormat,
@@ -1869,6 +1916,7 @@ SetupEventLog (
18691916
break;
18701917
}
18711918
FreePool (TcgEvent);
1919+
GuidHob.Raw = GET_NEXT_HOB (GuidHob);
18721920
}
18731921
}
18741922
}
@@ -2863,13 +2911,10 @@ DriverEntry (
28632911
}
28642912
}
28652913

2866-
mTcgDxeData.BsCap.SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
2867-
if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) == 0) {
2868-
//
2869-
// No need to expose TCG1.2 event log if SHA1 bank does not exist.
2870-
//
2871-
mTcgDxeData.BsCap.SupportedEventLogs &= ~EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
2872-
}
2914+
//
2915+
// Only expose TCG2 event log for TPM2.
2916+
//
2917+
mTcgDxeData.BsCap.SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
28732918

28742919
DEBUG ((EFI_D_INFO, "Tcg2.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
28752920
DEBUG ((EFI_D_INFO, "Tcg2.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));

SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.inf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
ReportStatusCodeLib
6565
Tcg2PhysicalPresenceLib
6666
PeCoffLib
67+
TpmMeasurementLib
6768

6869
[Guids]
6970
## SOMETIMES_CONSUMES ## Variable:L"SecureBoot"

0 commit comments

Comments
 (0)