tee: add MbedTEE driver
Subsystems (4)
v3 cover tee: add MbedTEE driver
This series adds a Linux TEE driver for MbedTEE, a Trusted Execution Environment for embedded systems
(https://github.com/mbedtee).
Two RPC transports are provided for systems where platform firmware or board configuration has already established the REE/TEE separation before Linux boots:
- ARM/ARM64: SMC calls and GIC SPI notifications (TrustZone)
- RISC-V: shared-memory ring buffers and IMSIC MSI notifications
The driver implements the TEE subsystem interface (tee_driver_ops) and provides GlobalPlatform TEE Client API support, dynamic shared memory registration, and tee-supplicant support for REE filesystem and RPMB operations.
The series is structured as follows:
[1/3] dt-bindings: vendor-prefixes: add mbedtee [2/3] dt-bindings: firmware: add mbedtee,tee binding [3/3] tee: add MbedTEE driver
---
Changes in v3:
- 0002: Drop all phandle stub nodes from examples.
- 0002: Remove redundant required: - interrupts from then branch.
- 0002: Simplify title and description.
- Link to v2: https://lore.kernel.org/r/[email protected]/
- Link to v1: https://lore.kernel.org/r/[email protected]/
Changes in v2:
- 0002: Fix DT binding review comments from Krzysztof Kozlowski:
- Drop $nodename, "YAML devicetree binding" wording, property descriptions
- Rename compatible string to mbedtee,tee
- Rename memory regions: rpc-t2r-ring -> t2r-ring, rpc-t2r-shm -> t2r-shm,
rpc-r2t-ring -> r2t-ring
- Add memory-region / memory-region-names to required
- Simplify allOf constraints (drop redundant else-branch items)
- Rewrite description to describe hardware/firmware, not the binding or driver
- Drop all irrelevant platform nodes (gic, cpus, reserved-memory)
- Add maxItems: 1 constraint to interrupts property (Sashiko AI review)
- 0003:
- Fix supp_release incorrectly aborting unclaimed requests on close
- Fix potential tee_shm double-free on supp_recv error path
- Fix async RPC ring skip leaving orphaned payload bytes
- Fix COMPLETE_TEE retry to also handle transient -ENOMEM on RISC-V
- Fix session leak on close_session allocation failure:
release kernel resources before sending RPC
---
Xing Loong (3):
dt-bindings: vendor-prefixes: add mbedtee
dt-bindings: firmware: add mbedtee,tee binding
tee: add MbedTEE driver
.../bindings/firmware/mbedtee,tee.yaml | 109 +++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + Documentation/tee/index.rst | 1 + Documentation/tee/mbedtee.rst | 155 +++++ MAINTAINERS | 9 + drivers/tee/Kconfig | 3 +- drivers/tee/Makefile | 1 + drivers/tee/mbedtee/Kconfig | 20 + drivers/tee/mbedtee/Makefile | 11 + drivers/tee/mbedtee/core.c | 236 +++++++ drivers/tee/mbedtee/mbedtee_drv.h | 269 ++++++++ drivers/tee/mbedtee/mbedtee_msg.h | 219 +++++++ drivers/tee/mbedtee/rpc_callee.c | 618 ++++++++++++++++++ drivers/tee/mbedtee/rpc_callee_arm.c | 91 +++ drivers/tee/mbedtee/rpc_callee_riscv.c | 203 ++++++ drivers/tee/mbedtee/rpc_caller.c | 532 +++++++++++++++ drivers/tee/mbedtee/rpc_caller_arm.c | 66 ++ drivers/tee/mbedtee/rpc_caller_riscv.c | 192 ++++++ drivers/tee/mbedtee/shm_pool.c | 105 +++ drivers/tee/mbedtee/shm_pool.h | 15 + drivers/tee/mbedtee/supp.c | 310 +++++++++ include/uapi/linux/tee.h | 1 + 22 files changed, 3167 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml create mode 100644 Documentation/tee/mbedtee.rst create mode 100644 drivers/tee/mbedtee/Kconfig create mode 100644 drivers/tee/mbedtee/Makefile create mode 100644 drivers/tee/mbedtee/core.c create mode 100644 drivers/tee/mbedtee/mbedtee_drv.h create mode 100644 drivers/tee/mbedtee/mbedtee_msg.h create mode 100644 drivers/tee/mbedtee/rpc_callee.c create mode 100644 drivers/tee/mbedtee/rpc_callee_arm.c create mode 100644 drivers/tee/mbedtee/rpc_callee_riscv.c create mode 100644 drivers/tee/mbedtee/rpc_caller.c create mode 100644 drivers/tee/mbedtee/rpc_caller_arm.c create mode 100644 drivers/tee/mbedtee/rpc_caller_riscv.c create mode 100644 drivers/tee/mbedtee/shm_pool.c create mode 100644 drivers/tee/mbedtee/shm_pool.h create mode 100644 drivers/tee/mbedtee/supp.c
base-commit: 03e2778d1f11de9260543f969e9e888a1c2bf830
Signature · 1 line
2.43.0
PATCH v3 1/3 dt-bindings: vendor-prefixes: add mbedtee
Register the "mbedtee" vendor prefix for the MbedTEE Trusted Execution Environment project (https://github.com/mbedtee).
--- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -1011,6 +1011,8 @@ patternProperties: description: Maxtor Corporation "^mayqueen,.*": description: Mayqueen Technologies Ltd. + "^mbedtee,.*": + description: MbedTEE Trusted Execution Environment project "^mbvl,.*": description: Mobiveil Inc. "^mcube,.*":
Signature · 1 line
2.43.0
Re: [PATCH v3 1/3] dt-bindings: vendor-prefixes: add mbedtee
Quoted 8 lines · Xing Loong
Register the "mbedtee" vendor prefix for the MbedTEE Trusted Execution Environment project (https://github.com/mbedtee).
Signed-off-by Xing Loong <[email protected]>--- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
PATCH v3 2/3 dt-bindings: firmware: add mbedtee,tee binding
In reply to Xing LoongMbedTEE is a Trusted Execution Environment for embedded systems (https://github.com/mbedtee). It communicates with the REE via shared-memory ring buffers using a fixed RPC protocol. Two platform configurations are supported:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (t2r-ring and t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).
---
Changes in v3:
- Drop all phandle stub nodes from examples.
- Remove redundant required: - interrupts from then branch.
- Simplify title and description.
Changes in v2:
- Fix DT binding review comments from Krzysztof Kozlowski:
- Drop $nodename, "YAML devicetree binding" wording, property descriptions
- Rename compatible string to mbedtee,tee
- Rename memory regions: rpc-t2r-ring -> t2r-ring, rpc-t2r-shm -> t2r-shm,
rpc-r2t-ring -> r2t-ring
- Add memory-region / memory-region-names to required
- Simplify allOf constraints (drop redundant else-branch items)
- Rewrite description to describe hardware/firmware, not the binding or driver
- Drop all irrelevant platform nodes (gic, cpus, reserved-memory)
- Add maxItems: 1 constraint to interrupts property (Sashiko AI review)
---
.../bindings/firmware/mbedtee,tee.yaml | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +109 -0
@@ -0,0 +1,109 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/firmware/mbedtee,tee.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MbedTEE + +maintainers: + - Xing Loong <[email protected]> + +description: | + MbedTEE is a Trusted Execution Environment for embedded systems. + It communicates with the REE (Linux) via shared-memory ring + buffers using a fixed RPC protocol. + + We're using "mbedtee" as the vendor prefix for the open-source TEE + project at https://github.com/mbedtee. + + The REE and TEE CPUs sharing the RPC memory must be in a + hardware-coherent domain. + + Two or three reserved-memory regions are required: + t2r-ring: ring buffer for TEE-to-REE notifications + t2r-shm: shared memory for TEE-to-REE RPC payloads + r2t-ring: ring buffer for REE-to-TEE command submissions (RISC-V) + + On ARM the transport uses SMC and a GIC SPI interrupt. On RISC-V + the transport uses shared-memory rings and IMSIC MSI; the TEE + polls r2t-ring for commands from the REE. + +properties: + compatible: + const: mbedtee,tee + + interrupts: + maxItems: 1 + + msi-parent: + maxItems: 1 + + memory-region: + minItems: 2 + maxItems: 3 + + memory-region-names: + minItems: 2 + maxItems: 3 + items: + enum: + - t2r-ring + - t2r-shm + - r2t-ring + +required: + - compatible + - memory-region + - memory-region-names + +allOf: + - if: + required: + - interrupts + then: + properties: + msi-parent: false + memory-region: + maxItems: 2 + memory-region-names: + items: + - const: t2r-ring + - const: t2r-shm + else: + required: + - msi-parent + properties: + interrupts: false + memory-region: + minItems: 3 + memory-region-names: + items: + - const: t2r-ring + - const: t2r-shm + - const: r2t-ring + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + + firmware { + mbedtee { + compatible = "mbedtee,tee"; + interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>; + memory-region = <&t2r_ring>, <&t2r_shm>; + memory-region-names = "t2r-ring", "t2r-shm"; + }; + }; + + - | + firmware { + mbedtee { + compatible = "mbedtee,tee"; + msi-parent = <&imsic>; + memory-region = <&t2r_ring>, <&t2r_shm>, <&r2t_ring>; + memory-region-names = "t2r-ring", "t2r-shm", "r2t-ring"; + }; + };
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 2/3] dt-bindings: firmware: add mbedtee,tee binding
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] The YAML examples will fail `make dt_binding_check` with `dtc` compilation errors due to unresolved…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The YAML examples will fail make dt_binding_check with dtc compilation errors due to unresolved phandle references.
- [Low] Top-level items for memory-region-names is defined as an object enum rather than a list, leading to unnecessary schema complexity and bypassing strict sequence expectations. --
--- Patch [2]: [PATCH v3 2/3] dt-bindings: firmware: add mbedtee,tee binding --- commit 2b2356c421476302d4bae45809eeb06223db44ab
Author: Xing Loong <[email protected]>
dt-bindings: firmware: add mbedtee,tee binding
This commit adds a new device tree binding for MbedTEE, a Trusted Execution Environment. It supports communication via shared memory ring buffers on ARM and RISC-V platforms.
Quoted 4 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 53
Quoted 8 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +8
-0+ memory-region-names: + minItems: 2 + maxItems: 3 + items: + enum: + - t2r-ring + - t2r-shm + - r2t-ring
[Severity: Low]
Does this top-level enum definition bypass the strict sequence expectations for *-names properties?
Typically, items should be an ordered list of elements. Defining it as an enum here allows the names to appear in any order, which is later overridden in the allOf branches. Could this be simplified by defining the exact list of const elements at the top level?
[ ... ]
Quoted 12 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +12
-0+examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + + firmware { + mbedtee { + compatible = "mbedtee,tee"; + interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>; + memory-region = <&t2r_ring>, <&t2r_shm>; + memory-region-names = "t2r-ring", "t2r-shm"; + }; + };
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 109
Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10
-0+ + - | + firmware { + mbedtee { + compatible = "mbedtee,tee"; + msi-parent = <&imsic>; + memory-region = <&t2r_ring>, <&t2r_shm>, <&r2t_ring>; + memory-region-names = "t2r-ring", "t2r-shm", "r2t-ring"; + }; + };
[Severity: Low]
Will these examples fail make dt_binding_check with dtc compilation errors?
The phandles like <&t2r_ring>, <&t2r_shm>, <&r2t_ring>, and <&imsic> are used here but are not defined as stub nodes anywhere in the example blocks. The device tree compiler generally cannot resolve these phandles without the stub definitions.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Rob Herring
maintainerRe: [PATCH v3 2/3] dt-bindings: firmware: add mbedtee,tee binding
In reply to Xing LoongQuoted 60 lines · Xing Loong
MbedTEE is a Trusted Execution Environment for embedded systems (https://github.com/mbedtee). It communicates with the REE via shared-memory ring buffers using a fixed RPC protocol. Two platform configurations are supported:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (t2r-ring and t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).Signed-off-by Xing Loong <[email protected]>---
Changes in v3:
- Drop all phandle stub nodes from examples.
- Remove redundant required: - interrupts from then branch.
- Simplify title and description.Changes in v2:
- Fix DT binding review comments from Krzysztof Kozlowski:
- Drop $nodename, "YAML devicetree binding" wording, property descriptions
- Rename compatible string to mbedtee,tee
- Rename memory regions: rpc-t2r-ring -> t2r-ring, rpc-t2r-shm -> t2r-shm,rpc-r2t-ring -> r2t-ring- Add memory-region / memory-region-names to required
- Simplify allOf constraints (drop redundant else-branch items)
- Rewrite description to describe hardware/firmware, not the binding or driver
- Drop all irrelevant platform nodes (gic, cpus, reserved-memory)
- Add maxItems: 1 constraint to interrupts property (Sashiko AI review)---
.../bindings/firmware/mbedtee,tee.yaml | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,tee.yamlDocumentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +16
-0@@ -0,0 +1,109 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/firmware/mbedtee,tee.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MbedTEE + +maintainers: + - Xing Loong <[email protected]> + +description: | + MbedTEE is a Trusted Execution Environment for embedded systems. + It communicates with the REE (Linux) via shared-memory ring + buffers using a fixed RPC protocol. +
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 26
Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10
-0+ We're using "mbedtee" as the vendor prefix for the open-source TEE + project at https://github.com/mbedtee. + + The REE and TEE CPUs sharing the RPC memory must be in a + hardware-coherent domain. + + Two or three reserved-memory regions are required: + t2r-ring: ring buffer for TEE-to-REE notifications + t2r-shm: shared memory for TEE-to-REE RPC payloads + r2t-ring: ring buffer for REE-to-TEE command submissions (RISC-V)
Put any details about specific properties with the property schema.
Quoted 17 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +17
-0+ + On ARM the transport uses SMC and a GIC SPI interrupt. On RISC-V + the transport uses shared-memory rings and IMSIC MSI; the TEE + polls r2t-ring for commands from the REE. + +properties: + compatible: + const: mbedtee,tee + + interrupts: + maxItems: 1 + + msi-parent: + maxItems: 1 + + memory-region: + minItems: 2
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 53
Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10
-0+ maxItems: 3 + + memory-region-names: + minItems: 2 + maxItems: 3 + items: + enum: + - t2r-ring + - t2r-shm + - r2t-ring
Needs to define the order as sashiko points out. The order doesn't change, so it doesn't need to be defined in the if/then schemas.
Quoted 9 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +9
-0+ +required: + - compatible + - memory-region + - memory-region-names + +allOf: + - if: + required:
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 72
Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10
-0+ - interrupts + then: + properties: + msi-parent: false + memory-region: + maxItems: 2 + memory-region-names: + items: + - const: t2r-ring + - const: t2r-shm
Just 'maxItems: 2'
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 84
Quoted 12 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +12
-0+ else: + required: + - msi-parent + properties: + interrupts: false + memory-region: + minItems: 3 + memory-region-names: + items: + - const: t2r-ring + - const: t2r-shm + - const: r2t-ring
Just 'minItems: 3'
Quoted 28 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +25
-0+ +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + + firmware { + mbedtee { + compatible = "mbedtee,tee"; + interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>; + memory-region = <&t2r_ring>, <&t2r_shm>; + memory-region-names = "t2r-ring", "t2r-shm"; + }; + }; + + - | + firmware { + mbedtee { + compatible = "mbedtee,tee"; + msi-parent = <&imsic>; + memory-region = <&t2r_ring>, <&t2r_shm>, <&r2t_ring>; + memory-region-names = "t2r-ring", "t2r-shm", "r2t-ring"; + }; + };Signature · 1 line
2.43.0
PATCH v3 3/3 tee: add MbedTEE driver
In reply to Xing LoongThis patch adds a Linux TEE driver that communicates with MbedTEE OS, a Trusted Execution Environment for embedded systems (https://github.com/mbedtee) via a fixed shared-memory RPC protocol over architecture-specific transports:
- ARM/ARM64: SMCCC fast/yield calls for REE-to-TEE requests; TEE-to-REE notifications via a GIC SPI edge interrupt.
- RISC-V: REE submits commands by writing to a shared-memory r2t-ring that the TEE polls; the TEE notifies the REE by writing to t2r-ring and raising a platform IMSIC MSI. No REE-to-TEE interrupt is used.
The driver implements the TEE subsystem interface (tee_driver_ops) and provides GlobalPlatform TEE Client API support, dynamic shared memory registration, and tee-supplicant support.
---
Changes in v3:
- None (driver changes only in v2)
Changes in v2:
- Fix supp_release incorrectly aborting unclaimed requests on close
- Fix potential tee_shm double-free on supp_recv error path
- Fix async RPC ring skip leaving orphaned payload bytes
- Fix COMPLETE_TEE retry to also handle transient -ENOMEM on RISC-V
- Fix session leak on close_session allocation failure:
release kernel resources before sending RPC
---
Documentation/tee/index.rst | 1 + Documentation/tee/mbedtee.rst | 155 +++++++ MAINTAINERS | 9 + drivers/tee/Kconfig | 3 +- drivers/tee/Makefile | 1 + drivers/tee/mbedtee/Kconfig | 20 + drivers/tee/mbedtee/Makefile | 11 + drivers/tee/mbedtee/core.c | 236 ++++++++++ drivers/tee/mbedtee/mbedtee_drv.h | 269 +++++++++++ drivers/tee/mbedtee/mbedtee_msg.h | 219 +++++++++ drivers/tee/mbedtee/rpc_callee.c | 618 +++++++++++++++++++++++++ drivers/tee/mbedtee/rpc_callee_arm.c | 91 ++++ drivers/tee/mbedtee/rpc_callee_riscv.c | 203 ++++++++ drivers/tee/mbedtee/rpc_caller.c | 532 +++++++++++++++++++++ drivers/tee/mbedtee/rpc_caller_arm.c | 66 +++ drivers/tee/mbedtee/rpc_caller_riscv.c | 192 ++++++++ drivers/tee/mbedtee/shm_pool.c | 105 +++++ drivers/tee/mbedtee/shm_pool.h | 15 + drivers/tee/mbedtee/supp.c | 310 +++++++++++++ include/uapi/linux/tee.h | 1 + 20 files changed, 3056 insertions(+), 1 deletion(-) create mode 100644 Documentation/tee/mbedtee.rst create mode 100644 drivers/tee/mbedtee/Kconfig create mode 100644 drivers/tee/mbedtee/Makefile create mode 100644 drivers/tee/mbedtee/core.c create mode 100644 drivers/tee/mbedtee/mbedtee_drv.h create mode 100644 drivers/tee/mbedtee/mbedtee_msg.h create mode 100644 drivers/tee/mbedtee/rpc_callee.c create mode 100644 drivers/tee/mbedtee/rpc_callee_arm.c create mode 100644 drivers/tee/mbedtee/rpc_callee_riscv.c create mode 100644 drivers/tee/mbedtee/rpc_caller.c create mode 100644 drivers/tee/mbedtee/rpc_caller_arm.c create mode 100644 drivers/tee/mbedtee/rpc_caller_riscv.c create mode 100644 drivers/tee/mbedtee/shm_pool.c create mode 100644 drivers/tee/mbedtee/shm_pool.h create mode 100644 drivers/tee/mbedtee/supp.c
Documentation/tee/index.rst +1 -0
@@ -12,3 +12,4 @@ TEE Subsystem amd-tee ts-tee qtee + mbedtee
Documentation/tee/mbedtee.rst new file +155 -0
155 lines not on this page · raw message
MAINTAINERS +9 -0
@@ -15920,6 +15920,15 @@ F: Documentation/devicetree/bindings/net/dsa/maxlinear,mxl862xx.yaml F: drivers/net/dsa/mxl862xx/ F: net/dsa/tag_mxl862xx.c +MBEDTEE TEE DRIVER +M: Xing Loong <[email protected]> +L: [email protected] (moderated for non-subscribers) +S: Maintained +W: https://github.com/mbedtee +F: Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml +F: Documentation/tee/mbedtee.rst +F: drivers/tee/mbedtee/ + MCAN DEVICE DRIVER M: Markus Schneider-Pargmann <[email protected]> L: [email protected]
drivers/tee/Kconfig +2 -1
@@ -2,7 +2,7 @@ # Generic Trusted Execution Environment Configuration menuconfig TEE tristate "Trusted Execution Environment support" - depends on HAVE_ARM_SMCCC || COMPILE_TEST || CPU_SUP_AMD + depends on HAVE_ARM_SMCCC || RISCV || COMPILE_TEST || CPU_SUP_AMD select CRYPTO_LIB_SHA1 select DMA_SHARED_BUFFER select GENERIC_ALLOCATOR
@@ -21,5 +21,6 @@ source "drivers/tee/optee/Kconfig" source "drivers/tee/amdtee/Kconfig" source "drivers/tee/tstee/Kconfig" source "drivers/tee/qcomtee/Kconfig" +source "drivers/tee/mbedtee/Kconfig" endif
drivers/tee/Makefile +1 -0
@@ -8,3 +8,4 @@ obj-$(CONFIG_OPTEE) += optee/ obj-$(CONFIG_AMDTEE) += amdtee/ obj-$(CONFIG_ARM_TSTEE) += tstee/ obj-$(CONFIG_QCOMTEE) += qcomtee/ +obj-$(CONFIG_MBEDTEE) += mbedtee/
drivers/tee/mbedtee/Kconfig new file +20 -0
@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0-only +# MbedTEE Trusted Execution Environment Configuration +config MBEDTEE + tristate "MbedTEE" + depends on MMU && OF + depends on ((ARM || ARM64) && HAVE_ARM_SMCCC) || (RISCV && GENERIC_MSI_IRQ) + help + This enables the MbedTEE Trusted Execution Environment (TEE) + driver. + + The driver supports ARM TrustZone (SMC calls, GIC SPI notifications) + and RISC-V IMSIC (shared-memory polling, MSI notifications), with + GlobalPlatform client API via /dev/tee*. + + The DT node should be under a firmware node and have compatible = + "mbedtee,tee" with the RPC ring buffer addresses and, + for ARM/ARM64, an interrupt property for TEE-to-REE notifications. + See Documentation/tee/mbedtee.rst for protocol details. + + If unsure, say N.
drivers/tee/mbedtee/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_MBEDTEE) += mbedtee.o + +mbedtee-y += core.o +mbedtee-y += supp.o +mbedtee-y += shm_pool.o +mbedtee-y += rpc_caller.o rpc_callee.o +mbedtee-$(CONFIG_ARM) += rpc_caller_arm.o rpc_callee_arm.o +mbedtee-$(CONFIG_ARM64) += rpc_caller_arm.o rpc_callee_arm.o +mbedtee-$(CONFIG_RISCV) += rpc_caller_riscv.o rpc_callee_riscv.o
drivers/tee/mbedtee/core.c new file +236 -0
236 lines not on this page · raw message
drivers/tee/mbedtee/mbedtee_drv.h new file +269 -0
269 lines not on this page · raw message
drivers/tee/mbedtee/mbedtee_msg.h new file +219 -0
219 lines not on this page · raw message
drivers/tee/mbedtee/rpc_callee.c new file +618 -0
618 lines not on this page · raw message
drivers/tee/mbedtee/rpc_callee_arm.c new file +91 -0
91 lines not on this page · raw message
drivers/tee/mbedtee/rpc_callee_riscv.c new file +203 -0
203 lines not on this page · raw message
drivers/tee/mbedtee/rpc_caller.c new file +532 -0
532 lines not on this page · raw message
drivers/tee/mbedtee/rpc_caller_arm.c new file +66 -0
66 lines not on this page · raw message
drivers/tee/mbedtee/rpc_caller_riscv.c new file +192 -0
192 lines not on this page · raw message
drivers/tee/mbedtee/shm_pool.c new file +105 -0
105 lines not on this page · raw message
drivers/tee/mbedtee/shm_pool.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (c) 2015, Linaro Limited +* Copyright (c) 2016, EPAM Systems +* Copyright (c) 2020 Xing Loong <[email protected]> +*/ + +#ifndef SHM_POOL_H +#define SHM_POOL_H + +#include <linux/tee_drv.h> + +struct tee_shm_pool *mbedtee_shm_pool_alloc_pages (void ); + +#endif
drivers/tee/mbedtee/supp.c new file +310 -0
231 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider: - [Critical] Unvalidated cmd.size in the rpc_pick_next error path causes kernel stack buffer overflow.…
Thank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider:
- [Critical] Unvalidated cmd.size in the rpc_pick_next error path causes kernel stack buffer overflow.
- [High] xa_insert_irq() called with GFP_KERNEL causes sleeping in atomic context.
- [High] Early parameter validation failure in mbedtee_supp_send() hangs kernel worker threads.
- [High] Malicious TEE can deadlock the kernel by sending supplicant requests as async RPCs.
- [High] RPC call object is accessed after being freed when wait_for_completion is interrupted.
- [High] UAF and list corruption in mbedtee_supp_recv due to unprotected access.
- [High] Unvalidated parameter attributes from the TEE allow an attacker to permanently leak shared memory object references.
- [Medium] RISC-V yield calls fail spuriously with -ENOSPC when the REE-to-TEE ring is transiently full.
- [Medium] The required buffer size is not returned to userspace when a supplicant buffer is too small.
- [Medium] Infinite retry loop in workqueue on TEE failure blocks teardown.
- [Medium] Leaked RPC calls on module removal due to xa_destroy without element freeing.
- [Medium] Data race on complete_work_pending between IRQ and workqueue contexts.
- [Medium] Transient memory allocation failure in rpc_pick_next stalls the ring buffer permanently.
--
--- Patch [3]: [PATCH v3 3/3] tee: add MbedTEE driver --- commit a7be6b832a5cd2c0d320336aea6907717da8717c
Author: Xing Loong <[email protected]>
tee: add MbedTEE driver
This patch adds a Linux TEE driver that communicates with MbedTEE OS via a fixed shared-memory RPC protocol. It supports ARM TrustZone and RISC-V IMSIC platforms.
Quoted 3 lines
drivers/tee/mbedtee/core.c +0
-0
[ ... ]
Quoted 5 lines
drivers/tee/mbedtee/core.c +0
-0@@ -198,16 +198,15 @@ static void mbedtee_remove(struct platform_device *pdev) {struct mbedtee_device *mbedtee =platform_get_drvdata (pdev);tee_device_unregister (mbedtee->teedev);
drivers/tee/mbedtee/core.c static void mbedtee_remove(struct platform_device *pdev) · line 211
Quoted 10 lines
drivers/tee/mbedtee/core.c +0
-0/* * Drain T2R before tearing down R2T: queued RPC work may still need * to send COMPLETE_TEE over R2T. Destroy the supplicant mutex only * after RPC work can no longer enter mbedtee_supp_handler(). */ mbedtee_rpc_uninit (mbedtee);mbedtee_r2t_uninit (mbedtee);mbedtee_supp_uninit (&mbedtee->supp);tee_shm_pool_free (mbedtee->pool);xa_destroy (&mbedtee->rpc_calls);
[Severity: Medium]
Are the elements in the xarray leaked on module removal? Calling xa_destroy frees the internal nodes of the xarray, but it does not iterate over and free the elements themselves. Any RPC calls left in the MBEDTEE_RPC_CALL_INTERRUPTED state would be permanently leaked.
Quoted 5 lines
drivers/tee/mbedtee/core.c +0
-0}drivers/tee/mbedtee/rpc_callee.c +0
-0
[ ... ]
drivers/tee/mbedtee/rpc_callee.c static void t2r_ring_read(struct mbedtee_t2r_ctx *ctx, · line 153
Quoted 7 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0@@ -148,15 +148,15 @@ static void t2r_ring_read(struct mbedtee_t2r_ctx *ctx, }static bool rpc_queue_complete_only (struct mbedtee_t2r_ctx *ctx,u64 waiter_id) {if (ctx->complete_work_pending)
[Severity: Medium]
Is there a data race on complete_work_pending here? This variable is read as a plain C variable in this IRQ context, but it is modified using WRITE_ONCE in rpc_routine from a workqueue context. This could allow the compiler to cache or reorder the read, potentially leaving the driver perpetually thinking a completion work is pending.
Quoted 8 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0return false; ctx->complete_work_pending = true; ctx->complete_work.waiter_id = waiter_id;queue_work (ctx->rpc_wq, &ctx->complete_work.work);return true; }
[ ... ]
drivers/tee/mbedtee/rpc_callee.c static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee, · line 221
Quoted 8 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0@@ -215,9 +215,9 @@ static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee,if (off > ctx->t2r_shm_sz - cmd.size) goto skip; new_work =kzalloc_obj (*new_work,GFP_ATOMIC );if (!new_work)return NULL ;
[Severity: Medium]
Does a transient memory allocation failure here permanently stall the ring buffer? If kzalloc_obj fails with GFP_ATOMIC, the function returns NULL and rpc_drain_ring breaks out of its loop without advancing the read pointer. The TEE thread would block forever since the entry remains unprocessed and no retry is scheduled.
Quoted 2 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0t2r_ring_advance (ctx,sizeof (cmd));
[ ... ]
drivers/tee/mbedtee/rpc_callee.c skip: · line 287
Quoted 10 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0@@ -279,11 +279,11 @@ skip:if (cmd.waiter_id !=0 && !rpc_queue_complete_only (ctx, cmd.waiter_id))return NULL ;/* * Bad or unhandled entry: consume header plus any inline payload * (async RPC payloads follow the header in the ring) to keep the * ring moving. For sync RPCs payload is in t2r_shm, not the ring. */ t2r_ring_advance (ctx,sizeof (cmd) + (cmd.waiter_id ?0 : cmd.size));
[Severity: Critical]
Can this advance the read pointer by an unvalidated size and cause a stack buffer overflow? In rpc_pick_next, if an async RPC jumps to the skip label, t2r_ring_advance is called with 32 + cmd.size without checking if cmd.size exceeds the ring buffer capacity.
If a malicious TEE sets a very large cmd.size, the read pointer wraps. On the next iteration, t2r_available_size underflows and t2r_ring_copy executes a memcpy of up to 4GB directly into the cmd struct on the kernel stack.
Quoted 3 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0return NULL ; }
[ ... ]
drivers/tee/mbedtee/rpc_callee.c static void rpc_drain_ring(struct mbedtee_device *mbedtee) · line 306
Quoted 12 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0@@ -296,14 +296,14 @@ static void rpc_drain_ring(struct mbedtee_device *mbedtee)spin_lock_irqsave (&ctx->ring_lock, flags);while (READ_ONCE (ctx->t2r_ring_rd) !=/* Pair with producer store-release after ring write. */ smp_load_acquire (&ctx->t2r_ring->wr)) { rw.data = ctx->rpc_data; c =rpc_pick_next (mbedtee, ctx, &rw);if (!c)break ;if (c->waiter_id ==0 ) { c->func (mbedtee, c->data, c->size);
[Severity: High]
Is it possible for a malicious TEE to deadlock the kernel by sending supplicant requests as async RPCs? The TEE-to-REE interrupt handler runs in hard IRQ context, and rpc_drain_ring relies entirely on cmd.waiter_id == 0 to process a command inline.
If the TEE sends MBEDTEE_RPC_REEFS with waiter_id = 0, the driver invokes mbedtee_supp_handler inline while holding the ring_lock. That function allocates memory with GFP_KERNEL and calls wait_for_completion, both of which sleep in atomic context.
Quoted 7 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0}else {INIT_WORK (&c->work, rpc_routine);queue_work (ctx->rpc_wq, &c->work);drivers/tee/mbedtee/rpc_callee.c +0
-0} }spin_unlock_irqrestore (&ctx->ring_lock, flags); }
[ ... ]
drivers/tee/mbedtee/rpc_callee.c static void rpc_routine(struct work_struct *work) · line 349
Quoted 12 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0@@ -339,12 +339,12 @@ static void rpc_routine(struct work_struct *work)* Keep retrying rather than silently dropping the completion, which * would leave the TEE thread blocked in rpc_call_sync forever. * On ARM the fastcall is a direct SMC so -ENOSPC never occurs. */ do { ret =mbedtee_rpc_fastcall (c->mbedtee,MBEDTEE_RPC_COMPLETE_TEE , (unsigned long )c->waiter_id,0 ,0 );if (ret != -ENOSPC && ret != -ENOMEM )break ;cond_resched (); }while (1 );
[Severity: Medium]
Could this loop infinitely if the ring buffer remains full? If mbedtee_rpc_fastcall repeatedly returns -ENOSPC (which occurs on RISC-V when the R2T ring is full), it retries indefinitely. If the TEE crashes and stops draining the ring, this loop never terminates, and destroy_workqueue during module removal would block forever waiting for the hung work item.
Quoted 6 lines
drivers/tee/mbedtee/rpc_callee.c +0
-0if (c != &ctx->complete_work) {drivers/tee/mbedtee/rpc_caller.c +0
-0
[ ... ]
drivers/tee/mbedtee/rpc_caller.c int mbedtee_rpc_call_alloc(struct mbedtee_device *mbedtee, · line 42
Quoted 9 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0@@ -35,12 +35,12 @@ int mbedtee_rpc_call_alloc(struct mbedtee_device *mbedtee, rcall->rpc.ret = -EOPNOTSUPP ; rcall->rpc_phys =mbedtee_virt_to_phys (&rcall->rpc);do { rpc_index =atomic_long_inc_return (&mbedtee->rpc_call_seq); }while (rpc_index ==0 ); ret =xa_insert_irq (&mbedtee->rpc_calls, rpc_index, rcall,GFP_KERNEL );
[Severity: High]
Does this code sleep in atomic context? The driver initializes mbedtee->rpc_calls with XA_FLAGS_ALLOC1. Calling xa_insert_irq with GFP_KERNEL drops the spinlock but leaves hardware interrupts disabled, then calls kmem_cache_alloc with GFP_KERNEL which can sleep, violating atomic context rules.
Quoted 4 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0if (ret !=0 ) {kfree (rcall);return ret; }
[ ... ]
drivers/tee/mbedtee/rpc_caller.c static int mbedtee_param_decode(struct tee_param *params, · line 178
Quoted 10 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0@@ -170,14 +170,14 @@ static int mbedtee_param_decode(struct tee_param *params,case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT : p->u.memref.size = rtp->memref.size;break ;default :return -EINVAL ; } p->attr = attr;
[Severity: High]
Can a malicious TEE permanently leak shared memory object references by returning unvalidated parameter attributes? The kernel decodes the parameter types returned by the TEE and unconditionally overwrites the local p->attr with the TEE's value.
If userspace passes a MEMREF, a compromised TEE could return a VALUE type for that slot, causing the TEE subsystem to skip dropping the reference during cleanup because tee_param_is_memref will see a VALUE type instead.
Quoted 3 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0}return 0 ; }
[ ... ]
drivers/tee/mbedtee/rpc_caller.c int mbedtee_invoke_func(struct tee_context *ctx, · line 423
Quoted 10 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0@@ -415,12 +415,12 @@ int mbedtee_invoke_func(struct tee_context *ctx,if (ret) {mbedtee_rpc_call_free (mbedtee, call);return ret; } ret =mbedtee_rpc_yieldcall (mbedtee,MBEDTEE_RPC_INVOKE_SESSION , call, true);dev_dbg (mbedtee->dev,"invoke session ret %d gp_ret %d\n" , ret, call->rpc.ret);
[Severity: High]
Does this introduce a use-after-free if wait_for_completion is interrupted? If mbedtee_rpc_wait_for_completion is interrupted by a signal, it sets the state to MBEDTEE_RPC_CALL_INTERRUPTED and returns -ERESTARTSYS. The caller here then reads call->rpc.ret and calls mbedtee_rpc_call_free.
Concurrently, mbedtee_rpc_complete_call can see the INTERRUPTED state, erase the XArray entry, and free the call structure, causing the original caller to perform UAF reads.
Quoted 7 lines
drivers/tee/mbedtee/rpc_caller.c +0
-0if (ret !=0 ) { arg->ret =TEEC_ERROR_COMMUNICATION ;drivers/tee/mbedtee/rpc_caller_riscv.c +0
-0
[ ... ]
drivers/tee/mbedtee/rpc_caller_riscv.c long mbedtee_rpc_yieldcall(struct mbedtee_device *mbedtee, · line 81
Quoted 8 lines
drivers/tee/mbedtee/rpc_caller_riscv.c +0
-0@@ -75,10 +75,10 @@ long mbedtee_rpc_yieldcall(struct mbedtee_device *mbedtee, call->rpc.id = fn;spin_lock_irqsave (&ctx->lock, flags);if (!rpc_ring_enough (ctx,sizeof (u64))) {dev_err_ratelimited (mbedtee->dev,"rpc ring full\n" );spin_unlock_irqrestore (&ctx->lock, flags);return -ENOSPC ;
[Severity: Medium]
Should this implement a wait queue or backoff instead of failing immediately? On RISC-V, mbedtee_rpc_yieldcall returns -ENOSPC if the r2t-ring buffer is full, which mbedtee_invoke_func translates to TEEC_ERROR_COMMUNICATION. Without a polling backoff or wait mechanism, high concurrency from userspace will easily fill the ring and cause spurious, unrecoverable failures.
Quoted 7 lines
drivers/tee/mbedtee/rpc_caller_riscv.c +0
-0} phys = call->rpc_phys;drivers/tee/mbedtee/supp.c +0
-0
[ ... ]
drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx, · line 241
Quoted 11 lines
drivers/tee/mbedtee/supp.c +0
-0@@ -232,18 +232,18 @@ int mbedtee_supp_recv(struct tee_context *ctx,if (wait_for_completion_interruptible (&supp->reqs_c)) { ret = -ERESTARTSYS ; goto err; } }/* Record which supplicant context owns this request. */ req->ctx = ctx;if (req->size > param->u.memref.size) {
[Severity: High]
Can this cause a use-after-free and linked list corruption? After supp_pop_req removes a request and drops the mutex, the function accesses req->ctx and req->size locklessly.
If mbedtee_supp_abort_all runs concurrently (e.g. on module removal), it could remove the request from active_reqs, complete it, and free it, causing UAF reads here. If the size check fails, it then takes the mutex and manipulates the list links of the freed node, corrupting the supp->reqs list.
[Severity: Medium]
Does this code fail to return the required buffer size to userspace? When req->size is larger than param->u.memref.size, it correctly returns -EOVERFLOW but fails to update param->u.memref.size with the required req->size. The generic TEE framework relies on this updated value to inform userspace how much memory to allocate, breaking buffer renegotiation.
Quoted 8 lines
drivers/tee/mbedtee/supp.c +0
-0/* Return the request to the queue so it isn't lost */ mutex_lock (&supp->mutex);list_del (&req->node);list_add (&req->node, &supp->reqs);mutex_unlock (&supp->mutex); ret = -EOVERFLOW ; goto err; }
[ ... ]
drivers/tee/mbedtee/supp.c int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, · line 271
Quoted 11 lines
drivers/tee/mbedtee/supp.c +0
-0@@ -262,11 +262,11 @@ int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, struct tee_param *param) {struct tee_device *teedev = ctx->teedev;struct mbedtee_device *mbedtee =tee_get_drvdata (teedev);struct mbedtee_supp *supp = &mbedtee->supp;struct mbedtee_supp_req *req;struct mbedtee_supp_req *_req;if (num_params !=1 )return -EINVAL ;
[Severity: High]
Could this lead to a permanently hung kernel worker thread? If userspace calls TEE_IOC_SUPPL_SEND with invalid parameters (like num_params != 1), mbedtee_supp_send returns -EINVAL early. Because it does not pop the active request or complete it, the worker thread executing mbedtee_supp_handler would remain permanently blocked in wait_for_completion.
Quoted 3 lines
drivers/tee/mbedtee/supp.c +0
-0if (!tee_param_is_memref (param))return
-EINVAL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3