Skip to content

plugins: example of using supplicant syslog plugin from TA code - #79

Merged
jforissier merged 1 commit into
linaro-swg:masterfrom
anisyanka:plugin-example
Feb 4, 2021
Merged

plugins: example of using supplicant syslog plugin from TA code#79
jforissier merged 1 commit into
linaro-swg:masterfrom
anisyanka:plugin-example

Conversation

@anisyanka

Copy link
Copy Markdown
Contributor

TEE clients can add to the tee-supplicant an own plugin.
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 and also the OP-TEE kernel code can interact
with the plugins with the help of the special PRC.

This patch adds example of using the plugin framework.
By default in tee-supplicant there is 'syslog' plugin.
It can write log messages from OP-TEE (TAs or kernel)
to system log. You can find the implementation of the plugin
in 'tee-supplicant/plugins/syslog/syslog_plugin.c' file.

This example adds a new TA, which interacts with
'syslog' plugin with the help of the plugin-pta.
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 a.anisimov@omprussia.ru

@anisyanka

Copy link
Copy Markdown
Contributor Author

The example relates to:
OP-TEE/optee_client#239
OP-TEE/optee_os#4248

@etienne-lms etienne-lms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the P-R.
Some minor comments.
It would be nice to have a Cmake script. I'll see if I can help.
The supplicant plugin syslog shall be built as a .so and installed in /usr/lib/tee-supplicant/plugins/ right?

Comment thread plugins/ta/user_ta_header_defines.h Outdated
* TA properties: multi-instance TA, no specific attribute
* TA_FLAG_EXEC_DDR is meaningless but mandated.
*/
#define TA_FLAGS TA_FLAG_EXEC_DDR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TA_FLAG_EXEC_DDR mandated? maybe by older optee_os. Can you check you still have this dependency. It not, prefer 0 here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. TA_FLAG_EXEC_DDR is already deprecated. I will replace to 0.

Comment thread plugins/ta/plugin_ta.c Outdated
case PLUGIN_TA_PING:
return syslog_plugin_ping();
default:
return TEE_ERROR_BAD_PARAMETERS;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TEE_ERROR_NOT_SUPPORTED

Comment thread plugins/ta/plugin_ta.c Outdated
inc_var++);

params[2].memref.buffer = log_str;
params[2].memref.size = strlen(log_str);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to add trace here IMSG("Push syslog plugin string \"%s\"", log_str);

Comment thread plugins/ta/plugin_ta.c Outdated
TEE_UUID syslog_uuid = SYSLOG_PLUGIN_UUID;
TEE_TASessionHandle sess;
TEE_Param params[4] = { 0 };
uint32_t types, orig;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to follow the same codiing rules as optee_os/optee_client.

	uint32_t types = 0;
	uint32_t orig = 0;

Comment thread plugins/ta/plugin_ta.c Outdated
uint32_t d2;
uint32_t d3;
uint32_t d4;
} plug_u;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= { 0 };

Comment thread plugins/ta/plugin_ta.c Outdated

static TEE_Result syslog_plugin_ping(void)
{
TEE_Result res;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to follow the same codiing rules as optee_os/optee_client: init all local vars.

Comment thread plugins/ta/plugin_ta.c Outdated
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;

/* If return value != TEE_SUCCESS the session will not be created. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this comment

Comment thread plugins/ta/plugin_ta.c Outdated
* with a value to be able to identify this session in subsequent calls to the
* TA. In this function you will normally do the global initialization for the
* TA.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to remove these inline comments

Comment thread plugins/ta/include/plugin_ta.h Outdated
* Copyright (c) 2020, Open Mobile Platform LLC
*/

#ifndef TA_PLUGIN_H

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PLUGIN_TA_H

Comment thread plugins/host/main.c Outdated

int main(void)
{
int i;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default init all loval variables

@anisyanka

Copy link
Copy Markdown
Contributor Author

It would be nice to have a Cmake script. I'll see if I can help.

Do you mean Cmake script to build syslog plugin here? Or maybe you need script in optee-client repo to build any future plugins?

The supplicant plugin syslog shall be built as a .so and installed in /usr/lib/tee-supplicant/plugins/ right?

Yes, it's right.

@anisyanka

Copy link
Copy Markdown
Contributor Author

All comments have been addressed

Comment thread plugins/syslog/tee_plugin_method.h Outdated
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: BSD-2-Clause */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be made available from optee_client instead to avoid keeping possibly diverging copies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I droped this file.
Now we use $(TEEC_EXPORT)/include in syslog Makefile

Comment thread plugins/ta/plugin_ta.c Outdated
/*
* Interface with syslog tee-supplicant plugin
*/
#define SYSLOG_PLUGIN_UUID { 0x96bcf744, 0x4f72, 0x4866, \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to have a shared .h file with all this?

@anisyanka

Copy link
Copy Markdown
Contributor Author

All comments have been addressed

Comment thread plugins/host/main.c Outdated
/* Initialize a context connecting us to the TEE */
res = TEEC_InitializeContext(NULL, &ctx);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use %#"PRIx32 instead of 0x%x". res is a basically uint32_t.
Same below for the other format strings.

Comment thread plugins/syslog/syslog_plugin.c Outdated

/* 'sub_cmd' in this case means priority according syslog.h */
openlog(NULL, LOG_CONS | LOG_PID, LOG_DAEMON);
syslog((int)sub_cmd, "%s", (const char *)data);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if data isn't null terminated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example I use null-terminated strings for the plugin to pass them to syslog().
But actually, data_len stores real size of the data. that's why we can use data and data_len in any other cases of the use of plugins.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, obviously it works but it isn't robust. Bear in mind that people might copy these examples.
I'd prefer something more safe like:

syslog(sub_cmd, "%*s", (int)data_len, (const char *)data);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@anisyanka

Copy link
Copy Markdown
Contributor Author

All comments have been addressed

@jenswikl

Copy link
Copy Markdown
Contributor

Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>

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 <a.anisimov@omprussia.ru>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
@anisyanka

Copy link
Copy Markdown
Contributor Author

The commit updated, thanks for the review!

@jforissier
jforissier merged commit 0607ed4 into linaro-swg:master Feb 4, 2021
@anisyanka
anisyanka deleted the plugin-example branch February 4, 2021 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants