Skip to contentSkip to conversation

generic TEE subsystem

PATCH Superseded Jens Wiklander 5 participants 10 messages Feb 11, 2016 – Mar 2, 2016

Subsystems (4)

v8 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v8 cover generic TEE subsystem

Hi,

This patch set introduces a generic TEE subsystem. The TEE subsystem will contain drivers for various TEE implementations. A TEE (Trusted Execution Environment) is a trusted OS running in some secure environment, for example, TrustZone on ARM CPUs, or a separate secure co-processor etc.

Regarding use cases, TrustZone has traditionally been used for offloading secure tasks to the secure world. Examples include:
- Secure key handling where the OS may or may not have direct access to key material.
- E-commerce and payment technologies. Credentials, credit card numbers etc could be stored in a more secure environment.
- Trusted User Interface (TUI) to ensure that no-one can snoop PIN-codes etc.
- Secure boot to ensure that loaded binaries haven?t been tampered with. It?s not strictly needed for secure boot, but you could enhance security by leveraging a TEE during boot.
- Digital Rights Management (DRM), the studios provides content with different resolution depending on the security of the device. Higher security means higher resolution.

A TEE could also be used in existing and new technologies. For example IMA (Integrity Measurement Architecture) which has been in the kernel for quite a while. Today you can enhance security by using a TPM-chip to sign the IMA measurement list. This is something that you also could do by leveraging a TEE.

Another example could be in 2-factor authentication which is becoming increasingly more important. FIDO (https://fidoalliance.org) for example are using public key cryptography in their 2-factor authentication standard (U2F). With FIDO, a private and public key pair will be generated for every site you visit and the private key should never leave the local device. This is an example where you could use secure storage in a TEE for the private key.

Today you will find a quite a few different out of tree implementations of TEE drivers which tends to fragment the TEE ecosystem and development. We think it would be a good idea to have a generic TEE driver integrated in the kernel which would serve as a base for several different TEE solutions, no matter if they are on-chip like TrustZone or if they are on a separate crypto co-processor.

To develop this TEE subsystem we have been using the open source TEE called OP-TEE (https://github.com/OP-TEE/optee_os) and therefore this would be the first TEE solution supported by this new subsystem. OP-TEE is a GlobalPlatform compliant TEE, however this TEE subsystem is not limited to only GlobalPlatform TEEs, instead we have tried to design it so that it should work with other TEE solutions also.

"tee: generic TEE subsystem" brings in the generic TEE subsystem which helps when writing a driver for a specific TEE, for example, OP-TEE.

"tee: add OP-TEE driver" is an OP-TEE driver which uses the subsystem to do its work.

This patch set has been prepared in cooperation with Javier Gonz?lez who proposed "Generic TrustZone Driver in Linux Kernel" patches 28 Nov 2014,
https://lwn.net/Articles/623380/ . We've since then changed the scope to TEE instead of TrustZone.

We have discussed the design on tee-dev at lists.linaro.org (archive at
https://lists.linaro.org/pipermail/tee-dev/) with people from other companies, including Valentin Manea <[email protected]>, Emmanuel MICHEL <[email protected]>,
Jean-michel DELORME <[email protected]>,
and Joakim Bech <[email protected]>. Our main concern has been to agree on something that is generic enough to support many different TEEs while still keeping the interface together.

v8:
* Rebased on v4.5-rc3
* dt/bindings: add bindings for optee

* Fixes build error for X86
* Fixes spell error in "dt/bindings: add bindings for optee"

v7:
* Rebased on v4.5-rc2
* Moved the ARM SMC Calling Convention support into a separate patch set, which is now merged

v6:
* Rebased on v4.3-rc7
* Changed smccc interface to let the compiler marshal most of the parameters
* Added ARCH64 capability for smccc interface
* Changed the PSCI firmware calls (both arm and arm64) to use the new generic smccc interface instead instead of own assembly functions.
* Move optee DT bindings to below arm/firmware
* Defines method for OP-TEE driver to call secure world in DT, smc or hvc
* Exposes implementation id of a TEE driver in sysfs
to easily spawn corresponding tee-supplicant when device is ready
* Update OP-TEE Message Protocol to better cope with fragmented physical memory
* Read time directly from OP-TEE driver instead of forwarding the RPC request to tee-supplicant

v5:
* Replaced kref reference counting for the device with a size_t instead as the counter is always protected by a mutex

v4:
* Rebased on 4.1
* Redesigned the synchronization around entry exit of normal SMC
* Replaced rwsem on the driver instance with kref and completion since rwsem wasn't intended to be used in this way
* Expanded the TEE_IOCTL_PARAM_ATTR_TYPE_MASK to make room for future additional parameter types
* Documents TEE subsystem and OP-TEE driver
* Replaced TEE_IOC_CMD with TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE, TEE_IOC_CANCEL and TEE_IOC_CLOSE_SESSION
* DT bindings in a separate patch
* Assembly parts moved to arch/arm and arch/arm64 respectively, in a separate patch
* Redefined/clarified the meaning of OPTEE_SMC_SHM_CACHED
* Removed CMA usage to limit the scope of the patch set

v3:
* Rebased on 4.1-rc3 (dma_buf_export() API change)
* A couple of small sparse fixes
* Documents bindings for OP-TEE driver
* Updated MAINTAINERS

v2:
* Replaced the stubbed OP-TEE driver with a real OP-TEE driver
* Removed most APIs not needed by OP-TEE in current state
* Update Documentation/ioctl/ioctl-number.txt with correct path to tee.h
* Rename tee_shm_pool_alloc_cma() to tee_shm_pool_alloc()
* Moved tee.h into include/uapi/linux/
* Redefined tee.h IOCTL macros to be directly based on _IOR and friends
* Removed version info on the API to user space, a data blob which can contain an UUID is left for user space to be able to tell which protocol to use in TEE_IOC_CMD
* Changed user space exposed structures to only have types with __ prefix
* Dropped THIS_MODULE from tee_fops
* Reworked how the driver is registered and ref counted:
- moved from using an embedded struct miscdevice to an embedded struct device.
- uses an struct rw_semaphore as synchronization for driver detachment
- uses alloc/register pattern from TPM

Thanks,
Jens

Jens Wiklander (4):
dt/bindings: add bindings for optee
tee: generic TEE subsystem
tee: add OP-TEE driver
Documentation: tee subsystem and op-tee driver

 Documentation/00-INDEX                             |   2 +
 .../bindings/arm/firmware/linaro,optee-tz.txt      |  31 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 Documentation/ioctl/ioctl-number.txt               |   1 +
 Documentation/tee.txt                              | 117 +++
 MAINTAINERS                                        |  13 +
 drivers/Kconfig                                    |   2 +
 drivers/Makefile                                   |   1 +
 drivers/tee/Kconfig                                |  19 +
 drivers/tee/Makefile                               |   4 +
 drivers/tee/optee/Kconfig                          |   8 +
 drivers/tee/optee/Makefile                         |   5 +
 drivers/tee/optee/call.c                           | 400 ++++++++++
 drivers/tee/optee/core.c                           | 522 ++++++++++++
 drivers/tee/optee/optee_msg.h                      | 423 ++++++++++
 drivers/tee/optee/optee_private.h                  | 146 ++++
 drivers/tee/optee/optee_smc.h                      | 418 ++++++++++
 drivers/tee/optee/rpc.c                            | 322 ++++++++
 drivers/tee/optee/supp.c                           | 212 +++++
 drivers/tee/tee.c                                  | 873 +++++++++++++++++++++
 drivers/tee/tee_private.h                          |  80 ++
 drivers/tee/tee_shm.c                              | 324 ++++++++
 drivers/tee/tee_shm_pool.c                         | 133 ++++
 include/linux/tee_drv.h                            | 299 +++++++
 include/uapi/linux/tee.h                           | 383 +++++++++
 25 files changed, 4739 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.txt
 create mode 100644 Documentation/tee.txt
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v8 1/4 dt/bindings: add bindings for optee

Introduces linaro prefix and adds bindings for ARM TrustZone based OP-TEE implementation.

---
 .../bindings/arm/firmware/linaro,optee-tz.txt      | 31 ++++++++++++++++++++++
 .../devicetree/bindings/vendor-prefixes.txt        |  1 +
 2 files changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.txt
Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.txt new file +31 -0
new file mode 100644
index 0000000..d38834c
@@ -0,0 +1,31 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip.
+
+We're using "linaro" as the first part of the compatible property for
+the reference implementation maintained by Linaro.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible     : should contain "linaro,optee-tz"
+
+- method         : The method of calling the OP-TEE Trusted OS. Permitted
+                   values are:
+
+                   "smc" : SMC #0, with the register assignments specified
+		           in drivers/tee/optee/optee_smc.h
+
+                   "hvc" : HVC #0, with the register assignments specified
+		           in drivers/tee/optee/optee_smc.h
+
+
+
+Example:
+	firmware {
+		optee {
+			compatible = "linaro,optee-tz";
+			method = "smc";
+		};
+	};
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 72e2c5a..0c1e2f2 100644
@@ -130,6 +130,7 @@ lacie	LaCie
 lantiq	Lantiq Semiconductor
 lenovo	Lenovo Group Ltd.
 lg	LG Corporation
+linaro	Linaro Limited
 linux	Linux-specific binding
 lsi	LSI Corp. (LSI Logic)
 lltc	Linear Technology Corporation
Signature · 1 line
1.9.1
#2 lore raw

PATCH v8 2/4 tee: generic TEE subsystem

In reply to Jens Wiklander

Initial patch for generic TEE subsystem.
This subsystem provides:
* Registration/un-registration of TEE drivers.
* Shared memory between normal world and secure world.
* Ioctl interface for interaction with user space.
* Sysfs implementation_id of TEE driver

A TEE (Trusted Execution Environment) driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

This patch builds on other similar implementations trying to solve the same problem:
* "optee_linuxdriver" by among others
Jean-michel DELORME<[email protected]> and Emmanuel MICHEL <[email protected]>
* "Generic TrustZone Driver" by Javier Gonz?lez <[email protected]>

---
 Documentation/ioctl/ioctl-number.txt |   1 +
 MAINTAINERS                          |   7 +
 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/tee/Kconfig                  |   9 +
 drivers/tee/Makefile                 |   3 +
 drivers/tee/tee.c                    | 873 +++++++++++++++++++++++++++++++++++
 drivers/tee/tee_private.h            |  80 ++++
 drivers/tee/tee_shm.c                | 324 +++++++++++++
 drivers/tee/tee_shm_pool.c           | 133 ++++++
 include/linux/tee_drv.h              | 299 ++++++++++++
 include/uapi/linux/tee.h             | 383 +++++++++++++++
 12 files changed, 2115 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Documentation/ioctl/ioctl-number.txt +1 -0
index 91261a3..b5ce7b6 100644
@@ -307,6 +307,7 @@ Code  Seq#(hex)	Include File		Comments
 0xA3	80-8F	Port ACL		in development:
 					<mailto:[email protected]>
 0xA3	90-9F	linux/dtlk.h
+0xA4	00-1F	uapi/linux/tee.h	Generic TEE subsystem
 0xAA	00-3F	linux/uapi/linux/userfaultfd.h
 0xAB	00-1F	linux/nbd.h
 0xAC	00-1F	linux/raw.h
MAINTAINERS +7 -0
index 7f1fa4f..b2bae0e 100644
@@ -9561,6 +9561,13 @@ F:	drivers/hwtracing/stm/
 F:	include/linux/stm.h
 F:	include/uapi/linux/stm.h
 
+TEE SUBSYSTEM
+M:	Jens Wiklander <[email protected]>
+S:	Maintained
+F:	include/linux/tee_drv.h
+F:	include/uapi/linux/tee.h
+F:	drivers/tee/
+
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
 S:	Maintained
drivers/Kconfig +2 -0
index d2ac339..63baceb 100644
@@ -198,4 +198,6 @@ source "drivers/hwtracing/intel_th/Kconfig"
 
 source "drivers/fpga/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index 8f5d076..231b409 100644
@@ -172,3 +172,4 @@ obj-$(CONFIG_STM)		+= hwtracing/stm/
 obj-$(CONFIG_ANDROID)		+= android/
 obj-$(CONFIG_NVMEM)		+= nvmem/
 obj-$(CONFIG_FPGA)		+= fpga/
+obj-$(CONFIG_TEE)		+= tee/
drivers/tee/Kconfig new file +9 -0
new file mode 100644
index 0000000..f3ba154
@@ -0,0 +1,9 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+	bool "Trusted Execution Environment support"
+	default n
+	select DMA_SHARED_BUFFER
+	select GENERIC_ALLOCATOR
+	help
+	  This implements a generic interface towards a Trusted Execution
+	  Environment (TEE).
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 0000000..60d2dab
@@ -0,0 +1,3 @@
+obj-y += tee.o
+obj-y += tee_shm.o
+obj-y += tee_shm_pool.o
drivers/tee/tee.c new file +873 -0

873 lines not on this page · raw message

drivers/tee/tee_private.h new file +80 -0

80 lines not on this page · raw message

drivers/tee/tee_shm.c new file +324 -0

324 lines not on this page · raw message

drivers/tee/tee_shm_pool.c new file +133 -0

133 lines not on this page · raw message

include/linux/tee_drv.h new file +299 -0

299 lines not on this page · raw message

include/uapi/linux/tee.h new file +383 -0

383 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH v8 3/4 tee: add OP-TEE driver

In reply to Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2 to communicate with secure world

---
 MAINTAINERS                       |   5 +
 drivers/tee/Kconfig               |  10 +
 drivers/tee/Makefile              |   1 +
 drivers/tee/optee/Kconfig         |   8 +
 drivers/tee/optee/Makefile        |   5 +
 drivers/tee/optee/call.c          | 400 +++++++++++++++++++++++++++++
 drivers/tee/optee/core.c          | 522 ++++++++++++++++++++++++++++++++++++++
 drivers/tee/optee/optee_msg.h     | 423 ++++++++++++++++++++++++++++++
 drivers/tee/optee/optee_private.h | 146 +++++++++++
 drivers/tee/optee/optee_smc.h     | 418 ++++++++++++++++++++++++++++++
 drivers/tee/optee/rpc.c           | 322 +++++++++++++++++++++++
 drivers/tee/optee/supp.c          | 212 ++++++++++++++++
 12 files changed, 2472 insertions(+)
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/supp.c
MAINTAINERS +5 -0
index b2bae0e..f13414a 100644
@@ -8083,6 +8083,11 @@ F:	arch/*/oprofile/
 F:	drivers/oprofile/
 F:	include/linux/oprofile.h
 
+OP-TEE DRIVER
+M:	Jens Wiklander <[email protected]>
+S:	Maintained
+F:	drivers/tee/optee/
+
 ORACLE CLUSTER FILESYSTEM 2 (OCFS2)
 M:	Mark Fasheh <[email protected]>
 M:	Joel Becker <[email protected]>
drivers/tee/Kconfig +10 -0
index f3ba154..7228045 100644
@@ -7,3 +7,13 @@ config TEE
 	help
 	  This implements a generic interface towards a Trusted Execution
 	  Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index 60d2dab..53f3c76 100644
@@ -1,3 +1,4 @@
 obj-y += tee.o
 obj-y += tee_shm.o
 obj-y += tee_shm_pool.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +8 -0
new file mode 100644
index 0000000..a7a8b71
@@ -0,0 +1,8 @@
+# OP-TEE Trusted Execution Environment Configuration
+config OPTEE
+	tristate "OP-TEE"
+	default n
+	depends on HAVE_ARM_SMCCC
+	help
+	  This implements the OP-TEE Trusted Execution Environment (TEE)
+	  driver.
drivers/tee/optee/Makefile new file +5 -0
new file mode 100644
index 0000000..92fe578
@@ -0,0 +1,5 @@
+obj-$(CONFIG_OPTEE) += optee.o
+optee-objs += core.o
+optee-objs += call.o
+optee-objs += rpc.o
+optee-objs += supp.o
drivers/tee/optee/call.c new file +400 -0

400 lines not on this page · raw message

drivers/tee/optee/core.c new file +522 -0

522 lines not on this page · raw message

drivers/tee/optee/optee_msg.h new file +423 -0

423 lines not on this page · raw message

drivers/tee/optee/optee_private.h new file +146 -0

146 lines not on this page · raw message

drivers/tee/optee/optee_smc.h new file +418 -0

418 lines not on this page · raw message

drivers/tee/optee/rpc.c new file +322 -0

322 lines not on this page · raw message

drivers/tee/optee/supp.c new file +212 -0

212 lines not on this page · raw message

Signature · 1 line
1.9.1
#7 lore raw

PATCH v8 4/4 Documentation: tee subsystem and op-tee driver

In reply to Jens Wiklander
---
 Documentation/00-INDEX |   2 +
 Documentation/tee.txt  | 117 +++++++++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS            |   1 +
 3 files changed, 120 insertions(+)
 create mode 100644 Documentation/tee.txt
Documentation/00-INDEX +2 -0
index cd077ca..bd3f803 100644
@@ -435,6 +435,8 @@ sysrq.txt
 	- info on the magic SysRq key.
 target/
 	- directory with info on generating TCM v4 fabric .ko modules
+tee.txt
+	- info on the TEE subsystem and drivers
 this_cpu_ops.txt
 	- List rationale behind and the way to use this_cpu operations.
 thermal/
Documentation/tee.txt new file +117 -0

117 lines not on this page · raw message

MAINTAINERS +1 -0
index f13414a..29af59a 100644
@@ -9572,6 +9572,7 @@ S:	Maintained
 F:	include/linux/tee_drv.h
 F:	include/uapi/linux/tee.h
 F:	drivers/tee/
+F:	Documentation/tee.txt
 
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
Signature · 1 line
1.9.1
reviewer
#8 lore raw

Re: [PATCH v8 4/4] Documentation: tee subsystem and op-tee driver

Quoted 14 lines · Jens Wiklander
---
 Documentation/00-INDEX |   2 +
 Documentation/tee.txt  | 117 +++++++++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS            |   1 +
 3 files changed, 120 insertions(+)
 create mode 100644 Documentation/tee.txt
Documentation/tee.txt new file +0 -0
new file mode 100644
index 0000000..4ac91d2

Documentation/tee.txt line 2

Quoted 2 lines
Documentation/tee.txt new file +2 -0
@@ -0,0 +1,117 @@
+TEE subsystem
+This document describes the TEE subsystem in Linux

Linux.

Quoted 14 lines
Documentation/tee.txt new file +14 -0
+
+A TEE (Trusted Execution Environment) is a trusted OS running in some
+secure environment, for example, TrustZone on ARM CPUs, or a separate
+secure co-processor etc. A TEE driver handles the details needed to
+communicate with the TEE.
+
+This subsystem deals with:
+
+- Registration of TEE drivers
+
+- Managing shared memory between Linux and the TEE
+
+- Providing a generic API to the TEE
+

Documentation/tee.txt line 26

Quoted 10 lines
Documentation/tee.txt new file +10 -0
+The TEE interface
+=================
+
+include/uapi/linux/tee.h defines the generic interface to a TEE.
+
+User space (the client) connects to the driver by opening /dev/tee[0-9]* or
+/dev/teepriv[0-9]*.
+
+- TEE_IOC_SHM_ALLOC allocates shared memory and returns a file descriptor which
+  user space can mmap. When user space doesn't need the file descriptor anymore

any more,

Documentation/tee.txt line 33

Quoted 7 lines
Documentation/tee.txt new file +7 -0
+  it should be closed. When shared memory isn't needed any longer it should be
+  unmapped with munmap() to allow the reuse of memory.
+
+- TEE_IOC_VERSION lets user space know which TEE this driver handles and
+  the its capabilities.
+
+- TEE_IOC_OPEN_SESSION opens a new session to a Trusted Application

end with '.'

Documentation/tee.txt line 35

Quoted 2 lines
Documentation/tee.txt new file +2 -0
+
+- TEE_IOC_INVOKE invokes a function in a Trusted Application

ditto

Documentation/tee.txt line 37

Quoted 2 lines
Documentation/tee.txt new file +2 -0
+
+- TEE_IOC_CANCEL may cancel an ongoing TEE_IOC_OPEN_SESSION or TEE_IOC_INVOKE

ditto

Documentation/tee.txt line 39

Quoted 2 lines
Documentation/tee.txt new file +2 -0
+
+- TEE_IOC_CLOSE_SESSION closes a session to a Trusted Application

ditto

Quoted 78 lines
Documentation/tee.txt new file +78 -0
+
+There are two classes of clients, normal clients and supplicants. The latter is
+a helper process for the TEE to access resources in Linux, for example file
+system access. A normal client opens /dev/tee[0-9]* and a supplicant opens
+/dev/teepriv[0-9].
+
+Much of the communication between clients and the TEE is opaque to the
+driver. The main job for the driver is to receive requests from the
+clients, forward them to the TEE and send back the results. In the case of
+supplicants the communication goes in the other direction, the TEE sends
+requests to the supplicant which then sends back the result.
+
+OP-TEE driver
+=============
+
+The OP-TEE driver handles OP-TEE [1] based TEEs. Currently it is only the ARM
+TrustZone based OP-TEE solution that is supported.
+
+Lowest level of communication with OP-TEE builds on ARM SMC Calling
+Convention (SMCCC) [2], which is the foundation for OP-TEE's SMC interface
+[3] used internally by the driver. Stacked on top of that is OP-TEE Message
+Protocol [4].
+
+OP-TEE SMC interface provides the basic functions required by SMCCC and some
+additional functions specific for OP-TEE. The most interesting functions are:
+
+- OPTEE_SMC_FUNCID_CALLS_UID (part of SMCCC) returns the version information
+  which is then returned by TEE_IOC_VERSION
+
+- OPTEE_SMC_CALL_GET_OS_UUID returns the particular OP-TEE implementation, used
+  to tell, for instance, a TrustZone OP-TEE apart from an OP-TEE running on a
+  separate secure co-processor.
+
+- OPTEE_SMC_CALL_WITH_ARG drives the OP-TEE message protocol
+
+- OPTEE_SMC_GET_SHM_CONFIG lets the driver and OP-TEE agree on which memory
+  range to used for shared memory between Linux and OP-TEE.
+
+The GlobalPlatform TEE Client API [5] is implemented on top of the generic
+TEE API.
+
+Picture of the relationship between the different components in the
+OP-TEE architecture.
+
+    User space                  Kernel                   Secure world
+    ~~~~~~~~~~                  ~~~~~~                   ~~~~~~~~~~~~
+ +--------+                                             +-------------+
+ | Client |                                             | Trusted     |
+ +--------+                                             | Application |
+    /\                                                  +-------------+
+    || +----------+                                           /\
+    || |tee-      |                                           ||
+    || |supplicant|                                           \/
+    || +----------+                                     +-------------+
+    \/      /\                                          | TEE Internal|
+ +-------+  ||                                          | API         |
+ + TEE   |  ||            +--------+--------+           +-------------+
+ | Client|  ||            | TEE    | OP-TEE |           | OP-TEE      |
+ | API   |  \/            | subsys | driver |           | Trusted OS  |
+ +-------+----------------+----+-------+----+-----------+-------------+
+ |      Generic TEE API        |       |     OP-TEE MSG               |
+ |      IOCTL (TEE_IOC_*)      |       |     SMCCC (OPTEE_SMC_CALL_*) |
+ +-----------------------------+       +------------------------------+
+
+RPC (Remote Procedure Call) are requests from secure world to kernel driver
+or tee-supplicant. An RPC is identified by a special range of SMCCC return
+values from OPTEE_SMC_CALL_WITH_ARG. RPC messages which are intended for the
+kernel are handled by the kernel driver. Other RPC messages will be forwarded to
+tee-supplicant without further involvement of the driver, except switching
+shared memory buffer representation.
+
+References:
+[1] https://github.com/OP-TEE/optee_os
+[2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
+[3] drivers/tee/optee/optee_smc.h
+[4] drivers/tee/optee/optee_msg.h
+[5] http://www.globalplatform.org/specificationsdevice.asp look for
+    "TEE Client API Specification v1.0" and click download.
Signature · 1 line
~Randy
#3 lore raw

Re: [PATCH v8 2/4] tee: generic TEE subsystem

In reply to Jens Wiklander
Quoted 14 lines · Jens Wiklander

Initial patch for generic TEE subsystem.
This subsystem provides:
* Registration/un-registration of TEE drivers.
* Shared memory between normal world and secure world.
* Ioctl interface for interaction with user space.
* Sysfs implementation_id of TEE driver

A TEE (Trusted Execution Environment) driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

Quoted 8 lines

This patch builds on other similar implementations trying to solve the same problem:
* "optee_linuxdriver" by among others
Jean-michel DELORME<[email protected]> and Emmanuel MICHEL <[email protected]>
* "Generic TrustZone Driver" by Javier Gonz?lez <[email protected]>

I need you to get other people to review this before I can accept this series. Please do that, surely someone else cares about this code becides you?

thanks,

greg k-h

#4 lore raw

Re: [PATCH v8 2/4] tee: generic TEE subsystem

In reply to Jens Wiklander
Quoted 32 lines · Jens Wiklander
drivers/tee/tee.c new file +32 -0
+static int tee_ioctl_shm_alloc(struct tee_context *ctx,
+		struct tee_ioctl_shm_alloc_data __user *udata)
+{
+	long ret;
+	struct tee_ioctl_shm_alloc_data data;
+	struct tee_shm *shm;
+
+	if (copy_from_user(&data, udata, sizeof(data)))
+		return -EFAULT;
+
+	/* Currently no input flags are supported */
+	if (data.flags)
+		return -EINVAL;
+
+	data.fd = -1;
+
+	shm = tee_shm_alloc(ctx->teedev, data.size,
+			    TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
+	if (IS_ERR(shm))
+		return PTR_ERR(shm);
+
+	data.flags = shm->flags;
+	data.size = shm->size;
+	data.fd = tee_shm_get_fd(shm);
+	if (data.fd < 0) {
+		ret = data.fd;
+		goto err;
+	}
+
+	if (copy_to_user(udata, &data, sizeof(data))) {
+		ret = -EFAULT;
+		goto err;

drivers/tee/tee.c line 128

Quoted 10 lines
drivers/tee/tee.c new file +10 -0
+	}
+	/*
+	 * When user space closes the file descriptor the shared memory
+	 * should be freed
+	 */
+	tee_shm_put(shm);
+	return 0;
+err:
+	if (data.fd >= 0)
+		tee_shm_put_fd(data.fd);

This is completely broken. Don't ever use that pattern. Once something is in descriptor table, that's _it_. You are already past the point of no return and there is no way to clean up.

In ABIs like that (and struct containing descriptor *is* a bad ABI design) solution is
* allocate a descriptor
* do everything that might fail, including copy_to_user()/put_user(),
etc.
* if failed, release unused descriptor and do fput(), if you already
have a struct file reference that needs to be released.
* FINALLY, when nothing no failures are possible, fd_install() the
sucker in place.

And yes, dma_buf_fd() encourages that kind of braindamage. It's tolerable only in one case - when we are about to return descriptor number directly as return value of syscall and really can't fail anymore. Not the case here.

#5 lore raw

Re: [PATCH v8 2/4] tee: generic TEE subsystem

Quoted 59 lines · Al Viro
Quoted 32 lines · Jens Wiklander
drivers/tee/tee.c new file +32 -0
+static int tee_ioctl_shm_alloc(struct tee_context *ctx,
+		struct tee_ioctl_shm_alloc_data __user *udata)
+{
+	long ret;
+	struct tee_ioctl_shm_alloc_data data;
+	struct tee_shm *shm;
+
+	if (copy_from_user(&data, udata, sizeof(data)))
+		return -EFAULT;
+
+	/* Currently no input flags are supported */
+	if (data.flags)
+		return -EINVAL;
+
+	data.fd = -1;
+
+	shm = tee_shm_alloc(ctx->teedev, data.size,
+			    TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
+	if (IS_ERR(shm))
+		return PTR_ERR(shm);
+
+	data.flags = shm->flags;
+	data.size = shm->size;
+	data.fd = tee_shm_get_fd(shm);
+	if (data.fd < 0) {
+		ret = data.fd;
+		goto err;
+	}
+
+	if (copy_to_user(udata, &data, sizeof(data))) {
+		ret = -EFAULT;
+		goto err;

drivers/tee/tee.c line 128

Quoted 10 lines
drivers/tee/tee.c new file +10 -0
+	}
+	/*
+	 * When user space closes the file descriptor the shared memory
+	 * should be freed
+	 */
+	tee_shm_put(shm);
+	return 0;
+err:
+	if (data.fd >= 0)
+		tee_shm_put_fd(data.fd);

This is completely broken. Don't ever use that pattern. Once something is in descriptor table, that's _it_. You are already past the point of no return and there is no way to clean up.

In ABIs like that (and struct containing descriptor *is* a bad ABI design) solution is
* allocate a descriptor
* do everything that might fail, including copy_to_user()/put_user(),
etc.
* if failed, release unused descriptor and do fput(), if you already
have a struct file reference that needs to be released.
* FINALLY, when nothing no failures are possible, fd_install() the
sucker in place.

Quoted 4 lines

And yes, dma_buf_fd() encourages that kind of braindamage. It's tolerable only in one case - when we are about to return descriptor number directly as return value of syscall and really can't fail anymore. Not the case here.

Thanks for the feedback, I'll change to return the descriptor in the return value instead.

--
Jens

Acked-by on the v8 series
#9 lore raw
In reply to Jens Wiklander
Quoted 70 lines · Jens Wiklander

Hi,

This patch set introduces a generic TEE subsystem. The TEE subsystem will contain drivers for various TEE implementations. A TEE (Trusted Execution Environment) is a trusted OS running in some secure environment, for example, TrustZone on ARM CPUs, or a separate secure co-processor etc.

Regarding use cases, TrustZone has traditionally been used for offloading secure tasks to the secure world. Examples include:
- Secure key handling where the OS may or may not have direct access to key material.
- E-commerce and payment technologies. Credentials, credit card numbers etc could be stored in a more secure environment.
- Trusted User Interface (TUI) to ensure that no-one can snoop PIN-codes etc.
- Secure boot to ensure that loaded binaries haven?t been tampered with. It?s not strictly needed for secure boot, but you could enhance security by leveraging a TEE during boot.
- Digital Rights Management (DRM), the studios provides content with different resolution depending on the security of the device. Higher security means higher resolution.

A TEE could also be used in existing and new technologies. For example IMA (Integrity Measurement Architecture) which has been in the kernel for quite a while. Today you can enhance security by using a TPM-chip to sign the IMA measurement list. This is something that you also could do by leveraging a TEE.

Another example could be in 2-factor authentication which is becoming increasingly more important. FIDO (https://fidoalliance.org) for example are using public key cryptography in their 2-factor authentication standard (U2F). With FIDO, a private and public key pair will be generated for every site you visit and the private key should never leave the local device. This is an example where you could use secure storage in a TEE for the private key.

Today you will find a quite a few different out of tree implementations of TEE drivers which tends to fragment the TEE ecosystem and development. We think it would be a good idea to have a generic TEE driver integrated in the kernel which would serve as a base for several different TEE solutions, no matter if they are on-chip like TrustZone or if they are on a separate crypto co-processor.

To develop this TEE subsystem we have been using the open source TEE called OP-TEE (https://github.com/OP-TEE/optee_os) and therefore this would be the first TEE solution supported by this new subsystem. OP-TEE is a GlobalPlatform compliant TEE, however this TEE subsystem is not limited to only GlobalPlatform TEEs, instead we have tried to design it so that it should work with other TEE solutions also.

"tee: generic TEE subsystem" brings in the generic TEE subsystem which helps when writing a driver for a specific TEE, for example, OP-TEE.

"tee: add OP-TEE driver" is an OP-TEE driver which uses the subsystem to do its work.

This patch set has been prepared in cooperation with Javier Gonz?lez who proposed "Generic TrustZone Driver in Linux Kernel" patches 28 Nov 2014,
https://lwn.net/Articles/623380/ . We've since then changed the scope to TEE instead of TrustZone.

We have discussed the design on tee-dev at lists.linaro.org (archive at
https://lists.linaro.org/pipermail/tee-dev/) with people from other companies, including Valentin Manea <[email protected]>, Emmanuel MICHEL <[email protected]>,
Jean-michel DELORME <[email protected]>,
and Joakim Bech <[email protected]>. Our main concern has been to agree on something that is generic enough to support many different TEEs while still keeping the interface together.

Quoted 6 lines

v8:
* Rebased on v4.5-rc3
* dt/bindings: add bindings for optee

* Fixes build error for X86
* Fixes spell error in "dt/bindings: add bindings for optee"

Jens,
thanks for the updated series and the continued work on this. I've recently starting testing/experimenting with this in the context of Kernel 4.4 (will try latest Kernel as a next step) and have not found any issues so far. This is an ongoing effort and I'll provide feedback as I encounter anything notable.

But I also wanted to use this opportunity to express that from a TI and TI customer point of view (and probably beyond that) this TEE subsystem support is seen as an important building block for a number real-world use cases involving security.

Regards,

--
Andreas Dannenberg
Texas Instruments Inc

Quoted 126 lines

v7:
* Rebased on v4.5-rc2
* Moved the ARM SMC Calling Convention support into a separate patch set, which is now merged

v6:
* Rebased on v4.3-rc7
* Changed smccc interface to let the compiler marshal most of the parameters
* Added ARCH64 capability for smccc interface
* Changed the PSCI firmware calls (both arm and arm64) to use the new generic smccc interface instead instead of own assembly functions.
* Move optee DT bindings to below arm/firmware
* Defines method for OP-TEE driver to call secure world in DT, smc or hvc
* Exposes implementation id of a TEE driver in sysfs
to easily spawn corresponding tee-supplicant when device is ready
* Update OP-TEE Message Protocol to better cope with fragmented physical memory
* Read time directly from OP-TEE driver instead of forwarding the RPC request to tee-supplicant

v5:
* Replaced kref reference counting for the device with a size_t instead as the counter is always protected by a mutex

v4:
* Rebased on 4.1
* Redesigned the synchronization around entry exit of normal SMC
* Replaced rwsem on the driver instance with kref and completion since rwsem wasn't intended to be used in this way
* Expanded the TEE_IOCTL_PARAM_ATTR_TYPE_MASK to make room for future additional parameter types
* Documents TEE subsystem and OP-TEE driver
* Replaced TEE_IOC_CMD with TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE, TEE_IOC_CANCEL and TEE_IOC_CLOSE_SESSION
* DT bindings in a separate patch
* Assembly parts moved to arch/arm and arch/arm64 respectively, in a separate patch
* Redefined/clarified the meaning of OPTEE_SMC_SHM_CACHED
* Removed CMA usage to limit the scope of the patch set

v3:
* Rebased on 4.1-rc3 (dma_buf_export() API change)
* A couple of small sparse fixes
* Documents bindings for OP-TEE driver
* Updated MAINTAINERS

v2:
* Replaced the stubbed OP-TEE driver with a real OP-TEE driver
* Removed most APIs not needed by OP-TEE in current state
* Update Documentation/ioctl/ioctl-number.txt with correct path to tee.h
* Rename tee_shm_pool_alloc_cma() to tee_shm_pool_alloc()
* Moved tee.h into include/uapi/linux/
* Redefined tee.h IOCTL macros to be directly based on _IOR and friends
* Removed version info on the API to user space, a data blob which can contain an UUID is left for user space to be able to tell which protocol to use in TEE_IOC_CMD
* Changed user space exposed structures to only have types with __ prefix
* Dropped THIS_MODULE from tee_fops
* Reworked how the driver is registered and ref counted:
- moved from using an embedded struct miscdevice to an embedded struct device.
- uses an struct rw_semaphore as synchronization for driver detachment
- uses alloc/register pattern from TPM

Thanks,
Jens

Jens Wiklander (4):
dt/bindings: add bindings for optee
tee: generic TEE subsystem
tee: add OP-TEE driver
Documentation: tee subsystem and op-tee driver

 Documentation/00-INDEX                             |   2 +
 .../bindings/arm/firmware/linaro,optee-tz.txt      |  31 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 Documentation/ioctl/ioctl-number.txt               |   1 +
 Documentation/tee.txt                              | 117 +++
 MAINTAINERS                                        |  13 +
 drivers/Kconfig                                    |   2 +
 drivers/Makefile                                   |   1 +
 drivers/tee/Kconfig                                |  19 +
 drivers/tee/Makefile                               |   4 +
 drivers/tee/optee/Kconfig                          |   8 +
 drivers/tee/optee/Makefile                         |   5 +
 drivers/tee/optee/call.c                           | 400 ++++++++++
 drivers/tee/optee/core.c                           | 522 ++++++++++++
 drivers/tee/optee/optee_msg.h                      | 423 ++++++++++
 drivers/tee/optee/optee_private.h                  | 146 ++++
 drivers/tee/optee/optee_smc.h                      | 418 ++++++++++
 drivers/tee/optee/rpc.c                            | 322 ++++++++
 drivers/tee/optee/supp.c                           | 212 +++++
 drivers/tee/tee.c                                  | 873 +++++++++++++++++++++
 drivers/tee/tee_private.h                          |  80 ++
 drivers/tee/tee_shm.c                              | 324 ++++++++
 drivers/tee/tee_shm_pool.c                         | 133 ++++
 include/linux/tee_drv.h                            | 299 +++++++
 include/uapi/linux/tee.h                           | 383 +++++++++
 25 files changed, 4739 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.txt
 create mode 100644 Documentation/tee.txt
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Signature · 1 line
1.9.1