svace fixes 87/213987/1
authorAleksey Volkov <a.volkov@samsung.com>
Thu, 5 Sep 2019 17:22:22 +0000 (20:22 +0300)
committerSudipto <sudipto.bal@samsung.com>
Tue, 17 Sep 2019 05:40:40 +0000 (11:10 +0530)
https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/commit/dfdaf037df98fbd0c8b659fccdfe0384bb466547
(cherry-picked from dfdaf037df98fbd0c8b659fccdfe0384bb466547)

Change-Id: Ic0361d29c7bc8f0aa4708ec103728e968b430a42
Signed-off-by: Aleksey Volkov <a.volkov@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
12 files changed:
resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c
resource/csdk/security/provisioning/src/multipleownershiptransfermanager.c
resource/csdk/security/provisioning/src/ownershiptransfermanager.c
resource/csdk/security/provisioning/src/pmutility.c
resource/csdk/security/provisioning/src/provisioningdatabasemanager.c
resource/csdk/security/provisioning/src/secureresourceprovider.c
resource/csdk/security/src/amaclresource.c
resource/csdk/security/src/credresource.c
resource/csdk/security/src/crlresource.c
resource/csdk/security/src/oxmpincommon.c
resource/csdk/security/src/pconfresource.c
resource/csdk/security/src/psinterface.c

index 966b2a4..ce4765a 100644 (file)
@@ -928,6 +928,10 @@ static int ParseChain(mbedtls_x509_crt * crt, unsigned char * buf, size_t bufLen
             else
             {
                 unsigned char * lastCert = (unsigned char *)OICMalloc((len + 1) * sizeof(unsigned char));
+                if (lastCert == NULL)
+                {
+                    goto exit;
+                }
                 memcpy(lastCert, buf + pos, len);
                 lastCert[len] = 0x00;
                 ret = mbedtls_x509_crt_parse(crt, lastCert, len + 1);
index 962e651..07b492e 100644 (file)
@@ -904,6 +904,7 @@ static OCStackResult PostSubOwnerCredential(OTMContext_t* motCtx)
             newCredential.eownerID = OICCalloc(1, sizeof(OicUuid_t));
             if(NULL == newCredential.eownerID)
             {
+                OICFree(secPayload);
                 return OC_STACK_NO_MEMORY;
             }
         }
@@ -1089,6 +1090,7 @@ static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
     if(OC_STACK_OK != res)
     {
         OIC_LOG(ERROR, TAG, "Failed to convert UUID to str");
+        OICFree(strUuid);
         SetMOTResult(motCtx, res);
         return res;
     }
@@ -1104,6 +1106,7 @@ static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
             OIC_LOG(ERROR, TAG, "Internal error in PDMDeleteDevice");
             OICFree(strUuid);
             SetMOTResult(motCtx, res);
+            return res;
         }
     }
     else if(PDM_DEVICE_INIT == deviceState)
index 986cadb..4a9021a 100644 (file)
@@ -853,7 +853,12 @@ static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
 
         OICFree( cred->privateData.data );
         cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
-        VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
+        if (cred->privateData.data == NULL)
+            {
+                OICFree(b64Buf);
+                res = OC_STACK_ERROR;
+                goto exit;
+            }
 
         strncpy((char*)(cred->privateData.data), b64Buf, outSize);
         cred->privateData.data[outSize] = '\0';
@@ -1585,6 +1590,7 @@ static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
     if(!ownerCredential)
     {
         OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
+        OICFree(secPayload);
         return OC_STACK_NO_RESOURCE;
     }
 
index 4c72d5f..444ec43 100644 (file)
@@ -488,6 +488,7 @@ bool PMGenerateQuery(bool isSecure,
     {
         case CT_ADAPTER_TCP:
             prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
+            /*FALLTHROUGH*/
         case CT_ADAPTER_IP:
             switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
             {
index c774b76..93ce7de 100644 (file)
@@ -323,7 +323,7 @@ OCStackResult OTMStart(const OicUuid_t *uuid, const OicUuid_t *owner)
     sqlite3_finalize(stmt);
 
     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
-    return OC_STACK_OK;
+    return ret;
 }
 
 OCStackResult OTMSetState(const OicUuid_t *uuid, int state)
@@ -393,12 +393,11 @@ OCStackResult OTMGetState(const OicUuid_t *uuid, int *state)
     res = sqlite3_bind_text(stmt, 1, uuidStr, strlen(uuidStr), SQLITE_TRANSIENT);
     PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
 
-    while (SQLITE_ROW == sqlite3_step(stmt))
+    if (SQLITE_ROW == sqlite3_step(stmt))
     {
         *state = sqlite3_column_int(stmt, 1);
         OIC_LOG_V(DEBUG, TAG, "%s state is %d", uuidStr, *state);
         ret = OC_STACK_OK;
-        break;
     }
 
     OICFree(uuidStr);
@@ -474,7 +473,7 @@ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id)
     res = sqlite3_prepare_v2(g_db, sqlStat, (int)sizeof(sqlStat), &stmt, 0);
     PDM_VERIFY_SQLITE_OK(TAG, res, stmt, ERROR, OC_STACK_ERROR);
 
-    while (SQLITE_ROW == sqlite3_step(stmt))
+    if (SQLITE_ROW == sqlite3_step(stmt))
     {
         int tempId = sqlite3_column_int(stmt, PDM_FIRST_INDEX);
         OIC_LOG_V(DEBUG, TAG, "ID is %d", tempId);
@@ -841,7 +840,7 @@ static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result)
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id);
     PDM_VERIFY_SQLITE_OK(TAG, res, stmt, ERROR, OC_STACK_ERROR);
 
-    while (SQLITE_ROW == sqlite3_step(stmt))
+    if (SQLITE_ROW == sqlite3_step(stmt))
     {
         const void *ptr = sqlite3_column_blob(stmt, PDM_FIRST_INDEX);
         memcpy(uid, ptr, sizeof(OicUuid_t));
index 081cccd..b93fc0a 100644 (file)
@@ -586,7 +586,12 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
     }
 
     cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
-    VERIFY_NON_NULL_RET(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
+    if (cred->credUsage == NULL)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s cant alloc credUsage", __func__);
+        OICFree(cred);
+        return OC_STACK_NO_MEMORY;
+    }
     OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA);
 
     cred->credType = SIGNED_ASYMMETRIC_KEY;
@@ -657,6 +662,12 @@ OCStackResult SRPSaveOwnCertChain(OicSecKey_t * cert, OicSecKey_t * key, uint16_
 
     cred->credUsage= (char *)OICCalloc(1, strlen(PRIMARY_CERT)+1 );
     VERIFY_NON_NULL_RET(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
+    if (cred->credUsage == NULL)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s cant alloc credUsage", __func__);
+        OICFree(cred);
+        return OC_STACK_NO_MEMORY;
+    }
     OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
 
     cred->credType = SIGNED_ASYMMETRIC_KEY;
@@ -671,6 +682,12 @@ OCStackResult SRPSaveOwnCertChain(OicSecKey_t * cert, OicSecKey_t * key, uint16_
     OicSecKey_t *privateData = &cred->privateData;
     privateData->data = (uint8_t *)OICCalloc(1, key->len);
     VERIFY_NON_NULL_RET(TAG, privateData->data, ERROR, OC_STACK_NO_MEMORY);
+    if (privateData->data == NULL)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s cant alloc publicData->data", __func__);
+        OICFree(publicData->data);
+        return OC_STACK_NO_MEMORY;
+    }
     memcpy(privateData->data, key->data, key->len);
     privateData->len = key->len;
     privateData->encoding = key->encoding;
@@ -1179,6 +1196,7 @@ static OCStackResult SendDeleteCredentialRequest(void* ctx,
     if (OC_STACK_OK != result)
     {
         OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
+        OICFree(subID);
         return OC_STACK_ERROR;
     }
 
@@ -1254,6 +1272,7 @@ static OCStackResult SendDeleteACLRequest(void* ctx,
                                                      destDev->endpoint.addr);
     if (OC_STACK_OK != result)
     {
+        OICFree(subID);
         OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
         return OC_STACK_ERROR;
     }
index ca0be5c..b99b967 100644 (file)
@@ -255,6 +255,7 @@ OCStackResult CBORPayloadToAmacl(const uint8_t *cborPayload, size_t size,
 
     cbor_parser_init(cborPayload, size, 0, &parser, &amaclCbor);
     OicSecAmacl_t *headAmacl = (OicSecAmacl_t *)OICCalloc(1, sizeof(OicSecAmacl_t));
+    VERIFY_NON_NULL(TAG, headAmacl, ERROR);
 
     CborValue amaclMap = { .parser = NULL };
     cborFindResult = cbor_value_enter_container(&amaclCbor, &amaclMap);
index 0bdaa5f..40aab22 100644 (file)
@@ -2619,6 +2619,7 @@ int32_t GetDtlsPskCredentials(CADtlsPskCredType_t type,
                                     if(B64_OK != b64Decode((char*)wildCardCred->privateData.data, wildCardCred->privateData.len, pinBuffer, pinBufSize, &pinLength))
                                     {
                                         OIC_LOG (ERROR, TAG, "Failed to base64 decoding.");
+                                        OICFree(pinBuffer);
                                         return ret;
                                     }
                                 }
index 5a0397a..222bb0b 100644 (file)
@@ -761,7 +761,10 @@ uint8_t *GetCrl()
     if (OC_STACK_OK == GetSecureVirtualDatabaseFromPS(OIC_CBOR_CRL_NAME, &data, &size) && data &&
         OC_STACK_OK == CBORPayloadToCrl(data, size, &crl))
     {
-        return crl->CrlData.data;
+        uint8_t *res = OICMalloc(crl->CrlData.len);
+        memcpy(res, crl->CrlData.data, crl->CrlData.len);
+        DeleteCrl(crl);
+        return res;
     }
     return NULL;
 }
index 4908451..9725b74 100644 (file)
@@ -536,6 +536,7 @@ int32_t GetDtlsPskForPreconfPinOxm( CADtlsPskCredType_t type,
                             if(B64_OK != b64Decode((char*)cred->privateData.data, cred->privateData.len, pinBuffer, pinBufSize, &pinLength))
                             {
                                 OIC_LOG (ERROR, TAG, "Failed to base64 decoding.");
+                                OICFree(pinBuffer);
                                 return ret;
                             }
                         }
@@ -636,6 +637,7 @@ int32_t GetDtlsPskForMotPreconfPinOxm( CADtlsPskCredType_t type,
                             if(B64_OK != b64Decode((char*)cred->privateData.data, cred->privateData.len, pinBuffer, pinBufSize, &pinLength))
                             {
                                 OIC_LOG (ERROR, TAG, "Failed to base64 decoding.");
+                                OICFree(pinBuffer);
                                 return ret;
                             }
                         }
index 3404a16..36b5295 100644 (file)
@@ -525,7 +525,7 @@ OCStackResult CBORPayloadToPconf(const uint8_t *cborPayload, size_t size, OicSec
                 uint8_t *pin = NULL;
                 cborFindResult = cbor_value_dup_byte_string(&pconfMap, &pin, &len, NULL);
                 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed to get value");
-                memcpy(pconf->pin.val, pin, len);
+                memcpy(pconf->pin.val, pin, len > 8 ? 8 : len);
                 OICFree(pin);
             }
 
index c24f05a..baba287 100644 (file)
@@ -213,6 +213,7 @@ static OCStackResult getPlaintextFromDB(const OCPersistentStorage *ps, uint8_t *
     fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
     if (NULL == fp)
     {
+        OICFree(plaintext);
         OIC_LOG(ERROR, TAG, "ps->open() Failed");
         return OC_STACK_ERROR;
     }