fix SHM path, remove unused code, replace system wih boost API (for simple cases) 59/166959/5
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Thu, 11 Jan 2018 15:37:10 +0000 (16:37 +0100)
committerKrzysztof Dynowski <k.dynowski@samsung.com>
Mon, 15 Jan 2018 12:47:52 +0000 (13:47 +0100)
Change-Id: I1a3e868fe11de69cae5ac6740ebe75b64ff4297e

TEECLib/src/teec_api.c
TEECLib/src/teec_connection.c
TEEStub/PropertyAccess/TEEProperty.cpp
TEEStub/TACommands/SharedMemoryMap.cpp
include/include/config.h
simulatordaemon/src/SimulatorDaemon.cpp
simulatordaemon/src/TABinaryManager/TABinaryManager.cpp

index c46c93304d1ef377fc3305d0cd05edf7b1695b62..085f57a61c2069b7888c32eb23fcda05d5c19111 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdint.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <config.h>
 
 /*-----------------------------------------------------------------------------
  *  MACROS
@@ -40,7 +41,6 @@
 #define PAGE_MASK       (~(PAGE_SIZE - 1))
 
 #define SHM_MAX_ID              INT32_MAX
-#define SHM_NAME_TEMPLATE       "/teec_shm%d"
 #define SHM_FILE_MODE           0660
 /*-----------------------------------------------------------------------------
  *  Globals
@@ -102,15 +102,13 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm)
        int res;
 
        do {
-               res = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, memKey);
-
+               res = snprintf(shm_name, sizeof(shm_name), SHM_NAME_PREFIX "%d", memKey);
                if (res == sizeof(shm_name)) {
                        LOGE(TEEC_LIB, "the shm object name is too long");
                        return TEEC_ERROR_GENERIC;
                }
 
                fd_shm = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, SHM_FILE_MODE);
-
                if (fd_shm >= 0) {
                        res = fchmod(fd_shm, SHM_FILE_MODE);
 
@@ -127,8 +125,8 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm)
                }
 
                if (errno != EEXIST) {
-                       LOGE(TEEC_LIB, "Cannot create shared memory object, error: %s",
-                                strerror(errno));
+                       LOGE(TEEC_LIB, "Cannot create shared memory object '%s', error: %s",
+                               shm_name, strerror(errno));
                        return TEEC_ERROR_GENERIC;
                }
 
@@ -191,15 +189,14 @@ static void freeSharedMemory(TEEC_SharedMemory *shm)
                return;
        }
 
-       ret = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, sharedMem_imp->shmKey);
-
+       ret = snprintf(shm_name, sizeof(shm_name), SHM_NAME_PREFIX "%d", sharedMem_imp->shmKey);
        if (ret == sizeof(shm_name)) {
-               LOGE(TEE_STUB, "the shm object name is too long");
+               LOGE(TEEC_LIB, "the shm object name is too long");
                return;
        }
 
        if (shm_unlink(shm_name) == -1) {
-               LOGE(TEE_STUB, "shm_unlink failed for %s, error: %s", shm_name,
+               LOGE(TEEC_LIB, "shm_unlink failed for %s, error: %s", shm_name,
                         strerror(errno));
                return;
        }
index a5feaa47feff334db3e5ca744a0b54c98e1a0dec..dbfff7315015568ef54d93977aaf36b724fdbec7 100644 (file)
@@ -49,21 +49,25 @@ int32_t connecttoServer(void)
 {
        LOGD(TEEC_LIB, "Entry");
        int32_t serverSocket, socklen;
-       size_t sock_path_len = 0;
        struct sockaddr *sockptr;
        struct sockaddr_un daemonsock;
 
+       daemonsock.sun_family = AF_UNIX;
+       daemonsock.sun_path[sizeof(daemonsock.sun_path)-1] = 0;
+       strncpy(daemonsock.sun_path, SIMDAEMON_SOCK_PATH, sizeof(daemonsock.sun_path));
+
+       // Check simulator socket name length is valid
+       if (daemonsock.sun_path[sizeof(daemonsock.sun_path)-1] != 0) {
+               LOGE(TEEC_LIB, "Socket name too long: ", daemonsock.sun_path);
+               return -1;
+       }
+
        // Get socket decriptor
        if ((serverSocket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
                LOGE(TEEC_LIB, "No socket for simdaemon");
                return -1;
        }
 
-       daemonsock.sun_family = AF_UNIX;
-
-       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;
 
index 1d3e1e2610830c564787305901d7ff551daba73e..d82af162e04835487b50cbaf1532e7a650d14171 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_PATH));\r
+       bool ret = readPropertyFile(TEE_PROP_PATH);\r
        currentItr = propertiesMap.begin();\r
        return ret;\r
 }\r
index 44e5ac41abc29cc95fa049f7dcd781bb87bf1e0b..1ee9c874fcd22aaf85a4e7d4cfe18d6bd0b47871 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/mman.h>
 #include <time.h>
 #include <unistd.h>
+#include <config.h>
 #include "log.h"
 
 using namespace std;
@@ -48,7 +49,6 @@ using namespace std;
 #define PAGE_MASK               (~(PAGE_SIZE - 1))
 
 #define SHM_MAX_ID                     INT32_MAX
-#define SHM_NAME_PREFIX                "/teec_shm"
 
 map<uint32_t, registerItem> SharedMemoryMap::shmMap;
 
index 19c251c40247d999d38d9cb9a7d7148b53e1a16a..07404a1a674d4a725e2ff4d2e9ff80a3601c51a3 100644 (file)
 
 //this is ln -s to /usr/lib/tastore or /usr/lib64/tastore (see spec file)
 #define TEE_TASTORE_ROOT "/opt/tastore/"
+#define TEE_SS_ROOT "/opt/tastore/"
 
 #define SIMDAEMON_SOCK_PATH "/tmp/simdaemon"
 //TEEStub must have write access in this directory (creating socket per TA)
 #define TEE_TASOCK_ROOT "/tmp/"
 #define TEE_TALOG_ROOT "/tmp/"
 
-#define SHM_PATH "/tmp/shm"
+// from manpages: For portable use, a shared memory object
+// should be identified by a name of the form /somename
+#define SHM_NAME_PREFIX  "/teec_shm"
 
 #define TEE_PROP_PATH "/usr/bin/GPD_TEE_PROP"
 
-#define TEE_SS_ROOT "/tmp/tastore2/"
-
 #endif /* INCLUDE_CONFIG_H_ */
index 70dd2797492d342a9f607f80a0dd063d3571cfe4..a8bd820c67f09e789a67a17e18aa603b507214f2 100644 (file)
 /*-----------------------------------------------------------------------------
  *  Local functions
  *-----------------------------------------------------------------------------*/
-/**
- * Create shm file for shared memory implementation (IPC)
- */
-void initializeShm() {
-       LOGD(SIM_DAEMON, "Entry");
-       ::unlink(SHM_PATH);
-       int fd = creat(SHM_PATH, S_IRWXU);
-       if (-1 == fd) {
-               LOGE(SIM_DAEMON, "shm file creation failed");
-               exit(0);
-       }
-       close(fd);
-}
 
 /**
  * Starts the Simulator Daemon as server which listens for connection from
@@ -109,7 +96,6 @@ int main() {
        try {
                int sockFD = getSystemdSocket(SIMDAEMON_SOCK_PATH);
 
-               //initializeShm();
                if (sockFD > 0) {
                        LOGI(SIM_DAEMON, "Using existing systemd socket %d", sockFD);
                        SimulatorDaemonServer s(ioService::getInstance(), sockFD);
index 18910d7fd6fa552b2808739fd97cb81f302079d5..dbb6b1e6fa0b464638b4f81df7dd386c84086df5 100644 (file)
@@ -163,9 +163,8 @@ bool TABinaryManager::initTA(const string &uuid) {
        LOGD(SIM_DAEMON, "Entry");
 
        pthread_rwlock_wrlock(&binaryMapLock);
-       StructBinaryInfo value;
-       bool res = false;
        StructBinaryInfo info;
+       bool res = false;
 
        if (boost::filesystem::exists(TEE_TASTORE_ROOT + uuid)) {
                pthread_mutex_lock(&taLock);
@@ -207,31 +206,34 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) {
                myfile.close();
        }
 
+       boost::filesystem::path decName = info.imagePath + "_dec";
        // hash of Keydata is not required.
        string dec_command = "openssl enc " + cipher + " -d -nopad -nosalt -K " + secret
-               + " -in " + info.imagePath + " -out " + info.imagePath
-               + "_dec -iv 0000000000000000";
+               + " -in " + info.imagePath + " -out " + decName.string() +
+               + " -iv 0000000000000000";
        result = system(dec_command.c_str());
        if (result != 0) {
                LOGE(SIM_DAEMON, "Image decryption failed");
        }
 
-       string removeEncImage = "rm -f " + info.imagePath;
-       result = system(removeEncImage.c_str());
-       if (result != 0) {
-               LOGE(SIM_DAEMON, "Post decryption operations failed");
+       boost::system::error_code ec;
+       boost::filesystem::remove(boost::filesystem::path(info.imagePath), ec);
+       if (ec) {
+               LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", info.imagePath.c_str(),
+                       ec.message());
        }
 
-       string renameDecImage = "mv " + info.imagePath + "_dec " + info.imagePath;
-       result = system(renameDecImage.c_str());
-       if (result != 0) {
-               LOGE(SIM_DAEMON, "Post decryption operations failed");
+       boost::filesystem::rename(decName, boost::filesystem::path(info.imagePath), ec);
+       if (ec) {
+               LOGE(SIM_DAEMON, "Post decryption failed: rename %s -> %s : %s",
+                       decName.string().c_str(), info.imagePath.c_str(),
+                       ec.message());
        }
 
-       string removeKeyHash = "rm -f " + keyhashFilename;
-       result = system(removeKeyHash.c_str());
-       if (result != 0) {
-               LOGE(SIM_DAEMON, "Post decryption operations failed");
+       boost::filesystem::remove(boost::filesystem::path(keyhashFilename), ec);
+       if (ec) {
+               LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", keyhashFilename.c_str(),
+                       ec.message());
        }
 }
 
@@ -261,12 +263,15 @@ bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) {
                LOGD(SIM_DAEMON, "Decrypting");
                // 3. Decrypt image using secret value in manifest
                if (info.manifest.properties.extension.launchMode == "debug")
-                 decryptImage(info);
+                       decryptImage(info);
 
-               string s = "chmod +x " + info.imagePath;
-               int result = system(s.c_str());
+               struct stat st;
+               int result = stat(info.imagePath.c_str(), &st);
+               if (result == 0) {
+                       result = chmod(info.imagePath.c_str(), st.st_mode|S_IXUSR|S_IXGRP|S_IXOTH);
+               }
                if (result != 0) {
-                       LOGE(SIM_DAEMON, "Unpacking executable TA failed");
+                       LOGE(SIM_DAEMON, "Unpacking executable TA failed: %s", strerror(errno));
                }
 
                ret = true;