fix TA paths, use unique_ptr for memory managment 54/164154/3
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Mon, 4 Dec 2017 16:49:08 +0000 (17:49 +0100)
committerKrzysztof Dynowski <k.dynowski@samsung.com>
Wed, 20 Dec 2017 13:44:35 +0000 (14:44 +0100)
Using unique_ptr requires -std=c++1y compiler flag

Change-Id: I463ed5616e623f4362906e469e134fee3679f7a2

14 files changed:
CMakeLists.txt
TEECLib/src/teec_api.c
TEECLib/src/teec_connection.c
TEEStub/PropertyAccess/TEEProperty.cpp
TEEStub/teestubmain.cpp
include/include/config.h
packaging/tef-simulator.spec
simulatordaemon/inc/SecurityContext.h
simulatordaemon/src/TABinaryManager/Config.h [deleted file]
simulatordaemon/src/TABinaryManager/TABinaryManager.cpp
simulatordaemon/src/TABinaryManager/TAUnpack.cpp
simulatordaemon/src/TAInstance.cpp
ssflib/dep/swdss/source/secure_file.cpp
ssflib/src/ssf_client.cpp

index 133c2ca..d0f1ceb 100644 (file)
@@ -40,7 +40,7 @@ ADD_DEFINITIONS("-DBOOST_ASIO_DISABLE_STD_ARRAY")
 
 ADD_DEFINITIONS("-D_GNU_SOURCE")
 
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y")
 
 # Set compiler warning flags
 # TODO uncomment these when it's cleanup time
index 91f5185..e269e02 100644 (file)
@@ -330,9 +330,11 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
        uint32_t i, type;
 
        // Check if Session is valid
-       if (session)
-               context = ((TEEC_SessionImp *)session->imp)->context;
-       else return TEEC_ERROR_BAD_PARAMETERS;
+       if (!session) {
+               LOGE(TEEC_LIB, "Bad parameters");
+               return TEEC_ERROR_BAD_PARAMETERS;
+       }
+       context = ((TEEC_SessionImp *)session->imp)->context;
 
        // Check if output Operation structure is valid
        if (!op)
@@ -377,8 +379,7 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                        op->paramTypes |= type << (8 * i);
 
                        if (!tmpSharedMem[i]) {
-                               result = tempSharedMemAllocate(&tmpSharedMem[i], type,
-                                       ((TEEC_SessionImp *)session->imp)->context, operation->params[i]);
+                               result = tempSharedMemAllocate(&tmpSharedMem[i], type, context, operation->params[i]);
                                if (result != TEEC_SUCCESS)
                                        goto cleanup;
                        }
@@ -442,6 +443,7 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                        break;
 
                default:
+                       LOGE(TEEC_LIB, "Bad parameters");
                        result = TEEC_ERROR_BAD_PARAMETERS;
                        goto cleanup;
                }
index 8aadfc1..a5feaa4 100644 (file)
  *  Include files
  *-----------------------------------------------------------------------------*/
 #include "teec_connection.h"
+#include <config.h>
 
 /*-----------------------------------------------------------------------------
  *  Macros
  *-----------------------------------------------------------------------------*/
-#define SOCKPATH "/tmp/simdaemon"
 
 /*-----------------------------------------------------------------------------
  *  TEST MACROS
@@ -61,15 +61,15 @@ int32_t connecttoServer(void)
 
        daemonsock.sun_family = AF_UNIX;
 
-       sock_path_len = strlen(SOCKPATH);
-       strncpy(daemonsock.sun_path, SOCKPATH, sock_path_len + 1);
+       sock_path_len = strlen(SIMDAEMON_SOCK_PATH);
+       strncpy(daemonsock.sun_path, SIMDAEMON_SOCK_PATH, sock_path_len + 1);
 
        socklen = sizeof(daemonsock);
        sockptr = (struct sockaddr *)&daemonsock;
 
        // Connect to Simulator Daemon
        if (connect(serverSocket, sockptr, socklen) == -1) {
-               LOGE(TEEC_LIB, "connection to simdaemon failed");
+               LOGE(TEEC_LIB, "connection to simdaemon(%s) failed errno=%d", SIMDAEMON_SOCK_PATH, errno);
                close(serverSocket);
                return -1;
        }
index ea431c2..1d3e1e2 100644 (file)
@@ -115,7 +115,7 @@ bool TEEProperty::getNextProperty() {
  * @return true if property file successfully read else false\r
  */\r
 bool TEEProperty::start() {\r
-       bool ret = readPropertyFile(string(TEE_PROP_FILE));\r
+       bool ret = readPropertyFile(string(TEE_PROP_PATH));\r
        currentItr = propertiesMap.begin();\r
        return ret;\r
 }\r
index 2f1f604..3b1cd3b 100644 (file)
@@ -30,6 +30,7 @@
 #include "TEEStubServer/TEEStubServer.h"
 #include <PropertyAccess/PropertyApi.h>
 #include <PropertyAccess/PropertyUtility.h>
+#include <config.h>
 #include "log.h"
 
 /*-----------------------------------------------------------------------------
@@ -49,7 +50,7 @@ boost::asio::io_service io_service; ///< io_service provides OS abstraction for
 void StartServer(string socketName) {
        try {
                ::unlink(socketName.c_str());
-               LOGD(TEE_STUB, "Waiting on socket");
+               LOGD(TEE_STUB, "Waiting on socket %s", socketName.c_str());
                TEEStubServer s(io_service, socketName.c_str());
                io_service.run();
        } catch (std::exception& e) {
@@ -107,7 +108,7 @@ int main(int argc, char* argv[]) {
 
        // Once the server is started, it exits only after the
        // connection is lost or gracefully disconnected.
-       StartServer(string("/tmp/") + string(argv[1]));
+       StartServer(string(TEE_TASOCK_ROOT) + string(argv[1]));
        LOGD(TEE_STUB, "Exiting TEEStub\n");
        // Deallocate property objects
        if (TEE_SUCCESS == initStatus) DeInitPropertyModule();
index b72f61f..51752fe 100644 (file)
 #ifndef INCLUDE_CONFIG_H_
 #define INCLUDE_CONFIG_H_
 
-#define TEE_PROP_FILE "/usr/bin/GPD_TEE_PROP"
-#define TA_ROOT "/tmp/"
-#if _ARCH_ == 32
-    #define TEE_TASTORE_ROOT "/usr/lib/tastore/"
-#elif _ARCH_ == 64
-    #define TEE_TASTORE_ROOT "/usr/lib64/tastore/"
-#else
-    #error "Invalid architecture was set!"
-#endif
+//this is ln -s to /usr/lib/tastore or /usr/lib64/tastore (see spec file)
+#define TEE_TASTORE_ROOT "/opt/tastore/"
+
+//TEEStub must have write access in this directory (creating socket per TA)
+#define TEE_TASOCK_ROOT "/tmp/"
+
+#define SHM_PATH "/tmp/shm"
+#define SIMDAEMON_SOCK_PATH "/tmp/simdaemon"
+
+#define TEE_PROP_PATH "/usr/bin/GPD_TEE_PROP"
 
 #endif /* INCLUDE_CONFIG_H_ */
index 2be2c70..83a8df9 100644 (file)
@@ -25,8 +25,8 @@ PreReq: tef-libteec
 %define lib_dir %{?TZ_SYS_LIB:%TZ_SYS_LIB}%{!?TZ_SYS_LIB:%_libdir}
 %define data_dir %{?TZ_SYS_RO_SHARE:%TZ_SYS_RO_SHARE}%{!?TZ_SYS_RO_SHARE:%_datadir}
 %define include_dir %{?TZ_SYS_INCLUDE:%TZ_SYS_INCLUDE}%{!?TZ_SYS_INCLUDE:%_includedir}
-%define tastore_dir /opt/tastore
-%define link_tastore_dir %{lib_dir}/tastore
+%define tastore_dir %{lib_dir}/tastore
+%define link_tastore_dir /opt/tastore
 
 %define build_bin_dir %{buildroot}%{bin_dir}
 %define build_lib_dir %{buildroot}%{lib_dir}
@@ -97,6 +97,9 @@ cp include/include/LICENSE LICENSE.BSD
 %pre
 
 %post
+rm -f %{link_tastore_dir}
+mkdir -p %{link_tastore_dir}
+rmdir %{link_tastore_dir}
 ln -sf %{tastore_dir} %{link_tastore_dir}
 tef-update.sh simulator
 systemctl enable tef-simulator
index 2008a57..645a110 100644 (file)
@@ -26,7 +26,7 @@
 
 #include <string>
 #include "log.h"
-#include "config.h"
+#include <config.h>
 #include <cynara/cynara-client.h>
 
 #define TA_LOCAL_PATH "/res/tee"
diff --git a/simulatordaemon/src/TABinaryManager/Config.h b/simulatordaemon/src/TABinaryManager/Config.h
deleted file mode 100644 (file)
index 0bd4c64..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-
-/**
- * @file
- * @author CHERYL (cb) (cheryl.b@samsung.com)
- * @brief  Configuration details
- */
-
-#ifndef CONFIG_H_
-#define CONFIG_H_
-
-/*-----------------------------------------------------------------------------
- *  MACROS
- *-----------------------------------------------------------------------------*/
-#define TA_STORE_PATH "/usr/lib/tastore/"
-
-#endif /* CONFIG_H_ */
index 4561b2e..298a76d 100644 (file)
@@ -25,7 +25,7 @@
  *  Include files
  *-----------------------------------------------------------------------------*/
 #include "TABinaryManager.h"
-#include "Config.h"
+#include <config.h>
 #include <iostream>
 #include <fstream>
 #include <stdio.h>
@@ -167,7 +167,7 @@ bool TABinaryManager::initTA(const string &uuid) {
        bool res = false;
        StructBinaryInfo info;
 
-       if (boost::filesystem::exists(TA_STORE_PATH + uuid)) {
+       if (boost::filesystem::exists(TEE_TASTORE_ROOT + uuid)) {
                pthread_mutex_lock(&taLock);
                try {
                        if (unpackBinary(uuid, info)) {
@@ -248,11 +248,11 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) {
 bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) {
        TAUnpack* unpacker = TAUnpack::getInstance();
        bool ret = false;
-       if (0 == unpacker->unpackTA(string(TA_STORE_PATH), uuid)) {
+       if (0 == unpacker->unpackTA(string(TEE_TASTORE_ROOT), uuid)) {
                LOGD(SIM_DAEMON, "Unpacked, filling info");
                // 1. Set binary info
-               info.path = string(TA_STORE_PATH)+ uuid;
-               info.extractpath = string(TA_STORE_PATH) + uuid + "-ext/";
+               info.path = string(TEE_TASTORE_ROOT)+ uuid;
+               info.extractpath = string(TEE_TASTORE_ROOT) + uuid + "-ext/";
                info.imagePath = info.extractpath + uuid + ".image";
                info.manifestPath = info.extractpath + uuid + ".manifest";
                // 2. Parse manifest and store results
index 01a4a32..5279024 100644 (file)
 #include "TAUnpack.h"
 #include <iostream>
 #include <fstream>
+#include <memory>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <log.h>
 
 using namespace std;
@@ -88,40 +90,45 @@ int TAUnpack::unpackTA(string path, string uuid) {
        }
        // 2. Verify header
        if (SECURITY_HEADER_MAGIC1 == packageHeader.magic1 &&
-       SECURITY_HEADER_MAGIC2 == packageHeader.magic2) {
+                       SECURITY_HEADER_MAGIC2 == packageHeader.magic2) {
+               string imageFile = extract_dir_path + uuid + ".image";
+               string manifestFile = extract_dir_path + uuid + ".manifest";
+               unlink(imageFile.c_str());
+               unlink(manifestFile.c_str());
+
                // 3. Read image and write to FS
                tapackage.seekg(packageHeader.image_offset);
-               char *imagedump = new char[packageHeader.image_size];
-               tapackage.read(imagedump, packageHeader.image_size);
+               auto imagedump = std::make_unique<char[]>(packageHeader.image_size);
+               tapackage.read(imagedump.get(), packageHeader.image_size);
                if (tapackage.fail()) {
                        LOGE(SIM_DAEMON, "Read failed");
                        return -1;
                }
-               string removeImage = "rm -f " + extract_dir_path + uuid + ".image";
-               int result = system(removeImage.c_str());
-               if (result != 0) {
-                       LOGE(SIM_DAEMON, "Failed to remove existing TA image");
-               }
 
-               ofstream image((extract_dir_path + uuid + ".image").c_str(),
-                   ios::out | ios::binary);
-               ofstream manifest((extract_dir_path + uuid + ".manifest").c_str(),
-                   ios::out | ios::binary);
-               image.write(imagedump, packageHeader.image_size);
-               image.flush();
-               delete[] imagedump;
+               ofstream image(imageFile.c_str(), ios::out | ios::binary);
+               if (image.fail()) {
+                       LOGE(SIM_DAEMON, "Open failed %s %s", imageFile.c_str(), strerror(errno));
+                       return -1;
+               }
+               image.write(imagedump.get(), packageHeader.image_size);
+               image.close();
 
                // 4. Read manifest and write to FS
                tapackage.seekg(packageHeader.manifest_offset);
-               char *manifestdump = new char[packageHeader.manifest_size];
-               tapackage.read(manifestdump, packageHeader.manifest_size);
+               auto manifestdump = std::make_unique<char[]>(packageHeader.manifest_size);
+               tapackage.read(manifestdump.get(), packageHeader.manifest_size);
                if (tapackage.fail()) {
                        LOGE(SIM_DAEMON, "Read failed");
                        return -1;
                }
-               manifest.write(manifestdump, packageHeader.manifest_size);
-               manifest.flush();
-               delete[] manifestdump;
+
+               ofstream manifest(manifestFile.c_str(), ios::out | ios::binary);
+               if (manifest.fail()) {
+                       LOGE(SIM_DAEMON, "Open failed %s %s", manifestFile.c_str(), strerror(errno));
+                       return -1;
+               }
+               manifest.write(manifestdump.get(), packageHeader.manifest_size);
+               manifest.close();
        } else {
                LOGE(SIM_DAEMON, "Header verification failed");
                return -1;
index be1f6a3..afa5ad7 100644 (file)
@@ -177,7 +177,7 @@ TEEC_Result TAInstance::connecttoTA(std::stringstream& str) {
        unsigned long int retry_count = 0;
        try {
                boost::system::error_code error = boost::asio::error::host_not_found;
-               stream_protocol::endpoint ep(string("/tmp/") + str.str());
+               stream_protocol::endpoint ep(string(TEE_TASOCK_ROOT) + str.str());
 
                LOGD(SIM_DAEMON, "Connect to TEEStub %s", str.str().c_str());
                // Try to connect to TA RETRY_COUNT number of times
index 0ab45f6..96c89e7 100644 (file)
 #include "ss_misc.h"
 #include "OsaLinuxUser.h"
 #include <new>
+#include <config.h>
 
 #ifdef _SECOS_SIM_
 #include "file_op.h"
-#define SWD_SS_ROOT "/tmp/tastore2/"
-
 #endif
 
 // this is RNG SEED for mask
@@ -1706,7 +1705,7 @@ int secure_file::get_data_name(char* data_name, int maxlen, bool is_dir) {
        char* ptr = data_name;
        int remain = maxlen;
 
-       strncpy(ptr, SWD_SS_ROOT, remain);
+       strncpy(ptr, TEE_TASTORE_ROOT, remain);
        if (ptr[remain - 1] != '\0')
                return -1;
        ptr += strlen(ptr);
index 2c89207..edec1cc 100644 (file)
 #include <assert.h>
 #include "ssf_client.h"
 #include <unistd.h>
+#include <config.h>
 
 /*-----------------------------------------------------------------------------
  *  MACROS
  *-----------------------------------------------------------------------------*/
-#define SOCKPATH "/tmp/simdaemon" //path to be updated
 
 
 /*-----------------------------------------------------------------------------
@@ -58,13 +58,13 @@ int32_t connecttoServer(void) {
        }
        daemonsock.sun_family = AF_UNIX;
 
-       sock_path_len = strlen(SOCKPATH);
-       strncpy(daemonsock.sun_path, SOCKPATH, sock_path_len+1);
+       sock_path_len = strlen(SIMDAEMON_SOCK_PATH);
+       strncpy(daemonsock.sun_path, SIMDAEMON_SOCK_PATH, sock_path_len+1);
 
        socklen = sizeof(daemonsock);
        sockptr = (struct sockaddr*)&daemonsock;
        if (connect(serverSocket, sockptr, socklen) == -1) {
-               LOGE(SSF_LIB, "connection to simdaemon failed");
+               LOGE(SSF_LIB, "connection to simdaemon failed (s=%s)", SIMDAEMON_SOCK_PATH);
                close(serverSocket);
                return -1;
        }