Skip to content

Commit cd4c33e

Browse files
committed
Fix Coverity CIDs 909308/909295/483395/483392 (fwTPM hash returns, setsockopt, secret_seal hex/policy)
1 parent 1601a78 commit cd4c33e

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

examples/boot/secret_seal.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int TPM2_Boot_SecretSeal_Example(void* userCtx, int argc, char *argv[])
116116
word32 secretSz = 0;
117117
const char* publicKeyFile = NULL;
118118
const char* outFile = "sealblob.bin";
119-
const char* policyFile = "policyauth.bin";
119+
const char* policyFile = NULL;
120120
byte policyDigest[WC_MAX_DIGEST_SIZE];
121121
word32 policyDigestSz = 0;
122122

@@ -151,9 +151,16 @@ int TPM2_Boot_SecretSeal_Example(void* userCtx, int argc, char *argv[])
151151
else if (XSTRNCMP(argv[argc-1], "-secrethex=", XSTRLEN("-secrethex=")) == 0) {
152152
const char* secretStr = argv[argc-1] + XSTRLEN("-secrethex=");
153153
word32 secretStrSz = (word32)XSTRLEN(secretStr);
154+
int secretHexSz;
154155
if (secretStrSz > (word32)(sizeof(secret)*2-1))
155156
secretStrSz = (word32)(sizeof(secret)*2-1);
156-
secretSz = hexToByte(secretStr, secret, secretStrSz);
157+
secretHexSz = hexToByte(secretStr, secret, secretStrSz);
158+
if (secretHexSz < 0) {
159+
printf("Invalid secret hex string\n");
160+
usage();
161+
return -1;
162+
}
163+
secretSz = (word32)secretHexSz;
157164
}
158165
else if (XSTRNCMP(argv[argc-1], "-policy=",
159166
XSTRLEN("-policy=")) == 0) {

src/fwtpm/fwtpm_command.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9948,14 +9948,24 @@ static TPM_RC FwCmd_PolicyNV(FWTPM_CTX* ctx, TPM2_Packet* cmd,
99489948
}
99499949
}
99509950
if (rc == 0 && operandBSz > 0) {
9951-
wc_HashUpdate(hashCtx, wcHash, operandB, operandBSz);
9951+
if (wc_HashUpdate(hashCtx, wcHash, operandB, operandBSz) != 0)
9952+
rc = TPM_RC_FAILURE;
99529953
}
99539954
if (rc == 0) {
99549955
FwStoreU16BE(tmpBuf, offset);
9955-
wc_HashUpdate(hashCtx, wcHash, tmpBuf, 2);
9956+
if (wc_HashUpdate(hashCtx, wcHash, tmpBuf, 2) != 0)
9957+
rc = TPM_RC_FAILURE;
9958+
}
9959+
if (rc == 0) {
99569960
FwStoreU16BE(tmpBuf, operation);
9957-
wc_HashUpdate(hashCtx, wcHash, tmpBuf, 2);
9958-
wc_HashFinal(hashCtx, wcHash, argsHash);
9961+
if (wc_HashUpdate(hashCtx, wcHash, tmpBuf, 2) != 0)
9962+
rc = TPM_RC_FAILURE;
9963+
}
9964+
if (rc == 0) {
9965+
if (wc_HashFinal(hashCtx, wcHash, argsHash) != 0)
9966+
rc = TPM_RC_FAILURE;
9967+
}
9968+
if (rc == 0) {
99599969
wc_HashFree(hashCtx, wcHash);
99609970
hashCtxInit = 0;
99619971
}
@@ -9972,15 +9982,28 @@ static TPM_RC FwCmd_PolicyNV(FWTPM_CTX* ctx, TPM2_Packet* cmd,
99729982
}
99739983
}
99749984
if (rc == 0) {
9975-
wc_HashUpdate(hashCtx, wcHash,
9976-
sess->policyDigest.buffer, sess->policyDigest.size);
9985+
if (wc_HashUpdate(hashCtx, wcHash,
9986+
sess->policyDigest.buffer, sess->policyDigest.size) != 0)
9987+
rc = TPM_RC_FAILURE;
9988+
}
9989+
if (rc == 0) {
99779990
FwStoreU32BE(ccBuf, cc);
9978-
wc_HashUpdate(hashCtx, wcHash, ccBuf, 4);
9979-
wc_HashUpdate(hashCtx, wcHash, argsHash, dSz);
9980-
if (nvNameSz > 0) {
9981-
wc_HashUpdate(hashCtx, wcHash, nvName, nvNameSz);
9982-
}
9983-
wc_HashFinal(hashCtx, wcHash, sess->policyDigest.buffer);
9991+
if (wc_HashUpdate(hashCtx, wcHash, ccBuf, 4) != 0)
9992+
rc = TPM_RC_FAILURE;
9993+
}
9994+
if (rc == 0) {
9995+
if (wc_HashUpdate(hashCtx, wcHash, argsHash, dSz) != 0)
9996+
rc = TPM_RC_FAILURE;
9997+
}
9998+
if (rc == 0 && nvNameSz > 0) {
9999+
if (wc_HashUpdate(hashCtx, wcHash, nvName, nvNameSz) != 0)
10000+
rc = TPM_RC_FAILURE;
10001+
}
10002+
if (rc == 0) {
10003+
if (wc_HashFinal(hashCtx, wcHash, sess->policyDigest.buffer) != 0)
10004+
rc = TPM_RC_FAILURE;
10005+
}
10006+
if (rc == 0) {
998410007
sess->policyDigest.size = (UINT16)dSz;
998510008
wc_HashFree(hashCtx, wcHash);
998610009
hashCtxInit = 0;

src/fwtpm/fwtpm_io.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,12 @@ static SOCKET_T CreateListenSocket(int port)
169169
return FWTPM_INVALID_FD;
170170
}
171171

172-
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
173-
(const char*)&optval, sizeof(optval));
172+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
173+
(const char*)&optval, sizeof(optval)) != 0) {
174+
#ifdef DEBUG_WOLFTPM
175+
printf("fwTPM: setsockopt(SO_REUSEADDR) failed\n");
176+
#endif
177+
}
174178

175179
XMEMSET(&addr, 0, sizeof(addr));
176180
addr.sin_family = AF_INET;

0 commit comments

Comments
 (0)