Blame SOURCES/lib-tpm2_options-restore-TCTI-configuration-environm.patch

22cd47
From 175e47711c72a8169f94b971c4e9973bbfb04efc Mon Sep 17 00:00:00 2001
22cd47
From: Joshua Lock <joshua.g.lock@intel.com>
22cd47
Date: Wed, 5 Sep 2018 23:21:21 +0100
22cd47
Subject: [PATCH] lib/tpm2_options: restore TCTI configuration environment
22cd47
 variables
22cd47
22cd47
The port to TSS2.0 introduced a new unified environment variable to
22cd47
configure a TCTI, TPM2TOOLS_ENV_TCTI. Unfortunately this also unwittingly
22cd47
removed the old-style environment variable per TCTI configuration options,
22cd47
which is a behavioural regression for the 3.x series of tpm2-tools.
22cd47
22cd47
Restore the original TPM2TOOLS_* environment variables in addition to the
22cd47
new style single environment variable.
22cd47
22cd47
Fixes issue #1171
22cd47
22cd47
Signed-off-by: Joshua Lock <joshua.g.lock@intel.com>
22cd47
---
22cd47
 lib/tpm2_options.c | 134 ++++++++++++++++++++++++++++++++++++---------
22cd47
 1 file changed, 107 insertions(+), 27 deletions(-)
22cd47
22cd47
diff --git a/lib/tpm2_options.c b/lib/tpm2_options.c
22cd47
index 751b0eee9819..2531948ecf74 100644
22cd47
--- a/lib/tpm2_options.c
22cd47
+++ b/lib/tpm2_options.c
22cd47
@@ -52,6 +52,10 @@
22cd47
 #endif
22cd47
 
22cd47
 #define TPM2TOOLS_ENV_TCTI      "TPM2TOOLS_TCTI"
22cd47
+#define TPM2TOOLS_ENV_TCTI_NAME "TPM2TOOLS_TCTI_NAME"
22cd47
+#define TPM2TOOLS_ENV_DEVICE    "TPM2TOOLS_DEVICE_FILE"
22cd47
+#define TPM2TOOLS_ENV_SOCK_ADDR "TPM2TOOLS_SOCKET_ADDRESS"
22cd47
+#define TPM2TOOLS_ENV_SOCK_PORT "TPM2TOOLS_SOCKET_PORT"
22cd47
 #define TPM2TOOLS_ENV_ENABLE_ERRATA  "TPM2TOOLS_ENABLE_ERRATA"
22cd47
 
22cd47
 tpm2_options *tpm2_options_new(const char *short_opts, size_t len,
22cd47
@@ -136,13 +140,25 @@ void tpm2_options_free(tpm2_options *opts) {
22cd47
 }
22cd47
 typedef struct tcti_conf tcti_conf;
22cd47
 struct tcti_conf {
22cd47
-    const char *name;
22cd47
-    const char *opts;
22cd47
+    char *name;
22cd47
+    char *opts;
22cd47
 };
22cd47
 
22cd47
+/*
22cd47
+ * Some tcti names changed in TSS 2.0, so in order to not break the
22cd47
+ * expected options of the 3.X tools series map:
22cd47
+ * - abrmd  -> tabrmd
22cd47
+ * - socket -> mssim
22cd47
+ */
22cd47
 static inline const char *fixup_name(const char *name) {
22cd47
 
22cd47
-    return !strcmp(name, "abrmd") ? "tabrmd" : name;
22cd47
+    if (!strcmp(name, "abrmd")) {
22cd47
+        return "tabrmd";
22cd47
+    } else if (!strcmp(name, "socket")) {
22cd47
+        return "mssim";
22cd47
+    }
22cd47
+
22cd47
+    return name;
22cd47
 }
22cd47
 
22cd47
 static const char *find_default_tcti(void) {
22cd47
@@ -165,27 +181,14 @@ static const char *find_default_tcti(void) {
22cd47
     return NULL;
22cd47
 }
22cd47
 
22cd47
-static tcti_conf tcti_get_config(const char *optstr) {
22cd47
-
22cd47
-    /* set up the default configuration */
22cd47
-    tcti_conf conf = {
22cd47
-        .name = find_default_tcti()
22cd47
-    };
22cd47
-
22cd47
-    /* no tcti config supplied, get it from env */
22cd47
-    if (!optstr) {
22cd47
-        optstr = getenv (TPM2TOOLS_ENV_TCTI);
22cd47
-        if (!optstr) {
22cd47
-            /* nothing user supplied, use default */
22cd47
-            return conf;
22cd47
-        }
22cd47
-    }
22cd47
+/* Parse new-style, TSS 2.0, environment variables */
22cd47
+static void parse_env_tcti(const char *optstr, tcti_conf *conf) {
22cd47
 
22cd47
     char *split = strchr(optstr, ':');
22cd47
     if (!split) {
22cd47
         /* --tcti=device */
22cd47
-        conf.name = fixup_name(optstr);
22cd47
-        return conf;
22cd47
+        conf->name = strdup(fixup_name(optstr));
22cd47
+        return;
22cd47
     }
22cd47
 
22cd47
     /*
22cd47
@@ -200,24 +203,99 @@ static tcti_conf tcti_get_config(const char *optstr) {
22cd47
 
22cd47
     /* Case A */
22cd47
     if (!optstr[0] && !split[1]) {
22cd47
-        return conf;
22cd47
+        return;
22cd47
     }
22cd47
 
22cd47
     /* Case B */
22cd47
     if (!optstr[0]) {
22cd47
-        conf.opts = &split[1];
22cd47
-        return conf;
22cd47
+        conf->opts = strdup(&split[1]);
22cd47
+        return;
22cd47
     }
22cd47
 
22cd47
     /* Case C */
22cd47
     if (!split[1]) {
22cd47
-        conf.name = fixup_name(optstr);
22cd47
-        return conf;
22cd47
+        conf->name = strdup(fixup_name(optstr));
22cd47
+        return;
22cd47
     }
22cd47
 
22cd47
     /* Case D */
22cd47
-    conf.name = fixup_name(optstr);
22cd47
-    conf.opts = &split[1];
22cd47
+    conf->name = strdup(fixup_name(optstr));
22cd47
+    conf->opts = strdup(&split[1]);
22cd47
+    return;
22cd47
+}
22cd47
+
22cd47
+static char* parse_device_tcti(void) {
22cd47
+    const char *device = getenv(TPM2TOOLS_ENV_DEVICE);
22cd47
+    return strdup(device);
22cd47
+}
22cd47
+
22cd47
+static char* parse_socket_tcti(void) {
22cd47
+
22cd47
+    /*
22cd47
+     * tpm2_tcti_ldr_load() expects conf->opts to be of the format
22cd47
+     * "host=localhost,port=2321" for the mssim tcti
22cd47
+     *
22cd47
+     * Max IPV6 IP address, 45 characters   (45)
22cd47
+     * Ports are 16bit int, 5 characters    (5)
22cd47
+     * "host=", 5 characters                (5)
22cd47
+     * "port=", 5 characters                (5)
22cd47
+     * strlen = 60
22cd47
+     */
22cd47
+    size_t optlen = 60;
22cd47
+    const char *host;
22cd47
+    const char *port;
22cd47
+    char *ret = malloc(optlen);
22cd47
+    if (!ret) {
22cd47
+        LOG_ERR ("OOM");
22cd47
+        return NULL;
22cd47
+    }
22cd47
+
22cd47
+    host = getenv(TPM2TOOLS_ENV_SOCK_ADDR);
22cd47
+    port = getenv(TPM2TOOLS_ENV_SOCK_PORT);
22cd47
+
22cd47
+    if (host && port) {
22cd47
+        snprintf(ret, optlen, "host=%s,port=%s", host, port);
22cd47
+    } else if (host) {
22cd47
+        snprintf(ret, optlen, "host=%s", host);
22cd47
+    } else if (port) {
22cd47
+        snprintf(ret, optlen, "port=%s", port);
22cd47
+    }
22cd47
+    return ret;
22cd47
+}
22cd47
+
22cd47
+static tcti_conf tcti_get_config(const char *optstr) {
22cd47
+
22cd47
+    tcti_conf conf = {
22cd47
+        .name = NULL
22cd47
+    };
22cd47
+
22cd47
+    /* no tcti config supplied, get it from env */
22cd47
+    if (!optstr) {
22cd47
+        /*
22cd47
+         * Check the "old" way of specifying TCTI, using a shared env var and
22cd47
+         * per-tcti option variables.
22cd47
+         */
22cd47
+        optstr = getenv (TPM2TOOLS_ENV_TCTI_NAME);
22cd47
+        if (optstr) {
22cd47
+            conf.name = strdup(fixup_name(optstr));
22cd47
+            if (!strcmp(conf.name, "mssim")) {
22cd47
+                conf.opts = parse_socket_tcti();
22cd47
+            } else if (!strcmp(conf.name, "device")) {
22cd47
+                conf.opts = parse_device_tcti();
22cd47
+            }
22cd47
+        } else {
22cd47
+            /* Check the new way of defining a TCTI using a shared env var */
22cd47
+            optstr = getenv (TPM2TOOLS_ENV_TCTI);
22cd47
+            if (optstr) {
22cd47
+                parse_env_tcti(optstr, &conf;;
22cd47
+            }
22cd47
+        }
22cd47
+    }
22cd47
+
22cd47
+    if (!conf.name) {
22cd47
+        conf.name = strdup(find_default_tcti());
22cd47
+    }
22cd47
+
22cd47
     return conf;
22cd47
 }
22cd47
 
22cd47
@@ -418,6 +496,8 @@ tpm2_option_code tpm2_handle_options (int argc, char **argv,
22cd47
         if (!flags->enable_errata) {
22cd47
             flags->enable_errata = !!getenv (TPM2TOOLS_ENV_ENABLE_ERRATA);
22cd47
         }
22cd47
+        free(conf.name);
22cd47
+        free(conf.opts);
22cd47
     }
22cd47
 
22cd47
     rc = tpm2_option_code_continue;
22cd47
-- 
22cd47
2.17.1
22cd47