Skip to content

Commit fcde77c

Browse files
committed
Fix coverity scan issues in wolfTPM
1 parent 25466a9 commit fcde77c

6 files changed

Lines changed: 58 additions & 13 deletions

File tree

examples/attestation/activate_credential.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
192192
}
193193
printf("Read credential blob and secret from %s, %d bytes\n",
194194
input, dataSize);
195+
/* Validate sizes from file data to prevent buffer overrun */
196+
if (activCredIn.credentialBlob.size >
197+
sizeof(activCredIn.credentialBlob.buffer)) {
198+
printf("Credential blob size %d exceeds buffer\n",
199+
activCredIn.credentialBlob.size);
200+
goto exit;
201+
}
202+
if (activCredIn.secret.size > sizeof(activCredIn.secret.secret)) {
203+
printf("Secret size %d exceeds buffer\n",
204+
activCredIn.secret.size);
205+
goto exit;
206+
}
195207
#else
196208
printf("Can not load credential. File support not enabled\n");
197209
goto exit;

examples/attestation/make_credential.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
156156
wolfTPM2_GetRandom(&dev, makeCredIn.credential.buffer,
157157
makeCredIn.credential.size);
158158
/* Set the object name */
159+
if (name.size > sizeof(makeCredIn.objectName.name)) {
160+
printf("Name size %d exceeds buffer\n", name.size);
161+
goto exit;
162+
}
159163
makeCredIn.objectName.size = name.size;
160164
XMEMCPY(makeCredIn.objectName.name, name.name,
161165
makeCredIn.objectName.size);

examples/pcr/extend.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
153153
if (fp != XBADFILE) {
154154
rc = TPM2_GetHashType(alg);
155155
hashType = (enum wc_HashType)rc;
156-
wc_HashInit(&dig, hashType);
157-
while (!XFEOF(fp)) {
156+
rc = wc_HashInit(&dig, hashType);
157+
while (rc == 0 && !XFEOF(fp)) {
158158
len = XFREAD(dataBuffer, 1, sizeof(dataBuffer), fp);
159159
if (len > 0) {
160-
wc_HashUpdate(&dig, hashType, dataBuffer, (int)len);
160+
rc = wc_HashUpdate(&dig, hashType, dataBuffer, (int)len);
161161
}
162162
}
163163
XFCLOSE(fp);
164-
wc_HashFinal(&dig, hashType, hash);
164+
if (rc == 0)
165+
rc = wc_HashFinal(&dig, hashType, hash);
165166

166167
XMEMCPY(cmdIn.pcrExtend.digests.digests[0].digest.H,
167168
hash, hashSz);

src/tpm2_linux.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,11 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
144144
rspSz = (int)ret;
145145
rc = TPM_RC_SUCCESS;
146146
}
147-
else if (ret == 0) {
148-
#ifdef DEBUG_WOLFTPM
149-
printf("Received EOF(0) from %s: errno %d = %s\n",
150-
TPM2_LINUX_DEV, errno, strerror(errno));
151-
#endif
152-
rc = TPM_RC_FAILURE;
153-
}
154147
else {
155148
#ifdef DEBUG_WOLFTPM
156-
printf("Failed to read from %s: errno %d = %s\n",
157-
TPM2_LINUX_DEV, errno, strerror(errno));
149+
printf("Failed to read from %s (ret %zd): errno %d"
150+
" = %s\n", TPM2_LINUX_DEV, ret, errno,
151+
strerror(errno));
158152
#endif
159153
rc = TPM_RC_FAILURE;
160154
}

src/tpm2_param_enc.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ static int TPM2_ParamEnc_XOR(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
209209
return BUFFER_E;
210210
}
211211

212+
/* Validate source key sizes to prevent overrun of source buffers */
213+
if (sessKey->size > sizeof(sessKey->buffer)) {
214+
return BUFFER_E;
215+
}
216+
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
217+
return BUFFER_E;
218+
}
219+
212220
/* Validate key sizes before copy to prevent buffer overflow */
213221
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
214222
return BUFFER_E;
@@ -264,6 +272,14 @@ static int TPM2_ParamDec_XOR(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
264272
return BUFFER_E;
265273
}
266274

275+
/* Validate source key sizes to prevent overrun of source buffers */
276+
if (sessKey->size > sizeof(sessKey->buffer)) {
277+
return BUFFER_E;
278+
}
279+
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
280+
return BUFFER_E;
281+
}
282+
267283
/* Validate key sizes before copy to prevent buffer overflow */
268284
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
269285
return BUFFER_E;
@@ -321,6 +337,14 @@ static int TPM2_ParamEnc_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
321337
return BUFFER_E;
322338
}
323339

340+
/* Validate source key sizes to prevent overrun of source buffers */
341+
if (sessKey->size > sizeof(sessKey->buffer)) {
342+
return BUFFER_E;
343+
}
344+
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
345+
return BUFFER_E;
346+
}
347+
324348
/* Validate key sizes before copy to prevent buffer overflow */
325349
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
326350
return BUFFER_E;
@@ -387,6 +411,14 @@ static int TPM2_ParamDec_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* sessKey,
387411
return BUFFER_E;
388412
}
389413

414+
/* Validate source key sizes to prevent overrun of source buffers */
415+
if (sessKey->size > sizeof(sessKey->buffer)) {
416+
return BUFFER_E;
417+
}
418+
if (bindKey != NULL && bindKey->size > sizeof(bindKey->buffer)) {
419+
return BUFFER_E;
420+
}
421+
390422
/* Validate key sizes before copy to prevent buffer overflow */
391423
if (sessKey->size + bindKeySz > sizeof(keyIn.buffer)) {
392424
return BUFFER_E;

src/tpm2_wrap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx,
123123
rc = TPM2_Init_ex(ctx, ioCb, userCtx, timeoutTries);
124124
#endif
125125
if (rc != TPM_RC_SUCCESS) {
126+
#ifdef DEBUG_WOLFTPM
126127
printf("TPM2_Init failed 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc));
128+
#endif
127129
return rc;
128130
}
129131
#ifdef DEBUG_WOLFTPM

0 commit comments

Comments
 (0)