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

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