Fix TEEC operation preprocessing 65/158065/5
authorakoszewski <a.koszewski@samsung.com>
Fri, 27 Oct 2017 13:25:02 +0000 (15:25 +0200)
committerr.tyminski <r.tyminski@partner.samsung.com>
Tue, 14 Nov 2017 12:06:02 +0000 (13:06 +0100)
TEEC Operation arguments are now parsed properly.

Change-Id: I91a811158b118066a2377a1d47eec36b9e8e03ac

TEECLib/src/teec_api.c

index a3c0c71..d1f68c8 100644 (file)
@@ -267,6 +267,42 @@ static uint32_t checkContext(TEEC_Context *context)
        return found;
 }
 
+TEEC_Result tempSharedMemAllocate(TEEC_SharedMemory** tmpSharedMem, uint32_t type,
+                                                                 TEEC_Context* context, TEEC_Parameter param)
+{
+       TEEC_Result result;
+       *tmpSharedMem = (TEEC_SharedMemory *)OsaMalloc(sizeof(TEEC_SharedMemory));
+       (*tmpSharedMem)->size = param.tmpref.size;
+       (*tmpSharedMem)->buffer = param.tmpref.buffer;
+
+       (*tmpSharedMem)->flags = type & (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT);
+
+       result = TEEC_RegisterSharedMemory(context, *tmpSharedMem);
+
+       if (result != TEEC_SUCCESS) {
+               if (*tmpSharedMem) {
+                       OsaFree(*tmpSharedMem);
+                       *tmpSharedMem = NULL;
+               }
+               return result;
+       }
+
+       if (type & TEEC_MEMREF_TEMP_INPUT) {
+               memcpy(((TEEC_SharedMemoryImp *)(*tmpSharedMem)->imp)->allocPtr,
+               (*tmpSharedMem)->buffer, (*tmpSharedMem)->size);
+       }
+       return result;
+}
+
+void tempSharedMemoryDeallocate(TEEC_SharedMemory** tmpSharedMem)
+{
+       if (*tmpSharedMem) {
+               TEEC_ReleaseSharedMemory(*tmpSharedMem);
+               OsaFree(*tmpSharedMem);
+               *tmpSharedMem = NULL;
+       }
+}
+
 /*
  * ===  FUNCTION  ======================================================================
  *         Name:  preProcessOperation
@@ -339,38 +375,10 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                        op->paramTypes |= type << (8 * i);
 
                        if (!tmpSharedMem[i]) {
-                               tmpSharedMem[i] = (TEEC_SharedMemory *)OsaMalloc(
-                                                                         sizeof(TEEC_SharedMemory));
-                               tmpSharedMem[i]->size = operation->params[i].tmpref.size;
-                               tmpSharedMem[i]->buffer = operation->params[i].tmpref.buffer;
-
-                               if (type == TEEC_MEMREF_TEMP_INPUT)
-                                       tmpSharedMem[i]->flags = TEEC_MEM_INPUT;
-
-                               else if (type == TEEC_MEMREF_TEMP_OUTPUT)
-                                       tmpSharedMem[i]->flags = TEEC_MEM_OUTPUT;
-
-                               else if (type == TEEC_MEMREF_TEMP_INOUT)
-                                       tmpSharedMem[i]->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
-
-                               result = TEEC_RegisterSharedMemory(
-                                                        ((TEEC_SessionImp *)session->imp)->context, tmpSharedMem[i]);
-
-                               if (result != TEEC_SUCCESS) {
-                                       for (i = 0; i < 4; i++) {
-                                               if (tmpSharedMem[i]) {
-                                                       OsaFree(tmpSharedMem[i]);
-                                                       tmpSharedMem[i] = NULL;
-                                               }
-                                       }
-
-                                       return result;
-                               }
-
-                               if (type & TEEC_MEMREF_TEMP_INPUT) {
-                                       memcpy(((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp)->allocPtr,
-                                                  tmpSharedMem[i]->buffer, tmpSharedMem[i]->size);
-                               }
+                               result = tempSharedMemAllocate(&tmpSharedMem[i], type,
+                                       ((TEEC_SessionImp *)session->imp)->context, operation->params[i]);
+                               if (result != TEEC_SUCCESS)
+                                       goto cleanup;
                        }
 
                        op->params[i].mem.size = tmpSharedMem[i]->size;
@@ -380,25 +388,19 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                        break;
 
                case TEEC_MEMREF_WHOLE:
-                       op->paramTypes |= TEE_PARAM_TYPE_MEMREF_INOUT << (8 * i);
                        memref = &operation->params[i].memref;
 
                        if ((NULL == memref) || (NULL == memref->parent)
                                        || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp
                                                != context->imp)) {
-                               for (i = 0; i < 4; i++) {
-                                       if (tmpSharedMem[i]) {
-                                               TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
-                                               OsaFree(tmpSharedMem[i]);
-                                               tmpSharedMem[i] = NULL;
-                                       }
-                               }
-
                                LOGE(TEEC_LIB, "Bad parameters");
-                               return TEEC_ERROR_BAD_PARAMETERS;
+                               result = TEEC_ERROR_BAD_PARAMETERS;
+                               goto cleanup;
                        }
 
                        memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp;
+                       op->paramTypes |= (operation->params[i].memref.parent->flags |
+                                                               (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i);
                        op->params[i].mem.offset = 0;
                        op->params[i].mem.size = memref->parent->size;
                        op->params[i].mem.shmKey = memref_imp->shmKey;
@@ -411,23 +413,16 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                case TEEC_MEMREF_PARTIAL_INPUT:
                case TEEC_MEMREF_PARTIAL_OUTPUT:
                case TEEC_MEMREF_PARTIAL_INOUT:
-                       op->paramTypes |= (type + TEE_PARAM_TYPE_MEMREF_INPUT
-                                                          - TEEC_MEMREF_PARTIAL_INPUT) << (8 * i);
+                       op->paramTypes |= ((operation->params[i].memref.parent->flags & type) |
+                                                               (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i);
                        memref = &operation->params[i].memref;
 
                        if ((NULL == memref) || (NULL == memref->parent)
                                        || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp
                                                != context->imp)) {
-                               for (i = 0; i < 4; i++) {
-                                       if (tmpSharedMem[i]) {
-                                               TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
-                                               OsaFree(tmpSharedMem[i]);
-                                               tmpSharedMem[i] = NULL;
-                                       }
-                               }
-
                                LOGE(TEEC_LIB, "Bad parameters");
-                               return TEEC_ERROR_BAD_PARAMETERS;
+                               result = TEEC_ERROR_BAD_PARAMETERS;
+                               goto cleanup;
                        }
 
                        memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp;
@@ -446,19 +441,18 @@ static TEEC_Result preProcessOperation(TEEC_Session *session,
                        break;
 
                default:
-                       for (i = 0; i < 4; i++) {
-                               if (tmpSharedMem[i]) {
-                                       TEEC_ReleaseSharedMemory(tmpSharedMem[i]);
-                                       OsaFree(tmpSharedMem[i]);
-                                       tmpSharedMem[i] = NULL;
-                               }
-                       }
-
-                       return TEEC_ERROR_BAD_PARAMETERS;
+                       result = TEEC_ERROR_BAD_PARAMETERS;
+                       goto cleanup;
                }
        }
 
-       return TEEC_SUCCESS;
+       return result;
+
+cleanup:
+       for (i = 0; i < 4; i++) {
+               tempSharedMemoryDeallocate(&tmpSharedMem[i]);
+       }
+       return result;
 }
 
 /*