Skip to contentSkip to conversation

tee: add MbedTEE driver

PATCH Superseded Xing Loong 4 participants 16 messages Jul 1 – Jul 3

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xing Loong

author
#0 lore raw

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,rpc binding [3/3] tee: add MbedTEE driver

Xing Loong (3):
dt-bindings: vendor-prefixes: add mbedtee
dt-bindings: firmware: add mbedtee,rpc binding
tee: add MbedTEE driver

 .../bindings/firmware/mbedtee,rpc.yaml        | 221 +++++++
 .../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              | 609 ++++++++++++++++++
 drivers/tee/mbedtee/rpc_callee_arm.c          |  91 +++
 drivers/tee/mbedtee/rpc_callee_riscv.c        | 203 ++++++
 drivers/tee/mbedtee/rpc_caller.c              | 539 ++++++++++++++++
 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, 3277 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,rpc.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
author
#1 lore raw

PATCH 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
index 28784d6..fd032a3 100644
@@ -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
author
#2 lore raw

PATCH 2/3 dt-bindings: firmware: add mbedtee,rpc binding

In reply to Xing Loong

Add YAML devicetree binding for the MbedTEE Trusted Execution Environment driver.

The binding covers two platform configurations:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (rpc-t2r-ring and rpc-t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding rpc-r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).

---
 .../bindings/firmware/mbedtee,rpc.yaml        | 221 ++++++++++++++++++
 1 file changed, 221 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +221 -0
new file mode 100644
index 0000000..08ae255
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/firmware/mbedtee,rpc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MbedTEE Trusted Execution Environment
+
+maintainers:
+  - Xing Loong <[email protected]>
+
+description: |
+  MbedTEE is a Trusted Execution Environment for embedded systems.
+  This binding describes the shared-memory regions used for RPC
+  communication between the Linux REE driver and MbedTEE OS.
+
+  The REE and TEE CPUs sharing the RPC memory must be in a
+  hardware-coherent domain (same CPU cluster, coherent caches).
+
+  Two or three reserved-memory regions are required:
+
+    rpc-t2r-ring  ring buffer for TEE-to-REE notifications (all platforms)
+    rpc-t2r-shm   shared memory for TEE-to-REE RPC payloads (all platforms)
+    rpc-r2t-ring  ring buffer for REE-to-TEE command submissions (RISC-V only)
+
+  On ARM/AArch64 the transport uses SMC calls; TEE-to-REE
+  notifications use a GIC SPI edge interrupt.
+
+  On RISC-V the TEE notifies the REE via IMSIC MSI; the REE submits
+  commands via shared-memory rpc-r2t-ring that the TEE polls. No
+  REE-to-TEE interrupt is used. No SBI ecall is involved.
+
+properties:
+  $nodename:
+    const: mbedtee
+
+  compatible:
+    const: mbedtee,rpc
+
+  interrupts:
+    description:
+      GIC interrupt used by the TEE to notify the REE of pending RPC
+      responses (ARM/AArch64 only). Not present on RISC-V platforms which
+      use IMSIC platform MSI interrupts allocated dynamically at runtime.
+
+  msi-parent:
+    maxItems: 1
+    description:
+      IMSIC MSI controller used by the Linux driver to allocate the
+      TEE-to-REE notification interrupt on RISC-V platforms. Not present on
+      ARM/AArch64 platforms, which use the interrupts property.
+
+  memory-region:
+    minItems: 2
+    maxItems: 3
+    description:
+      References to reserved-memory regions for REE<->TEE communication.
+      Entries must match memory-region-names order.
+
+  memory-region-names:
+    minItems: 2
+    maxItems: 3
+    items:
+      enum:
+        - rpc-t2r-ring
+        - rpc-t2r-shm
+        - rpc-r2t-ring
+
+required:
+  - compatible
+
+allOf:
+  - if:
+      required:
+        - interrupts
+    then:
+      required:
+        - interrupts
+        - memory-region
+        - memory-region-names
+      properties:
+        msi-parent: false
+        memory-region:
+          minItems: 2
+          maxItems: 2
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
+            - const: rpc-t2r-shm
+    else:
+      required:
+        - msi-parent
+        - memory-region
+        - memory-region-names
+      properties:
+        memory-region:
+          minItems: 3
+          maxItems: 3
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
+            - const: rpc-t2r-shm
+            - const: rpc-r2t-ring
+
+additionalProperties: false
+
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    / {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      gic: interrupt-controller@2f000000 {
+        compatible = "arm,gic-v3";
+        reg = <0 0x2f000000 0 0x10000>,
+              <0 0x2f100000 0 0x200000>;
+        interrupt-controller;
+        #interrupt-cells = <3>;
+      };
+
+      reserved-memory {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+
+        mbedtee_t2r_ring: rpc-t2r-ring@85f10000 {
+          reg = <0 0x85f10000 0 0x20000>;
+          no-map;
+        };
+
+        mbedtee_t2r_shm: rpc-t2r-shm@85f30000 {
+          reg = <0 0x85f30000 0 0x40000>;
+          no-map;
+        };
+      };
+
+      firmware {
+        mbedtee {
+          compatible = "mbedtee,rpc";
+          interrupt-parent = <&gic>;
+          interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+          memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
+          memory-region-names = "rpc-t2r-ring", "rpc-t2r-shm";
+        };
+      };
+    };
+
+  - |
+    /* RISC-V IMSIC (ring-buffer polling REE->TEE, MSI TEE->REE) */
+    / {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      cpus {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        cpu@0 {
+          device_type = "cpu";
+          reg = <0>;
+
+          cpu0_intc: interrupt-controller {
+            compatible = "riscv,cpu-intc";
+            interrupt-controller;
+            #interrupt-cells = <1>;
+          };
+        };
+      };
+
+      imsic: interrupt-controller@28000000 {
+        compatible = "qemu,imsics", "riscv,imsics";
+        reg = <0 0x28000000 0 0x1000>;
+        interrupts-extended = <&cpu0_intc 9>;
+        interrupt-controller;
+        #interrupt-cells = <0>;
+        msi-controller;
+        #msi-cells = <0>;
+        riscv,num-ids = <255>;
+      };
+
+      reserved-memory {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+
+        rv_t2r_ring: rpc-t2r-ring@5f10000 {
+          reg = <0 0x5f10000 0 0x20000>;
+          no-map;
+        };
+
+        rv_t2r_shm: rpc-t2r-shm@5f30000 {
+          reg = <0 0x5f30000 0 0x40000>;
+          no-map;
+        };
+
+        rv_r2t_ring: rpc-r2t-ring@5f70000 {
+          reg = <0 0x5f70000 0 0x20000>;
+          no-map;
+        };
+      };
+
+      /*
+       * The riscv container avoids a duplicate /firmware/mbedtee path
+       * with the ARM example above when the DT checker concatenates
+       * examples into a single .dts.
+       */
+      riscv {
+        firmware {
+          mbedtee {
+            compatible = "mbedtee,rpc";
+            msi-parent = <&imsic>;
+            memory-region = <&rv_t2r_ring>, <&rv_t2r_shm>,
+                            <&rv_r2t_ring>;
+            memory-region-names = "rpc-t2r-ring", "rpc-t2r-shm",
+                                  "rpc-r2t-ring";
+          };
+        };
+      };
+    };
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/3] dt-bindings: firmware: add mbedtee,rpc binding

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing `maxItems` constraint for the `interrupts` property. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing maxItems constraint for the interrupts property.
--

commit ad51309fadfb753784caac099277a6b19357c757
Author: Xing Loong <[email protected]>

dt-bindings: firmware: add mbedtee,rpc binding

This commit adds the YAML devicetree binding for the MbedTEE Trusted Execution Environment driver. It describes the shared-memory regions used for RPC communication between the Linux REE driver and MbedTEE OS on ARM/AArch64 and RISC-V platforms.

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +0 -0
new file mode 100644
index 0000000000000..08ae255d32c2a

[ ... ]

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 44

Quoted 12 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +12 -0
+properties:
+  $nodename:
+    const: mbedtee
+
+  compatible:
+    const: mbedtee,rpc
+
+  interrupts:
+    description:
+      GIC interrupt used by the TEE to notify the REE of pending RPC
+      responses (ARM/AArch64 only). Not present on RISC-V platforms which
+      use IMSIC platform MSI interrupts allocated dynamically at runtime.

[Severity: Low]
Does this property need a maxItems constraint?

Without maxItems: 1, device tree validation will not catch configurations that accidentally supply multiple interrupts.

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +5 -0
+
+  msi-parent:
+    maxItems: 1
+    description:
+      IMSIC MSI controller used by the Linux driver to allocate the

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH 2/3] dt-bindings: firmware: add mbedtee,rpc binding

In reply to Xing Loong
Quoted 1 line · Xing Loong

Add YAML devicetree binding for the MbedTEE Trusted Execution

Drop YAML, there is no such thing as YAML binding.

Quoted 1 line

Environment driver.

We don't take bindings for drivers but for hardware or firmware. Please describe these instead.

Quoted 25 lines

The binding covers two platform configurations:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (rpc-t2r-ring and rpc-t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding rpc-r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).

---
 .../bindings/firmware/mbedtee,rpc.yaml        | 221 ++++++++++++++++++
 1 file changed, 221 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +4 -0
new file mode 100644
index 0000000..08ae255
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/firmware/mbedtee,rpc.yaml#

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 14

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +10 -0
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MbedTEE Trusted Execution Environment
+
+maintainers:
+  - Xing Loong <[email protected]>
+
+description: |
+  MbedTEE is a Trusted Execution Environment for embedded systems.
+  This binding describes the shared-memory regions used for RPC

Describe firmware, not the binding. It's redundant to say what the binding is about, just say what is the hardware.

Quoted 11 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +11 -0
+  communication between the Linux REE driver and MbedTEE OS.
+
+  The REE and TEE CPUs sharing the RPC memory must be in a
+  hardware-coherent domain (same CPU cluster, coherent caches).
+
+  Two or three reserved-memory regions are required:
+
+    rpc-t2r-ring  ring buffer for TEE-to-REE notifications (all platforms)
+    rpc-t2r-shm   shared memory for TEE-to-REE RPC payloads (all platforms)
+    rpc-r2t-ring  ring buffer for REE-to-TEE command submissions (RISC-V only)
+

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 35

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +10 -0
+  On ARM/AArch64 the transport uses SMC calls; TEE-to-REE
+  notifications use a GIC SPI edge interrupt.
+
+  On RISC-V the TEE notifies the REE via IMSIC MSI; the REE submits
+  commands via shared-memory rpc-r2t-ring that the TEE polls. No
+  REE-to-TEE interrupt is used. No SBI ecall is involved.
+
+properties:
+  $nodename:
+    const: mbedtee

Drop, why would it be relevant?

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 38

Quoted 3 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +3 -0
+
+  compatible:
+    const: mbedtee,rpc

Feels way too generic. First, Google results on mbedtee are basically non-existing, so what sort of company is that?

Second, rpc is just not specific enough. Please carefully read writing bindings doc.

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 44

Quoted 6 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +6 -0
+
+  interrupts:
+    description:
+      GIC interrupt used by the TEE to notify the REE of pending RPC
+      responses (ARM/AArch64 only). Not present on RISC-V platforms which
+      use IMSIC platform MSI interrupts allocated dynamically at runtime.

Please read writing bindings doc.

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 49

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +5 -0
+
+  msi-parent:
+    maxItems: 1
+    description:
+      IMSIC MSI controller used by the Linux driver to allocate the

Again drivers...

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 58

Quoted 9 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +9 -0
+      TEE-to-REE notification interrupt on RISC-V platforms. Not present on
+      ARM/AArch64 platforms, which use the interrupts property.
+
+  memory-region:
+    minItems: 2
+    maxItems: 3
+    description:
+      References to reserved-memory regions for REE<->TEE communication.
+      Entries must match memory-region-names order.

Obvious. Please do not come with your own style of bindings.

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 62

Quoted 4 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +4 -0
+
+  memory-region-names:
+    minItems: 2
+    maxItems: 3

Why is this flexible?

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 65

Quoted 3 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +3 -0
+    items:
+      enum:
+        - rpc-t2r-ring

rpc is redundant, drop

Quoted 19 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +19 -0
+        - rpc-t2r-shm
+        - rpc-r2t-ring
+
+required:
+  - compatible
+
+allOf:
+  - if:
+      required:
+        - interrupts
+    then:
+      required:
+        - interrupts
+        - memory-region
+        - memory-region-names
+      properties:
+        msi-parent: false
+        memory-region:
+          minItems: 2

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 94

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +10 -0
+          maxItems: 2
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
+            - const: rpc-t2r-shm
+    else:
+      required:
+        - msi-parent
+        - memory-region
+        - memory-region-names

So memory-region is always required?

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 103

Quoted 9 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +9 -0
+      properties:
+        memory-region:
+          minItems: 3
+          maxItems: 3
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
+            - const: rpc-t2r-shm
+            - const: rpc-r2t-ring

Your top level schema said that. You only need minItems.

Quoted 24 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +24 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    / {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      gic: interrupt-controller@2f000000 {
+        compatible = "arm,gic-v3";
+        reg = <0 0x2f000000 0 0x10000>,
+              <0 0x2f100000 0 0x200000>;
+        interrupt-controller;
+        #interrupt-cells = <3>;
+      };
+
+      reserved-memory {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 137

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +10 -0
+        mbedtee_t2r_ring: rpc-t2r-ring@85f10000 {
+          reg = <0 0x85f10000 0 0x20000>;
+          no-map;
+        };
+
+        mbedtee_t2r_shm: rpc-t2r-shm@85f30000 {
+          reg = <0 0x85f30000 0 0x40000>;
+          no-map;
+        };
+      };

None of the above is relevant, drop.

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 149

Quoted 12 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +12 -0
+
+      firmware {
+        mbedtee {
+          compatible = "mbedtee,rpc";
+          interrupt-parent = <&gic>;
+          interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+          memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
+          memory-region-names = "rpc-t2r-ring", "rpc-t2r-shm";
+        };
+      };
+    };
+

Best regards,
Krzysztof

author
#5 lore raw

Re: [PATCH 2/3] dt-bindings: firmware: add mbedtee,rpc binding

Quoted 4 lines · Krzysztof Kozlowski
Quoted 1 line · Xing Loong

Add YAML devicetree binding for the MbedTEE Trusted Execution

Drop YAML, there is no such thing as YAML binding.

Will drop.

Quoted 4 lines
Quoted 1 line

Environment driver.

We don't take bindings for drivers but for hardware or firmware. Please describe these instead.

Will rewrite the description to describe what MbedTEE is as firmware, not the driver or the binding.

Quoted 29 lines
Quoted 29 lines

The binding covers two platform configurations:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (rpc-t2r-ring and rpc-t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding rpc-r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).

---
 .../bindings/firmware/mbedtee,rpc.yaml        | 221 ++++++++++++++++++
 1 file changed, 221 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +9 -0
new file mode 100644
index 0000000..08ae255
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/firmware/mbedtee,rpc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MbedTEE Trusted Execution Environment
+
+maintainers:
Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 14

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +5 -0
+  - Xing Loong <[email protected]>
+
+description: |
+  MbedTEE is a Trusted Execution Environment for embedded systems.
+  This binding describes the shared-memory regions used for RPC

Describe firmware, not the binding. It's redundant to say what the binding is about, just say what is the hardware.

Will rewrite, removing "driver", "YAML" and "This binding describes" wording throughout.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +15 -0
+  communication between the Linux REE driver and MbedTEE OS.
+
+  The REE and TEE CPUs sharing the RPC memory must be in a
+  hardware-coherent domain (same CPU cluster, coherent caches).
+
+  Two or three reserved-memory regions are required:
+
+    rpc-t2r-ring  ring buffer for TEE-to-REE notifications (all platforms)
+    rpc-t2r-shm   shared memory for TEE-to-REE RPC payloads (all platforms)
+    rpc-r2t-ring  ring buffer for REE-to-TEE command submissions (RISC-V only)
+
+  On ARM/AArch64 the transport uses SMC calls; TEE-to-REE
+  notifications use a GIC SPI edge interrupt.
+
+  On RISC-V the TEE notifies the REE via IMSIC MSI; the REE submits
Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 35

Quoted 6 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +6 -0
+  commands via shared-memory rpc-r2t-ring that the TEE polls. No
+  REE-to-TEE interrupt is used. No SBI ecall is involved.
+
+properties:
+  $nodename:
+    const: mbedtee

Drop, why would it be relevant?

Will drop.

Quoted 9 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 38

Quoted 3 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +3 -0
+
+  compatible:
+    const: mbedtee,rpc

Feels way too generic. First, Google results on mbedtee are basically non-existing, so what sort of company is that?

Second, rpc is just not specific enough. Please carefully read writing bindings doc.

Will rename to "mbedtee,tee". mbedtee is an open-source TEE project (https://github.com/mbedtee), not a company. Patch 1/3 adds the corresponding "mbedtee" entry to vendor-prefixes.yaml.

Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 44

Quoted 6 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +6 -0
+
+  interrupts:
+    description:
+      GIC interrupt used by the TEE to notify the REE of pending RPC
+      responses (ARM/AArch64 only). Not present on RISC-V platforms which
+      use IMSIC platform MSI interrupts allocated dynamically at runtime.

Please read writing bindings doc.

Will drop the description. Will add maxItems: 1 instead.

Quoted 7 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 49

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +5 -0
+
+  msi-parent:
+    maxItems: 1
+    description:
+      IMSIC MSI controller used by the Linux driver to allocate the

Again drivers...

Will drop the description. Will keep only maxItems: 1.

Quoted 11 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 58

Quoted 9 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +9 -0
+      TEE-to-REE notification interrupt on RISC-V platforms. Not present on
+      ARM/AArch64 platforms, which use the interrupts property.
+
+  memory-region:
+    minItems: 2
+    maxItems: 3
+    description:
+      References to reserved-memory regions for REE<->TEE communication.
+      Entries must match memory-region-names order.

Obvious. Please do not come with your own style of bindings.

Will drop the description. Understood, will follow the standard style used by existing bindings.

Quoted 6 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 62

Quoted 4 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +4 -0
+
+  memory-region-names:
+    minItems: 2
+    maxItems: 3

Why is this flexible?

Will constrain it via the allOf: if/then branches (ARM: fixed at 2,
RISC-V: fixed at 3).

Quoted 5 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 65

Quoted 3 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +3 -0
+    items:
+      enum:
+        - rpc-t2r-ring

rpc is redundant, drop

Will drop the "rpc-" prefix. Will use t2r-ring, t2r-shm, r2t-ring.

Quoted 23 lines
Quoted 23 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +23 -0
+        - rpc-t2r-shm
+        - rpc-r2t-ring
+
+required:
+  - compatible
+
+allOf:
+  - if:
+      required:
+        - interrupts
+    then:
+      required:
+        - interrupts
+        - memory-region
+        - memory-region-names
+      properties:
+        msi-parent: false
+        memory-region:
+          minItems: 2
+          maxItems: 2
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 94

Quoted 6 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +6 -0
+            - const: rpc-t2r-shm
+    else:
+      required:
+        - msi-parent
+        - memory-region
+        - memory-region-names

So memory-region is always required?

Yes. Will move memory-region and memory-region-names to the top-level required: list to make this explicit. The interrupts and msi-parent properties will also be made mutually exclusive per platform (msi-parent: false on ARM, interrupts: false on RISC-V).

Quoted 11 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 103

Quoted 9 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +9 -0
+      properties:
+        memory-region:
+          minItems: 3
+          maxItems: 3
+        memory-region-names:
+          items:
+            - const: rpc-t2r-ring
+            - const: rpc-t2r-shm
+            - const: rpc-r2t-ring

Your top level schema said that. You only need minItems.

Will drop the redundant constraints from the else branch.

Quoted 28 lines
Quoted 28 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +28 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    / {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      gic: interrupt-controller@2f000000 {
+        compatible = "arm,gic-v3";
+        reg = <0 0x2f000000 0 0x10000>,
+              <0 0x2f100000 0 0x200000>;
+        interrupt-controller;
+        #interrupt-cells = <3>;
+      };
+
+      reserved-memory {
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+
+        mbedtee_t2r_ring: rpc-t2r-ring@85f10000 {
+          reg = <0 0x85f10000 0 0x20000>;
+          no-map;
+        };
Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 137

Quoted 6 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +6 -0
+
+        mbedtee_t2r_shm: rpc-t2r-shm@85f30000 {
+          reg = <0 0x85f30000 0 0x40000>;
+          no-map;
+        };
+      };

None of the above is relevant, drop.

Will drop all platform infrastructure nodes. Will follow the minimal example pattern seen in linaro,optee-tz.yaml.

Quoted 7 lines
Quoted 7 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +7 -0
+
+      firmware {
+        mbedtee {
+          compatible = "mbedtee,rpc";
+          interrupt-parent = <&gic>;
+          interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+          memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
Quoted 8 lines

Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml line 149

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml new file +5 -0
+          memory-region-names = "rpc-t2r-ring", "rpc-t2r-shm";
+        };
+      };
+    };
+

Best regards,
Krzysztof

Thanks for the thorough review. Will send v3 after incorporating all of the above.

Best regards,
Xing Loong

#6 lore raw

Re: [PATCH 2/3] dt-bindings: firmware: add mbedtee,rpc binding

In reply to Xing Loong
Quoted 11 lines · Xing Loong

Add YAML devicetree binding for the MbedTEE Trusted Execution Environment driver.

The binding covers two platform configurations:
- ARM/AArch64 (TrustZone, SMC): two reserved-memory regions (rpc-t2r-ring and rpc-t2r-shm) plus a GIC SPI edge interrupt for TEE-to-REE notifications.
- RISC-V (IMSIC): three reserved-memory regions, adding rpc-r2t-ring for REE-to-TEE command submissions; no interrupts property (T2R notifications use IMSIC MSI allocated at runtime).

Quoted 6 lines
---
 .../bindings/firmware/mbedtee,rpc.yaml        | 221 ++++++++++++++++++
 1 file changed, 221 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,rpc.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/firmware/mbedtee,rpc.example.dtb: /: 'compatible' is a required property
from schema $id: http://devicetree.org/schemas/root-node.yaml
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/firmware/mbedtee,rpc.example.dtb: /: 'model' is a required property
from schema $id: http://devicetree.org/schemas/root-node.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.kernel.org/project/devicetree/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

author
#7 lore raw

PATCH 3/3 tee: add MbedTEE driver

In reply to Xing Loong

This 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 rpc-r2t-ring that the TEE polls; the TEE notifies the REE by writing to rpc-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.

---
 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       | 609 +++++++++++++++++++++++++
 drivers/tee/mbedtee/rpc_callee_arm.c   |  91 ++++
 drivers/tee/mbedtee/rpc_callee_riscv.c | 203 +++++++++
 drivers/tee/mbedtee/rpc_caller.c       | 539 ++++++++++++++++++++++
 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, 3054 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
index 10c3cec..6415855 100644
@@ -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
index c8d4b91..9393dfb 100644
@@ -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,rpc.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
index 98c3ad0..3dc3439 100644
@@ -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
index 3239b91..33117ea 100644
@@ -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
new file mode 100644
index 0000000..a28be27
@@ -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,rpc" 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
new file mode 100644
index 0000000..309eb6d
@@ -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 +609 -0

609 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 +539 -0

539 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
new file mode 100644
index 0000000..4510676
@@ -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

233 lines not on this page · raw message

include/uapi/linux/tee.h +1 -0
index 5203977..2d30296 100644

Lines of this file are not shown. View the 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.

#8 lore raw
Thank you for your contribution! Sashiko AI review found 16 potential issue(s) to consider: - [High] Calling xa_insert_irq() with GFP_KERNEL without XA_FLAGS_LOCK_IRQ causes a sleep-in-atomic kernel…

Thank you for your contribution! Sashiko AI review found 16 potential issue(s) to consider:
- [High] Calling xa_insert_irq() with GFP_KERNEL without XA_FLAGS_LOCK_IRQ causes a sleep-in-atomic kernel panic.
- [High] mbedtee_release() leaks sessions if a TEE context acts as a supplicant.
- [High] Transient GFP_ATOMIC allocation failures cause a permanent hang of the TEE-to-REE channel.
- [High] rpc_routine() silently drops the COMPLETE_TEE notification if mbedtee_rpc_fastcall() fails with -ENOMEM.
- [High] Closing any single supplicant context incorrectly aborts all unassigned global requests.
- [High] Dereferencing and freeing call after transferring ownership to the background completion handler on interrupted wait.
- [High] Double free of tee_shm object on error path in mbedtee_supp_recv due to missing NULL assignment.
- [High] Missing reference acquisition for req->shm leads to UAF and double free in mbedtee_supp_send.
- [High] Uninitialized or stale error codes are sent to the TEE when supplicant requests complete or abort.
- [High] The TEE-to-REE RPC hardirq handler sleeps while holding a spinlock if the TEE sends an async supplicant request, leading to an immediate kernel panic.
- [High] The RPC ring buffer parser skips only the command header when encountering invalid or oversized async RPCs, abandoning payload bytes in the ring.
- [High] Physical pages backing dynamic shared memory are unconditionally freed to the Linux allocator even if the driver fails to revoke the TEE's access.
- [High] The driver initializes its local ring buffer indices directly from unvalidated shared memory, enabling arbitrary out-of-bounds kernel memory accesses.
- [Medium] mbedtee_release() leaks mbedtee_session objects if mbedtee_close_session() fails.
- [Medium] Bypassing wait_for_completion_interruptible() creates an unbounded phantom wakeup accumulation causing CPU spikes.
- [Medium] Permanent memory leak of mbedtee_rpc_call structures on module removal for interrupted calls.
--

commit 37902c22c67b16bd6c582807221293c21dc8b899
Author: Xing Loong <[email protected]>
Subject: tee: add MbedTEE driver

This commit adds a new Linux TEE driver for communicating with the MbedTEE OS. It supports ARM TrustZone and RISC-V IMSIC platforms, utilizing a shared-memory RPC protocol to interact with the Trusted Execution Environment.

drivers/tee/mbedtee/core.c static void mbedtee_release(struct tee_context *ctx) · line 91

Quoted 13 lines
drivers/tee/mbedtee/core.c +0 -0
@@ -83,10 +83,11 @@ static void mbedtee_release(struct tee_context *ctx)
 
 	if (d->is_supp_ctx) {
 		mbedtee_supp_release(&mbedtee->supp, ctx);
 	} else {
 		list_for_each_entry_safe(s, n, &d->sess_list, list_node)
 			mbedtee_close_session(ctx, s->session_id);
 	}
 
 	kfree(d);

[Severity: High]
Does this mutually exclusive block skip session cleanup entirely if the context was acting as a supplicant? It appears a userspace application could open a session, act as a supplicant, and close the file descriptor, resulting in leaked mbedtee_session objects since the else branch is completely bypassed.

[Severity: Medium]
If mbedtee_close_session fails (for example, due to an RPC allocation failure) and leaves a session in the list, does freeing the context data via kfree permanently lose the pointers to those session objects?

drivers/tee/mbedtee/core.c static void mbedtee_remove(struct platform_device *pdev) · line 201

Quoted 8 lines

[ ... ]

drivers/tee/mbedtee/core.c +0 -0
@@ -195,12 +196,10 @@ static void mbedtee_remove(struct platform_device *pdev)
 	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]
Does xa_destroy only free the internal XArray nodes? If there are any mbedtee_rpc_call structures left behind by interrupted calls when the module is removed, it seems this might cause a kernel memory leak.

drivers/tee/mbedtee/rpc_callee.c static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee, · line 232

Quoted 13 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -224,10 +224,9 @@ static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee,
 skip:
 	if (cmd.waiter_id != 0 && !rpc_queue_complete_only(ctx, cmd.waiter_id))
 		return NULL;
 
 	/* Bad or unhandled header: consume it to keep the ring moving */
 	t2r_ring_advance(ctx, sizeof(cmd));
 
 	return NULL;
 }

[Severity: High]
If an async RPC with an inline payload reaches this skip path, does advancing only the header size abandon the payload bytes in the ring? It looks like the next iteration might mistakenly parse those remaining payload bytes as a new command header, corrupting the ring buffer permanently.

drivers/tee/mbedtee/rpc_callee.c static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee, · line 251

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -245,10 +244,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;
 
 		t2r_ring_advance(ctx, sizeof(cmd));

[Severity: High]
If this transient GFP_ATOMIC memory allocation fails, returning NULL without advancing the ring or queuing a retry mechanism seems to leave the ring permanently stalled. Is there a way for the TEE-to-REE channel to recover from this condition?

Quoted 4 lines

[ ... ]

drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -288,14 +286,13 @@ static void rpc_drain_ring(struct mbedtee_device *mbedtee)
 	while (READ_ONCE(ctx->t2r_ring_rd) !=
 	    /* Pair with producer store-release after ring write. */

drivers/tee/mbedtee/rpc_callee.c static void rpc_drain_ring(struct mbedtee_device *mbedtee) · line 297

Quoted 10 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
 	    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);
 		} else {
 			INIT_WORK(&c->work, rpc_routine);

[Severity: High]
Breaking the loop here on a NULL return from rpc_pick_next due to an allocation failure appears to abandon ring processing entirely. Will this leave the TEE waiting for completion indefinitely?

[Severity: High]
For async requests (waiter_id == 0), the handler is called inline while holding the ctx->ring_lock spinlock with interrupts disabled. If the TEE sends an async supplicant request, does mbedtee_supp_handler end up sleeping on wait_for_completion in atomic context, leading to a kernel panic?

drivers/tee/mbedtee/rpc_callee.c static void rpc_routine(struct work_struct *work) · line 331

Quoted 9 lines

[ ... ]

drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -328,9 +325,8 @@ 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.
 	 */
 	while (mbedtee_rpc_fastcall(c->mbedtee, MBEDTEE_RPC_COMPLETE_TEE,
 				    (unsigned long)c->waiter_id, 0, 0) == -ENOSPC)
 		cond_resched();

[Severity: High]
Could mbedtee_rpc_fastcall return -ENOMEM instead of -ENOSPC on RISC-V? If so, this loop exits immediately and drops the COMPLETE_TEE notification, which might leave the TEE thread permanently blocked.

drivers/tee/mbedtee/rpc_callee.c int mbedtee_rpc_init(struct mbedtee_device *mbedtee) · line 422

Quoted 9 lines

[ ... ]

drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -420,10 +416,9 @@ int mbedtee_rpc_init(struct mbedtee_device *mbedtee)
 		memunmap(ctx->t2r_ring);
 		ctx->t2r_ring = NULL;
 		return -EINVAL;
 	}
 	ctx->t2r_ring_sz = resource_size(&res) - sizeof(struct rpc_ringbuf);
 	/* Observe latest producer index only after ring metadata is visible. */
 	WRITE_ONCE(ctx->t2r_ring_rd, smp_load_acquire(&ctx->t2r_ring->rd));

[Severity: High]
Does reading the initial consumer index directly from unvalidated shared memory allow a compromised TEE to provide out-of-bounds indices? This might lead to out-of-bounds reads or writes in kernel memory during ring buffer copies.

drivers/tee/mbedtee/rpc_caller.c int mbedtee_rpc_call_alloc(struct mbedtee_device *mbedtee, · line 44

Quoted 12 lines
drivers/tee/mbedtee/rpc_caller.c +0 -0
@@ -37,10 +37,9 @@ int mbedtee_rpc_call_alloc(struct mbedtee_device *mbedtee,
 
 	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);
 	if (ret != 0) {
 		kfree(rcall);

[Severity: High]
The XArray for rpc_calls is initialized with XA_FLAGS_ALLOC1 but lacks XA_FLAGS_LOCK_IRQ. When xa_insert_irq is called here with GFP_KERNEL, it disables local interrupts and might attempt to sleep during memory allocation if the array needs to expand. Could this trigger a sleep-in-atomic panic?

drivers/tee/mbedtee/rpc_caller.c void mbedtee_rpc_call_free(struct mbedtee_device *mbedtee, · line 65

Quoted 11 lines

[ ... ]

drivers/tee/mbedtee/rpc_caller.c +0 -0
@@ -58,10 +57,9 @@ void mbedtee_rpc_call_free(struct mbedtee_device *mbedtee,
 
 	/*
 	 * If interrupted/completed, complete_call owns this allocation
 	 * and will xa_erase + kfree when the TEE eventually finishes.
 	 */
 	if (READ_ONCE(call->state) == MBEDTEE_RPC_CALL_INTERRUPTED)
 		return;
 
 	xa_erase_irq(&mbedtee->rpc_calls, rpc_index);

[Severity: High]
If ownership is transferred to the background completion handler upon an interrupted wait, could the background handler free the call object concurrently while the caller is still accessing it?

drivers/tee/mbedtee/rpc_caller.c int mbedtee_invoke_func(struct tee_context *ctx, · line 387

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/rpc_caller.c +0 -0
@@ -382,10 +380,9 @@ int mbedtee_invoke_func(struct tee_context *ctx,
 
 	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);
 
 	if (ret != 0) {
 		arg->ret = TEEC_ERROR_COMMUNICATION;

[Severity: High]
If mbedtee_rpc_yieldcall was interrupted and transferred ownership of the call object to the background handler, is reading call->rpc.ret here susceptible to a use-after-free?

drivers/tee/mbedtee/rpc_caller.c int mbedtee_close_session(struct tee_context *ctx, u32 session) · line 421

Quoted 11 lines

[ ... ]

drivers/tee/mbedtee/rpc_caller.c +0 -0
@@ -416,10 +413,9 @@ int mbedtee_close_session(struct tee_context *ctx, u32 session)
 	ret = mbedtee_rpc_call_alloc(mbedtee, sizeof(*rp), &call);
 	if (ret != 0) {
 		/*
 		 * RPC allocation failed but the session is still alive on
 		 * the TEE side. Leave it in the session list so a future
 		 * close attempt can retry; warn so the condition is visible.
 		 */
 		mutex_unlock(&ctxdata->mutex);
 		return ret;

[Severity: Medium]
If this returns early leaving the session in the list, the caller (mbedtee_release) ignores the error and proceeds to kfree the context data. Does this permanently leak the mbedtee_session objects?

drivers/tee/mbedtee/rpc_caller_riscv.c int mbedtee_r2t_init(struct mbedtee_device *mbedtee) · line 175

Quoted 10 lines
drivers/tee/mbedtee/rpc_caller_riscv.c +0 -0
@@ -170,9 +170,8 @@ int mbedtee_r2t_init(struct mbedtee_device *mbedtee)
 	}
 	ctx->ring_sz = resource_size(&res) - sizeof(struct rpc_ringbuf);
 	/* Read initial writer index with acquire for coherent ring bootstrap. */
 	WRITE_ONCE(ctx->ring_wr, smp_load_acquire(&ctx->ring->wr));
 
 	dev_dbg(mbedtee->dev, "rpc-r2t-ring %pa\n", &res.start);

[Severity: High]
Similar to the T2R ring, does accepting this unvalidated write index from the TEE-controlled shared memory pose an out-of-bounds memory access risk?

drivers/tee/mbedtee/shm_pool.c static void pool_op_free(struct tee_shm_pool *pool, · line 70

Quoted 13 lines
drivers/tee/mbedtee/shm_pool.c +0 -0
@@ -62,10 +62,9 @@ static void pool_op_free(struct tee_shm_pool *pool,
 	if (!(shm->flags & TEE_SHM_PRIV)) {
 		struct mbedtee_device *mbedtee = tee_get_drvdata(shm->ctx->teedev);
 
 		if (mbedtee->yield && shm->sec_world_id)
 			mbedtee_shm_unregister(shm->ctx, shm);
 	}
 
 	free_pages((unsigned long)shm->kaddr, get_order(shm->size));
 	shm->kaddr = NULL;

[Severity: High]
If mbedtee_shm_unregister fails to revoke the TEE's access (for instance, if the RPC ring is full), the memory pages are still unconditionally freed to the Linux allocator. Could this allow the TEE to maintain access to memory that gets reused for other kernel processes?

drivers/tee/mbedtee/supp.c void mbedtee_supp_release(struct mbedtee_supp *supp, struct tee_context *ctx) · line 79

Quoted 11 lines
drivers/tee/mbedtee/supp.c +0 -0
@@ -73,10 +73,9 @@ void mbedtee_supp_release(struct mbedtee_supp *supp, struct tee_context *ctx)
 	 * interfere with each other.
 	 */
 	list_for_each_entry_safe(r, n, &supp->reqs, node) {
 		if (r->ctx && r->ctx != ctx)
 			continue;
 		mbedtee_supp_complete_req(r);
 	}

[Severity: High]
Unassigned global requests waiting in supp->reqs will have r->ctx set to NULL. This makes the condition evaluate to false. Does this mean that whenever any single supplicant context is closed, it incorrectly aborts all unassigned global requests across the system?

drivers/tee/mbedtee/supp.c void mbedtee_supp_handler(struct mbedtee_device *mbedtee, · line 162

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/supp.c +0 -0
@@ -156,10 +155,9 @@ void mbedtee_supp_handler(struct mbedtee_device *mbedtee,
 
 	/* Wake up the supplicant daemon to handle this request. */
 	complete(&supp->reqs_c);
 
 	wait_for_completion(&req->c);
 
 	kfree(req);
 }

[Severity: High]
When the request finishes, req->ret contains the completion status. Is this return code ever copied back into the wire-format cmd->ret in shared memory before freeing the request, or is the TEE receiving uninitialized data?

drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx, · line 211

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/supp.c +0 -0
@@ -206,10 +204,9 @@ int mbedtee_supp_recv(struct tee_context *ctx,
 	if (ret != 0)
 		return ret;
 
 	shm = tee_shm_get_from_id(ctx, shm->id);
 	tee_shm_put(param->u.memref.shm);
 	if (IS_ERR(shm))
 		return PTR_ERR(shm);
 	param->u.memref.shm = shm;

[Severity: High]
If tee_shm_get_from_id fails, the function returns the error without clearing param->u.memref.shm. Will the TEE core caller's cleanup path call tee_shm_put again on this same pointer, causing a double free?

drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx, · line 221

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/supp.c +0 -0
@@ -217,10 +214,9 @@ int mbedtee_supp_recv(struct tee_context *ctx,
 	d->is_supp_ctx = true;
 
 	while (true) {
 		ret = supp_pop_req(supp, &req);
 		if (ret != 0)
 			goto err;
 		if (req)
 			break;

[Severity: Medium]
If a request is already queued, this bypasses wait_for_completion_interruptible entirely. Could this leave the internal completion counter un-decremented, causing phantom wakeups to accumulate and creating a high CPU spinning loop later when the queue is empty?

drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx, · line 241

Quoted 10 lines

[ ... ]

drivers/tee/mbedtee/supp.c +0 -0
@@ -238,10 +234,9 @@ int mbedtee_supp_recv(struct tee_context *ctx,
 		goto err;
 	}
 
 	*func = req->func;
 	req->shm = shm;
 
 	memcpy(shm->kaddr, req->data, req->size);
 	param->u.memref.size = req->size;

[Severity: High]
This assigns the shared memory pointer to req->shm without bumping its reference count. If the TEE core drops its reference when returning to userspace, might this memory be freed prematurely?

drivers/tee/mbedtee/supp.c int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, · line 298

Quoted 11 lines

[ ... ]

drivers/tee/mbedtee/supp.c +0 -0
@@ -295,11 +290,10 @@ int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
 
 	if (param->u.memref.size > req->size) {
 		req->ret = TEEC_ERROR_COMMUNICATION;
 	} else {
 		req->ret = ret;
 		memcpy(req->data, req->shm->kaddr, param->u.memref.size);
 	}
 	if (req->shm)
 		tee_shm_put(req->shm);

[Severity: High]
Following up on the previous question, if req->shm was freed because a reference was not acquired in mbedtee_supp_recv, does this memcpy and subsequent tee_shm_put result in a use-after-free and a double free?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#9 lore raw

v2 cover tee: add MbedTEE driver

In reply to Xing Loong

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 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 containers, reg addresses, riscv wrapper); the ARM example now uses bare interrupts (matching arm,sbsa-gwdt.yaml precedent) with only phandle-required stubs (imsic, t2r-ring, r2t-ring)
- 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        | 132 ++++
 .../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, 3190 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
author
#10 lore raw

PATCH v2 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
index 28784d6..fd032a3 100644
@@ -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
author
#11 lore raw

PATCH v2 2/3 dt-bindings: firmware: add mbedtee,tee binding

In reply to 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).

---
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 containers, reg addresses, riscv wrapper); the ARM example now uses bare interrupts (matching arm,sbsa-gwdt.yaml precedent) with only phandle-required stubs (imsic, t2r-ring, r2t-ring)
- Add maxItems: 1 constraint to interrupts property (Sashiko AI review)

---

 .../bindings/firmware/mbedtee,tee.yaml        | 132 ++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +132 -0
new file mode 100644
index 0000000..62f2522
@@ -0,0 +1,132 @@
+# 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 Trusted Execution Environment
+
+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.
+
+  The REE and TEE CPUs sharing the RPC memory must be in a
+  hardware-coherent domain (same CPU cluster, coherent caches).
+
+  Two or three reserved-memory regions are required:
+
+    t2r-ring  ring buffer for TEE-to-REE notifications (all platforms)
+    t2r-shm   shared memory for TEE-to-REE RPC payloads (all platforms)
+    r2t-ring  ring buffer for REE-to-TEE command submissions (RISC-V only)
+
+  On ARM/AArch64 the TEE is invoked via SMC and signals the REE via
+  a GIC SPI edge interrupt.
+
+  On RISC-V the TEE signals the REE via IMSIC MSI; the REE submits
+  commands via r2t-ring that the TEE polls. No REE-to-TEE interrupt
+  is used. No SBI ecall is involved.
+
+properties:
+  compatible:
+    const: mbedtee,tee
+
+  interrupts:
+    maxItems: 1
+
+  msi-parent:
+    maxItems: 1
+
+  memory-region:
+    minItems: 2
+    maxItems: 3
+    # Minimum across platforms (ARM: 2, RISC-V: 3), constrained by allOf below
+
+  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:
+      required:
+        - interrupts
+      properties:
+        msi-parent: false
+        memory-region:
+          maxItems: 2
+        memory-region-names:
+          items:
+            - const: t2r-ring
+            - const: t2r-shm
+    else:
+      required:
+        - msi-parent
+      properties:
+        memory-region:
+          minItems: 3
+        memory-region-names:
+          items:
+            - const: t2r-ring
+            - const: t2r-shm
+            - const: r2t-ring
+
+additionalProperties: false
+
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    firmware {
+      mbedtee {
+        compatible = "mbedtee,tee";
+        interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+        memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
+        memory-region-names = "t2r-ring", "t2r-shm";
+      };
+    };
+
+    /* memory-region phandle targets */
+    mbedtee_t2r_ring: t2r-ring {};
+    mbedtee_t2r_shm: t2r-shm {};
+
+  - |
+    /* RISC-V IMSIC (ring-buffer polling REE->TEE, MSI TEE->REE) */
+
+    firmware {
+      mbedtee {
+        compatible = "mbedtee,tee";
+        msi-parent = <&imsic>;
+        memory-region = <&rv_t2r_ring>, <&rv_t2r_shm>,
+                        <&rv_r2t_ring>;
+        memory-region-names = "t2r-ring", "t2r-shm",
+                              "r2t-ring";
+      };
+    };
+
+    /* msi-parent phandle target */
+    imsic: imsic {
+      msi-controller;
+      #msi-cells = <0>;
+    };
+
+    /* memory-region phandle targets */
+    rv_t2r_ring: t2r-ring {};
+    rv_t2r_shm: t2r-shm {};
+    rv_r2t_ring: r2t-ring {};
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH v2 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 `memory-region-names` property incorrectly defines `items` as an object instead of an array of…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The memory-region-names property incorrectly defines items as an object instead of an array of schemas.
- [Low] The provided DT binding examples contain malformed root-level nodes that will cause dt_binding_check to fail. --

commit 5f6618bd551455752fa4251daab79dbc393c89d6
Author: Xing Loong <[email protected]>

dt-bindings: firmware: add mbedtee,tee binding

This commit introduces a new device tree binding for MbedTEE, a Trusted Execution Environment for embedded systems. It supports both ARM/AArch64 using SMC and RISC-V using IMSIC configurations, communicating via shared memory ring buffers.

Quoted 4 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +0 -0
new file mode 100644

[ ... ]

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 55

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 items definition violate the dt-schema constraints for properties ending in -names?

dt-schema typically requires an array of schemas for items to enforce strict sequence order, rather than an enum object. This could cause make dt_binding_check to fail against the core meta-schema.

[ ... ]

Quoted 7 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +7 -0
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    firmware {
+      mbedtee {

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 107

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10 -0
+        compatible = "mbedtee,tee";
+        interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+        memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
+        memory-region-names = "t2r-ring", "t2r-shm";
+      };
+    };
+
+    /* memory-region phandle targets */
+    mbedtee_t2r_ring: t2r-ring {};
+    mbedtee_t2r_shm: t2r-shm {};

[Severity: Low]
Will placing these memory region nodes at the root level instead of inside a reserved-memory node cause dt_binding_check to reject the example?

The root node schema typically rejects arbitrary children lacking compatible strings.

[ ... ]

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 127

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +5 -0
+    /* msi-parent phandle target */
+    imsic: imsic {
+      msi-controller;
+      #msi-cells = <0>;
+    };

[Severity: Low]
Can this dummy node cause schema validation failures?

It lacks a compatible string and is placed directly at the root level, which dt-schema usually flags as malformed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#13 lore raw

Re: [PATCH v2 2/3] dt-bindings: firmware: add mbedtee,tee binding

In reply to Xing Loong

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 35

Quoted 8 lines · Xing Loong
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +8 -0
+
+  On RISC-V the TEE signals the REE via IMSIC MSI; the REE submits
+  commands via r2t-ring that the TEE polls. No REE-to-TEE interrupt
+  is used. No SBI ecall is involved.
+
+properties:
+  compatible:
+    const: mbedtee,tee

No, you need to respond to feedback instead of sending the same code called v2. You received comments for a reason. Sending v2 now hides all this comments and people might think discussion is resolved. This is very bad practice.

Also,
Do not attach (thread) your patchsets to some other threads (unrelated or older versions). This buries them deep in the mailbox and might interfere with applying entire sets. See also:
https://elixir.bootlin.com/linux/v6.16-rc2/source/Documentation/process/submitting-patches.rst#L830

...

Quoted 8 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +8 -0
+
+examples:
+  - |
+    /* ARM TrustZone (SMC) */
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    firmware {
+      mbedtee {

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 107

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10 -0
+        compatible = "mbedtee,tee";
+        interrupts = <GIC_SPI 72 IRQ_TYPE_EDGE_RISING>;
+        memory-region = <&mbedtee_t2r_ring>, <&mbedtee_t2r_shm>;
+        memory-region-names = "t2r-ring", "t2r-shm";
+      };
+    };
+
+    /* memory-region phandle targets */
+    mbedtee_t2r_ring: t2r-ring {};
+    mbedtee_t2r_shm: t2r-shm {};

I asked you to drop all these irrelevant entries.

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10 -0
+
+  - |
+    /* RISC-V IMSIC (ring-buffer polling REE->TEE, MSI TEE->REE) */
+
+    firmware {
+      mbedtee {
+        compatible = "mbedtee,tee";
+        msi-parent = <&imsic>;
+        memory-region = <&rv_t2r_ring>, <&rv_t2r_shm>,
+                        <&rv_r2t_ring>;

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 127

Quoted 10 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +10 -0
+        memory-region-names = "t2r-ring", "t2r-shm",
+                              "r2t-ring";
+      };
+    };
+
+    /* msi-parent phandle target */
+    imsic: imsic {
+      msi-controller;
+      #msi-cells = <0>;
+    };

Also drop.

Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml line 132

Quoted 5 lines
Documentation/devicetree/bindings/firmware/mbedtee,tee.yaml new file +5 -0
+
+    /* memory-region phandle targets */
+    rv_t2r_ring: t2r-ring {};
+    rv_t2r_shm: t2r-shm {};
+    rv_r2t_ring: r2t-ring {};

Also not relevant.

I will not continue the review. Please go back to v1 and continue the discussion.

I consider this version NAKed.

Best regards,
Krzysztof

author
#14 lore raw

PATCH v2 3/3 tee: add MbedTEE driver

In reply to Xing Loong

This 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 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
index 10c3cec..6415855 100644
@@ -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
index c8d4b91..344b82b 100644
@@ -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
index 98c3ad0..3dc3439 100644
@@ -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
index 3239b91..33117ea 100644
@@ -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
new file mode 100644
index 0000000..b1fa38a
@@ -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
new file mode 100644
index 0000000..309eb6d
@@ -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
new file mode 100644
index 0000000..4510676
@@ -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

include/uapi/linux/tee.h +1 -0
index 5203977..2d30296 100644

Lines of this file are not shown. View the 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.

#15 lore raw
Thank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider: - [Critical] Malicious or malformed asynchronous supplicant RPCs will execute sleeping operations in a…

Thank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider:
- [Critical] Malicious or malformed asynchronous supplicant RPCs will execute sleeping operations in a hard IRQ handler, causing deadlocks.
- [Critical] The req->shm reference is under-retained in mbedtee_supp_recv(), leading to a Use-After-Free and double free.
- [Critical] Unvalidated cmd.size for rejected async RPCs causes the consumer index to exceed ring buffer bounds, leading to massive memory corruption.
- [Critical] Type confusion privilege escalation via TEE-controlled parameter attributes.
- [High] Unhandled MSI migration on RISC-V causes lost TEE interrupts and hanging RPCs.
- [High] Physical address of the RPC command is silently truncated on 32-bit ARM with LPAE.
- [High] The tee-supplicant's return code is lost and never propagated back to the TEE.
- [High] Module removal deadlocks because the T2R interrupt is torn down before aborting supplicant requests.
- [High] Initial ring buffer consumer and producer pointers are read from untrusted shared memory without bounds checking.
- [High] Unaligned memory access in mbedtee_ree_time can trigger alignment faults and kernel panics.
- [Medium] The required buffer size is not returned to the tee-supplicant when -EOVERFLOW occurs.
- [Medium] Leaking TEE sessions if a context acts as both a client and a supplicant.
- [Medium] Encountering an invalid or unhandled RPC command in the TEE-to-REE ring causes a state machine stall.
--

commit e35eb394dcd64d2ef130e20e3614fccbb5021a07
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 over architecture-specific transports.

drivers/tee/mbedtee/core.c static void mbedtee_release(struct tee_context *ctx)

Quoted 11 lines
drivers/tee/mbedtee/core.c +0 -0
@@ -80,5 +80,6 @@ static void mbedtee_release(struct tee_context *ctx)
 
 	if (d->is_supp_ctx) {
 		mbedtee_supp_release(&mbedtee->supp, ctx);
 	} else {
 		list_for_each_entry_safe(s, n, &d->sess_list, list_node)
 			mbedtee_close_session(ctx, s->session_id);
 	}

[Severity: Medium]
Does this skip session cleanup for mixed-use contexts? If a userspace process opens standard TEE sessions and then calls TEE_IOC_SUPPL_RECV, d->is_supp_ctx becomes true. When the file descriptor is closed, the mutually exclusive else branch is skipped, which appears to leak the sessions in d->sess_list and leaves them permanently open in the TEE.

drivers/tee/mbedtee/rpc_callee.c static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee,

Quoted 12 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -245,6 +245,5 @@ static struct rpc_work *rpc_pick_next(struct mbedtee_device *mbedtee,
 skip:
 	if (cmd.waiter_id != 0 && !rpc_queue_complete_only(ctx, cmd.waiter_id))
 		return NULL;
 
 	t2r_ring_advance(ctx, sizeof(cmd) + (cmd.waiter_id ? 0 : cmd.size));
 
 	return NULL;
 }

[Severity: Critical]
Does this blind advance allow a malicious TEE to corrupt the ring read pointer? Because cmd.size is not validated on this error path, an arbitrarily large value could cause t2r_ring_advance() to jump far outside the actual ring bounds, leading to integer underflows and out-of-bounds memory accesses in subsequent operations.

[Severity: Medium]
Does returning NULL here cause the rpc_drain_ring() loop to break entirely? If an unrecognized command is skipped, returning NULL will unconditionally stop the processing loop, leaving subsequent valid commands stranded until another interrupt fires, which could stall the protocol.

drivers/tee/mbedtee/rpc_callee.c static void rpc_drain_ring(struct mbedtee_device *mbedtee)

Quoted 8 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -267,7 +267,6 @@ static void rpc_drain_ring(struct mbedtee_device *mbedtee)
 		c = rpc_pick_next(mbedtee, ctx, &rw);
 		if (!c)
 			break;
 
 		if (c->waiter_id == 0) {
 			c->func(mbedtee, c->data, c->size);
 		} else {

[Severity: Critical]
Can this sequence cause a deadlock in hard IRQ context? The rpc_drain_ring() function runs in mbedtee_rpc_irq_handler() with the ctx->ring_lock spinlock held. If a TEE sends a synchronous supplicant RPC like MBEDTEE_RPC_REEFS with waiter_id = 0, this inline execution will route to mbedtee_supp_handler(), which performs a GFP_KERNEL allocation and calls wait_for_completion(). Sleeping in a hard IRQ handler while holding a spinlock will crash the system.

drivers/tee/mbedtee/rpc_callee.c static void mbedtee_ree_time(struct mbedtee_device *mbedtee,

Quoted 10 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -400,6 +400,5 @@ static void mbedtee_ree_time(struct mbedtee_device *mbedtee,
 			     void *data, size_t size)
 {
 	struct timespec64 *ts = data;
 
 	if (size < sizeof(*ts))
 		return;
 
 	ktime_get_real_ts64(ts);
 }

[Severity: High]
Does this cause unaligned memory accesses? The data pointer points directly into shared memory at an offset controlled by the TEE. Writing 64-bit fields into an unaligned timespec64 structure can trigger alignment faults and kernel panics on architectures with strict alignment requirements.

drivers/tee/mbedtee/rpc_callee.c int mbedtee_rpc_init(struct mbedtee_device *mbedtee)

Quoted 6 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -486,4 +486,5 @@ int mbedtee_rpc_init(struct mbedtee_device *mbedtee)
 		return -EINVAL;
 	}
 	ctx->t2r_ring_sz = resource_size(&res) - sizeof(struct rpc_ringbuf);
 	/* Observe latest producer index only after ring metadata is visible. */
 	WRITE_ONCE(ctx->t2r_ring_rd, smp_load_acquire(&ctx->t2r_ring->rd));

[Severity: High]
Are we missing a bounds check on the initial read pointer? Loading this directly from TEE-controlled shared memory without validating that it is within ctx->t2r_ring_sz could allow a compromised TEE to instantly trigger out-of-bounds accesses during driver initialization.

drivers/tee/mbedtee/rpc_callee.c void mbedtee_rpc_uninit(struct mbedtee_device *mbedtee)

Quoted 13 lines
drivers/tee/mbedtee/rpc_callee.c +0 -0
@@ -585,5 +585,6 @@ void mbedtee_rpc_uninit(struct mbedtee_device *mbedtee)
 	mutex_unlock(&mbedtee_cpuhp_mutex);
 
 	if (mbedtee->rpc_ops)
 		mbedtee->rpc_ops->uninit(mbedtee);
 
 	/*
 	 * Work queued before IRQ teardown may be blocked in the supplicant
 	 * path. Abort those requests and reject later ones before draining the
 	 * workqueue. R2T must stay alive until destroy_workqueue() returns
 	 * because rpc_routine() sends COMPLETE_TEE replies over R2T.
 	 */
 	mbedtee_supp_abort_all(&mbedtee->supp);

[Severity: High]
Will this sequence deadlock during module removal? Unregistering the interrupt via uninit() happens before mbedtee_supp_abort_all(). If aborting requests triggers shm unregistration, that process makes a synchronous RPC call to the TEE. Since the interrupt is already torn down, the completion response will never arrive, causing the removal thread to block indefinitely.

drivers/tee/mbedtee/rpc_callee_riscv.c static void mbedtee_rpc_write_msi_msg(struct msi_desc *desc_msi,

Quoted 11 lines
drivers/tee/mbedtee/rpc_callee_riscv.c +0 -0
@@ -34,6 +34,5 @@ static void mbedtee_rpc_write_msi_msg(struct msi_desc *desc_msi,
 				      struct msi_msg *msg)
 {
 	struct mbedtee_device *mbedtee = dev_get_drvdata(desc_msi->dev);
 
 	if (mbedtee)
 		mbedtee->transport.rpc_msi_msg = *msg;
 }

[Severity: High]
Does this fail to propagate the new IMSIC target to the TEE? When the IRQ subsystem changes affinity, this callback updates the local message cache, but it does not write the new callee_imsic_id and callee_hartid back to the shared ring buffer header. The TEE will continue sending interrupts to the stale target, leading to lost notifications and hanging RPCs.

Quoted 4 lines

drivers/tee/mbedtee/rpc_caller.c static int mbedtee_param_decode(struct tee_param *params,

Quoted 10 lines
drivers/tee/mbedtee/rpc_caller.c +0 -0
@@ -182,5 +182,6 @@ static int mbedtee_param_decode(struct tee_param *params,
 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
 			p->u.memref.size = rtp->memref.size;
 			break;
 		default:
 			return -EINVAL;
 		}
 		p->attr = attr;
 	}
 	return 0;
 }

[Severity: Critical]
Could this lead to type confusion and privilege escalation? Blindly overwriting p->attr with the TEE-supplied attribute without verifying it matches the original request allows a malicious TEE to change a VALUE parameter to a MEMREF parameter. The generic subsystem will then interpret the user-controlled value fields as a kernel pointer during tee_shm_put(), allowing arbitrary kernel memory dereferences.

drivers/tee/mbedtee/rpc_caller_arm.c long mbedtee_rpc_yieldcall(struct mbedtee_device *mbedtee, · line 34

Quoted 9 lines
drivers/tee/mbedtee/rpc_caller_arm.c +0 -0
@@ -30,5 +30,6 @@ long mbedtee_rpc_yieldcall(struct mbedtee_device *mbedtee,
 	call->rpc.id = fn;
 
 	arm_smccc_smc(fn, call->rpc_phys,
 		      0, 0, 0, 0, 0, 0, &res);
 	ret = res.a0;

[Severity: High]
Does this silently truncate the physical address on 32-bit ARM with LPAE? The call->rpc_phys field is a 64-bit phys_addr_t, but arm_smccc_smc() casts its arguments to a 32-bit unsigned long. If the memory resides above 4GB, the upper bits will be lost, giving the TEE an invalid address and causing memory corruption or crashes.

drivers/tee/mbedtee/rpc_caller_riscv.c int mbedtee_r2t_init(struct mbedtee_device *mbedtee)

Quoted 9 lines
drivers/tee/mbedtee/rpc_caller_riscv.c +0 -0
@@ -166,4 +166,5 @@ int mbedtee_r2t_init(struct mbedtee_device *mbedtee)
 		return -ENOMEM;
 	}
 	ctx->ring_sz = resource_size(&res) - sizeof(struct rpc_ringbuf);
 	/* Read initial writer index with acquire for coherent ring bootstrap. */
 	WRITE_ONCE(ctx->ring_wr, smp_load_acquire(&ctx->ring->wr));

[Severity: High]
Is it safe to load the initial write pointer from shared memory without bounds validation? A compromised TEE could set this to an out-of-bounds value prior to driver initialization, leading to immediate out-of-bounds memory accesses.

drivers/tee/mbedtee/supp.c void mbedtee_supp_handler(struct mbedtee_device *mbedtee,

Quoted 11 lines
drivers/tee/mbedtee/supp.c +0 -0
@@ -153,6 +153,5 @@ void mbedtee_supp_handler(struct mbedtee_device *mbedtee,
 	/* Wake up the supplicant daemon to handle this request. */
 	complete(&supp->reqs_c);
 
 	wait_for_completion(&req->c);
 
 	kfree(req);
 }

[Severity: High]
Are we missing a step to propagate the supplicant's return code back to the TEE? After wait_for_completion() returns, req->ret holds the execution status, but this value is never written into the shared memory cmd->ret before req is freed, causing the TEE to lose the actual result of the operation.

drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx, · line 207

Quoted 6 lines
drivers/tee/mbedtee/supp.c +0 -0
@@ -203,6 +203,5 @@ int mbedtee_supp_recv(struct tee_context *ctx,
 		return ret;
 
 	shm = tee_shm_get_from_id(ctx, shm->id);
 	tee_shm_put(param->u.memref.shm);
 	if (IS_ERR(shm)) {

[Severity: Critical]
Does this pattern lead to a use-after-free and double free? The code acquires a generic SHM reference and immediately drops the previous reference. This results in a net zero reference count change, meaning the driver does not truly own the reference it assigns to req->shm. When the IOCTL finishes, the generic layer will put the reference again, leaving req->shm dangling.

drivers/tee/mbedtee/supp.c int mbedtee_supp_recv(struct tee_context *ctx,

Quoted 12 lines
drivers/tee/mbedtee/supp.c +0 -0
@@ -231,9 +231,8 @@ int mbedtee_supp_recv(struct tee_context *ctx,
 	req->ctx = ctx;
 
 	if (req->size > param->u.memref.size) {
 		/* 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;
 	}

[Severity: Medium]
Is the required buffer size missing from the -EOVERFLOW response? If the user-provided buffer is too small, the driver returns -EOVERFLOW but fails to update param->u.memref.size with req->size. The userspace supplicant daemon relies on this updated size to know how much memory to allocate for a retry.

drivers/tee/mbedtee/supp.c int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,

Quoted 7 lines
drivers/tee/mbedtee/supp.c +0 -0
@@ -298,5 +298,6 @@ int mbedtee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
 		req->ret = ret;
 		memcpy(req->data, req->shm->kaddr, param->u.memref.size);
 	}
 	if (req->shm)
 		tee_shm_put(req->shm);
 	req->shm = NULL;

[Severity: Critical]
Because the reference was not correctly incremented in mbedtee_supp_recv(), calling tee_shm_put(req->shm) here drops a reference that belongs to the userspace process, completing the double free.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3