From 765482fbf7fad4b1af7507849581617f710963a6 Mon Sep 17 00:00:00 2001 From: Robert Chudek Date: Tue, 12 Feb 2019 11:28:29 +0100 Subject: [PATCH] Add CAcloseSslConnectionFreeEndpoint method [Model] IoTivity [BinType] AP [Customer] IoT-Core [Issue#] CONPRO-1392 [Request] Robert Chudek / r.chudek [Occurrence Version] n/a [Problem] After calling CAcloseSslConnection there was a memory leak. [Cause & Measure] Cause: After calling CAcloseSslConnection endpoint wasn't freed which led to memory leak. [Checking Method] n/a [Team] IoT Connection [Developer] Robert Chudek / r.chudek [Solution company] Samsung [Change Type] bug fix https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/414 (cherry picked from 18a20af57324bc3d816a862e3aa5194712d2e237) Change-Id: I283055bea913401c6c23953857b7ccfe5db3581b Signed-off-by: Abhishek Sansanwal --- .../csdk/connectivity/api/casecurityinterface.h | 11 ++++++ .../csdk/connectivity/inc/ca_adapter_net_ssl.h | 11 ++++++ .../src/adapter_util/ca_adapter_net_ssl.c | 13 ++++++ .../connectivity/src/bt_le_adapter/caleadapter.c | 46 ++++++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/resource/csdk/connectivity/api/casecurityinterface.h b/resource/csdk/connectivity/api/casecurityinterface.h index c891460..117ab18 100644 --- a/resource/csdk/connectivity/api/casecurityinterface.h +++ b/resource/csdk/connectivity/api/casecurityinterface.h @@ -324,6 +324,17 @@ CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint); CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint); /** + * Close the DTLS session and free endpoint. + * + * @param[in] endpoint information of network address; + * CAcloseSslConnectionWrapper takes ownership of endpoint + * + * @retval ::CA_STATUS_OK Successful. + * @retval ::CA_STATUS_FAILED Operation failed. + */ +CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint); + +/** * Close the TLS session using UUID * * @param[in] identity UUID of target device diff --git a/resource/csdk/connectivity/inc/ca_adapter_net_ssl.h b/resource/csdk/connectivity/inc/ca_adapter_net_ssl.h index cc9e8d5..4dd2510 100644 --- a/resource/csdk/connectivity/inc/ca_adapter_net_ssl.h +++ b/resource/csdk/connectivity/inc/ca_adapter_net_ssl.h @@ -90,6 +90,17 @@ void CAsetSslCredentialsCallback(CAgetPskCredentialsHandler credCallback); CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint); /** + * Close the DTLS session and free endpoint. + * + * @param[in] endpoint information of network address; + * CAcloseSslConnectionWrapper takes ownership of endpoint + * + * @retval ::CA_STATUS_OK Successful. + * @retval ::CA_STATUS_FAILED Operation failed. + */ +CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint); + +/** * initialize mbedTLS library and other necessary initialization. * * @return 0 on success otherwise a positive error value. diff --git a/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c b/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c index 187d23c..2553e5b 100644 --- a/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c +++ b/resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c @@ -1505,6 +1505,19 @@ CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint) return CA_STATUS_OK; } +CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint) +{ + OIC_LOG_V(INFO, NET_SSL_TAG, "In %s", __func__); + VERIFY_NON_NULL_RET(endpoint, NET_SSL_TAG, "Param endpoint is NULL" , CA_STATUS_INVALID_PARAM); + + CAResult_t ret = CAcloseSslConnection(endpoint); + OICFree(endpoint); + + OIC_LOG_V(INFO, NET_SSL_TAG, "Out %s", __func__); + return ret; +} + + CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength) { OIC_LOG_V(DEBUG, NET_SSL_TAG, "In %s", __func__); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 78e4c12..93c8bde 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -3194,8 +3194,54 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha #endif #ifdef __WITH_DTLS__ +#ifndef SINGLE_THREAD + // CAcloseSslConnection returns CAResult_t instead of void*, but the size is the same and crash shouldn't occur + pthread_t ccThread; + pthread_attr_t attr; + int initAttrRes = -1; + int pthreadCreateRes = -1; + int detachStatusRes = -1; + int memoryAllocationRes = -1; + + do + { + initAttrRes = pthread_attr_init(&attr); + if (initAttrRes != 0) + { + break; + } + CAEndpoint_t *localEndpointCpyPtr = OICMalloc(sizeof(CAEndpoint_t)); + + if(NULL == localEndpointCpyPtr) + { + memoryAllocationRes = -1; + break; + } + else + { + memoryAllocationRes = 0; + } + + (*localEndpointCpyPtr) = localEndpoint; + // this piece of code is reached on the main thread + // CAcloseSslConnection might wait for too long (network + mutexes) and watchdog might kill it + // Asynchronous call protects this function from watchdog + pthreadCreateRes = pthread_create(&ccThread, &attr, (void *(*)(void*))&CAcloseSslConnectionFreeEndpoint, (void*)localEndpointCpyPtr); + + if (pthreadCreateRes != 0) + { + break; + } + detachStatusRes = pthread_detach(ccThread); + }while (0); + + // regardless of CAcloseSslConnection result, the function will continue and g_connectionCallback will be called + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "CAcloseSslConnection pthread_init [%d], mem_alloc [%d] pthread_create [%d], pthread_detach [%d]", + initAttrRes, memoryAllocationRes, pthreadCreateRes, detachStatusRes); +#else CAcloseSslConnection(&localEndpoint); #endif +#endif } if (g_connectionCallback) -- 2.7.4