OP-TEE / optee_os

Trusted side of the TEE
Other
1.56k stars 1.05k forks source link

I wants to display Secure OS name when hello_world executing which is OPTEE #2534

Closed akbarsaleemt closed 5 years ago

akbarsaleemt commented 6 years ago

Hi, In TA side i wants to implement my own api before TA_Createentrypoin() for finding the OPTEE which is global platform OPTEE or any other secure world like QEE. Is there any way to find that platform(optee or qee ) in trusted world. I wants to display Secure OS name when hello_world executing which is OPTEE. where it is checking this is OPTEE OS or not (note: not in REE side only in Secure side(TEE)). Thank you

jforissier commented 6 years ago

Hi @akbarsaleemt,

You can't do anything before TA_CreateEntryPoint(), because it is "the Trusted Application’s constructor, which the Framework calls when it creates a new instance of the Trusted Application." (GP spec). You could however call TEE_GetPropertyAsString() in your TA's entry point, like so:

TEE_GetPropertyAsString(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description", buf, bufsize);

It will return "OPTEE" if the trusted OS is OP-TEE.

akbarsaleemt commented 6 years ago

thank you

akbarsaleemt commented 6 years ago

I used this code in create entry point but I got error message as hello_world: TEEC_Opensession failed with code 0xffff3024 origin 0x3

/*****************************************************************************************************/

uint32_t *b=1024;
char *buf;
/*
 * Called when the instance of the TA is created. This is the first call in
 * the TA.
 */
TEE_Result TA_CreateEntryPoint(void)
{

TEE_GetPropertyAsString(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description", buf, b);
IMSG("Got value: %s from NW", buf);
    DMSG("has been called");

    return TEE_SUCCESS;
}
/*********************************************************************************************************/
jforissier commented 6 years ago

Your TA has crashed because you have not allocated the output buffer. Try this:

    char buf[1024];
    uint32_t s = sizeof(buf);

    TEE_GetPropertyAsString(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description", buf, &s);
    IMSG("gpd.tee.description=%s", buf)
akbarsaleemt commented 5 years ago

Hi, can I use this propget_get_property() function in Ta application to display name of OS(optee). Thank you.

akbarsaleemt commented 5 years ago

how to use this in Ta application propget_get_property()

vchong commented 5 years ago

User mode TAs can only use functions specified in the GlobalPlatform TEE Internal Core API Specification. TEE_GetPropertyAsString() didn't work for you?

akbarsaleemt commented 5 years ago

hi, I used instead of TEE_GetPropertyAsString() following is it correct any syntax mistake

res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, NULL, NULL,
          buf, len, &prop_type);
if (res == TEE_ERROR_ITEM_NOT_FOUND){
            res = TEE_ERROR_BAD_PARAMETERS;
 IMSG("RES==%d\n",res); 
    }
IMSG("res==%d\n",res);
IMSG("from utee get property:%s",buf);
akbarsaleemt commented 5 years ago

User mode TAs can only use functions specified in the GlobalPlatform TEE Internal Core API Specification. TEE_GetPropertyAsString() didn't work for you?

It's working but we are trying with the following

res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, NULL, NULL,
buf, len, &prop_type);

Thank you

akbarsaleemt commented 5 years ago

Hi team, I used instead of TEE_GetPropertyAsString() in createntrypoint following . it is compiling but while running in QEMU terminal giving

/*********************************QEMU terminal output *********************************************/
# hello_world 
hello_world: TEEC_Opensession failed with code 0xffff0008 origin 0x4
/********************************************************************************************************/
#include <printk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tee_api_defines.h>
#include <tee_api.h>
#include <tee_api_types.h>
#include <tee_arith_internal.h>
#include <tee_internal_api_extensions.h>
#include <tee_isocket.h>
#include <user_ta_header.h>
#include <utee_syscalls.h>
#include <util.h>
#include "/home/takkaakb/Qemu/optee_os/out/arm/export-ta_arm32/include/string_ext.h"
#include "/home/takkaakb/Qemu/optee_os/lib/libutee/base64.h"
#include<tee_svc.h>
#include <tee_internal_api.h>
#include <hello_world_ta.h>
#include<user_ta_header.h>

struct user_ta_property *tee_props;  
struct prop_enumerator {
  uint32_t idx;     /* current index */
  TEE_PropSetHandle prop_set; /* part of TEE_PROPSET_xxx */
};

static TEE_Result propget_get_ext_prop(const struct user_ta_property *ep,
                       enum user_ta_prop_type *type,
                       void *buf, uint32_t *len)
{
    size_t l;

    *type = ep->type;
    switch (*type) {
    case USER_TA_PROP_TYPE_BOOL:
        l = sizeof(bool);
        break;
    case USER_TA_PROP_TYPE_U32:
        l = sizeof(uint32_t);
        break;
    case USER_TA_PROP_TYPE_UUID:
        l = sizeof(TEE_UUID);
        break;
    case USER_TA_PROP_TYPE_IDENTITY:
        l = sizeof(TEE_Identity);
        break;
    case USER_TA_PROP_TYPE_STRING:
        l = strlen(ep->value) + 1;
        break;
    case USER_TA_PROP_TYPE_BINARY_BLOCK:
        l = *len;
        if (!base64_dec(ep->value, strlen(ep->value), buf, &l) &&
            (l <= *len))
            return TEE_ERROR_GENERIC;
        if (*len < l) {
            *len = l;
            return TEE_ERROR_SHORT_BUFFER;
        }

        *len = l;
        return TEE_SUCCESS;
    default:
        return TEE_ERROR_GENERIC;
    }

    if (*len < l) {
        *len = l;
        return TEE_ERROR_SHORT_BUFFER;
    }

    *len = l;
    memcpy(buf, ep->value, l);
    return TEE_SUCCESS;
}

static TEE_Result propset_get(TEE_PropSetHandle h,
                  const struct user_ta_property **eps,
                  size_t *eps_len)
{
    if (h == TEE_PROPSET_CURRENT_TA) {
        *eps = ta_props;
        *eps_len = ta_num_props;
    } else if (h == TEE_PROPSET_CURRENT_CLIENT) {
        *eps = NULL;
        *eps_len = 0;
    } else if (h == TEE_PROPSET_TEE_IMPLEMENTATION) {
        *eps = tee_props;
        *eps_len = ARRAY_SIZE(tee_props);
    } else {
        return TEE_ERROR_ITEM_NOT_FOUND;
    }

    return TEE_SUCCESS;
}

TEE_Result TA_CreateEntryPoint(void)
{
uint32_t index;
TEE_Result res;
enum user_ta_prop_type *type;
const struct user_ta_property *eps;
uint32_t prop_type;
size_t n;
size_t eps_len;
DMSG("enterd into create entry point\n");
char buf[1024];
uint32_t value_len = sizeof(buf);
char *name;
name=buf;s
uint32_t *len= sizeof(buf);;
res=propset_get(TEE_PROPSET_TEE_IMPLEMENTATION, &eps, &eps_len);
if (res != TEE_SUCCESS)
            return res;

        for (n = 0; n < eps_len; n++) {
            if (!strcmp(name, eps[n].name))
                return propget_get_ext_prop(eps + n, type,
                                buf, len);
            }

res=utee_get_property_name_to_index(TEE_PROPSET_TEE_IMPLEMENTATION, name,
                             strlen(name) + 1, &index);
if (res != TEE_SUCCESS)
            return res;

res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, NULL, NULL,
                            buf, len, &prop_type);
if (res != TEE_SUCCESS)
            return res;

IMSG("utee_get_property_using_helloworld=%s", buf);
    DMSG("has been called");

    return TEE_SUCCESS;
}

Thank you.

akbarsaleemt commented 5 years ago

HI Team , I used this following code to display OSname in hello_world_ta.c create entry point but it is not showing anything while running in QEMU terminal. I added related header files to ta also those are

#include <printk.h>
#include<user_ta_header.h>
#include<utee_syscalls.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include "/home/Qemu/optee_os/lib/libutee/base64.h"

TEE_Result TA_CreateEntryPoint(void)
{
    DMSG("enterd into create entry point\n");
    char buf[1024];
    uint32_t value_len = sizeof(buf);
    uint32_t s = sizeof(buf);
    //char *name;
    void *name;
    const struct user_ta_property *eps;
    uint32_t prop_type;
    uint32_t *len;
    TEE_Result res;
    void *tmp_buf = 0;
    uint32_t tmp_len;
    size_t eps_len;
    name=(char *)malloc(s);
    if(name==NULL){
        DMSG("malloc error\n");
        return -1;
    }

    res=utee_get_property_name_to_index(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description",
                        strlen("gpd.tee.description") + 1, &index);
    if (res == TEE_ERROR_ITEM_NOT_FOUND){
                res = TEE_ERROR_BAD_PARAMETERS;
     IMSG("RES==%d\n",res); 
        }
                    IMSG("INDEX:%d",index); IMSG("res==%d\n",res);
    /*res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, NULL, NULL,
       buf, len, &prop_type);*/

    res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, name, &s,
       buf, len, &prop_type);

    if (res == TEE_ERROR_ITEM_NOT_FOUND){
                res = TEE_ERROR_BAD_PARAMETERS;
     IMSG("RES==%d\n",res); 
        }
    IMSG("utee_get_property_using_helloworld=%s", buf);

    IMSG("from utee get property:%s",name); 

    DMSG("has been called");

    return TEE_SUCCESS;
}
jbech-linaro commented 5 years ago

I've reformatted all your comments so they are a bit more readable (now they at least do some indentation and some syntax highlight), please do this by yourself in the future, how? See here.

Your code contains a mismatch of everything. You have printk.h included (which is a Linux kernel thing), you can use malloc, but from a TA it's usually a better idea to use TEE_Malloc. The code style with random indentation and spaces (and lack of) here and there, unused variables etc, all this just gives a bad impression. Please fix up the code, run checkpatch.pl, then post it again and I'll have a look at it.

jforissier commented 5 years ago

len is not initialized. You should set uint32_t len = sizeof(buf); then call utee_get_property(..., &len, ...);

But I fail to see what you are trying to achieve. If, like you said in your first message, you are trying to detect which TEE is running the TA, then you should obviously not be using an OP-TEE specific function (utee_* are implementation-specific).

akbarsaleemt commented 5 years ago

I've reformatted all your comments so they are a bit more readable (now they at least do some indentation and some syntax highlight), please do this by yourself in the future, how? See here.

Your code contains a mismatch of everything. You have printk.h included (which is a Linux kernel thing), you can use malloc, but from a TA it's usually a better idea to use TEE_Malloc. The code style with random indentation and spaces (and lack of) here and there, unused variables etc, all this just gives a bad impression. Please fix up the code, run checkpatch.pl, then post it again and I'll have a look at it.

include

include

include

include

include

include

include

include "/home/takkaakb/Qemu/optee_os/lib/libutee/base64.h"

TEE_Result TA_CreateEntryPoint(void) { DMSG("enterd into create entry point\n"); char buf[1024]; uint32_t index; uint32_t s = sizeof(buf); char name; uint32_t prop_type; uint32_t len= sizeof(buf); TEE_Result res; name=(char )malloc(s); if(name==NULL){ DMSG("malloc error\n"); return -1; } res=utee_get_property_name_to_index(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description", strlen("gpd.tee.description") + 1, &index); if (res == TEE_ERROR_ITEM_NOT_FOUND){ res = TEE_ERROR_BAD_PARAMETERS; IMSG("RES==%d\n",res); } IMSG("INDEX:%d",index); IMSG("res==%d\n",res); res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, name, &s, buf, len, &prop_type); if (res == TEE_ERROR_ITEM_NOT_FOUND){ res = TEE_ERROR_BAD_PARAMETERS; IMSG("RES==%d\n",res); } IMSG("utee_get_property_using_helloworld=%s", buf); IMSG("from utee get property:%s",name); DMSG("has been called"); return TEE_SUCCESS; }

please find it .

Thankyou

jforissier commented 5 years ago

@akbarsaleemt you did not read my previous reply carefully. Please read it again and your code will work.

jbech-linaro commented 5 years ago

@akbarsaleemt , it's better but still far away from what I asked. I'm still missing a proper GitHub code section, please re-read the information on the link on my previous reply to you. Also checkpatch gives:

WARNING: Missing or malformed SPDX-License-Identifier tag in line 1
#1: FILE: foobar.c:1:
+#include <tee_internal_api.h>

ERROR: spaces required around that '=' (ctx:VxW)
#17: FILE: foobar.c:17:
+   uint32_t len= sizeof(buf);
                ^

WARNING: Missing a blank line after declarations
#19: FILE: foobar.c:19:
+   TEE_Result res;
+   name=(char *)malloc(s);

ERROR: spaces required around that '=' (ctx:VxV)
#19: FILE: foobar.c:19:
+   name=(char *)malloc(s);
        ^

ERROR: spaces required around that '==' (ctx:VxV)
#20: FILE: foobar.c:20:
+   if(name==NULL){
           ^

ERROR: space required before the open brace '{'
#20: FILE: foobar.c:20:
+   if(name==NULL){

ERROR: space required before the open parenthesis '('
#20: FILE: foobar.c:20:
+   if(name==NULL){

WARNING: line over 80 characters
#24: FILE: foobar.c:24:
+   res=utee_get_property_name_to_index(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description",

ERROR: spaces required around that '=' (ctx:VxV)
#24: FILE: foobar.c:24:
+   res=utee_get_property_name_to_index(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.description",
       ^

WARNING: line over 80 characters
#25: FILE: foobar.c:25:
+                       strlen("gpd.tee.description") + 1, &index);

ERROR: space required before the open brace '{'
#26: FILE: foobar.c:26:
+   if (res == TEE_ERROR_ITEM_NOT_FOUND){

ERROR: space required after that ',' (ctx:VxV)
#28: FILE: foobar.c:28:
+       IMSG("RES==%d\n",res);
                        ^

ERROR: space required after that ',' (ctx:VxV)
#30: FILE: foobar.c:30:
+   IMSG("INDEX:%d",index);
                   ^

ERROR: space required after that ',' (ctx:VxV)
#31: FILE: foobar.c:31:
+   IMSG("res==%d\n",res);
                    ^

ERROR: spaces required around that '=' (ctx:VxV)
#32: FILE: foobar.c:32:
+   res=utee_get_property(TEE_PROPSET_TEE_IMPLEMENTATION, index, name, &s,
       ^

ERROR: space required before the open brace '{'
#34: FILE: foobar.c:34:
+   if (res == TEE_ERROR_ITEM_NOT_FOUND){

ERROR: space required after that ',' (ctx:VxV)
#36: FILE: foobar.c:36:
+       IMSG("RES==%d\n",res);
                        ^

ERROR: space required after that ',' (ctx:VxV)
#39: FILE: foobar.c:39:
+   IMSG("from utee get property:%s",name);
                                    ^

total: 14 errors, 4 warnings, 42 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

foobar.c has style problems, please review.

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.

Having that said, here is a patch that what does exactly what you are asking for (I've more or less just copy pasted @jforissier second reply).

From c0f5948f1766bea4a24dd81604cd1e9bdd017efa Mon Sep 17 00:00:00 2001
From: Joakim Bech <joakim.bech@linaro.org>
Date: Fri, 5 Oct 2018 08:56:49 +0200
Subject: [PATCH] hello_world: show TEE OS name

Signed-off-by: Joakim Bech <joakim.bech@linaro.org>
---
 hello_world/ta/hello_world_ta.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/hello_world/ta/hello_world_ta.c b/hello_world/ta/hello_world_ta.c
index 2423b30..3336f63 100644
--- a/hello_world/ta/hello_world_ta.c
+++ b/hello_world/ta/hello_world_ta.c
@@ -36,7 +36,13 @@
  */
 TEE_Result TA_CreateEntryPoint(void)
 {
-   DMSG("has been called");
+   char buf[1024];
+   uint32_t s = sizeof(buf);
+
+   TEE_GetPropertyAsString(TEE_PROPSET_TEE_IMPLEMENTATION,
+               "gpd.tee.description", buf, &s);
+
+   DMSG("gpd.tee.description=%s", buf);

    return TEE_SUCCESS;
 }
-- 
2.16.2

Please just apply that on current master in optee_examples. I.e. save the patch to a file foo.patch, then in optee_examples, run git am foo.patch. Rebuild (make run) and you're good to go.

FYI, I've just run the code here using QEMU and the output on secure side is shown like:

D/TA:  TA_CreateEntryPoint:45 gpd.tee.description=OPTEE
akbarsaleemt commented 5 years ago

Thankyou.

akbarsaleemt commented 5 years ago

Hi Team, I tried to display OS information in tee-svc.c file by using IMSG(); it displayed following are they correct or any other way. OS name OPTEE I/TC: OS version3.2.0-56-ga06857f9-dev I/TC: OS manufacturer LINARO

Thankyou