Blame SOURCES/0001-tpm2-Reset-TPM2B-buffer-sizes-after-test-fails-for-v.patch

ffd936
From 52a82f3eb6d61833c22294cb6377b8cad14e8c83 Mon Sep 17 00:00:00 2001
ffd936
From: Stefan Berger <stefanb@linux.vnet.ibm.com>
ffd936
Date: Mon, 21 Jun 2021 14:04:34 -0400
ffd936
Subject: [PATCH 1/3] tpm2: Reset TPM2B buffer sizes after test fails for valid
ffd936
 buffer size
ffd936
ffd936
Reset the buffer size indicator in a TPM2B type of buffer after it failed
ffd936
the test for the maximum buffer size it allows. This prevents having bad
ffd936
buffer sizes in memory that can come to haunt us when writing the volatile
ffd936
state for example.
ffd936
ffd936
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
ffd936
---
ffd936
 src/tpm2/NVMarshal.c |  1 +
ffd936
 src/tpm2/Unmarshal.c | 21 +++++++++++++++++++++
ffd936
 2 files changed, 22 insertions(+)
ffd936
ffd936
diff --git a/src/tpm2/NVMarshal.c b/src/tpm2/NVMarshal.c
ffd936
index 141550e..b9934d5 100644
ffd936
--- a/src/tpm2/NVMarshal.c
ffd936
+++ b/src/tpm2/NVMarshal.c
ffd936
@@ -1440,6 +1440,7 @@ bn_prime_t_Unmarshal(bn_prime_t *data, BYTE **buffer, INT32 *size)
ffd936
                                 "allocated %zu\n",
ffd936
                                 (size_t)data->size, (size_t)data->allocated);
ffd936
             rc = TPM_RC_SIZE;
ffd936
+            data->size = 0;
ffd936
         }
ffd936
     }
ffd936
 
ffd936
diff --git a/src/tpm2/Unmarshal.c b/src/tpm2/Unmarshal.c
ffd936
index 9a1d723..bac7d76 100644
ffd936
--- a/src/tpm2/Unmarshal.c
ffd936
+++ b/src/tpm2/Unmarshal.c
ffd936
@@ -137,6 +137,7 @@ TPM2B_Unmarshal(TPM2B *target, UINT16 targetSize, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->size > targetSize) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->size = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -1617,6 +1618,7 @@ TPMS_PCR_SELECTION_Unmarshal(TPMS_PCR_SELECTION *target, BYTE **buffer, INT32 *s
ffd936
 	if ((target->sizeofSelect < PCR_SELECT_MIN) ||
ffd936
 	    (target->sizeofSelect > PCR_SELECT_MAX)) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    target->sizeofSelect = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -1787,6 +1789,7 @@ TPML_CC_Unmarshal(TPML_CC *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_CAP_CC) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1824,6 +1827,7 @@ TPML_CCA_Unmarshal(TPML_CCA *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_CAP_CC) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1846,6 +1850,7 @@ TPML_ALG_Unmarshal(TPML_ALG *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_ALG_LIST_SIZE) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1868,6 +1873,7 @@ TPML_HANDLE_Unmarshal(TPML_HANDLE *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_CAP_HANDLES) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1895,11 +1901,13 @@ TPML_DIGEST_Unmarshal(TPML_DIGEST *target, BYTE **buffer, INT32 *size)
ffd936
 	/* TPM side is hard coded to 2 minimum */
ffd936
 	if (target->count < 2) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > 8) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1922,6 +1930,7 @@ TPML_DIGEST_VALUES_Unmarshal(TPML_DIGEST_VALUES *target, BYTE **buffer, INT32 *s
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > HASH_COUNT) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1944,6 +1953,7 @@ TPML_PCR_SELECTION_Unmarshal(TPML_PCR_SELECTION *target, BYTE **buffer, INT32 *s
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > HASH_COUNT) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1967,6 +1977,7 @@ TPML_ALG_PROPERTY_Unmarshal(TPML_ALG_PROPERTY *target, BYTE **buffer, INT32 *siz
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_CAP_ALGS) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -1989,6 +2000,7 @@ TPML_TAGGED_TPM_PROPERTY_Unmarshal(TPML_TAGGED_TPM_PROPERTY  *target, BYTE **buf
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_TPM_PROPERTIES) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -2011,6 +2023,7 @@ TPML_TAGGED_PCR_PROPERTY_Unmarshal(TPML_TAGGED_PCR_PROPERTY *target, BYTE **buff
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_PCR_PROPERTIES) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -2033,6 +2046,7 @@ TPML_ECC_CURVE_Unmarshal(TPML_ECC_CURVE *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_ECC_CURVES) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -2055,6 +2069,7 @@ TPML_TAGGED_POLICY_Unmarshal(TPML_TAGGED_POLICY *target, BYTE **buffer, INT32 *s
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->count > MAX_TAGGED_POLICIES) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->count = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
ffd936
@@ -2704,6 +2719,7 @@ TPM2B_SENSITIVE_CREATE_Unmarshal(TPM2B_SENSITIVE_CREATE *target, BYTE **buffer,
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->size != startSize - *size) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->size = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3462,6 +3478,7 @@ TPM2B_ECC_POINT_Unmarshal(TPM2B_ECC_POINT *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->size != startSize - *size) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->size = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3985,6 +4002,7 @@ TPM2B_PUBLIC_Unmarshal(TPM2B_PUBLIC *target, BYTE **buffer, INT32 *size, BOOL al
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->size != startSize - *size) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->size = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -4080,6 +4098,7 @@ TPM2B_SENSITIVE_Unmarshal(TPM2B_SENSITIVE *target, BYTE **buffer, INT32 *size)
ffd936
 	if (rc == TPM_RC_SUCCESS) {
ffd936
 	    if (target->size != startSize - *size) {
ffd936
 		rc = TPM_RC_SIZE;
ffd936
+		target->size = 0; // libtpms added
ffd936
 	    }
ffd936
 	}
ffd936
     }
ffd936
@@ -4155,6 +4174,7 @@ TPMS_NV_PUBLIC_Unmarshal(TPMS_NV_PUBLIC *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->dataSize > MAX_NV_INDEX_SIZE) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->dataSize = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -4185,6 +4205,7 @@ TPM2B_NV_PUBLIC_Unmarshal(TPM2B_NV_PUBLIC *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->size != startSize - *size) {
ffd936
 	    rc = TPM_RC_SIZE;
ffd936
+	    target->size = 0; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
-- 
ffd936
2.29.0
ffd936