Skip to content

Commit ba82968

Browse files
liqiqiiimergify[bot]
authored andcommitted
SecurityPkg: Fix Tcg2SubmitCommand in TPM field upgrade scenario
There's a bug that after the device was forced shut down during the tpm firmware update process, then the TPM will stay in field upgrade mode and refuse to work properly. This will block TPM startup and cause the device to bootloop. Now in the TCG_DXE_DATA struct we don’t have a flag to indicate the current TPM mode, and we will simply treat the upper Scenario with this DEVICE_ERROR and set the TPMPresentFlag to FALSE. Later in Tcg2SubmitCommand we will just return DEVICE_ERROR and skip the actual TPM recovery. We should have an extra flag that check for the TPM response code and if it's TPM_RC_UPGRADE, we knew the device is in field upgrade mode and should continue the workflow. We should only return EFI_DEVICE_ERROR when both TPMPresentFlag and TpmUpdateFlag are false. The field upgrade is part of TCG spec, and the capsule update/recovery is part of UEFI spec. Make this PR to bring in the fix for this corner case. Signed-off-by: Liqi Qi <[email protected]>
1 parent 02c88f2 commit ba82968

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,17 @@ typedef struct {
7979
UINTN Next800155EventOffset;
8080
} TCG_EVENT_LOG_AREA_STRUCT;
8181

82+
// Mapping of TPM return status to BIOS/OS TPM support and related flags (TPMPresentFlag, TpmUpdateFlag)
83+
// +-------------------+---------------------+-------------------+----------------+---------------+
84+
// | TPM Return Status | Support TPM in BIOS | Support TPM in OS | TPMPresentFlag | TpmUpdateFlag |
85+
// |-------------------|---------------------|-------------------|----------------|---------------|
86+
// | SUCCESS | YES | YES | TRUE | FALSE |
87+
// | FIELD_UPGRADE | YES | NO | FALSE | TRUE |
88+
// | Other FAIL | NO | NO | FALSE | FALSE |
89+
// +-------------------+---------------------+-------------------+----------------+---------------+
8290
typedef struct _TCG_DXE_DATA {
8391
EFI_TCG2_BOOT_SERVICE_CAPABILITY BsCap;
92+
BOOLEAN TpmUpdateFlag;
8493
TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
8594
BOOLEAN GetEventLogCalled[TCG_EVENT_LOG_AREA_COUNT_MAX];
8695
TCG_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
@@ -101,6 +110,7 @@ TCG_DXE_DATA mTcgDxeData = {
101110
0, // NumberOfPCRBanks
102111
0, // ActivePcrBanks
103112
},
113+
FALSE,
104114
};
105115

106116
UINTN mBootAttempts = 0;
@@ -1407,6 +1417,8 @@ Tcg2SubmitCommand (
14071417
)
14081418
{
14091419
EFI_STATUS Status;
1420+
TPM_RC ResponseCode;
1421+
UINT32 CurrentOutputBlockSize;
14101422

14111423
if ((This == NULL) ||
14121424
(InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||
@@ -1415,10 +1427,6 @@ Tcg2SubmitCommand (
14151427
return EFI_INVALID_PARAMETER;
14161428
}
14171429

1418-
if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1419-
return EFI_DEVICE_ERROR;
1420-
}
1421-
14221430
if (InputParameterBlockSize > mTcgDxeData.BsCap.MaxCommandSize) {
14231431
return EFI_INVALID_PARAMETER;
14241432
}
@@ -1427,13 +1435,53 @@ Tcg2SubmitCommand (
14271435
return EFI_INVALID_PARAMETER;
14281436
}
14291437

1430-
Status = Tpm2SubmitCommand (
1431-
InputParameterBlockSize,
1432-
InputParameterBlock,
1433-
&OutputParameterBlockSize,
1434-
OutputParameterBlock
1435-
);
1436-
return Status;
1438+
//
1439+
// Always attempt to submit the command, but if the TPM is already flagged
1440+
// as not present, we expect it to fail other than the capsule update scenario.
1441+
//
1442+
CurrentOutputBlockSize = OutputParameterBlockSize;
1443+
Status = Tpm2SubmitCommand (
1444+
InputParameterBlockSize,
1445+
InputParameterBlock,
1446+
&CurrentOutputBlockSize,
1447+
OutputParameterBlock
1448+
);
1449+
if (EFI_ERROR (Status)) {
1450+
return mTcgDxeData.BsCap.TPMPresentFlag ? Status : EFI_DEVICE_ERROR;
1451+
}
1452+
1453+
if (CurrentOutputBlockSize < sizeof (TPM2_RESPONSE_HEADER)) {
1454+
DEBUG ((DEBUG_ERROR, "%a: Response buffer too small!\n", __func__));
1455+
return EFI_DEVICE_ERROR;
1456+
}
1457+
1458+
//
1459+
// Correctly read the response code and swap bytes from Big-Endian to Host order.
1460+
// The responseCode field is at offset 6 of the response header.
1461+
//
1462+
ResponseCode = SwapBytes32 (ReadUnaligned32 ((UINT32 *)(OutputParameterBlock + 6)));
1463+
DEBUG ((DEBUG_ERROR, "Response code is %x", ResponseCode));
1464+
// If the response code ever equals to TPM_RC_UPGRADE, it means the TPM is in field
1465+
// upgrade mode, we set TpmUpdateFlag to TRUE.
1466+
if (ResponseCode == TPM_RC_UPGRADE) {
1467+
DEBUG ((DEBUG_INFO, "TPM response code TPM_RC_UPDATE received. Setting flag.\n"));
1468+
mTcgDxeData.TpmUpdateFlag = TRUE;
1469+
}
1470+
1471+
// Now that we have set the TPMPresentFlag, it should be able to reflect the actual TPM presence
1472+
// as long as the device is not in field update mode.
1473+
if ((mTcgDxeData.BsCap.TPMPresentFlag == FALSE) && (mTcgDxeData.TpmUpdateFlag == FALSE)) {
1474+
DEBUG ((DEBUG_WARN, "%a: TPMPresentFlag is FALSE. Expecting command to fail.\n", __func__));
1475+
return EFI_DEVICE_ERROR;
1476+
}
1477+
1478+
// If the response code is not TPM_RC_SUCCESS and the device is not in field update mode, return error.
1479+
if ((ResponseCode != TPM_RC_SUCCESS) && (mTcgDxeData.TpmUpdateFlag == FALSE)) {
1480+
DEBUG ((DEBUG_ERROR, "%a: Command failed with response code 0x%x\n", __func__, ResponseCode));
1481+
return EFI_DEVICE_ERROR;
1482+
}
1483+
1484+
return EFI_SUCCESS;
14371485
}
14381486

14391487
/**

0 commit comments

Comments
 (0)