From 27d1a22144e847c0041e8782f1bbc90babb020af Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 3 Dec 2025 12:47:21 -0800 Subject: [PATCH] Various minor coverity fixes --- examples/attestation/make_credential.c | 2 ++ examples/boot/secret_seal.c | 2 +- examples/keygen/external_import.c | 1 + examples/seal/unseal.c | 3 ++- examples/tls/tls_client_notpm.c | 13 +++++++++- examples/tpm_test_keys.c | 12 +++++++--- examples/wrap/wrap_test.c | 1 - src/tpm2.c | 4 +++- src/tpm2_tis.c | 2 +- src/tpm2_wrap.c | 33 ++++++++++++-------------- 10 files changed, 46 insertions(+), 27 deletions(-) diff --git a/examples/attestation/make_credential.c b/examples/attestation/make_credential.c index a8965190..b411603b 100644 --- a/examples/attestation/make_credential.c +++ b/examples/attestation/make_credential.c @@ -98,6 +98,8 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[]) goto exit_badargs; } + XMEMSET(&primary, 0, sizeof(primary)); + printf("Demo how to create a credential challenge for remote attestation\n"); printf("Credential will be stored in %s\n", output); diff --git a/examples/boot/secret_seal.c b/examples/boot/secret_seal.c index a334a32c..069e7233 100644 --- a/examples/boot/secret_seal.c +++ b/examples/boot/secret_seal.c @@ -185,7 +185,7 @@ int TPM2_Boot_SecretSeal_Example(void* userCtx, int argc, char *argv[]) wc_FreeRng(&rng); } } - if (rc != 0 || secretSz == 0) { + if (rc != 0 || secretSz <= 0) { printf("Error getting secret\n"); goto exit; } diff --git a/examples/keygen/external_import.c b/examples/keygen/external_import.c index cb008f84..504fa542 100644 --- a/examples/keygen/external_import.c +++ b/examples/keygen/external_import.c @@ -131,6 +131,7 @@ int TPM2_ExternalImport_Example(void* userCtx, int argc, char *argv[]) key2 = wolfTPM2_NewKeyBlob(); rsaKey3 = wolfTPM2_NewKeyBlob(); #endif + XMEMSET(&storage, 0, sizeof(storage)); primary = &storage; rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL); diff --git a/examples/seal/unseal.c b/examples/seal/unseal.c index 079512c2..c01397f2 100644 --- a/examples/seal/unseal.c +++ b/examples/seal/unseal.c @@ -170,12 +170,13 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[]) fp = XFOPEN(filename, "wb"); if (fp != XBADFILE) { len = XFWRITE(cmdOut_unseal.outData.buffer, 1, cmdOut_unseal.outData.size, fp); + XFCLOSE(fp); + if (len != cmdOut_unseal.outData.size) { printf("Error while writing the unsealed data to a file.\n"); goto exit; } } - XFCLOSE(fp); printf("Stored unsealed data to file = %s\n", filename); } #else diff --git a/examples/tls/tls_client_notpm.c b/examples/tls/tls_client_notpm.c index 47333771..648a6d40 100644 --- a/examples/tls/tls_client_notpm.c +++ b/examples/tls/tls_client_notpm.c @@ -287,7 +287,18 @@ exit: printf("Failure %d (0x%x): %s\n", rc, rc, wolfSSL_ERR_reason_error_string(rc)); } - wolfSSL_shutdown(ssl); + if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) { + /* Bidirectional shutdown */ + if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) { + int ret = wolfSSL_shutdown(ssl); + if (ret == WOLFSSL_SUCCESS) { + printf("Bidirectional shutdown complete\n"); + } + else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) { + fprintf(stderr, "Bidirectional shutdown failed\n"); + } + } + } CloseAndCleanupSocket(&sockIoCtx); wolfSSL_free(ssl); diff --git a/examples/tpm_test_keys.c b/examples/tpm_test_keys.c index ae66f063..4d94fcf7 100644 --- a/examples/tpm_test_keys.c +++ b/examples/tpm_test_keys.c @@ -149,10 +149,13 @@ int writeKeyBlob(const char* filename, * save space */ rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer), &pubAreaSize, &key->pub); - if (rc != TPM_RC_SUCCESS) + if (rc != TPM_RC_SUCCESS) { + XFCLOSE(fp); return rc; + } if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) { printf("writeKeyBlob: Sanity check for publicArea size failed\n"); + XFCLOSE(fp); return BUFFER_E; } #ifdef WOLFTPM_DEBUG_VERBOSE @@ -204,10 +207,13 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key) goto exit; } fileSz -= bytes_read; - + if (key->pub.size > sizeof(UINT16) + sizeof(pubAreaBuffer)) { + printf("Public key size is too large\n"); + rc = BUFFER_E; goto exit; + } bytes_read = XFREAD(pubAreaBuffer, 1, sizeof(UINT16) + key->pub.size, fp); - if (bytes_read != sizeof(UINT16) + key->pub.size) { + if (bytes_read != (sizeof(UINT16) + key->pub.size)) { printf("Read %zu, expected public blob %zu bytes\n", bytes_read, sizeof(UINT16) + key->pub.size); goto exit; diff --git a/examples/wrap/wrap_test.c b/examples/wrap/wrap_test.c index 84828001..2b22ecdf 100644 --- a/examples/wrap/wrap_test.c +++ b/examples/wrap/wrap_test.c @@ -939,7 +939,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[]) printf("Encrypt/Decrypt test failed, result not as expected!\n"); goto exit; } - if (rc != 0) goto exit; #else (void)aesIv; #endif /* !WOLFTPM2_NO_WOLFCRYPT */ diff --git a/src/tpm2.c b/src/tpm2.c index 01e2891b..994f8f7e 100644 --- a/src/tpm2.c +++ b/src/tpm2.c @@ -429,8 +429,10 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet, /* Is auth session required for this TPM command? */ if (tag == TPM_ST_SESSIONS) { /* Is there at least one auth session present? */ - if (info->authCnt < 1 || ctx->session == NULL) + if (info->authCnt < 1 || ctx->session == NULL) { + packet->pos = cmdSz; /* restore */ return TPM_RC_AUTH_MISSING; + } #ifdef WOLFTPM_DEBUG_VERBOSE printf("Found %d auth sessions\n", info->authCnt); diff --git a/src/tpm2_tis.c b/src/tpm2_tis.c index 95defd76..c7578a0c 100644 --- a/src/tpm2_tis.c +++ b/src/tpm2_tis.c @@ -237,7 +237,7 @@ int TPM2_TIS_Write(TPM2_CTX* ctx, word32 addr, const byte* value, txBuf[2] = (addr>>8) & 0xFF; txBuf[3] = (addr) & 0xFF; XMEMCPY(&txBuf[TPM_TIS_HEADER_SZ], value, len); - XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len], 0, + XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len - 1], 0, sizeof(txBuf) - TPM_TIS_HEADER_SZ - len); XMEMSET(rxBuf, 0, sizeof(rxBuf)); diff --git a/src/tpm2_wrap.c b/src/tpm2_wrap.c index 3c10706c..fb2237ec 100644 --- a/src/tpm2_wrap.c +++ b/src/tpm2_wrap.c @@ -1690,7 +1690,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session, keyIn.size += bind->auth.size; } if (session->salt.size > 0) { - if (keyIn.size + session->salt.size > sizeof(keyIn.buffer)) { + if ((keyIn.size + session->salt.size) > (UINT16)sizeof(keyIn.buffer)) { return BUFFER_E; } XMEMCPY(&keyIn.buffer[keyIn.size], session->salt.buffer, @@ -1754,6 +1754,7 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey, int rc; CreatePrimary_In createPriIn; CreatePrimary_Out createPriOut; + TPMT_TK_CREATION* ticket; if (dev == NULL || pkey == NULL || publicTemplate == NULL) return BAD_FUNC_ARG; @@ -1819,19 +1820,17 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey, pkey->creationHash.size = sizeof(pkey->creationHash.buffer); } XMEMCPY(pkey->creationHash.buffer, createPriOut.creationHash.buffer, - createPriOut.creationHash.size); + pkey->creationHash.size); - pkey->creationTicket.tag = createPriOut.creationTicket.tag; - pkey->creationTicket.hierarchy = createPriOut.creationTicket.hierarchy; - pkey->creationTicket.digest.size = createPriOut.creationTicket.digest.size; - if (pkey->creationTicket.digest.size > - sizeof(pkey->creationTicket.digest.buffer)) { - pkey->creationTicket.digest.size = - sizeof(pkey->creationTicket.digest.buffer); + ticket = &pkey->creationTicket; + ticket->tag = createPriOut.creationTicket.tag; + ticket->hierarchy = createPriOut.creationTicket.hierarchy; + ticket->digest.size = createPriOut.creationTicket.digest.size; + if (ticket->digest.size > sizeof(ticket->digest.buffer)) { + ticket->digest.size = sizeof(ticket->digest.buffer); } - XMEMCPY(pkey->creationTicket.digest.buffer, - createPriOut.creationTicket.digest.buffer, - createPriOut.creationTicket.digest.size); + XMEMCPY(ticket->digest.buffer, createPriOut.creationTicket.digest.buffer, + ticket->digest.size); #ifdef DEBUG_WOLFTPM printf("TPM2_CreatePrimary: 0x%x (%d bytes)\n", @@ -1880,8 +1879,8 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, if (auth) { if (authSz > (int)sizeof(changeIn.newAuth.buffer)) authSz = (int)sizeof(changeIn.newAuth.buffer); - changeIn.newAuth.size = authSz; - XMEMCPY(changeIn.newAuth.buffer, auth, changeIn.newAuth.size); + changeIn.newAuth.size = (UINT16)authSz; + XMEMCPY(changeIn.newAuth.buffer, auth, authSz); } rc = TPM2_ObjectChangeAuth(&changeIn, &changeOut); @@ -4593,8 +4592,8 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, if (inSz > (int)sizeof(rsaDecIn.cipherText.buffer)) { inSz = (int)sizeof(rsaDecIn.cipherText.buffer); /* truncate */ } - rsaDecIn.cipherText.size = inSz; - XMEMCPY(rsaDecIn.cipherText.buffer, in, rsaDecIn.cipherText.size); + rsaDecIn.cipherText.size = (UINT16)inSz; + XMEMCPY(rsaDecIn.cipherText.buffer, in, inSz); /* TPM_ALG_NULL, TPM_ALG_OAEP, TPM_ALG_RSASSA or TPM_ALG_RSAPSS */ rsaDecIn.inScheme.scheme = padScheme; rsaDecIn.inScheme.details.anySig.hashAlg = WOLFTPM2_WRAP_DIGEST; @@ -5707,7 +5706,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg, printf("wolfTPM2_LoadSymmetricKey: 0x%x\n", (word32)loadExtOut.objectHandle); #endif - return rc; } exit: @@ -5717,7 +5715,6 @@ exit: printf("TPM2_LoadExternal: failed %d: %s\n", rc, wolfTPM2_GetRCString(rc)); #endif - return rc; } return rc;