Skip to content

Commit 27d1a22

Browse files
committed
Various minor coverity fixes
1 parent 0a3b8de commit 27d1a22

File tree

10 files changed

+46
-27
lines changed

10 files changed

+46
-27
lines changed

examples/attestation/make_credential.c

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

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

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
939939
printf("Encrypt/Decrypt test failed, result not as expected!\n");
940940
goto exit;
941941
}
942-
if (rc != 0) goto exit;
943942
#else
944943
(void)aesIv;
945944
#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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int TPM2_TIS_Write(TPM2_CTX* ctx, word32 addr, const byte* value,
237237
txBuf[2] = (addr>>8) & 0xFF;
238238
txBuf[3] = (addr) & 0xFF;
239239
XMEMCPY(&txBuf[TPM_TIS_HEADER_SZ], value, len);
240-
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len], 0,
240+
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len - 1], 0,
241241
sizeof(txBuf) - TPM_TIS_HEADER_SZ - len);
242242
XMEMSET(rxBuf, 0, sizeof(rxBuf));
243243

src/tpm2_wrap.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16901690
keyIn.size += bind->auth.size;
16911691
}
16921692
if (session->salt.size > 0) {
1693-
if (keyIn.size + session->salt.size > sizeof(keyIn.buffer)) {
1693+
if ((keyIn.size + session->salt.size) > (UINT16)sizeof(keyIn.buffer)) {
16941694
return BUFFER_E;
16951695
}
16961696
XMEMCPY(&keyIn.buffer[keyIn.size], session->salt.buffer,
@@ -1754,6 +1754,7 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
17541754
int rc;
17551755
CreatePrimary_In createPriIn;
17561756
CreatePrimary_Out createPriOut;
1757+
TPMT_TK_CREATION* ticket;
17571758

17581759
if (dev == NULL || pkey == NULL || publicTemplate == NULL)
17591760
return BAD_FUNC_ARG;
@@ -1819,19 +1820,17 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
18191820
pkey->creationHash.size = sizeof(pkey->creationHash.buffer);
18201821
}
18211822
XMEMCPY(pkey->creationHash.buffer, createPriOut.creationHash.buffer,
1822-
createPriOut.creationHash.size);
1823+
pkey->creationHash.size);
18231824

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);
1825+
ticket = &pkey->creationTicket;
1826+
ticket->tag = createPriOut.creationTicket.tag;
1827+
ticket->hierarchy = createPriOut.creationTicket.hierarchy;
1828+
ticket->digest.size = createPriOut.creationTicket.digest.size;
1829+
if (ticket->digest.size > sizeof(ticket->digest.buffer)) {
1830+
ticket->digest.size = sizeof(ticket->digest.buffer);
18311831
}
1832-
XMEMCPY(pkey->creationTicket.digest.buffer,
1833-
createPriOut.creationTicket.digest.buffer,
1834-
createPriOut.creationTicket.digest.size);
1832+
XMEMCPY(ticket->digest.buffer, createPriOut.creationTicket.digest.buffer,
1833+
ticket->digest.size);
18351834

18361835
#ifdef DEBUG_WOLFTPM
18371836
printf("TPM2_CreatePrimary: 0x%x (%d bytes)\n",
@@ -1880,8 +1879,8 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
18801879
if (auth) {
18811880
if (authSz > (int)sizeof(changeIn.newAuth.buffer))
18821881
authSz = (int)sizeof(changeIn.newAuth.buffer);
1883-
changeIn.newAuth.size = authSz;
1884-
XMEMCPY(changeIn.newAuth.buffer, auth, changeIn.newAuth.size);
1882+
changeIn.newAuth.size = (UINT16)authSz;
1883+
XMEMCPY(changeIn.newAuth.buffer, auth, authSz);
18851884
}
18861885

18871886
rc = TPM2_ObjectChangeAuth(&changeIn, &changeOut);
@@ -4593,8 +4592,8 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
45934592
if (inSz > (int)sizeof(rsaDecIn.cipherText.buffer)) {
45944593
inSz = (int)sizeof(rsaDecIn.cipherText.buffer); /* truncate */
45954594
}
4596-
rsaDecIn.cipherText.size = inSz;
4597-
XMEMCPY(rsaDecIn.cipherText.buffer, in, rsaDecIn.cipherText.size);
4595+
rsaDecIn.cipherText.size = (UINT16)inSz;
4596+
XMEMCPY(rsaDecIn.cipherText.buffer, in, inSz);
45984597
/* TPM_ALG_NULL, TPM_ALG_OAEP, TPM_ALG_RSASSA or TPM_ALG_RSAPSS */
45994598
rsaDecIn.inScheme.scheme = padScheme;
46004599
rsaDecIn.inScheme.details.anySig.hashAlg = WOLFTPM2_WRAP_DIGEST;
@@ -5707,7 +5706,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57075706
printf("wolfTPM2_LoadSymmetricKey: 0x%x\n",
57085707
(word32)loadExtOut.objectHandle);
57095708
#endif
5710-
return rc;
57115709
}
57125710

57135711
exit:
@@ -5717,7 +5715,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57175715
printf("TPM2_LoadExternal: failed %d: %s\n",
57185716
rc, wolfTPM2_GetRCString(rc));
57195717
#endif
5720-
return rc;
57215718
}
57225719

57235720
return rc;

0 commit comments

Comments
 (0)