Skip to content

Commit 75867cc

Browse files
committed
F-2975 - https://fenrir.wolfssl.com/finding/2975 - Fix NULL pointer dereference in wolfTPM2_LoadRsaPrivateKey_ex and wolfTPM2_LoadEccPrivateKey
1 parent 5e0f300 commit 75867cc

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/tpm2_wrap.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,8 +3499,15 @@ int wolfTPM2_LoadRsaPrivateKey_ex(WOLFTPM2_DEV* dev,
34993499
rc = wolfTPM2_ImportRsaPrivateKey(dev, parentKey, &keyBlob, rsaPub, rsaPubSz,
35003500
exponent, rsaPriv, rsaPrivSz, scheme, hashAlg);
35013501
if (rc == 0) {
3502-
rc = wolfTPM2_LoadKey(dev, &keyBlob,
3503-
(WOLFTPM2_HANDLE*)&parentKey->handle);
3502+
WOLFTPM2_HANDLE parentHandle_lcl, *parentHandle = &parentHandle_lcl;
3503+
if (parentKey != NULL) {
3504+
parentHandle = (WOLFTPM2_HANDLE*)&parentKey->handle;
3505+
}
3506+
else {
3507+
XMEMSET(parentHandle, 0, sizeof(*parentHandle));
3508+
parentHandle->hndl = TPM_RH_OWNER;
3509+
}
3510+
rc = wolfTPM2_LoadKey(dev, &keyBlob, parentHandle);
35043511
}
35053512

35063513
/* return loaded key */
@@ -3673,8 +3680,15 @@ int wolfTPM2_LoadEccPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey,
36733680
rc = wolfTPM2_ImportEccPrivateKey(dev, parentKey, &keyBlob, curveId,
36743681
eccPubX, eccPubXSz, eccPubY, eccPubYSz, eccPriv, eccPrivSz);
36753682
if (rc == 0) {
3676-
rc = wolfTPM2_LoadKey(dev, &keyBlob,
3677-
(WOLFTPM2_HANDLE*)&parentKey->handle);
3683+
WOLFTPM2_HANDLE parentHandle_lcl, *parentHandle = &parentHandle_lcl;
3684+
if (parentKey != NULL) {
3685+
parentHandle = (WOLFTPM2_HANDLE*)&parentKey->handle;
3686+
}
3687+
else {
3688+
XMEMSET(parentHandle, 0, sizeof(*parentHandle));
3689+
parentHandle->hndl = TPM_RH_OWNER;
3690+
}
3691+
rc = wolfTPM2_LoadKey(dev, &keyBlob, parentHandle);
36783692
}
36793693

36803694
/* return loaded key */

tests/unit_tests.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,51 @@ static void test_wolfTPM2_KeyBlob(TPM_ALG_ID alg)
14411441
TPM2_GetAlgName(alg), rc == 0 ? "Passed" : "Failed");
14421442
}
14431443

1444+
/* Test NULL parentKey handling in LoadRsaPrivateKey_ex and LoadEccPrivateKey */
1445+
static void test_wolfTPM2_LoadPrivateKey_NullParent(void)
1446+
{
1447+
int rc;
1448+
WOLFTPM2_DEV dev;
1449+
WOLFTPM2_KEY key;
1450+
#ifndef NO_RSA
1451+
/* Dummy RSA key material for testing NULL parentKey handling */
1452+
byte rsaPub[1] = {0};
1453+
byte rsaPriv[1] = {0};
1454+
#endif
1455+
#ifdef HAVE_ECC
1456+
/* Dummy ECC key material for testing NULL parentKey handling */
1457+
byte eccPubX[32] = {0};
1458+
byte eccPubY[32] = {0};
1459+
byte eccPriv[32] = {0};
1460+
#endif
1461+
1462+
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);
1463+
AssertIntEQ(rc, 0);
1464+
1465+
XMEMSET(&key, 0, sizeof(key));
1466+
1467+
/* Test NULL parentKey does not crash (should not dereference NULL) */
1468+
#ifndef NO_RSA
1469+
rc = wolfTPM2_LoadRsaPrivateKey_ex(&dev, NULL, &key, rsaPub, sizeof(rsaPub),
1470+
RSA_DEFAULT_PUBLIC_EXPONENT, rsaPriv, sizeof(rsaPriv),
1471+
TPM_ALG_NULL, TPM_ALG_NULL);
1472+
/* rc may fail due to no real TPM, but must not crash */
1473+
AssertIntNE(rc, BAD_FUNC_ARG);
1474+
#endif
1475+
#ifdef HAVE_ECC
1476+
XMEMSET(&key, 0, sizeof(key));
1477+
rc = wolfTPM2_LoadEccPrivateKey(&dev, NULL, &key, TPM_ECC_NIST_P256,
1478+
eccPubX, sizeof(eccPubX), eccPubY, sizeof(eccPubY),
1479+
eccPriv, sizeof(eccPriv));
1480+
/* rc may fail due to no real TPM, but must not crash */
1481+
AssertIntNE(rc, BAD_FUNC_ARG);
1482+
#endif
1483+
1484+
wolfTPM2_Cleanup(&dev);
1485+
1486+
printf("Test TPM Wrapper:\tLoadPrivateKey NullParent:\tPassed\n");
1487+
}
1488+
14441489
#endif /* !WOLFTPM2_NO_WRAPPER */
14451490

14461491
#ifndef NO_MAIN_DRIVER
@@ -1476,6 +1521,7 @@ int unit_tests(int argc, char *argv[])
14761521
test_wolfTPM2_PCRPolicy();
14771522
#endif
14781523
test_wolfTPM2_EncryptSecret();
1524+
test_wolfTPM2_LoadPrivateKey_NullParent();
14791525
test_wolfTPM2_KeyBlob(TPM_ALG_RSA);
14801526
test_wolfTPM2_KeyBlob(TPM_ALG_ECC);
14811527
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \

0 commit comments

Comments
 (0)