From f16250b35aff6995e540143a9858c9cf0d1f9573 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 21 Jun 2021 14:04:34 -0400 Subject: [PATCH 1/3] tpm2: Reset TPM2B buffer sizes after test fails for valid buffer size Reset the buffer size indicator in a TPM2B type of buffer after it failed the test for the maximum buffer size it allows. This prevents having bad buffer sizes in memory that can come to haunt us when writing the volatile state for example. Signed-off-by: Stefan Berger --- src/tpm2/NVMarshal.c | 1 + src/tpm2/Unmarshal.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/tpm2/NVMarshal.c b/src/tpm2/NVMarshal.c index efbab70..9f6d0f7 100644 --- a/src/tpm2/NVMarshal.c +++ b/src/tpm2/NVMarshal.c @@ -1503,6 +1503,7 @@ bn_prime_t_Unmarshal(bn_prime_t *data, BYTE **buffer, INT32 *size) "allocated %zu\n", (size_t)data->size, (size_t)data->allocated); rc = TPM_RC_SIZE; + data->size = 0; } } diff --git a/src/tpm2/Unmarshal.c b/src/tpm2/Unmarshal.c index c692ccc..8e7a9b7 100644 --- a/src/tpm2/Unmarshal.c +++ b/src/tpm2/Unmarshal.c @@ -136,6 +136,7 @@ TPM2B_Unmarshal(TPM2B *target, UINT16 targetSize, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->size > targetSize) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } if (rc == TPM_RC_SUCCESS) { @@ -1686,6 +1687,7 @@ TPMS_PCR_SELECTION_Unmarshal(TPMS_PCR_SELECTION *target, BYTE **buffer, INT32 *s if ((target->sizeofSelect < PCR_SELECT_MIN) || (target->sizeofSelect > PCR_SELECT_MAX)) { rc = TPM_RC_VALUE; + target->sizeofSelect = 0; // libtpms added } } if (rc == TPM_RC_SUCCESS) { @@ -1859,6 +1861,7 @@ TPML_CC_Unmarshal(TPML_CC *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_CAP_CC) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -1897,6 +1900,7 @@ TPML_CCA_Unmarshal(TPML_CCA *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_CAP_CC) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -1920,6 +1924,7 @@ TPML_ALG_Unmarshal(TPML_ALG *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_ALG_LIST_SIZE) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -1942,6 +1947,7 @@ TPML_HANDLE_Unmarshal(TPML_HANDLE *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_CAP_HANDLES) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -1970,11 +1976,13 @@ TPML_DIGEST_Unmarshal(TPML_DIGEST *target, BYTE **buffer, INT32 *size) /* TPM side is hard coded to 2 minimum */ if (target->count < 2) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } if (rc == TPM_RC_SUCCESS) { if (target->count > 8) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -1997,6 +2005,7 @@ TPML_DIGEST_VALUES_Unmarshal(TPML_DIGEST_VALUES *target, BYTE **buffer, INT32 *s if (rc == TPM_RC_SUCCESS) { if (target->count > HASH_COUNT) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2019,6 +2028,7 @@ TPML_PCR_SELECTION_Unmarshal(TPML_PCR_SELECTION *target, BYTE **buffer, INT32 *s if (rc == TPM_RC_SUCCESS) { if (target->count > HASH_COUNT) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2044,6 +2054,7 @@ TPML_ALG_PROPERTY_Unmarshal(TPML_ALG_PROPERTY *target, BYTE **buffer, INT32 *siz if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_CAP_ALGS) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2066,6 +2077,7 @@ TPML_TAGGED_TPM_PROPERTY_Unmarshal(TPML_TAGGED_TPM_PROPERTY *target, BYTE **buf if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_TPM_PROPERTIES) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2088,6 +2100,7 @@ TPML_TAGGED_PCR_PROPERTY_Unmarshal(TPML_TAGGED_PCR_PROPERTY *target, BYTE **buff if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_PCR_PROPERTIES) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2110,6 +2123,7 @@ TPML_ECC_CURVE_Unmarshal(TPML_ECC_CURVE *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_ECC_CURVES) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2132,6 +2146,7 @@ TPML_TAGGED_POLICY_Unmarshal(TPML_TAGGED_POLICY *target, BYTE **buffer, INT32 *s if (rc == TPM_RC_SUCCESS) { if (target->count > MAX_TAGGED_POLICIES) { rc = TPM_RC_SIZE; + target->count = 0; // libtpms added } } for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) { @@ -2781,6 +2796,7 @@ TPM2B_SENSITIVE_CREATE_Unmarshal(TPM2B_SENSITIVE_CREATE *target, BYTE **buffer, if (rc == TPM_RC_SUCCESS) { if (target->size != startSize - *size) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } return rc; @@ -3540,6 +3556,7 @@ TPM2B_ECC_POINT_Unmarshal(TPM2B_ECC_POINT *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->size != startSize - *size) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } return rc; @@ -4063,6 +4080,7 @@ TPM2B_PUBLIC_Unmarshal(TPM2B_PUBLIC *target, BYTE **buffer, INT32 *size, BOOL al if (rc == TPM_RC_SUCCESS) { if (target->size != startSize - *size) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } return rc; @@ -4158,6 +4176,7 @@ TPM2B_SENSITIVE_Unmarshal(TPM2B_SENSITIVE *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->size != startSize - *size) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } } @@ -4233,6 +4252,7 @@ TPMS_NV_PUBLIC_Unmarshal(TPMS_NV_PUBLIC *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->dataSize > MAX_NV_INDEX_SIZE) { rc = TPM_RC_SIZE; + target->dataSize = 0; // libtpms added } } return rc; @@ -4263,6 +4283,7 @@ TPM2B_NV_PUBLIC_Unmarshal(TPM2B_NV_PUBLIC *target, BYTE **buffer, INT32 *size) if (rc == TPM_RC_SUCCESS) { if (target->size != startSize - *size) { rc = TPM_RC_SIZE; + target->size = 0; // libtpms added } } return rc; -- 2.29.0