Check security.tee feature when opening context 66/181366/2
authorIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Mon, 11 Jun 2018 10:55:30 +0000 (12:55 +0200)
committerIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Thu, 13 Dec 2018 15:00:17 +0000 (16:00 +0100)
All the other entry points check for initialized context, so there should be no
need to check the feature anywhere else.

Change-Id: I20f032f91714460e3dea1b2fafd2f83f8020a2cb
Signed-off-by: Igor Kotrasinski <i.kotrasinsk@partner.samsung.com>
CMakeLists.txt
packaging/tef-libteec.spec
src/tef_libteec.c

index 56b2f20..26fb048 100644 (file)
@@ -1,5 +1,5 @@
 #
-#  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+#  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
 #
 #  Contact: Krzysztof Dynowski <k.dynowski@samsung.com>
 #
@@ -61,15 +61,24 @@ IF(DEFINED FORCE_32BIT)
     SET(CMAKE_C_FLAGS -m32)
 ENDIF()
 
-INCLUDE_DIRECTORIES(
-    ${TEF_API_PATH}
-)
-
 FILE(GLOB TEF_API_HEADERS api/tef/*.h)
 FILE(GLOB TEF_API_SOURCES src/*.c src/simulator/*.c src/optee/*.c)
 
 ADD_LIBRARY(${TEF_TARGET} SHARED ${TEF_API_SOURCES})
 
+PKG_CHECK_MODULES(LIBTEEC_DEPS
+    capi-system-info
+)
+
+INCLUDE_DIRECTORIES(
+    ${TEF_API_PATH}
+    ${LIBTEEC_DEPS_INCLUDE_DIRS}
+)
+
+TARGET_LINK_LIBRARIES(${TEF_TARGET}
+    ${LIBTEEC_DEPS_LIBRARIES}
+)
+
 SET(_LIB_VERSION_ "${VERSION}")
 SET(_LIB_SOVERSION_ "0")
 SET_TARGET_PROPERTIES(${TEF_TARGET} PROPERTIES
index 273578e..07c7df5 100644 (file)
@@ -5,6 +5,7 @@ License:            Apache-2.0 and BSD-2-Clause
 Group:              Security/Other
 Summary:            Global Platform client API
 BuildRequires:      cmake
+BuildRequires:      pkgconfig(capi-system-info)
 Requires(post):     /sbin/ldconfig
 Requires(postun):   /sbin/ldconfig
 Source0:            %{name}-%{version}.tar.gz
index b5d27a4..b94e212 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Krzysztof Dynowski <k.dynowski@samsung.com>
  *
@@ -27,6 +27,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <stdbool.h>
+#include <system_info.h>
 
 #include "simulator/creators.h"
 #include "optee/creators.h"
@@ -36,6 +38,7 @@
 #define TEF_LIB_PATH LIB_INSTALL_DIR "/tef"
 #define TEF_IMPLEMENTATION_SONAME "libteec.so"
 #define TEEC_SYMBOL(lib, name) *((void**)&lib.w##name) = dlsym(lib.handle, "TEEC_" #name)
+#define TEE_FEATURE "http://tizen.org/feature/security.tee"
 
 typedef struct {
        const char *name;
@@ -74,6 +77,42 @@ typedef struct {
        void *lastOperation;
 } TEF_SessionImpl;
 
+static TEEC_Result tee_enabled_check()
+{
+       bool enabled;
+       int sys_ret = system_info_get_platform_bool(TEE_FEATURE, &enabled);
+       if (sys_ret == SYSTEM_INFO_ERROR_NONE) {
+               if (enabled)
+                       return TEEC_SUCCESS;
+               else
+                       return TEEC_ERROR_NOT_SUPPORTED;
+       }
+
+       const char *error_str;
+       char error_str_buf[256];
+       int ret = TEEC_ERROR_GENERIC, err;
+
+       switch (sys_ret) {
+       case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
+               error_str = "Feature key not present";
+               break;
+       case SYSTEM_INFO_ERROR_IO_ERROR:
+               error_str = "Input/output error";
+               break;
+       case SYSTEM_INFO_ERROR_PERMISSION_DENIED:
+               error_str = "Permission denied";
+               break;
+       default:
+               err = snprintf(error_str_buf, sizeof(error_str_buf), "Unknown error %d", ret);
+               if (err < 0 || (unsigned int) err >= sizeof(error_str_buf))
+                       return TEEC_ERROR_GENERIC;
+               error_str = error_str_buf;
+       }
+       fprintf(stderr, "tef-libteec: Failed to query feature %s: %s",
+            TEE_FEATURE, error_str);
+       return ret;
+}
+
 TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context)
 {
        if (lib.handle == NULL) {
@@ -82,8 +121,13 @@ TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context)
        if (context == NULL) {
                return TEEC_ERROR_BAD_PARAMETERS;
        }
+       TEEC_Result result;
+       result = tee_enabled_check();
+       if (result != TEEC_SUCCESS)
+               return result;
+
        context->imp = lib.createContext();
-       TEEC_Result result = lib.wInitializeContext(name, (TEEC_Context *)context->imp);
+       result = lib.wInitializeContext(name, (TEEC_Context *)context->imp);
        if (result != TEEC_SUCCESS) {
                free(context->imp);
                context->imp = NULL;