Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/attestation/activate_credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
}
printf("Read credential blob and secret from %s, %d bytes\n",
input, dataSize);
/* Validate sizes from file data to prevent buffer overrun */
if (activCredIn.credentialBlob.size >
sizeof(activCredIn.credentialBlob.buffer)) {
printf("Credential blob size %d exceeds buffer\n",
activCredIn.credentialBlob.size);
rc = BAD_FUNC_ARG;
goto exit;
}
if (activCredIn.secret.size > sizeof(activCredIn.secret.secret)) {
printf("Secret size %d exceeds buffer\n",
activCredIn.secret.size);
rc = BAD_FUNC_ARG;
goto exit;
}
#else
printf("Can not load credential. File support not enabled\n");
goto exit;
Expand Down
5 changes: 5 additions & 0 deletions examples/attestation/make_credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
wolfTPM2_GetRandom(&dev, makeCredIn.credential.buffer,
makeCredIn.credential.size);
/* Set the object name */
if (name.size > sizeof(makeCredIn.objectName.name)) {
printf("Name size %d exceeds buffer\n", name.size);
rc = BAD_FUNC_ARG;
goto exit;
}
makeCredIn.objectName.size = name.size;
XMEMCPY(makeCredIn.objectName.name, name.name,
makeCredIn.objectName.size);
Expand Down
18 changes: 14 additions & 4 deletions examples/pcr/extend.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
BYTE dataBuffer[1024];
enum wc_HashType hashType;
wc_HashAlg dig;
int hashInitialized = 0;
#endif

union {
Expand Down Expand Up @@ -153,15 +154,24 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
if (fp != XBADFILE) {
rc = TPM2_GetHashType(alg);
hashType = (enum wc_HashType)rc;
wc_HashInit(&dig, hashType);
while (!XFEOF(fp)) {
rc = wc_HashInit(&dig, hashType);
if (rc == 0)
hashInitialized = 1;
while (rc == 0 && !XFEOF(fp)) {
len = XFREAD(dataBuffer, 1, sizeof(dataBuffer), fp);
if (len > 0) {
wc_HashUpdate(&dig, hashType, dataBuffer, (int)len);
rc = wc_HashUpdate(&dig, hashType, dataBuffer, (int)len);
}
}
XFCLOSE(fp);
wc_HashFinal(&dig, hashType, hash);
if (rc == 0)
rc = wc_HashFinal(&dig, hashType, hash);
if (hashInitialized)
wc_HashFree(&dig, hashType);
if (rc != 0) {
printf("Hash operation failed %d\n", rc);
goto exit;
}

XMEMCPY(cmdIn.pcrExtend.digests.digests[0].digest.H,
hash, hashSz);
Expand Down
17 changes: 8 additions & 9 deletions src/tpm2_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,16 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
rspSz = (int)ret;
rc = TPM_RC_SUCCESS;
}
else if (ret == 0) {
#ifdef DEBUG_WOLFTPM
printf("Received EOF(0) from %s: errno %d = %s\n",
TPM2_LINUX_DEV, errno, strerror(errno));
#endif
rc = TPM_RC_FAILURE;
}
else {
#ifdef DEBUG_WOLFTPM
printf("Failed to read from %s: errno %d = %s\n",
TPM2_LINUX_DEV, errno, strerror(errno));
if (ret == 0) {
printf("Received EOF from %s\n", TPM2_LINUX_DEV);
}
else {
printf("Failed to read from %s (ret %zd):"
" errno %d = %s\n", TPM2_LINUX_DEV, ret,
errno, strerror(errno));
}
#endif
rc = TPM_RC_FAILURE;
}
Expand Down
32 changes: 32 additions & 0 deletions src/tpm2_param_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ static int TPM2_ParamEnc_XOR(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
return BUFFER_E;
}

/* Validate source key sizes to prevent overrun of source buffers */
if (sessKey->size > sizeof(sessKey->buffer)) {
return BUFFER_E;
}
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
return BUFFER_E;
}

/* Validate key sizes before copy to prevent buffer overflow */
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
return BUFFER_E;
Expand Down Expand Up @@ -264,6 +272,14 @@ static int TPM2_ParamDec_XOR(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
return BUFFER_E;
}

/* Validate source key sizes to prevent overrun of source buffers */
if (sessKey->size > sizeof(sessKey->buffer)) {
return BUFFER_E;
}
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
return BUFFER_E;
}

/* Validate key sizes before copy to prevent buffer overflow */
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
return BUFFER_E;
Expand Down Expand Up @@ -321,6 +337,14 @@ static int TPM2_ParamEnc_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
return BUFFER_E;
}

/* Validate source key sizes to prevent overrun of source buffers */
if (sessKey->size > sizeof(sessKey->buffer)) {
return BUFFER_E;
}
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
return BUFFER_E;
}

/* Validate key sizes before copy to prevent buffer overflow */
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
return BUFFER_E;
Expand Down Expand Up @@ -387,6 +411,14 @@ static int TPM2_ParamDec_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
return BUFFER_E;
}

/* Validate source key sizes to prevent overrun of source buffers */
if (sessKey->size > sizeof(sessKey->buffer)) {
return BUFFER_E;
}
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
return BUFFER_E;
}

/* Validate key sizes before copy to prevent buffer overflow */
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
return BUFFER_E;
Expand Down
2 changes: 2 additions & 0 deletions src/tpm2_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx,
rc = TPM2_Init_ex(ctx, ioCb, userCtx, timeoutTries);
#endif
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("TPM2_Init failed 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc));
#endif
return rc;
}
#ifdef DEBUG_WOLFTPM
Expand Down
Loading