Skip to content

Commit 0607ed4

Browse files
Aleksandr Anisimovjforissier
authored andcommitted
plugins: example of using the tee-supplicant plugin framework
TEE clients can add to the tee-supplicant own plugins. This feature makes the supplicant a bit more flexible in terms of providing services. Any external TEE services can be designed as a tee-supplicant plugin. User TAs can interact with the plugins using libutee and the OP-TEE kernel code can use a special plugin PRC for it. This patch adds example of using the plugin framework. 'syslog' plugin is used as an example. It can write log messages from OP-TEE (TAs or kernel) to system log. Also patch adds a new TA, which interacts with the 'syslog' plugin with the help of 'tee_invoke_supp_plugin()'. This TA increments a value and prints some strings to the syslog. Also patch adds a host CA, which calls the TA 'TA_PING_CNT' times. If the example works successfully, we can find the following strings in the log file (for qemu it's '/var/log/messages' file): ``` 21:18:05 buildroot daemon.info tee-supplicant[92]: Hello, plugin! value = 0x0 21:18:07 buildroot daemon.info tee-supplicant[92]: Hello, plugin! value = 0x1 21:18:09 buildroot daemon.info tee-supplicant[92]: Hello, plugin! value = 0x2 21:18:11 buildroot daemon.info tee-supplicant[92]: Hello, plugin! value = 0x3 21:18:13 buildroot daemon.info tee-supplicant[92]: Hello, plugin! value = 0x4 ``` Signed-off-by: Aleksandr Anisimov <[email protected]> Reviewed-by: Jens Wiklander <[email protected]>
1 parent 9a755dc commit 0607ed4

File tree

15 files changed

+437
-0
lines changed

15 files changed

+437
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ random/host/optee_example_random
1818
aes/host/optee_example_aes
1919
hotp/host/optee_example_hotp
2020
secure_storage/host/optee_example_secure_storage
21+
plugins/host/optee_example_plugins

plugins/Android.mk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###################### optee-plugins ######################
2+
LOCAL_PATH := $(call my-dir)
3+
4+
OPTEE_CLIENT_EXPORT = $(LOCAL_PATH)/../../optee_client/out/export
5+
6+
include $(CLEAR_VARS)
7+
LOCAL_CFLAGS += -DANDROID_BUILD
8+
LOCAL_CFLAGS += -Wall
9+
10+
LOCAL_SRC_FILES += host/main.c
11+
12+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \
13+
$(OPTEE_CLIENT_EXPORT)/include \
14+
15+
LOCAL_SHARED_LIBRARIES := libteec
16+
LOCAL_MODULE := optee_example_plugins
17+
LOCAL_VENDOR_MODULE := true
18+
LOCAL_MODULE_TAGS := optional
19+
include $(BUILD_EXECUTABLE)
20+
21+
include $(LOCAL_PATH)/ta/Android.mk

plugins/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project (optee_example_plugins C)
2+
3+
set (SRC host/main.c)
4+
5+
add_executable (${PROJECT_NAME} ${SRC})
6+
7+
target_include_directories(${PROJECT_NAME}
8+
PRIVATE ta/include
9+
PRIVATE include)
10+
11+
target_compile_definitions (${PROJECT_NAME}
12+
PRIVATE -DBINARY_PREFIX="TEE-EXMPL"
13+
)
14+
15+
target_link_libraries (${PROJECT_NAME}
16+
PRIVATE teec)
17+
18+
install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
19+
20+
add_subdirectory (syslog)

plugins/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export V?=0
2+
3+
# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
4+
HOST_CROSS_COMPILE ?= $(CROSS_COMPILE)
5+
TA_CROSS_COMPILE ?= $(CROSS_COMPILE)
6+
7+
.PHONY: all
8+
all:
9+
$(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" --no-builtin-variables
10+
$(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" LDFLAGS=""
11+
$(MAKE) -C syslog CROSS_COMPILE="$(HOST_CROSS_COMPILE)"
12+
13+
.PHONY: clean
14+
clean:
15+
$(MAKE) -C host clean
16+
$(MAKE) -C ta clean
17+
$(MAKE) -C syslog clean

plugins/host/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CC ?= $(CROSS_COMPILE)gcc
2+
LD ?= $(CROSS_COMPILE)ld
3+
AR ?= $(CROSS_COMPILE)ar
4+
NM ?= $(CROSS_COMPILE)nm
5+
OBJCOPY ?= $(CROSS_COMPILE)objcopy
6+
OBJDUMP ?= $(CROSS_COMPILE)objdump
7+
READELF ?= $(CROSS_COMPILE)readelf
8+
9+
OBJS = main.o
10+
11+
CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include
12+
CFLAGS += -DBINARY_PREFIX=\"TEE-EXMPL\"
13+
14+
#Add/link other required libraries here
15+
LDADD += -lteec -L$(TEEC_EXPORT)/lib
16+
17+
BINARY = optee_example_plugins
18+
19+
.PHONY: all
20+
all: $(BINARY)
21+
22+
$(BINARY): $(OBJS)
23+
$(CC) -o $@ $< $(LDADD)
24+
25+
.PHONY: clean
26+
clean:
27+
rm -f $(OBJS) $(BINARY)
28+
29+
%.o: %.c
30+
$(CC) $(CFLAGS) -c $< -o $@

plugins/host/main.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
/*
3+
* Copyright (c) 2020, Open Mobile Platform LLC
4+
*/
5+
6+
#include <err.h>
7+
#include <inttypes.h>
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <stdbool.h>
11+
#include <unistd.h>
12+
13+
/* OP-TEE TEE client API (built by optee_client) */
14+
#include <tee_client_api.h>
15+
16+
/* For the UUID (found in the TA's h-file(s)) */
17+
#include <plugin_ta.h>
18+
19+
#define SLEEP_SEC 2
20+
#define TA_PING_CNT 5
21+
22+
int main(void)
23+
{
24+
int i = 0;
25+
TEEC_Result res = TEEC_SUCCESS;
26+
TEEC_Context ctx = { };
27+
TEEC_Session sess = { };
28+
TEEC_Operation op = { };
29+
TEEC_UUID uuid = PLUGIN_TA_UUID;
30+
uint32_t err_origin = 0;
31+
32+
/* Initialize a context connecting us to the TEE */
33+
res = TEEC_InitializeContext(NULL, &ctx);
34+
if (res != TEEC_SUCCESS)
35+
errx(1, "TEEC_InitializeContext failed with code %#" PRIx32,
36+
res);
37+
38+
/* Open a session to the "plugin" TA */
39+
res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
40+
NULL, &err_origin);
41+
if (res != TEEC_SUCCESS)
42+
errx(1, "TEEC_Opensession failed with code %#" PRIx32 "origin %#" PRIx32,
43+
res, err_origin);
44+
45+
/* Clear the TEEC_Operation struct */
46+
memset(&op, 0, sizeof(op));
47+
op.paramTypes =
48+
TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
49+
50+
/*
51+
* TA will refer to the syslog plugin to print some log messages to REE.
52+
*
53+
* See the plugin code in the optee-client.
54+
* See the log through 'journalctl'.
55+
*/
56+
57+
printf("Work logic: REE --> plugin TA --> syslog plugin in REE --> syslog\n");
58+
printf("See the log from TEE through 'journalctl'\n\n");
59+
60+
for (i = 0; i < TA_PING_CNT; ++i) {
61+
res = TEEC_InvokeCommand(&sess, PLUGIN_TA_PING, &op,
62+
&err_origin);
63+
64+
printf("Attempt #%d: TEEC_InvokeCommand() %s; res=%#" PRIx32 " orig=%#" PRIx32 "\n",
65+
i + 1, (res == TEEC_SUCCESS) ? "success" : "failed",
66+
res, err_origin);
67+
68+
sleep(SLEEP_SEC);
69+
}
70+
71+
/*
72+
* We're done with the TA, close the session and
73+
* destroy the context.
74+
*/
75+
76+
TEEC_CloseSession(&sess);
77+
TEEC_FinalizeContext(&ctx);
78+
79+
return 0;
80+
}

plugins/syslog/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project (96bcf744-4f72-4866-bf1d-8634fd9c65e5.plugin C)
2+
3+
set (CFG_TEE_PLUGIN_LOAD_PATH "/usr/lib/tee-supplicant/plugins/")
4+
set (CMAKE_SHARED_LIBRARY_PREFIX "")
5+
6+
add_library(${PROJECT_NAME} SHARED syslog_plugin.c)
7+
8+
install (TARGETS ${PROJECT_NAME} DESTINATION ${CFG_TEE_PLUGIN_LOAD_PATH})

plugins/syslog/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PLUGIN_UUID = 96bcf744-4f72-4866-bf1d-8634fd9c65e5
2+
3+
PLUGIN = $(PLUGIN_UUID).plugin
4+
PLUGIN_SRS = $(wildcard ./*.c)
5+
PLUGIN_OBJ = $(patsubst %.c, %.o, $(PLUGIN_SRS))
6+
PLUGIN_INCLUDES_DIR = $(CURDIR) $(TEEC_EXPORT)/include
7+
8+
PLUGIN_INCLUDES = $(addprefix -I, $(PLUGIN_INCLUDES_DIR))
9+
PLUGIN_CCFLAGS = -Wall -fPIC
10+
PLUGIN_LDFLAGS = -shared
11+
12+
.PHONY: all
13+
all: $(PLUGIN)
14+
15+
$(PLUGIN): $(PLUGIN_OBJ)
16+
$(CROSS_COMPILE)gcc $(PLUGIN_LDFLAGS) $(PLUGIN_OBJ) -o $@
17+
18+
%.o: %.c
19+
$(CROSS_COMPILE)gcc $(PLUGIN_CCFLAGS) $(PLUGIN_INCLUDES) -c $*.c -o $*.o
20+
21+
.PHONY: clean
22+
clean:
23+
$(RM) $(PLUGIN_OBJ) $(PLUGIN_UUID).plugin
24+

plugins/syslog/syslog_plugin.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
/*
3+
* Copyright (c) 2020, Open Mobile Platform LLC
4+
*/
5+
6+
#include <stddef.h>
7+
#include <syslog.h>
8+
#include <tee_plugin_method.h>
9+
10+
/*
11+
* OPTEE has access to the plugin by the UUID
12+
*/
13+
#define SYSLOG_PLUGIN_UUID { 0x96bcf744, 0x4f72, 0x4866, \
14+
{ 0xbf, 0x1d, 0x86, 0x34, 0xfd, 0x9c, 0x65, 0xe5 } }
15+
16+
/* plugin cmd */
17+
#define TO_SYSLOG 0
18+
19+
static TEEC_Result syslog_plugin_init(void)
20+
{
21+
return TEEC_SUCCESS;
22+
}
23+
24+
static TEEC_Result write_syslog(unsigned int sub_cmd, void *data, size_t data_len)
25+
{
26+
/* 'sub_cmd' in this case means priority according syslog.h */
27+
openlog(NULL, LOG_CONS | LOG_PID, LOG_DAEMON);
28+
syslog(sub_cmd, "%*s", (int)data_len, (const char *)data);
29+
closelog();
30+
31+
return TEEC_SUCCESS;
32+
}
33+
34+
static TEEC_Result syslog_plugin_invoke(unsigned int cmd, unsigned int sub_cmd,
35+
void *data, size_t data_len,
36+
size_t *out_len)
37+
{
38+
/*
39+
* The pointer 'out_len' is used to save length of
40+
* output data from the plugin for TEE, when TEE will be needed
41+
* by the data.
42+
*
43+
* Buffer 'data' is used like input and output.
44+
*/
45+
(void)out_len;
46+
47+
switch (cmd) {
48+
case TO_SYSLOG:
49+
return write_syslog(sub_cmd, data, data_len);
50+
default:
51+
break;
52+
}
53+
54+
return TEEC_ERROR_NOT_SUPPORTED;
55+
}
56+
57+
struct plugin_method plugin_method = {
58+
"syslog",
59+
SYSLOG_PLUGIN_UUID,
60+
syslog_plugin_init, /* can be NULL */
61+
syslog_plugin_invoke,
62+
};

plugins/ta/Android.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
local_module := 2a287631-de1b-4fdd-a55c-b9312e40769a.ta
4+
include $(BUILD_OPTEE_MK)

0 commit comments

Comments
 (0)