Skip to content

Commit 4d1ec8a

Browse files
committed
Fix Fenrir findings #342, #354, #539, #540, #548, #549, #550, #551, #552, #881, #882, #887
- #342: Add ForceZero for rsaKey/rng in wolfTPM2_EncryptSecret_RSA - #354: Reorder NULL checks in wolfTPM2_EncryptSecret - #539: Fix incorrect ASN.1 version tag validation in TPM2_ASN_DecodeX509Cert - #540: Add buffer size check in tpm2_ifx_cap_vendor_get - #548: Add ForceZero for inPad in wolfTPM2_PK_RsaSign - #549: Fix integer overflow in wolfTPM2_UnloadHandles loop - #550: Add minimum size check in TPM2_ASN_RsaUnpadPkcsv15 - #551: Limit multi-byte ASN.1 length to 3 bytes in TPM2_ASN_GetLength_ex - #552: Add NULL checks for out/outSz in wolfTPM2_ExportPublicKeyBuffer - #881: Remove dead code in wolfTPM2_NVCreateAuthPolicy - #882: Fix printf format specifier (0x%d -> 0x%x) in wolfTPM2_NVCreateAuthPolicy - #887: Add bounds check for GPIO config count in TPM2_GPIO_Config
1 parent b8ad8f7 commit 4d1ec8a

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ 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+
if (bytes > 3 || (idx + bytes) > maxIdx) {
6060
return TPM_RC_INSUFFICIENT;
6161
}
6262
while (bytes--) {
@@ -188,8 +188,8 @@ int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
188188
}
189189

190190
if (rc >= 0) {
191-
/* check version == 1 */
192-
if (input[idx] != TPM2_ASN_INTEGER && input[idx] != 1) {
191+
/* check version tag is INTEGER */
192+
if (input[idx] != TPM2_ASN_INTEGER) {
193193
rc = TPM_RC_VALUE;
194194
}
195195
}
@@ -356,6 +356,8 @@ int TPM2_ASN_RsaUnpadPkcsv15(uint8_t** pSig, int* sigSz)
356356
uint8_t* sig = *pSig;
357357
int idx = 0;
358358

359+
if (*sigSz < 3) return rc;
360+
359361
if (sig[idx++] == 0x00 && sig[idx++] == 0x01) {
360362
while (idx < *sigSz) {
361363
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: 12 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 || out == 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,16 @@ 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
}
62996298
XMEMSET(&handle, 0, sizeof(handle));
63006299
wolfTPM2_CopyAuth(&handle.auth, &dev->session[0].auth);
63016300

6302-
for (hndl=handleStart; hndl < handleStart+handleCount; hndl++) {
6303-
handle.hndl = hndl;
6301+
for (i = 0; i < handleCount; i++) {
6302+
handle.hndl = handleStart + i;
63046303
/* ignore return code failures */
63056304
(void)wolfTPM2_UnloadHandle(dev, &handle);
63066305
}

0 commit comments

Comments
 (0)