Skip to contentSkip to conversation

Introduce TEE based Trusted Keys support

RFC Superseded Sumit Garg 5 participants 27 messages Jul 30, 2019 – Aug 9, 2019

Subsystems (7)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

v2 cover Introduce TEE based Trusted Keys support

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Also, this is an alternative in case platform doesn't possess a TPM device.

This series also adds some TEE features like:

Patch #1, #2 enables support for registered kernel shared memory with TEE.

Patch #3 enables support for private kernel login method required for cases like trusted keys where we don't wan't user-space to directly access TEE service to retrieve trusted key contents.

Rest of the patches from #4 to #6 adds support for TEE based trusted keys.

This patch-set has been tested with OP-TEE based pseudo TA which can be found here [1].

Also, this patch-set is dependent on generic Trusted Keys framework patch-set [2].

[1] https://github.com/OP-TEE/optee_os/pull/3082
[2] https://lkml.org/lkml/2019/7/18/284

Changes in v2:
1. Add reviewed-by tags for patch #1 and #2.
2. Incorporate comments from Jens for patch #3.
3. Switch to use generic trusted keys framework.

Sumit Garg (6):
tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

 Documentation/security/keys/index.rst       |   1 +
 Documentation/security/keys/tee-trusted.rst |  93 +++++++++
 MAINTAINERS                                 |   9 +
 drivers/tee/optee/call.c                    |   7 +
 drivers/tee/tee_core.c                      |   6 +
 drivers/tee/tee_shm.c                       |  16 +-
 include/keys/trusted-type.h                 |   3 +
 include/keys/trusted_tee.h                  |  66 +++++++
 include/linux/tee_drv.h                     |   1 +
 include/uapi/linux/tee.h                    |   8 +
 security/keys/Kconfig                       |   3 +
 security/keys/trusted-keys/Makefile         |   3 +-
 security/keys/trusted-keys/trusted-tee.c    | 282 ++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c        |   3 +
 14 files changed, 498 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c
Signature · 1 line
2.7.4
author
#1 lore raw

RFC v2 1/6 tee: optee: allow kernel pages to register as shm

Kernel pages are marked as normal type memory only so allow kernel pages to be registered as shared memory with OP-TEE.

---
 drivers/tee/optee/call.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/tee/optee/call.c +7 -0
index aa94270..bce45b1 100644
@@ -553,6 +553,13 @@ static int check_mem_type(unsigned long start, size_t num_pages)
 	struct mm_struct *mm = current->mm;
 	int rc;
 
+	/*
+	 * Allow kernel address to register with OP-TEE as kernel
+	 * pages are configured as normal memory only.
+	 */
+	if (virt_addr_valid(start))
+		return 0;
+
 	down_read(&mm->mmap_sem);
 	rc = __check_mem_type(find_vma(mm, start),
 			      start + num_pages * PAGE_SIZE);
Signature · 1 line
2.7.4
author
#2 lore raw

RFC v2 2/6 tee: enable support to register kernel memory

In reply to Sumit Garg

Enable support to register kernel memory reference with TEE. This change will allow TEE bus drivers to register memory references.

---
 drivers/tee/tee_shm.c   | 16 ++++++++++++++--
 include/linux/tee_drv.h |  1 +
 2 files changed, 15 insertions(+), 2 deletions(-)
drivers/tee/tee_shm.c +14 -2
index 2da026f..5c69b89 100644
@@ -9,6 +9,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/tee_drv.h>
+#include <linux/uio.h>
 #include "tee_private.h"
 
 static void tee_shm_release(struct tee_shm *shm)
@@ -224,13 +225,14 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 {
 	struct tee_device *teedev = ctx->teedev;
 	const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
+	const u32 req_ker_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
 	struct tee_shm *shm;
 	void *ret;
 	int rc;
 	int num_pages;
 	unsigned long start;
 
-	if (flags != req_flags)
+	if (flags != req_flags && flags != req_ker_flags)
 		return ERR_PTR(-ENOTSUPP);
 
 	if (!tee_device_get(teedev))
@@ -264,7 +266,17 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 		goto err;
 	}
 
-	rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
+	if (flags & TEE_SHM_USER_MAPPED) {
+		rc = get_user_pages_fast(start, num_pages, FOLL_WRITE,
+					 shm->pages);
+	} else {
+		const struct kvec kiov = {
+			.iov_base = (void *)start,
+			.iov_len = PAGE_SIZE
+		};
+
+		rc = get_kernel_pages(&kiov, num_pages, 0, shm->pages);
+	}
 	if (rc > 0)
 		shm->num_pages = rc;
 	if (rc != num_pages) {
include/linux/tee_drv.h +1 -0
index 7a03f68..dedf8fa 100644
@@ -26,6 +26,7 @@
 #define TEE_SHM_REGISTER	BIT(3)  /* Memory registered in secure world */
 #define TEE_SHM_USER_MAPPED	BIT(4)  /* Memory mapped in user space */
 #define TEE_SHM_POOL		BIT(5)  /* Memory allocated from pool */
+#define TEE_SHM_KERNEL_MAPPED	BIT(6)  /* Memory mapped in kernel space */
 
 struct device;
 struct tee_device;
Signature · 1 line
2.7.4
author
#5 lore raw

RFC v2 3/6 tee: add private login method for kernel clients

In reply to Sumit Garg

There are use-cases where user-space shouldn't be allowed to communicate directly with a TEE device which is dedicated to provide a specific service for a kernel client. So add a private login method for kernel clients and disallow user-space to open-session using GP implementation defined login method range: (0x80000000 - 0xFFFFFFFF).

---
 drivers/tee/tee_core.c   | 6 ++++++
 include/uapi/linux/tee.h | 8 ++++++++
 2 files changed, 14 insertions(+)
drivers/tee/tee_core.c +6 -0
index 0f16d9f..2c2f646 100644
@@ -334,6 +334,12 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
 			goto out;
 	}
 
+	if (arg.clnt_login & TEE_IOCTL_LOGIN_MASK) {
+		pr_debug("login method not allowed for user-space client\n");
+		rc = -EPERM;
+		goto out;
+	}
+
 	rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
 	if (rc)
 		goto out;
include/uapi/linux/tee.h +8 -0
index 4b9eb06..a0a3d52 100644
@@ -172,6 +172,14 @@ struct tee_ioctl_buf_data {
 #define TEE_IOCTL_LOGIN_APPLICATION		4
 #define TEE_IOCTL_LOGIN_USER_APPLICATION	5
 #define TEE_IOCTL_LOGIN_GROUP_APPLICATION	6
+/*
+ * Disallow user-space to use GP implementation specific login
+ * method range (0x80000000 - 0xFFFFFFFF). This range is rather
+ * being reserved for REE kernel clients or TEE implementation.
+ */
+#define TEE_IOCTL_LOGIN_MASK			0x80000000
+/* Private login method for REE kernel clients */
+#define TEE_IOCTL_LOGIN_REE_KERNEL		0x80000000
 
 /**
  * struct tee_ioctl_param - parameter
Signature · 1 line
2.7.4
author
#6 lore raw

RFC v2 4/6 KEYS: trusted: Introduce TEE based Trusted Keys

In reply to Sumit Garg

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key.

Refer to Documentation/tee.txt for detailed information about TEE.

---
 include/keys/trusted-type.h              |   3 +
 include/keys/trusted_tee.h               |  66 ++++++++
 security/keys/Kconfig                    |   3 +
 security/keys/trusted-keys/Makefile      |   3 +-
 security/keys/trusted-keys/trusted-tee.c | 282 +++++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c     |   3 +
 6 files changed, 359 insertions(+), 1 deletion(-)
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c
include/keys/trusted-type.h +3 -0
index 5559010..e0df5df 100644
@@ -67,6 +67,9 @@ extern struct key_type key_type_trusted;
 #if defined(CONFIG_TCG_TPM)
 extern struct trusted_key_ops tpm_trusted_key_ops;
 #endif
+#if defined(CONFIG_TEE)
+extern struct trusted_key_ops tee_trusted_key_ops;
+#endif
 
 #define TRUSTED_DEBUG 0
 
include/keys/trusted_tee.h new file +66 -0

66 lines not on this page · raw message

security/keys/Kconfig +3 -0
index dd31343..0d5e37c 100644
@@ -88,6 +88,9 @@ config TRUSTED_KEYS
 	  if the boot PCRs and other criteria match.  Userspace will only ever
 	  see encrypted blobs.
 
+	  It also provides support for alternative TEE based Trusted keys
+	  generation and sealing in case TPM isn't present.
+
 	  If you are unsure as to whether this is required, answer N.
 
 config ENCRYPTED_KEYS
security/keys/trusted-keys/Makefile +2 -1
index 6ecadfb..5fcf2ae 100644
@@ -4,4 +4,5 @@
 #
 
 obj-$(CONFIG_TRUSTED_KEYS) += trusted.o \
-			trusted-tpm.o
+			trusted-tpm.o \
+			trusted-tee.o
security/keys/trusted-keys/trusted-tee.c new file +282 -0

282 lines not on this page · raw message

security/keys/trusted-keys/trusted.c +3 -0
index 8f00fde..a0a171f 100644
@@ -27,6 +27,9 @@ static struct trusted_key_ops *available_tk_ops[] = {
 #if defined(CONFIG_TCG_TPM)
 	&tpm_trusted_key_ops,
 #endif
+#if defined(CONFIG_TEE)
+	&tee_trusted_key_ops,
+#endif
 };
 static struct trusted_key_ops *tk_ops;
 
Signature · 1 line
2.7.4
author
#7 lore raw

RFC v2 5/6 doc: keys: Document usage of TEE based Trusted Keys

In reply to Sumit Garg

Provide documentation for usage of TEE based Trusted Keys via existing user-space "keyctl" utility. Also, document various use-cases.

---
 Documentation/security/keys/index.rst       |  1 +
 Documentation/security/keys/tee-trusted.rst | 93 +++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
Documentation/security/keys/index.rst +1 -0
index 647d58f..f9ef557 100644
@@ -9,3 +9,4 @@ Kernel Keys
    ecryptfs
    request-key
    trusted-encrypted
+   tee-trusted
Documentation/security/keys/tee-trusted.rst new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#8 lore raw

RFC v2 6/6 MAINTAINERS: Add entry for TEE based Trusted Keys

In reply to Sumit Garg

Add MAINTAINERS entry for TEE based Trusted Keys framework.

---
 MAINTAINERS | 9 +++++++++
 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
index ce06877..0b61ecf 100644
@@ -8930,6 +8930,15 @@ F:	include/keys/trusted-type.h
 F:	security/keys/trusted.c
 F:	security/keys/trusted.h
 
+KEYS-TEE-TRUSTED
+M:	Sumit Garg <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Supported
+F:	Documentation/security/keys/tee-trusted.rst
+F:	include/keys/trusted_tee.h
+F:	security/keys/trusted-keys/trusted-tee.c
+
 KEYS/KEYRINGS:
 M:	David Howells <[email protected]>
 L:	[email protected]
Signature · 1 line
2.7.4
#9 lore raw
In reply to Sumit Garg

Hi,

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Since there are no generic 'TEEs' available, I implemented the same thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

--
Janne

Quoted 59 lines · Sumit Garg

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Also, this is an alternative in case platform doesn't possess a TPM device.

This series also adds some TEE features like:

Patch #1, #2 enables support for registered kernel shared memory with TEE.

Patch #3 enables support for private kernel login method required for cases like trusted keys where we don't wan't user-space to directly access TEE service to retrieve trusted key contents.

Rest of the patches from #4 to #6 adds support for TEE based trusted keys.

This patch-set has been tested with OP-TEE based pseudo TA which can be found here [1].

Also, this patch-set is dependent on generic Trusted Keys framework patch-set [2].

[1] https://github.com/OP-TEE/optee_os/pull/3082
[2] https://lkml.org/lkml/2019/7/18/284

Changes in v2:
1. Add reviewed-by tags for patch #1 and #2.
2. Incorporate comments from Jens for patch #3.
3. Switch to use generic trusted keys framework.

Sumit Garg (6):
tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

 Documentation/security/keys/index.rst       |   1 +
 Documentation/security/keys/tee-trusted.rst |  93 +++++++++
 MAINTAINERS                                 |   9 +
 drivers/tee/optee/call.c                    |   7 +
 drivers/tee/tee_core.c                      |   6 +
 drivers/tee/tee_shm.c                       |  16 +-
 include/keys/trusted-type.h                 |   3 +
 include/keys/trusted_tee.h                  |  66 +++++++
 include/linux/tee_drv.h                     |   1 +
 include/uapi/linux/tee.h                    |   8 +
 security/keys/Kconfig                       |   3 +
 security/keys/trusted-keys/Makefile         |   3 +-
 security/keys/trusted-keys/trusted-tee.c    | 282 ++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c        |   3 +
 14 files changed, 498 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c

--
2.7.4

#10 lore raw

Hi,

To clarify a bit further - my thought was to support any type of trust source. Remote, local or both. Just having one particular type of locally bound 'TEE' sounded very limited, especially when nothing from the TEE execution side is really needed for supporting the kernel crypto. What you really need is the seal/unseal transaction going somewhere and where that somewhere is does not matter much. With the user mode helper in between anyone can easily add their own thing in there.

--
Janne

Quoted 79 lines · Janne Karhunen

Hi,

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Since there are no generic 'TEEs' available, I implemented the same thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

--
Janne

Quoted 59 lines · Sumit Garg

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Also, this is an alternative in case platform doesn't possess a TPM device.

This series also adds some TEE features like:

Patch #1, #2 enables support for registered kernel shared memory with TEE.

Patch #3 enables support for private kernel login method required for cases like trusted keys where we don't wan't user-space to directly access TEE service to retrieve trusted key contents.

Rest of the patches from #4 to #6 adds support for TEE based trusted keys.

This patch-set has been tested with OP-TEE based pseudo TA which can be found here [1].

Also, this patch-set is dependent on generic Trusted Keys framework patch-set [2].

[1] https://github.com/OP-TEE/optee_os/pull/3082
[2] https://lkml.org/lkml/2019/7/18/284

Changes in v2:
1. Add reviewed-by tags for patch #1 and #2.
2. Incorporate comments from Jens for patch #3.
3. Switch to use generic trusted keys framework.

Sumit Garg (6):
tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

 Documentation/security/keys/index.rst       |   1 +
 Documentation/security/keys/tee-trusted.rst |  93 +++++++++
 MAINTAINERS                                 |   9 +
 drivers/tee/optee/call.c                    |   7 +
 drivers/tee/tee_core.c                      |   6 +
 drivers/tee/tee_shm.c                       |  16 +-
 include/keys/trusted-type.h                 |   3 +
 include/keys/trusted_tee.h                  |  66 +++++++
 include/linux/tee_drv.h                     |   1 +
 include/uapi/linux/tee.h                    |   8 +
 security/keys/Kconfig                       |   3 +
 security/keys/trusted-keys/Makefile         |   3 +-
 security/keys/trusted-keys/trusted-tee.c    | 282 ++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c        |   3 +
 14 files changed, 498 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c

--
2.7.4

author
#17 lore raw
In reply to Janne Karhunen

Hi Janne,

Quoted 7 lines · Janne Karhunen

Hi,

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Since there are no generic 'TEEs' available,

There is already a generic TEE interface driver available in kernel. Have a look here: "Documentation/tee.txt".

Quoted 6 lines

I implemented the same
thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

If you look at patch #1 and #2, they add support to register kernel memory buffer (keydata buffer in this case) with TEE to operate on. So there isn't any limitation due to the size of the keydata.

-Sumit

Quoted 67 lines

--
Janne

Quoted 59 lines · Sumit Garg

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Also, this is an alternative in case platform doesn't possess a TPM device.

This series also adds some TEE features like:

Patch #1, #2 enables support for registered kernel shared memory with TEE.

Patch #3 enables support for private kernel login method required for cases like trusted keys where we don't wan't user-space to directly access TEE service to retrieve trusted key contents.

Rest of the patches from #4 to #6 adds support for TEE based trusted keys.

This patch-set has been tested with OP-TEE based pseudo TA which can be found here [1].

Also, this patch-set is dependent on generic Trusted Keys framework patch-set [2].

[1] https://github.com/OP-TEE/optee_os/pull/3082
[2] https://lkml.org/lkml/2019/7/18/284

Changes in v2:
1. Add reviewed-by tags for patch #1 and #2.
2. Incorporate comments from Jens for patch #3.
3. Switch to use generic trusted keys framework.

Sumit Garg (6):
tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

 Documentation/security/keys/index.rst       |   1 +
 Documentation/security/keys/tee-trusted.rst |  93 +++++++++
 MAINTAINERS                                 |   9 +
 drivers/tee/optee/call.c                    |   7 +
 drivers/tee/tee_core.c                      |   6 +
 drivers/tee/tee_shm.c                       |  16 +-
 include/keys/trusted-type.h                 |   3 +
 include/keys/trusted_tee.h                  |  66 +++++++
 include/linux/tee_drv.h                     |   1 +
 include/uapi/linux/tee.h                    |   8 +
 security/keys/Kconfig                       |   3 +
 security/keys/trusted-keys/Makefile         |   3 +-
 security/keys/trusted-keys/trusted-tee.c    | 282 ++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c        |   3 +
 14 files changed, 498 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c

--
2.7.4

#18 lore raw
Quoted 7 lines · Sumit Garg
Quoted 4 lines

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Since there are no generic 'TEEs' available,

There is already a generic TEE interface driver available in kernel. Have a look here: "Documentation/tee.txt".

I guess my wording was wrong, tried to say that physical TEEs in the wild vary massively hardware wise. Generalizing these things is rough.

Quoted 10 lines
Quoted 6 lines

I implemented the same
thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

If you look at patch #1 and #2, they add support to register kernel memory buffer (keydata buffer in this case) with TEE to operate on. So there isn't any limitation due to the size of the keydata.

Ah, didn't mean that. Meant that the keydata is typically pretty small in size, so there is limited benefit from passing that in via shm if that complicates anything.

Signature · 1 line
Janne
author
#11 lore raw
In reply to Janne Karhunen
Quoted 5 lines · Janne Karhunen

Hi,

To clarify a bit further - my thought was to support any type of trust source.

That could be very well accomplished via Trusted Keys abstraction framework [1]. A trust source just need to implement following APIs:

struct trusted_key_ops ts_trusted_key_ops = {
.migratable = 0, /* non-migratable */
.init = init_ts_trusted,
.seal = ts_key_seal,
.unseal = ts_key_unseal,
.get_random = ts_get_random,
.cleanup = cleanup_ts_trusted,
};

Quoted 2 lines

Remote, local or both. Just having one particular type of locally bound 'TEE' sounded very limited,

TEE is just one of trust source like TPM, we can have other trust source as mentioned above.

Quoted 4 lines

especially when nothing from
the TEE execution side is really needed for supporting the kernel crypto. What you really need is the seal/unseal transaction going somewhere and where that somewhere is does not matter much.

Its only the seal/unseal operations that are provided by TEE driver that hooks up under trusted keys abstraction layer.

Quoted 4 lines

With the
user mode helper in between anyone can easily add their own thing in there.

Isn't actual purpose to have trusted keys is to protect user-space from access to kernel keys in plain format? Doesn't user mode helper defeat that purpose in one way or another?

Quoted 1 line

[1] https://lkml.org/lkml/2019/7/18/284

-Sumit

Quoted 84 lines

--
Janne

Quoted 79 lines · Janne Karhunen

Hi,

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Since there are no generic 'TEEs' available, I implemented the same thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

--
Janne

Quoted 59 lines · Sumit Garg

Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Also, this is an alternative in case platform doesn't possess a TPM device.

This series also adds some TEE features like:

Patch #1, #2 enables support for registered kernel shared memory with TEE.

Patch #3 enables support for private kernel login method required for cases like trusted keys where we don't wan't user-space to directly access TEE service to retrieve trusted key contents.

Rest of the patches from #4 to #6 adds support for TEE based trusted keys.

This patch-set has been tested with OP-TEE based pseudo TA which can be found here [1].

Also, this patch-set is dependent on generic Trusted Keys framework patch-set [2].

[1] https://github.com/OP-TEE/optee_os/pull/3082
[2] https://lkml.org/lkml/2019/7/18/284

Changes in v2:
1. Add reviewed-by tags for patch #1 and #2.
2. Incorporate comments from Jens for patch #3.
3. Switch to use generic trusted keys framework.

Sumit Garg (6):
tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

 Documentation/security/keys/index.rst       |   1 +
 Documentation/security/keys/tee-trusted.rst |  93 +++++++++
 MAINTAINERS                                 |   9 +
 drivers/tee/optee/call.c                    |   7 +
 drivers/tee/tee_core.c                      |   6 +
 drivers/tee/tee_shm.c                       |  16 +-
 include/keys/trusted-type.h                 |   3 +
 include/keys/trusted_tee.h                  |  66 +++++++
 include/linux/tee_drv.h                     |   1 +
 include/uapi/linux/tee.h                    |   8 +
 security/keys/Kconfig                       |   3 +
 security/keys/trusted-keys/Makefile         |   3 +-
 security/keys/trusted-keys/trusted-tee.c    | 282 ++++++++++++++++++++++++++++
 security/keys/trusted-keys/trusted.c        |   3 +
 14 files changed, 498 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/security/keys/tee-trusted.rst
 create mode 100644 include/keys/trusted_tee.h
 create mode 100644 security/keys/trusted-keys/trusted-tee.c

--
2.7.4

author
#19 lore raw
In reply to Janne Karhunen
Quoted 6 lines · Janne Karhunen
Quoted 3 lines · Sumit Garg
Quoted 3 lines

Interesting, I wrote something similar and posted it to the lists a while back:
https://github.com/jkrh/linux/commit/d77ea03afedcb5fd42234cd834da8f8a0809f6a6

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Since there are no generic 'TEEs' available,

There is already a generic TEE interface driver available in kernel. Have a look here: "Documentation/tee.txt".

I guess my wording was wrong, tried to say that physical TEEs in the wild vary massively hardware wise. Generalizing these things is rough.

There are already well defined GlobalPlatform Standards to generalize the TEE interface. One of them is GlobalPlatform TEE Client API [1] which provides the basis for this TEE interface.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

I implemented the same
thing as a generic protocol translator. The shared memory binding for instance already assumes fair amount about the TEE and how that is physically present in the system. Besides, the help from usage of shm is pretty limited due to the size of the keydata.

Quoted 8 lines
Quoted 3 lines

If you look at patch #1 and #2, they add support to register kernel memory buffer (keydata buffer in this case) with TEE to operate on. So there isn't any limitation due to the size of the keydata.

Ah, didn't mean that. Meant that the keydata is typically pretty small in size, so there is limited benefit from passing that in via shm if that complicates anything.

Ah, ok. Do you think of any better approach rather than to use SHM?

[1] https://globalplatform.org/specs-library/tee-client-api-specification/

-Sumit

Quoted 3 lines

--
Janne

#12 lore raw
In reply to Sumit Garg
Quoted 6 lines · Sumit Garg
Quoted 2 lines

To clarify a bit further - my thought was to support any type of trust source.

That could be very well accomplished via Trusted Keys abstraction framework [1]. A trust source just need to implement following APIs:

Quoted 8 lines

struct trusted_key_ops ts_trusted_key_ops = {
.migratable = 0, /* non-migratable */
.init = init_ts_trusted,
.seal = ts_key_seal,
.unseal = ts_key_unseal,
.get_random = ts_get_random,
.cleanup = cleanup_ts_trusted,
};

Which is basically the same as implementing a new keytype in the kernel; abstraction is not raised in any considerable manner this way?

I chose the userspace plugin due to this, you can use userspace aids to provide any type of service. Use the crypto library you desire to do the magic you want.

Quoted 7 lines
Quoted 3 lines

With the
user mode helper in between anyone can easily add their own thing in there.

Isn't actual purpose to have trusted keys is to protect user-space from access to kernel keys in plain format? Doesn't user mode helper defeat that purpose in one way or another?

Not really. CPU is in the user mode while running the code, but the code or the secure keydata being is not available to the 'normal' userspace. It's like microkernel service/driver this way. The usermode driver is part of the kernel image and it runs on top of a invisible rootfs.

--
Janne

#20 lore raw
In reply to Sumit Garg
Quoted 7 lines · Sumit Garg
Quoted 3 lines

I guess my wording was wrong, tried to say that physical TEEs in the wild vary massively hardware wise. Generalizing these things is rough.

There are already well defined GlobalPlatform Standards to generalize the TEE interface. One of them is GlobalPlatform TEE Client API [1] which provides the basis for this TEE interface.

I'm aware of it - I have implemented a large part of the GP TEE APIs earlier (primarily the crypto functions). Does the TEE you work with actually support GP properly? Can I take a look at the code?

Normally the TEE implementations are well-guarded secrets and the state of the implementation is quite random. In many cases keeping things secret is fine from my point of view, given that it is a RoT after all. The secrecy is the core business here. So, this is why I opted the userspace 'secret' route - no secrets in the kernel, but it's fine for the userspace. Umh was a logical fit to implement it.

--
Janne

#21 lore raw
Quoted 9 lines · Janne Karhunen
Quoted 6 lines · Sumit Garg
Quoted 5 lines

I guess my wording was wrong, tried to say that physical TEEs in the
wild vary massively hardware wise. Generalizing these things is rough.

Quoted 8 lines
Quoted 4 lines

There are already well defined GlobalPlatform Standards to generalize
the TEE interface. One of them is GlobalPlatform TEE Client API [1] which provides the basis for this TEE interface.

I'm aware of it - I have implemented a large part of the GP TEE APIs earlier (primarily the crypto functions). Does the TEE you work with actually support GP properly? Can I take a look at the code?

AFAIK Sumit is working with the OP-TEE implementation, which can be found on github: https://github.com/op-tee/optee_os

Regards,
Rouven

#22 lore raw
Quoted 6 lines · Rouven Czerwinski
Quoted 3 lines

I'm aware of it - I have implemented a large part of the GP TEE APIs earlier (primarily the crypto functions). Does the TEE you work with actually support GP properly? Can I take a look at the code?

AFAIK Sumit is working with the OP-TEE implementation, which can be found on github: https://github.com/op-tee/optee_os

Thanks, I will take a look. The fundamental problem with these things is that there are infinite amount of ways how TEEs and ROTs can be done in terms of the hardware and software. I really doubt there are 2 implementations in existence that are even remotely compatible in real life. As such, all things TEE/ROT would logically really belong in the userland and thanks to the bpfilter folks now the umh logic really makes that possible ... I think. The key implementation I did was just an RFC on the concept, what if we start to move the stuff that really belongs in the userspace to this pseudo-userland. It's not kernel, but it's not commonly accessible userland either. The shared memory would also work without any modifications between the umh based TEE/ROT driver and the userland if needed.

Anyway, just my .02c. I guess having any new support in the kernel for new trust sources is good and improvement from the current state. I can certainly make my stuff work with your setup as well, what ever people think is the best.

--
Janne

author
#13 lore raw
In reply to Janne Karhunen
Quoted 18 lines · Janne Karhunen
Quoted 6 lines · Sumit Garg
Quoted 2 lines

To clarify a bit further - my thought was to support any type of trust source.

That could be very well accomplished via Trusted Keys abstraction framework [1]. A trust source just need to implement following APIs:

Quoted 8 lines

struct trusted_key_ops ts_trusted_key_ops = {
.migratable = 0, /* non-migratable */
.init = init_ts_trusted,
.seal = ts_key_seal,
.unseal = ts_key_unseal,
.get_random = ts_get_random,
.cleanup = cleanup_ts_trusted,
};

Quoted 3 lines

Which is basically the same as implementing a new keytype in the kernel; abstraction is not raised in any considerable manner this way?

It doesn't create a new keytype. There is only single keytype:
"trusted" which could be implemented via one of the trust source available in the system like TPM, TEE etc.

Quoted 3 lines

I chose the userspace plugin due to this, you can use userspace aids to provide any type of service. Use the crypto library you desire to do the magic you want.

Here TEE isn't similar to a user-space crypto library. In our case TEE is based on ARM TrustZone which only allows TEE communications to be initiated from privileged mode. So why would you like to route communications via user-mode (which is less secure) when we have standardised TEE interface available in kernel?

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

With the
user mode helper in between anyone can easily add their own thing in there.

Isn't actual purpose to have trusted keys is to protect user-space from access to kernel keys in plain format? Doesn't user mode helper defeat that purpose in one way or another?

Quoted 6 lines

Not really. CPU is in the user mode while running the code, but the code or the secure keydata being is not available to the 'normal' userspace. It's like microkernel service/driver this way. The usermode driver is part of the kernel image and it runs on top of a invisible rootfs.

Can you elaborate here with an example regarding how this user-mode helper will securely communicate with a hardware based trust source with other user-space processes denied access to that trust source?

-Sumit

Quoted 3 lines

--
Janne

author
#23 lore raw
In reply to Janne Karhunen
Quoted 4 lines · Janne Karhunen

On Thu, Aug 1, 2019 at 9:50 AM Rouven Czerwinski
<[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I'm aware of it - I have implemented a large part of the GP TEE APIs earlier (primarily the crypto functions). Does the TEE you work with actually support GP properly? Can I take a look at the code?

AFAIK Sumit is working with the OP-TEE implementation, which can be found on github: https://github.com/op-tee/optee_os

Thanks, I will take a look.

For documentation, refer to: https://optee.readthedocs.io/

Quoted 5 lines

The fundamental problem with these things
is that there are infinite amount of ways how TEEs and ROTs can be done in terms of the hardware and software. I really doubt there are 2 implementations in existence that are even remotely compatible in real life.

I agree with you regarding implementation specific nature of TEE but having a standardized client interface does solves the problem.

Quoted 9 lines

As such, all things TEE/ROT would logically really belong in the userland and thanks to the bpfilter folks now the umh logic really makes that possible ... I think. The key implementation I did was just an RFC on the concept, what if we start to move the stuff that really belongs in the userspace to this pseudo-userland. It's not kernel, but it's not commonly accessible userland either. The shared memory would also work without any modifications between the umh based TEE/ROT driver and the userland if needed.

Quoted 4 lines

Anyway, just my .02c. I guess having any new support in the kernel for new trust sources is good and improvement from the current state. I can certainly make my stuff work with your setup as well, what ever people think is the best.

Yes your implementation can very well fit under trusted keys abstraction framework without creating a new keytype: "ext-trusted".

-Sumit

Quoted 4 lines

--
Janne

#14 lore raw
In reply to Sumit Garg
Quoted 9 lines · Sumit Garg
Quoted 3 lines

I chose the userspace plugin due to this, you can use userspace aids to provide any type of service. Use the crypto library you desire to do the magic you want.

Here TEE isn't similar to a user-space crypto library. In our case TEE is based on ARM TrustZone which only allows TEE communications to be initiated from privileged mode. So why would you like to route communications via user-mode (which is less secure) when we have standardised TEE interface available in kernel?

The physical access guards for reading/writing the involved critical memory are identical as far as I know? Layered security is generally a good thing, and the userspace pass actually adds a layer, so not sure which is really safer?

In my case the rerouting was to done generalize it. Any type of trust source, anywhere.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Isn't actual purpose to have trusted keys is to protect user-space from access to kernel keys in plain format? Doesn't user mode helper defeat that purpose in one way or another?

Not really. CPU is in the user mode while running the code, but the code or the secure keydata being is not available to the 'normal' userspace. It's like microkernel service/driver this way. The usermode driver is part of the kernel image and it runs on top of a invisible rootfs.

Quoted 3 lines

Can you elaborate here with an example regarding how this user-mode helper will securely communicate with a hardware based trust source with other user-space processes denied access to that trust source?

The other user mode processes will never see the device node to open. There is none in existence for them; it only exists in the ramfs based root for the user mode helper.

--
Janne

#24 lore raw
In reply to Sumit Garg
Quoted 7 lines · Sumit Garg
Quoted 4 lines

Anyway, just my .02c. I guess having any new support in the kernel for new trust sources is good and improvement from the current state. I can certainly make my stuff work with your setup as well, what ever people think is the best.

Yes your implementation can very well fit under trusted keys abstraction framework without creating a new keytype: "ext-trusted".

The fundamental problem with the 'standardized kernel tee' still exists - it will never be generic in real life. Getting all this in the kernel will solve your problem and sell this particular product, but it is quite unlikely to help that many users. If the security is truly important to you, would you really trust any of this code to someone else? In this day and age, I really doubt many do. Everyone does their own thing, so this is why I really see all that as a userspace problem.

--
Janne

author
#15 lore raw
In reply to Janne Karhunen
Quoted 13 lines · Janne Karhunen
Quoted 9 lines · Sumit Garg
Quoted 3 lines

I chose the userspace plugin due to this, you can use userspace aids to provide any type of service. Use the crypto library you desire to do the magic you want.

Here TEE isn't similar to a user-space crypto library. In our case TEE is based on ARM TrustZone which only allows TEE communications to be initiated from privileged mode. So why would you like to route communications via user-mode (which is less secure) when we have standardised TEE interface available in kernel?

Quoted 5 lines

The physical access guards for reading/writing the involved critical memory are identical as far as I know? Layered security is generally a good thing, and the userspace pass actually adds a layer, so not sure which is really safer?

AFAIK, layered security is better in case we move from lower privilege level to higher privilege level rather than in reverse order.

-Sumit

Quoted 25 lines

In my case the rerouting was to done generalize it. Any type of trust source, anywhere.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Isn't actual purpose to have trusted keys is to protect user-space from access to kernel keys in plain format? Doesn't user mode helper defeat that purpose in one way or another?

Not really. CPU is in the user mode while running the code, but the code or the secure keydata being is not available to the 'normal' userspace. It's like microkernel service/driver this way. The usermode driver is part of the kernel image and it runs on top of a invisible rootfs.

Quoted 3 lines

Can you elaborate here with an example regarding how this user-mode helper will securely communicate with a hardware based trust source with other user-space processes denied access to that trust source?

The other user mode processes will never see the device node to open. There is none in existence for them; it only exists in the ramfs based root for the user mode helper.

--
Janne

author
#25 lore raw
In reply to Janne Karhunen
Quoted 11 lines · Janne Karhunen
Quoted 7 lines · Sumit Garg
Quoted 4 lines

Anyway, just my .02c. I guess having any new support in the kernel for new trust sources is good and improvement from the current state. I can certainly make my stuff work with your setup as well, what ever people think is the best.

Yes your implementation can very well fit under trusted keys abstraction framework without creating a new keytype: "ext-trusted".

Quoted 6 lines

The fundamental problem with the 'standardized kernel tee' still exists - it will never be generic in real life. Getting all this in the kernel will solve your problem and sell this particular product, but it is quite unlikely to help that many users. If the security is truly important to you, would you really trust any of this code to someone else? In this day and age, I really doubt many do.

There are already multiple platforms supported by OP-TEE [1] which could benefit from this trusted keys interface.

Quoted 4 lines

Everyone
does their own thing, so this is why I really see all that as a userspace problem.

IMO, we should try to use standardized interfaces which are well thought off rather than implementing your own.

[1] https://optee.readthedocs.io/general/platforms.html

-Sumit

Quoted 3 lines

--
Janne

#16 lore raw
In reply to Sumit Garg
Quoted 6 lines · Sumit Garg
Quoted 6 lines
Quoted 5 lines

Here TEE isn't similar to a user-space crypto library. In our case TEE is based on ARM TrustZone which only allows TEE communications to be initiated from privileged mode. So why would you like to route communications via user-mode (which is less secure) when we have standardised TEE interface available in kernel?

Quoted 7 lines
Quoted 4 lines

The physical access guards for reading/writing the involved critical memory are identical as far as I know? Layered security is generally a good thing, and the userspace pass actually adds a layer, so not sure which is really safer?

AFAIK, layered security is better in case we move from lower privilege level to higher privilege level rather than in reverse order.

You can look at this in many ways. Another way to look at it is that the services should be provided with the least amount of permissions required for the task. Further you can containerize something, the better.

As for your PLATFORMS support: it is all nice, but there is no way to convince op-tee or any other tee to be adopted by many real users. Every serious user can and will do their own thing, or at very best, buy it from someone who did their own thing and is trusted. There is zero chance that samsung, huawei, apple, nsa, google, rambus, payment system vendors, .. would actually share the tee (or probably even the interfaces). It is just too vital and people do not trust each other anymore :(

Anyway, enough about the topic from my side. I guess people will tell what they want, I'm fine with any, and it is all progress from the current state :)

--
Janne

#26 lore raw
In reply to Sumit Garg
Quoted 6 lines · Sumit Garg

tee: optee: allow kernel pages to register as shm
tee: enable support to register kernel memory
tee: add private login method for kernel clients
KEYS: trusted: Introduce TEE based Trusted Keys
doc: keys: Document usage of TEE based Trusted Keys
MAINTAINERS: Add entry for TEE based Trusted Keys

Skimmed through the patches. I think it is better to sort out the current LKM dependency issue with trusted.ko and get TPM 1.2 and TPM 2.0 trusted keys code consolidated before it makes sense to really go detail on this.

/Jarkko

#3 lore raw

Re: [Tee-dev] [RFC v2 2/6] tee: enable support to register kernel memory

In reply to Sumit Garg
Quoted 5 lines · Sumit Garg
drivers/tee/tee_shm.c +0 -1
@@ -264,7 +266,17 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
  		goto err;
  	}
 
-	rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);

drivers/tee/tee_shm.c struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, · line 278

Quoted 10 lines
drivers/tee/tee_shm.c +10 -0
+	if (flags & TEE_SHM_USER_MAPPED) {
+		rc = get_user_pages_fast(start, num_pages, FOLL_WRITE,
+					 shm->pages);
+	} else {
+		const struct kvec kiov = {
+			.iov_base = (void *)start,
+			.iov_len = PAGE_SIZE
+		};
+
+		rc = get_kernel_pages(&kiov, num_pages, 0, shm->pages);

Passing a single kvec struct is temporary I assume? Because as currently written this will only work with num_pages=1.

Stuart

author
#4 lore raw

Re: [Tee-dev] [RFC v2 2/6] tee: enable support to register kernel memory

Quoted 9 lines · Stuart Yoder
Quoted 4 lines · Sumit Garg
drivers/tee/tee_shm.c +0 -0
@@ -264,7 +266,17 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
              goto err;
      }
 
Quoted 15 lines

drivers/tee/tee_shm.c struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, · line 278

Quoted 11 lines
drivers/tee/tee_shm.c +10 -1
-     rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
+     if (flags & TEE_SHM_USER_MAPPED) {
+             rc = get_user_pages_fast(start, num_pages, FOLL_WRITE,
+                                      shm->pages);
+     } else {
+             const struct kvec kiov = {
+                     .iov_base = (void *)start,
+                     .iov_len = PAGE_SIZE
+             };
+
+             rc = get_kernel_pages(&kiov, num_pages, 0, shm->pages);

Passing a single kvec struct is temporary I assume? Because as currently written this will only work with num_pages==1.

Ah, thanks Stuart for pointing this out. It should rather be an array of kvec struct. Will fix it in next version.

-Sumit

Quoted 1 line

Stuart