Skip to content

Commit 5ccf623

Browse files
authored
Merge pull request #441 from dgarske/coverity_20251203
Various minor coverity fixes
2 parents bfbbd2e + d465155 commit 5ccf623

File tree

10 files changed

+60
-31
lines changed

10 files changed

+60
-31
lines changed

examples/attestation/make_credential.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
9898
goto exit_badargs;
9999
}
100100

101+
XMEMSET(&primary, 0, sizeof(primary));
102+
XMEMSET(&handle, 0, sizeof(handle));
103+
101104
printf("Demo how to create a credential challenge for remote attestation\n");
102105
printf("Credential will be stored in %s\n", output);
103106

examples/boot/secret_seal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int TPM2_Boot_SecretSeal_Example(void* userCtx, int argc, char *argv[])
185185
wc_FreeRng(&rng);
186186
}
187187
}
188-
if (rc != 0 || secretSz == 0) {
188+
if (rc != 0 || secretSz <= 0) {
189189
printf("Error getting secret\n");
190190
goto exit;
191191
}

examples/keygen/external_import.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ int TPM2_ExternalImport_Example(void* userCtx, int argc, char *argv[])
131131
key2 = wolfTPM2_NewKeyBlob();
132132
rsaKey3 = wolfTPM2_NewKeyBlob();
133133
#endif
134+
XMEMSET(&storage, 0, sizeof(storage));
134135
primary = &storage;
135136

136137
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);

examples/seal/unseal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
170170
fp = XFOPEN(filename, "wb");
171171
if (fp != XBADFILE) {
172172
len = XFWRITE(cmdOut_unseal.outData.buffer, 1, cmdOut_unseal.outData.size, fp);
173+
XFCLOSE(fp);
174+
173175
if (len != cmdOut_unseal.outData.size) {
174176
printf("Error while writing the unsealed data to a file.\n");
175177
goto exit;
176178
}
177179
}
178-
XFCLOSE(fp);
179180
printf("Stored unsealed data to file = %s\n", filename);
180181
}
181182
#else

examples/tls/tls_client_notpm.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,18 @@ int TLS_ClientArgs(int argc, char *argv[])
287287
printf("Failure %d (0x%x): %s\n", rc, rc, wolfSSL_ERR_reason_error_string(rc));
288288
}
289289

290-
wolfSSL_shutdown(ssl);
290+
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
291+
/* Bidirectional shutdown */
292+
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
293+
int ret = wolfSSL_shutdown(ssl);
294+
if (ret == WOLFSSL_SUCCESS) {
295+
printf("Bidirectional shutdown complete\n");
296+
}
297+
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
298+
fprintf(stderr, "Bidirectional shutdown failed\n");
299+
}
300+
}
301+
}
291302

292303
CloseAndCleanupSocket(&sockIoCtx);
293304
wolfSSL_free(ssl);

examples/tpm_test_keys.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ int writeKeyBlob(const char* filename,
149149
* save space */
150150
rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer),
151151
&pubAreaSize, &key->pub);
152-
if (rc != TPM_RC_SUCCESS)
152+
if (rc != TPM_RC_SUCCESS) {
153+
XFCLOSE(fp);
153154
return rc;
155+
}
154156
if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) {
155157
printf("writeKeyBlob: Sanity check for publicArea size failed\n");
158+
XFCLOSE(fp);
156159
return BUFFER_E;
157160
}
158161
#ifdef WOLFTPM_DEBUG_VERBOSE
@@ -204,10 +207,13 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
204207
goto exit;
205208
}
206209
fileSz -= bytes_read;
207-
210+
if (key->pub.size > sizeof(UINT16) + sizeof(pubAreaBuffer)) {
211+
printf("Public key size is too large\n");
212+
rc = BUFFER_E; goto exit;
213+
}
208214
bytes_read = XFREAD(pubAreaBuffer, 1,
209215
sizeof(UINT16) + key->pub.size, fp);
210-
if (bytes_read != sizeof(UINT16) + key->pub.size) {
216+
if (bytes_read != (sizeof(UINT16) + key->pub.size)) {
211217
printf("Read %zu, expected public blob %zu bytes\n",
212218
bytes_read, sizeof(UINT16) + key->pub.size);
213219
goto exit;

examples/wrap/wrap_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,13 +933,11 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
933933
}
934934
else if (WOLFTPM_IS_COMMAND_UNAVAILABLE(rc)) {
935935
printf("Encrypt/Decrypt: Is not a supported feature due to export controls\n");
936-
rc = TPM_RC_SUCCESS; /* clear error code */
937936
}
938937
else {
939938
printf("Encrypt/Decrypt test failed, result not as expected!\n");
940939
goto exit;
941940
}
942-
if (rc != 0) goto exit;
943941
#else
944942
(void)aesIv;
945943
#endif /* !WOLFTPM2_NO_WOLFCRYPT */

src/tpm2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,10 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
429429
/* Is auth session required for this TPM command? */
430430
if (tag == TPM_ST_SESSIONS) {
431431
/* Is there at least one auth session present? */
432-
if (info->authCnt < 1 || ctx->session == NULL)
432+
if (info->authCnt < 1 || ctx->session == NULL) {
433+
packet->pos = cmdSz; /* restore */
433434
return TPM_RC_AUTH_MISSING;
435+
}
434436

435437
#ifdef WOLFTPM_DEBUG_VERBOSE
436438
printf("Found %d auth sessions\n", info->authCnt);

src/tpm2_tis.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ int TPM2_TIS_Write(TPM2_CTX* ctx, word32 addr, const byte* value,
236236
txBuf[2] = (addr>>8) & 0xFF;
237237
txBuf[3] = (addr) & 0xFF;
238238
XMEMCPY(&txBuf[TPM_TIS_HEADER_SZ], value, len);
239-
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len], 0,
240-
sizeof(txBuf) - TPM_TIS_HEADER_SZ - len);
239+
if (len < MAX_SPI_FRAMESIZE) {
240+
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len], 0,
241+
sizeof(txBuf) - TPM_TIS_HEADER_SZ - len);
242+
}
241243
XMEMSET(rxBuf, 0, sizeof(rxBuf));
242244

243245
rc = ctx->ioCb(ctx, txBuf, rxBuf, len + TPM_TIS_HEADER_SZ, ctx->userCtx);

src/tpm2_wrap.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,9 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16821682
/* key is bindAuthValue || salt */
16831683
XMEMSET(&keyIn, 0, sizeof(keyIn));
16841684
if (bind && bind->auth.size > 0) {
1685+
if (bind->auth.size > (UINT16)sizeof(bind->auth.buffer)) {
1686+
return BUFFER_E;
1687+
}
16851688
if ((keyIn.size + bind->auth.size) > (UINT16)sizeof(keyIn.buffer)) {
16861689
return BUFFER_E;
16871690
}
@@ -1690,7 +1693,10 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16901693
keyIn.size += bind->auth.size;
16911694
}
16921695
if (session->salt.size > 0) {
1693-
if (keyIn.size + session->salt.size > sizeof(keyIn.buffer)) {
1696+
if (session->salt.size > (UINT16)sizeof(session->salt.buffer)) {
1697+
return BUFFER_E;
1698+
}
1699+
if ((keyIn.size + session->salt.size) > (UINT16)sizeof(keyIn.buffer)) {
16941700
return BUFFER_E;
16951701
}
16961702
XMEMCPY(&keyIn.buffer[keyIn.size], session->salt.buffer,
@@ -1754,6 +1760,7 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
17541760
int rc;
17551761
CreatePrimary_In createPriIn;
17561762
CreatePrimary_Out createPriOut;
1763+
TPMT_TK_CREATION* ticket;
17571764

17581765
if (dev == NULL || pkey == NULL || publicTemplate == NULL)
17591766
return BAD_FUNC_ARG;
@@ -1819,19 +1826,17 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
18191826
pkey->creationHash.size = sizeof(pkey->creationHash.buffer);
18201827
}
18211828
XMEMCPY(pkey->creationHash.buffer, createPriOut.creationHash.buffer,
1822-
createPriOut.creationHash.size);
1829+
pkey->creationHash.size);
18231830

1824-
pkey->creationTicket.tag = createPriOut.creationTicket.tag;
1825-
pkey->creationTicket.hierarchy = createPriOut.creationTicket.hierarchy;
1826-
pkey->creationTicket.digest.size = createPriOut.creationTicket.digest.size;
1827-
if (pkey->creationTicket.digest.size >
1828-
sizeof(pkey->creationTicket.digest.buffer)) {
1829-
pkey->creationTicket.digest.size =
1830-
sizeof(pkey->creationTicket.digest.buffer);
1831+
ticket = &pkey->creationTicket;
1832+
ticket->tag = createPriOut.creationTicket.tag;
1833+
ticket->hierarchy = createPriOut.creationTicket.hierarchy;
1834+
ticket->digest.size = createPriOut.creationTicket.digest.size;
1835+
if (ticket->digest.size > sizeof(ticket->digest.buffer)) {
1836+
ticket->digest.size = sizeof(ticket->digest.buffer);
18311837
}
1832-
XMEMCPY(pkey->creationTicket.digest.buffer,
1833-
createPriOut.creationTicket.digest.buffer,
1834-
createPriOut.creationTicket.digest.size);
1838+
XMEMCPY(ticket->digest.buffer, createPriOut.creationTicket.digest.buffer,
1839+
ticket->digest.size);
18351840

18361841
#ifdef DEBUG_WOLFTPM
18371842
printf("TPM2_CreatePrimary: 0x%x (%d bytes)\n",
@@ -1880,8 +1885,8 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
18801885
if (auth) {
18811886
if (authSz > (int)sizeof(changeIn.newAuth.buffer))
18821887
authSz = (int)sizeof(changeIn.newAuth.buffer);
1883-
changeIn.newAuth.size = authSz;
1884-
XMEMCPY(changeIn.newAuth.buffer, auth, changeIn.newAuth.size);
1888+
changeIn.newAuth.size = (UINT16)authSz;
1889+
XMEMCPY(changeIn.newAuth.buffer, auth, authSz);
18851890
}
18861891

18871892
rc = TPM2_ObjectChangeAuth(&changeIn, &changeOut);
@@ -3691,12 +3696,14 @@ int wolfTPM2_EccKey_TpmToWolf(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
36913696

36923697
/* load public key */
36933698
qxSz = tpmKey->pub.publicArea.unique.ecc.x.size;
3694-
if (qxSz > sizeof(qx)) {
3699+
if (qxSz > sizeof(qx) ||
3700+
qxSz > sizeof(tpmKey->pub.publicArea.unique.ecc.x.buffer)) {
36953701
return BUFFER_E;
36963702
}
36973703
XMEMCPY(qx, tpmKey->pub.publicArea.unique.ecc.x.buffer, qxSz);
36983704
qySz = tpmKey->pub.publicArea.unique.ecc.y.size;
3699-
if (qySz > sizeof(qy)) {
3705+
if (qySz > sizeof(qy) ||
3706+
qySz > sizeof(tpmKey->pub.publicArea.unique.ecc.y.buffer)) {
37003707
return BUFFER_E;
37013708
}
37023709
XMEMCPY(qy, tpmKey->pub.publicArea.unique.ecc.y.buffer, qySz);
@@ -4593,8 +4600,8 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
45934600
if (inSz > (int)sizeof(rsaDecIn.cipherText.buffer)) {
45944601
inSz = (int)sizeof(rsaDecIn.cipherText.buffer); /* truncate */
45954602
}
4596-
rsaDecIn.cipherText.size = inSz;
4597-
XMEMCPY(rsaDecIn.cipherText.buffer, in, rsaDecIn.cipherText.size);
4603+
rsaDecIn.cipherText.size = (UINT16)inSz;
4604+
XMEMCPY(rsaDecIn.cipherText.buffer, in, inSz);
45984605
/* TPM_ALG_NULL, TPM_ALG_OAEP, TPM_ALG_RSASSA or TPM_ALG_RSAPSS */
45994606
rsaDecIn.inScheme.scheme = padScheme;
46004607
rsaDecIn.inScheme.details.anySig.hashAlg = WOLFTPM2_WRAP_DIGEST;
@@ -5707,7 +5714,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57075714
printf("wolfTPM2_LoadSymmetricKey: 0x%x\n",
57085715
(word32)loadExtOut.objectHandle);
57095716
#endif
5710-
return rc;
57115717
}
57125718

57135719
exit:
@@ -5717,7 +5723,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57175723
printf("TPM2_LoadExternal: failed %d: %s\n",
57185724
rc, wolfTPM2_GetRCString(rc));
57195725
#endif
5720-
return rc;
57215726
}
57225727

57235728
return rc;

0 commit comments

Comments
 (0)