* [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-02 14:18 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-02 14:30 UTC (permalink / raw)
To: jarkko.sakkinen, zohar, jejb
Cc: tee-dev, daniel.thompson, Sumit Garg, op-tee, corbet,
janne.karhunen, linux-doc, jmorris, linux-kernel, dhowells,
linux-security-module, keyrings, Markus.Wamser, casey,
linux-integrity, jens.wiklander, linux-arm-kernel, serge
Current trusted keys framework is tightly coupled to use TPM device as
an underlying implementation which makes it difficult for implementations
like Trusted Execution Environment (TEE) etc. to provide trusted keys
support in case platform doesn't posses a TPM device.
So this patch tries to add generic trusted keys framework where underlying
implementations like TPM, TEE etc. could be easily plugged-in.
Suggested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
---
include/keys/trusted-type.h | 45 ++++
include/keys/trusted_tpm.h | 15 --
security/keys/Kconfig | 21 +-
security/keys/trusted-keys/Makefile | 5 +-
security/keys/trusted-keys/trusted_core.c | 319 ++++++++++++++++++++++++++++
security/keys/trusted-keys/trusted_tpm1.c | 335 ++++++------------------------
6 files changed, 440 insertions(+), 300 deletions(-)
create mode 100644 security/keys/trusted-keys/trusted_core.c
diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index a94c03a..819a102 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -40,6 +40,51 @@ struct trusted_key_options {
uint32_t policyhandle;
};
+struct trusted_key_ops {
+ /*
+ * flag to indicate if trusted key implementation supports migration
+ * or not.
+ */
+ unsigned char migratable;
+
+ /* Initialize key interface. */
+ int (*init)(void);
+
+ /* Seal a key. */
+ int (*seal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Unseal a key. */
+ int (*unseal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Get a randomized key. */
+ int (*get_random)(unsigned char *key, size_t key_len);
+
+ /* Exit key interface. */
+ void (*exit)(void);
+};
+
extern struct key_type key_type_trusted;
+#if defined(CONFIG_TRUSTED_TPM)
+extern struct trusted_key_ops tpm_trusted_key_ops;
+#endif
+
+#define TRUSTED_DEBUG 0
+
+#if TRUSTED_DEBUG
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+ pr_info("trusted_key: key_len %d\n", p->key_len);
+ print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
+ 16, 1, p->key, p->key_len, 0);
+ pr_info("trusted_key: bloblen %d\n", p->blob_len);
+ print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
+ 16, 1, p->blob, p->blob_len, 0);
+ pr_info("trusted_key: migratable %d\n", p->migratable);
+}
+#else
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+}
+#endif
#endif /* _KEYS_TRUSTED_TYPE_H */
diff --git a/include/keys/trusted_tpm.h b/include/keys/trusted_tpm.h
index a56d8e1..5753231 100644
--- a/include/keys/trusted_tpm.h
+++ b/include/keys/trusted_tpm.h
@@ -60,17 +60,6 @@ static inline void dump_options(struct trusted_key_options *o)
16, 1, o->pcrinfo, o->pcrinfo_len, 0);
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
- pr_info("trusted_key: key_len %d\n", p->key_len);
- print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
- 16, 1, p->key, p->key_len, 0);
- pr_info("trusted_key: bloblen %d\n", p->blob_len);
- print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
- 16, 1, p->blob, p->blob_len, 0);
- pr_info("trusted_key: migratable %d\n", p->migratable);
-}
-
static inline void dump_sess(struct osapsess *s)
{
print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
@@ -96,10 +85,6 @@ static inline void dump_options(struct trusted_key_options *o)
{
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
-}
-
static inline void dump_sess(struct osapsess *s)
{
}
diff --git a/security/keys/Kconfig b/security/keys/Kconfig
index 47c0415..22632c6 100644
--- a/security/keys/Kconfig
+++ b/security/keys/Kconfig
@@ -72,17 +72,26 @@ config BIG_KEYS
config TRUSTED_KEYS
tristate "TRUSTED KEYS"
- depends on KEYS && TCG_TPM
+ depends on KEYS
+ help
+ This option provides support for creating, sealing, and unsealing
+ keys in the kernel. Trusted keys are random number symmetric keys,
+ generated and sealed by a trust source (TPM or TEE). Userspace will
+ only ever see encrypted blobs.
+
+ If you are unsure as to whether this is required, answer N.
+
+config TRUSTED_TPM
+ bool "TPM based TRUSTED KEYS"
+ depends on TRUSTED_KEYS && TCG_TPM
select CRYPTO
select CRYPTO_HMAC
select CRYPTO_SHA1
select CRYPTO_HASH_INFO
help
- This option provides support for creating, sealing, and unsealing
- keys in the kernel. Trusted keys are random number symmetric keys,
- generated and RSA-sealed by the TPM. The TPM only unseals the keys,
- if the boot PCRs and other criteria match. Userspace will only ever
- see encrypted blobs.
+ This option provides support for TPM based trusted keys where TPM acts
+ as the trust source. The keys are RSA-sealed by the TPM and it only
+ unseals the keys, if the boot PCRs and other criteria match.
If you are unsure as to whether this is required, answer N.
diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
index 7b73ceb..03c3ad4 100644
--- a/security/keys/trusted-keys/Makefile
+++ b/security/keys/trusted-keys/Makefile
@@ -4,5 +4,6 @@
#
obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
-trusted-y += trusted_tpm1.o
-trusted-y += trusted_tpm2.o
+trusted-y += trusted_core.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
new file mode 100644
index 0000000..3d07d27
--- /dev/null
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
+ *
+ * Author:
+ * David Safford <safford@us.ibm.com>
+ * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
+ *
+ * See Documentation/security/keys/trusted-encrypted.rst
+ */
+
+#include <keys/user-type.h>
+#include <keys/trusted-type.h>
+#include <linux/capability.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/key-type.h>
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+
+#if defined(CONFIG_TRUSTED_TPM)
+static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
+#else
+static struct trusted_key_ops *trusted_key_ops;
+#endif
+
+enum {
+ Opt_err,
+ Opt_new, Opt_load, Opt_update,
+};
+
+static const match_table_t key_tokens = {
+ {Opt_new, "new"},
+ {Opt_load, "load"},
+ {Opt_update, "update"},
+ {Opt_err, NULL}
+};
+
+/*
+ * datablob_parse - parse the keyctl data and fill in the
+ * payload structure
+ *
+ * On success returns 0, otherwise -EINVAL.
+ */
+static int datablob_parse(char *datablob, struct trusted_key_payload *p)
+{
+ substring_t args[MAX_OPT_ARGS];
+ long keylen;
+ int ret = -EINVAL;
+ int key_cmd;
+ char *c;
+
+ /* main command */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ key_cmd = match_token(c, key_tokens, args);
+ switch (key_cmd) {
+ case Opt_new:
+ /* first argument is key size */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ ret = kstrtol(c, 10, &keylen);
+ if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
+ return -EINVAL;
+ p->key_len = keylen;
+ ret = Opt_new;
+ break;
+ case Opt_load:
+ /* first argument is sealed blob */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ p->blob_len = strlen(c) / 2;
+ if (p->blob_len > MAX_BLOB_SIZE)
+ return -EINVAL;
+ ret = hex2bin(p->blob, c, p->blob_len);
+ if (ret < 0)
+ return -EINVAL;
+ ret = Opt_load;
+ break;
+ case Opt_update:
+ ret = Opt_update;
+ break;
+ case Opt_err:
+ return -EINVAL;
+ }
+ return ret;
+}
+
+static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+{
+ struct trusted_key_payload *p = NULL;
+ int ret;
+
+ ret = key_payload_reserve(key, sizeof(*p));
+ if (ret < 0)
+ return p;
+ p = kzalloc(sizeof(*p), GFP_KERNEL);
+
+ p->migratable = trusted_key_ops->migratable;
+
+ return p;
+}
+
+/*
+ * trusted_instantiate - create a new trusted key
+ *
+ * Unseal an existing trusted blob or, for a new key, get a
+ * random key, then seal and create a trusted key-type key,
+ * adding it to the specified keyring.
+ *
+ * On success, return 0. Otherwise return errno.
+ */
+static int trusted_instantiate(struct key *key,
+ struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *payload = NULL;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+ int key_cmd;
+ size_t key_len;
+
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+
+ payload = trusted_payload_alloc(key);
+ if (!payload) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ key_cmd = datablob_parse(datablob, payload);
+ if (key_cmd < 0) {
+ ret = key_cmd;
+ goto out;
+ }
+
+ dump_payload(payload);
+
+ switch (key_cmd) {
+ case Opt_load:
+ ret = trusted_key_ops->unseal(payload, datablob);
+ dump_payload(payload);
+ if (ret < 0)
+ pr_info("trusted_key: key_unseal failed (%d)\n", ret);
+ break;
+ case Opt_new:
+ key_len = payload->key_len;
+ ret = trusted_key_ops->get_random(payload->key, key_len);
+ if (ret != key_len) {
+ pr_info("trusted_key: key_create failed (%d)\n", ret);
+ goto out;
+ }
+
+ ret = trusted_key_ops->seal(payload, datablob);
+ if (ret < 0)
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+out:
+ kzfree(datablob);
+ if (!ret)
+ rcu_assign_keypointer(key, payload);
+ else
+ kzfree(payload);
+ return ret;
+}
+
+static void trusted_rcu_free(struct rcu_head *rcu)
+{
+ struct trusted_key_payload *p;
+
+ p = container_of(rcu, struct trusted_key_payload, rcu);
+ kzfree(p);
+}
+
+/*
+ * trusted_update - reseal an existing key with new PCR values
+ */
+static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *p;
+ struct trusted_key_payload *new_p;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+
+ if (key_is_negative(key))
+ return -ENOKEY;
+ p = key->payload.data[0];
+ if (!p->migratable)
+ return -EPERM;
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+
+ new_p = trusted_payload_alloc(key);
+ if (!new_p) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+ ret = datablob_parse(datablob, new_p);
+ if (ret != Opt_update) {
+ ret = -EINVAL;
+ kzfree(new_p);
+ goto out;
+ }
+
+ /* copy old key values, and reseal with new pcrs */
+ new_p->migratable = p->migratable;
+ new_p->key_len = p->key_len;
+ memcpy(new_p->key, p->key, p->key_len);
+ dump_payload(p);
+ dump_payload(new_p);
+
+ ret = trusted_key_ops->seal(new_p, datablob);
+ if (ret < 0) {
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ kzfree(new_p);
+ goto out;
+ }
+
+ rcu_assign_keypointer(key, new_p);
+ call_rcu(&p->rcu, trusted_rcu_free);
+out:
+ kzfree(datablob);
+ return ret;
+}
+
+/*
+ * trusted_read - copy the sealed blob data to userspace in hex.
+ * On success, return to userspace the trusted key datablob size.
+ */
+static long trusted_read(const struct key *key, char *buffer,
+ size_t buflen)
+{
+ const struct trusted_key_payload *p;
+ char *bufp;
+ int i;
+
+ p = dereference_key_locked(key);
+ if (!p)
+ return -EINVAL;
+
+ if (buffer && buflen >= 2 * p->blob_len) {
+ bufp = buffer;
+ for (i = 0; i < p->blob_len; i++)
+ bufp = hex_byte_pack(bufp, p->blob[i]);
+ }
+ return 2 * p->blob_len;
+}
+
+/*
+ * trusted_destroy - clear and free the key's payload
+ */
+static void trusted_destroy(struct key *key)
+{
+ kzfree(key->payload.data[0]);
+}
+
+struct key_type key_type_trusted = {
+ .name = "trusted",
+ .instantiate = trusted_instantiate,
+ .update = trusted_update,
+ .destroy = trusted_destroy,
+ .describe = user_describe,
+ .read = trusted_read,
+};
+EXPORT_SYMBOL_GPL(key_type_trusted);
+
+static int __init init_trusted(void)
+{
+ int ret;
+
+ /*
+ * encrypted_keys.ko depends on successful load of this module even if
+ * trusted key implementation is not found.
+ */
+ if (!trusted_key_ops)
+ return 0;
+
+ ret = trusted_key_ops->init();
+ if (ret = -ENODEV)
+ return 0;
+
+ return ret;
+}
+
+static void __exit cleanup_trusted(void)
+{
+ trusted_key_ops->exit();
+}
+
+late_initcall(init_trusted);
+module_exit(cleanup_trusted);
+
+MODULE_LICENSE("GPL");
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 8001ab0..e378b27 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -1,29 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
*
* Author:
* David Safford <safford@us.ibm.com>
+ * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
*
* See Documentation/security/keys/trusted-encrypted.rst
*/
#include <crypto/hash_info.h>
-#include <linux/uaccess.h>
-#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/parser.h>
#include <linux/string.h>
#include <linux/err.h>
-#include <keys/user-type.h>
#include <keys/trusted-type.h>
#include <linux/key-type.h>
-#include <linux/rcupdate.h>
#include <linux/crypto.h>
#include <crypto/hash.h>
#include <crypto/sha.h>
-#include <linux/capability.h>
#include <linux/tpm.h>
#include <linux/tpm_command.h>
@@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
enum {
Opt_err,
- Opt_new, Opt_load, Opt_update,
Opt_keyhandle, Opt_keyauth, Opt_blobauth,
Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
Opt_hash,
@@ -712,9 +708,6 @@ enum {
};
static const match_table_t key_tokens = {
- {Opt_new, "new"},
- {Opt_load, "load"},
- {Opt_update, "update"},
{Opt_keyhandle, "keyhandle=%s"},
{Opt_keyauth, "keyauth=%s"},
{Opt_blobauth, "blobauth=%s"},
@@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
return 0;
}
-/*
- * datablob_parse - parse the keyctl data and fill in the
- * payload and options structures
- *
- * On success returns 0, otherwise -EINVAL.
- */
-static int datablob_parse(char *datablob, struct trusted_key_payload *p,
- struct trusted_key_options *o)
-{
- substring_t args[MAX_OPT_ARGS];
- long keylen;
- int ret = -EINVAL;
- int key_cmd;
- char *c;
-
- /* main command */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- key_cmd = match_token(c, key_tokens, args);
- switch (key_cmd) {
- case Opt_new:
- /* first argument is key size */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- ret = kstrtol(c, 10, &keylen);
- if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
- return -EINVAL;
- p->key_len = keylen;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_new;
- break;
- case Opt_load:
- /* first argument is sealed blob */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- p->blob_len = strlen(c) / 2;
- if (p->blob_len > MAX_BLOB_SIZE)
- return -EINVAL;
- ret = hex2bin(p->blob, c, p->blob_len);
- if (ret < 0)
- return -EINVAL;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_load;
- break;
- case Opt_update:
- /* all arguments are options */
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_update;
- break;
- case Opt_err:
- return -EINVAL;
- break;
- }
- return ret;
-}
-
static struct trusted_key_options *trusted_options_alloc(void)
{
struct trusted_key_options *options;
@@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
return options;
}
-static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p = NULL;
- int ret;
-
- ret = key_payload_reserve(key, sizeof *p);
- if (ret < 0)
- return p;
- p = kzalloc(sizeof *p, GFP_KERNEL);
- if (p)
- p->migratable = 1; /* migratable by default */
- return p;
-}
-
-/*
- * trusted_instantiate - create a new trusted key
- *
- * Unseal an existing trusted blob or, for a new key, get a
- * random key, then seal and create a trusted key-type key,
- * adding it to the specified keyring.
- *
- * On success, return 0. Otherwise return errno.
- */
-static int trusted_instantiate(struct key *key,
- struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *payload = NULL;
struct trusted_key_options *options = NULL;
- size_t datalen = prep->datalen;
- char *datablob;
int ret = 0;
- int key_cmd;
- size_t key_len;
int tpm2;
tpm2 = tpm_is_tpm2(chip);
if (tpm2 < 0)
return tpm2;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
-
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
- return -ENOMEM;
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
-
options = trusted_options_alloc();
- if (!options) {
- ret = -ENOMEM;
- goto out;
- }
- payload = trusted_payload_alloc(key);
- if (!payload) {
- ret = -ENOMEM;
- goto out;
- }
+ if (!options)
+ return -ENOMEM;
- key_cmd = datablob_parse(datablob, payload, options);
- if (key_cmd < 0) {
- ret = key_cmd;
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
if (!options->keyhandle) {
ret = -EINVAL;
goto out;
}
- dump_payload(payload);
- dump_options(options);
+ if (tpm2)
+ ret = tpm2_seal_trusted(chip, p, options);
+ else
+ ret = key_seal(p, options);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
+ goto out;
+ }
- switch (key_cmd) {
- case Opt_load:
- if (tpm2)
- ret = tpm2_unseal_trusted(chip, payload, options);
- else
- ret = key_unseal(payload, options);
- dump_payload(payload);
- dump_options(options);
- if (ret < 0)
- pr_info("trusted_key: key_unseal failed (%d)\n", ret);
- break;
- case Opt_new:
- key_len = payload->key_len;
- ret = tpm_get_random(chip, payload->key, key_len);
- if (ret != key_len) {
- pr_info("trusted_key: key_create failed (%d)\n", ret);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
- if (tpm2)
- ret = tpm2_seal_trusted(chip, payload, options);
- else
- ret = key_seal(payload, options);
- if (ret < 0)
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- break;
- default:
- ret = -EINVAL;
- goto out;
}
- if (!ret && options->pcrlock)
- ret = pcrlock(options->pcrlock);
out:
- kzfree(datablob);
kzfree(options);
- if (!ret)
- rcu_assign_keypointer(key, payload);
- else
- kzfree(payload);
return ret;
}
-static void trusted_rcu_free(struct rcu_head *rcu)
+static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p;
-
- p = container_of(rcu, struct trusted_key_payload, rcu);
- kzfree(p);
-}
-
-/*
- * trusted_update - reseal an existing key with new PCR values
- */
-static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *p;
- struct trusted_key_payload *new_p;
- struct trusted_key_options *new_o;
- size_t datalen = prep->datalen;
- char *datablob;
+ struct trusted_key_options *options = NULL;
int ret = 0;
+ int tpm2;
- if (key_is_negative(key))
- return -ENOKEY;
- p = key->payload.data[0];
- if (!p->migratable)
- return -EPERM;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
+ tpm2 = tpm_is_tpm2(chip);
+ if (tpm2 < 0)
+ return tpm2;
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
+ options = trusted_options_alloc();
+ if (!options)
return -ENOMEM;
- new_o = trusted_options_alloc();
- if (!new_o) {
- ret = -ENOMEM;
- goto out;
- }
- new_p = trusted_payload_alloc(key);
- if (!new_p) {
- ret = -ENOMEM;
- goto out;
- }
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
- ret = datablob_parse(datablob, new_p, new_o);
- if (ret != Opt_update) {
- ret = -EINVAL;
- kzfree(new_p);
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
- if (!new_o->keyhandle) {
+ if (!options->keyhandle) {
ret = -EINVAL;
- kzfree(new_p);
goto out;
}
- /* copy old key values, and reseal with new pcrs */
- new_p->migratable = p->migratable;
- new_p->key_len = p->key_len;
- memcpy(new_p->key, p->key, p->key_len);
- dump_payload(p);
- dump_payload(new_p);
+ if (tpm2)
+ ret = tpm2_unseal_trusted(chip, p, options);
+ else
+ ret = key_unseal(p, options);
+ if (ret < 0)
+ pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
- ret = key_seal(new_p, new_o);
- if (ret < 0) {
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- kzfree(new_p);
- goto out;
- }
- if (new_o->pcrlock) {
- ret = pcrlock(new_o->pcrlock);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
if (ret < 0) {
- pr_info("trusted_key: pcrlock failed (%d)\n", ret);
- kzfree(new_p);
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
}
- rcu_assign_keypointer(key, new_p);
- call_rcu(&p->rcu, trusted_rcu_free);
out:
- kzfree(datablob);
- kzfree(new_o);
+ kzfree(options);
return ret;
}
-/*
- * trusted_read - copy the sealed blob data to userspace in hex.
- * On success, return to userspace the trusted key datablob size.
- */
-static long trusted_read(const struct key *key, char *buffer,
- size_t buflen)
-{
- const struct trusted_key_payload *p;
- char *bufp;
- int i;
-
- p = dereference_key_locked(key);
- if (!p)
- return -EINVAL;
-
- if (buffer && buflen >= 2 * p->blob_len) {
- bufp = buffer;
- for (i = 0; i < p->blob_len; i++)
- bufp = hex_byte_pack(bufp, p->blob[i]);
- }
- return 2 * p->blob_len;
-}
-
-/*
- * trusted_destroy - clear and free the key's payload
- */
-static void trusted_destroy(struct key *key)
+int tpm_trusted_get_random(unsigned char *key, size_t key_len)
{
- kzfree(key->payload.data[0]);
+ return tpm_get_random(chip, key, key_len);
}
-struct key_type key_type_trusted = {
- .name = "trusted",
- .instantiate = trusted_instantiate,
- .update = trusted_update,
- .destroy = trusted_destroy,
- .describe = user_describe,
- .read = trusted_read,
-};
-
-EXPORT_SYMBOL_GPL(key_type_trusted);
-
static void trusted_shash_release(void)
{
if (hashalg)
@@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
if (IS_ERR(hmacalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hmac_alg);
return PTR_ERR(hmacalg);
}
hashalg = crypto_alloc_shash(hash_alg, 0, 0);
if (IS_ERR(hashalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hash_alg);
ret = PTR_ERR(hashalg);
goto hashalg_fail;
@@ -1217,16 +996,13 @@ static int __init init_digests(void)
return 0;
}
-static int __init init_trusted(void)
+static int __init init_tpm_trusted(void)
{
int ret;
- /* encrypted_keys.ko depends on successful load of this module even if
- * TPM is not used.
- */
chip = tpm_default_chip();
if (!chip)
- return 0;
+ return -ENODEV;
ret = init_digests();
if (ret < 0)
@@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
return ret;
}
-static void __exit cleanup_trusted(void)
+static void __exit exit_tpm_trusted(void)
{
if (chip) {
put_device(&chip->dev);
@@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
}
}
-late_initcall(init_trusted);
-module_exit(cleanup_trusted);
-
-MODULE_LICENSE("GPL");
+struct trusted_key_ops tpm_trusted_key_ops = {
+ .migratable = 1, /* migratable by default */
+ .init = init_tpm_trusted,
+ .seal = tpm_trusted_seal,
+ .unseal = tpm_trusted_unseal,
+ .get_random = tpm_trusted_get_random,
+ .exit = exit_tpm_trusted,
+};
+EXPORT_SYMBOL_GPL(tpm_trusted_key_ops);
--
2.7.4
^ permalink raw reply related [flat|nested] 56+ messages in thread* [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-02 14:18 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-02 14:18 UTC (permalink / raw)
To: jarkko.sakkinen, zohar, jejb
Cc: tee-dev, daniel.thompson, Sumit Garg, op-tee, corbet,
janne.karhunen, linux-doc, jmorris, linux-kernel, dhowells,
linux-security-module, keyrings, Markus.Wamser, casey,
linux-integrity, jens.wiklander, linux-arm-kernel, serge
Current trusted keys framework is tightly coupled to use TPM device as
an underlying implementation which makes it difficult for implementations
like Trusted Execution Environment (TEE) etc. to provide trusted keys
support in case platform doesn't posses a TPM device.
So this patch tries to add generic trusted keys framework where underlying
implementations like TPM, TEE etc. could be easily plugged-in.
Suggested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
---
include/keys/trusted-type.h | 45 ++++
include/keys/trusted_tpm.h | 15 --
security/keys/Kconfig | 21 +-
security/keys/trusted-keys/Makefile | 5 +-
security/keys/trusted-keys/trusted_core.c | 319 ++++++++++++++++++++++++++++
security/keys/trusted-keys/trusted_tpm1.c | 335 ++++++------------------------
6 files changed, 440 insertions(+), 300 deletions(-)
create mode 100644 security/keys/trusted-keys/trusted_core.c
diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index a94c03a..819a102 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -40,6 +40,51 @@ struct trusted_key_options {
uint32_t policyhandle;
};
+struct trusted_key_ops {
+ /*
+ * flag to indicate if trusted key implementation supports migration
+ * or not.
+ */
+ unsigned char migratable;
+
+ /* Initialize key interface. */
+ int (*init)(void);
+
+ /* Seal a key. */
+ int (*seal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Unseal a key. */
+ int (*unseal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Get a randomized key. */
+ int (*get_random)(unsigned char *key, size_t key_len);
+
+ /* Exit key interface. */
+ void (*exit)(void);
+};
+
extern struct key_type key_type_trusted;
+#if defined(CONFIG_TRUSTED_TPM)
+extern struct trusted_key_ops tpm_trusted_key_ops;
+#endif
+
+#define TRUSTED_DEBUG 0
+
+#if TRUSTED_DEBUG
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+ pr_info("trusted_key: key_len %d\n", p->key_len);
+ print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
+ 16, 1, p->key, p->key_len, 0);
+ pr_info("trusted_key: bloblen %d\n", p->blob_len);
+ print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
+ 16, 1, p->blob, p->blob_len, 0);
+ pr_info("trusted_key: migratable %d\n", p->migratable);
+}
+#else
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+}
+#endif
#endif /* _KEYS_TRUSTED_TYPE_H */
diff --git a/include/keys/trusted_tpm.h b/include/keys/trusted_tpm.h
index a56d8e1..5753231 100644
--- a/include/keys/trusted_tpm.h
+++ b/include/keys/trusted_tpm.h
@@ -60,17 +60,6 @@ static inline void dump_options(struct trusted_key_options *o)
16, 1, o->pcrinfo, o->pcrinfo_len, 0);
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
- pr_info("trusted_key: key_len %d\n", p->key_len);
- print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
- 16, 1, p->key, p->key_len, 0);
- pr_info("trusted_key: bloblen %d\n", p->blob_len);
- print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
- 16, 1, p->blob, p->blob_len, 0);
- pr_info("trusted_key: migratable %d\n", p->migratable);
-}
-
static inline void dump_sess(struct osapsess *s)
{
print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
@@ -96,10 +85,6 @@ static inline void dump_options(struct trusted_key_options *o)
{
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
-}
-
static inline void dump_sess(struct osapsess *s)
{
}
diff --git a/security/keys/Kconfig b/security/keys/Kconfig
index 47c0415..22632c6 100644
--- a/security/keys/Kconfig
+++ b/security/keys/Kconfig
@@ -72,17 +72,26 @@ config BIG_KEYS
config TRUSTED_KEYS
tristate "TRUSTED KEYS"
- depends on KEYS && TCG_TPM
+ depends on KEYS
+ help
+ This option provides support for creating, sealing, and unsealing
+ keys in the kernel. Trusted keys are random number symmetric keys,
+ generated and sealed by a trust source (TPM or TEE). Userspace will
+ only ever see encrypted blobs.
+
+ If you are unsure as to whether this is required, answer N.
+
+config TRUSTED_TPM
+ bool "TPM based TRUSTED KEYS"
+ depends on TRUSTED_KEYS && TCG_TPM
select CRYPTO
select CRYPTO_HMAC
select CRYPTO_SHA1
select CRYPTO_HASH_INFO
help
- This option provides support for creating, sealing, and unsealing
- keys in the kernel. Trusted keys are random number symmetric keys,
- generated and RSA-sealed by the TPM. The TPM only unseals the keys,
- if the boot PCRs and other criteria match. Userspace will only ever
- see encrypted blobs.
+ This option provides support for TPM based trusted keys where TPM acts
+ as the trust source. The keys are RSA-sealed by the TPM and it only
+ unseals the keys, if the boot PCRs and other criteria match.
If you are unsure as to whether this is required, answer N.
diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
index 7b73ceb..03c3ad4 100644
--- a/security/keys/trusted-keys/Makefile
+++ b/security/keys/trusted-keys/Makefile
@@ -4,5 +4,6 @@
#
obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
-trusted-y += trusted_tpm1.o
-trusted-y += trusted_tpm2.o
+trusted-y += trusted_core.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
new file mode 100644
index 0000000..3d07d27
--- /dev/null
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
+ *
+ * Author:
+ * David Safford <safford@us.ibm.com>
+ * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
+ *
+ * See Documentation/security/keys/trusted-encrypted.rst
+ */
+
+#include <keys/user-type.h>
+#include <keys/trusted-type.h>
+#include <linux/capability.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/key-type.h>
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+
+#if defined(CONFIG_TRUSTED_TPM)
+static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
+#else
+static struct trusted_key_ops *trusted_key_ops;
+#endif
+
+enum {
+ Opt_err,
+ Opt_new, Opt_load, Opt_update,
+};
+
+static const match_table_t key_tokens = {
+ {Opt_new, "new"},
+ {Opt_load, "load"},
+ {Opt_update, "update"},
+ {Opt_err, NULL}
+};
+
+/*
+ * datablob_parse - parse the keyctl data and fill in the
+ * payload structure
+ *
+ * On success returns 0, otherwise -EINVAL.
+ */
+static int datablob_parse(char *datablob, struct trusted_key_payload *p)
+{
+ substring_t args[MAX_OPT_ARGS];
+ long keylen;
+ int ret = -EINVAL;
+ int key_cmd;
+ char *c;
+
+ /* main command */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ key_cmd = match_token(c, key_tokens, args);
+ switch (key_cmd) {
+ case Opt_new:
+ /* first argument is key size */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ ret = kstrtol(c, 10, &keylen);
+ if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
+ return -EINVAL;
+ p->key_len = keylen;
+ ret = Opt_new;
+ break;
+ case Opt_load:
+ /* first argument is sealed blob */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ p->blob_len = strlen(c) / 2;
+ if (p->blob_len > MAX_BLOB_SIZE)
+ return -EINVAL;
+ ret = hex2bin(p->blob, c, p->blob_len);
+ if (ret < 0)
+ return -EINVAL;
+ ret = Opt_load;
+ break;
+ case Opt_update:
+ ret = Opt_update;
+ break;
+ case Opt_err:
+ return -EINVAL;
+ }
+ return ret;
+}
+
+static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+{
+ struct trusted_key_payload *p = NULL;
+ int ret;
+
+ ret = key_payload_reserve(key, sizeof(*p));
+ if (ret < 0)
+ return p;
+ p = kzalloc(sizeof(*p), GFP_KERNEL);
+
+ p->migratable = trusted_key_ops->migratable;
+
+ return p;
+}
+
+/*
+ * trusted_instantiate - create a new trusted key
+ *
+ * Unseal an existing trusted blob or, for a new key, get a
+ * random key, then seal and create a trusted key-type key,
+ * adding it to the specified keyring.
+ *
+ * On success, return 0. Otherwise return errno.
+ */
+static int trusted_instantiate(struct key *key,
+ struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *payload = NULL;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+ int key_cmd;
+ size_t key_len;
+
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+
+ payload = trusted_payload_alloc(key);
+ if (!payload) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ key_cmd = datablob_parse(datablob, payload);
+ if (key_cmd < 0) {
+ ret = key_cmd;
+ goto out;
+ }
+
+ dump_payload(payload);
+
+ switch (key_cmd) {
+ case Opt_load:
+ ret = trusted_key_ops->unseal(payload, datablob);
+ dump_payload(payload);
+ if (ret < 0)
+ pr_info("trusted_key: key_unseal failed (%d)\n", ret);
+ break;
+ case Opt_new:
+ key_len = payload->key_len;
+ ret = trusted_key_ops->get_random(payload->key, key_len);
+ if (ret != key_len) {
+ pr_info("trusted_key: key_create failed (%d)\n", ret);
+ goto out;
+ }
+
+ ret = trusted_key_ops->seal(payload, datablob);
+ if (ret < 0)
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+out:
+ kzfree(datablob);
+ if (!ret)
+ rcu_assign_keypointer(key, payload);
+ else
+ kzfree(payload);
+ return ret;
+}
+
+static void trusted_rcu_free(struct rcu_head *rcu)
+{
+ struct trusted_key_payload *p;
+
+ p = container_of(rcu, struct trusted_key_payload, rcu);
+ kzfree(p);
+}
+
+/*
+ * trusted_update - reseal an existing key with new PCR values
+ */
+static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *p;
+ struct trusted_key_payload *new_p;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+
+ if (key_is_negative(key))
+ return -ENOKEY;
+ p = key->payload.data[0];
+ if (!p->migratable)
+ return -EPERM;
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+
+ new_p = trusted_payload_alloc(key);
+ if (!new_p) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+ ret = datablob_parse(datablob, new_p);
+ if (ret != Opt_update) {
+ ret = -EINVAL;
+ kzfree(new_p);
+ goto out;
+ }
+
+ /* copy old key values, and reseal with new pcrs */
+ new_p->migratable = p->migratable;
+ new_p->key_len = p->key_len;
+ memcpy(new_p->key, p->key, p->key_len);
+ dump_payload(p);
+ dump_payload(new_p);
+
+ ret = trusted_key_ops->seal(new_p, datablob);
+ if (ret < 0) {
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ kzfree(new_p);
+ goto out;
+ }
+
+ rcu_assign_keypointer(key, new_p);
+ call_rcu(&p->rcu, trusted_rcu_free);
+out:
+ kzfree(datablob);
+ return ret;
+}
+
+/*
+ * trusted_read - copy the sealed blob data to userspace in hex.
+ * On success, return to userspace the trusted key datablob size.
+ */
+static long trusted_read(const struct key *key, char *buffer,
+ size_t buflen)
+{
+ const struct trusted_key_payload *p;
+ char *bufp;
+ int i;
+
+ p = dereference_key_locked(key);
+ if (!p)
+ return -EINVAL;
+
+ if (buffer && buflen >= 2 * p->blob_len) {
+ bufp = buffer;
+ for (i = 0; i < p->blob_len; i++)
+ bufp = hex_byte_pack(bufp, p->blob[i]);
+ }
+ return 2 * p->blob_len;
+}
+
+/*
+ * trusted_destroy - clear and free the key's payload
+ */
+static void trusted_destroy(struct key *key)
+{
+ kzfree(key->payload.data[0]);
+}
+
+struct key_type key_type_trusted = {
+ .name = "trusted",
+ .instantiate = trusted_instantiate,
+ .update = trusted_update,
+ .destroy = trusted_destroy,
+ .describe = user_describe,
+ .read = trusted_read,
+};
+EXPORT_SYMBOL_GPL(key_type_trusted);
+
+static int __init init_trusted(void)
+{
+ int ret;
+
+ /*
+ * encrypted_keys.ko depends on successful load of this module even if
+ * trusted key implementation is not found.
+ */
+ if (!trusted_key_ops)
+ return 0;
+
+ ret = trusted_key_ops->init();
+ if (ret == -ENODEV)
+ return 0;
+
+ return ret;
+}
+
+static void __exit cleanup_trusted(void)
+{
+ trusted_key_ops->exit();
+}
+
+late_initcall(init_trusted);
+module_exit(cleanup_trusted);
+
+MODULE_LICENSE("GPL");
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 8001ab0..e378b27 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -1,29 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
*
* Author:
* David Safford <safford@us.ibm.com>
+ * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
*
* See Documentation/security/keys/trusted-encrypted.rst
*/
#include <crypto/hash_info.h>
-#include <linux/uaccess.h>
-#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/parser.h>
#include <linux/string.h>
#include <linux/err.h>
-#include <keys/user-type.h>
#include <keys/trusted-type.h>
#include <linux/key-type.h>
-#include <linux/rcupdate.h>
#include <linux/crypto.h>
#include <crypto/hash.h>
#include <crypto/sha.h>
-#include <linux/capability.h>
#include <linux/tpm.h>
#include <linux/tpm_command.h>
@@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
enum {
Opt_err,
- Opt_new, Opt_load, Opt_update,
Opt_keyhandle, Opt_keyauth, Opt_blobauth,
Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
Opt_hash,
@@ -712,9 +708,6 @@ enum {
};
static const match_table_t key_tokens = {
- {Opt_new, "new"},
- {Opt_load, "load"},
- {Opt_update, "update"},
{Opt_keyhandle, "keyhandle=%s"},
{Opt_keyauth, "keyauth=%s"},
{Opt_blobauth, "blobauth=%s"},
@@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
return 0;
}
-/*
- * datablob_parse - parse the keyctl data and fill in the
- * payload and options structures
- *
- * On success returns 0, otherwise -EINVAL.
- */
-static int datablob_parse(char *datablob, struct trusted_key_payload *p,
- struct trusted_key_options *o)
-{
- substring_t args[MAX_OPT_ARGS];
- long keylen;
- int ret = -EINVAL;
- int key_cmd;
- char *c;
-
- /* main command */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- key_cmd = match_token(c, key_tokens, args);
- switch (key_cmd) {
- case Opt_new:
- /* first argument is key size */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- ret = kstrtol(c, 10, &keylen);
- if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
- return -EINVAL;
- p->key_len = keylen;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_new;
- break;
- case Opt_load:
- /* first argument is sealed blob */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- p->blob_len = strlen(c) / 2;
- if (p->blob_len > MAX_BLOB_SIZE)
- return -EINVAL;
- ret = hex2bin(p->blob, c, p->blob_len);
- if (ret < 0)
- return -EINVAL;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_load;
- break;
- case Opt_update:
- /* all arguments are options */
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_update;
- break;
- case Opt_err:
- return -EINVAL;
- break;
- }
- return ret;
-}
-
static struct trusted_key_options *trusted_options_alloc(void)
{
struct trusted_key_options *options;
@@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
return options;
}
-static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p = NULL;
- int ret;
-
- ret = key_payload_reserve(key, sizeof *p);
- if (ret < 0)
- return p;
- p = kzalloc(sizeof *p, GFP_KERNEL);
- if (p)
- p->migratable = 1; /* migratable by default */
- return p;
-}
-
-/*
- * trusted_instantiate - create a new trusted key
- *
- * Unseal an existing trusted blob or, for a new key, get a
- * random key, then seal and create a trusted key-type key,
- * adding it to the specified keyring.
- *
- * On success, return 0. Otherwise return errno.
- */
-static int trusted_instantiate(struct key *key,
- struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *payload = NULL;
struct trusted_key_options *options = NULL;
- size_t datalen = prep->datalen;
- char *datablob;
int ret = 0;
- int key_cmd;
- size_t key_len;
int tpm2;
tpm2 = tpm_is_tpm2(chip);
if (tpm2 < 0)
return tpm2;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
-
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
- return -ENOMEM;
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
-
options = trusted_options_alloc();
- if (!options) {
- ret = -ENOMEM;
- goto out;
- }
- payload = trusted_payload_alloc(key);
- if (!payload) {
- ret = -ENOMEM;
- goto out;
- }
+ if (!options)
+ return -ENOMEM;
- key_cmd = datablob_parse(datablob, payload, options);
- if (key_cmd < 0) {
- ret = key_cmd;
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
if (!options->keyhandle) {
ret = -EINVAL;
goto out;
}
- dump_payload(payload);
- dump_options(options);
+ if (tpm2)
+ ret = tpm2_seal_trusted(chip, p, options);
+ else
+ ret = key_seal(p, options);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
+ goto out;
+ }
- switch (key_cmd) {
- case Opt_load:
- if (tpm2)
- ret = tpm2_unseal_trusted(chip, payload, options);
- else
- ret = key_unseal(payload, options);
- dump_payload(payload);
- dump_options(options);
- if (ret < 0)
- pr_info("trusted_key: key_unseal failed (%d)\n", ret);
- break;
- case Opt_new:
- key_len = payload->key_len;
- ret = tpm_get_random(chip, payload->key, key_len);
- if (ret != key_len) {
- pr_info("trusted_key: key_create failed (%d)\n", ret);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
- if (tpm2)
- ret = tpm2_seal_trusted(chip, payload, options);
- else
- ret = key_seal(payload, options);
- if (ret < 0)
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- break;
- default:
- ret = -EINVAL;
- goto out;
}
- if (!ret && options->pcrlock)
- ret = pcrlock(options->pcrlock);
out:
- kzfree(datablob);
kzfree(options);
- if (!ret)
- rcu_assign_keypointer(key, payload);
- else
- kzfree(payload);
return ret;
}
-static void trusted_rcu_free(struct rcu_head *rcu)
+static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p;
-
- p = container_of(rcu, struct trusted_key_payload, rcu);
- kzfree(p);
-}
-
-/*
- * trusted_update - reseal an existing key with new PCR values
- */
-static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *p;
- struct trusted_key_payload *new_p;
- struct trusted_key_options *new_o;
- size_t datalen = prep->datalen;
- char *datablob;
+ struct trusted_key_options *options = NULL;
int ret = 0;
+ int tpm2;
- if (key_is_negative(key))
- return -ENOKEY;
- p = key->payload.data[0];
- if (!p->migratable)
- return -EPERM;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
+ tpm2 = tpm_is_tpm2(chip);
+ if (tpm2 < 0)
+ return tpm2;
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
+ options = trusted_options_alloc();
+ if (!options)
return -ENOMEM;
- new_o = trusted_options_alloc();
- if (!new_o) {
- ret = -ENOMEM;
- goto out;
- }
- new_p = trusted_payload_alloc(key);
- if (!new_p) {
- ret = -ENOMEM;
- goto out;
- }
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
- ret = datablob_parse(datablob, new_p, new_o);
- if (ret != Opt_update) {
- ret = -EINVAL;
- kzfree(new_p);
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
- if (!new_o->keyhandle) {
+ if (!options->keyhandle) {
ret = -EINVAL;
- kzfree(new_p);
goto out;
}
- /* copy old key values, and reseal with new pcrs */
- new_p->migratable = p->migratable;
- new_p->key_len = p->key_len;
- memcpy(new_p->key, p->key, p->key_len);
- dump_payload(p);
- dump_payload(new_p);
+ if (tpm2)
+ ret = tpm2_unseal_trusted(chip, p, options);
+ else
+ ret = key_unseal(p, options);
+ if (ret < 0)
+ pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
- ret = key_seal(new_p, new_o);
- if (ret < 0) {
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- kzfree(new_p);
- goto out;
- }
- if (new_o->pcrlock) {
- ret = pcrlock(new_o->pcrlock);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
if (ret < 0) {
- pr_info("trusted_key: pcrlock failed (%d)\n", ret);
- kzfree(new_p);
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
}
- rcu_assign_keypointer(key, new_p);
- call_rcu(&p->rcu, trusted_rcu_free);
out:
- kzfree(datablob);
- kzfree(new_o);
+ kzfree(options);
return ret;
}
-/*
- * trusted_read - copy the sealed blob data to userspace in hex.
- * On success, return to userspace the trusted key datablob size.
- */
-static long trusted_read(const struct key *key, char *buffer,
- size_t buflen)
-{
- const struct trusted_key_payload *p;
- char *bufp;
- int i;
-
- p = dereference_key_locked(key);
- if (!p)
- return -EINVAL;
-
- if (buffer && buflen >= 2 * p->blob_len) {
- bufp = buffer;
- for (i = 0; i < p->blob_len; i++)
- bufp = hex_byte_pack(bufp, p->blob[i]);
- }
- return 2 * p->blob_len;
-}
-
-/*
- * trusted_destroy - clear and free the key's payload
- */
-static void trusted_destroy(struct key *key)
+int tpm_trusted_get_random(unsigned char *key, size_t key_len)
{
- kzfree(key->payload.data[0]);
+ return tpm_get_random(chip, key, key_len);
}
-struct key_type key_type_trusted = {
- .name = "trusted",
- .instantiate = trusted_instantiate,
- .update = trusted_update,
- .destroy = trusted_destroy,
- .describe = user_describe,
- .read = trusted_read,
-};
-
-EXPORT_SYMBOL_GPL(key_type_trusted);
-
static void trusted_shash_release(void)
{
if (hashalg)
@@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
if (IS_ERR(hmacalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hmac_alg);
return PTR_ERR(hmacalg);
}
hashalg = crypto_alloc_shash(hash_alg, 0, 0);
if (IS_ERR(hashalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hash_alg);
ret = PTR_ERR(hashalg);
goto hashalg_fail;
@@ -1217,16 +996,13 @@ static int __init init_digests(void)
return 0;
}
-static int __init init_trusted(void)
+static int __init init_tpm_trusted(void)
{
int ret;
- /* encrypted_keys.ko depends on successful load of this module even if
- * TPM is not used.
- */
chip = tpm_default_chip();
if (!chip)
- return 0;
+ return -ENODEV;
ret = init_digests();
if (ret < 0)
@@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
return ret;
}
-static void __exit cleanup_trusted(void)
+static void __exit exit_tpm_trusted(void)
{
if (chip) {
put_device(&chip->dev);
@@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
}
}
-late_initcall(init_trusted);
-module_exit(cleanup_trusted);
-
-MODULE_LICENSE("GPL");
+struct trusted_key_ops tpm_trusted_key_ops = {
+ .migratable = 1, /* migratable by default */
+ .init = init_tpm_trusted,
+ .seal = tpm_trusted_seal,
+ .unseal = tpm_trusted_unseal,
+ .get_random = tpm_trusted_get_random,
+ .exit = exit_tpm_trusted,
+};
+EXPORT_SYMBOL_GPL(tpm_trusted_key_ops);
--
2.7.4
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 56+ messages in thread* [op-tee] [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-02 14:18 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-02 14:18 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 25460 bytes --]
Current trusted keys framework is tightly coupled to use TPM device as
an underlying implementation which makes it difficult for implementations
like Trusted Execution Environment (TEE) etc. to provide trusted keys
support in case platform doesn't posses a TPM device.
So this patch tries to add generic trusted keys framework where underlying
implementations like TPM, TEE etc. could be easily plugged-in.
Suggested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
---
include/keys/trusted-type.h | 45 ++++
include/keys/trusted_tpm.h | 15 --
security/keys/Kconfig | 21 +-
security/keys/trusted-keys/Makefile | 5 +-
security/keys/trusted-keys/trusted_core.c | 319 ++++++++++++++++++++++++++++
security/keys/trusted-keys/trusted_tpm1.c | 335 ++++++------------------------
6 files changed, 440 insertions(+), 300 deletions(-)
create mode 100644 security/keys/trusted-keys/trusted_core.c
diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index a94c03a..819a102 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -40,6 +40,51 @@ struct trusted_key_options {
uint32_t policyhandle;
};
+struct trusted_key_ops {
+ /*
+ * flag to indicate if trusted key implementation supports migration
+ * or not.
+ */
+ unsigned char migratable;
+
+ /* Initialize key interface. */
+ int (*init)(void);
+
+ /* Seal a key. */
+ int (*seal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Unseal a key. */
+ int (*unseal)(struct trusted_key_payload *p, char *datablob);
+
+ /* Get a randomized key. */
+ int (*get_random)(unsigned char *key, size_t key_len);
+
+ /* Exit key interface. */
+ void (*exit)(void);
+};
+
extern struct key_type key_type_trusted;
+#if defined(CONFIG_TRUSTED_TPM)
+extern struct trusted_key_ops tpm_trusted_key_ops;
+#endif
+
+#define TRUSTED_DEBUG 0
+
+#if TRUSTED_DEBUG
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+ pr_info("trusted_key: key_len %d\n", p->key_len);
+ print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
+ 16, 1, p->key, p->key_len, 0);
+ pr_info("trusted_key: bloblen %d\n", p->blob_len);
+ print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
+ 16, 1, p->blob, p->blob_len, 0);
+ pr_info("trusted_key: migratable %d\n", p->migratable);
+}
+#else
+static inline void dump_payload(struct trusted_key_payload *p)
+{
+}
+#endif
#endif /* _KEYS_TRUSTED_TYPE_H */
diff --git a/include/keys/trusted_tpm.h b/include/keys/trusted_tpm.h
index a56d8e1..5753231 100644
--- a/include/keys/trusted_tpm.h
+++ b/include/keys/trusted_tpm.h
@@ -60,17 +60,6 @@ static inline void dump_options(struct trusted_key_options *o)
16, 1, o->pcrinfo, o->pcrinfo_len, 0);
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
- pr_info("trusted_key: key_len %d\n", p->key_len);
- print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
- 16, 1, p->key, p->key_len, 0);
- pr_info("trusted_key: bloblen %d\n", p->blob_len);
- print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
- 16, 1, p->blob, p->blob_len, 0);
- pr_info("trusted_key: migratable %d\n", p->migratable);
-}
-
static inline void dump_sess(struct osapsess *s)
{
print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
@@ -96,10 +85,6 @@ static inline void dump_options(struct trusted_key_options *o)
{
}
-static inline void dump_payload(struct trusted_key_payload *p)
-{
-}
-
static inline void dump_sess(struct osapsess *s)
{
}
diff --git a/security/keys/Kconfig b/security/keys/Kconfig
index 47c0415..22632c6 100644
--- a/security/keys/Kconfig
+++ b/security/keys/Kconfig
@@ -72,17 +72,26 @@ config BIG_KEYS
config TRUSTED_KEYS
tristate "TRUSTED KEYS"
- depends on KEYS && TCG_TPM
+ depends on KEYS
+ help
+ This option provides support for creating, sealing, and unsealing
+ keys in the kernel. Trusted keys are random number symmetric keys,
+ generated and sealed by a trust source (TPM or TEE). Userspace will
+ only ever see encrypted blobs.
+
+ If you are unsure as to whether this is required, answer N.
+
+config TRUSTED_TPM
+ bool "TPM based TRUSTED KEYS"
+ depends on TRUSTED_KEYS && TCG_TPM
select CRYPTO
select CRYPTO_HMAC
select CRYPTO_SHA1
select CRYPTO_HASH_INFO
help
- This option provides support for creating, sealing, and unsealing
- keys in the kernel. Trusted keys are random number symmetric keys,
- generated and RSA-sealed by the TPM. The TPM only unseals the keys,
- if the boot PCRs and other criteria match. Userspace will only ever
- see encrypted blobs.
+ This option provides support for TPM based trusted keys where TPM acts
+ as the trust source. The keys are RSA-sealed by the TPM and it only
+ unseals the keys, if the boot PCRs and other criteria match.
If you are unsure as to whether this is required, answer N.
diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
index 7b73ceb..03c3ad4 100644
--- a/security/keys/trusted-keys/Makefile
+++ b/security/keys/trusted-keys/Makefile
@@ -4,5 +4,6 @@
#
obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
-trusted-y += trusted_tpm1.o
-trusted-y += trusted_tpm2.o
+trusted-y += trusted_core.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
+trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
new file mode 100644
index 0000000..3d07d27
--- /dev/null
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -0,0 +1,319 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
+ *
+ * Author:
+ * David Safford <safford@us.ibm.com>
+ * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
+ *
+ * See Documentation/security/keys/trusted-encrypted.rst
+ */
+
+#include <keys/user-type.h>
+#include <keys/trusted-type.h>
+#include <linux/capability.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/key-type.h>
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+
+#if defined(CONFIG_TRUSTED_TPM)
+static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
+#else
+static struct trusted_key_ops *trusted_key_ops;
+#endif
+
+enum {
+ Opt_err,
+ Opt_new, Opt_load, Opt_update,
+};
+
+static const match_table_t key_tokens = {
+ {Opt_new, "new"},
+ {Opt_load, "load"},
+ {Opt_update, "update"},
+ {Opt_err, NULL}
+};
+
+/*
+ * datablob_parse - parse the keyctl data and fill in the
+ * payload structure
+ *
+ * On success returns 0, otherwise -EINVAL.
+ */
+static int datablob_parse(char *datablob, struct trusted_key_payload *p)
+{
+ substring_t args[MAX_OPT_ARGS];
+ long keylen;
+ int ret = -EINVAL;
+ int key_cmd;
+ char *c;
+
+ /* main command */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ key_cmd = match_token(c, key_tokens, args);
+ switch (key_cmd) {
+ case Opt_new:
+ /* first argument is key size */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ ret = kstrtol(c, 10, &keylen);
+ if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
+ return -EINVAL;
+ p->key_len = keylen;
+ ret = Opt_new;
+ break;
+ case Opt_load:
+ /* first argument is sealed blob */
+ c = strsep(&datablob, " \t");
+ if (!c)
+ return -EINVAL;
+ p->blob_len = strlen(c) / 2;
+ if (p->blob_len > MAX_BLOB_SIZE)
+ return -EINVAL;
+ ret = hex2bin(p->blob, c, p->blob_len);
+ if (ret < 0)
+ return -EINVAL;
+ ret = Opt_load;
+ break;
+ case Opt_update:
+ ret = Opt_update;
+ break;
+ case Opt_err:
+ return -EINVAL;
+ }
+ return ret;
+}
+
+static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+{
+ struct trusted_key_payload *p = NULL;
+ int ret;
+
+ ret = key_payload_reserve(key, sizeof(*p));
+ if (ret < 0)
+ return p;
+ p = kzalloc(sizeof(*p), GFP_KERNEL);
+
+ p->migratable = trusted_key_ops->migratable;
+
+ return p;
+}
+
+/*
+ * trusted_instantiate - create a new trusted key
+ *
+ * Unseal an existing trusted blob or, for a new key, get a
+ * random key, then seal and create a trusted key-type key,
+ * adding it to the specified keyring.
+ *
+ * On success, return 0. Otherwise return errno.
+ */
+static int trusted_instantiate(struct key *key,
+ struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *payload = NULL;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+ int key_cmd;
+ size_t key_len;
+
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+
+ payload = trusted_payload_alloc(key);
+ if (!payload) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ key_cmd = datablob_parse(datablob, payload);
+ if (key_cmd < 0) {
+ ret = key_cmd;
+ goto out;
+ }
+
+ dump_payload(payload);
+
+ switch (key_cmd) {
+ case Opt_load:
+ ret = trusted_key_ops->unseal(payload, datablob);
+ dump_payload(payload);
+ if (ret < 0)
+ pr_info("trusted_key: key_unseal failed (%d)\n", ret);
+ break;
+ case Opt_new:
+ key_len = payload->key_len;
+ ret = trusted_key_ops->get_random(payload->key, key_len);
+ if (ret != key_len) {
+ pr_info("trusted_key: key_create failed (%d)\n", ret);
+ goto out;
+ }
+
+ ret = trusted_key_ops->seal(payload, datablob);
+ if (ret < 0)
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+out:
+ kzfree(datablob);
+ if (!ret)
+ rcu_assign_keypointer(key, payload);
+ else
+ kzfree(payload);
+ return ret;
+}
+
+static void trusted_rcu_free(struct rcu_head *rcu)
+{
+ struct trusted_key_payload *p;
+
+ p = container_of(rcu, struct trusted_key_payload, rcu);
+ kzfree(p);
+}
+
+/*
+ * trusted_update - reseal an existing key with new PCR values
+ */
+static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
+{
+ struct trusted_key_payload *p;
+ struct trusted_key_payload *new_p;
+ size_t datalen = prep->datalen;
+ char *datablob;
+ int ret = 0;
+
+ if (key_is_negative(key))
+ return -ENOKEY;
+ p = key->payload.data[0];
+ if (!p->migratable)
+ return -EPERM;
+ if (datalen <= 0 || datalen > 32767 || !prep->data)
+ return -EINVAL;
+
+ datablob = kmalloc(datalen + 1, GFP_KERNEL);
+ if (!datablob)
+ return -ENOMEM;
+
+ new_p = trusted_payload_alloc(key);
+ if (!new_p) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ memcpy(datablob, prep->data, datalen);
+ datablob[datalen] = '\0';
+ ret = datablob_parse(datablob, new_p);
+ if (ret != Opt_update) {
+ ret = -EINVAL;
+ kzfree(new_p);
+ goto out;
+ }
+
+ /* copy old key values, and reseal with new pcrs */
+ new_p->migratable = p->migratable;
+ new_p->key_len = p->key_len;
+ memcpy(new_p->key, p->key, p->key_len);
+ dump_payload(p);
+ dump_payload(new_p);
+
+ ret = trusted_key_ops->seal(new_p, datablob);
+ if (ret < 0) {
+ pr_info("trusted_key: key_seal failed (%d)\n", ret);
+ kzfree(new_p);
+ goto out;
+ }
+
+ rcu_assign_keypointer(key, new_p);
+ call_rcu(&p->rcu, trusted_rcu_free);
+out:
+ kzfree(datablob);
+ return ret;
+}
+
+/*
+ * trusted_read - copy the sealed blob data to userspace in hex.
+ * On success, return to userspace the trusted key datablob size.
+ */
+static long trusted_read(const struct key *key, char *buffer,
+ size_t buflen)
+{
+ const struct trusted_key_payload *p;
+ char *bufp;
+ int i;
+
+ p = dereference_key_locked(key);
+ if (!p)
+ return -EINVAL;
+
+ if (buffer && buflen >= 2 * p->blob_len) {
+ bufp = buffer;
+ for (i = 0; i < p->blob_len; i++)
+ bufp = hex_byte_pack(bufp, p->blob[i]);
+ }
+ return 2 * p->blob_len;
+}
+
+/*
+ * trusted_destroy - clear and free the key's payload
+ */
+static void trusted_destroy(struct key *key)
+{
+ kzfree(key->payload.data[0]);
+}
+
+struct key_type key_type_trusted = {
+ .name = "trusted",
+ .instantiate = trusted_instantiate,
+ .update = trusted_update,
+ .destroy = trusted_destroy,
+ .describe = user_describe,
+ .read = trusted_read,
+};
+EXPORT_SYMBOL_GPL(key_type_trusted);
+
+static int __init init_trusted(void)
+{
+ int ret;
+
+ /*
+ * encrypted_keys.ko depends on successful load of this module even if
+ * trusted key implementation is not found.
+ */
+ if (!trusted_key_ops)
+ return 0;
+
+ ret = trusted_key_ops->init();
+ if (ret == -ENODEV)
+ return 0;
+
+ return ret;
+}
+
+static void __exit cleanup_trusted(void)
+{
+ trusted_key_ops->exit();
+}
+
+late_initcall(init_trusted);
+module_exit(cleanup_trusted);
+
+MODULE_LICENSE("GPL");
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 8001ab0..e378b27 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -1,29 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2010 IBM Corporation
+ * Copyright (c) 2019, Linaro Limited
*
* Author:
* David Safford <safford@us.ibm.com>
+ * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
*
* See Documentation/security/keys/trusted-encrypted.rst
*/
#include <crypto/hash_info.h>
-#include <linux/uaccess.h>
-#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/parser.h>
#include <linux/string.h>
#include <linux/err.h>
-#include <keys/user-type.h>
#include <keys/trusted-type.h>
#include <linux/key-type.h>
-#include <linux/rcupdate.h>
#include <linux/crypto.h>
#include <crypto/hash.h>
#include <crypto/sha.h>
-#include <linux/capability.h>
#include <linux/tpm.h>
#include <linux/tpm_command.h>
@@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
enum {
Opt_err,
- Opt_new, Opt_load, Opt_update,
Opt_keyhandle, Opt_keyauth, Opt_blobauth,
Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
Opt_hash,
@@ -712,9 +708,6 @@ enum {
};
static const match_table_t key_tokens = {
- {Opt_new, "new"},
- {Opt_load, "load"},
- {Opt_update, "update"},
{Opt_keyhandle, "keyhandle=%s"},
{Opt_keyauth, "keyauth=%s"},
{Opt_blobauth, "blobauth=%s"},
@@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
return 0;
}
-/*
- * datablob_parse - parse the keyctl data and fill in the
- * payload and options structures
- *
- * On success returns 0, otherwise -EINVAL.
- */
-static int datablob_parse(char *datablob, struct trusted_key_payload *p,
- struct trusted_key_options *o)
-{
- substring_t args[MAX_OPT_ARGS];
- long keylen;
- int ret = -EINVAL;
- int key_cmd;
- char *c;
-
- /* main command */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- key_cmd = match_token(c, key_tokens, args);
- switch (key_cmd) {
- case Opt_new:
- /* first argument is key size */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- ret = kstrtol(c, 10, &keylen);
- if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
- return -EINVAL;
- p->key_len = keylen;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_new;
- break;
- case Opt_load:
- /* first argument is sealed blob */
- c = strsep(&datablob, " \t");
- if (!c)
- return -EINVAL;
- p->blob_len = strlen(c) / 2;
- if (p->blob_len > MAX_BLOB_SIZE)
- return -EINVAL;
- ret = hex2bin(p->blob, c, p->blob_len);
- if (ret < 0)
- return -EINVAL;
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_load;
- break;
- case Opt_update:
- /* all arguments are options */
- ret = getoptions(datablob, p, o);
- if (ret < 0)
- return ret;
- ret = Opt_update;
- break;
- case Opt_err:
- return -EINVAL;
- break;
- }
- return ret;
-}
-
static struct trusted_key_options *trusted_options_alloc(void)
{
struct trusted_key_options *options;
@@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
return options;
}
-static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
+static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p = NULL;
- int ret;
-
- ret = key_payload_reserve(key, sizeof *p);
- if (ret < 0)
- return p;
- p = kzalloc(sizeof *p, GFP_KERNEL);
- if (p)
- p->migratable = 1; /* migratable by default */
- return p;
-}
-
-/*
- * trusted_instantiate - create a new trusted key
- *
- * Unseal an existing trusted blob or, for a new key, get a
- * random key, then seal and create a trusted key-type key,
- * adding it to the specified keyring.
- *
- * On success, return 0. Otherwise return errno.
- */
-static int trusted_instantiate(struct key *key,
- struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *payload = NULL;
struct trusted_key_options *options = NULL;
- size_t datalen = prep->datalen;
- char *datablob;
int ret = 0;
- int key_cmd;
- size_t key_len;
int tpm2;
tpm2 = tpm_is_tpm2(chip);
if (tpm2 < 0)
return tpm2;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
-
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
- return -ENOMEM;
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
-
options = trusted_options_alloc();
- if (!options) {
- ret = -ENOMEM;
- goto out;
- }
- payload = trusted_payload_alloc(key);
- if (!payload) {
- ret = -ENOMEM;
- goto out;
- }
+ if (!options)
+ return -ENOMEM;
- key_cmd = datablob_parse(datablob, payload, options);
- if (key_cmd < 0) {
- ret = key_cmd;
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
if (!options->keyhandle) {
ret = -EINVAL;
goto out;
}
- dump_payload(payload);
- dump_options(options);
+ if (tpm2)
+ ret = tpm2_seal_trusted(chip, p, options);
+ else
+ ret = key_seal(p, options);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
+ goto out;
+ }
- switch (key_cmd) {
- case Opt_load:
- if (tpm2)
- ret = tpm2_unseal_trusted(chip, payload, options);
- else
- ret = key_unseal(payload, options);
- dump_payload(payload);
- dump_options(options);
- if (ret < 0)
- pr_info("trusted_key: key_unseal failed (%d)\n", ret);
- break;
- case Opt_new:
- key_len = payload->key_len;
- ret = tpm_get_random(chip, payload->key, key_len);
- if (ret != key_len) {
- pr_info("trusted_key: key_create failed (%d)\n", ret);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
+ if (ret < 0) {
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
- if (tpm2)
- ret = tpm2_seal_trusted(chip, payload, options);
- else
- ret = key_seal(payload, options);
- if (ret < 0)
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- break;
- default:
- ret = -EINVAL;
- goto out;
}
- if (!ret && options->pcrlock)
- ret = pcrlock(options->pcrlock);
out:
- kzfree(datablob);
kzfree(options);
- if (!ret)
- rcu_assign_keypointer(key, payload);
- else
- kzfree(payload);
return ret;
}
-static void trusted_rcu_free(struct rcu_head *rcu)
+static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
{
- struct trusted_key_payload *p;
-
- p = container_of(rcu, struct trusted_key_payload, rcu);
- kzfree(p);
-}
-
-/*
- * trusted_update - reseal an existing key with new PCR values
- */
-static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
-{
- struct trusted_key_payload *p;
- struct trusted_key_payload *new_p;
- struct trusted_key_options *new_o;
- size_t datalen = prep->datalen;
- char *datablob;
+ struct trusted_key_options *options = NULL;
int ret = 0;
+ int tpm2;
- if (key_is_negative(key))
- return -ENOKEY;
- p = key->payload.data[0];
- if (!p->migratable)
- return -EPERM;
- if (datalen <= 0 || datalen > 32767 || !prep->data)
- return -EINVAL;
+ tpm2 = tpm_is_tpm2(chip);
+ if (tpm2 < 0)
+ return tpm2;
- datablob = kmalloc(datalen + 1, GFP_KERNEL);
- if (!datablob)
+ options = trusted_options_alloc();
+ if (!options)
return -ENOMEM;
- new_o = trusted_options_alloc();
- if (!new_o) {
- ret = -ENOMEM;
- goto out;
- }
- new_p = trusted_payload_alloc(key);
- if (!new_p) {
- ret = -ENOMEM;
- goto out;
- }
- memcpy(datablob, prep->data, datalen);
- datablob[datalen] = '\0';
- ret = datablob_parse(datablob, new_p, new_o);
- if (ret != Opt_update) {
- ret = -EINVAL;
- kzfree(new_p);
+ ret = getoptions(datablob, p, options);
+ if (ret < 0)
goto out;
- }
+ dump_options(options);
- if (!new_o->keyhandle) {
+ if (!options->keyhandle) {
ret = -EINVAL;
- kzfree(new_p);
goto out;
}
- /* copy old key values, and reseal with new pcrs */
- new_p->migratable = p->migratable;
- new_p->key_len = p->key_len;
- memcpy(new_p->key, p->key, p->key_len);
- dump_payload(p);
- dump_payload(new_p);
+ if (tpm2)
+ ret = tpm2_unseal_trusted(chip, p, options);
+ else
+ ret = key_unseal(p, options);
+ if (ret < 0)
+ pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
- ret = key_seal(new_p, new_o);
- if (ret < 0) {
- pr_info("trusted_key: key_seal failed (%d)\n", ret);
- kzfree(new_p);
- goto out;
- }
- if (new_o->pcrlock) {
- ret = pcrlock(new_o->pcrlock);
+ if (options->pcrlock) {
+ ret = pcrlock(options->pcrlock);
if (ret < 0) {
- pr_info("trusted_key: pcrlock failed (%d)\n", ret);
- kzfree(new_p);
+ pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
goto out;
}
}
- rcu_assign_keypointer(key, new_p);
- call_rcu(&p->rcu, trusted_rcu_free);
out:
- kzfree(datablob);
- kzfree(new_o);
+ kzfree(options);
return ret;
}
-/*
- * trusted_read - copy the sealed blob data to userspace in hex.
- * On success, return to userspace the trusted key datablob size.
- */
-static long trusted_read(const struct key *key, char *buffer,
- size_t buflen)
-{
- const struct trusted_key_payload *p;
- char *bufp;
- int i;
-
- p = dereference_key_locked(key);
- if (!p)
- return -EINVAL;
-
- if (buffer && buflen >= 2 * p->blob_len) {
- bufp = buffer;
- for (i = 0; i < p->blob_len; i++)
- bufp = hex_byte_pack(bufp, p->blob[i]);
- }
- return 2 * p->blob_len;
-}
-
-/*
- * trusted_destroy - clear and free the key's payload
- */
-static void trusted_destroy(struct key *key)
+int tpm_trusted_get_random(unsigned char *key, size_t key_len)
{
- kzfree(key->payload.data[0]);
+ return tpm_get_random(chip, key, key_len);
}
-struct key_type key_type_trusted = {
- .name = "trusted",
- .instantiate = trusted_instantiate,
- .update = trusted_update,
- .destroy = trusted_destroy,
- .describe = user_describe,
- .read = trusted_read,
-};
-
-EXPORT_SYMBOL_GPL(key_type_trusted);
-
static void trusted_shash_release(void)
{
if (hashalg)
@@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
if (IS_ERR(hmacalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hmac_alg);
return PTR_ERR(hmacalg);
}
hashalg = crypto_alloc_shash(hash_alg, 0, 0);
if (IS_ERR(hashalg)) {
- pr_info("trusted_key: could not allocate crypto %s\n",
+ pr_info("tpm_trusted_key: could not allocate crypto %s\n",
hash_alg);
ret = PTR_ERR(hashalg);
goto hashalg_fail;
@@ -1217,16 +996,13 @@ static int __init init_digests(void)
return 0;
}
-static int __init init_trusted(void)
+static int __init init_tpm_trusted(void)
{
int ret;
- /* encrypted_keys.ko depends on successful load of this module even if
- * TPM is not used.
- */
chip = tpm_default_chip();
if (!chip)
- return 0;
+ return -ENODEV;
ret = init_digests();
if (ret < 0)
@@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
return ret;
}
-static void __exit cleanup_trusted(void)
+static void __exit exit_tpm_trusted(void)
{
if (chip) {
put_device(&chip->dev);
@@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
}
}
-late_initcall(init_trusted);
-module_exit(cleanup_trusted);
-
-MODULE_LICENSE("GPL");
+struct trusted_key_ops tpm_trusted_key_ops = {
+ .migratable = 1, /* migratable by default */
+ .init = init_tpm_trusted,
+ .seal = tpm_trusted_seal,
+ .unseal = tpm_trusted_unseal,
+ .get_random = tpm_trusted_get_random,
+ .exit = exit_tpm_trusted,
+};
+EXPORT_SYMBOL_GPL(tpm_trusted_key_ops);
--
2.7.4
^ permalink raw reply related [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-02 14:18 ` [op-tee] " Sumit Garg
` (2 preceding siblings ...)
(?)
@ 2020-06-02 19:33 ` kbuild test robot
-1 siblings, 0 replies; 56+ messages in thread
From: kbuild test robot @ 2020-06-02 19:33 UTC (permalink / raw)
To: kbuild-all
[-- Attachment #1: Type: text/plain, Size: 9011 bytes --]
Hi Sumit,
I love your patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.7 next-20200602]
[cannot apply to security/next-testing dhowells-fs/fscache-next]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Sumit-Garg/Introduce-TEE-based-Trusted-Keys-support/20200602-222340
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git f359287765c04711ff54fbd11645271d8e5ff763
config: arm-randconfig-c024-20200602 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot <lkp@intel.com>
All errors (new ones prefixed by >>, old ones prefixed by <<):
arm-linux-gnueabi-ld: section .data VMA [0000000000808000,00000000009f2f9f] overlaps section .rodata VMA [0000000000734000,0000000000c252d7]
arm-linux-gnueabi-ld: section .modinfo VMA [0000000000c252d8,0000000000c5d0b4] overlaps section .bss VMA [0000000000a08000,000000000113d5ff]
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_flushspecific':
>> crypto/asymmetric_keys/asym_tpm.c:101: undefined reference to `trusted_tpm_send'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_loadkey2':
>> crypto/asymmetric_keys/asym_tpm.c:46: undefined reference to `oiap'
>> arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:60: undefined reference to `TSS_authhmac'
>> arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:75: undefined reference to `trusted_tpm_send'
>> arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:81: undefined reference to `TSS_checkhmac1'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_sign':
crypto/asymmetric_keys/asym_tpm.c:209: undefined reference to `oiap'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:223: undefined reference to `TSS_authhmac'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:240: undefined reference to `trusted_tpm_send'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:248: undefined reference to `TSS_checkhmac1'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_unbind':
crypto/asymmetric_keys/asym_tpm.c:126: undefined reference to `oiap'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:140: undefined reference to `TSS_authhmac'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:157: undefined reference to `trusted_tpm_send'
arm-linux-gnueabi-ld: crypto/asymmetric_keys/asym_tpm.c:165: undefined reference to `TSS_checkhmac1'
vim +101 crypto/asymmetric_keys/asym_tpm.c
0c36264aa1d5b77 Denis Kenzior 2018-10-09 26
0c36264aa1d5b77 Denis Kenzior 2018-10-09 27 /*
0c36264aa1d5b77 Denis Kenzior 2018-10-09 28 * Load a TPM key from the blob provided by userspace
0c36264aa1d5b77 Denis Kenzior 2018-10-09 29 */
c6f61e59760df04 Sumit Garg 2019-10-16 30 static int tpm_loadkey2(struct tpm_buf *tb,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 31 uint32_t keyhandle, unsigned char *keyauth,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 32 const unsigned char *keyblob, int keybloblen,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 33 uint32_t *newhandle)
0c36264aa1d5b77 Denis Kenzior 2018-10-09 34 {
0c36264aa1d5b77 Denis Kenzior 2018-10-09 35 unsigned char nonceodd[TPM_NONCE_SIZE];
0c36264aa1d5b77 Denis Kenzior 2018-10-09 36 unsigned char enonce[TPM_NONCE_SIZE];
0c36264aa1d5b77 Denis Kenzior 2018-10-09 37 unsigned char authdata[SHA1_DIGEST_SIZE];
0c36264aa1d5b77 Denis Kenzior 2018-10-09 38 uint32_t authhandle = 0;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 39 unsigned char cont = 0;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 40 uint32_t ordinal;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 41 int ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 42
0c36264aa1d5b77 Denis Kenzior 2018-10-09 43 ordinal = htonl(TPM_ORD_LOADKEY2);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 44
0c36264aa1d5b77 Denis Kenzior 2018-10-09 45 /* session for loading the key */
0c36264aa1d5b77 Denis Kenzior 2018-10-09 @46 ret = oiap(tb, &authhandle, enonce);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 47 if (ret < 0) {
0c36264aa1d5b77 Denis Kenzior 2018-10-09 48 pr_info("oiap failed (%d)\n", ret);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 49 return ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 50 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 51
0c36264aa1d5b77 Denis Kenzior 2018-10-09 52 /* generate odd nonce */
0c36264aa1d5b77 Denis Kenzior 2018-10-09 53 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 54 if (ret < 0) {
0c36264aa1d5b77 Denis Kenzior 2018-10-09 55 pr_info("tpm_get_random failed (%d)\n", ret);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 56 return ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 57 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 58
0c36264aa1d5b77 Denis Kenzior 2018-10-09 59 /* calculate authorization HMAC value */
0c36264aa1d5b77 Denis Kenzior 2018-10-09 @60 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 61 nonceodd, cont, sizeof(uint32_t), &ordinal,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 62 keybloblen, keyblob, 0, 0);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 63 if (ret < 0)
0c36264aa1d5b77 Denis Kenzior 2018-10-09 64 return ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 65
0c36264aa1d5b77 Denis Kenzior 2018-10-09 66 /* build the request buffer */
c6f61e59760df04 Sumit Garg 2019-10-16 67 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_LOADKEY2);
c6f61e59760df04 Sumit Garg 2019-10-16 68 tpm_buf_append_u32(tb, keyhandle);
c6f61e59760df04 Sumit Garg 2019-10-16 69 tpm_buf_append(tb, keyblob, keybloblen);
c6f61e59760df04 Sumit Garg 2019-10-16 70 tpm_buf_append_u32(tb, authhandle);
c6f61e59760df04 Sumit Garg 2019-10-16 71 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
c6f61e59760df04 Sumit Garg 2019-10-16 72 tpm_buf_append_u8(tb, cont);
c6f61e59760df04 Sumit Garg 2019-10-16 73 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 74
0c36264aa1d5b77 Denis Kenzior 2018-10-09 @75 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 76 if (ret < 0) {
0c36264aa1d5b77 Denis Kenzior 2018-10-09 77 pr_info("authhmac failed (%d)\n", ret);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 78 return ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 79 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 80
0c36264aa1d5b77 Denis Kenzior 2018-10-09 @81 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd, keyauth,
0c36264aa1d5b77 Denis Kenzior 2018-10-09 82 SHA1_DIGEST_SIZE, 0, 0);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 83 if (ret < 0) {
0c36264aa1d5b77 Denis Kenzior 2018-10-09 84 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 85 return ret;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 86 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 87
0c36264aa1d5b77 Denis Kenzior 2018-10-09 88 *newhandle = LOAD32(tb->data, TPM_DATA_OFFSET);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 89 return 0;
0c36264aa1d5b77 Denis Kenzior 2018-10-09 90 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 91
0c36264aa1d5b77 Denis Kenzior 2018-10-09 92 /*
0c36264aa1d5b77 Denis Kenzior 2018-10-09 93 * Execute the FlushSpecific TPM command
0c36264aa1d5b77 Denis Kenzior 2018-10-09 94 */
c6f61e59760df04 Sumit Garg 2019-10-16 95 static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
0c36264aa1d5b77 Denis Kenzior 2018-10-09 96 {
c6f61e59760df04 Sumit Garg 2019-10-16 97 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_FLUSHSPECIFIC);
c6f61e59760df04 Sumit Garg 2019-10-16 98 tpm_buf_append_u32(tb, handle);
c6f61e59760df04 Sumit Garg 2019-10-16 99 tpm_buf_append_u32(tb, TPM_RT_KEY);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 100
0c36264aa1d5b77 Denis Kenzior 2018-10-09 @101 return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
0c36264aa1d5b77 Denis Kenzior 2018-10-09 102 }
0c36264aa1d5b77 Denis Kenzior 2018-10-09 103
:::::: The code at line 101 was first introduced by commit
:::::: 0c36264aa1d5b77eb21f646f83223134e4528cfe KEYS: asym_tpm: Add loadkey2 and flushspecific [ver #2]
:::::: TO: Denis Kenzior <denkenz@gmail.com>
:::::: CC: James Morris <james.morris@microsoft.com>
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 22180 bytes --]
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-02 14:18 ` [op-tee] " Sumit Garg
` (3 preceding siblings ...)
(?)
@ 2020-06-02 20:54 ` kbuild test robot
-1 siblings, 0 replies; 56+ messages in thread
From: kbuild test robot @ 2020-06-02 20:54 UTC (permalink / raw)
To: kbuild-all
[-- Attachment #1: Type: text/plain, Size: 2297 bytes --]
Hi Sumit,
I love your patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[also build test WARNING on v5.7 next-20200602]
[cannot apply to security/next-testing dhowells-fs/fscache-next]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Sumit-Garg/Introduce-TEE-based-Trusted-Keys-support/20200602-222340
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git f359287765c04711ff54fbd11645271d8e5ff763
config: x86_64-allyesconfig (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project 2388a096e7865c043e83ece4e26654bd3d1a20d5)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot <lkp@intel.com>
All warnings (new ones prefixed by >>, old ones prefixed by <<):
>> security/keys/trusted-keys/trusted_tpm1.c:945:5: warning: no previous prototype for function 'tpm_trusted_get_random' [-Wmissing-prototypes]
int tpm_trusted_get_random(unsigned char *key, size_t key_len)
^
security/keys/trusted-keys/trusted_tpm1.c:945:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
int tpm_trusted_get_random(unsigned char *key, size_t key_len)
^
static
1 warning generated.
vim +/tpm_trusted_get_random +945 security/keys/trusted-keys/trusted_tpm1.c
944
> 945 int tpm_trusted_get_random(unsigned char *key, size_t key_len)
946 {
947 return tpm_get_random(chip, key, key_len);
948 }
949
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 73594 bytes --]
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-02 14:18 ` [op-tee] " Sumit Garg
` (4 preceding siblings ...)
(?)
@ 2020-06-02 22:37 ` kbuild test robot
-1 siblings, 0 replies; 56+ messages in thread
From: kbuild test robot @ 2020-06-02 22:37 UTC (permalink / raw)
To: kbuild-all
[-- Attachment #1: Type: text/plain, Size: 3863 bytes --]
Hi Sumit,
I love your patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.7 next-20200602]
[cannot apply to security/next-testing dhowells-fs/fscache-next]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Sumit-Garg/Introduce-TEE-based-Trusted-Keys-support/20200602-222340
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git f359287765c04711ff54fbd11645271d8e5ff763
config: nds32-randconfig-r036-20200602 (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=nds32
If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot <lkp@intel.com>
All errors (new ones prefixed by >>, old ones prefixed by <<):
nds32le-linux-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_flushspecific':
>> asym_tpm.c:(.text+0x940): undefined reference to `trusted_tpm_send'
>> nds32le-linux-ld: asym_tpm.c:(.text+0x944): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_loadkey2.constprop.0':
>> asym_tpm.c:(.text+0x984): undefined reference to `oiap'
>> nds32le-linux-ld: asym_tpm.c:(.text+0x988): undefined reference to `oiap'
>> nds32le-linux-ld: asym_tpm.c:(.text+0xa14): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0xa18): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0xc02): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: asym_tpm.c:(.text+0xc06): undefined reference to `trusted_tpm_send'
>> nds32le-linux-ld: asym_tpm.c:(.text+0xc40): undefined reference to `TSS_checkhmac1'
nds32le-linux-ld: asym_tpm.c:(.text+0xc44): undefined reference to `TSS_checkhmac1'
nds32le-linux-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_unbind':
asym_tpm.c:(.text+0xcbc): undefined reference to `oiap'
nds32le-linux-ld: asym_tpm.c:(.text+0xcc0): undefined reference to `oiap'
nds32le-linux-ld: asym_tpm.c:(.text+0xd5e): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0xd62): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0xfbe): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: asym_tpm.c:(.text+0xfc2): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: asym_tpm.c:(.text+0x101c): undefined reference to `TSS_checkhmac1'
nds32le-linux-ld: asym_tpm.c:(.text+0x1020): undefined reference to `TSS_checkhmac1'
nds32le-linux-ld: crypto/asymmetric_keys/asym_tpm.o: in function `tpm_sign':
asym_tpm.c:(.text+0x10b8): undefined reference to `oiap'
nds32le-linux-ld: asym_tpm.c:(.text+0x10bc): undefined reference to `oiap'
nds32le-linux-ld: asym_tpm.c:(.text+0x115a): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0x115e): undefined reference to `TSS_authhmac'
nds32le-linux-ld: asym_tpm.c:(.text+0x13ba): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: asym_tpm.c:(.text+0x13be): undefined reference to `trusted_tpm_send'
nds32le-linux-ld: asym_tpm.c:(.text+0x1418): undefined reference to `TSS_checkhmac1'
nds32le-linux-ld: asym_tpm.c:(.text+0x141c): undefined reference to `TSS_checkhmac1'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 26797 bytes --]
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-02 14:18 ` [op-tee] " Sumit Garg
(?)
(?)
@ 2020-06-15 18:24 ` Jarkko Sakkinen
-1 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-15 18:24 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev, daniel.thompson, op-tee, corbet, jejb, janne.karhunen,
linux-doc, jmorris, zohar, linux-kernel, dhowells,
linux-security-module, keyrings, Markus.Wamser, casey,
linux-integrity, jens.wiklander, linux-arm-kernel, serge
On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> index 47c0415..22632c6 100644
> --- a/security/keys/Kconfig
> +++ b/security/keys/Kconfig
> @@ -72,17 +72,26 @@ config BIG_KEYS
>
> config TRUSTED_KEYS
> tristate "TRUSTED KEYS"
> - depends on KEYS && TCG_TPM
> + depends on KEYS
> + help
> + This option provides support for creating, sealing, and unsealing
> + keys in the kernel. Trusted keys are random number symmetric keys,
> + generated and sealed by a trust source (TPM or TEE). Userspace will
> + only ever see encrypted blobs.
> +
> + If you are unsure as to whether this is required, answer N.
> +
> +config TRUSTED_TPM
TRUSTED_TPM_KEYS
> + bool "TPM based TRUSTED KEYS"
> + depends on TRUSTED_KEYS && TCG_TPM
> select CRYPTO
> select CRYPTO_HMAC
> select CRYPTO_SHA1
> select CRYPTO_HASH_INFO
> help
> - This option provides support for creating, sealing, and unsealing
> - keys in the kernel. Trusted keys are random number symmetric keys,
> - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> - if the boot PCRs and other criteria match. Userspace will only ever
> - see encrypted blobs.
> + This option provides support for TPM based trusted keys where TPM acts
> + as the trust source. The keys are RSA-sealed by the TPM and it only
> + unseals the keys, if the boot PCRs and other criteria match.
>
> If you are unsure as to whether this is required, answer N.
>
> diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> index 7b73ceb..03c3ad4 100644
> --- a/security/keys/trusted-keys/Makefile
> +++ b/security/keys/trusted-keys/Makefile
> @@ -4,5 +4,6 @@
> #
>
> obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> -trusted-y += trusted_tpm1.o
> -trusted-y += trusted_tpm2.o
> +trusted-y += trusted_core.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> new file mode 100644
> index 0000000..3d07d27
> --- /dev/null
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -0,0 +1,319 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> + *
> + * Author:
> + * David Safford <safford@us.ibm.com>
> + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
Rather remove David's because
1. Git log gives a verifiable and exact log of authority.
2. David's commit was done post-epoch of Git.
> + *
> + * See Documentation/security/keys/trusted-encrypted.rst
> + */
> +
> +#include <keys/user-type.h>
> +#include <keys/trusted-type.h>
> +#include <linux/capability.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/key-type.h>
> +#include <linux/module.h>
> +#include <linux/parser.h>
> +#include <linux/rcupdate.h>
> +#include <linux/slab.h>
> +#include <linux/string.h>
> +#include <linux/uaccess.h>
> +
> +#if defined(CONFIG_TRUSTED_TPM)
> +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> +#else
> +static struct trusted_key_ops *trusted_key_ops;
> +#endif
You should just have
extern trusted_key_ops *trusted_key_ops;
> +
> +enum {
> + Opt_err,
> + Opt_new, Opt_load, Opt_update,
> +};
> +
> +static const match_table_t key_tokens = {
> + {Opt_new, "new"},
> + {Opt_load, "load"},
> + {Opt_update, "update"},
> + {Opt_err, NULL}
> +};
> +
> +/*
> + * datablob_parse - parse the keyctl data and fill in the
> + * payload structure
> + *
> + * On success returns 0, otherwise -EINVAL.
> + */
> +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> +{
> + substring_t args[MAX_OPT_ARGS];
> + long keylen;
> + int ret = -EINVAL;
> + int key_cmd;
> + char *c;
> +
> + /* main command */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + key_cmd = match_token(c, key_tokens, args);
> + switch (key_cmd) {
> + case Opt_new:
> + /* first argument is key size */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + ret = kstrtol(c, 10, &keylen);
> + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> + return -EINVAL;
> + p->key_len = keylen;
> + ret = Opt_new;
> + break;
> + case Opt_load:
> + /* first argument is sealed blob */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + p->blob_len = strlen(c) / 2;
> + if (p->blob_len > MAX_BLOB_SIZE)
> + return -EINVAL;
> + ret = hex2bin(p->blob, c, p->blob_len);
> + if (ret < 0)
> + return -EINVAL;
> + ret = Opt_load;
> + break;
> + case Opt_update:
> + ret = Opt_update;
> + break;
> + case Opt_err:
> + return -EINVAL;
> + }
> + return ret;
> +}
> +
> +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +{
> + struct trusted_key_payload *p = NULL;
> + int ret;
> +
> + ret = key_payload_reserve(key, sizeof(*p));
> + if (ret < 0)
> + return p;
> + p = kzalloc(sizeof(*p), GFP_KERNEL);
> +
> + p->migratable = trusted_key_ops->migratable;
> +
> + return p;
> +}
> +
> +/*
> + * trusted_instantiate - create a new trusted key
> + *
> + * Unseal an existing trusted blob or, for a new key, get a
> + * random key, then seal and create a trusted key-type key,
> + * adding it to the specified keyring.
> + *
> + * On success, return 0. Otherwise return errno.
> + */
> +static int trusted_instantiate(struct key *key,
> + struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *payload = NULL;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> + int key_cmd;
> + size_t key_len;
> +
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> +
> + payload = trusted_payload_alloc(key);
> + if (!payload) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + key_cmd = datablob_parse(datablob, payload);
> + if (key_cmd < 0) {
> + ret = key_cmd;
> + goto out;
> + }
> +
> + dump_payload(payload);
> +
> + switch (key_cmd) {
> + case Opt_load:
> + ret = trusted_key_ops->unseal(payload, datablob);
> + dump_payload(payload);
> + if (ret < 0)
> + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> + break;
> + case Opt_new:
> + key_len = payload->key_len;
> + ret = trusted_key_ops->get_random(payload->key, key_len);
> + if (ret != key_len) {
> + pr_info("trusted_key: key_create failed (%d)\n", ret);
> + goto out;
> + }
> +
> + ret = trusted_key_ops->seal(payload, datablob);
> + if (ret < 0)
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> +out:
> + kzfree(datablob);
> + if (!ret)
> + rcu_assign_keypointer(key, payload);
> + else
> + kzfree(payload);
> + return ret;
> +}
> +
> +static void trusted_rcu_free(struct rcu_head *rcu)
> +{
> + struct trusted_key_payload *p;
> +
> + p = container_of(rcu, struct trusted_key_payload, rcu);
> + kzfree(p);
> +}
> +
> +/*
> + * trusted_update - reseal an existing key with new PCR values
> + */
> +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *p;
> + struct trusted_key_payload *new_p;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> +
> + if (key_is_negative(key))
> + return -ENOKEY;
> + p = key->payload.data[0];
> + if (!p->migratable)
> + return -EPERM;
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> +
> + new_p = trusted_payload_alloc(key);
> + if (!new_p) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> + ret = datablob_parse(datablob, new_p);
> + if (ret != Opt_update) {
> + ret = -EINVAL;
> + kzfree(new_p);
> + goto out;
> + }
> +
> + /* copy old key values, and reseal with new pcrs */
> + new_p->migratable = p->migratable;
> + new_p->key_len = p->key_len;
> + memcpy(new_p->key, p->key, p->key_len);
> + dump_payload(p);
> + dump_payload(new_p);
> +
> + ret = trusted_key_ops->seal(new_p, datablob);
> + if (ret < 0) {
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + kzfree(new_p);
> + goto out;
> + }
> +
> + rcu_assign_keypointer(key, new_p);
> + call_rcu(&p->rcu, trusted_rcu_free);
> +out:
> + kzfree(datablob);
> + return ret;
> +}
> +
> +/*
> + * trusted_read - copy the sealed blob data to userspace in hex.
> + * On success, return to userspace the trusted key datablob size.
> + */
> +static long trusted_read(const struct key *key, char *buffer,
> + size_t buflen)
> +{
> + const struct trusted_key_payload *p;
> + char *bufp;
> + int i;
> +
> + p = dereference_key_locked(key);
> + if (!p)
> + return -EINVAL;
> +
> + if (buffer && buflen >= 2 * p->blob_len) {
> + bufp = buffer;
> + for (i = 0; i < p->blob_len; i++)
> + bufp = hex_byte_pack(bufp, p->blob[i]);
> + }
> + return 2 * p->blob_len;
> +}
> +
> +/*
> + * trusted_destroy - clear and free the key's payload
> + */
> +static void trusted_destroy(struct key *key)
> +{
> + kzfree(key->payload.data[0]);
> +}
> +
> +struct key_type key_type_trusted = {
> + .name = "trusted",
> + .instantiate = trusted_instantiate,
> + .update = trusted_update,
> + .destroy = trusted_destroy,
> + .describe = user_describe,
> + .read = trusted_read,
> +};
> +EXPORT_SYMBOL_GPL(key_type_trusted);
> +
> +static int __init init_trusted(void)
> +{
> + int ret;
> +
> + /*
> + * encrypted_keys.ko depends on successful load of this module even if
> + * trusted key implementation is not found.
> + */
> + if (!trusted_key_ops)
> + return 0;
> +
> + ret = trusted_key_ops->init();
> + if (ret = -ENODEV)
> + return 0;
> +
> + return ret;
> +}
> +
> +static void __exit cleanup_trusted(void)
> +{
> + trusted_key_ops->exit();
> +}
> +
> +late_initcall(init_trusted);
> +module_exit(cleanup_trusted);
> +
> +MODULE_LICENSE("GPL");
> diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> index 8001ab0..e378b27 100644
> --- a/security/keys/trusted-keys/trusted_tpm1.c
> +++ b/security/keys/trusted-keys/trusted_tpm1.c
> @@ -1,29 +1,26 @@
> // SPDX-License-Identifier: GPL-2.0-only
> /*
> * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> *
> * Author:
> * David Safford <safford@us.ibm.com>
> + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> *
> * See Documentation/security/keys/trusted-encrypted.rst
> */
>
> #include <crypto/hash_info.h>
> -#include <linux/uaccess.h>
> -#include <linux/module.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/parser.h>
> #include <linux/string.h>
> #include <linux/err.h>
> -#include <keys/user-type.h>
> #include <keys/trusted-type.h>
> #include <linux/key-type.h>
> -#include <linux/rcupdate.h>
> #include <linux/crypto.h>
> #include <crypto/hash.h>
> #include <crypto/sha.h>
> -#include <linux/capability.h>
> #include <linux/tpm.h>
> #include <linux/tpm_command.h>
>
> @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
>
> enum {
> Opt_err,
> - Opt_new, Opt_load, Opt_update,
> Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> Opt_hash,
> @@ -712,9 +708,6 @@ enum {
> };
>
> static const match_table_t key_tokens = {
> - {Opt_new, "new"},
> - {Opt_load, "load"},
> - {Opt_update, "update"},
> {Opt_keyhandle, "keyhandle=%s"},
> {Opt_keyauth, "keyauth=%s"},
> {Opt_blobauth, "blobauth=%s"},
> @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> return 0;
> }
>
> -/*
> - * datablob_parse - parse the keyctl data and fill in the
> - * payload and options structures
> - *
> - * On success returns 0, otherwise -EINVAL.
> - */
> -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> - struct trusted_key_options *o)
> -{
> - substring_t args[MAX_OPT_ARGS];
> - long keylen;
> - int ret = -EINVAL;
> - int key_cmd;
> - char *c;
> -
> - /* main command */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - key_cmd = match_token(c, key_tokens, args);
> - switch (key_cmd) {
> - case Opt_new:
> - /* first argument is key size */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - ret = kstrtol(c, 10, &keylen);
> - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> - return -EINVAL;
> - p->key_len = keylen;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_new;
> - break;
> - case Opt_load:
> - /* first argument is sealed blob */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - p->blob_len = strlen(c) / 2;
> - if (p->blob_len > MAX_BLOB_SIZE)
> - return -EINVAL;
> - ret = hex2bin(p->blob, c, p->blob_len);
> - if (ret < 0)
> - return -EINVAL;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_load;
> - break;
> - case Opt_update:
> - /* all arguments are options */
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_update;
> - break;
> - case Opt_err:
> - return -EINVAL;
> - break;
> - }
> - return ret;
> -}
> -
> static struct trusted_key_options *trusted_options_alloc(void)
> {
> struct trusted_key_options *options;
> @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> return options;
> }
>
> -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p = NULL;
> - int ret;
> -
> - ret = key_payload_reserve(key, sizeof *p);
> - if (ret < 0)
> - return p;
> - p = kzalloc(sizeof *p, GFP_KERNEL);
> - if (p)
> - p->migratable = 1; /* migratable by default */
> - return p;
> -}
> -
> -/*
> - * trusted_instantiate - create a new trusted key
> - *
> - * Unseal an existing trusted blob or, for a new key, get a
> - * random key, then seal and create a trusted key-type key,
> - * adding it to the specified keyring.
> - *
> - * On success, return 0. Otherwise return errno.
> - */
> -static int trusted_instantiate(struct key *key,
> - struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *payload = NULL;
> struct trusted_key_options *options = NULL;
> - size_t datalen = prep->datalen;
> - char *datablob;
> int ret = 0;
> - int key_cmd;
> - size_t key_len;
> int tpm2;
>
> tpm2 = tpm_is_tpm2(chip);
> if (tpm2 < 0)
> return tpm2;
>
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> -
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> - return -ENOMEM;
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> -
> options = trusted_options_alloc();
> - if (!options) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - payload = trusted_payload_alloc(key);
> - if (!payload) {
> - ret = -ENOMEM;
> - goto out;
> - }
> + if (!options)
> + return -ENOMEM;
>
> - key_cmd = datablob_parse(datablob, payload, options);
> - if (key_cmd < 0) {
> - ret = key_cmd;
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> if (!options->keyhandle) {
> ret = -EINVAL;
> goto out;
> }
>
> - dump_payload(payload);
> - dump_options(options);
> + if (tpm2)
> + ret = tpm2_seal_trusted(chip, p, options);
> + else
> + ret = key_seal(p, options);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> + goto out;
> + }
>
> - switch (key_cmd) {
> - case Opt_load:
> - if (tpm2)
> - ret = tpm2_unseal_trusted(chip, payload, options);
> - else
> - ret = key_unseal(payload, options);
> - dump_payload(payload);
> - dump_options(options);
> - if (ret < 0)
> - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> - break;
> - case Opt_new:
> - key_len = payload->key_len;
> - ret = tpm_get_random(chip, payload->key, key_len);
> - if (ret != key_len) {
> - pr_info("trusted_key: key_create failed (%d)\n", ret);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> - if (tpm2)
> - ret = tpm2_seal_trusted(chip, payload, options);
> - else
> - ret = key_seal(payload, options);
> - if (ret < 0)
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - break;
> - default:
> - ret = -EINVAL;
> - goto out;
> }
> - if (!ret && options->pcrlock)
> - ret = pcrlock(options->pcrlock);
> out:
> - kzfree(datablob);
> kzfree(options);
> - if (!ret)
> - rcu_assign_keypointer(key, payload);
> - else
> - kzfree(payload);
> return ret;
> }
>
> -static void trusted_rcu_free(struct rcu_head *rcu)
> +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p;
> -
> - p = container_of(rcu, struct trusted_key_payload, rcu);
> - kzfree(p);
> -}
> -
> -/*
> - * trusted_update - reseal an existing key with new PCR values
> - */
> -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *p;
> - struct trusted_key_payload *new_p;
> - struct trusted_key_options *new_o;
> - size_t datalen = prep->datalen;
> - char *datablob;
> + struct trusted_key_options *options = NULL;
> int ret = 0;
> + int tpm2;
>
> - if (key_is_negative(key))
> - return -ENOKEY;
> - p = key->payload.data[0];
> - if (!p->migratable)
> - return -EPERM;
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> + tpm2 = tpm_is_tpm2(chip);
> + if (tpm2 < 0)
> + return tpm2;
>
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> + options = trusted_options_alloc();
> + if (!options)
> return -ENOMEM;
> - new_o = trusted_options_alloc();
> - if (!new_o) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - new_p = trusted_payload_alloc(key);
> - if (!new_p) {
> - ret = -ENOMEM;
> - goto out;
> - }
>
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> - ret = datablob_parse(datablob, new_p, new_o);
> - if (ret != Opt_update) {
> - ret = -EINVAL;
> - kzfree(new_p);
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> - if (!new_o->keyhandle) {
> + if (!options->keyhandle) {
> ret = -EINVAL;
> - kzfree(new_p);
> goto out;
> }
>
> - /* copy old key values, and reseal with new pcrs */
> - new_p->migratable = p->migratable;
> - new_p->key_len = p->key_len;
> - memcpy(new_p->key, p->key, p->key_len);
> - dump_payload(p);
> - dump_payload(new_p);
> + if (tpm2)
> + ret = tpm2_unseal_trusted(chip, p, options);
> + else
> + ret = key_unseal(p, options);
> + if (ret < 0)
> + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
>
> - ret = key_seal(new_p, new_o);
> - if (ret < 0) {
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - kzfree(new_p);
> - goto out;
> - }
> - if (new_o->pcrlock) {
> - ret = pcrlock(new_o->pcrlock);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> if (ret < 0) {
> - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> - kzfree(new_p);
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> }
> - rcu_assign_keypointer(key, new_p);
> - call_rcu(&p->rcu, trusted_rcu_free);
> out:
> - kzfree(datablob);
> - kzfree(new_o);
> + kzfree(options);
> return ret;
> }
>
> -/*
> - * trusted_read - copy the sealed blob data to userspace in hex.
> - * On success, return to userspace the trusted key datablob size.
> - */
> -static long trusted_read(const struct key *key, char *buffer,
> - size_t buflen)
> -{
> - const struct trusted_key_payload *p;
> - char *bufp;
> - int i;
> -
> - p = dereference_key_locked(key);
> - if (!p)
> - return -EINVAL;
> -
> - if (buffer && buflen >= 2 * p->blob_len) {
> - bufp = buffer;
> - for (i = 0; i < p->blob_len; i++)
> - bufp = hex_byte_pack(bufp, p->blob[i]);
> - }
> - return 2 * p->blob_len;
> -}
> -
> -/*
> - * trusted_destroy - clear and free the key's payload
> - */
> -static void trusted_destroy(struct key *key)
> +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> {
> - kzfree(key->payload.data[0]);
> + return tpm_get_random(chip, key, key_len);
> }
>
> -struct key_type key_type_trusted = {
> - .name = "trusted",
> - .instantiate = trusted_instantiate,
> - .update = trusted_update,
> - .destroy = trusted_destroy,
> - .describe = user_describe,
> - .read = trusted_read,
> -};
> -
> -EXPORT_SYMBOL_GPL(key_type_trusted);
> -
> static void trusted_shash_release(void)
> {
> if (hashalg)
> @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
>
> hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> if (IS_ERR(hmacalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hmac_alg);
> return PTR_ERR(hmacalg);
> }
>
> hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> if (IS_ERR(hashalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hash_alg);
> ret = PTR_ERR(hashalg);
> goto hashalg_fail;
> @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> return 0;
> }
>
> -static int __init init_trusted(void)
> +static int __init init_tpm_trusted(void)
> {
> int ret;
>
> - /* encrypted_keys.ko depends on successful load of this module even if
> - * TPM is not used.
> - */
> chip = tpm_default_chip();
> if (!chip)
> - return 0;
> + return -ENODEV;
>
> ret = init_digests();
> if (ret < 0)
> @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> return ret;
> }
>
> -static void __exit cleanup_trusted(void)
> +static void __exit exit_tpm_trusted(void)
> {
> if (chip) {
> put_device(&chip->dev);
> @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> }
> }
>
> -late_initcall(init_trusted);
> -module_exit(cleanup_trusted);
> -
> -MODULE_LICENSE("GPL");
> +struct trusted_key_ops tpm_trusted_key_ops = {
> + .migratable = 1, /* migratable by default */
> + .init = init_tpm_trusted,
> + .seal = tpm_trusted_seal,
> + .unseal = tpm_trusted_unseal,
> + .get_random = tpm_trusted_get_random,
> + .exit = exit_tpm_trusted,
> +};
> +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
> 2.7.4
>
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-15 18:24 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-15 18:24 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev, daniel.thompson, op-tee, corbet, jejb, janne.karhunen,
linux-doc, jmorris, zohar, linux-kernel, dhowells,
linux-security-module, keyrings, Markus.Wamser, casey,
linux-integrity, jens.wiklander, linux-arm-kernel, serge
On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> index 47c0415..22632c6 100644
> --- a/security/keys/Kconfig
> +++ b/security/keys/Kconfig
> @@ -72,17 +72,26 @@ config BIG_KEYS
>
> config TRUSTED_KEYS
> tristate "TRUSTED KEYS"
> - depends on KEYS && TCG_TPM
> + depends on KEYS
> + help
> + This option provides support for creating, sealing, and unsealing
> + keys in the kernel. Trusted keys are random number symmetric keys,
> + generated and sealed by a trust source (TPM or TEE). Userspace will
> + only ever see encrypted blobs.
> +
> + If you are unsure as to whether this is required, answer N.
> +
> +config TRUSTED_TPM
TRUSTED_TPM_KEYS
> + bool "TPM based TRUSTED KEYS"
> + depends on TRUSTED_KEYS && TCG_TPM
> select CRYPTO
> select CRYPTO_HMAC
> select CRYPTO_SHA1
> select CRYPTO_HASH_INFO
> help
> - This option provides support for creating, sealing, and unsealing
> - keys in the kernel. Trusted keys are random number symmetric keys,
> - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> - if the boot PCRs and other criteria match. Userspace will only ever
> - see encrypted blobs.
> + This option provides support for TPM based trusted keys where TPM acts
> + as the trust source. The keys are RSA-sealed by the TPM and it only
> + unseals the keys, if the boot PCRs and other criteria match.
>
> If you are unsure as to whether this is required, answer N.
>
> diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> index 7b73ceb..03c3ad4 100644
> --- a/security/keys/trusted-keys/Makefile
> +++ b/security/keys/trusted-keys/Makefile
> @@ -4,5 +4,6 @@
> #
>
> obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> -trusted-y += trusted_tpm1.o
> -trusted-y += trusted_tpm2.o
> +trusted-y += trusted_core.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> new file mode 100644
> index 0000000..3d07d27
> --- /dev/null
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -0,0 +1,319 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> + *
> + * Author:
> + * David Safford <safford@us.ibm.com>
> + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
Rather remove David's because
1. Git log gives a verifiable and exact log of authority.
2. David's commit was done post-epoch of Git.
> + *
> + * See Documentation/security/keys/trusted-encrypted.rst
> + */
> +
> +#include <keys/user-type.h>
> +#include <keys/trusted-type.h>
> +#include <linux/capability.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/key-type.h>
> +#include <linux/module.h>
> +#include <linux/parser.h>
> +#include <linux/rcupdate.h>
> +#include <linux/slab.h>
> +#include <linux/string.h>
> +#include <linux/uaccess.h>
> +
> +#if defined(CONFIG_TRUSTED_TPM)
> +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> +#else
> +static struct trusted_key_ops *trusted_key_ops;
> +#endif
You should just have
extern trusted_key_ops *trusted_key_ops;
> +
> +enum {
> + Opt_err,
> + Opt_new, Opt_load, Opt_update,
> +};
> +
> +static const match_table_t key_tokens = {
> + {Opt_new, "new"},
> + {Opt_load, "load"},
> + {Opt_update, "update"},
> + {Opt_err, NULL}
> +};
> +
> +/*
> + * datablob_parse - parse the keyctl data and fill in the
> + * payload structure
> + *
> + * On success returns 0, otherwise -EINVAL.
> + */
> +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> +{
> + substring_t args[MAX_OPT_ARGS];
> + long keylen;
> + int ret = -EINVAL;
> + int key_cmd;
> + char *c;
> +
> + /* main command */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + key_cmd = match_token(c, key_tokens, args);
> + switch (key_cmd) {
> + case Opt_new:
> + /* first argument is key size */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + ret = kstrtol(c, 10, &keylen);
> + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> + return -EINVAL;
> + p->key_len = keylen;
> + ret = Opt_new;
> + break;
> + case Opt_load:
> + /* first argument is sealed blob */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + p->blob_len = strlen(c) / 2;
> + if (p->blob_len > MAX_BLOB_SIZE)
> + return -EINVAL;
> + ret = hex2bin(p->blob, c, p->blob_len);
> + if (ret < 0)
> + return -EINVAL;
> + ret = Opt_load;
> + break;
> + case Opt_update:
> + ret = Opt_update;
> + break;
> + case Opt_err:
> + return -EINVAL;
> + }
> + return ret;
> +}
> +
> +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +{
> + struct trusted_key_payload *p = NULL;
> + int ret;
> +
> + ret = key_payload_reserve(key, sizeof(*p));
> + if (ret < 0)
> + return p;
> + p = kzalloc(sizeof(*p), GFP_KERNEL);
> +
> + p->migratable = trusted_key_ops->migratable;
> +
> + return p;
> +}
> +
> +/*
> + * trusted_instantiate - create a new trusted key
> + *
> + * Unseal an existing trusted blob or, for a new key, get a
> + * random key, then seal and create a trusted key-type key,
> + * adding it to the specified keyring.
> + *
> + * On success, return 0. Otherwise return errno.
> + */
> +static int trusted_instantiate(struct key *key,
> + struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *payload = NULL;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> + int key_cmd;
> + size_t key_len;
> +
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> +
> + payload = trusted_payload_alloc(key);
> + if (!payload) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + key_cmd = datablob_parse(datablob, payload);
> + if (key_cmd < 0) {
> + ret = key_cmd;
> + goto out;
> + }
> +
> + dump_payload(payload);
> +
> + switch (key_cmd) {
> + case Opt_load:
> + ret = trusted_key_ops->unseal(payload, datablob);
> + dump_payload(payload);
> + if (ret < 0)
> + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> + break;
> + case Opt_new:
> + key_len = payload->key_len;
> + ret = trusted_key_ops->get_random(payload->key, key_len);
> + if (ret != key_len) {
> + pr_info("trusted_key: key_create failed (%d)\n", ret);
> + goto out;
> + }
> +
> + ret = trusted_key_ops->seal(payload, datablob);
> + if (ret < 0)
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> +out:
> + kzfree(datablob);
> + if (!ret)
> + rcu_assign_keypointer(key, payload);
> + else
> + kzfree(payload);
> + return ret;
> +}
> +
> +static void trusted_rcu_free(struct rcu_head *rcu)
> +{
> + struct trusted_key_payload *p;
> +
> + p = container_of(rcu, struct trusted_key_payload, rcu);
> + kzfree(p);
> +}
> +
> +/*
> + * trusted_update - reseal an existing key with new PCR values
> + */
> +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *p;
> + struct trusted_key_payload *new_p;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> +
> + if (key_is_negative(key))
> + return -ENOKEY;
> + p = key->payload.data[0];
> + if (!p->migratable)
> + return -EPERM;
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> +
> + new_p = trusted_payload_alloc(key);
> + if (!new_p) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> + ret = datablob_parse(datablob, new_p);
> + if (ret != Opt_update) {
> + ret = -EINVAL;
> + kzfree(new_p);
> + goto out;
> + }
> +
> + /* copy old key values, and reseal with new pcrs */
> + new_p->migratable = p->migratable;
> + new_p->key_len = p->key_len;
> + memcpy(new_p->key, p->key, p->key_len);
> + dump_payload(p);
> + dump_payload(new_p);
> +
> + ret = trusted_key_ops->seal(new_p, datablob);
> + if (ret < 0) {
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + kzfree(new_p);
> + goto out;
> + }
> +
> + rcu_assign_keypointer(key, new_p);
> + call_rcu(&p->rcu, trusted_rcu_free);
> +out:
> + kzfree(datablob);
> + return ret;
> +}
> +
> +/*
> + * trusted_read - copy the sealed blob data to userspace in hex.
> + * On success, return to userspace the trusted key datablob size.
> + */
> +static long trusted_read(const struct key *key, char *buffer,
> + size_t buflen)
> +{
> + const struct trusted_key_payload *p;
> + char *bufp;
> + int i;
> +
> + p = dereference_key_locked(key);
> + if (!p)
> + return -EINVAL;
> +
> + if (buffer && buflen >= 2 * p->blob_len) {
> + bufp = buffer;
> + for (i = 0; i < p->blob_len; i++)
> + bufp = hex_byte_pack(bufp, p->blob[i]);
> + }
> + return 2 * p->blob_len;
> +}
> +
> +/*
> + * trusted_destroy - clear and free the key's payload
> + */
> +static void trusted_destroy(struct key *key)
> +{
> + kzfree(key->payload.data[0]);
> +}
> +
> +struct key_type key_type_trusted = {
> + .name = "trusted",
> + .instantiate = trusted_instantiate,
> + .update = trusted_update,
> + .destroy = trusted_destroy,
> + .describe = user_describe,
> + .read = trusted_read,
> +};
> +EXPORT_SYMBOL_GPL(key_type_trusted);
> +
> +static int __init init_trusted(void)
> +{
> + int ret;
> +
> + /*
> + * encrypted_keys.ko depends on successful load of this module even if
> + * trusted key implementation is not found.
> + */
> + if (!trusted_key_ops)
> + return 0;
> +
> + ret = trusted_key_ops->init();
> + if (ret == -ENODEV)
> + return 0;
> +
> + return ret;
> +}
> +
> +static void __exit cleanup_trusted(void)
> +{
> + trusted_key_ops->exit();
> +}
> +
> +late_initcall(init_trusted);
> +module_exit(cleanup_trusted);
> +
> +MODULE_LICENSE("GPL");
> diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> index 8001ab0..e378b27 100644
> --- a/security/keys/trusted-keys/trusted_tpm1.c
> +++ b/security/keys/trusted-keys/trusted_tpm1.c
> @@ -1,29 +1,26 @@
> // SPDX-License-Identifier: GPL-2.0-only
> /*
> * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> *
> * Author:
> * David Safford <safford@us.ibm.com>
> + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> *
> * See Documentation/security/keys/trusted-encrypted.rst
> */
>
> #include <crypto/hash_info.h>
> -#include <linux/uaccess.h>
> -#include <linux/module.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/parser.h>
> #include <linux/string.h>
> #include <linux/err.h>
> -#include <keys/user-type.h>
> #include <keys/trusted-type.h>
> #include <linux/key-type.h>
> -#include <linux/rcupdate.h>
> #include <linux/crypto.h>
> #include <crypto/hash.h>
> #include <crypto/sha.h>
> -#include <linux/capability.h>
> #include <linux/tpm.h>
> #include <linux/tpm_command.h>
>
> @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
>
> enum {
> Opt_err,
> - Opt_new, Opt_load, Opt_update,
> Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> Opt_hash,
> @@ -712,9 +708,6 @@ enum {
> };
>
> static const match_table_t key_tokens = {
> - {Opt_new, "new"},
> - {Opt_load, "load"},
> - {Opt_update, "update"},
> {Opt_keyhandle, "keyhandle=%s"},
> {Opt_keyauth, "keyauth=%s"},
> {Opt_blobauth, "blobauth=%s"},
> @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> return 0;
> }
>
> -/*
> - * datablob_parse - parse the keyctl data and fill in the
> - * payload and options structures
> - *
> - * On success returns 0, otherwise -EINVAL.
> - */
> -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> - struct trusted_key_options *o)
> -{
> - substring_t args[MAX_OPT_ARGS];
> - long keylen;
> - int ret = -EINVAL;
> - int key_cmd;
> - char *c;
> -
> - /* main command */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - key_cmd = match_token(c, key_tokens, args);
> - switch (key_cmd) {
> - case Opt_new:
> - /* first argument is key size */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - ret = kstrtol(c, 10, &keylen);
> - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> - return -EINVAL;
> - p->key_len = keylen;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_new;
> - break;
> - case Opt_load:
> - /* first argument is sealed blob */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - p->blob_len = strlen(c) / 2;
> - if (p->blob_len > MAX_BLOB_SIZE)
> - return -EINVAL;
> - ret = hex2bin(p->blob, c, p->blob_len);
> - if (ret < 0)
> - return -EINVAL;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_load;
> - break;
> - case Opt_update:
> - /* all arguments are options */
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_update;
> - break;
> - case Opt_err:
> - return -EINVAL;
> - break;
> - }
> - return ret;
> -}
> -
> static struct trusted_key_options *trusted_options_alloc(void)
> {
> struct trusted_key_options *options;
> @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> return options;
> }
>
> -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p = NULL;
> - int ret;
> -
> - ret = key_payload_reserve(key, sizeof *p);
> - if (ret < 0)
> - return p;
> - p = kzalloc(sizeof *p, GFP_KERNEL);
> - if (p)
> - p->migratable = 1; /* migratable by default */
> - return p;
> -}
> -
> -/*
> - * trusted_instantiate - create a new trusted key
> - *
> - * Unseal an existing trusted blob or, for a new key, get a
> - * random key, then seal and create a trusted key-type key,
> - * adding it to the specified keyring.
> - *
> - * On success, return 0. Otherwise return errno.
> - */
> -static int trusted_instantiate(struct key *key,
> - struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *payload = NULL;
> struct trusted_key_options *options = NULL;
> - size_t datalen = prep->datalen;
> - char *datablob;
> int ret = 0;
> - int key_cmd;
> - size_t key_len;
> int tpm2;
>
> tpm2 = tpm_is_tpm2(chip);
> if (tpm2 < 0)
> return tpm2;
>
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> -
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> - return -ENOMEM;
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> -
> options = trusted_options_alloc();
> - if (!options) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - payload = trusted_payload_alloc(key);
> - if (!payload) {
> - ret = -ENOMEM;
> - goto out;
> - }
> + if (!options)
> + return -ENOMEM;
>
> - key_cmd = datablob_parse(datablob, payload, options);
> - if (key_cmd < 0) {
> - ret = key_cmd;
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> if (!options->keyhandle) {
> ret = -EINVAL;
> goto out;
> }
>
> - dump_payload(payload);
> - dump_options(options);
> + if (tpm2)
> + ret = tpm2_seal_trusted(chip, p, options);
> + else
> + ret = key_seal(p, options);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> + goto out;
> + }
>
> - switch (key_cmd) {
> - case Opt_load:
> - if (tpm2)
> - ret = tpm2_unseal_trusted(chip, payload, options);
> - else
> - ret = key_unseal(payload, options);
> - dump_payload(payload);
> - dump_options(options);
> - if (ret < 0)
> - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> - break;
> - case Opt_new:
> - key_len = payload->key_len;
> - ret = tpm_get_random(chip, payload->key, key_len);
> - if (ret != key_len) {
> - pr_info("trusted_key: key_create failed (%d)\n", ret);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> - if (tpm2)
> - ret = tpm2_seal_trusted(chip, payload, options);
> - else
> - ret = key_seal(payload, options);
> - if (ret < 0)
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - break;
> - default:
> - ret = -EINVAL;
> - goto out;
> }
> - if (!ret && options->pcrlock)
> - ret = pcrlock(options->pcrlock);
> out:
> - kzfree(datablob);
> kzfree(options);
> - if (!ret)
> - rcu_assign_keypointer(key, payload);
> - else
> - kzfree(payload);
> return ret;
> }
>
> -static void trusted_rcu_free(struct rcu_head *rcu)
> +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p;
> -
> - p = container_of(rcu, struct trusted_key_payload, rcu);
> - kzfree(p);
> -}
> -
> -/*
> - * trusted_update - reseal an existing key with new PCR values
> - */
> -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *p;
> - struct trusted_key_payload *new_p;
> - struct trusted_key_options *new_o;
> - size_t datalen = prep->datalen;
> - char *datablob;
> + struct trusted_key_options *options = NULL;
> int ret = 0;
> + int tpm2;
>
> - if (key_is_negative(key))
> - return -ENOKEY;
> - p = key->payload.data[0];
> - if (!p->migratable)
> - return -EPERM;
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> + tpm2 = tpm_is_tpm2(chip);
> + if (tpm2 < 0)
> + return tpm2;
>
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> + options = trusted_options_alloc();
> + if (!options)
> return -ENOMEM;
> - new_o = trusted_options_alloc();
> - if (!new_o) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - new_p = trusted_payload_alloc(key);
> - if (!new_p) {
> - ret = -ENOMEM;
> - goto out;
> - }
>
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> - ret = datablob_parse(datablob, new_p, new_o);
> - if (ret != Opt_update) {
> - ret = -EINVAL;
> - kzfree(new_p);
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> - if (!new_o->keyhandle) {
> + if (!options->keyhandle) {
> ret = -EINVAL;
> - kzfree(new_p);
> goto out;
> }
>
> - /* copy old key values, and reseal with new pcrs */
> - new_p->migratable = p->migratable;
> - new_p->key_len = p->key_len;
> - memcpy(new_p->key, p->key, p->key_len);
> - dump_payload(p);
> - dump_payload(new_p);
> + if (tpm2)
> + ret = tpm2_unseal_trusted(chip, p, options);
> + else
> + ret = key_unseal(p, options);
> + if (ret < 0)
> + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
>
> - ret = key_seal(new_p, new_o);
> - if (ret < 0) {
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - kzfree(new_p);
> - goto out;
> - }
> - if (new_o->pcrlock) {
> - ret = pcrlock(new_o->pcrlock);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> if (ret < 0) {
> - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> - kzfree(new_p);
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> }
> - rcu_assign_keypointer(key, new_p);
> - call_rcu(&p->rcu, trusted_rcu_free);
> out:
> - kzfree(datablob);
> - kzfree(new_o);
> + kzfree(options);
> return ret;
> }
>
> -/*
> - * trusted_read - copy the sealed blob data to userspace in hex.
> - * On success, return to userspace the trusted key datablob size.
> - */
> -static long trusted_read(const struct key *key, char *buffer,
> - size_t buflen)
> -{
> - const struct trusted_key_payload *p;
> - char *bufp;
> - int i;
> -
> - p = dereference_key_locked(key);
> - if (!p)
> - return -EINVAL;
> -
> - if (buffer && buflen >= 2 * p->blob_len) {
> - bufp = buffer;
> - for (i = 0; i < p->blob_len; i++)
> - bufp = hex_byte_pack(bufp, p->blob[i]);
> - }
> - return 2 * p->blob_len;
> -}
> -
> -/*
> - * trusted_destroy - clear and free the key's payload
> - */
> -static void trusted_destroy(struct key *key)
> +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> {
> - kzfree(key->payload.data[0]);
> + return tpm_get_random(chip, key, key_len);
> }
>
> -struct key_type key_type_trusted = {
> - .name = "trusted",
> - .instantiate = trusted_instantiate,
> - .update = trusted_update,
> - .destroy = trusted_destroy,
> - .describe = user_describe,
> - .read = trusted_read,
> -};
> -
> -EXPORT_SYMBOL_GPL(key_type_trusted);
> -
> static void trusted_shash_release(void)
> {
> if (hashalg)
> @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
>
> hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> if (IS_ERR(hmacalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hmac_alg);
> return PTR_ERR(hmacalg);
> }
>
> hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> if (IS_ERR(hashalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hash_alg);
> ret = PTR_ERR(hashalg);
> goto hashalg_fail;
> @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> return 0;
> }
>
> -static int __init init_trusted(void)
> +static int __init init_tpm_trusted(void)
> {
> int ret;
>
> - /* encrypted_keys.ko depends on successful load of this module even if
> - * TPM is not used.
> - */
> chip = tpm_default_chip();
> if (!chip)
> - return 0;
> + return -ENODEV;
>
> ret = init_digests();
> if (ret < 0)
> @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> return ret;
> }
>
> -static void __exit cleanup_trusted(void)
> +static void __exit exit_tpm_trusted(void)
> {
> if (chip) {
> put_device(&chip->dev);
> @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> }
> }
>
> -late_initcall(init_trusted);
> -module_exit(cleanup_trusted);
> -
> -MODULE_LICENSE("GPL");
> +struct trusted_key_ops tpm_trusted_key_ops = {
> + .migratable = 1, /* migratable by default */
> + .init = init_tpm_trusted,
> + .seal = tpm_trusted_seal,
> + .unseal = tpm_trusted_unseal,
> + .get_random = tpm_trusted_get_random,
> + .exit = exit_tpm_trusted,
> +};
> +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
> 2.7.4
>
/Jarkko
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [op-tee] [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-15 18:24 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-15 18:24 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 23852 bytes --]
On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> index 47c0415..22632c6 100644
> --- a/security/keys/Kconfig
> +++ b/security/keys/Kconfig
> @@ -72,17 +72,26 @@ config BIG_KEYS
>
> config TRUSTED_KEYS
> tristate "TRUSTED KEYS"
> - depends on KEYS && TCG_TPM
> + depends on KEYS
> + help
> + This option provides support for creating, sealing, and unsealing
> + keys in the kernel. Trusted keys are random number symmetric keys,
> + generated and sealed by a trust source (TPM or TEE). Userspace will
> + only ever see encrypted blobs.
> +
> + If you are unsure as to whether this is required, answer N.
> +
> +config TRUSTED_TPM
TRUSTED_TPM_KEYS
> + bool "TPM based TRUSTED KEYS"
> + depends on TRUSTED_KEYS && TCG_TPM
> select CRYPTO
> select CRYPTO_HMAC
> select CRYPTO_SHA1
> select CRYPTO_HASH_INFO
> help
> - This option provides support for creating, sealing, and unsealing
> - keys in the kernel. Trusted keys are random number symmetric keys,
> - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> - if the boot PCRs and other criteria match. Userspace will only ever
> - see encrypted blobs.
> + This option provides support for TPM based trusted keys where TPM acts
> + as the trust source. The keys are RSA-sealed by the TPM and it only
> + unseals the keys, if the boot PCRs and other criteria match.
>
> If you are unsure as to whether this is required, answer N.
>
> diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> index 7b73ceb..03c3ad4 100644
> --- a/security/keys/trusted-keys/Makefile
> +++ b/security/keys/trusted-keys/Makefile
> @@ -4,5 +4,6 @@
> #
>
> obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> -trusted-y += trusted_tpm1.o
> -trusted-y += trusted_tpm2.o
> +trusted-y += trusted_core.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> new file mode 100644
> index 0000000..3d07d27
> --- /dev/null
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -0,0 +1,319 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> + *
> + * Author:
> + * David Safford <safford@us.ibm.com>
> + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
Rather remove David's because
1. Git log gives a verifiable and exact log of authority.
2. David's commit was done post-epoch of Git.
> + *
> + * See Documentation/security/keys/trusted-encrypted.rst
> + */
> +
> +#include <keys/user-type.h>
> +#include <keys/trusted-type.h>
> +#include <linux/capability.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/key-type.h>
> +#include <linux/module.h>
> +#include <linux/parser.h>
> +#include <linux/rcupdate.h>
> +#include <linux/slab.h>
> +#include <linux/string.h>
> +#include <linux/uaccess.h>
> +
> +#if defined(CONFIG_TRUSTED_TPM)
> +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> +#else
> +static struct trusted_key_ops *trusted_key_ops;
> +#endif
You should just have
extern trusted_key_ops *trusted_key_ops;
> +
> +enum {
> + Opt_err,
> + Opt_new, Opt_load, Opt_update,
> +};
> +
> +static const match_table_t key_tokens = {
> + {Opt_new, "new"},
> + {Opt_load, "load"},
> + {Opt_update, "update"},
> + {Opt_err, NULL}
> +};
> +
> +/*
> + * datablob_parse - parse the keyctl data and fill in the
> + * payload structure
> + *
> + * On success returns 0, otherwise -EINVAL.
> + */
> +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> +{
> + substring_t args[MAX_OPT_ARGS];
> + long keylen;
> + int ret = -EINVAL;
> + int key_cmd;
> + char *c;
> +
> + /* main command */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + key_cmd = match_token(c, key_tokens, args);
> + switch (key_cmd) {
> + case Opt_new:
> + /* first argument is key size */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + ret = kstrtol(c, 10, &keylen);
> + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> + return -EINVAL;
> + p->key_len = keylen;
> + ret = Opt_new;
> + break;
> + case Opt_load:
> + /* first argument is sealed blob */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + p->blob_len = strlen(c) / 2;
> + if (p->blob_len > MAX_BLOB_SIZE)
> + return -EINVAL;
> + ret = hex2bin(p->blob, c, p->blob_len);
> + if (ret < 0)
> + return -EINVAL;
> + ret = Opt_load;
> + break;
> + case Opt_update:
> + ret = Opt_update;
> + break;
> + case Opt_err:
> + return -EINVAL;
> + }
> + return ret;
> +}
> +
> +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +{
> + struct trusted_key_payload *p = NULL;
> + int ret;
> +
> + ret = key_payload_reserve(key, sizeof(*p));
> + if (ret < 0)
> + return p;
> + p = kzalloc(sizeof(*p), GFP_KERNEL);
> +
> + p->migratable = trusted_key_ops->migratable;
> +
> + return p;
> +}
> +
> +/*
> + * trusted_instantiate - create a new trusted key
> + *
> + * Unseal an existing trusted blob or, for a new key, get a
> + * random key, then seal and create a trusted key-type key,
> + * adding it to the specified keyring.
> + *
> + * On success, return 0. Otherwise return errno.
> + */
> +static int trusted_instantiate(struct key *key,
> + struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *payload = NULL;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> + int key_cmd;
> + size_t key_len;
> +
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> +
> + payload = trusted_payload_alloc(key);
> + if (!payload) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + key_cmd = datablob_parse(datablob, payload);
> + if (key_cmd < 0) {
> + ret = key_cmd;
> + goto out;
> + }
> +
> + dump_payload(payload);
> +
> + switch (key_cmd) {
> + case Opt_load:
> + ret = trusted_key_ops->unseal(payload, datablob);
> + dump_payload(payload);
> + if (ret < 0)
> + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> + break;
> + case Opt_new:
> + key_len = payload->key_len;
> + ret = trusted_key_ops->get_random(payload->key, key_len);
> + if (ret != key_len) {
> + pr_info("trusted_key: key_create failed (%d)\n", ret);
> + goto out;
> + }
> +
> + ret = trusted_key_ops->seal(payload, datablob);
> + if (ret < 0)
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> +out:
> + kzfree(datablob);
> + if (!ret)
> + rcu_assign_keypointer(key, payload);
> + else
> + kzfree(payload);
> + return ret;
> +}
> +
> +static void trusted_rcu_free(struct rcu_head *rcu)
> +{
> + struct trusted_key_payload *p;
> +
> + p = container_of(rcu, struct trusted_key_payload, rcu);
> + kzfree(p);
> +}
> +
> +/*
> + * trusted_update - reseal an existing key with new PCR values
> + */
> +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *p;
> + struct trusted_key_payload *new_p;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> +
> + if (key_is_negative(key))
> + return -ENOKEY;
> + p = key->payload.data[0];
> + if (!p->migratable)
> + return -EPERM;
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> +
> + new_p = trusted_payload_alloc(key);
> + if (!new_p) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> + ret = datablob_parse(datablob, new_p);
> + if (ret != Opt_update) {
> + ret = -EINVAL;
> + kzfree(new_p);
> + goto out;
> + }
> +
> + /* copy old key values, and reseal with new pcrs */
> + new_p->migratable = p->migratable;
> + new_p->key_len = p->key_len;
> + memcpy(new_p->key, p->key, p->key_len);
> + dump_payload(p);
> + dump_payload(new_p);
> +
> + ret = trusted_key_ops->seal(new_p, datablob);
> + if (ret < 0) {
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + kzfree(new_p);
> + goto out;
> + }
> +
> + rcu_assign_keypointer(key, new_p);
> + call_rcu(&p->rcu, trusted_rcu_free);
> +out:
> + kzfree(datablob);
> + return ret;
> +}
> +
> +/*
> + * trusted_read - copy the sealed blob data to userspace in hex.
> + * On success, return to userspace the trusted key datablob size.
> + */
> +static long trusted_read(const struct key *key, char *buffer,
> + size_t buflen)
> +{
> + const struct trusted_key_payload *p;
> + char *bufp;
> + int i;
> +
> + p = dereference_key_locked(key);
> + if (!p)
> + return -EINVAL;
> +
> + if (buffer && buflen >= 2 * p->blob_len) {
> + bufp = buffer;
> + for (i = 0; i < p->blob_len; i++)
> + bufp = hex_byte_pack(bufp, p->blob[i]);
> + }
> + return 2 * p->blob_len;
> +}
> +
> +/*
> + * trusted_destroy - clear and free the key's payload
> + */
> +static void trusted_destroy(struct key *key)
> +{
> + kzfree(key->payload.data[0]);
> +}
> +
> +struct key_type key_type_trusted = {
> + .name = "trusted",
> + .instantiate = trusted_instantiate,
> + .update = trusted_update,
> + .destroy = trusted_destroy,
> + .describe = user_describe,
> + .read = trusted_read,
> +};
> +EXPORT_SYMBOL_GPL(key_type_trusted);
> +
> +static int __init init_trusted(void)
> +{
> + int ret;
> +
> + /*
> + * encrypted_keys.ko depends on successful load of this module even if
> + * trusted key implementation is not found.
> + */
> + if (!trusted_key_ops)
> + return 0;
> +
> + ret = trusted_key_ops->init();
> + if (ret == -ENODEV)
> + return 0;
> +
> + return ret;
> +}
> +
> +static void __exit cleanup_trusted(void)
> +{
> + trusted_key_ops->exit();
> +}
> +
> +late_initcall(init_trusted);
> +module_exit(cleanup_trusted);
> +
> +MODULE_LICENSE("GPL");
> diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> index 8001ab0..e378b27 100644
> --- a/security/keys/trusted-keys/trusted_tpm1.c
> +++ b/security/keys/trusted-keys/trusted_tpm1.c
> @@ -1,29 +1,26 @@
> // SPDX-License-Identifier: GPL-2.0-only
> /*
> * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> *
> * Author:
> * David Safford <safford@us.ibm.com>
> + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> *
> * See Documentation/security/keys/trusted-encrypted.rst
> */
>
> #include <crypto/hash_info.h>
> -#include <linux/uaccess.h>
> -#include <linux/module.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/parser.h>
> #include <linux/string.h>
> #include <linux/err.h>
> -#include <keys/user-type.h>
> #include <keys/trusted-type.h>
> #include <linux/key-type.h>
> -#include <linux/rcupdate.h>
> #include <linux/crypto.h>
> #include <crypto/hash.h>
> #include <crypto/sha.h>
> -#include <linux/capability.h>
> #include <linux/tpm.h>
> #include <linux/tpm_command.h>
>
> @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
>
> enum {
> Opt_err,
> - Opt_new, Opt_load, Opt_update,
> Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> Opt_hash,
> @@ -712,9 +708,6 @@ enum {
> };
>
> static const match_table_t key_tokens = {
> - {Opt_new, "new"},
> - {Opt_load, "load"},
> - {Opt_update, "update"},
> {Opt_keyhandle, "keyhandle=%s"},
> {Opt_keyauth, "keyauth=%s"},
> {Opt_blobauth, "blobauth=%s"},
> @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> return 0;
> }
>
> -/*
> - * datablob_parse - parse the keyctl data and fill in the
> - * payload and options structures
> - *
> - * On success returns 0, otherwise -EINVAL.
> - */
> -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> - struct trusted_key_options *o)
> -{
> - substring_t args[MAX_OPT_ARGS];
> - long keylen;
> - int ret = -EINVAL;
> - int key_cmd;
> - char *c;
> -
> - /* main command */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - key_cmd = match_token(c, key_tokens, args);
> - switch (key_cmd) {
> - case Opt_new:
> - /* first argument is key size */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - ret = kstrtol(c, 10, &keylen);
> - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> - return -EINVAL;
> - p->key_len = keylen;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_new;
> - break;
> - case Opt_load:
> - /* first argument is sealed blob */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - p->blob_len = strlen(c) / 2;
> - if (p->blob_len > MAX_BLOB_SIZE)
> - return -EINVAL;
> - ret = hex2bin(p->blob, c, p->blob_len);
> - if (ret < 0)
> - return -EINVAL;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_load;
> - break;
> - case Opt_update:
> - /* all arguments are options */
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_update;
> - break;
> - case Opt_err:
> - return -EINVAL;
> - break;
> - }
> - return ret;
> -}
> -
> static struct trusted_key_options *trusted_options_alloc(void)
> {
> struct trusted_key_options *options;
> @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> return options;
> }
>
> -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p = NULL;
> - int ret;
> -
> - ret = key_payload_reserve(key, sizeof *p);
> - if (ret < 0)
> - return p;
> - p = kzalloc(sizeof *p, GFP_KERNEL);
> - if (p)
> - p->migratable = 1; /* migratable by default */
> - return p;
> -}
> -
> -/*
> - * trusted_instantiate - create a new trusted key
> - *
> - * Unseal an existing trusted blob or, for a new key, get a
> - * random key, then seal and create a trusted key-type key,
> - * adding it to the specified keyring.
> - *
> - * On success, return 0. Otherwise return errno.
> - */
> -static int trusted_instantiate(struct key *key,
> - struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *payload = NULL;
> struct trusted_key_options *options = NULL;
> - size_t datalen = prep->datalen;
> - char *datablob;
> int ret = 0;
> - int key_cmd;
> - size_t key_len;
> int tpm2;
>
> tpm2 = tpm_is_tpm2(chip);
> if (tpm2 < 0)
> return tpm2;
>
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> -
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> - return -ENOMEM;
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> -
> options = trusted_options_alloc();
> - if (!options) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - payload = trusted_payload_alloc(key);
> - if (!payload) {
> - ret = -ENOMEM;
> - goto out;
> - }
> + if (!options)
> + return -ENOMEM;
>
> - key_cmd = datablob_parse(datablob, payload, options);
> - if (key_cmd < 0) {
> - ret = key_cmd;
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> if (!options->keyhandle) {
> ret = -EINVAL;
> goto out;
> }
>
> - dump_payload(payload);
> - dump_options(options);
> + if (tpm2)
> + ret = tpm2_seal_trusted(chip, p, options);
> + else
> + ret = key_seal(p, options);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> + goto out;
> + }
>
> - switch (key_cmd) {
> - case Opt_load:
> - if (tpm2)
> - ret = tpm2_unseal_trusted(chip, payload, options);
> - else
> - ret = key_unseal(payload, options);
> - dump_payload(payload);
> - dump_options(options);
> - if (ret < 0)
> - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> - break;
> - case Opt_new:
> - key_len = payload->key_len;
> - ret = tpm_get_random(chip, payload->key, key_len);
> - if (ret != key_len) {
> - pr_info("trusted_key: key_create failed (%d)\n", ret);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> - if (tpm2)
> - ret = tpm2_seal_trusted(chip, payload, options);
> - else
> - ret = key_seal(payload, options);
> - if (ret < 0)
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - break;
> - default:
> - ret = -EINVAL;
> - goto out;
> }
> - if (!ret && options->pcrlock)
> - ret = pcrlock(options->pcrlock);
> out:
> - kzfree(datablob);
> kzfree(options);
> - if (!ret)
> - rcu_assign_keypointer(key, payload);
> - else
> - kzfree(payload);
> return ret;
> }
>
> -static void trusted_rcu_free(struct rcu_head *rcu)
> +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p;
> -
> - p = container_of(rcu, struct trusted_key_payload, rcu);
> - kzfree(p);
> -}
> -
> -/*
> - * trusted_update - reseal an existing key with new PCR values
> - */
> -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *p;
> - struct trusted_key_payload *new_p;
> - struct trusted_key_options *new_o;
> - size_t datalen = prep->datalen;
> - char *datablob;
> + struct trusted_key_options *options = NULL;
> int ret = 0;
> + int tpm2;
>
> - if (key_is_negative(key))
> - return -ENOKEY;
> - p = key->payload.data[0];
> - if (!p->migratable)
> - return -EPERM;
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> + tpm2 = tpm_is_tpm2(chip);
> + if (tpm2 < 0)
> + return tpm2;
>
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> + options = trusted_options_alloc();
> + if (!options)
> return -ENOMEM;
> - new_o = trusted_options_alloc();
> - if (!new_o) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - new_p = trusted_payload_alloc(key);
> - if (!new_p) {
> - ret = -ENOMEM;
> - goto out;
> - }
>
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> - ret = datablob_parse(datablob, new_p, new_o);
> - if (ret != Opt_update) {
> - ret = -EINVAL;
> - kzfree(new_p);
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> - if (!new_o->keyhandle) {
> + if (!options->keyhandle) {
> ret = -EINVAL;
> - kzfree(new_p);
> goto out;
> }
>
> - /* copy old key values, and reseal with new pcrs */
> - new_p->migratable = p->migratable;
> - new_p->key_len = p->key_len;
> - memcpy(new_p->key, p->key, p->key_len);
> - dump_payload(p);
> - dump_payload(new_p);
> + if (tpm2)
> + ret = tpm2_unseal_trusted(chip, p, options);
> + else
> + ret = key_unseal(p, options);
> + if (ret < 0)
> + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
>
> - ret = key_seal(new_p, new_o);
> - if (ret < 0) {
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - kzfree(new_p);
> - goto out;
> - }
> - if (new_o->pcrlock) {
> - ret = pcrlock(new_o->pcrlock);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> if (ret < 0) {
> - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> - kzfree(new_p);
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> }
> - rcu_assign_keypointer(key, new_p);
> - call_rcu(&p->rcu, trusted_rcu_free);
> out:
> - kzfree(datablob);
> - kzfree(new_o);
> + kzfree(options);
> return ret;
> }
>
> -/*
> - * trusted_read - copy the sealed blob data to userspace in hex.
> - * On success, return to userspace the trusted key datablob size.
> - */
> -static long trusted_read(const struct key *key, char *buffer,
> - size_t buflen)
> -{
> - const struct trusted_key_payload *p;
> - char *bufp;
> - int i;
> -
> - p = dereference_key_locked(key);
> - if (!p)
> - return -EINVAL;
> -
> - if (buffer && buflen >= 2 * p->blob_len) {
> - bufp = buffer;
> - for (i = 0; i < p->blob_len; i++)
> - bufp = hex_byte_pack(bufp, p->blob[i]);
> - }
> - return 2 * p->blob_len;
> -}
> -
> -/*
> - * trusted_destroy - clear and free the key's payload
> - */
> -static void trusted_destroy(struct key *key)
> +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> {
> - kzfree(key->payload.data[0]);
> + return tpm_get_random(chip, key, key_len);
> }
>
> -struct key_type key_type_trusted = {
> - .name = "trusted",
> - .instantiate = trusted_instantiate,
> - .update = trusted_update,
> - .destroy = trusted_destroy,
> - .describe = user_describe,
> - .read = trusted_read,
> -};
> -
> -EXPORT_SYMBOL_GPL(key_type_trusted);
> -
> static void trusted_shash_release(void)
> {
> if (hashalg)
> @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
>
> hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> if (IS_ERR(hmacalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hmac_alg);
> return PTR_ERR(hmacalg);
> }
>
> hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> if (IS_ERR(hashalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hash_alg);
> ret = PTR_ERR(hashalg);
> goto hashalg_fail;
> @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> return 0;
> }
>
> -static int __init init_trusted(void)
> +static int __init init_tpm_trusted(void)
> {
> int ret;
>
> - /* encrypted_keys.ko depends on successful load of this module even if
> - * TPM is not used.
> - */
> chip = tpm_default_chip();
> if (!chip)
> - return 0;
> + return -ENODEV;
>
> ret = init_digests();
> if (ret < 0)
> @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> return ret;
> }
>
> -static void __exit cleanup_trusted(void)
> +static void __exit exit_tpm_trusted(void)
> {
> if (chip) {
> put_device(&chip->dev);
> @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> }
> }
>
> -late_initcall(init_trusted);
> -module_exit(cleanup_trusted);
> -
> -MODULE_LICENSE("GPL");
> +struct trusted_key_ops tpm_trusted_key_ops = {
> + .migratable = 1, /* migratable by default */
> + .init = init_tpm_trusted,
> + .seal = tpm_trusted_seal,
> + .unseal = tpm_trusted_unseal,
> + .get_random = tpm_trusted_get_random,
> + .exit = exit_tpm_trusted,
> +};
> +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
> 2.7.4
>
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-15 18:24 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-15 18:24 UTC (permalink / raw)
To: Sumit Garg
Cc: zohar, jejb, dhowells, jens.wiklander, corbet, jmorris, serge,
casey, janne.karhunen, daniel.thompson, Markus.Wamser, keyrings,
linux-integrity, linux-security-module, linux-doc, linux-kernel,
linux-arm-kernel, op-tee, tee-dev
On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> index 47c0415..22632c6 100644
> --- a/security/keys/Kconfig
> +++ b/security/keys/Kconfig
> @@ -72,17 +72,26 @@ config BIG_KEYS
>
> config TRUSTED_KEYS
> tristate "TRUSTED KEYS"
> - depends on KEYS && TCG_TPM
> + depends on KEYS
> + help
> + This option provides support for creating, sealing, and unsealing
> + keys in the kernel. Trusted keys are random number symmetric keys,
> + generated and sealed by a trust source (TPM or TEE). Userspace will
> + only ever see encrypted blobs.
> +
> + If you are unsure as to whether this is required, answer N.
> +
> +config TRUSTED_TPM
TRUSTED_TPM_KEYS
> + bool "TPM based TRUSTED KEYS"
> + depends on TRUSTED_KEYS && TCG_TPM
> select CRYPTO
> select CRYPTO_HMAC
> select CRYPTO_SHA1
> select CRYPTO_HASH_INFO
> help
> - This option provides support for creating, sealing, and unsealing
> - keys in the kernel. Trusted keys are random number symmetric keys,
> - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> - if the boot PCRs and other criteria match. Userspace will only ever
> - see encrypted blobs.
> + This option provides support for TPM based trusted keys where TPM acts
> + as the trust source. The keys are RSA-sealed by the TPM and it only
> + unseals the keys, if the boot PCRs and other criteria match.
>
> If you are unsure as to whether this is required, answer N.
>
> diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> index 7b73ceb..03c3ad4 100644
> --- a/security/keys/trusted-keys/Makefile
> +++ b/security/keys/trusted-keys/Makefile
> @@ -4,5 +4,6 @@
> #
>
> obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> -trusted-y += trusted_tpm1.o
> -trusted-y += trusted_tpm2.o
> +trusted-y += trusted_core.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> new file mode 100644
> index 0000000..3d07d27
> --- /dev/null
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -0,0 +1,319 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> + *
> + * Author:
> + * David Safford <safford@us.ibm.com>
> + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
Rather remove David's because
1. Git log gives a verifiable and exact log of authority.
2. David's commit was done post-epoch of Git.
> + *
> + * See Documentation/security/keys/trusted-encrypted.rst
> + */
> +
> +#include <keys/user-type.h>
> +#include <keys/trusted-type.h>
> +#include <linux/capability.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/key-type.h>
> +#include <linux/module.h>
> +#include <linux/parser.h>
> +#include <linux/rcupdate.h>
> +#include <linux/slab.h>
> +#include <linux/string.h>
> +#include <linux/uaccess.h>
> +
> +#if defined(CONFIG_TRUSTED_TPM)
> +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> +#else
> +static struct trusted_key_ops *trusted_key_ops;
> +#endif
You should just have
extern trusted_key_ops *trusted_key_ops;
> +
> +enum {
> + Opt_err,
> + Opt_new, Opt_load, Opt_update,
> +};
> +
> +static const match_table_t key_tokens = {
> + {Opt_new, "new"},
> + {Opt_load, "load"},
> + {Opt_update, "update"},
> + {Opt_err, NULL}
> +};
> +
> +/*
> + * datablob_parse - parse the keyctl data and fill in the
> + * payload structure
> + *
> + * On success returns 0, otherwise -EINVAL.
> + */
> +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> +{
> + substring_t args[MAX_OPT_ARGS];
> + long keylen;
> + int ret = -EINVAL;
> + int key_cmd;
> + char *c;
> +
> + /* main command */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + key_cmd = match_token(c, key_tokens, args);
> + switch (key_cmd) {
> + case Opt_new:
> + /* first argument is key size */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + ret = kstrtol(c, 10, &keylen);
> + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> + return -EINVAL;
> + p->key_len = keylen;
> + ret = Opt_new;
> + break;
> + case Opt_load:
> + /* first argument is sealed blob */
> + c = strsep(&datablob, " \t");
> + if (!c)
> + return -EINVAL;
> + p->blob_len = strlen(c) / 2;
> + if (p->blob_len > MAX_BLOB_SIZE)
> + return -EINVAL;
> + ret = hex2bin(p->blob, c, p->blob_len);
> + if (ret < 0)
> + return -EINVAL;
> + ret = Opt_load;
> + break;
> + case Opt_update:
> + ret = Opt_update;
> + break;
> + case Opt_err:
> + return -EINVAL;
> + }
> + return ret;
> +}
> +
> +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +{
> + struct trusted_key_payload *p = NULL;
> + int ret;
> +
> + ret = key_payload_reserve(key, sizeof(*p));
> + if (ret < 0)
> + return p;
> + p = kzalloc(sizeof(*p), GFP_KERNEL);
> +
> + p->migratable = trusted_key_ops->migratable;
> +
> + return p;
> +}
> +
> +/*
> + * trusted_instantiate - create a new trusted key
> + *
> + * Unseal an existing trusted blob or, for a new key, get a
> + * random key, then seal and create a trusted key-type key,
> + * adding it to the specified keyring.
> + *
> + * On success, return 0. Otherwise return errno.
> + */
> +static int trusted_instantiate(struct key *key,
> + struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *payload = NULL;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> + int key_cmd;
> + size_t key_len;
> +
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> +
> + payload = trusted_payload_alloc(key);
> + if (!payload) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + key_cmd = datablob_parse(datablob, payload);
> + if (key_cmd < 0) {
> + ret = key_cmd;
> + goto out;
> + }
> +
> + dump_payload(payload);
> +
> + switch (key_cmd) {
> + case Opt_load:
> + ret = trusted_key_ops->unseal(payload, datablob);
> + dump_payload(payload);
> + if (ret < 0)
> + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> + break;
> + case Opt_new:
> + key_len = payload->key_len;
> + ret = trusted_key_ops->get_random(payload->key, key_len);
> + if (ret != key_len) {
> + pr_info("trusted_key: key_create failed (%d)\n", ret);
> + goto out;
> + }
> +
> + ret = trusted_key_ops->seal(payload, datablob);
> + if (ret < 0)
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> +out:
> + kzfree(datablob);
> + if (!ret)
> + rcu_assign_keypointer(key, payload);
> + else
> + kzfree(payload);
> + return ret;
> +}
> +
> +static void trusted_rcu_free(struct rcu_head *rcu)
> +{
> + struct trusted_key_payload *p;
> +
> + p = container_of(rcu, struct trusted_key_payload, rcu);
> + kzfree(p);
> +}
> +
> +/*
> + * trusted_update - reseal an existing key with new PCR values
> + */
> +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> +{
> + struct trusted_key_payload *p;
> + struct trusted_key_payload *new_p;
> + size_t datalen = prep->datalen;
> + char *datablob;
> + int ret = 0;
> +
> + if (key_is_negative(key))
> + return -ENOKEY;
> + p = key->payload.data[0];
> + if (!p->migratable)
> + return -EPERM;
> + if (datalen <= 0 || datalen > 32767 || !prep->data)
> + return -EINVAL;
> +
> + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> + if (!datablob)
> + return -ENOMEM;
> +
> + new_p = trusted_payload_alloc(key);
> + if (!new_p) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + memcpy(datablob, prep->data, datalen);
> + datablob[datalen] = '\0';
> + ret = datablob_parse(datablob, new_p);
> + if (ret != Opt_update) {
> + ret = -EINVAL;
> + kzfree(new_p);
> + goto out;
> + }
> +
> + /* copy old key values, and reseal with new pcrs */
> + new_p->migratable = p->migratable;
> + new_p->key_len = p->key_len;
> + memcpy(new_p->key, p->key, p->key_len);
> + dump_payload(p);
> + dump_payload(new_p);
> +
> + ret = trusted_key_ops->seal(new_p, datablob);
> + if (ret < 0) {
> + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> + kzfree(new_p);
> + goto out;
> + }
> +
> + rcu_assign_keypointer(key, new_p);
> + call_rcu(&p->rcu, trusted_rcu_free);
> +out:
> + kzfree(datablob);
> + return ret;
> +}
> +
> +/*
> + * trusted_read - copy the sealed blob data to userspace in hex.
> + * On success, return to userspace the trusted key datablob size.
> + */
> +static long trusted_read(const struct key *key, char *buffer,
> + size_t buflen)
> +{
> + const struct trusted_key_payload *p;
> + char *bufp;
> + int i;
> +
> + p = dereference_key_locked(key);
> + if (!p)
> + return -EINVAL;
> +
> + if (buffer && buflen >= 2 * p->blob_len) {
> + bufp = buffer;
> + for (i = 0; i < p->blob_len; i++)
> + bufp = hex_byte_pack(bufp, p->blob[i]);
> + }
> + return 2 * p->blob_len;
> +}
> +
> +/*
> + * trusted_destroy - clear and free the key's payload
> + */
> +static void trusted_destroy(struct key *key)
> +{
> + kzfree(key->payload.data[0]);
> +}
> +
> +struct key_type key_type_trusted = {
> + .name = "trusted",
> + .instantiate = trusted_instantiate,
> + .update = trusted_update,
> + .destroy = trusted_destroy,
> + .describe = user_describe,
> + .read = trusted_read,
> +};
> +EXPORT_SYMBOL_GPL(key_type_trusted);
> +
> +static int __init init_trusted(void)
> +{
> + int ret;
> +
> + /*
> + * encrypted_keys.ko depends on successful load of this module even if
> + * trusted key implementation is not found.
> + */
> + if (!trusted_key_ops)
> + return 0;
> +
> + ret = trusted_key_ops->init();
> + if (ret == -ENODEV)
> + return 0;
> +
> + return ret;
> +}
> +
> +static void __exit cleanup_trusted(void)
> +{
> + trusted_key_ops->exit();
> +}
> +
> +late_initcall(init_trusted);
> +module_exit(cleanup_trusted);
> +
> +MODULE_LICENSE("GPL");
> diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> index 8001ab0..e378b27 100644
> --- a/security/keys/trusted-keys/trusted_tpm1.c
> +++ b/security/keys/trusted-keys/trusted_tpm1.c
> @@ -1,29 +1,26 @@
> // SPDX-License-Identifier: GPL-2.0-only
> /*
> * Copyright (C) 2010 IBM Corporation
> + * Copyright (c) 2019, Linaro Limited
> *
> * Author:
> * David Safford <safford@us.ibm.com>
> + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> *
> * See Documentation/security/keys/trusted-encrypted.rst
> */
>
> #include <crypto/hash_info.h>
> -#include <linux/uaccess.h>
> -#include <linux/module.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/parser.h>
> #include <linux/string.h>
> #include <linux/err.h>
> -#include <keys/user-type.h>
> #include <keys/trusted-type.h>
> #include <linux/key-type.h>
> -#include <linux/rcupdate.h>
> #include <linux/crypto.h>
> #include <crypto/hash.h>
> #include <crypto/sha.h>
> -#include <linux/capability.h>
> #include <linux/tpm.h>
> #include <linux/tpm_command.h>
>
> @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
>
> enum {
> Opt_err,
> - Opt_new, Opt_load, Opt_update,
> Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> Opt_hash,
> @@ -712,9 +708,6 @@ enum {
> };
>
> static const match_table_t key_tokens = {
> - {Opt_new, "new"},
> - {Opt_load, "load"},
> - {Opt_update, "update"},
> {Opt_keyhandle, "keyhandle=%s"},
> {Opt_keyauth, "keyauth=%s"},
> {Opt_blobauth, "blobauth=%s"},
> @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> return 0;
> }
>
> -/*
> - * datablob_parse - parse the keyctl data and fill in the
> - * payload and options structures
> - *
> - * On success returns 0, otherwise -EINVAL.
> - */
> -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> - struct trusted_key_options *o)
> -{
> - substring_t args[MAX_OPT_ARGS];
> - long keylen;
> - int ret = -EINVAL;
> - int key_cmd;
> - char *c;
> -
> - /* main command */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - key_cmd = match_token(c, key_tokens, args);
> - switch (key_cmd) {
> - case Opt_new:
> - /* first argument is key size */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - ret = kstrtol(c, 10, &keylen);
> - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> - return -EINVAL;
> - p->key_len = keylen;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_new;
> - break;
> - case Opt_load:
> - /* first argument is sealed blob */
> - c = strsep(&datablob, " \t");
> - if (!c)
> - return -EINVAL;
> - p->blob_len = strlen(c) / 2;
> - if (p->blob_len > MAX_BLOB_SIZE)
> - return -EINVAL;
> - ret = hex2bin(p->blob, c, p->blob_len);
> - if (ret < 0)
> - return -EINVAL;
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_load;
> - break;
> - case Opt_update:
> - /* all arguments are options */
> - ret = getoptions(datablob, p, o);
> - if (ret < 0)
> - return ret;
> - ret = Opt_update;
> - break;
> - case Opt_err:
> - return -EINVAL;
> - break;
> - }
> - return ret;
> -}
> -
> static struct trusted_key_options *trusted_options_alloc(void)
> {
> struct trusted_key_options *options;
> @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> return options;
> }
>
> -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p = NULL;
> - int ret;
> -
> - ret = key_payload_reserve(key, sizeof *p);
> - if (ret < 0)
> - return p;
> - p = kzalloc(sizeof *p, GFP_KERNEL);
> - if (p)
> - p->migratable = 1; /* migratable by default */
> - return p;
> -}
> -
> -/*
> - * trusted_instantiate - create a new trusted key
> - *
> - * Unseal an existing trusted blob or, for a new key, get a
> - * random key, then seal and create a trusted key-type key,
> - * adding it to the specified keyring.
> - *
> - * On success, return 0. Otherwise return errno.
> - */
> -static int trusted_instantiate(struct key *key,
> - struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *payload = NULL;
> struct trusted_key_options *options = NULL;
> - size_t datalen = prep->datalen;
> - char *datablob;
> int ret = 0;
> - int key_cmd;
> - size_t key_len;
> int tpm2;
>
> tpm2 = tpm_is_tpm2(chip);
> if (tpm2 < 0)
> return tpm2;
>
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> -
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> - return -ENOMEM;
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> -
> options = trusted_options_alloc();
> - if (!options) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - payload = trusted_payload_alloc(key);
> - if (!payload) {
> - ret = -ENOMEM;
> - goto out;
> - }
> + if (!options)
> + return -ENOMEM;
>
> - key_cmd = datablob_parse(datablob, payload, options);
> - if (key_cmd < 0) {
> - ret = key_cmd;
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> if (!options->keyhandle) {
> ret = -EINVAL;
> goto out;
> }
>
> - dump_payload(payload);
> - dump_options(options);
> + if (tpm2)
> + ret = tpm2_seal_trusted(chip, p, options);
> + else
> + ret = key_seal(p, options);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> + goto out;
> + }
>
> - switch (key_cmd) {
> - case Opt_load:
> - if (tpm2)
> - ret = tpm2_unseal_trusted(chip, payload, options);
> - else
> - ret = key_unseal(payload, options);
> - dump_payload(payload);
> - dump_options(options);
> - if (ret < 0)
> - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> - break;
> - case Opt_new:
> - key_len = payload->key_len;
> - ret = tpm_get_random(chip, payload->key, key_len);
> - if (ret != key_len) {
> - pr_info("trusted_key: key_create failed (%d)\n", ret);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> + if (ret < 0) {
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> - if (tpm2)
> - ret = tpm2_seal_trusted(chip, payload, options);
> - else
> - ret = key_seal(payload, options);
> - if (ret < 0)
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - break;
> - default:
> - ret = -EINVAL;
> - goto out;
> }
> - if (!ret && options->pcrlock)
> - ret = pcrlock(options->pcrlock);
> out:
> - kzfree(datablob);
> kzfree(options);
> - if (!ret)
> - rcu_assign_keypointer(key, payload);
> - else
> - kzfree(payload);
> return ret;
> }
>
> -static void trusted_rcu_free(struct rcu_head *rcu)
> +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> {
> - struct trusted_key_payload *p;
> -
> - p = container_of(rcu, struct trusted_key_payload, rcu);
> - kzfree(p);
> -}
> -
> -/*
> - * trusted_update - reseal an existing key with new PCR values
> - */
> -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> -{
> - struct trusted_key_payload *p;
> - struct trusted_key_payload *new_p;
> - struct trusted_key_options *new_o;
> - size_t datalen = prep->datalen;
> - char *datablob;
> + struct trusted_key_options *options = NULL;
> int ret = 0;
> + int tpm2;
>
> - if (key_is_negative(key))
> - return -ENOKEY;
> - p = key->payload.data[0];
> - if (!p->migratable)
> - return -EPERM;
> - if (datalen <= 0 || datalen > 32767 || !prep->data)
> - return -EINVAL;
> + tpm2 = tpm_is_tpm2(chip);
> + if (tpm2 < 0)
> + return tpm2;
>
> - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> - if (!datablob)
> + options = trusted_options_alloc();
> + if (!options)
> return -ENOMEM;
> - new_o = trusted_options_alloc();
> - if (!new_o) {
> - ret = -ENOMEM;
> - goto out;
> - }
> - new_p = trusted_payload_alloc(key);
> - if (!new_p) {
> - ret = -ENOMEM;
> - goto out;
> - }
>
> - memcpy(datablob, prep->data, datalen);
> - datablob[datalen] = '\0';
> - ret = datablob_parse(datablob, new_p, new_o);
> - if (ret != Opt_update) {
> - ret = -EINVAL;
> - kzfree(new_p);
> + ret = getoptions(datablob, p, options);
> + if (ret < 0)
> goto out;
> - }
> + dump_options(options);
>
> - if (!new_o->keyhandle) {
> + if (!options->keyhandle) {
> ret = -EINVAL;
> - kzfree(new_p);
> goto out;
> }
>
> - /* copy old key values, and reseal with new pcrs */
> - new_p->migratable = p->migratable;
> - new_p->key_len = p->key_len;
> - memcpy(new_p->key, p->key, p->key_len);
> - dump_payload(p);
> - dump_payload(new_p);
> + if (tpm2)
> + ret = tpm2_unseal_trusted(chip, p, options);
> + else
> + ret = key_unseal(p, options);
> + if (ret < 0)
> + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
>
> - ret = key_seal(new_p, new_o);
> - if (ret < 0) {
> - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> - kzfree(new_p);
> - goto out;
> - }
> - if (new_o->pcrlock) {
> - ret = pcrlock(new_o->pcrlock);
> + if (options->pcrlock) {
> + ret = pcrlock(options->pcrlock);
> if (ret < 0) {
> - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> - kzfree(new_p);
> + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> goto out;
> }
> }
> - rcu_assign_keypointer(key, new_p);
> - call_rcu(&p->rcu, trusted_rcu_free);
> out:
> - kzfree(datablob);
> - kzfree(new_o);
> + kzfree(options);
> return ret;
> }
>
> -/*
> - * trusted_read - copy the sealed blob data to userspace in hex.
> - * On success, return to userspace the trusted key datablob size.
> - */
> -static long trusted_read(const struct key *key, char *buffer,
> - size_t buflen)
> -{
> - const struct trusted_key_payload *p;
> - char *bufp;
> - int i;
> -
> - p = dereference_key_locked(key);
> - if (!p)
> - return -EINVAL;
> -
> - if (buffer && buflen >= 2 * p->blob_len) {
> - bufp = buffer;
> - for (i = 0; i < p->blob_len; i++)
> - bufp = hex_byte_pack(bufp, p->blob[i]);
> - }
> - return 2 * p->blob_len;
> -}
> -
> -/*
> - * trusted_destroy - clear and free the key's payload
> - */
> -static void trusted_destroy(struct key *key)
> +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> {
> - kzfree(key->payload.data[0]);
> + return tpm_get_random(chip, key, key_len);
> }
>
> -struct key_type key_type_trusted = {
> - .name = "trusted",
> - .instantiate = trusted_instantiate,
> - .update = trusted_update,
> - .destroy = trusted_destroy,
> - .describe = user_describe,
> - .read = trusted_read,
> -};
> -
> -EXPORT_SYMBOL_GPL(key_type_trusted);
> -
> static void trusted_shash_release(void)
> {
> if (hashalg)
> @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
>
> hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> if (IS_ERR(hmacalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hmac_alg);
> return PTR_ERR(hmacalg);
> }
>
> hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> if (IS_ERR(hashalg)) {
> - pr_info("trusted_key: could not allocate crypto %s\n",
> + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> hash_alg);
> ret = PTR_ERR(hashalg);
> goto hashalg_fail;
> @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> return 0;
> }
>
> -static int __init init_trusted(void)
> +static int __init init_tpm_trusted(void)
> {
> int ret;
>
> - /* encrypted_keys.ko depends on successful load of this module even if
> - * TPM is not used.
> - */
> chip = tpm_default_chip();
> if (!chip)
> - return 0;
> + return -ENODEV;
>
> ret = init_digests();
> if (ret < 0)
> @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> return ret;
> }
>
> -static void __exit cleanup_trusted(void)
> +static void __exit exit_tpm_trusted(void)
> {
> if (chip) {
> put_device(&chip->dev);
> @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> }
> }
>
> -late_initcall(init_trusted);
> -module_exit(cleanup_trusted);
> -
> -MODULE_LICENSE("GPL");
> +struct trusted_key_ops tpm_trusted_key_ops = {
> + .migratable = 1, /* migratable by default */
> + .init = init_tpm_trusted,
> + .seal = tpm_trusted_seal,
> + .unseal = tpm_trusted_unseal,
> + .get_random = tpm_trusted_get_random,
> + .exit = exit_tpm_trusted,
> +};
> +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
> 2.7.4
>
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-15 18:24 ` Jarkko Sakkinen
(?)
(?)
@ 2020-06-16 13:32 ` Sumit Garg
-1 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-16 13:32 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: Mimi Zohar, James Bottomley, David Howells, Jens Wiklander,
Jonathan Corbet, James Morris, Serge E. Hallyn, Casey Schaufler,
Janne Karhunen, Daniel Thompson, Markus Wamser,
open list:ASYMMETRIC KEYS, linux-integrity, linux-security-module,
Linux Doc Mailing List, Linux Kernel Mailing List,
linux-arm-kernel, op-tee, tee-dev @ lists . linaro . org,
Luke Hinds
+ Luke
Hi Jarkko,
Prior to addressing your comments below which seems to show your
preference for compile time selection of trust source (TPM or TEE), I
would just like to hear the reasons for this preference especially if
it makes distro vendor's life difficult [1] to make opinionated
selection which could rather be achieved dynamically based on platform
capability.
[1] https://lkml.org/lkml/2020/6/3/405
-Sumit
On Mon, 15 Jun 2020 at 23:55, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> > diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> > index 47c0415..22632c6 100644
> > --- a/security/keys/Kconfig
> > +++ b/security/keys/Kconfig
> > @@ -72,17 +72,26 @@ config BIG_KEYS
> >
> > config TRUSTED_KEYS
> > tristate "TRUSTED KEYS"
> > - depends on KEYS && TCG_TPM
> > + depends on KEYS
> > + help
> > + This option provides support for creating, sealing, and unsealing
> > + keys in the kernel. Trusted keys are random number symmetric keys,
> > + generated and sealed by a trust source (TPM or TEE). Userspace will
> > + only ever see encrypted blobs.
> > +
> > + If you are unsure as to whether this is required, answer N.
> > +
> > +config TRUSTED_TPM
>
> TRUSTED_TPM_KEYS
>
> > + bool "TPM based TRUSTED KEYS"
> > + depends on TRUSTED_KEYS && TCG_TPM
> > select CRYPTO
> > select CRYPTO_HMAC
> > select CRYPTO_SHA1
> > select CRYPTO_HASH_INFO
> > help
> > - This option provides support for creating, sealing, and unsealing
> > - keys in the kernel. Trusted keys are random number symmetric keys,
> > - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> > - if the boot PCRs and other criteria match. Userspace will only ever
> > - see encrypted blobs.
> > + This option provides support for TPM based trusted keys where TPM acts
> > + as the trust source. The keys are RSA-sealed by the TPM and it only
> > + unseals the keys, if the boot PCRs and other criteria match.
> >
> > If you are unsure as to whether this is required, answer N.
> >
> > diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> > index 7b73ceb..03c3ad4 100644
> > --- a/security/keys/trusted-keys/Makefile
> > +++ b/security/keys/trusted-keys/Makefile
> > @@ -4,5 +4,6 @@
> > #
> >
> > obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> > -trusted-y += trusted_tpm1.o
> > -trusted-y += trusted_tpm2.o
> > +trusted-y += trusted_core.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> > diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> > new file mode 100644
> > index 0000000..3d07d27
> > --- /dev/null
> > +++ b/security/keys/trusted-keys/trusted_core.c
> > @@ -0,0 +1,319 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > + *
> > + * Author:
> > + * David Safford <safford@us.ibm.com>
> > + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
>
> Rather remove David's because
>
> 1. Git log gives a verifiable and exact log of authority.
> 2. David's commit was done post-epoch of Git.
>
> > + *
> > + * See Documentation/security/keys/trusted-encrypted.rst
> > + */
> > +
> > +#include <keys/user-type.h>
> > +#include <keys/trusted-type.h>
> > +#include <linux/capability.h>
> > +#include <linux/err.h>
> > +#include <linux/init.h>
> > +#include <linux/key-type.h>
> > +#include <linux/module.h>
> > +#include <linux/parser.h>
> > +#include <linux/rcupdate.h>
> > +#include <linux/slab.h>
> > +#include <linux/string.h>
> > +#include <linux/uaccess.h>
> > +
> > +#if defined(CONFIG_TRUSTED_TPM)
> > +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> > +#else
> > +static struct trusted_key_ops *trusted_key_ops;
> > +#endif
>
> You should just have
>
> extern trusted_key_ops *trusted_key_ops;
>
> > +
> > +enum {
> > + Opt_err,
> > + Opt_new, Opt_load, Opt_update,
> > +};
> > +
> > +static const match_table_t key_tokens = {
> > + {Opt_new, "new"},
> > + {Opt_load, "load"},
> > + {Opt_update, "update"},
> > + {Opt_err, NULL}
> > +};
> > +
> > +/*
> > + * datablob_parse - parse the keyctl data and fill in the
> > + * payload structure
> > + *
> > + * On success returns 0, otherwise -EINVAL.
> > + */
> > +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> > +{
> > + substring_t args[MAX_OPT_ARGS];
> > + long keylen;
> > + int ret = -EINVAL;
> > + int key_cmd;
> > + char *c;
> > +
> > + /* main command */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + key_cmd = match_token(c, key_tokens, args);
> > + switch (key_cmd) {
> > + case Opt_new:
> > + /* first argument is key size */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + ret = kstrtol(c, 10, &keylen);
> > + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > + return -EINVAL;
> > + p->key_len = keylen;
> > + ret = Opt_new;
> > + break;
> > + case Opt_load:
> > + /* first argument is sealed blob */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + p->blob_len = strlen(c) / 2;
> > + if (p->blob_len > MAX_BLOB_SIZE)
> > + return -EINVAL;
> > + ret = hex2bin(p->blob, c, p->blob_len);
> > + if (ret < 0)
> > + return -EINVAL;
> > + ret = Opt_load;
> > + break;
> > + case Opt_update:
> > + ret = Opt_update;
> > + break;
> > + case Opt_err:
> > + return -EINVAL;
> > + }
> > + return ret;
> > +}
> > +
> > +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +{
> > + struct trusted_key_payload *p = NULL;
> > + int ret;
> > +
> > + ret = key_payload_reserve(key, sizeof(*p));
> > + if (ret < 0)
> > + return p;
> > + p = kzalloc(sizeof(*p), GFP_KERNEL);
> > +
> > + p->migratable = trusted_key_ops->migratable;
> > +
> > + return p;
> > +}
> > +
> > +/*
> > + * trusted_instantiate - create a new trusted key
> > + *
> > + * Unseal an existing trusted blob or, for a new key, get a
> > + * random key, then seal and create a trusted key-type key,
> > + * adding it to the specified keyring.
> > + *
> > + * On success, return 0. Otherwise return errno.
> > + */
> > +static int trusted_instantiate(struct key *key,
> > + struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *payload = NULL;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > + int key_cmd;
> > + size_t key_len;
> > +
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > +
> > + payload = trusted_payload_alloc(key);
> > + if (!payload) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + key_cmd = datablob_parse(datablob, payload);
> > + if (key_cmd < 0) {
> > + ret = key_cmd;
> > + goto out;
> > + }
> > +
> > + dump_payload(payload);
> > +
> > + switch (key_cmd) {
> > + case Opt_load:
> > + ret = trusted_key_ops->unseal(payload, datablob);
> > + dump_payload(payload);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > + break;
> > + case Opt_new:
> > + key_len = payload->key_len;
> > + ret = trusted_key_ops->get_random(payload->key, key_len);
> > + if (ret != key_len) {
> > + pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + goto out;
> > + }
> > +
> > + ret = trusted_key_ops->seal(payload, datablob);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > +out:
> > + kzfree(datablob);
> > + if (!ret)
> > + rcu_assign_keypointer(key, payload);
> > + else
> > + kzfree(payload);
> > + return ret;
> > +}
> > +
> > +static void trusted_rcu_free(struct rcu_head *rcu)
> > +{
> > + struct trusted_key_payload *p;
> > +
> > + p = container_of(rcu, struct trusted_key_payload, rcu);
> > + kzfree(p);
> > +}
> > +
> > +/*
> > + * trusted_update - reseal an existing key with new PCR values
> > + */
> > +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *p;
> > + struct trusted_key_payload *new_p;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > +
> > + if (key_is_negative(key))
> > + return -ENOKEY;
> > + p = key->payload.data[0];
> > + if (!p->migratable)
> > + return -EPERM;
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > +
> > + new_p = trusted_payload_alloc(key);
> > + if (!new_p) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > + ret = datablob_parse(datablob, new_p);
> > + if (ret != Opt_update) {
> > + ret = -EINVAL;
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + /* copy old key values, and reseal with new pcrs */
> > + new_p->migratable = p->migratable;
> > + new_p->key_len = p->key_len;
> > + memcpy(new_p->key, p->key, p->key_len);
> > + dump_payload(p);
> > + dump_payload(new_p);
> > +
> > + ret = trusted_key_ops->seal(new_p, datablob);
> > + if (ret < 0) {
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + rcu_assign_keypointer(key, new_p);
> > + call_rcu(&p->rcu, trusted_rcu_free);
> > +out:
> > + kzfree(datablob);
> > + return ret;
> > +}
> > +
> > +/*
> > + * trusted_read - copy the sealed blob data to userspace in hex.
> > + * On success, return to userspace the trusted key datablob size.
> > + */
> > +static long trusted_read(const struct key *key, char *buffer,
> > + size_t buflen)
> > +{
> > + const struct trusted_key_payload *p;
> > + char *bufp;
> > + int i;
> > +
> > + p = dereference_key_locked(key);
> > + if (!p)
> > + return -EINVAL;
> > +
> > + if (buffer && buflen >= 2 * p->blob_len) {
> > + bufp = buffer;
> > + for (i = 0; i < p->blob_len; i++)
> > + bufp = hex_byte_pack(bufp, p->blob[i]);
> > + }
> > + return 2 * p->blob_len;
> > +}
> > +
> > +/*
> > + * trusted_destroy - clear and free the key's payload
> > + */
> > +static void trusted_destroy(struct key *key)
> > +{
> > + kzfree(key->payload.data[0]);
> > +}
> > +
> > +struct key_type key_type_trusted = {
> > + .name = "trusted",
> > + .instantiate = trusted_instantiate,
> > + .update = trusted_update,
> > + .destroy = trusted_destroy,
> > + .describe = user_describe,
> > + .read = trusted_read,
> > +};
> > +EXPORT_SYMBOL_GPL(key_type_trusted);
> > +
> > +static int __init init_trusted(void)
> > +{
> > + int ret;
> > +
> > + /*
> > + * encrypted_keys.ko depends on successful load of this module even if
> > + * trusted key implementation is not found.
> > + */
> > + if (!trusted_key_ops)
> > + return 0;
> > +
> > + ret = trusted_key_ops->init();
> > + if (ret == -ENODEV)
> > + return 0;
> > +
> > + return ret;
> > +}
> > +
> > +static void __exit cleanup_trusted(void)
> > +{
> > + trusted_key_ops->exit();
> > +}
> > +
> > +late_initcall(init_trusted);
> > +module_exit(cleanup_trusted);
> > +
> > +MODULE_LICENSE("GPL");
> > diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> > index 8001ab0..e378b27 100644
> > --- a/security/keys/trusted-keys/trusted_tpm1.c
> > +++ b/security/keys/trusted-keys/trusted_tpm1.c
> > @@ -1,29 +1,26 @@
> > // SPDX-License-Identifier: GPL-2.0-only
> > /*
> > * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > *
> > * Author:
> > * David Safford <safford@us.ibm.com>
> > + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> > *
> > * See Documentation/security/keys/trusted-encrypted.rst
> > */
> >
> > #include <crypto/hash_info.h>
> > -#include <linux/uaccess.h>
> > -#include <linux/module.h>
> > #include <linux/init.h>
> > #include <linux/slab.h>
> > #include <linux/parser.h>
> > #include <linux/string.h>
> > #include <linux/err.h>
> > -#include <keys/user-type.h>
> > #include <keys/trusted-type.h>
> > #include <linux/key-type.h>
> > -#include <linux/rcupdate.h>
> > #include <linux/crypto.h>
> > #include <crypto/hash.h>
> > #include <crypto/sha.h>
> > -#include <linux/capability.h>
> > #include <linux/tpm.h>
> > #include <linux/tpm_command.h>
> >
> > @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
> >
> > enum {
> > Opt_err,
> > - Opt_new, Opt_load, Opt_update,
> > Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> > Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> > Opt_hash,
> > @@ -712,9 +708,6 @@ enum {
> > };
> >
> > static const match_table_t key_tokens = {
> > - {Opt_new, "new"},
> > - {Opt_load, "load"},
> > - {Opt_update, "update"},
> > {Opt_keyhandle, "keyhandle=%s"},
> > {Opt_keyauth, "keyauth=%s"},
> > {Opt_blobauth, "blobauth=%s"},
> > @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> > return 0;
> > }
> >
> > -/*
> > - * datablob_parse - parse the keyctl data and fill in the
> > - * payload and options structures
> > - *
> > - * On success returns 0, otherwise -EINVAL.
> > - */
> > -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> > - struct trusted_key_options *o)
> > -{
> > - substring_t args[MAX_OPT_ARGS];
> > - long keylen;
> > - int ret = -EINVAL;
> > - int key_cmd;
> > - char *c;
> > -
> > - /* main command */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - key_cmd = match_token(c, key_tokens, args);
> > - switch (key_cmd) {
> > - case Opt_new:
> > - /* first argument is key size */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - ret = kstrtol(c, 10, &keylen);
> > - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > - return -EINVAL;
> > - p->key_len = keylen;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_new;
> > - break;
> > - case Opt_load:
> > - /* first argument is sealed blob */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - p->blob_len = strlen(c) / 2;
> > - if (p->blob_len > MAX_BLOB_SIZE)
> > - return -EINVAL;
> > - ret = hex2bin(p->blob, c, p->blob_len);
> > - if (ret < 0)
> > - return -EINVAL;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_load;
> > - break;
> > - case Opt_update:
> > - /* all arguments are options */
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_update;
> > - break;
> > - case Opt_err:
> > - return -EINVAL;
> > - break;
> > - }
> > - return ret;
> > -}
> > -
> > static struct trusted_key_options *trusted_options_alloc(void)
> > {
> > struct trusted_key_options *options;
> > @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> > return options;
> > }
> >
> > -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p = NULL;
> > - int ret;
> > -
> > - ret = key_payload_reserve(key, sizeof *p);
> > - if (ret < 0)
> > - return p;
> > - p = kzalloc(sizeof *p, GFP_KERNEL);
> > - if (p)
> > - p->migratable = 1; /* migratable by default */
> > - return p;
> > -}
> > -
> > -/*
> > - * trusted_instantiate - create a new trusted key
> > - *
> > - * Unseal an existing trusted blob or, for a new key, get a
> > - * random key, then seal and create a trusted key-type key,
> > - * adding it to the specified keyring.
> > - *
> > - * On success, return 0. Otherwise return errno.
> > - */
> > -static int trusted_instantiate(struct key *key,
> > - struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *payload = NULL;
> > struct trusted_key_options *options = NULL;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > int ret = 0;
> > - int key_cmd;
> > - size_t key_len;
> > int tpm2;
> >
> > tpm2 = tpm_is_tpm2(chip);
> > if (tpm2 < 0)
> > return tpm2;
> >
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > -
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > - return -ENOMEM;
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > -
> > options = trusted_options_alloc();
> > - if (!options) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - payload = trusted_payload_alloc(key);
> > - if (!payload) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > + if (!options)
> > + return -ENOMEM;
> >
> > - key_cmd = datablob_parse(datablob, payload, options);
> > - if (key_cmd < 0) {
> > - ret = key_cmd;
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > if (!options->keyhandle) {
> > ret = -EINVAL;
> > goto out;
> > }
> >
> > - dump_payload(payload);
> > - dump_options(options);
> > + if (tpm2)
> > + ret = tpm2_seal_trusted(chip, p, options);
> > + else
> > + ret = key_seal(p, options);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> > + goto out;
> > + }
> >
> > - switch (key_cmd) {
> > - case Opt_load:
> > - if (tpm2)
> > - ret = tpm2_unseal_trusted(chip, payload, options);
> > - else
> > - ret = key_unseal(payload, options);
> > - dump_payload(payload);
> > - dump_options(options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > - break;
> > - case Opt_new:
> > - key_len = payload->key_len;
> > - ret = tpm_get_random(chip, payload->key, key_len);
> > - if (ret != key_len) {
> > - pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > - if (tpm2)
> > - ret = tpm2_seal_trusted(chip, payload, options);
> > - else
> > - ret = key_seal(payload, options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - break;
> > - default:
> > - ret = -EINVAL;
> > - goto out;
> > }
> > - if (!ret && options->pcrlock)
> > - ret = pcrlock(options->pcrlock);
> > out:
> > - kzfree(datablob);
> > kzfree(options);
> > - if (!ret)
> > - rcu_assign_keypointer(key, payload);
> > - else
> > - kzfree(payload);
> > return ret;
> > }
> >
> > -static void trusted_rcu_free(struct rcu_head *rcu)
> > +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p;
> > -
> > - p = container_of(rcu, struct trusted_key_payload, rcu);
> > - kzfree(p);
> > -}
> > -
> > -/*
> > - * trusted_update - reseal an existing key with new PCR values
> > - */
> > -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *p;
> > - struct trusted_key_payload *new_p;
> > - struct trusted_key_options *new_o;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > + struct trusted_key_options *options = NULL;
> > int ret = 0;
> > + int tpm2;
> >
> > - if (key_is_negative(key))
> > - return -ENOKEY;
> > - p = key->payload.data[0];
> > - if (!p->migratable)
> > - return -EPERM;
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > + tpm2 = tpm_is_tpm2(chip);
> > + if (tpm2 < 0)
> > + return tpm2;
> >
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > + options = trusted_options_alloc();
> > + if (!options)
> > return -ENOMEM;
> > - new_o = trusted_options_alloc();
> > - if (!new_o) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - new_p = trusted_payload_alloc(key);
> > - if (!new_p) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> >
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > - ret = datablob_parse(datablob, new_p, new_o);
> > - if (ret != Opt_update) {
> > - ret = -EINVAL;
> > - kzfree(new_p);
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > - if (!new_o->keyhandle) {
> > + if (!options->keyhandle) {
> > ret = -EINVAL;
> > - kzfree(new_p);
> > goto out;
> > }
> >
> > - /* copy old key values, and reseal with new pcrs */
> > - new_p->migratable = p->migratable;
> > - new_p->key_len = p->key_len;
> > - memcpy(new_p->key, p->key, p->key_len);
> > - dump_payload(p);
> > - dump_payload(new_p);
> > + if (tpm2)
> > + ret = tpm2_unseal_trusted(chip, p, options);
> > + else
> > + ret = key_unseal(p, options);
> > + if (ret < 0)
> > + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
> >
> > - ret = key_seal(new_p, new_o);
> > - if (ret < 0) {
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - kzfree(new_p);
> > - goto out;
> > - }
> > - if (new_o->pcrlock) {
> > - ret = pcrlock(new_o->pcrlock);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > if (ret < 0) {
> > - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> > - kzfree(new_p);
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > }
> > - rcu_assign_keypointer(key, new_p);
> > - call_rcu(&p->rcu, trusted_rcu_free);
> > out:
> > - kzfree(datablob);
> > - kzfree(new_o);
> > + kzfree(options);
> > return ret;
> > }
> >
> > -/*
> > - * trusted_read - copy the sealed blob data to userspace in hex.
> > - * On success, return to userspace the trusted key datablob size.
> > - */
> > -static long trusted_read(const struct key *key, char *buffer,
> > - size_t buflen)
> > -{
> > - const struct trusted_key_payload *p;
> > - char *bufp;
> > - int i;
> > -
> > - p = dereference_key_locked(key);
> > - if (!p)
> > - return -EINVAL;
> > -
> > - if (buffer && buflen >= 2 * p->blob_len) {
> > - bufp = buffer;
> > - for (i = 0; i < p->blob_len; i++)
> > - bufp = hex_byte_pack(bufp, p->blob[i]);
> > - }
> > - return 2 * p->blob_len;
> > -}
> > -
> > -/*
> > - * trusted_destroy - clear and free the key's payload
> > - */
> > -static void trusted_destroy(struct key *key)
> > +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> > {
> > - kzfree(key->payload.data[0]);
> > + return tpm_get_random(chip, key, key_len);
> > }
> >
> > -struct key_type key_type_trusted = {
> > - .name = "trusted",
> > - .instantiate = trusted_instantiate,
> > - .update = trusted_update,
> > - .destroy = trusted_destroy,
> > - .describe = user_describe,
> > - .read = trusted_read,
> > -};
> > -
> > -EXPORT_SYMBOL_GPL(key_type_trusted);
> > -
> > static void trusted_shash_release(void)
> > {
> > if (hashalg)
> > @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
> >
> > hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> > if (IS_ERR(hmacalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hmac_alg);
> > return PTR_ERR(hmacalg);
> > }
> >
> > hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> > if (IS_ERR(hashalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hash_alg);
> > ret = PTR_ERR(hashalg);
> > goto hashalg_fail;
> > @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> > return 0;
> > }
> >
> > -static int __init init_trusted(void)
> > +static int __init init_tpm_trusted(void)
> > {
> > int ret;
> >
> > - /* encrypted_keys.ko depends on successful load of this module even if
> > - * TPM is not used.
> > - */
> > chip = tpm_default_chip();
> > if (!chip)
> > - return 0;
> > + return -ENODEV;
> >
> > ret = init_digests();
> > if (ret < 0)
> > @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> > return ret;
> > }
> >
> > -static void __exit cleanup_trusted(void)
> > +static void __exit exit_tpm_trusted(void)
> > {
> > if (chip) {
> > put_device(&chip->dev);
> > @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> > }
> > }
> >
> > -late_initcall(init_trusted);
> > -module_exit(cleanup_trusted);
> > -
> > -MODULE_LICENSE("GPL");
> > +struct trusted_key_ops tpm_trusted_key_ops = {
> > + .migratable = 1, /* migratable by default */
> > + .init = init_tpm_trusted,
> > + .seal = tpm_trusted_seal,
> > + .unseal = tpm_trusted_unseal,
> > + .get_random = tpm_trusted_get_random,
> > + .exit = exit_tpm_trusted,
> > +};
> > +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
>
> Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
>
> > 2.7.4
> >
>
> /Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-16 13:32 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-16 13:44 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
+ Luke
Hi Jarkko,
Prior to addressing your comments below which seems to show your
preference for compile time selection of trust source (TPM or TEE), I
would just like to hear the reasons for this preference especially if
it makes distro vendor's life difficult [1] to make opinionated
selection which could rather be achieved dynamically based on platform
capability.
[1] https://lkml.org/lkml/2020/6/3/405
-Sumit
On Mon, 15 Jun 2020 at 23:55, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> > diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> > index 47c0415..22632c6 100644
> > --- a/security/keys/Kconfig
> > +++ b/security/keys/Kconfig
> > @@ -72,17 +72,26 @@ config BIG_KEYS
> >
> > config TRUSTED_KEYS
> > tristate "TRUSTED KEYS"
> > - depends on KEYS && TCG_TPM
> > + depends on KEYS
> > + help
> > + This option provides support for creating, sealing, and unsealing
> > + keys in the kernel. Trusted keys are random number symmetric keys,
> > + generated and sealed by a trust source (TPM or TEE). Userspace will
> > + only ever see encrypted blobs.
> > +
> > + If you are unsure as to whether this is required, answer N.
> > +
> > +config TRUSTED_TPM
>
> TRUSTED_TPM_KEYS
>
> > + bool "TPM based TRUSTED KEYS"
> > + depends on TRUSTED_KEYS && TCG_TPM
> > select CRYPTO
> > select CRYPTO_HMAC
> > select CRYPTO_SHA1
> > select CRYPTO_HASH_INFO
> > help
> > - This option provides support for creating, sealing, and unsealing
> > - keys in the kernel. Trusted keys are random number symmetric keys,
> > - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> > - if the boot PCRs and other criteria match. Userspace will only ever
> > - see encrypted blobs.
> > + This option provides support for TPM based trusted keys where TPM acts
> > + as the trust source. The keys are RSA-sealed by the TPM and it only
> > + unseals the keys, if the boot PCRs and other criteria match.
> >
> > If you are unsure as to whether this is required, answer N.
> >
> > diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> > index 7b73ceb..03c3ad4 100644
> > --- a/security/keys/trusted-keys/Makefile
> > +++ b/security/keys/trusted-keys/Makefile
> > @@ -4,5 +4,6 @@
> > #
> >
> > obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> > -trusted-y += trusted_tpm1.o
> > -trusted-y += trusted_tpm2.o
> > +trusted-y += trusted_core.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> > diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> > new file mode 100644
> > index 0000000..3d07d27
> > --- /dev/null
> > +++ b/security/keys/trusted-keys/trusted_core.c
> > @@ -0,0 +1,319 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > + *
> > + * Author:
> > + * David Safford <safford@us.ibm.com>
> > + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
>
> Rather remove David's because
>
> 1. Git log gives a verifiable and exact log of authority.
> 2. David's commit was done post-epoch of Git.
>
> > + *
> > + * See Documentation/security/keys/trusted-encrypted.rst
> > + */
> > +
> > +#include <keys/user-type.h>
> > +#include <keys/trusted-type.h>
> > +#include <linux/capability.h>
> > +#include <linux/err.h>
> > +#include <linux/init.h>
> > +#include <linux/key-type.h>
> > +#include <linux/module.h>
> > +#include <linux/parser.h>
> > +#include <linux/rcupdate.h>
> > +#include <linux/slab.h>
> > +#include <linux/string.h>
> > +#include <linux/uaccess.h>
> > +
> > +#if defined(CONFIG_TRUSTED_TPM)
> > +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> > +#else
> > +static struct trusted_key_ops *trusted_key_ops;
> > +#endif
>
> You should just have
>
> extern trusted_key_ops *trusted_key_ops;
>
> > +
> > +enum {
> > + Opt_err,
> > + Opt_new, Opt_load, Opt_update,
> > +};
> > +
> > +static const match_table_t key_tokens = {
> > + {Opt_new, "new"},
> > + {Opt_load, "load"},
> > + {Opt_update, "update"},
> > + {Opt_err, NULL}
> > +};
> > +
> > +/*
> > + * datablob_parse - parse the keyctl data and fill in the
> > + * payload structure
> > + *
> > + * On success returns 0, otherwise -EINVAL.
> > + */
> > +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> > +{
> > + substring_t args[MAX_OPT_ARGS];
> > + long keylen;
> > + int ret = -EINVAL;
> > + int key_cmd;
> > + char *c;
> > +
> > + /* main command */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + key_cmd = match_token(c, key_tokens, args);
> > + switch (key_cmd) {
> > + case Opt_new:
> > + /* first argument is key size */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + ret = kstrtol(c, 10, &keylen);
> > + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > + return -EINVAL;
> > + p->key_len = keylen;
> > + ret = Opt_new;
> > + break;
> > + case Opt_load:
> > + /* first argument is sealed blob */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + p->blob_len = strlen(c) / 2;
> > + if (p->blob_len > MAX_BLOB_SIZE)
> > + return -EINVAL;
> > + ret = hex2bin(p->blob, c, p->blob_len);
> > + if (ret < 0)
> > + return -EINVAL;
> > + ret = Opt_load;
> > + break;
> > + case Opt_update:
> > + ret = Opt_update;
> > + break;
> > + case Opt_err:
> > + return -EINVAL;
> > + }
> > + return ret;
> > +}
> > +
> > +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +{
> > + struct trusted_key_payload *p = NULL;
> > + int ret;
> > +
> > + ret = key_payload_reserve(key, sizeof(*p));
> > + if (ret < 0)
> > + return p;
> > + p = kzalloc(sizeof(*p), GFP_KERNEL);
> > +
> > + p->migratable = trusted_key_ops->migratable;
> > +
> > + return p;
> > +}
> > +
> > +/*
> > + * trusted_instantiate - create a new trusted key
> > + *
> > + * Unseal an existing trusted blob or, for a new key, get a
> > + * random key, then seal and create a trusted key-type key,
> > + * adding it to the specified keyring.
> > + *
> > + * On success, return 0. Otherwise return errno.
> > + */
> > +static int trusted_instantiate(struct key *key,
> > + struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *payload = NULL;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > + int key_cmd;
> > + size_t key_len;
> > +
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > +
> > + payload = trusted_payload_alloc(key);
> > + if (!payload) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + key_cmd = datablob_parse(datablob, payload);
> > + if (key_cmd < 0) {
> > + ret = key_cmd;
> > + goto out;
> > + }
> > +
> > + dump_payload(payload);
> > +
> > + switch (key_cmd) {
> > + case Opt_load:
> > + ret = trusted_key_ops->unseal(payload, datablob);
> > + dump_payload(payload);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > + break;
> > + case Opt_new:
> > + key_len = payload->key_len;
> > + ret = trusted_key_ops->get_random(payload->key, key_len);
> > + if (ret != key_len) {
> > + pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + goto out;
> > + }
> > +
> > + ret = trusted_key_ops->seal(payload, datablob);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > +out:
> > + kzfree(datablob);
> > + if (!ret)
> > + rcu_assign_keypointer(key, payload);
> > + else
> > + kzfree(payload);
> > + return ret;
> > +}
> > +
> > +static void trusted_rcu_free(struct rcu_head *rcu)
> > +{
> > + struct trusted_key_payload *p;
> > +
> > + p = container_of(rcu, struct trusted_key_payload, rcu);
> > + kzfree(p);
> > +}
> > +
> > +/*
> > + * trusted_update - reseal an existing key with new PCR values
> > + */
> > +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *p;
> > + struct trusted_key_payload *new_p;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > +
> > + if (key_is_negative(key))
> > + return -ENOKEY;
> > + p = key->payload.data[0];
> > + if (!p->migratable)
> > + return -EPERM;
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > +
> > + new_p = trusted_payload_alloc(key);
> > + if (!new_p) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > + ret = datablob_parse(datablob, new_p);
> > + if (ret != Opt_update) {
> > + ret = -EINVAL;
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + /* copy old key values, and reseal with new pcrs */
> > + new_p->migratable = p->migratable;
> > + new_p->key_len = p->key_len;
> > + memcpy(new_p->key, p->key, p->key_len);
> > + dump_payload(p);
> > + dump_payload(new_p);
> > +
> > + ret = trusted_key_ops->seal(new_p, datablob);
> > + if (ret < 0) {
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + rcu_assign_keypointer(key, new_p);
> > + call_rcu(&p->rcu, trusted_rcu_free);
> > +out:
> > + kzfree(datablob);
> > + return ret;
> > +}
> > +
> > +/*
> > + * trusted_read - copy the sealed blob data to userspace in hex.
> > + * On success, return to userspace the trusted key datablob size.
> > + */
> > +static long trusted_read(const struct key *key, char *buffer,
> > + size_t buflen)
> > +{
> > + const struct trusted_key_payload *p;
> > + char *bufp;
> > + int i;
> > +
> > + p = dereference_key_locked(key);
> > + if (!p)
> > + return -EINVAL;
> > +
> > + if (buffer && buflen >= 2 * p->blob_len) {
> > + bufp = buffer;
> > + for (i = 0; i < p->blob_len; i++)
> > + bufp = hex_byte_pack(bufp, p->blob[i]);
> > + }
> > + return 2 * p->blob_len;
> > +}
> > +
> > +/*
> > + * trusted_destroy - clear and free the key's payload
> > + */
> > +static void trusted_destroy(struct key *key)
> > +{
> > + kzfree(key->payload.data[0]);
> > +}
> > +
> > +struct key_type key_type_trusted = {
> > + .name = "trusted",
> > + .instantiate = trusted_instantiate,
> > + .update = trusted_update,
> > + .destroy = trusted_destroy,
> > + .describe = user_describe,
> > + .read = trusted_read,
> > +};
> > +EXPORT_SYMBOL_GPL(key_type_trusted);
> > +
> > +static int __init init_trusted(void)
> > +{
> > + int ret;
> > +
> > + /*
> > + * encrypted_keys.ko depends on successful load of this module even if
> > + * trusted key implementation is not found.
> > + */
> > + if (!trusted_key_ops)
> > + return 0;
> > +
> > + ret = trusted_key_ops->init();
> > + if (ret = -ENODEV)
> > + return 0;
> > +
> > + return ret;
> > +}
> > +
> > +static void __exit cleanup_trusted(void)
> > +{
> > + trusted_key_ops->exit();
> > +}
> > +
> > +late_initcall(init_trusted);
> > +module_exit(cleanup_trusted);
> > +
> > +MODULE_LICENSE("GPL");
> > diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> > index 8001ab0..e378b27 100644
> > --- a/security/keys/trusted-keys/trusted_tpm1.c
> > +++ b/security/keys/trusted-keys/trusted_tpm1.c
> > @@ -1,29 +1,26 @@
> > // SPDX-License-Identifier: GPL-2.0-only
> > /*
> > * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > *
> > * Author:
> > * David Safford <safford@us.ibm.com>
> > + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> > *
> > * See Documentation/security/keys/trusted-encrypted.rst
> > */
> >
> > #include <crypto/hash_info.h>
> > -#include <linux/uaccess.h>
> > -#include <linux/module.h>
> > #include <linux/init.h>
> > #include <linux/slab.h>
> > #include <linux/parser.h>
> > #include <linux/string.h>
> > #include <linux/err.h>
> > -#include <keys/user-type.h>
> > #include <keys/trusted-type.h>
> > #include <linux/key-type.h>
> > -#include <linux/rcupdate.h>
> > #include <linux/crypto.h>
> > #include <crypto/hash.h>
> > #include <crypto/sha.h>
> > -#include <linux/capability.h>
> > #include <linux/tpm.h>
> > #include <linux/tpm_command.h>
> >
> > @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
> >
> > enum {
> > Opt_err,
> > - Opt_new, Opt_load, Opt_update,
> > Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> > Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> > Opt_hash,
> > @@ -712,9 +708,6 @@ enum {
> > };
> >
> > static const match_table_t key_tokens = {
> > - {Opt_new, "new"},
> > - {Opt_load, "load"},
> > - {Opt_update, "update"},
> > {Opt_keyhandle, "keyhandle=%s"},
> > {Opt_keyauth, "keyauth=%s"},
> > {Opt_blobauth, "blobauth=%s"},
> > @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> > return 0;
> > }
> >
> > -/*
> > - * datablob_parse - parse the keyctl data and fill in the
> > - * payload and options structures
> > - *
> > - * On success returns 0, otherwise -EINVAL.
> > - */
> > -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> > - struct trusted_key_options *o)
> > -{
> > - substring_t args[MAX_OPT_ARGS];
> > - long keylen;
> > - int ret = -EINVAL;
> > - int key_cmd;
> > - char *c;
> > -
> > - /* main command */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - key_cmd = match_token(c, key_tokens, args);
> > - switch (key_cmd) {
> > - case Opt_new:
> > - /* first argument is key size */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - ret = kstrtol(c, 10, &keylen);
> > - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > - return -EINVAL;
> > - p->key_len = keylen;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_new;
> > - break;
> > - case Opt_load:
> > - /* first argument is sealed blob */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - p->blob_len = strlen(c) / 2;
> > - if (p->blob_len > MAX_BLOB_SIZE)
> > - return -EINVAL;
> > - ret = hex2bin(p->blob, c, p->blob_len);
> > - if (ret < 0)
> > - return -EINVAL;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_load;
> > - break;
> > - case Opt_update:
> > - /* all arguments are options */
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_update;
> > - break;
> > - case Opt_err:
> > - return -EINVAL;
> > - break;
> > - }
> > - return ret;
> > -}
> > -
> > static struct trusted_key_options *trusted_options_alloc(void)
> > {
> > struct trusted_key_options *options;
> > @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> > return options;
> > }
> >
> > -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p = NULL;
> > - int ret;
> > -
> > - ret = key_payload_reserve(key, sizeof *p);
> > - if (ret < 0)
> > - return p;
> > - p = kzalloc(sizeof *p, GFP_KERNEL);
> > - if (p)
> > - p->migratable = 1; /* migratable by default */
> > - return p;
> > -}
> > -
> > -/*
> > - * trusted_instantiate - create a new trusted key
> > - *
> > - * Unseal an existing trusted blob or, for a new key, get a
> > - * random key, then seal and create a trusted key-type key,
> > - * adding it to the specified keyring.
> > - *
> > - * On success, return 0. Otherwise return errno.
> > - */
> > -static int trusted_instantiate(struct key *key,
> > - struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *payload = NULL;
> > struct trusted_key_options *options = NULL;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > int ret = 0;
> > - int key_cmd;
> > - size_t key_len;
> > int tpm2;
> >
> > tpm2 = tpm_is_tpm2(chip);
> > if (tpm2 < 0)
> > return tpm2;
> >
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > -
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > - return -ENOMEM;
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > -
> > options = trusted_options_alloc();
> > - if (!options) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - payload = trusted_payload_alloc(key);
> > - if (!payload) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > + if (!options)
> > + return -ENOMEM;
> >
> > - key_cmd = datablob_parse(datablob, payload, options);
> > - if (key_cmd < 0) {
> > - ret = key_cmd;
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > if (!options->keyhandle) {
> > ret = -EINVAL;
> > goto out;
> > }
> >
> > - dump_payload(payload);
> > - dump_options(options);
> > + if (tpm2)
> > + ret = tpm2_seal_trusted(chip, p, options);
> > + else
> > + ret = key_seal(p, options);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> > + goto out;
> > + }
> >
> > - switch (key_cmd) {
> > - case Opt_load:
> > - if (tpm2)
> > - ret = tpm2_unseal_trusted(chip, payload, options);
> > - else
> > - ret = key_unseal(payload, options);
> > - dump_payload(payload);
> > - dump_options(options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > - break;
> > - case Opt_new:
> > - key_len = payload->key_len;
> > - ret = tpm_get_random(chip, payload->key, key_len);
> > - if (ret != key_len) {
> > - pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > - if (tpm2)
> > - ret = tpm2_seal_trusted(chip, payload, options);
> > - else
> > - ret = key_seal(payload, options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - break;
> > - default:
> > - ret = -EINVAL;
> > - goto out;
> > }
> > - if (!ret && options->pcrlock)
> > - ret = pcrlock(options->pcrlock);
> > out:
> > - kzfree(datablob);
> > kzfree(options);
> > - if (!ret)
> > - rcu_assign_keypointer(key, payload);
> > - else
> > - kzfree(payload);
> > return ret;
> > }
> >
> > -static void trusted_rcu_free(struct rcu_head *rcu)
> > +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p;
> > -
> > - p = container_of(rcu, struct trusted_key_payload, rcu);
> > - kzfree(p);
> > -}
> > -
> > -/*
> > - * trusted_update - reseal an existing key with new PCR values
> > - */
> > -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *p;
> > - struct trusted_key_payload *new_p;
> > - struct trusted_key_options *new_o;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > + struct trusted_key_options *options = NULL;
> > int ret = 0;
> > + int tpm2;
> >
> > - if (key_is_negative(key))
> > - return -ENOKEY;
> > - p = key->payload.data[0];
> > - if (!p->migratable)
> > - return -EPERM;
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > + tpm2 = tpm_is_tpm2(chip);
> > + if (tpm2 < 0)
> > + return tpm2;
> >
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > + options = trusted_options_alloc();
> > + if (!options)
> > return -ENOMEM;
> > - new_o = trusted_options_alloc();
> > - if (!new_o) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - new_p = trusted_payload_alloc(key);
> > - if (!new_p) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> >
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > - ret = datablob_parse(datablob, new_p, new_o);
> > - if (ret != Opt_update) {
> > - ret = -EINVAL;
> > - kzfree(new_p);
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > - if (!new_o->keyhandle) {
> > + if (!options->keyhandle) {
> > ret = -EINVAL;
> > - kzfree(new_p);
> > goto out;
> > }
> >
> > - /* copy old key values, and reseal with new pcrs */
> > - new_p->migratable = p->migratable;
> > - new_p->key_len = p->key_len;
> > - memcpy(new_p->key, p->key, p->key_len);
> > - dump_payload(p);
> > - dump_payload(new_p);
> > + if (tpm2)
> > + ret = tpm2_unseal_trusted(chip, p, options);
> > + else
> > + ret = key_unseal(p, options);
> > + if (ret < 0)
> > + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
> >
> > - ret = key_seal(new_p, new_o);
> > - if (ret < 0) {
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - kzfree(new_p);
> > - goto out;
> > - }
> > - if (new_o->pcrlock) {
> > - ret = pcrlock(new_o->pcrlock);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > if (ret < 0) {
> > - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> > - kzfree(new_p);
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > }
> > - rcu_assign_keypointer(key, new_p);
> > - call_rcu(&p->rcu, trusted_rcu_free);
> > out:
> > - kzfree(datablob);
> > - kzfree(new_o);
> > + kzfree(options);
> > return ret;
> > }
> >
> > -/*
> > - * trusted_read - copy the sealed blob data to userspace in hex.
> > - * On success, return to userspace the trusted key datablob size.
> > - */
> > -static long trusted_read(const struct key *key, char *buffer,
> > - size_t buflen)
> > -{
> > - const struct trusted_key_payload *p;
> > - char *bufp;
> > - int i;
> > -
> > - p = dereference_key_locked(key);
> > - if (!p)
> > - return -EINVAL;
> > -
> > - if (buffer && buflen >= 2 * p->blob_len) {
> > - bufp = buffer;
> > - for (i = 0; i < p->blob_len; i++)
> > - bufp = hex_byte_pack(bufp, p->blob[i]);
> > - }
> > - return 2 * p->blob_len;
> > -}
> > -
> > -/*
> > - * trusted_destroy - clear and free the key's payload
> > - */
> > -static void trusted_destroy(struct key *key)
> > +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> > {
> > - kzfree(key->payload.data[0]);
> > + return tpm_get_random(chip, key, key_len);
> > }
> >
> > -struct key_type key_type_trusted = {
> > - .name = "trusted",
> > - .instantiate = trusted_instantiate,
> > - .update = trusted_update,
> > - .destroy = trusted_destroy,
> > - .describe = user_describe,
> > - .read = trusted_read,
> > -};
> > -
> > -EXPORT_SYMBOL_GPL(key_type_trusted);
> > -
> > static void trusted_shash_release(void)
> > {
> > if (hashalg)
> > @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
> >
> > hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> > if (IS_ERR(hmacalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hmac_alg);
> > return PTR_ERR(hmacalg);
> > }
> >
> > hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> > if (IS_ERR(hashalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hash_alg);
> > ret = PTR_ERR(hashalg);
> > goto hashalg_fail;
> > @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> > return 0;
> > }
> >
> > -static int __init init_trusted(void)
> > +static int __init init_tpm_trusted(void)
> > {
> > int ret;
> >
> > - /* encrypted_keys.ko depends on successful load of this module even if
> > - * TPM is not used.
> > - */
> > chip = tpm_default_chip();
> > if (!chip)
> > - return 0;
> > + return -ENODEV;
> >
> > ret = init_digests();
> > if (ret < 0)
> > @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> > return ret;
> > }
> >
> > -static void __exit cleanup_trusted(void)
> > +static void __exit exit_tpm_trusted(void)
> > {
> > if (chip) {
> > put_device(&chip->dev);
> > @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> > }
> > }
> >
> > -late_initcall(init_trusted);
> > -module_exit(cleanup_trusted);
> > -
> > -MODULE_LICENSE("GPL");
> > +struct trusted_key_ops tpm_trusted_key_ops = {
> > + .migratable = 1, /* migratable by default */
> > + .init = init_tpm_trusted,
> > + .seal = tpm_trusted_seal,
> > + .unseal = tpm_trusted_unseal,
> > + .get_random = tpm_trusted_get_random,
> > + .exit = exit_tpm_trusted,
> > +};
> > +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
>
> Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
>
> > 2.7.4
> >
>
> /Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-16 13:32 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-16 13:32 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
+ Luke
Hi Jarkko,
Prior to addressing your comments below which seems to show your
preference for compile time selection of trust source (TPM or TEE), I
would just like to hear the reasons for this preference especially if
it makes distro vendor's life difficult [1] to make opinionated
selection which could rather be achieved dynamically based on platform
capability.
[1] https://lkml.org/lkml/2020/6/3/405
-Sumit
On Mon, 15 Jun 2020 at 23:55, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> > diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> > index 47c0415..22632c6 100644
> > --- a/security/keys/Kconfig
> > +++ b/security/keys/Kconfig
> > @@ -72,17 +72,26 @@ config BIG_KEYS
> >
> > config TRUSTED_KEYS
> > tristate "TRUSTED KEYS"
> > - depends on KEYS && TCG_TPM
> > + depends on KEYS
> > + help
> > + This option provides support for creating, sealing, and unsealing
> > + keys in the kernel. Trusted keys are random number symmetric keys,
> > + generated and sealed by a trust source (TPM or TEE). Userspace will
> > + only ever see encrypted blobs.
> > +
> > + If you are unsure as to whether this is required, answer N.
> > +
> > +config TRUSTED_TPM
>
> TRUSTED_TPM_KEYS
>
> > + bool "TPM based TRUSTED KEYS"
> > + depends on TRUSTED_KEYS && TCG_TPM
> > select CRYPTO
> > select CRYPTO_HMAC
> > select CRYPTO_SHA1
> > select CRYPTO_HASH_INFO
> > help
> > - This option provides support for creating, sealing, and unsealing
> > - keys in the kernel. Trusted keys are random number symmetric keys,
> > - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> > - if the boot PCRs and other criteria match. Userspace will only ever
> > - see encrypted blobs.
> > + This option provides support for TPM based trusted keys where TPM acts
> > + as the trust source. The keys are RSA-sealed by the TPM and it only
> > + unseals the keys, if the boot PCRs and other criteria match.
> >
> > If you are unsure as to whether this is required, answer N.
> >
> > diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> > index 7b73ceb..03c3ad4 100644
> > --- a/security/keys/trusted-keys/Makefile
> > +++ b/security/keys/trusted-keys/Makefile
> > @@ -4,5 +4,6 @@
> > #
> >
> > obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> > -trusted-y += trusted_tpm1.o
> > -trusted-y += trusted_tpm2.o
> > +trusted-y += trusted_core.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> > diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> > new file mode 100644
> > index 0000000..3d07d27
> > --- /dev/null
> > +++ b/security/keys/trusted-keys/trusted_core.c
> > @@ -0,0 +1,319 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > + *
> > + * Author:
> > + * David Safford <safford@us.ibm.com>
> > + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
>
> Rather remove David's because
>
> 1. Git log gives a verifiable and exact log of authority.
> 2. David's commit was done post-epoch of Git.
>
> > + *
> > + * See Documentation/security/keys/trusted-encrypted.rst
> > + */
> > +
> > +#include <keys/user-type.h>
> > +#include <keys/trusted-type.h>
> > +#include <linux/capability.h>
> > +#include <linux/err.h>
> > +#include <linux/init.h>
> > +#include <linux/key-type.h>
> > +#include <linux/module.h>
> > +#include <linux/parser.h>
> > +#include <linux/rcupdate.h>
> > +#include <linux/slab.h>
> > +#include <linux/string.h>
> > +#include <linux/uaccess.h>
> > +
> > +#if defined(CONFIG_TRUSTED_TPM)
> > +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> > +#else
> > +static struct trusted_key_ops *trusted_key_ops;
> > +#endif
>
> You should just have
>
> extern trusted_key_ops *trusted_key_ops;
>
> > +
> > +enum {
> > + Opt_err,
> > + Opt_new, Opt_load, Opt_update,
> > +};
> > +
> > +static const match_table_t key_tokens = {
> > + {Opt_new, "new"},
> > + {Opt_load, "load"},
> > + {Opt_update, "update"},
> > + {Opt_err, NULL}
> > +};
> > +
> > +/*
> > + * datablob_parse - parse the keyctl data and fill in the
> > + * payload structure
> > + *
> > + * On success returns 0, otherwise -EINVAL.
> > + */
> > +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> > +{
> > + substring_t args[MAX_OPT_ARGS];
> > + long keylen;
> > + int ret = -EINVAL;
> > + int key_cmd;
> > + char *c;
> > +
> > + /* main command */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + key_cmd = match_token(c, key_tokens, args);
> > + switch (key_cmd) {
> > + case Opt_new:
> > + /* first argument is key size */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + ret = kstrtol(c, 10, &keylen);
> > + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > + return -EINVAL;
> > + p->key_len = keylen;
> > + ret = Opt_new;
> > + break;
> > + case Opt_load:
> > + /* first argument is sealed blob */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + p->blob_len = strlen(c) / 2;
> > + if (p->blob_len > MAX_BLOB_SIZE)
> > + return -EINVAL;
> > + ret = hex2bin(p->blob, c, p->blob_len);
> > + if (ret < 0)
> > + return -EINVAL;
> > + ret = Opt_load;
> > + break;
> > + case Opt_update:
> > + ret = Opt_update;
> > + break;
> > + case Opt_err:
> > + return -EINVAL;
> > + }
> > + return ret;
> > +}
> > +
> > +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +{
> > + struct trusted_key_payload *p = NULL;
> > + int ret;
> > +
> > + ret = key_payload_reserve(key, sizeof(*p));
> > + if (ret < 0)
> > + return p;
> > + p = kzalloc(sizeof(*p), GFP_KERNEL);
> > +
> > + p->migratable = trusted_key_ops->migratable;
> > +
> > + return p;
> > +}
> > +
> > +/*
> > + * trusted_instantiate - create a new trusted key
> > + *
> > + * Unseal an existing trusted blob or, for a new key, get a
> > + * random key, then seal and create a trusted key-type key,
> > + * adding it to the specified keyring.
> > + *
> > + * On success, return 0. Otherwise return errno.
> > + */
> > +static int trusted_instantiate(struct key *key,
> > + struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *payload = NULL;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > + int key_cmd;
> > + size_t key_len;
> > +
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > +
> > + payload = trusted_payload_alloc(key);
> > + if (!payload) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + key_cmd = datablob_parse(datablob, payload);
> > + if (key_cmd < 0) {
> > + ret = key_cmd;
> > + goto out;
> > + }
> > +
> > + dump_payload(payload);
> > +
> > + switch (key_cmd) {
> > + case Opt_load:
> > + ret = trusted_key_ops->unseal(payload, datablob);
> > + dump_payload(payload);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > + break;
> > + case Opt_new:
> > + key_len = payload->key_len;
> > + ret = trusted_key_ops->get_random(payload->key, key_len);
> > + if (ret != key_len) {
> > + pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + goto out;
> > + }
> > +
> > + ret = trusted_key_ops->seal(payload, datablob);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > +out:
> > + kzfree(datablob);
> > + if (!ret)
> > + rcu_assign_keypointer(key, payload);
> > + else
> > + kzfree(payload);
> > + return ret;
> > +}
> > +
> > +static void trusted_rcu_free(struct rcu_head *rcu)
> > +{
> > + struct trusted_key_payload *p;
> > +
> > + p = container_of(rcu, struct trusted_key_payload, rcu);
> > + kzfree(p);
> > +}
> > +
> > +/*
> > + * trusted_update - reseal an existing key with new PCR values
> > + */
> > +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *p;
> > + struct trusted_key_payload *new_p;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > +
> > + if (key_is_negative(key))
> > + return -ENOKEY;
> > + p = key->payload.data[0];
> > + if (!p->migratable)
> > + return -EPERM;
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > +
> > + new_p = trusted_payload_alloc(key);
> > + if (!new_p) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > + ret = datablob_parse(datablob, new_p);
> > + if (ret != Opt_update) {
> > + ret = -EINVAL;
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + /* copy old key values, and reseal with new pcrs */
> > + new_p->migratable = p->migratable;
> > + new_p->key_len = p->key_len;
> > + memcpy(new_p->key, p->key, p->key_len);
> > + dump_payload(p);
> > + dump_payload(new_p);
> > +
> > + ret = trusted_key_ops->seal(new_p, datablob);
> > + if (ret < 0) {
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + rcu_assign_keypointer(key, new_p);
> > + call_rcu(&p->rcu, trusted_rcu_free);
> > +out:
> > + kzfree(datablob);
> > + return ret;
> > +}
> > +
> > +/*
> > + * trusted_read - copy the sealed blob data to userspace in hex.
> > + * On success, return to userspace the trusted key datablob size.
> > + */
> > +static long trusted_read(const struct key *key, char *buffer,
> > + size_t buflen)
> > +{
> > + const struct trusted_key_payload *p;
> > + char *bufp;
> > + int i;
> > +
> > + p = dereference_key_locked(key);
> > + if (!p)
> > + return -EINVAL;
> > +
> > + if (buffer && buflen >= 2 * p->blob_len) {
> > + bufp = buffer;
> > + for (i = 0; i < p->blob_len; i++)
> > + bufp = hex_byte_pack(bufp, p->blob[i]);
> > + }
> > + return 2 * p->blob_len;
> > +}
> > +
> > +/*
> > + * trusted_destroy - clear and free the key's payload
> > + */
> > +static void trusted_destroy(struct key *key)
> > +{
> > + kzfree(key->payload.data[0]);
> > +}
> > +
> > +struct key_type key_type_trusted = {
> > + .name = "trusted",
> > + .instantiate = trusted_instantiate,
> > + .update = trusted_update,
> > + .destroy = trusted_destroy,
> > + .describe = user_describe,
> > + .read = trusted_read,
> > +};
> > +EXPORT_SYMBOL_GPL(key_type_trusted);
> > +
> > +static int __init init_trusted(void)
> > +{
> > + int ret;
> > +
> > + /*
> > + * encrypted_keys.ko depends on successful load of this module even if
> > + * trusted key implementation is not found.
> > + */
> > + if (!trusted_key_ops)
> > + return 0;
> > +
> > + ret = trusted_key_ops->init();
> > + if (ret == -ENODEV)
> > + return 0;
> > +
> > + return ret;
> > +}
> > +
> > +static void __exit cleanup_trusted(void)
> > +{
> > + trusted_key_ops->exit();
> > +}
> > +
> > +late_initcall(init_trusted);
> > +module_exit(cleanup_trusted);
> > +
> > +MODULE_LICENSE("GPL");
> > diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> > index 8001ab0..e378b27 100644
> > --- a/security/keys/trusted-keys/trusted_tpm1.c
> > +++ b/security/keys/trusted-keys/trusted_tpm1.c
> > @@ -1,29 +1,26 @@
> > // SPDX-License-Identifier: GPL-2.0-only
> > /*
> > * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > *
> > * Author:
> > * David Safford <safford@us.ibm.com>
> > + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> > *
> > * See Documentation/security/keys/trusted-encrypted.rst
> > */
> >
> > #include <crypto/hash_info.h>
> > -#include <linux/uaccess.h>
> > -#include <linux/module.h>
> > #include <linux/init.h>
> > #include <linux/slab.h>
> > #include <linux/parser.h>
> > #include <linux/string.h>
> > #include <linux/err.h>
> > -#include <keys/user-type.h>
> > #include <keys/trusted-type.h>
> > #include <linux/key-type.h>
> > -#include <linux/rcupdate.h>
> > #include <linux/crypto.h>
> > #include <crypto/hash.h>
> > #include <crypto/sha.h>
> > -#include <linux/capability.h>
> > #include <linux/tpm.h>
> > #include <linux/tpm_command.h>
> >
> > @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
> >
> > enum {
> > Opt_err,
> > - Opt_new, Opt_load, Opt_update,
> > Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> > Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> > Opt_hash,
> > @@ -712,9 +708,6 @@ enum {
> > };
> >
> > static const match_table_t key_tokens = {
> > - {Opt_new, "new"},
> > - {Opt_load, "load"},
> > - {Opt_update, "update"},
> > {Opt_keyhandle, "keyhandle=%s"},
> > {Opt_keyauth, "keyauth=%s"},
> > {Opt_blobauth, "blobauth=%s"},
> > @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> > return 0;
> > }
> >
> > -/*
> > - * datablob_parse - parse the keyctl data and fill in the
> > - * payload and options structures
> > - *
> > - * On success returns 0, otherwise -EINVAL.
> > - */
> > -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> > - struct trusted_key_options *o)
> > -{
> > - substring_t args[MAX_OPT_ARGS];
> > - long keylen;
> > - int ret = -EINVAL;
> > - int key_cmd;
> > - char *c;
> > -
> > - /* main command */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - key_cmd = match_token(c, key_tokens, args);
> > - switch (key_cmd) {
> > - case Opt_new:
> > - /* first argument is key size */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - ret = kstrtol(c, 10, &keylen);
> > - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > - return -EINVAL;
> > - p->key_len = keylen;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_new;
> > - break;
> > - case Opt_load:
> > - /* first argument is sealed blob */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - p->blob_len = strlen(c) / 2;
> > - if (p->blob_len > MAX_BLOB_SIZE)
> > - return -EINVAL;
> > - ret = hex2bin(p->blob, c, p->blob_len);
> > - if (ret < 0)
> > - return -EINVAL;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_load;
> > - break;
> > - case Opt_update:
> > - /* all arguments are options */
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_update;
> > - break;
> > - case Opt_err:
> > - return -EINVAL;
> > - break;
> > - }
> > - return ret;
> > -}
> > -
> > static struct trusted_key_options *trusted_options_alloc(void)
> > {
> > struct trusted_key_options *options;
> > @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> > return options;
> > }
> >
> > -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p = NULL;
> > - int ret;
> > -
> > - ret = key_payload_reserve(key, sizeof *p);
> > - if (ret < 0)
> > - return p;
> > - p = kzalloc(sizeof *p, GFP_KERNEL);
> > - if (p)
> > - p->migratable = 1; /* migratable by default */
> > - return p;
> > -}
> > -
> > -/*
> > - * trusted_instantiate - create a new trusted key
> > - *
> > - * Unseal an existing trusted blob or, for a new key, get a
> > - * random key, then seal and create a trusted key-type key,
> > - * adding it to the specified keyring.
> > - *
> > - * On success, return 0. Otherwise return errno.
> > - */
> > -static int trusted_instantiate(struct key *key,
> > - struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *payload = NULL;
> > struct trusted_key_options *options = NULL;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > int ret = 0;
> > - int key_cmd;
> > - size_t key_len;
> > int tpm2;
> >
> > tpm2 = tpm_is_tpm2(chip);
> > if (tpm2 < 0)
> > return tpm2;
> >
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > -
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > - return -ENOMEM;
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > -
> > options = trusted_options_alloc();
> > - if (!options) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - payload = trusted_payload_alloc(key);
> > - if (!payload) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > + if (!options)
> > + return -ENOMEM;
> >
> > - key_cmd = datablob_parse(datablob, payload, options);
> > - if (key_cmd < 0) {
> > - ret = key_cmd;
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > if (!options->keyhandle) {
> > ret = -EINVAL;
> > goto out;
> > }
> >
> > - dump_payload(payload);
> > - dump_options(options);
> > + if (tpm2)
> > + ret = tpm2_seal_trusted(chip, p, options);
> > + else
> > + ret = key_seal(p, options);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> > + goto out;
> > + }
> >
> > - switch (key_cmd) {
> > - case Opt_load:
> > - if (tpm2)
> > - ret = tpm2_unseal_trusted(chip, payload, options);
> > - else
> > - ret = key_unseal(payload, options);
> > - dump_payload(payload);
> > - dump_options(options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > - break;
> > - case Opt_new:
> > - key_len = payload->key_len;
> > - ret = tpm_get_random(chip, payload->key, key_len);
> > - if (ret != key_len) {
> > - pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > - if (tpm2)
> > - ret = tpm2_seal_trusted(chip, payload, options);
> > - else
> > - ret = key_seal(payload, options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - break;
> > - default:
> > - ret = -EINVAL;
> > - goto out;
> > }
> > - if (!ret && options->pcrlock)
> > - ret = pcrlock(options->pcrlock);
> > out:
> > - kzfree(datablob);
> > kzfree(options);
> > - if (!ret)
> > - rcu_assign_keypointer(key, payload);
> > - else
> > - kzfree(payload);
> > return ret;
> > }
> >
> > -static void trusted_rcu_free(struct rcu_head *rcu)
> > +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p;
> > -
> > - p = container_of(rcu, struct trusted_key_payload, rcu);
> > - kzfree(p);
> > -}
> > -
> > -/*
> > - * trusted_update - reseal an existing key with new PCR values
> > - */
> > -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *p;
> > - struct trusted_key_payload *new_p;
> > - struct trusted_key_options *new_o;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > + struct trusted_key_options *options = NULL;
> > int ret = 0;
> > + int tpm2;
> >
> > - if (key_is_negative(key))
> > - return -ENOKEY;
> > - p = key->payload.data[0];
> > - if (!p->migratable)
> > - return -EPERM;
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > + tpm2 = tpm_is_tpm2(chip);
> > + if (tpm2 < 0)
> > + return tpm2;
> >
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > + options = trusted_options_alloc();
> > + if (!options)
> > return -ENOMEM;
> > - new_o = trusted_options_alloc();
> > - if (!new_o) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - new_p = trusted_payload_alloc(key);
> > - if (!new_p) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> >
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > - ret = datablob_parse(datablob, new_p, new_o);
> > - if (ret != Opt_update) {
> > - ret = -EINVAL;
> > - kzfree(new_p);
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > - if (!new_o->keyhandle) {
> > + if (!options->keyhandle) {
> > ret = -EINVAL;
> > - kzfree(new_p);
> > goto out;
> > }
> >
> > - /* copy old key values, and reseal with new pcrs */
> > - new_p->migratable = p->migratable;
> > - new_p->key_len = p->key_len;
> > - memcpy(new_p->key, p->key, p->key_len);
> > - dump_payload(p);
> > - dump_payload(new_p);
> > + if (tpm2)
> > + ret = tpm2_unseal_trusted(chip, p, options);
> > + else
> > + ret = key_unseal(p, options);
> > + if (ret < 0)
> > + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
> >
> > - ret = key_seal(new_p, new_o);
> > - if (ret < 0) {
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - kzfree(new_p);
> > - goto out;
> > - }
> > - if (new_o->pcrlock) {
> > - ret = pcrlock(new_o->pcrlock);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > if (ret < 0) {
> > - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> > - kzfree(new_p);
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > }
> > - rcu_assign_keypointer(key, new_p);
> > - call_rcu(&p->rcu, trusted_rcu_free);
> > out:
> > - kzfree(datablob);
> > - kzfree(new_o);
> > + kzfree(options);
> > return ret;
> > }
> >
> > -/*
> > - * trusted_read - copy the sealed blob data to userspace in hex.
> > - * On success, return to userspace the trusted key datablob size.
> > - */
> > -static long trusted_read(const struct key *key, char *buffer,
> > - size_t buflen)
> > -{
> > - const struct trusted_key_payload *p;
> > - char *bufp;
> > - int i;
> > -
> > - p = dereference_key_locked(key);
> > - if (!p)
> > - return -EINVAL;
> > -
> > - if (buffer && buflen >= 2 * p->blob_len) {
> > - bufp = buffer;
> > - for (i = 0; i < p->blob_len; i++)
> > - bufp = hex_byte_pack(bufp, p->blob[i]);
> > - }
> > - return 2 * p->blob_len;
> > -}
> > -
> > -/*
> > - * trusted_destroy - clear and free the key's payload
> > - */
> > -static void trusted_destroy(struct key *key)
> > +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> > {
> > - kzfree(key->payload.data[0]);
> > + return tpm_get_random(chip, key, key_len);
> > }
> >
> > -struct key_type key_type_trusted = {
> > - .name = "trusted",
> > - .instantiate = trusted_instantiate,
> > - .update = trusted_update,
> > - .destroy = trusted_destroy,
> > - .describe = user_describe,
> > - .read = trusted_read,
> > -};
> > -
> > -EXPORT_SYMBOL_GPL(key_type_trusted);
> > -
> > static void trusted_shash_release(void)
> > {
> > if (hashalg)
> > @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
> >
> > hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> > if (IS_ERR(hmacalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hmac_alg);
> > return PTR_ERR(hmacalg);
> > }
> >
> > hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> > if (IS_ERR(hashalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hash_alg);
> > ret = PTR_ERR(hashalg);
> > goto hashalg_fail;
> > @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> > return 0;
> > }
> >
> > -static int __init init_trusted(void)
> > +static int __init init_tpm_trusted(void)
> > {
> > int ret;
> >
> > - /* encrypted_keys.ko depends on successful load of this module even if
> > - * TPM is not used.
> > - */
> > chip = tpm_default_chip();
> > if (!chip)
> > - return 0;
> > + return -ENODEV;
> >
> > ret = init_digests();
> > if (ret < 0)
> > @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> > return ret;
> > }
> >
> > -static void __exit cleanup_trusted(void)
> > +static void __exit exit_tpm_trusted(void)
> > {
> > if (chip) {
> > put_device(&chip->dev);
> > @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> > }
> > }
> >
> > -late_initcall(init_trusted);
> > -module_exit(cleanup_trusted);
> > -
> > -MODULE_LICENSE("GPL");
> > +struct trusted_key_ops tpm_trusted_key_ops = {
> > + .migratable = 1, /* migratable by default */
> > + .init = init_tpm_trusted,
> > + .seal = tpm_trusted_seal,
> > + .unseal = tpm_trusted_unseal,
> > + .get_random = tpm_trusted_get_random,
> > + .exit = exit_tpm_trusted,
> > +};
> > +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
>
> Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
>
> > 2.7.4
> >
>
> /Jarkko
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-16 13:32 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-16 13:32 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 29880 bytes --]
+ Luke
Hi Jarkko,
Prior to addressing your comments below which seems to show your
preference for compile time selection of trust source (TPM or TEE), I
would just like to hear the reasons for this preference especially if
it makes distro vendor's life difficult [1] to make opinionated
selection which could rather be achieved dynamically based on platform
capability.
[1] https://lkml.org/lkml/2020/6/3/405
-Sumit
On Mon, 15 Jun 2020 at 23:55, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 02, 2020 at 07:48:22PM +0530, Sumit Garg wrote:
> > diff --git a/security/keys/Kconfig b/security/keys/Kconfig
> > index 47c0415..22632c6 100644
> > --- a/security/keys/Kconfig
> > +++ b/security/keys/Kconfig
> > @@ -72,17 +72,26 @@ config BIG_KEYS
> >
> > config TRUSTED_KEYS
> > tristate "TRUSTED KEYS"
> > - depends on KEYS && TCG_TPM
> > + depends on KEYS
> > + help
> > + This option provides support for creating, sealing, and unsealing
> > + keys in the kernel. Trusted keys are random number symmetric keys,
> > + generated and sealed by a trust source (TPM or TEE). Userspace will
> > + only ever see encrypted blobs.
> > +
> > + If you are unsure as to whether this is required, answer N.
> > +
> > +config TRUSTED_TPM
>
> TRUSTED_TPM_KEYS
>
> > + bool "TPM based TRUSTED KEYS"
> > + depends on TRUSTED_KEYS && TCG_TPM
> > select CRYPTO
> > select CRYPTO_HMAC
> > select CRYPTO_SHA1
> > select CRYPTO_HASH_INFO
> > help
> > - This option provides support for creating, sealing, and unsealing
> > - keys in the kernel. Trusted keys are random number symmetric keys,
> > - generated and RSA-sealed by the TPM. The TPM only unseals the keys,
> > - if the boot PCRs and other criteria match. Userspace will only ever
> > - see encrypted blobs.
> > + This option provides support for TPM based trusted keys where TPM acts
> > + as the trust source. The keys are RSA-sealed by the TPM and it only
> > + unseals the keys, if the boot PCRs and other criteria match.
> >
> > If you are unsure as to whether this is required, answer N.
> >
> > diff --git a/security/keys/trusted-keys/Makefile b/security/keys/trusted-keys/Makefile
> > index 7b73ceb..03c3ad4 100644
> > --- a/security/keys/trusted-keys/Makefile
> > +++ b/security/keys/trusted-keys/Makefile
> > @@ -4,5 +4,6 @@
> > #
> >
> > obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
> > -trusted-y += trusted_tpm1.o
> > -trusted-y += trusted_tpm2.o
> > +trusted-y += trusted_core.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm1.o
> > +trusted-$(CONFIG_TRUSTED_TPM) += trusted_tpm2.o
> > diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> > new file mode 100644
> > index 0000000..3d07d27
> > --- /dev/null
> > +++ b/security/keys/trusted-keys/trusted_core.c
> > @@ -0,0 +1,319 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > + *
> > + * Author:
> > + * David Safford <safford@us.ibm.com>
> > + * Added generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
>
> Rather remove David's because
>
> 1. Git log gives a verifiable and exact log of authority.
> 2. David's commit was done post-epoch of Git.
>
> > + *
> > + * See Documentation/security/keys/trusted-encrypted.rst
> > + */
> > +
> > +#include <keys/user-type.h>
> > +#include <keys/trusted-type.h>
> > +#include <linux/capability.h>
> > +#include <linux/err.h>
> > +#include <linux/init.h>
> > +#include <linux/key-type.h>
> > +#include <linux/module.h>
> > +#include <linux/parser.h>
> > +#include <linux/rcupdate.h>
> > +#include <linux/slab.h>
> > +#include <linux/string.h>
> > +#include <linux/uaccess.h>
> > +
> > +#if defined(CONFIG_TRUSTED_TPM)
> > +static struct trusted_key_ops *trusted_key_ops = &tpm_trusted_key_ops;
> > +#else
> > +static struct trusted_key_ops *trusted_key_ops;
> > +#endif
>
> You should just have
>
> extern trusted_key_ops *trusted_key_ops;
>
> > +
> > +enum {
> > + Opt_err,
> > + Opt_new, Opt_load, Opt_update,
> > +};
> > +
> > +static const match_table_t key_tokens = {
> > + {Opt_new, "new"},
> > + {Opt_load, "load"},
> > + {Opt_update, "update"},
> > + {Opt_err, NULL}
> > +};
> > +
> > +/*
> > + * datablob_parse - parse the keyctl data and fill in the
> > + * payload structure
> > + *
> > + * On success returns 0, otherwise -EINVAL.
> > + */
> > +static int datablob_parse(char *datablob, struct trusted_key_payload *p)
> > +{
> > + substring_t args[MAX_OPT_ARGS];
> > + long keylen;
> > + int ret = -EINVAL;
> > + int key_cmd;
> > + char *c;
> > +
> > + /* main command */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + key_cmd = match_token(c, key_tokens, args);
> > + switch (key_cmd) {
> > + case Opt_new:
> > + /* first argument is key size */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + ret = kstrtol(c, 10, &keylen);
> > + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > + return -EINVAL;
> > + p->key_len = keylen;
> > + ret = Opt_new;
> > + break;
> > + case Opt_load:
> > + /* first argument is sealed blob */
> > + c = strsep(&datablob, " \t");
> > + if (!c)
> > + return -EINVAL;
> > + p->blob_len = strlen(c) / 2;
> > + if (p->blob_len > MAX_BLOB_SIZE)
> > + return -EINVAL;
> > + ret = hex2bin(p->blob, c, p->blob_len);
> > + if (ret < 0)
> > + return -EINVAL;
> > + ret = Opt_load;
> > + break;
> > + case Opt_update:
> > + ret = Opt_update;
> > + break;
> > + case Opt_err:
> > + return -EINVAL;
> > + }
> > + return ret;
> > +}
> > +
> > +static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +{
> > + struct trusted_key_payload *p = NULL;
> > + int ret;
> > +
> > + ret = key_payload_reserve(key, sizeof(*p));
> > + if (ret < 0)
> > + return p;
> > + p = kzalloc(sizeof(*p), GFP_KERNEL);
> > +
> > + p->migratable = trusted_key_ops->migratable;
> > +
> > + return p;
> > +}
> > +
> > +/*
> > + * trusted_instantiate - create a new trusted key
> > + *
> > + * Unseal an existing trusted blob or, for a new key, get a
> > + * random key, then seal and create a trusted key-type key,
> > + * adding it to the specified keyring.
> > + *
> > + * On success, return 0. Otherwise return errno.
> > + */
> > +static int trusted_instantiate(struct key *key,
> > + struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *payload = NULL;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > + int key_cmd;
> > + size_t key_len;
> > +
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > +
> > + payload = trusted_payload_alloc(key);
> > + if (!payload) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + key_cmd = datablob_parse(datablob, payload);
> > + if (key_cmd < 0) {
> > + ret = key_cmd;
> > + goto out;
> > + }
> > +
> > + dump_payload(payload);
> > +
> > + switch (key_cmd) {
> > + case Opt_load:
> > + ret = trusted_key_ops->unseal(payload, datablob);
> > + dump_payload(payload);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > + break;
> > + case Opt_new:
> > + key_len = payload->key_len;
> > + ret = trusted_key_ops->get_random(payload->key, key_len);
> > + if (ret != key_len) {
> > + pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + goto out;
> > + }
> > +
> > + ret = trusted_key_ops->seal(payload, datablob);
> > + if (ret < 0)
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > +out:
> > + kzfree(datablob);
> > + if (!ret)
> > + rcu_assign_keypointer(key, payload);
> > + else
> > + kzfree(payload);
> > + return ret;
> > +}
> > +
> > +static void trusted_rcu_free(struct rcu_head *rcu)
> > +{
> > + struct trusted_key_payload *p;
> > +
> > + p = container_of(rcu, struct trusted_key_payload, rcu);
> > + kzfree(p);
> > +}
> > +
> > +/*
> > + * trusted_update - reseal an existing key with new PCR values
> > + */
> > +static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > +{
> > + struct trusted_key_payload *p;
> > + struct trusted_key_payload *new_p;
> > + size_t datalen = prep->datalen;
> > + char *datablob;
> > + int ret = 0;
> > +
> > + if (key_is_negative(key))
> > + return -ENOKEY;
> > + p = key->payload.data[0];
> > + if (!p->migratable)
> > + return -EPERM;
> > + if (datalen <= 0 || datalen > 32767 || !prep->data)
> > + return -EINVAL;
> > +
> > + datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > + if (!datablob)
> > + return -ENOMEM;
> > +
> > + new_p = trusted_payload_alloc(key);
> > + if (!new_p) {
> > + ret = -ENOMEM;
> > + goto out;
> > + }
> > +
> > + memcpy(datablob, prep->data, datalen);
> > + datablob[datalen] = '\0';
> > + ret = datablob_parse(datablob, new_p);
> > + if (ret != Opt_update) {
> > + ret = -EINVAL;
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + /* copy old key values, and reseal with new pcrs */
> > + new_p->migratable = p->migratable;
> > + new_p->key_len = p->key_len;
> > + memcpy(new_p->key, p->key, p->key_len);
> > + dump_payload(p);
> > + dump_payload(new_p);
> > +
> > + ret = trusted_key_ops->seal(new_p, datablob);
> > + if (ret < 0) {
> > + pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > + kzfree(new_p);
> > + goto out;
> > + }
> > +
> > + rcu_assign_keypointer(key, new_p);
> > + call_rcu(&p->rcu, trusted_rcu_free);
> > +out:
> > + kzfree(datablob);
> > + return ret;
> > +}
> > +
> > +/*
> > + * trusted_read - copy the sealed blob data to userspace in hex.
> > + * On success, return to userspace the trusted key datablob size.
> > + */
> > +static long trusted_read(const struct key *key, char *buffer,
> > + size_t buflen)
> > +{
> > + const struct trusted_key_payload *p;
> > + char *bufp;
> > + int i;
> > +
> > + p = dereference_key_locked(key);
> > + if (!p)
> > + return -EINVAL;
> > +
> > + if (buffer && buflen >= 2 * p->blob_len) {
> > + bufp = buffer;
> > + for (i = 0; i < p->blob_len; i++)
> > + bufp = hex_byte_pack(bufp, p->blob[i]);
> > + }
> > + return 2 * p->blob_len;
> > +}
> > +
> > +/*
> > + * trusted_destroy - clear and free the key's payload
> > + */
> > +static void trusted_destroy(struct key *key)
> > +{
> > + kzfree(key->payload.data[0]);
> > +}
> > +
> > +struct key_type key_type_trusted = {
> > + .name = "trusted",
> > + .instantiate = trusted_instantiate,
> > + .update = trusted_update,
> > + .destroy = trusted_destroy,
> > + .describe = user_describe,
> > + .read = trusted_read,
> > +};
> > +EXPORT_SYMBOL_GPL(key_type_trusted);
> > +
> > +static int __init init_trusted(void)
> > +{
> > + int ret;
> > +
> > + /*
> > + * encrypted_keys.ko depends on successful load of this module even if
> > + * trusted key implementation is not found.
> > + */
> > + if (!trusted_key_ops)
> > + return 0;
> > +
> > + ret = trusted_key_ops->init();
> > + if (ret == -ENODEV)
> > + return 0;
> > +
> > + return ret;
> > +}
> > +
> > +static void __exit cleanup_trusted(void)
> > +{
> > + trusted_key_ops->exit();
> > +}
> > +
> > +late_initcall(init_trusted);
> > +module_exit(cleanup_trusted);
> > +
> > +MODULE_LICENSE("GPL");
> > diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> > index 8001ab0..e378b27 100644
> > --- a/security/keys/trusted-keys/trusted_tpm1.c
> > +++ b/security/keys/trusted-keys/trusted_tpm1.c
> > @@ -1,29 +1,26 @@
> > // SPDX-License-Identifier: GPL-2.0-only
> > /*
> > * Copyright (C) 2010 IBM Corporation
> > + * Copyright (c) 2019, Linaro Limited
> > *
> > * Author:
> > * David Safford <safford@us.ibm.com>
> > + * Switch to generic trusted key framework: Sumit Garg <sumit.garg@linaro.org>
> > *
> > * See Documentation/security/keys/trusted-encrypted.rst
> > */
> >
> > #include <crypto/hash_info.h>
> > -#include <linux/uaccess.h>
> > -#include <linux/module.h>
> > #include <linux/init.h>
> > #include <linux/slab.h>
> > #include <linux/parser.h>
> > #include <linux/string.h>
> > #include <linux/err.h>
> > -#include <keys/user-type.h>
> > #include <keys/trusted-type.h>
> > #include <linux/key-type.h>
> > -#include <linux/rcupdate.h>
> > #include <linux/crypto.h>
> > #include <crypto/hash.h>
> > #include <crypto/sha.h>
> > -#include <linux/capability.h>
> > #include <linux/tpm.h>
> > #include <linux/tpm_command.h>
> >
> > @@ -703,7 +700,6 @@ static int key_unseal(struct trusted_key_payload *p,
> >
> > enum {
> > Opt_err,
> > - Opt_new, Opt_load, Opt_update,
> > Opt_keyhandle, Opt_keyauth, Opt_blobauth,
> > Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
> > Opt_hash,
> > @@ -712,9 +708,6 @@ enum {
> > };
> >
> > static const match_table_t key_tokens = {
> > - {Opt_new, "new"},
> > - {Opt_load, "load"},
> > - {Opt_update, "update"},
> > {Opt_keyhandle, "keyhandle=%s"},
> > {Opt_keyauth, "keyauth=%s"},
> > {Opt_blobauth, "blobauth=%s"},
> > @@ -841,71 +834,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
> > return 0;
> > }
> >
> > -/*
> > - * datablob_parse - parse the keyctl data and fill in the
> > - * payload and options structures
> > - *
> > - * On success returns 0, otherwise -EINVAL.
> > - */
> > -static int datablob_parse(char *datablob, struct trusted_key_payload *p,
> > - struct trusted_key_options *o)
> > -{
> > - substring_t args[MAX_OPT_ARGS];
> > - long keylen;
> > - int ret = -EINVAL;
> > - int key_cmd;
> > - char *c;
> > -
> > - /* main command */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - key_cmd = match_token(c, key_tokens, args);
> > - switch (key_cmd) {
> > - case Opt_new:
> > - /* first argument is key size */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - ret = kstrtol(c, 10, &keylen);
> > - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
> > - return -EINVAL;
> > - p->key_len = keylen;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_new;
> > - break;
> > - case Opt_load:
> > - /* first argument is sealed blob */
> > - c = strsep(&datablob, " \t");
> > - if (!c)
> > - return -EINVAL;
> > - p->blob_len = strlen(c) / 2;
> > - if (p->blob_len > MAX_BLOB_SIZE)
> > - return -EINVAL;
> > - ret = hex2bin(p->blob, c, p->blob_len);
> > - if (ret < 0)
> > - return -EINVAL;
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_load;
> > - break;
> > - case Opt_update:
> > - /* all arguments are options */
> > - ret = getoptions(datablob, p, o);
> > - if (ret < 0)
> > - return ret;
> > - ret = Opt_update;
> > - break;
> > - case Opt_err:
> > - return -EINVAL;
> > - break;
> > - }
> > - return ret;
> > -}
> > -
> > static struct trusted_key_options *trusted_options_alloc(void)
> > {
> > struct trusted_key_options *options;
> > @@ -926,248 +854,99 @@ static struct trusted_key_options *trusted_options_alloc(void)
> > return options;
> > }
> >
> > -static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
> > +static int tpm_trusted_seal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p = NULL;
> > - int ret;
> > -
> > - ret = key_payload_reserve(key, sizeof *p);
> > - if (ret < 0)
> > - return p;
> > - p = kzalloc(sizeof *p, GFP_KERNEL);
> > - if (p)
> > - p->migratable = 1; /* migratable by default */
> > - return p;
> > -}
> > -
> > -/*
> > - * trusted_instantiate - create a new trusted key
> > - *
> > - * Unseal an existing trusted blob or, for a new key, get a
> > - * random key, then seal and create a trusted key-type key,
> > - * adding it to the specified keyring.
> > - *
> > - * On success, return 0. Otherwise return errno.
> > - */
> > -static int trusted_instantiate(struct key *key,
> > - struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *payload = NULL;
> > struct trusted_key_options *options = NULL;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > int ret = 0;
> > - int key_cmd;
> > - size_t key_len;
> > int tpm2;
> >
> > tpm2 = tpm_is_tpm2(chip);
> > if (tpm2 < 0)
> > return tpm2;
> >
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > -
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > - return -ENOMEM;
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > -
> > options = trusted_options_alloc();
> > - if (!options) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - payload = trusted_payload_alloc(key);
> > - if (!payload) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > + if (!options)
> > + return -ENOMEM;
> >
> > - key_cmd = datablob_parse(datablob, payload, options);
> > - if (key_cmd < 0) {
> > - ret = key_cmd;
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > if (!options->keyhandle) {
> > ret = -EINVAL;
> > goto out;
> > }
> >
> > - dump_payload(payload);
> > - dump_options(options);
> > + if (tpm2)
> > + ret = tpm2_seal_trusted(chip, p, options);
> > + else
> > + ret = key_seal(p, options);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: key_seal failed (%d)\n", ret);
> > + goto out;
> > + }
> >
> > - switch (key_cmd) {
> > - case Opt_load:
> > - if (tpm2)
> > - ret = tpm2_unseal_trusted(chip, payload, options);
> > - else
> > - ret = key_unseal(payload, options);
> > - dump_payload(payload);
> > - dump_options(options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_unseal failed (%d)\n", ret);
> > - break;
> > - case Opt_new:
> > - key_len = payload->key_len;
> > - ret = tpm_get_random(chip, payload->key, key_len);
> > - if (ret != key_len) {
> > - pr_info("trusted_key: key_create failed (%d)\n", ret);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > + if (ret < 0) {
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > - if (tpm2)
> > - ret = tpm2_seal_trusted(chip, payload, options);
> > - else
> > - ret = key_seal(payload, options);
> > - if (ret < 0)
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - break;
> > - default:
> > - ret = -EINVAL;
> > - goto out;
> > }
> > - if (!ret && options->pcrlock)
> > - ret = pcrlock(options->pcrlock);
> > out:
> > - kzfree(datablob);
> > kzfree(options);
> > - if (!ret)
> > - rcu_assign_keypointer(key, payload);
> > - else
> > - kzfree(payload);
> > return ret;
> > }
> >
> > -static void trusted_rcu_free(struct rcu_head *rcu)
> > +static int tpm_trusted_unseal(struct trusted_key_payload *p, char *datablob)
> > {
> > - struct trusted_key_payload *p;
> > -
> > - p = container_of(rcu, struct trusted_key_payload, rcu);
> > - kzfree(p);
> > -}
> > -
> > -/*
> > - * trusted_update - reseal an existing key with new PCR values
> > - */
> > -static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
> > -{
> > - struct trusted_key_payload *p;
> > - struct trusted_key_payload *new_p;
> > - struct trusted_key_options *new_o;
> > - size_t datalen = prep->datalen;
> > - char *datablob;
> > + struct trusted_key_options *options = NULL;
> > int ret = 0;
> > + int tpm2;
> >
> > - if (key_is_negative(key))
> > - return -ENOKEY;
> > - p = key->payload.data[0];
> > - if (!p->migratable)
> > - return -EPERM;
> > - if (datalen <= 0 || datalen > 32767 || !prep->data)
> > - return -EINVAL;
> > + tpm2 = tpm_is_tpm2(chip);
> > + if (tpm2 < 0)
> > + return tpm2;
> >
> > - datablob = kmalloc(datalen + 1, GFP_KERNEL);
> > - if (!datablob)
> > + options = trusted_options_alloc();
> > + if (!options)
> > return -ENOMEM;
> > - new_o = trusted_options_alloc();
> > - if (!new_o) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> > - new_p = trusted_payload_alloc(key);
> > - if (!new_p) {
> > - ret = -ENOMEM;
> > - goto out;
> > - }
> >
> > - memcpy(datablob, prep->data, datalen);
> > - datablob[datalen] = '\0';
> > - ret = datablob_parse(datablob, new_p, new_o);
> > - if (ret != Opt_update) {
> > - ret = -EINVAL;
> > - kzfree(new_p);
> > + ret = getoptions(datablob, p, options);
> > + if (ret < 0)
> > goto out;
> > - }
> > + dump_options(options);
> >
> > - if (!new_o->keyhandle) {
> > + if (!options->keyhandle) {
> > ret = -EINVAL;
> > - kzfree(new_p);
> > goto out;
> > }
> >
> > - /* copy old key values, and reseal with new pcrs */
> > - new_p->migratable = p->migratable;
> > - new_p->key_len = p->key_len;
> > - memcpy(new_p->key, p->key, p->key_len);
> > - dump_payload(p);
> > - dump_payload(new_p);
> > + if (tpm2)
> > + ret = tpm2_unseal_trusted(chip, p, options);
> > + else
> > + ret = key_unseal(p, options);
> > + if (ret < 0)
> > + pr_info("tpm_trusted_key: key_unseal failed (%d)\n", ret);
> >
> > - ret = key_seal(new_p, new_o);
> > - if (ret < 0) {
> > - pr_info("trusted_key: key_seal failed (%d)\n", ret);
> > - kzfree(new_p);
> > - goto out;
> > - }
> > - if (new_o->pcrlock) {
> > - ret = pcrlock(new_o->pcrlock);
> > + if (options->pcrlock) {
> > + ret = pcrlock(options->pcrlock);
> > if (ret < 0) {
> > - pr_info("trusted_key: pcrlock failed (%d)\n", ret);
> > - kzfree(new_p);
> > + pr_info("tpm_trusted_key: pcrlock failed (%d)\n", ret);
> > goto out;
> > }
> > }
> > - rcu_assign_keypointer(key, new_p);
> > - call_rcu(&p->rcu, trusted_rcu_free);
> > out:
> > - kzfree(datablob);
> > - kzfree(new_o);
> > + kzfree(options);
> > return ret;
> > }
> >
> > -/*
> > - * trusted_read - copy the sealed blob data to userspace in hex.
> > - * On success, return to userspace the trusted key datablob size.
> > - */
> > -static long trusted_read(const struct key *key, char *buffer,
> > - size_t buflen)
> > -{
> > - const struct trusted_key_payload *p;
> > - char *bufp;
> > - int i;
> > -
> > - p = dereference_key_locked(key);
> > - if (!p)
> > - return -EINVAL;
> > -
> > - if (buffer && buflen >= 2 * p->blob_len) {
> > - bufp = buffer;
> > - for (i = 0; i < p->blob_len; i++)
> > - bufp = hex_byte_pack(bufp, p->blob[i]);
> > - }
> > - return 2 * p->blob_len;
> > -}
> > -
> > -/*
> > - * trusted_destroy - clear and free the key's payload
> > - */
> > -static void trusted_destroy(struct key *key)
> > +int tpm_trusted_get_random(unsigned char *key, size_t key_len)
> > {
> > - kzfree(key->payload.data[0]);
> > + return tpm_get_random(chip, key, key_len);
> > }
> >
> > -struct key_type key_type_trusted = {
> > - .name = "trusted",
> > - .instantiate = trusted_instantiate,
> > - .update = trusted_update,
> > - .destroy = trusted_destroy,
> > - .describe = user_describe,
> > - .read = trusted_read,
> > -};
> > -
> > -EXPORT_SYMBOL_GPL(key_type_trusted);
> > -
> > static void trusted_shash_release(void)
> > {
> > if (hashalg)
> > @@ -1182,14 +961,14 @@ static int __init trusted_shash_alloc(void)
> >
> > hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
> > if (IS_ERR(hmacalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hmac_alg);
> > return PTR_ERR(hmacalg);
> > }
> >
> > hashalg = crypto_alloc_shash(hash_alg, 0, 0);
> > if (IS_ERR(hashalg)) {
> > - pr_info("trusted_key: could not allocate crypto %s\n",
> > + pr_info("tpm_trusted_key: could not allocate crypto %s\n",
> > hash_alg);
> > ret = PTR_ERR(hashalg);
> > goto hashalg_fail;
> > @@ -1217,16 +996,13 @@ static int __init init_digests(void)
> > return 0;
> > }
> >
> > -static int __init init_trusted(void)
> > +static int __init init_tpm_trusted(void)
> > {
> > int ret;
> >
> > - /* encrypted_keys.ko depends on successful load of this module even if
> > - * TPM is not used.
> > - */
> > chip = tpm_default_chip();
> > if (!chip)
> > - return 0;
> > + return -ENODEV;
> >
> > ret = init_digests();
> > if (ret < 0)
> > @@ -1247,7 +1023,7 @@ static int __init init_trusted(void)
> > return ret;
> > }
> >
> > -static void __exit cleanup_trusted(void)
> > +static void __exit exit_tpm_trusted(void)
> > {
> > if (chip) {
> > put_device(&chip->dev);
> > @@ -1257,7 +1033,12 @@ static void __exit cleanup_trusted(void)
> > }
> > }
> >
> > -late_initcall(init_trusted);
> > -module_exit(cleanup_trusted);
> > -
> > -MODULE_LICENSE("GPL");
> > +struct trusted_key_ops tpm_trusted_key_ops = {
> > + .migratable = 1, /* migratable by default */
> > + .init = init_tpm_trusted,
> > + .seal = tpm_trusted_seal,
> > + .unseal = tpm_trusted_unseal,
> > + .get_random = tpm_trusted_get_random,
> > + .exit = exit_tpm_trusted,
> > +};
> > +EXPORT_SYMBOL_GPL(tpm_trusted_key_ops); > --
>
> Rename this as trusted_key_ops and remove EXPORT_SYMBOL_GPL()
>
> > 2.7.4
> >
>
> /Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-16 13:32 ` Sumit Garg
(?)
(?)
@ 2020-06-17 23:14 ` Jarkko Sakkinen
-1 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-17 23:14 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> + Luke
>
> Hi Jarkko,
>
> Prior to addressing your comments below which seems to show your
> preference for compile time selection of trust source (TPM or TEE), I
> would just like to hear the reasons for this preference especially if
> it makes distro vendor's life difficult [1] to make opinionated
> selection which could rather be achieved dynamically based on platform
> capability.
>
> [1] https://lkml.org/lkml/2020/6/3/405
>
> -Sumit
Hmm... I do get the distribution kernel point. OK, lets revert to
dynamic then. Thanks for the remark.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-17 23:14 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-17 23:14 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> + Luke
>
> Hi Jarkko,
>
> Prior to addressing your comments below which seems to show your
> preference for compile time selection of trust source (TPM or TEE), I
> would just like to hear the reasons for this preference especially if
> it makes distro vendor's life difficult [1] to make opinionated
> selection which could rather be achieved dynamically based on platform
> capability.
>
> [1] https://lkml.org/lkml/2020/6/3/405
>
> -Sumit
Hmm... I do get the distribution kernel point. OK, lets revert to
dynamic then. Thanks for the remark.
/Jarkko
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-17 23:14 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-17 23:14 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 621 bytes --]
On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> + Luke
>
> Hi Jarkko,
>
> Prior to addressing your comments below which seems to show your
> preference for compile time selection of trust source (TPM or TEE), I
> would just like to hear the reasons for this preference especially if
> it makes distro vendor's life difficult [1] to make opinionated
> selection which could rather be achieved dynamically based on platform
> capability.
>
> [1] https://lkml.org/lkml/2020/6/3/405
>
> -Sumit
Hmm... I do get the distribution kernel point. OK, lets revert to
dynamic then. Thanks for the remark.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-17 23:14 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-17 23:14 UTC (permalink / raw)
To: Sumit Garg
Cc: Mimi Zohar, James Bottomley, David Howells, Jens Wiklander,
Jonathan Corbet, James Morris, Serge E. Hallyn, Casey Schaufler,
Janne Karhunen, Daniel Thompson, Markus Wamser,
open list:ASYMMETRIC KEYS, linux-integrity, linux-security-module,
Linux Doc Mailing List, Linux Kernel Mailing List,
linux-arm-kernel, op-tee, tee-dev @ lists . linaro . org,
Luke Hinds
On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> + Luke
>
> Hi Jarkko,
>
> Prior to addressing your comments below which seems to show your
> preference for compile time selection of trust source (TPM or TEE), I
> would just like to hear the reasons for this preference especially if
> it makes distro vendor's life difficult [1] to make opinionated
> selection which could rather be achieved dynamically based on platform
> capability.
>
> [1] https://lkml.org/lkml/2020/6/3/405
>
> -Sumit
Hmm... I do get the distribution kernel point. OK, lets revert to
dynamic then. Thanks for the remark.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-17 23:14 ` Jarkko Sakkinen
(?)
(?)
@ 2020-06-18 6:42 ` Sumit Garg
-1 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-18 6:42 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: Mimi Zohar, James Bottomley, David Howells, Jens Wiklander,
Jonathan Corbet, James Morris, Serge E. Hallyn, Casey Schaufler,
Janne Karhunen, Daniel Thompson, Markus Wamser,
open list:ASYMMETRIC KEYS, linux-integrity, linux-security-module,
Linux Doc Mailing List, Linux Kernel Mailing List,
linux-arm-kernel, op-tee, tee-dev @ lists . linaro . org,
Luke Hinds
On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > + Luke
> >
> > Hi Jarkko,
> >
> > Prior to addressing your comments below which seems to show your
> > preference for compile time selection of trust source (TPM or TEE), I
> > would just like to hear the reasons for this preference especially if
> > it makes distro vendor's life difficult [1] to make opinionated
> > selection which could rather be achieved dynamically based on platform
> > capability.
> >
> > [1] https://lkml.org/lkml/2020/6/3/405
> >
> > -Sumit
>
> Hmm... I do get the distribution kernel point. OK, lets revert to
> dynamic then. Thanks for the remark.
>
> /Jarkko
Thanks, will revert to dynamic mode in v6.
-Sumit
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 6:42 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-18 6:54 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > + Luke
> >
> > Hi Jarkko,
> >
> > Prior to addressing your comments below which seems to show your
> > preference for compile time selection of trust source (TPM or TEE), I
> > would just like to hear the reasons for this preference especially if
> > it makes distro vendor's life difficult [1] to make opinionated
> > selection which could rather be achieved dynamically based on platform
> > capability.
> >
> > [1] https://lkml.org/lkml/2020/6/3/405
> >
> > -Sumit
>
> Hmm... I do get the distribution kernel point. OK, lets revert to
> dynamic then. Thanks for the remark.
>
> /Jarkko
Thanks, will revert to dynamic mode in v6.
-Sumit
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 6:42 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-18 6:42 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > + Luke
> >
> > Hi Jarkko,
> >
> > Prior to addressing your comments below which seems to show your
> > preference for compile time selection of trust source (TPM or TEE), I
> > would just like to hear the reasons for this preference especially if
> > it makes distro vendor's life difficult [1] to make opinionated
> > selection which could rather be achieved dynamically based on platform
> > capability.
> >
> > [1] https://lkml.org/lkml/2020/6/3/405
> >
> > -Sumit
>
> Hmm... I do get the distribution kernel point. OK, lets revert to
> dynamic then. Thanks for the remark.
>
> /Jarkko
Thanks, will revert to dynamic mode in v6.
-Sumit
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 6:42 ` Sumit Garg
0 siblings, 0 replies; 56+ messages in thread
From: Sumit Garg @ 2020-06-18 6:42 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 796 bytes --]
On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
<jarkko.sakkinen@linux.intel.com> wrote:
>
> On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > + Luke
> >
> > Hi Jarkko,
> >
> > Prior to addressing your comments below which seems to show your
> > preference for compile time selection of trust source (TPM or TEE), I
> > would just like to hear the reasons for this preference especially if
> > it makes distro vendor's life difficult [1] to make opinionated
> > selection which could rather be achieved dynamically based on platform
> > capability.
> >
> > [1] https://lkml.org/lkml/2020/6/3/405
> >
> > -Sumit
>
> Hmm... I do get the distribution kernel point. OK, lets revert to
> dynamic then. Thanks for the remark.
>
> /Jarkko
Thanks, will revert to dynamic mode in v6.
-Sumit
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
2020-06-18 6:42 ` Sumit Garg
(?)
(?)
@ 2020-06-18 7:19 ` Jarkko Sakkinen
-1 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-18 7:19 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Thu, Jun 18, 2020 at 12:12:41PM +0530, Sumit Garg wrote:
> On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
> <jarkko.sakkinen@linux.intel.com> wrote:
> >
> > On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > > + Luke
> > >
> > > Hi Jarkko,
> > >
> > > Prior to addressing your comments below which seems to show your
> > > preference for compile time selection of trust source (TPM or TEE), I
> > > would just like to hear the reasons for this preference especially if
> > > it makes distro vendor's life difficult [1] to make opinionated
> > > selection which could rather be achieved dynamically based on platform
> > > capability.
> > >
> > > [1] https://lkml.org/lkml/2020/6/3/405
> > >
> > > -Sumit
> >
> > Hmm... I do get the distribution kernel point. OK, lets revert to
> > dynamic then. Thanks for the remark.
> >
> > /Jarkko
>
> Thanks, will revert to dynamic mode in v6.
Sorry about the extra trouble caused by me.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 7:19 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-18 7:19 UTC (permalink / raw)
To: Sumit Garg
Cc: tee-dev @ lists . linaro . org, Daniel Thompson, Luke Hinds,
op-tee, Jonathan Corbet, James Bottomley, Janne Karhunen,
Linux Doc Mailing List, James Morris, Mimi Zohar,
Linux Kernel Mailing List, David Howells, linux-security-module,
open list:ASYMMETRIC KEYS, Markus Wamser, Casey Schaufler,
linux-integrity, Jens Wiklander, linux-arm-kernel,
Serge E. Hallyn
On Thu, Jun 18, 2020 at 12:12:41PM +0530, Sumit Garg wrote:
> On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
> <jarkko.sakkinen@linux.intel.com> wrote:
> >
> > On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > > + Luke
> > >
> > > Hi Jarkko,
> > >
> > > Prior to addressing your comments below which seems to show your
> > > preference for compile time selection of trust source (TPM or TEE), I
> > > would just like to hear the reasons for this preference especially if
> > > it makes distro vendor's life difficult [1] to make opinionated
> > > selection which could rather be achieved dynamically based on platform
> > > capability.
> > >
> > > [1] https://lkml.org/lkml/2020/6/3/405
> > >
> > > -Sumit
> >
> > Hmm... I do get the distribution kernel point. OK, lets revert to
> > dynamic then. Thanks for the remark.
> >
> > /Jarkko
>
> Thanks, will revert to dynamic mode in v6.
Sorry about the extra trouble caused by me.
/Jarkko
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 7:19 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-18 7:19 UTC (permalink / raw)
To: op-tee
[-- Attachment #1: Type: text/plain, Size: 952 bytes --]
On Thu, Jun 18, 2020 at 12:12:41PM +0530, Sumit Garg wrote:
> On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
> <jarkko.sakkinen@linux.intel.com> wrote:
> >
> > On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > > + Luke
> > >
> > > Hi Jarkko,
> > >
> > > Prior to addressing your comments below which seems to show your
> > > preference for compile time selection of trust source (TPM or TEE), I
> > > would just like to hear the reasons for this preference especially if
> > > it makes distro vendor's life difficult [1] to make opinionated
> > > selection which could rather be achieved dynamically based on platform
> > > capability.
> > >
> > > [1] https://lkml.org/lkml/2020/6/3/405
> > >
> > > -Sumit
> >
> > Hmm... I do get the distribution kernel point. OK, lets revert to
> > dynamic then. Thanks for the remark.
> >
> > /Jarkko
>
> Thanks, will revert to dynamic mode in v6.
Sorry about the extra trouble caused by me.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread
* Re: [PATCH v5 1/4] KEYS: trusted: Add generic trusted keys framework
@ 2020-06-18 7:19 ` Jarkko Sakkinen
0 siblings, 0 replies; 56+ messages in thread
From: Jarkko Sakkinen @ 2020-06-18 7:19 UTC (permalink / raw)
To: Sumit Garg
Cc: Mimi Zohar, James Bottomley, David Howells, Jens Wiklander,
Jonathan Corbet, James Morris, Serge E. Hallyn, Casey Schaufler,
Janne Karhunen, Daniel Thompson, Markus Wamser,
open list:ASYMMETRIC KEYS, linux-integrity, linux-security-module,
Linux Doc Mailing List, Linux Kernel Mailing List,
linux-arm-kernel, op-tee, tee-dev @ lists . linaro . org,
Luke Hinds
On Thu, Jun 18, 2020 at 12:12:41PM +0530, Sumit Garg wrote:
> On Thu, 18 Jun 2020 at 04:44, Jarkko Sakkinen
> <jarkko.sakkinen@linux.intel.com> wrote:
> >
> > On Tue, Jun 16, 2020 at 07:02:37PM +0530, Sumit Garg wrote:
> > > + Luke
> > >
> > > Hi Jarkko,
> > >
> > > Prior to addressing your comments below which seems to show your
> > > preference for compile time selection of trust source (TPM or TEE), I
> > > would just like to hear the reasons for this preference especially if
> > > it makes distro vendor's life difficult [1] to make opinionated
> > > selection which could rather be achieved dynamically based on platform
> > > capability.
> > >
> > > [1] https://lkml.org/lkml/2020/6/3/405
> > >
> > > -Sumit
> >
> > Hmm... I do get the distribution kernel point. OK, lets revert to
> > dynamic then. Thanks for the remark.
> >
> > /Jarkko
>
> Thanks, will revert to dynamic mode in v6.
Sorry about the extra trouble caused by me.
/Jarkko
^ permalink raw reply [flat|nested] 56+ messages in thread