Blame SOURCES/0001-Esys_CreateLoaded-fix-resource-name-calculation.patch

b9d646
From 70e9fae7ef535e7cf27a72ddbc818dfefcbdbdbb Mon Sep 17 00:00:00 2001
b9d646
From: William Roberts <william.c.roberts@intel.com>
b9d646
Date: Wed, 18 Sep 2019 11:29:57 -0700
b9d646
Subject: [PATCH] Esys_CreateLoaded: fix resource name calculation
b9d646
b9d646
The name calculated and cached for the ESYS_TR resource object was based
b9d646
on the user supplied TPMT_PUBLIC. However, this template is often
b9d646
missing data that the TPM fills in and returns in the TPM2B_PUBLIC
b9d646
structure. Because of this, the cached name returned from
b9d646
Esys_TR_GetName() and the name read from Esys_ReadPublic() would differ.
b9d646
b9d646
Add a test to detect this condition and correct it by copying the
b9d646
returned TPM2B_PUBLIC to the ESYS_TR resource nodes TPM2B_PUBLIC cache
b9d646
and calculate the name off of that.
b9d646
b9d646
Fixes: #1516
b9d646
b9d646
Signed-off-by: William Roberts <william.c.roberts@intel.com>
b9d646
---
b9d646
 src/tss2-esys/api/Esys_CreateLoaded.c    | 14 ++++-----
b9d646
 test/integration/esys-createloaded.int.c | 37 ++++++++++++++++++++++++
b9d646
 2 files changed, 42 insertions(+), 9 deletions(-)
b9d646
b9d646
diff --git a/src/tss2-esys/api/Esys_CreateLoaded.c b/src/tss2-esys/api/Esys_CreateLoaded.c
b9d646
index a92649cade27..44c4400fcff9 100644
b9d646
--- a/src/tss2-esys/api/Esys_CreateLoaded.c
b9d646
+++ b/src/tss2-esys/api/Esys_CreateLoaded.c
b9d646
@@ -317,14 +317,6 @@ Esys_CreateLoaded_Finish(
b9d646
         goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
b9d646
     }
b9d646
 
b9d646
-    /* Update the meta data of the ESYS_TR object */
b9d646
-    objectHandleNode->rsrc.rsrcType = IESYSC_KEY_RSRC;
b9d646
-    size_t offset = 0;
b9d646
-    r = Tss2_MU_TPMT_PUBLIC_Unmarshal(&esysContext->in.CreateLoaded.inPublic->buffer[0],
b9d646
-                                      sizeof(TPMT_PUBLIC), &offset ,
b9d646
-                                      &objectHandleNode->rsrc.misc.rsrc_key_pub.publicArea);
b9d646
-    goto_if_error(r, "Unmarshal TPMT_PUBULIC", error_cleanup);
b9d646
-
b9d646
     /*Receive the TPM response and handle resubmissions if necessary. */
b9d646
     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
b9d646
     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
b9d646
@@ -386,8 +378,12 @@ Esys_CreateLoaded_Finish(
b9d646
                         error_cleanup);
b9d646
 
b9d646
 
b9d646
+    /* Update the meta data of the ESYS_TR object */
b9d646
+    objectHandleNode->rsrc.rsrcType = IESYSC_KEY_RSRC;
b9d646
+    objectHandleNode->rsrc.misc.rsrc_key_pub = *loutPublic;
b9d646
+
b9d646
     /* Check name and outPublic for consistency */
b9d646
-    if (!iesys_compare_name(loutPublic, &name))
b9d646
+    if (!iesys_compare_name(&objectHandleNode->rsrc.misc.rsrc_key_pub, &name))
b9d646
         goto_error(r, TSS2_ESYS_RC_MALFORMED_RESPONSE,
b9d646
             "in Public name not equal name in response", error_cleanup);
b9d646
 
b9d646
diff --git a/test/integration/esys-createloaded.int.c b/test/integration/esys-createloaded.int.c
b9d646
index ec8d68a0d43d..118f2a3bb1ff 100644
b9d646
--- a/test/integration/esys-createloaded.int.c
b9d646
+++ b/test/integration/esys-createloaded.int.c
b9d646
@@ -8,6 +8,7 @@
b9d646
 #include <config.h>
b9d646
 #endif
b9d646
 
b9d646
+#include <stdbool.h>
b9d646
 #include <stdlib.h>
b9d646
 
b9d646
 #include "tss2_esys.h"
b9d646
@@ -19,6 +20,35 @@
b9d646
 #include "util/log.h"
b9d646
 #include "util/aux_util.h"
b9d646
 
b9d646
+static bool check_name(ESYS_CONTEXT * esys_context, ESYS_TR object_handle)
b9d646
+{
b9d646
+    bool result = false;
b9d646
+
b9d646
+    TPM2B_NAME *read_name = NULL;
b9d646
+    TPM2B_NAME *get_name = NULL;
b9d646
+
b9d646
+    TSS2_RC r = Esys_ReadPublic(esys_context, object_handle,
b9d646
+                                ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
b9d646
+                                NULL, &read_name, NULL);
b9d646
+    goto_if_error(r, "Error esys readpublic", out);
b9d646
+
b9d646
+    r = Esys_TR_GetName(esys_context, object_handle, &get_name);
b9d646
+    goto_if_error(r, "Error esys getname", out);
b9d646
+
b9d646
+    if (read_name->size != get_name->size) {
b9d646
+        LOG_ERROR("name size mismatch %u != %u",
b9d646
+                  read_name->size, get_name->size);
b9d646
+        goto out;
b9d646
+    }
b9d646
+
b9d646
+    result = memcmp(read_name->name, get_name->name, get_name->size) == 0;
b9d646
+
b9d646
+out:
b9d646
+    free(read_name);
b9d646
+    free(get_name);
b9d646
+
b9d646
+    return result;
b9d646
+}
b9d646
 /** This test is intended to test the ESAPI command CreateLoaded.
b9d646
  *
b9d646
  * We start by creating a primary key (Esys_CreatePrimary).
b9d646
@@ -29,6 +59,8 @@
b9d646
  *  - Esys_CreatePrimary() (M)
b9d646
  *  - Esys_FlushContext() (M)
b9d646
  *  - Esys_StartAuthSession() (M)
b9d646
+ *  - Esys_TR_GetName() (M)
b9d646
+ *  - Esys_TR_ReadPublic() (M)
b9d646
  *
b9d646
  * Used compiler defines: TEST_SESSION
b9d646
  *
b9d646
@@ -239,6 +271,11 @@ test_esys_createloaded(ESYS_CONTEXT * esys_context)
b9d646
 
b9d646
     goto_if_error(r, "Error During CreateLoaded", error);
b9d646
 
b9d646
+    bool names_match = check_name(esys_context, objectHandle);
b9d646
+    if (!names_match) {
b9d646
+        goto error;
b9d646
+    }
b9d646
+
b9d646
     r = Esys_FlushContext(esys_context, primaryHandle);
b9d646
     goto_if_error(r, "Flushing context", error);
b9d646
 
b9d646
-- 
b9d646
2.27.0
b9d646