diff --git a/source/InterDeviceManager/Idm_msg_process.c b/source/InterDeviceManager/Idm_msg_process.c index 6cc17c8..1651e2a 100644 --- a/source/InterDeviceManager/Idm_msg_process.c +++ b/source/InterDeviceManager/Idm_msg_process.c @@ -498,17 +498,18 @@ char* IDM_Incoming_FT_Response(connection_info_t* conn_info,payload_t* payload) while(lengthenc.ssl != NULL){ - bytes = SSL_read(conn_info->enc.ssl, buf, total_bytes-bytes); + bytes = SSL_read(conn_info->enc.ssl, buf, total_bytes - length); } else{ CcspTraceError(("%s:%d ssl session is null\n",__FUNCTION__,__LINE__)); + remove(req->output_location); fclose(fptr); free(buf); free(req); return FT_ERROR; } #else - bytes = read( conn_info->conn , buf, total_bytes-bytes); + bytes = read( conn_info->conn , buf, total_bytes - length); #endif CcspTraceInfo(("bytes transfered : %d\n",bytes)); if(bytes > 0){ @@ -516,7 +517,9 @@ char* IDM_Incoming_FT_Response(connection_info_t* conn_info,payload_t* payload) length+=bytes; } else{ - CcspTraceError(("(%s:%d) Data encryption failed (Err: %d)\n", __FUNCTION__, __LINE__,bytes)); + CcspTraceError(("(%s:%d) Socket Read Error (bytes read: %d)\n", __FUNCTION__, __LINE__,bytes)); + // Exit from loop if socket is not usable or broken + break; } } if(buf){ @@ -524,6 +527,13 @@ char* IDM_Incoming_FT_Response(connection_info_t* conn_info,payload_t* payload) } } fclose(fptr); + if(length < total_bytes) + { + // we couldn't read full bytes + remove(req->output_location); + free(req); + return FT_ERROR; + } free(req); return FT_SUCCESS; } @@ -856,10 +866,12 @@ char* IDM_SFT_receive(connection_info_t* conn_info,void* payload) while(lengthenc.ssl != NULL){ - bytes = SSL_read(conn_info->enc.ssl, buf, total_bytes-bytes); + // read remaining bytes + bytes = SSL_read(conn_info->enc.ssl, buf, total_bytes - length); } else{ CcspTraceError(("%s:%d ssl session is null\n",__FUNCTION__,__LINE__)); + remove(Data->param_name); fclose(fptr); if(buf){ free(buf); @@ -867,7 +879,8 @@ char* IDM_SFT_receive(connection_info_t* conn_info,void* payload) return FT_ERROR; } #else - bytes = read( conn_info->conn , buf, total_bytes-bytes); + // read remaining bytes + bytes = read( conn_info->conn , buf, total_bytes - length); #endif CcspTraceInfo(("bytes transfered : %d\n",bytes)); if(bytes > 0){ @@ -875,7 +888,9 @@ char* IDM_SFT_receive(connection_info_t* conn_info,void* payload) length+=bytes; } else{ - CcspTraceError(("(%s:%d) Data encryption failed (Err: %d)\n", __FUNCTION__, __LINE__,bytes)); + CcspTraceError(("(%s:%d) Socket Read Error (bytes read: %d)\n", __FUNCTION__, __LINE__,bytes)); + // Exit from loop if socket is not usable or broken + break; } } if(buf){ @@ -890,6 +905,12 @@ char* IDM_SFT_receive(connection_info_t* conn_info,void* payload) IdmMgrDml_GetConfigData_release(pidmDmlInfo); return FT_ERROR; } + if(length < total_bytes) + { + // we couldn't read full bytes. Remove partially written file + remove(Data->param_name); + return FT_ERROR; + } return FT_SUCCESS; }