From 6eff36d91ed505bb560f86f1c1820c31b816d84a Mon Sep 17 00:00:00 2001 From: liuzhongming Date: Tue, 29 Jul 2025 15:24:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=87=AA=E6=A3=80=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20Signed-off-by:=20liuzhongming=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/dfx/standard/dm_hidumper.h | 2 ++ common/src/dfx/standard/dm_hidumper.cpp | 3 ++- .../include/ipc/lite/ipc_client_manager.h | 1 + .../include/ipc/standard/dm_service_load.h | 3 +++ .../src/ipc/lite/ipc_client_manager.cpp | 3 +++ .../src/ipc/standard/dm_service_load.cpp | 12 +++++---- interfaces/kits/js/src/dm_native_event.cpp | 13 ++++++---- interfaces/kits/js4.0/src/dm_native_event.cpp | 13 ++++++---- .../src/authentication_v2/auth_manager.cpp | 7 ++---- .../dm_auth_message_processor.cpp | 12 ++++++--- .../src/device_manager_service_impl.cpp | 23 +++++++++-------- .../src/advertise/advertise_manager.cpp | 5 ++++ .../service/src/device_manager_service.cpp | 3 ++- .../src/devicenamemgr/device_name_manager.cpp | 5 ++++ .../src/discovery/discovery_manager.cpp | 25 +++++++++++++++++++ .../src/ipc/lite/ipc_server_listenermgr.cpp | 5 ++++ .../dm_package_common_event.cpp | 3 ++- .../src/relationshipsyncmgr/dm_comm_tool.cpp | 8 +++--- .../relationship_sync_mgr.cpp | 5 ++++ .../standard/dm_distributed_hardware_load.h | 2 +- .../standard/dm_distributed_hardware_load.cpp | 6 ++--- 21 files changed, 114 insertions(+), 45 deletions(-) diff --git a/common/include/dfx/standard/dm_hidumper.h b/common/include/dfx/standard/dm_hidumper.h index 4bba26763..d09b526d2 100644 --- a/common/include/dfx/standard/dm_hidumper.h +++ b/common/include/dfx/standard/dm_hidumper.h @@ -17,6 +17,7 @@ #define OHOS_DM_HIDUMPER_H #include +#include #include #include @@ -41,6 +42,7 @@ private: std::string GetDeviceType(int32_t deviceTypeId); private: + std::mutex locks_; std::vector nodeInfos_; }; } // namespace DistributedHardware diff --git a/common/src/dfx/standard/dm_hidumper.cpp b/common/src/dfx/standard/dm_hidumper.cpp index 62f18202d..925eb227c 100644 --- a/common/src/dfx/standard/dm_hidumper.cpp +++ b/common/src/dfx/standard/dm_hidumper.cpp @@ -70,6 +70,7 @@ int32_t HiDumpHelper::HiDump(const std::vector& args, std::string & void HiDumpHelper::SetNodeInfo(const DmDeviceInfo& deviceInfo) { LOGI("HiDumpHelper::SetNodeInfo"); + std::lock_guard autoLock(locks_); if (nodeInfos_.size() >= MAX_CONTAINER_SIZE) { LOGE("nodeInfos_ size is more than max size"); return; @@ -102,7 +103,7 @@ int32_t HiDumpHelper::ShowAllLoadTrustedList(std::string &result) { LOGI("dump all trusted device List"); int32_t ret = DM_OK; - + std::lock_guard autoLock(locks_); if (nodeInfos_.size() == 0) { LOGE("dump trusted device list is empty"); result.append("dump trusted device list is empty"); diff --git a/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h index 2d8bf57b2..21c863c4a 100644 --- a/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h +++ b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h @@ -54,6 +54,7 @@ private: private: IpcClientServerProxy serverProxy_; + std::mutex lock_; std::set packageInitSet_; }; } // namespace DistributedHardware diff --git a/interfaces/inner_kits/native_cpp/include/ipc/standard/dm_service_load.h b/interfaces/inner_kits/native_cpp/include/ipc/standard/dm_service_load.h index beb819b5f..0acb282bf 100644 --- a/interfaces/inner_kits/native_cpp/include/ipc/standard/dm_service_load.h +++ b/interfaces/inner_kits/native_cpp/include/ipc/standard/dm_service_load.h @@ -16,6 +16,8 @@ #ifndef OHOS_DM_SA_LOAD_H #define OHOS_DM_SA_LOAD_H +#include + #include "dm_single_instance.h" #include "iremote_object.h" #include "system_ability_load_callback_stub.h" @@ -35,6 +37,7 @@ public: void SetLoadFinish(void); private: std::atomic isDMServiceLoading_ = false; + std::mutex lock_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp index f7731d277..26efa44cf 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp @@ -57,6 +57,7 @@ int32_t IpcClientManager::Init(const std::string &pkgName) ret); return ret; } + std::lock_guard autoLock(lock_); packageInitSet_.emplace(pkgName); return DM_OK; } @@ -81,6 +82,7 @@ int32_t IpcClientManager::UnInit(const std::string &pkgName) ret); return ret; } + std::lock_guard autoLock(lock_); packageInitSet_.erase(pkgName); LOGI("UnInitDeviceManager SUCCESS"); return DM_OK; @@ -100,6 +102,7 @@ int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr r bool IpcClientManager::IsInit(const std::string &pkgName) { + std::lock_guard autoLock(lock_); for (auto &iter : packageInitSet_) { size_t len = iter.size(); if (len > pkgName.size()) { diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/dm_service_load.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/dm_service_load.cpp index baf59b9a6..d1b671d8b 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/dm_service_load.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/dm_service_load.cpp @@ -27,22 +27,23 @@ DM_IMPLEMENT_SINGLE_INSTANCE(DmServiceLoad); int32_t DmServiceLoad::LoadDMService(void) { - if (isDMServiceLoading_) { + std::lock_guard autoLock(lock_); + if (isDMServiceLoading_.load()) { LOGI("DM service is loading."); return DM_OK; } LOGI("start"); - isDMServiceLoading_ = true; + isDMServiceLoading_.store(true); sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr == nullptr) { - isDMServiceLoading_ = false; + isDMServiceLoading_.store(false); LOGE("failed to get system ability mgr."); return ERR_DM_POINT_NULL; } sptr dmLoadCallback_(new DMLoadCallback()); int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, dmLoadCallback_); if (ret != DM_OK) { - isDMServiceLoading_ = false; + isDMServiceLoading_.store(false); LOGE("Failed to Load DM service, ret code:%{public}d", ret); return ret; } @@ -51,7 +52,8 @@ int32_t DmServiceLoad::LoadDMService(void) void DmServiceLoad::SetLoadFinish(void) { - isDMServiceLoading_ = false; + std::lock_guard autoLock(lock_); + isDMServiceLoading_.store(false); } void DMLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, diff --git a/interfaces/kits/js/src/dm_native_event.cpp b/interfaces/kits/js/src/dm_native_event.cpp index e7a02abbd..bc26da6b8 100644 --- a/interfaces/kits/js/src/dm_native_event.cpp +++ b/interfaces/kits/js/src/dm_native_event.cpp @@ -28,13 +28,16 @@ DmNativeEvent::DmNativeEvent(napi_env env, napi_value thisVar) DmNativeEvent::~DmNativeEvent() { - for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) { - auto listener = iter->second; - if (listener != nullptr) { - napi_delete_reference(env_, listener->handlerRef); + { + std::lock_guard autoLock(lock_); + for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) { + auto listener = iter->second; + if (listener != nullptr) { + napi_delete_reference(env_, listener->handlerRef); + } } + eventMap_.clear(); } - eventMap_.clear(); napi_delete_reference(env_, thisVarRef_); } diff --git a/interfaces/kits/js4.0/src/dm_native_event.cpp b/interfaces/kits/js4.0/src/dm_native_event.cpp index d60a6f090..be78cd198 100644 --- a/interfaces/kits/js4.0/src/dm_native_event.cpp +++ b/interfaces/kits/js4.0/src/dm_native_event.cpp @@ -28,12 +28,15 @@ DmNativeEvent::DmNativeEvent(napi_env env, napi_value thisVar) DmNativeEvent::~DmNativeEvent() { - for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) { - auto listener = iter->second; - CHECK_NULL_VOID(listener); - napi_delete_reference(env_, listener->handlerRef); + { + std::lock_guard autoLock(lock_); + for (auto iter = eventMap_.begin(); iter != eventMap_.end(); iter++) { + auto listener = iter->second; + CHECK_NULL_VOID(listener); + napi_delete_reference(env_, listener->handlerRef); + } + eventMap_.clear(); } - eventMap_.clear(); napi_delete_reference(env_, thisVarRef_); } diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 4a33f36c8..9e91f2f5d 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -543,7 +543,7 @@ int32_t AuthManager::GetBindLevel(int32_t bindLevel) void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { - LOGI("Get auth param with pkgName %{public}s and extra %{public}s.", pkgName.c_str(), extra.c_str()); + LOGI("Get auth param with pkgName %{public}s.", pkgName.c_str()); char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); context_->accesser.deviceId = std::string(localDeviceId); @@ -597,7 +597,7 @@ void AuthManager::InitAuthState(const std::string &pkgName, int32_t authType, int32_t AuthManager::AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { - LOGI("AuthManager::AuthenticateDevice start auth type %{public}d, extra %{public}s.", authType, extra.c_str()); + LOGI("AuthManager::AuthenticateDevice start auth type %{public}d.", authType); SetAuthType(authType); context_->processInfo.pkgName = pkgName; GetBindCallerInfo(); @@ -660,9 +660,6 @@ int32_t AuthManager::BindTarget(const std::string &pkgName, const PeerTargetId & { int ret = DM_OK; LOGI("AuthManager::BindTarget start. pkgName: %{public}s", pkgName.c_str()); - for (auto iter = bindParam.begin(); iter != bindParam.end(); iter++) { - LOGI("AuthManager::BindTarget para: %{public}s : %{public}s ", iter->first.c_str(), iter->second.c_str()); - } struct RadarInfo info = { .funcName = "AuthenticateDevice", diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index 9884946db..5a2a9966b 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -109,6 +109,7 @@ const char* TAG_DEVICE_TYPE = "DEVICETYPE"; constexpr int32_t DM_ULTRASONIC_FORWARD = 0; constexpr int32_t DM_ULTRASONIC_REVERSE = 1; constexpr uint32_t MAX_SESSION_KEY_LENGTH = 512; +constexpr uint32_t MAX_MESSAGE_LENGTH = 64 * 1024 * 1024; void ParseDmAccessToSync(const std::string &jsonString, DmAccess &access, bool isUseDeviceFullName) { @@ -688,7 +689,6 @@ int32_t DmAuthMessageProcessor::ParseMessageRspCredExchange(const JsonObject &js LOGE("DmAuthMessageProcessor::ParseMessageRspCredExchange error, decrypt data failed."); return ERR_DM_FAILED; } - LOGI("plainText=%{public}s", GetAnonyJsonString(plainText).c_str()); JsonObject jsonData(plainText); @@ -1942,7 +1942,10 @@ std::string DmAuthMessageProcessor::Base64Encode(std::string &inputStr) // Convert input string to binary const unsigned char* src = reinterpret_cast(inputStr.data()); size_t srcLen = inputStr.size(); - + if (srcLen >= MAX_MESSAGE_LENGTH) { + LOGE("inputStr too long"); + return ""; + } // Calculate the maximum length after base64 encoding size_t maxEncodeLen = ((srcLen + 2) / 3) * 4 + 1; std::vector buffer(maxEncodeLen); @@ -1962,7 +1965,10 @@ std::string DmAuthMessageProcessor::Base64Decode(std::string &inputStr) // Convert input string to binary const unsigned char* src = reinterpret_cast(inputStr.data()); size_t srcLen = inputStr.size(); - + if (srcLen >= MAX_MESSAGE_LENGTH) { + LOGE("inputStr too long"); + return ""; + } // Calculate the maximum length after base64 encoding size_t maxEncodeLen = (srcLen / 4) * 3 + 1; std::vector buffer(maxEncodeLen); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 75dfdec64..f3191701a 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -233,8 +233,8 @@ void DeviceManagerServiceImpl::ImportAuthCodeToConfig(std::shared_ptr authMgr = nullptr; // Create a new auth_mgr, create authMgr if (isSrcSide) { @@ -264,7 +264,7 @@ int32_t DeviceManagerServiceImpl::InitNewProtocolAuthMgr(bool isSrcSide, uint64_ int32_t DeviceManagerServiceImpl::InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName) { - LOGI("tokenId: %{public}" PRIu64 ", pkgname:%{public}s", tokenId, pkgName.c_str()); + LOGI("tokenId: %{public}s, pkgname:%{public}s", GetAnonyInt32(tokenId).c_str(), pkgName.c_str()); if (authMgr_ == nullptr) { CreateGlobalClassicalAuthMgr(); } @@ -969,7 +969,8 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr uint64_t tokenId = 0; int32_t ret = GetLogicalIdAndTokenIdBySessionId(logicalSessionId, tokenId, sessionId); if (ret != DM_OK) { - LOGE("failed, logicalSessionId: %{public}" PRIu64 ", tokenId: %{public}" PRIu64 "", logicalSessionId, tokenId); + LOGE("failed, logicalSessionId: %{public}" PRIu64 ", tokenId: %{public}s", logicalSessionId, + GetAnonyInt32(tokenId).c_str()); return ret; } std::string pkgName; @@ -1779,7 +1780,7 @@ void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::strin CleanAuthMgrByLogicalSessionId(logicalSessionId); OnAuthResultAndOnBindResult(processInfo, targetId, targetIdTmp.deviceId, ret); } - LOGI("end, tokenId %{public}" PRId64".", tokenId); + LOGI("end, tokenId %{public}s.", GetAnonyInt32(tokenId).c_str()); return; } @@ -1979,7 +1980,7 @@ void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEvent std::lock_guard lock(authMgrMapMtx_); for (auto& pair : authMgrMap_) { if (pair.second != nullptr) { - LOGI("tokenId: %{public}" PRId64 ".", pair.first); + LOGI("tokenId: %{public}s.", GetAnonyInt32(pair.first).c_str()); pair.second->OnScreenLocked(); } } @@ -2147,7 +2148,7 @@ void DeviceManagerServiceImpl::HandleDevUnBindEvent(int32_t remoteUserId, const void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId) { - LOGI("tokenId = %{public}d.", tokenId); + LOGI("tokenId = %{public}s.", GetAnonyInt32(tokenId).c_str()); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); @@ -2219,8 +2220,8 @@ void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const void DeviceManagerServiceImpl::HandleServiceUnBindEvent(int32_t userId, const std::string &remoteUdid, int32_t remoteTokenId) { - LOGI("remoteTokenId = %{public}d, userId: %{public}d, remoteUdid: %{public}s.", - remoteTokenId, userId, GetAnonyString(remoteUdid).c_str()); + LOGI("remoteTokenId = %{public}s, userId: %{public}d, remoteUdid: %{public}s.", + GetAnonyInt32(remoteTokenId).c_str(), userId, GetAnonyString(remoteUdid).c_str()); char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); std::string localUdid = std::string(localUdidTemp); @@ -2983,8 +2984,8 @@ void DeviceManagerServiceImpl::GetBindCallerInfo(DmBindCallerInfo &bindCallerInf bindCallerInfo.hostPkgLabel = hostPkgLabel; bindCallerInfo.processName = processName; bindCallerInfo.isSystemSA = isSystemSA; - LOGI("userId %{public}d, tokenId %{public}d, bindLevel %{public}d, bundleName %{public}s, hostPkgLabel %{public}s," - "processName %{public}s, isSystemSA %{public}d", userId, callingTokenId, bindLevel, + LOGI("userId %{public}d, tokenId %{public}s, bindLevel %{public}d, bundleName %{public}s, hostPkgLabel %{public}s," + "processName %{public}s, isSystemSA %{public}d", userId, GetAnonyInt32(callingTokenId).c_str(), bindLevel, GetAnonyString(bundleName).c_str(), GetAnonyString(hostPkgLabel).c_str(), GetAnonyString(processName).c_str(), isSystemSA); } diff --git a/services/service/src/advertise/advertise_manager.cpp b/services/service/src/advertise/advertise_manager.cpp index 769ba0ba5..66c57ea9a 100644 --- a/services/service/src/advertise/advertise_manager.cpp +++ b/services/service/src/advertise/advertise_manager.cpp @@ -28,6 +28,7 @@ const std::string AUTO_STOP_ADVERTISE_TASK = "AutoStopAdvertisingTask"; const int32_t DM_MIN_RANDOM = 1; const int32_t DM_MAX_RANDOM = INT32_MAX; const int32_t DM_INVALID_FLAG_ID = 0; +constexpr uint32_t MAX_PUBLISH_LEN = 1000; AdvertiseManager::AdvertiseManager(std::shared_ptr softbusListener) : softbusListener_(softbusListener) { @@ -153,6 +154,10 @@ int32_t AdvertiseManager::GenInnerPublishId(const std::string &pkgName, int32_t int32_t tempPublishId = DM_INVALID_FLAG_ID; { std::lock_guard autoLock(pubMapLock_); + if (pkgName2PubIdMap_.size() >= MAX_PUBLISH_LEN || publishIdSet_.size() >= MAX_PUBLISH_LEN) { + LOGE("too many publishId"); + return tempPublishId; + } if (pkgName2PubIdMap_[pkgName].find(publishId) != pkgName2PubIdMap_[pkgName].end()) { return pkgName2PubIdMap_[pkgName][publishId]; } diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index ea4524253..5fab8efdd 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -3093,7 +3093,8 @@ DM_EXPORT int32_t DeviceManagerService::GetUdidHashByAnoyDeviceId( void DeviceManagerService::SendUnBindBroadCast(const std::vector &peerUdids, int32_t userId, uint64_t tokenId, int32_t bindLevel) { - LOGI("TokenId %{public}" PRId64", bindLevel %{public}d, userId %{public}d.", tokenId, bindLevel, userId); + LOGI("TokenId %{public}s, bindLevel %{public}d, userId %{public}d.", GetAnonyInt32(tokenId).c_str(), + bindLevel, userId); if (static_cast(bindLevel) == USER) { SendDeviceUnBindBroadCast(peerUdids, userId); return; diff --git a/services/service/src/devicenamemgr/device_name_manager.cpp b/services/service/src/devicenamemgr/device_name_manager.cpp index f58602049..a68dd6f5d 100644 --- a/services/service/src/devicenamemgr/device_name_manager.cpp +++ b/services/service/src/devicenamemgr/device_name_manager.cpp @@ -69,6 +69,7 @@ constexpr const char *PERSIST_GLOBAL_LOCALE = "persist.global.locale"; constexpr const char *DEFAULT_LOCALE_KEY = "const.global.locale"; const std::string LOCAL_ZH_HANS_CN = "zh-Hans-CN"; constexpr int32_t DEFAULT_USER_ID = -1; +constexpr uint32_t MAX_REGISTER_LEN = 1000; } IMPLEMENT_SINGLE_INSTANCE(DeviceNameManager); @@ -225,6 +226,10 @@ void DeviceNameManager::RegisterDeviceNameChangeMonitor(int32_t curUserId, int32 LOGE("monitor is nullptr"); return; } + if (monitorMap_.size() >= MAX_REGISTER_LEN) { + LOGE("too many monitor"); + return; + } monitorMap_[curUserId] = monitor; } std::string proxyUri = GetProxyUriStr(SETTINGSDATA_SECURE, curUserId); diff --git a/services/service/src/discovery/discovery_manager.cpp b/services/service/src/discovery/discovery_manager.cpp index c83110f90..2efe20c9a 100644 --- a/services/service/src/discovery/discovery_manager.cpp +++ b/services/service/src/discovery/discovery_manager.cpp @@ -33,6 +33,7 @@ const uint16_t DM_INVALID_FLAG_ID = 0; constexpr const char* LNN_DISC_CAPABILITY = "capability"; const std::string TYPE_MINE = "findDeviceMode"; const int32_t DECIMALISM = 10; +constexpr uint32_t MAX_DISCOVERY_LEN = 1000; #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) static std::mutex comDependencyLoadLock; @@ -225,6 +226,10 @@ int32_t DiscoveryManager::StartDiscovering4MineLibary(const std::string &pkgName pkgName.c_str(), std::string(dmSubInfo.capability).c_str()); { std::lock_guard capLock(capabilityMapLocks_); + if (capabilityMap_.size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return ERR_DM_START_DISCOVERING_FAILED; + } capabilityMap_[pkgName] = std::string(dmSubInfo.capability); } int32_t ret = mineSoftbusListener_->RefreshSoftbusLNN(pkgName, searchJson, dmSubInfo); @@ -254,6 +259,10 @@ int32_t DiscoveryManager::StartDiscoveringNoMetaType(const std::string &pkgName, { std::lock_guard capLock(capabilityMapLocks_); + if (capabilityMap_.size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return ERR_DM_START_DISCOVERING_FAILED; + } capabilityMap_[pkgName] = std::string(dmSubInfo.capability); } std::string customData = LNN_DISC_CAPABILITY; @@ -306,6 +315,10 @@ int32_t DiscoveryManager::StartDiscovering4MetaType(const std::string &pkgName, LOGI("capability = %{public}s,", std::string(dmSubInfo.capability).c_str()); { std::lock_guard capLock(capabilityMapLocks_); + if (capabilityMap_.size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return ERR_DM_START_DISCOVERING_FAILED; + } capabilityMap_[pkgName] =std::string(dmSubInfo.capability); } @@ -532,6 +545,10 @@ int32_t DiscoveryManager::HandleDiscoveryQueue(const std::string &pkgName, uint1 } { std::lock_guard autoLock(locks_); + if (pkgNameSet_.size() >= MAX_DISCOVERY_LEN || discoveryContextMap_.size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return ERR_DM_DISCOVERY_REPEATED; + } if (pkgNameSet_.find(pkgName) == pkgNameSet_.end()) { pkgNameSet_.emplace(pkgName); DiscoveryContext context = {pkgName, filterData, subscribeId, dmFilter.filterOp_, dmFilter.filters_}; @@ -757,6 +774,10 @@ std::string DiscoveryManager::AddMultiUserIdentify(const std::string &pkgName) std::string pkgNameTemp = ComposeStr(pkgName, userId); { std::lock_guard autoLock(multiUserDiscLocks_); + if (multiUserDiscMap_.size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return pkgNameTemp; + } multiUserDiscMap_[pkgNameTemp] = multiUserDisc; } return pkgNameTemp; @@ -801,6 +822,10 @@ int32_t DiscoveryManager::GenInnerSubId(const std::string &pkgName, uint16_t sub uint16_t tempSubId = DM_INVALID_FLAG_ID; { std::lock_guard autoLock(subIdMapLocks_); + if (pkgName2SubIdMap_.size() >= MAX_DISCOVERY_LEN || pkgName2SubIdMap_[pkgName].size() >= MAX_DISCOVERY_LEN) { + LOGE("too many discovery"); + return tempSubId; + } if (pkgName2SubIdMap_[pkgName].find(subId) != pkgName2SubIdMap_[pkgName].end()) { return pkgName2SubIdMap_[pkgName][subId]; } diff --git a/services/service/src/ipc/lite/ipc_server_listenermgr.cpp b/services/service/src/ipc/lite/ipc_server_listenermgr.cpp index 36bf2276c..abd1f99ae 100644 --- a/services/service/src/ipc/lite/ipc_server_listenermgr.cpp +++ b/services/service/src/ipc/lite/ipc_server_listenermgr.cpp @@ -21,6 +21,7 @@ namespace OHOS { namespace DistributedHardware { DM_IMPLEMENT_SINGLE_INSTANCE(IpcServerListenermgr); +constexpr uint32_t MAX_REGISTER_LEN = 1000; int32_t IpcServerListenermgr::RegisterListener(std::string &pkgName, const CommonSvcId *svcId) { @@ -30,6 +31,10 @@ int32_t IpcServerListenermgr::RegisterListener(std::string &pkgName, const Commo } LOGI("new listener register:%{public}s", pkgName.c_str()); std::lock_guard autoLock(lock_); + if (dmListenerMap_.size() >= MAX_REGISTER_LEN) { + LOGE("to many register"); + return ERR_DM_FAILED; + } dmListenerMap_[pkgName] = *svcId; return DM_OK; } diff --git a/services/service/src/publishcommonevent/dm_package_common_event.cpp b/services/service/src/publishcommonevent/dm_package_common_event.cpp index e914eb3ec..58b7c62fe 100644 --- a/services/service/src/publishcommonevent/dm_package_common_event.cpp +++ b/services/service/src/publishcommonevent/dm_package_common_event.cpp @@ -139,7 +139,8 @@ void DmPackageEventSubscriber::OnReceiveEvent(const CommonEventData &data) std::string appId = data.GetWant().GetStringParam(APP_ID); int32_t accessTokenId = static_cast(data.GetWant().GetIntParam(ACCESS_TOKEN_ID, 0)); if (accessTokenId == -1 || appId == "") { - LOGE("Invalid parameters: accessTokenId = %{public}d, appId = %{public}s", accessTokenId, appId.c_str()); + LOGE("Invalid parameters: accessTokenId = %{public}s, appId = %{public}s", + GetAnonyInt32(accessTokenId).c_str(), appId.c_str()); return; } LOGI("Received package event: %{public}s", receiveEvent.c_str()); diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 09b7dbb0b..3fc92fae3 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -530,8 +530,8 @@ void DMCommTool::ProcessReceiveUninstAppEvent(const std::shared_ptrremoteNetworkId, commMsg->socketId); if (uninstAppMsg.userId_ == -1 || uninstAppMsg.tokenId_ == -1) { - LOGE("param invalid, userId: %{public}d, tokenId: %{public}d", - uninstAppMsg.userId_, uninstAppMsg.tokenId_); + LOGE("param invalid, userId: %{public}d, tokenId: %{public}s", + uninstAppMsg.userId_, GetAnonyInt32(uninstAppMsg.tokenId_).c_str()); return; } else { DeviceManagerService::GetInstance().ProcessUninstApp(uninstAppMsg.userId_, @@ -558,8 +558,8 @@ void DMCommTool::ProcessReceiveUnBindAppEvent(const std::shared_ptrremoteNetworkId, commMsg->socketId); if (unBindAppMsg.userId_ == -1 || unBindAppMsg.tokenId_ == -1) { - LOGE("param invalid, userId: %{public}d, tokenId: %{public}d", - unBindAppMsg.userId_, unBindAppMsg.tokenId_); + LOGE("param invalid, userId: %{public}d, tokenId: %{public}s", + unBindAppMsg.userId_, GetAnonyInt32(unBindAppMsg.tokenId_).c_str()); return; } else { DeviceManagerService::GetInstance().ProcessUnBindApp(unBindAppMsg.userId_, diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index 8d9923d2d..ee98812b3 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -101,6 +101,7 @@ namespace { const uint16_t ALL_USERID_NUM_MASK = 0b00111000; const uint32_t MAX_MEM_MALLOC_SIZE = 4 * 1024; const uint32_t MAX_USER_ID_NUM = 4; + constexpr uint32_t MAX_BROADCAST_LEN = 1000; } RelationShipChangeMsg::RelationShipChangeMsg() : type(RelationShipChangeType::TYPE_MAX), @@ -965,6 +966,10 @@ bool ReleationShipSyncMgr::IsNewBroadCastId(const RelationShipChangeMsg &msg) } auto iter = recvBroadCastIdMap_.find(key); if (iter == recvBroadCastIdMap_.end()) { + if (recvBroadCastIdMap_.size() >= MAX_BROADCAST_LEN) { + LOGE("to many broadcast"); + return true; + } recvBroadCastIdMap_[key] = msg.broadCastId; timer_->StartTimer(key, BROADCAST_TIMEOUT_S, [this](std::string key) { ReleationShipSyncMgr::HandleRecvBroadCastTimeout(key); diff --git a/utils/include/fwkload/standard/dm_distributed_hardware_load.h b/utils/include/fwkload/standard/dm_distributed_hardware_load.h index c9ac3c548..c709f23a2 100644 --- a/utils/include/fwkload/standard/dm_distributed_hardware_load.h +++ b/utils/include/fwkload/standard/dm_distributed_hardware_load.h @@ -35,7 +35,7 @@ public: void InitDistributedHardwareLoadCount(void); uint32_t GetDistributedHardwareLoadCount(void); private: - uint32_t distributedHardwareLoadCount_; + std::atomic distributedHardwareLoadCount_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/fwkload/standard/dm_distributed_hardware_load.cpp b/utils/src/fwkload/standard/dm_distributed_hardware_load.cpp index 43c1dd014..43ec5ea5d 100644 --- a/utils/src/fwkload/standard/dm_distributed_hardware_load.cpp +++ b/utils/src/fwkload/standard/dm_distributed_hardware_load.cpp @@ -33,7 +33,7 @@ DM_EXPORT void DmDistributedHardwareLoad::LoadDistributedHardwareFwk(void) LOGE("failed to get system ability mgr."); return; } - distributedHardwareLoadCount_++; + distributedHardwareLoadCount_.fetch_add(1); sptr distributedHardwareLoadCallback_(new DistributedHardwareLoadCallback()); int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, distributedHardwareLoadCallback_); if (ret != DM_OK) { @@ -44,11 +44,11 @@ DM_EXPORT void DmDistributedHardwareLoad::LoadDistributedHardwareFwk(void) } void DmDistributedHardwareLoad::InitDistributedHardwareLoadCount(void) { - distributedHardwareLoadCount_ = 0; + distributedHardwareLoadCount_.store(0); } uint32_t DmDistributedHardwareLoad::GetDistributedHardwareLoadCount() { - return distributedHardwareLoadCount_; + return distributedHardwareLoadCount_.load(); } void DistributedHardwareLoadCallback::OnLoadSystemAbilitySuccess( -- Gitee