Skip to content

Commit efaab4a

Browse files
authored
Merge pull request #475 from aidangarske/fenrir-fixes-4
Fix wolfTPM Fenrir Findings (4)
2 parents b8ad8f7 + 73887a1 commit efaab4a

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/tpm2.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5558,7 +5558,8 @@ int TPM2_GPIO_Config(GpioConfig_In* in)
55585558
TPM2_CTX* ctx = TPM2_GetActiveCtx();
55595559
UINT32 i;
55605560

5561-
if (ctx == NULL || in == NULL || ctx->session == NULL)
5561+
if (ctx == NULL || in == NULL || ctx->session == NULL ||
5562+
in->config.count > MAX_GPIO_COUNT)
55625563
return BAD_FUNC_ARG;
55635564

55645565
rc = TPM2_AcquireLock(ctx);

src/tpm2_asn.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ int TPM2_ASN_GetLength_ex(const uint8_t* input, word32* inOutIdx, int* len,
5656
b = input[idx++];
5757
if (b >= TPM2_ASN_LONG_LENGTH) {
5858
word32 bytes = b & 0x7F;
59-
if ((idx + bytes) > maxIdx) {
59+
/* DER does not allow BER indefinite-length (0x80 => bytes == 0) */
60+
if (bytes == 0 || bytes > 3 || (idx + bytes) > maxIdx) {
6061
return TPM_RC_INSUFFICIENT;
6162
}
6263
while (bytes--) {
@@ -188,8 +189,14 @@ int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
188189
}
189190

190191
if (rc >= 0) {
191-
/* check version == 1 */
192-
if (input[idx] != TPM2_ASN_INTEGER && input[idx] != 1) {
192+
if (len <= 0 || idx >= (word32)inputSz) {
193+
rc = TPM_RC_VALUE;
194+
}
195+
}
196+
197+
if (rc >= 0) {
198+
/* check version tag is INTEGER */
199+
if (input[idx] != TPM2_ASN_INTEGER) {
193200
rc = TPM_RC_VALUE;
194201
}
195202
}
@@ -356,6 +363,8 @@ int TPM2_ASN_RsaUnpadPkcsv15(uint8_t** pSig, int* sigSz)
356363
uint8_t* sig = *pSig;
357364
int idx = 0;
358365

366+
if (*sigSz < 3) return rc;
367+
359368
if (sig[idx++] == 0x00 && sig[idx++] == 0x01) {
360369
while (idx < *sigSz) {
361370
if (sig[idx] != 0xFF)

src/tpm2_cryptocb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ int wolfTPM2_PK_RsaSign(WOLFSSL* ssl,
856856
inPad, inPadSz,
857857
out, (int*)outSz);
858858
}
859+
TPM2_ForceZero(inPad, sizeof(inPad));
859860
}
860861
wc_FreeRsaKey(&rsapub);
861862
}

src/tpm2_wrap.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ static int tpm2_ifx_cap_vendor_get(WOLFTPM2_CAPS* cap, uint32_t property,
810810
if (rc == TPM_RC_SUCCESS) {
811811
TPM2B_MAX_BUFFER* buf = &out.capabilityData.data.vendor;
812812
/* 4 bytes=count + 2 bytes=len + vendor value */
813-
if (buf->buffer[3] == 1 && buf->buffer[5] == valSz) {
813+
if (buf->size >= (int)(6 + valSz) && buf->buffer[3] == 1 && buf->buffer[5] == valSz) {
814814
XMEMCPY(val, &buf->buffer[6], valSz);
815815
if (valSz == 2) {
816816
*((uint16_t*)val) = be16_to_cpu(*((uint16_t*)val));
@@ -1615,6 +1615,8 @@ static int wolfTPM2_EncryptSecret_RSA(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpm
16151615

16161616
wc_FreeRsaKey(&rsaKey);
16171617
wc_FreeRng(&rng);
1618+
TPM2_ForceZero(&rsaKey, sizeof(rsaKey));
1619+
TPM2_ForceZero(&rng, sizeof(rng));
16181620

16191621
if (rc > 0) {
16201622
rc = (rc == secret->size) ? 0 /* success */ : BUFFER_E /* fail */;
@@ -1630,15 +1632,15 @@ int wolfTPM2_EncryptSecret(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* tpmKey,
16301632
{
16311633
int rc = NOT_COMPILED_IN;
16321634

1635+
if (dev == NULL || data == NULL || secret == NULL) {
1636+
return BAD_FUNC_ARG;
1637+
}
1638+
16331639
/* if a tpmKey is not present then we are using an unsalted session */
16341640
if (tpmKey == NULL) {
16351641
return TPM_RC_SUCCESS;
16361642
}
16371643

1638-
if (dev == NULL || data == NULL || secret == NULL) {
1639-
return BAD_FUNC_ARG;
1640-
}
1641-
16421644
#ifdef DEBUG_WOLFTPM
16431645
printf("Encrypt secret: Alg %s, Label %s\n",
16441646
TPM2_GetAlgName(tpmKey->pub.publicArea.type), label);
@@ -3275,7 +3277,7 @@ int wolfTPM2_ExportPublicKeyBuffer(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
32753277
#endif
32763278
} key;
32773279

3278-
if (dev == NULL || tpmKey == NULL) {
3280+
if (dev == NULL || tpmKey == NULL || outSz == NULL) {
32793281
return BAD_FUNC_ARG;
32803282
}
32813283

@@ -4995,17 +4997,14 @@ int wolfTPM2_NVCreateAuthPolicy(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* parent,
49954997
#endif
49964998
return rc;
49974999
}
4998-
if (rc == TPM_RC_SUCCESS && alreadyExists)
4999-
rc = TPM_RC_NV_DEFINED;
5000-
50015000
/* compute NV object with name */
50025001
XMEMSET(nv, 0, sizeof(*nv));
50035002
rctmp = wolfTPM2_NVOpen(dev, nv, nvIndex, auth, authSz);
50045003
if (rctmp != TPM_RC_SUCCESS)
50055004
rc = rctmp;
50065005

50075006
#ifdef DEBUG_WOLFTPM
5008-
printf("TPM2_NV_DefineSpace: Auth 0x%x, Idx 0x%x, Attribs 0x%d, Size %d\n",
5007+
printf("TPM2_NV_DefineSpace: Auth 0x%x, Idx 0x%x, Attribs 0x%x, Size %d\n",
50095008
(word32)in.authHandle,
50105009
(word32)in.publicInfo.nvPublic.nvIndex,
50115010
(word32)in.publicInfo.nvPublic.attributes,
@@ -6291,16 +6290,19 @@ int wolfTPM2_UnloadHandles(WOLFTPM2_DEV* dev, word32 handleStart,
62916290
word32 handleCount)
62926291
{
62936292
int rc = TPM_RC_SUCCESS;
6294-
word32 hndl;
6293+
word32 i;
62956294
WOLFTPM2_HANDLE handle;
62966295
if (dev == NULL) {
62976296
return BAD_FUNC_ARG;
62986297
}
6298+
if (handleCount != 0 && handleStart > (word32)0xFFFFFFFF - (handleCount - 1)) {
6299+
return BAD_FUNC_ARG;
6300+
}
62996301
XMEMSET(&handle, 0, sizeof(handle));
63006302
wolfTPM2_CopyAuth(&handle.auth, &dev->session[0].auth);
63016303

6302-
for (hndl=handleStart; hndl < handleStart+handleCount; hndl++) {
6303-
handle.hndl = hndl;
6304+
for (i = 0; i < handleCount; i++) {
6305+
handle.hndl = handleStart + i;
63046306
/* ignore return code failures */
63056307
(void)wolfTPM2_UnloadHandle(dev, &handle);
63066308
}

0 commit comments

Comments
 (0)