Skip to content

Commit 34919d2

Browse files
committed
Coverity fixes for new fwtpm code.
1 parent b8e63ea commit 34919d2

4 files changed

Lines changed: 53 additions & 32 deletions

File tree

examples/pcr/policy.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int TPM2_PCR_Policy_Test(void* userCtx, int argc, char *argv[])
6161
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
6262
WOLFTPM2_SESSION tpmSession;
6363
int i;
64+
int hexRet;
6465
byte digest[32];
6566
word32 digestLen = 0;
6667
union {
@@ -108,7 +109,13 @@ int TPM2_PCR_Policy_Test(void* userCtx, int argc, char *argv[])
108109
usage();
109110
return 0;
110111
}
111-
digestLen = hexToByte(digestStr, digest, digestLen / 2);
112+
hexRet = hexToByte(digestStr, digest, digestLen / 2);
113+
if (hexRet < 0) {
114+
printf("Invalid hex digest string\n");
115+
usage();
116+
return 0;
117+
}
118+
digestLen = (word32)hexRet;
112119
}
113120
else if (argv[argc-1][0] != '-') {
114121
/* TODO: Allow selection of multiple PCR's SHA-1 or SHA2-256 */

src/fwtpm/fwtpm_command.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,7 +3587,7 @@ static TPM_RC FwCmd_Create(FWTPM_CTX* ctx, TPM2_Packet* cmd,
35873587

35883588
if (rc == 0 && sensDataSize > 0) {
35893589
/* Use caller-supplied key material */
3590-
if (sensDataSize > (UINT16)FWTPM_MAX_PRIVKEY_DER) {
3590+
if (sensDataSize > (UINT16)FWTPM_MAX_DATA_BUF) {
35913591
rc = TPM_RC_SIZE;
35923592
}
35933593
if (rc == 0) {
@@ -4090,7 +4090,7 @@ static TPM_RC FwCmd_LoadExternal(FWTPM_CTX* ctx, TPM2_Packet* cmd,
40904090
if (rc == 0 && inPrivSize > 0 && sensitiveType == TPM_ALG_SYMCIPHER &&
40914091
qSz > 0) {
40924092
/* For SYMCIPHER, qBuf contains the raw AES key bytes */
4093-
if (qSz > (UINT16)FWTPM_MAX_PRIVKEY_DER) {
4093+
if (qSz > (UINT16)FWTPM_MAX_DER_SIG_BUF) {
40944094
rc = TPM_RC_SIZE;
40954095
}
40964096
if (rc == 0) {
@@ -5459,7 +5459,7 @@ static TPM_RC FwCmd_CreateLoaded(FWTPM_CTX* ctx, TPM2_Packet* cmd,
54595459
}
54605460

54615461
if (rc == 0 && sensDataSize > 0) {
5462-
if (sensDataSize > (UINT16)FWTPM_MAX_PRIVKEY_DER) {
5462+
if (sensDataSize > (UINT16)FWTPM_MAX_DATA_BUF) {
54635463
rc = TPM_RC_SIZE;
54645464
}
54655465
if (rc == 0) {
@@ -5496,7 +5496,7 @@ static TPM_RC FwCmd_CreateLoaded(FWTPM_CTX* ctx, TPM2_Packet* cmd,
54965496
}
54975497

54985498
if (rc == 0 && sensDataSize > 0) {
5499-
if (sensDataSize > (UINT16)FWTPM_MAX_PRIVKEY_DER) {
5499+
if (sensDataSize > (UINT16)FWTPM_MAX_DATA_BUF) {
55005500
rc = TPM_RC_SIZE;
55015501
}
55025502
if (rc == 0) {
@@ -7339,6 +7339,9 @@ static TPM_RC FwCmd_StartAuthSession(FWTPM_CTX* ctx, TPM2_Packet* cmd,
73397339
XMEMCPY(&bindAuth, &bindObj->authValue, sizeof(TPM2B_AUTH));
73407340
}
73417341
}
7342+
if (bindAuth.size > sizeof(bindAuth.buffer)) {
7343+
bindAuth.size = (UINT16)sizeof(bindAuth.buffer);
7344+
}
73427345
if (bindAuth.size > 0) {
73437346
if (keyInSz + bindAuth.size <= (int)sizeof(keyIn)) {
73447347
XMEMCPY(keyIn + keyInSz, bindAuth.buffer, bindAuth.size);
@@ -11108,31 +11111,35 @@ static TPM_RC FwCmd_Quote(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1110811111
wcH = FwGetWcHashType(pcrHashAlg);
1110911112
dSz = TPM2_GetHashDigestSize(pcrHashAlg);
1111011113
if (wcH != WC_HASH_TYPE_NONE && dSz > 0) {
11111-
wc_HashInit(hashCtx, wcH);
11112-
for (s = 0; s < numSel; s++) {
11113-
int bank = FwGetPcrBankIndex(selections[s].hashAlg);
11114-
int bankDSz = TPM2_GetHashDigestSize(
11115-
selections[s].hashAlg);
11116-
UINT32 j;
11117-
if (bank < 0 || bankDSz == 0)
11118-
continue;
11119-
for (j = 0; j < selections[s].sizeOfSelect; j++) {
11120-
int pcr;
11121-
for (pcr = 0; pcr < 8; pcr++) {
11122-
if (selections[s].pcrSelect[j] & (1 << pcr)) {
11123-
int pcrIdx = j * 8 + pcr;
11124-
if (pcrIdx < IMPLEMENTATION_PCR) {
11125-
wc_HashUpdate(hashCtx, wcH,
11126-
ctx->pcrDigest[pcrIdx][bank],
11127-
bankDSz);
11114+
if (wc_HashInit(hashCtx, wcH) != 0) {
11115+
rc = TPM_RC_FAILURE;
11116+
}
11117+
if (rc == 0) {
11118+
for (s = 0; s < numSel; s++) {
11119+
int bank = FwGetPcrBankIndex(selections[s].hashAlg);
11120+
int bankDSz = TPM2_GetHashDigestSize(
11121+
selections[s].hashAlg);
11122+
UINT32 j;
11123+
if (bank < 0 || bankDSz == 0)
11124+
continue;
11125+
for (j = 0; j < selections[s].sizeOfSelect; j++) {
11126+
int pcr;
11127+
for (pcr = 0; pcr < 8; pcr++) {
11128+
if (selections[s].pcrSelect[j] & (1 << pcr)) {
11129+
int pcrIdx = j * 8 + pcr;
11130+
if (pcrIdx < IMPLEMENTATION_PCR) {
11131+
wc_HashUpdate(hashCtx, wcH,
11132+
ctx->pcrDigest[pcrIdx][bank],
11133+
bankDSz);
11134+
}
1112811135
}
1112911136
}
1113011137
}
1113111138
}
11139+
wc_HashFinal(hashCtx, wcH, pcrDigestBuf);
11140+
wc_HashFree(hashCtx, wcH);
11141+
pcrDigestSz = dSz;
1113211142
}
11133-
wc_HashFinal(hashCtx, wcH, pcrDigestBuf);
11134-
wc_HashFree(hashCtx, wcH);
11135-
pcrDigestSz = dSz;
1113611143
}
1113711144
TPM2_Packet_AppendU16(&attestPkt, (UINT16)pcrDigestSz);
1113811145
TPM2_Packet_AppendBytes(&attestPkt, pcrDigestBuf, pcrDigestSz);
@@ -11547,8 +11554,13 @@ static TPM_RC FwCmd_NV_Certify(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1154711554
dSz = TPM2_GetHashDigestSize(nv->nvPublic.nameAlg);
1154811555
if (wcH != WC_HASH_TYPE_NONE && dSz > 0) {
1154911556
FwStoreU16BE(nvName.name, nv->nvPublic.nameAlg);
11550-
wc_Hash(wcH, nvPubBuf, tmpPkt.pos, nvName.name + 2, dSz);
11551-
nvName.size = (UINT16)(2 + dSz);
11557+
if (wc_Hash(wcH, nvPubBuf, tmpPkt.pos,
11558+
nvName.name + 2, dSz) == 0) {
11559+
nvName.size = (UINT16)(2 + dSz);
11560+
}
11561+
else {
11562+
rc = TPM_RC_FAILURE;
11563+
}
1155211564
}
1155311565
}
1155411566

@@ -11738,7 +11750,7 @@ static TPM_RC FwCmd_MakeCredential(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1173811750
/* patch blob size */
1173911751
blobSz = rsp->pos - blobStart;
1174011752
if (blobSz < 0 || blobSz > 0xFFFF ||
11741-
encSeedSz < 0 || encSeedSz > 0xFFFF) {
11753+
encSeedSz < 0 || encSeedSz > (int)FWTPM_MAX_PUB_BUF) {
1174211754
rc = TPM_RC_SIZE;
1174311755
}
1174411756
if (rc == 0) {
@@ -11914,6 +11926,9 @@ static TPM_RC FwCmd_ActivateCredential(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1191411926
objName->name, objName->size,
1191511927
credOut, (int)sizeof(credOut), &credSz);
1191611928
}
11929+
if (rc == 0 && credSz > (UINT16)sizeof(credOut)) {
11930+
rc = TPM_RC_SIZE;
11931+
}
1191711932

1191811933
/* Build response: TPM2B_DIGEST */
1191911934
if (rc == 0) {
@@ -12849,7 +12864,6 @@ int FWTPM_ProcessCommand(FWTPM_CTX* ctx,
1284912864
cmdAuthCnt++;
1285012865
}
1285112866

12852-
cmdPkt.pos = authEnd;
1285312867
cpStart = authEnd; /* cpBuffer starts after auth area */
1285412868
}
1285512869
}

src/fwtpm/fwtpm_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int main(int argc, char* argv[])
108108
/* Delete NV state file if --clear was requested */
109109
if (clearNv) {
110110
printf("Clearing NV state file: %s\n", FWTPM_NV_FILE);
111-
remove(FWTPM_NV_FILE);
111+
(void)remove(FWTPM_NV_FILE);
112112
}
113113

114114
/* Initialize fwTPM */

tests/fwtpm_unit_tests.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ int fwtpm_unit_tests(int argc, char *argv[])
20952095
printf("fwTPM Unit Tests\n");
20962096

20972097
/* Remove stale NV state to ensure clean test runs */
2098-
remove(FWTPM_NV_FILE);
2098+
(void)remove(FWTPM_NV_FILE);
20992099

21002100
/* Lifecycle */
21012101
test_fwtpm_init_cleanup();
@@ -2142,7 +2142,7 @@ int fwtpm_unit_tests(int argc, char *argv[])
21422142
* state, so remove FWTPM_NV_FILE afterwards. */
21432143
test_fwtpm_clock_sethal();
21442144
test_fwtpm_nv_sethal_mock();
2145-
remove(FWTPM_NV_FILE);
2145+
(void)remove(FWTPM_NV_FILE);
21462146

21472147
/* Key operations */
21482148
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)

0 commit comments

Comments
 (0)