diff --git a/common/src/interfaces/device_profile_filter_options.cpp b/common/src/interfaces/device_profile_filter_options.cpp index af1d19991b49c1de3aff43462e61d2c77acaf66f..b3445f68694e1a6390550e86f63fccf182a6af12 100644 --- a/common/src/interfaces/device_profile_filter_options.cpp +++ b/common/src/interfaces/device_profile_filter_options.cpp @@ -32,16 +32,28 @@ int32_t DeviceProfileFilterOptions::GetUserId() const void DeviceProfileFilterOptions::AddDeviceIds(std::string deviceId) { + if (deviceIds_.size() >= MAX_DEVICE_SIZE) { + HILOGE("too many device!"); + return; + } deviceIds_.emplace_back(deviceId); } void DeviceProfileFilterOptions::AddWiseDeviceIds(std::string wiseDeviceId) { + if (wiseDeviceIds_.size() >= MAX_DEVICE_SIZE) { + HILOGE("too many device!"); + return; + } wiseDeviceIds_.emplace_back(wiseDeviceId); } void DeviceProfileFilterOptions::AddDeviceProfileIds(int32_t deviceProfileId) { + if (deviceProfileIds_.size() >= MAX_DEVICE_SIZE) { + HILOGE("too many device!"); + return; + } deviceProfileIds_.emplace_back(deviceProfileId); } diff --git a/common/src/interfaces/dp_sync_options.cpp b/common/src/interfaces/dp_sync_options.cpp index 4d42d9fc9ccddbe153b62fbcdedba2ca01c8aeda..eb53651ec0b672abd25299028e6a631fd44ceb94 100644 --- a/common/src/interfaces/dp_sync_options.cpp +++ b/common/src/interfaces/dp_sync_options.cpp @@ -31,6 +31,9 @@ std::vector DpSyncOptions::GetDeviceList() const void DpSyncOptions::AddDevice(const std::string& deviceId) { + if (syncDeviceIds_.size() > MAX_DEVICE_SIZE) { + return; + } syncDeviceIds_.emplace_back(deviceId); } diff --git a/common/src/utils/profile_utils.cpp b/common/src/utils/profile_utils.cpp index 3ba188da503db78d94ede267dc69d1a789c831c8..f624c2a173827a73b06d460fec7fcffefb806768 100644 --- a/common/src/utils/profile_utils.cpp +++ b/common/src/utils/profile_utils.cpp @@ -140,6 +140,9 @@ std::string ProfileUtils::GetProfileKey(const std::string& dbKey) return ""; } std::size_t pos = dbKey.find_last_of("#"); + if (pos == std::string::npos) { + return ""; + } return dbKey.substr(0, pos); } diff --git a/services/core/src/deviceprofilemanager/device_profile_manager.cpp b/services/core/src/deviceprofilemanager/device_profile_manager.cpp index 582906650093ba0ec4a9c57d2be8b9525d1037cd..02fb7745aac2c34ca836d1e1b058293628cf7d30 100644 --- a/services/core/src/deviceprofilemanager/device_profile_manager.cpp +++ b/services/core/src/deviceprofilemanager/device_profile_manager.cpp @@ -48,6 +48,7 @@ namespace { const std::string TAG = "DeviceProfileManager"; const std::unordered_set NON_OHBASE_NEED_CLEAR_SVR_NAMES { "collaborationFwk", "Nfc_Publish_Br_Mac_Address" }; + constexpr uint32_t MAX_MAP_LEN = 1000; } int32_t DeviceProfileManager::Init() @@ -549,7 +550,7 @@ bool DeviceProfileManager::LoadDpSyncAdapter() return false; } dlerror(); - auto func = (CreateDPSyncAdapterFuncPtr)dlsym(so_handle, "CreateDPSyncAdaptertObject"); + auto func = (CreateDPSyncAdapterFuncPtr)dlsym(so_handle, "CreateDPSyncAdapterObject"); if (dlerror() != nullptr || func == nullptr) { dlclose(so_handle); HILOGE("Create object function is not exist."); @@ -687,6 +688,10 @@ void DeviceProfileManager::AddToPutTempCache(const std::map lock(putTempCacheMutex_); + if (putTempCache_.size() > MAX_MAP_LEN) { + HILOGE("too many values!"); + return; + } for (const auto& [key, value] : values) { putTempCache_[key] = value; } diff --git a/services/core/src/distributed_device_profile_service_new.cpp b/services/core/src/distributed_device_profile_service_new.cpp index e9d2419c154a0e941c64ad18885022e8d322cb0a..93b63b8ec561b0ad20311d459a937f47f6c7cadf 100644 --- a/services/core/src/distributed_device_profile_service_new.cpp +++ b/services/core/src/distributed_device_profile_service_new.cpp @@ -63,6 +63,7 @@ constexpr int32_t WAIT_BUSINESS_PUT_TIME_S = 5; constexpr int32_t WRTE_CACHE_PROFILE_DELAY_TIME_US = 200 * 1000; constexpr int32_t WRTE_CACHE_PROFILE_RETRY_TIMES = 20; constexpr int32_t DP_IPC_THREAD_NUM = 32; +constexpr uint32_t MAX_CALLBACK_LEN = 1000; } IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileServiceNew); @@ -1259,6 +1260,10 @@ int32_t DistributedDeviceProfileServiceNew::SubscribeDeviceProfileInited(int32_t callbackProxy->OnDpInited(); } std::lock_guard lock(dpInitedCallbackMapMtx_); + if (dpInitedCallbackMap_.size() > MAX_CALLBACK_LEN) { + HILOGE("too many callback"); + return DP_INVALID_PARAM; + } dpInitedCallbackMap_[saId] = dpInitedCallback; return DP_SUCCESS; } @@ -1294,6 +1299,10 @@ int32_t DistributedDeviceProfileServiceNew::SubscribePinCodeInvalid(const std::s return DP_INVALID_PARAM; } std::lock_guard lock(pinCodeCallbackMapMtx_); + if (pinCodeCallbackMap_.size() > MAX_CALLBACK_LEN) { + HILOGE("too many callback"); + return DP_INVALID_PARAM; + } pinCodeCallbackMap_[std::make_pair(bundleName, pinExchangeType)] = pinCodeCallback; return DP_SUCCESS; } @@ -1398,6 +1407,10 @@ int32_t DistributedDeviceProfileServiceNew::RegisterBusinessCallback(const std:: saId.c_str(), businessKey.c_str()); return DP_INVALID_PARAM; } + if (businessCallbackMap_.size() > MAX_CALLBACK_LEN) { + HILOGE("too many callback"); + return DP_INVALID_PARAM; + } businessCallbackMap_[std::make_pair(saId, businessKey)] = businessCallback; return DP_SUCCESS; diff --git a/services/core/src/utils/profile_cache.cpp b/services/core/src/utils/profile_cache.cpp index 78625bbd937ee074b7530035f66c89ce50a50653..ac8a15b85fafcf63f07ab96848e2ffb26e4fb999 100644 --- a/services/core/src/utils/profile_cache.cpp +++ b/services/core/src/utils/profile_cache.cpp @@ -544,6 +544,10 @@ int32_t ProfileCache::SetSwitchByProfileBatch(const std::vector(service->second)); uint32_t value = STATUS_INIT; auto result = std::from_chars(item.GetCharacteristicValue().data(), @@ -574,6 +578,10 @@ int32_t ProfileCache::SetSwitchByProfile(const CharacteristicProfile& charProfil return DP_INVALID_PARAMS; } auto service = switchServiceMap.find(charProfile.GetServiceName()); + if (service == switchServiceMap.end()) { + HILOGE("not found serviceName"); + return DP_INVALID_PARAMS; + } uint32_t mask = NUM_1U << (static_cast(service->second)); uint32_t value = STATUS_INIT; auto result = std::from_chars(charProfile.GetCharacteristicValue().data(), @@ -626,6 +634,10 @@ int32_t ProfileCache::SetSwitchProfile(CharacteristicProfile& charProfile, uint3 return DP_INVALID_PARAMS; } auto service = SWITCH_SERVICE_MAP.find(charProfile.GetServiceName()); + if (service == SWITCH_SERVICE_MAP.end()) { + HILOGE("not found serviceName"); + return DP_INVALID_PARAMS; + } uint32_t mask = NUM_1U << static_cast(service->second); charProfile.SetCharacteristicValue(std::to_string((((switchValue & mask) >> (static_cast(service->second)))))); diff --git a/services/core/test/unittest/device_profile_locd_callback_test.cpp b/services/core/test/unittest/device_profile_locd_callback_test.cpp index b696c5d22e5f1d007513acbec374094cedb6b414..362f5a4b16ec81ac41e93afaa30c4dcae8fea3cb 100644 --- a/services/core/test/unittest/device_profile_locd_callback_test.cpp +++ b/services/core/test/unittest/device_profile_locd_callback_test.cpp @@ -69,9 +69,7 @@ HWTEST_F(DeviceProfileLoadCallbackTest, OnLoadSystemAbilitySuccess_001, TestSize int32_t systemAbilityId = 0; const OHOS::sptr remoteObject = nullptr; DeviceProfileLoadCallback callBack; - callBack.OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject); - int32_t abilityId = 0; - EXPECT_EQ(abilityId, systemAbilityId); + EXPECT_NO_FATAL_FAILURE(callBack.OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject)); } /* @@ -84,8 +82,7 @@ HWTEST_F(DeviceProfileLoadCallbackTest, OnLoadSystemAbilityFail_001, TestSize.Le int32_t systemAbilityId = 0; DeviceProfileLoadCallback callBack; callBack.OnLoadSystemAbilityFail(systemAbilityId); - int32_t abilityId = 0; - EXPECT_EQ(abilityId, systemAbilityId); + EXPECT_NO_FATAL_FAILURE(callBack.OnLoadSystemAbilityFail(systemAbilityId)); } } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/sync_completed_callback_test.cpp b/services/core/test/unittest/sync_completed_callback_test.cpp index b25733024a1b25d31b8e2ed4341c5e6b7f8571a3..3e22b2ea4ad6a29c92bb783a5f270335021dd547 100644 --- a/services/core/test/unittest/sync_completed_callback_test.cpp +++ b/services/core/test/unittest/sync_completed_callback_test.cpp @@ -84,18 +84,12 @@ HWTEST_F(SyncCompletedCallbackStubTest, OnRemoteRequest_001, TestSize.Level2) MessageOption option { MessageOption::TF_ASYNC }; SyncResult syncResults; syncResults.emplace("testdeviceid", SUCCEEDED); - if (!data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor())) { - return; - } - if (!data.WriteInt32(static_cast(syncResults.size()))) { - return; - } + ASSERT_NE(data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor()), ERR_FLATTEN_OBJECT); + ASSERT_NE(data.WriteInt32(static_cast(syncResults.size())), ERR_FLATTEN_OBJECT); for (const auto& [deviceId, syncResult] : syncResults) { - if (!data.WriteString(deviceId) || - !data.WriteInt32(static_cast(syncResult))) { - return; - } + ASSERT_NE(data.WriteString(deviceId), ERR_FLATTEN_OBJECT); + ASSERT_NE(data.WriteInt32(static_cast(syncResult)), ERR_FLATTEN_OBJECT); } uint32_t code = 27; SyncCompletedListener syncCallBackStub; @@ -114,9 +108,7 @@ HWTEST_F(SyncCompletedCallbackStubTest, OnRemoteRequest_002, TestSize.Level3) MessageParcel data; MessageParcel reply; MessageOption option { MessageOption::TF_ASYNC }; - if (!data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor())) { - return; - } + ASSERT_NE(data.WriteInterfaceToken(SyncCompletedCallbackStub::GetDescriptor()), ERR_FLATTEN_OBJECT); uint32_t code = 1; SyncCompletedListener syncCallBackStub; int32_t errCode = syncCallBackStub.OnRemoteRequest(code, data, reply, option);