From c9ecf48bfb3452cfd39a7f50289b972723889522 Mon Sep 17 00:00:00 2001 From: tangfan Date: Fri, 23 Sep 2022 16:11:47 +0800 Subject: [PATCH 1/3] fix secure problem Signed-off-by: tangfan --- common/utils/include/constants.h | 7 +++ .../include/distributed_hardware_errno.h | 5 +- .../include/ipc/distributed_hardware_proxy.h | 1 - .../src/distributed_hardware_fwk_kit.cpp | 9 ++++ .../src/ipc/distributed_hardware_proxy.cpp | 32 ++++++------- .../src/ipc/publisher_listener_stub.cpp | 9 ++++ .../include/distributed_hardware_stub.h | 1 - .../src/accessmanager/access_manager.cpp | 22 +++++++-- .../src/componentloader/component_loader.cpp | 18 ++++---- .../componentmanager/component_disable.cpp | 5 ++ .../src/componentmanager/component_enable.cpp | 5 ++ .../src/distributed_hardware_manager.cpp | 21 +++------ .../distributed_hardware_manager_factory.cpp | 26 ++++------- .../src/distributed_hardware_service.cpp | 2 +- .../src/distributed_hardware_stub.cpp | 13 ------ .../src/ipc/publisher_listener_proxy.cpp | 11 ++++- .../local_hardware_manager.cpp | 4 ++ .../plugin_listener_impl.cpp | 9 ++++ .../src/publisher/publisher_item.cpp | 6 ++- .../src/resourcemanager/capability_info.cpp | 14 +++--- .../capability_info_manager.cpp | 46 +++++++++++-------- .../src/resourcemanager/db_adapter.cpp | 24 +++++++--- .../src/resourcemanager/version_info.cpp | 15 +++--- .../resourcemanager/version_info_manager.cpp | 14 ++++-- .../src/utils/dh_context.cpp | 10 ++-- utils/src/dh_utils_tool.cpp | 4 ++ 26 files changed, 205 insertions(+), 128 deletions(-) diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 0612aa86..7fe1b666 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -24,6 +24,13 @@ namespace DistributedHardware { constexpr int32_t LOG_MAX_LEN = 4096; constexpr int32_t ENABLE_TIMEOUT_MS = 1000; constexpr int32_t DISABLE_TIMEOUT_MS = 500; + constexpr uint32_t MAX_STRING_LEN = 40 * 1024 * 1024; + constexpr uint32_t MAX_ID_LEN = 256; + constexpr uint32_t MAX_TOPIC_SIZE = 128; + constexpr uint32_t MAX_LISTENER_SIZE = 256; + constexpr uint32_t MAX_COMP_SIZE = 128; + constexpr uint32_t MAX_DB_DATA_SIZE = 10000; + constexpr uint32_t MAX_ONLINE_DEVICE_SIZE = 10000; const std::u16string DHMS_STUB_INTERFACE_TOKEN = u"ohos.distributedhardware.accessToken"; const std::string COMPONENTSLOAD_PROFILE_PATH = R"(/vendor/etc/distributedhardware/distributed_hardware_components_cfg.json)"; diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 224e6be5..3cfb2a12 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -61,6 +61,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; constexpr int32_t ERR_DH_FWK_RESOURCE_DEVICE_ID_NOT_EXIST = -10412; constexpr int32_t ERR_DH_FWK_RESOURCE_UUID_NOT_FOUND = -10413; + constexpr int32_t ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID = -10415; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; @@ -69,7 +70,7 @@ namespace DistributedHardware { /* ComponentLoader errno, range: [-10600, -10699] */ constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_IS_NULL = -10600; - constexpr int32_t ERR_DH_FWK_LOADER_COMPONENT_PROFILE_IS_EMPTY = -10601; + constexpr int32_t ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID = -10601; constexpr int32_t ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL = -10602; constexpr int32_t ERR_DH_FWK_LOADER_DLCLOSE_FAIL = -10603; @@ -85,6 +86,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL = -10805; constexpr int32_t ERR_DH_FWK_SERVICE_REMOTE_IS_NULL = -10806; constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL = -10807; + constexpr int32_t ERR_DH_FWK_SERVICE_MSG_INVALID = -10809; /* AccessManager errno, range: [-10900, -10999] */ constexpr int32_t ERR_DH_FWK_ACCESS_INIT_DM_FAILED = -10900; @@ -99,6 +101,7 @@ namespace DistributedHardware { /* DHFWK Publisher errno, range: [-11100, -11199] */ constexpr int32_t ERR_DH_FWK_PUBLISH_MSG_FAILED = -11100; + constexpr uint32_t ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE = -11101; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DISTRIBUTED_HARDWARE_ERRNO diff --git a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 5bd57379..bf3b38e5 100644 --- a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -37,7 +37,6 @@ public: int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; private: - std::unordered_map FromJson(const std::string &json) const; static inline BrokerDelegator delegator_; }; } // namespace DistributedHardware diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 54913644..3c9df100 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -17,6 +17,7 @@ #include +#include "constants.h" #include "dhfwk_sa_manager.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -53,6 +54,10 @@ int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic } else { DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); std::lock_guard lock(listenerMutex_); + if (listenerMap_.size() >= MAX_TOPIC_SIZE || listenerMap_[topic].size() >= MAX_LISTENER_SIZE) { + DHLOGE("listeners are over size!"); + return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; + } listenerMap_[topic].insert(listener); } @@ -86,6 +91,10 @@ int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std DHLOGE("Topic invalid, topic: " PRIu32, (uint32_t)topic); return ERR_DH_FWK_PARA_INVALID; } + if (message.empty() || message.size() > MAX_STRING_LEN) { + DHLOGE("Message size is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { DHLOGI("DHFWK not online, can not publish message"); diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index c47f2a5a..406a6a8d 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -41,6 +41,10 @@ int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, DHLOGE("remote service is null"); return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } MessageParcel data; MessageParcel reply; @@ -81,6 +85,10 @@ int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topi DHLOGE("remote service is null"); return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } MessageParcel data; MessageParcel reply; @@ -120,6 +128,14 @@ int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std: DHLOGE("remote service is null"); return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + if (msg.empty() || msg.size() > MAX_STRING_LEN) { + DHLOGE("Msg is invalid"); + return ERR_DH_FWK_SERVICE_MSG_INVALID; + } MessageParcel data; MessageParcel reply; @@ -151,21 +167,5 @@ int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std: return ret; } - -void from_json(const nlohmann::json &jsonObj, std::unordered_map &versionMap) -{ - for (const auto &item : jsonObj.value(DH_COMPONENT_VERSIONS, nlohmann::json {})) { - DHType dhType = (DH_TYPE_SET.find(item.value(DH_COMPONENT_TYPE, DHType::UNKNOWN)) != DH_TYPE_SET.end()) ? - item.value(DH_COMPONENT_TYPE, DHType::UNKNOWN) : - DHType::UNKNOWN; - std::string sinkVersion = item.value(DH_COMPONENT_SINK_VER, DH_COMPONENT_DEFAULT_VERSION); - versionMap.emplace(std::pair(dhType, sinkVersion)); - } -} - -std::unordered_map DistributedHardwareProxy::FromJson(const std::string &json) const -{ - return nlohmann::json::parse(json).get>(); -} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp index 84be82c9..5ba2c8c5 100644 --- a/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp +++ b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp @@ -15,6 +15,7 @@ #include "publisher_listener_stub.h" +#include "constants.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -39,7 +40,15 @@ int32_t PublisherListenerStub::OnRemoteRequest( switch (msgCode) { case IPublisherListener::Message::ON_MESSAGE: { DHTopic topic = (DHTopic)data.ReadUint32(); + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_INVALID_DATA; + } std::string message = data.ReadString(); + if (message.empty() || message.size() > MAX_STRING_LEN) { + DHLOGE("Message is invalid!"); + return ERR_INVALID_DATA; + } OnMessage(topic, message); break; } diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index f549c24e..cabb412e 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -31,7 +31,6 @@ private: int32_t UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply); int32_t PublishMessageInner(MessageParcel &data, MessageParcel &reply); bool ValidTopic(uint32_t topic); - std::string ToJson(const std::unordered_map &versionMap) const; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index 085c7925..cf11db69 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -131,13 +131,17 @@ void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo) GetAnonyString(deviceInfo.deviceName).c_str(), deviceInfo.deviceTypeId); auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) { + DHLOGE("NetworkId is invalid!"); + return; + } auto uuid = GetUUIDBySoftBus(networkId); // when other device restart, the device receives online and offline messages in sequence // uuid is empty call by GetUUIDBySoftBus function. So, get uuid by memory cache when other device restart uuid = uuid.empty() ? DHContext::GetInstance().GetUUIDByNetworkId(networkId) : uuid; - if (uuid.empty()) { - DHLOGI("uuid is empty!"); + if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + DHLOGE("Uuid is invalid!"); return; } @@ -153,8 +157,16 @@ void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo) DHLOGI("start, networkId = %s, deviceName = %s, deviceTypeId = %d", GetAnonyString(deviceInfo.deviceId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(), deviceInfo.deviceTypeId); - auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId + auto networkId = std::string(deviceInfo.deviceId); + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) { + DHLOGE("NetworkId is invalid!"); + return; + } auto uuid = GetUUIDBySoftBus(networkId); + if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + DHLOGE("Uuid is invalid!"); + return; + } auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId); DHLOGI("online result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(), @@ -171,6 +183,10 @@ void AccessManager::SendTrustedDeviceOnline() { std::vector deviceList; DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList); + if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) { + DHLOGE("DeviceList size is invalid!"); + return; + } for (const auto &deviceInfo : deviceList) { const auto networkId = std::string(deviceInfo.deviceId); const auto uuid = GetUUIDBySoftBus(networkId); diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 69f7d300..7314a519 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -137,7 +137,11 @@ int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std:: std::vector vecJsnCfg = jsonCfg.at(COMPONENTSLOAD_DISTRIBUTED_COMPONENTS).get>(); DHLOGI("get distributed_components CompConfig size is %d", vecJsnCfg.size()); - for (std::vector::iterator iter = vecJsnCfg.begin(); iter != vecJsnCfg.end(); ++iter) { + if (vecJsnCfg.size() == 0 || vecJsnCfg.size() > MAX_COMP_SIZE) { + DHLOGE("CompConfig size is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + for (auto iter = vecJsnCfg.begin(); iter != vecJsnCfg.end(); ++iter) { dhtypeMap.insert(std::pair((*iter).type, (*iter))); localDHVersion_.compVersions.insert( std::pair((*iter).type, GetCompVersionFromComConfig(*iter))); @@ -171,10 +175,6 @@ void ComponentLoader::StoreLocalDHVersionInDB() void *ComponentLoader::GetHandler(const std::string &soName) { - if (soName.length() <= 0) { - DHLOGE("%s soName length is 0", soName.c_str()); - return nullptr; - } char path[PATH_MAX + 1] = {0x00}; if (soName.length() == 0 || (LIB_LOAD_PATH.length() + soName.length()) > PATH_MAX || realpath((LIB_LOAD_PATH + soName).c_str(), path) == nullptr) { @@ -302,9 +302,9 @@ int32_t ComponentLoader::ParseConfig() int32_t ret; DHLOGI("ParseConfig start"); std::string jsonStr = Readfile(COMPONENTSLOAD_PROFILE_PATH); - if (jsonStr.length() == 0) { - DHLOGE("profile is empty return"); - return ERR_DH_FWK_LOADER_COMPONENT_PROFILE_IS_EMPTY; + if (jsonStr.length() == 0 || jsonStr.size() > MAX_STRING_LEN) { + DHLOGE("ConfigJson size is invalid!"); + return ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID; } ret = GetCompPathAndVersion(jsonStr, dhtypeMap); if (ret != DH_FWK_SUCCESS) { @@ -402,7 +402,6 @@ int32_t ComponentLoader::GetSourceSaId(const DHType dhType) DHLOGE("DHType not exist, dhType: " PRIu32, (uint32_t)dhType); return DEFAULT_SA_ID; } - return compHandlerMap_[dhType].sourceSaId; } @@ -415,7 +414,6 @@ DHType ComponentLoader::GetDHTypeBySrcSaId(const int32_t saId) break; } } - return type; } } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp index 4314457e..392c40aa 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp @@ -67,6 +67,11 @@ int32_t ComponentDisable::Disable(const std::string &networkId, const std::strin int32_t ComponentDisable::OnUnregisterResult(const std::string &networkId, const std::string &dhId, int32_t status, const std::string &data) { + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || dhId.size() == 0 || dhId.size() > MAX_ID_LEN || + data.size() == 0 || data.size() > MAX_STRING_LEN) { + DHLOGE("Param is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } if (status == DH_FWK_SUCCESS) { DHLOGI("disable success, networkId = %s, dhId = %s, data = %s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), data.c_str()); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp index 5a05ae65..b53f12c5 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp @@ -67,6 +67,11 @@ int32_t ComponentEnable::Enable(const std::string &networkId, const std::string int32_t ComponentEnable::OnRegisterResult(const std::string &networkId, const std::string &dhId, int32_t status, const std::string &data) { + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || dhId.size() == 0 || dhId.size() > MAX_ID_LEN || + data.size() == 0 || data.size() > MAX_STRING_LEN) { + DHLOGE("Param is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } if (status == DH_FWK_SUCCESS) { DHLOGI("enable success, networkId = %s, dhId = %s, data = %s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), data.c_str()); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp index ce4c9687..c73ab2d8 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp @@ -85,13 +85,9 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId { (void)deviceType; - if (networkId.empty()) { - DHLOGE("networkId is empty"); - return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; - } - if (uuid.empty()) { - DHLOGE("uuid is empty, networkId = %s", GetAnonyString(networkId).c_str()); - return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + DHLOGE("NetworkId or uuid is invalid"); + return ERR_DH_FWK_PARA_INVALID; } DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str()); @@ -116,14 +112,9 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI { (void)deviceType; - if (networkId.empty()) { - DHLOGE("networkId is empty"); - return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; - } - - if (uuid.empty()) { - DHLOGW("uuid is empty"); - return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; + if (networkId.empty() || networkId.size() > MAX_ID_LEN || uuid.empty() || uuid.size() > MAX_ID_LEN) { + DHLOGE("NetworkId or uuid is invalid"); + return ERR_DH_FWK_PARA_INVALID; } DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str()); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index ca7629ab..1ed0dfd8 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -71,8 +71,8 @@ void DistributedHardwareManagerFactory::CheckExitSAOrNot() { std::vector deviceList; DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList); - if (deviceList.size() == 0) { - DHLOGI("DM report devices offline, exit sa process"); + if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) { + DHLOGI("DM report devices offline or deviceList is over size, exit sa process"); HiSysEventWriteMsg(DHFWK_EXIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "dhfwk sa exit end."); @@ -99,14 +99,9 @@ bool DistributedHardwareManagerFactory::IsInit() int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid, uint16_t deviceType) { - if (networkId.empty()) { - DHLOGE("networkId is empty"); - return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; - } - - if (uuid.empty()) { - DHLOGE("uuid is empty"); - return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; + if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + DHLOGE("NetworkId or uuid is invalid"); + return ERR_DH_FWK_PARA_INVALID; } if (DHContext::GetInstance().IsDeviceOnline(uuid)) { @@ -132,14 +127,9 @@ int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &ne int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &networkId, const std::string &uuid, uint16_t deviceType) { - if (networkId.empty()) { - DHLOGE("networkId is empty"); - return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; - } - - if (uuid.empty()) { - DHLOGE("uuid is empty"); - return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; + if (networkId.empty() || networkId.size() > MAX_ID_LEN || uuid.empty() || uuid.size() > MAX_ID_LEN) { + DHLOGE("NetworkId or uuid is invalid"); + return ERR_DH_FWK_PARA_INVALID; } if (!isInit && !Init()) { diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 389d742c..ab92ed6e 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -71,7 +71,7 @@ bool DistributedHardwareService::Init() } auto ret = AccessManager::GetInstance()->Init(); if (ret != DH_FWK_SUCCESS) { - DHLOGI("DistributedHardwareService::Init failed."); + DHLOGE("DistributedHardwareService::Init failed."); HiSysEventWriteErrCodeMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, ret, "dhfwk sa AccessManager init fail."); return false; diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index 0ede44fe..1af2cda8 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -116,20 +116,7 @@ bool DistributedHardwareStub::ValidTopic(uint32_t topic) if (topic <= (uint32_t)DHTopic::TOPIC_MIN || topic >= (uint32_t)DHTopic::TOPIC_MAX) { return false; } - return true; } - -std::string DistributedHardwareStub::ToJson(const std::unordered_map &versionMap) const -{ - nlohmann::json jsonObj; - for (const auto &item : versionMap) { - nlohmann::json json; - json[DH_COMPONENT_TYPE] = item.first; - json[DH_COMPONENT_SINK_VER] = item.second; - jsonObj[DH_COMPONENT_VERSIONS].emplace_back(json); - } - return jsonObj.dump(); -} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp index 704675e9..aad58fbe 100644 --- a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp +++ b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp @@ -13,8 +13,9 @@ * limitations under the License. */ -#include "publisher_listener_proxy.h" +#include "constants.h" #include "distributed_hardware_log.h" +#include "publisher_listener_proxy.h" namespace OHOS { namespace DistributedHardware { @@ -34,6 +35,14 @@ void PublisherListenerProxy::OnMessage(const DHTopic topic, const std::string& m DHLOGE("Get Remote IRemoteObject failed"); return; } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return; + } + if (message.size() == 0 || message.size() > MAX_STRING_LEN) { + DHLOGE("Message is invalid"); + return; + } MessageParcel data; MessageParcel reply; diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp index 3ab0ae50..52faf9f4 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp @@ -152,6 +152,10 @@ void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector void LocalHardwareManager::GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap) { std::string localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (localDeviceId.size() == 0 || localDeviceId.size() > MAX_ID_LEN) { + DHLOGE("LocalDeviceId is invalid"); + return; + } if (DHTypePrefixMap.find(dhType) == DHTypePrefixMap.end()) { DHLOGE("DHTypePrefixMap can not find dhType: %#X", dhType); return; diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp index b5a3c57a..f908b974 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "capability_info.h" #include "capability_info_manager.h" +#include "constants.h" #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -29,6 +30,10 @@ namespace DistributedHardware { void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::string &attrs) { + if (dhId.size() == 0 || dhId.size() > MAX_ID_LEN || attrs.size() == 0 || attrs.size() > MAX_STRING_LEN) { + DHLOGE("Param is invalid!"); + return; + } DHLOGI("plugin start, dhId: %s", GetAnonyString(dhId).c_str()); std::vector> capabilityInfos; std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; @@ -44,6 +49,10 @@ void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::stri void PluginListenerImpl::UnPluginHardware(const std::string &dhId) { + if (dhId.size() == 0 || dhId.size() > MAX_ID_LEN) { + DHLOGE("DhId is invalid!"); + return; + } DHLOGI("unplugin start, dhId: %s", GetAnonyString(dhId).c_str()); std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; std::shared_ptr capability = nullptr; diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp index 1ce2e5c8..ff896f7f 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ +#include "constants.h" #include "publisher_item.h" - #include "distributed_hardware_log.h" namespace OHOS { @@ -64,6 +64,10 @@ void PublisherItem::RemoveListener(const sptr &listener) void PublisherItem::PublishMessage(const std::string &message) { + if (message.size() == 0 || message.size() > MAX_STRING_LEN) { + DHLOGE("Message is invalid"); + return; + } std::lock_guard lock(mutex_); for (const auto &listener : listeners_) { listener->OnMessage(topic_, message); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp index 50eefa1a..8843fbba 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -162,22 +162,24 @@ void ToJson(nlohmann::json &jsonObject, const CapabilityInfo &capability) void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability) { - if (jsonObject.find(DH_ID) != jsonObject.end()) { + if (jsonObject.find(DH_ID) != jsonObject.end() && jsonObject[DH_ID].is_string()) { capability.SetDHId(jsonObject.at(DH_ID).get()); } - if (jsonObject.find(DEV_ID) != jsonObject.end()) { + if (jsonObject.find(DEV_ID) != jsonObject.end() && jsonObject[DH_ID].is_string()) { capability.SetDeviceId(jsonObject.at(DEV_ID).get()); } - if (jsonObject.find(DEV_NAME) != jsonObject.end()) { + if (jsonObject.find(DEV_NAME) != jsonObject.end() && jsonObject[DH_ID].is_string()) { capability.SetDeviceName(jsonObject.at(DEV_NAME).get()); } - if (jsonObject.find(DEV_TYPE) != jsonObject.end()) { + if (jsonObject.find(DEV_TYPE) != jsonObject.end() && jsonObject[DEV_TYPE].is_number_unsigned() && + jsonObject[DEV_TYPE] <= UINT16_MAX) { capability.SetDeviceType(jsonObject.at(DEV_TYPE).get()); } - if (jsonObject.find(DH_TYPE) != jsonObject.end()) { + if (jsonObject.find(DH_TYPE) != jsonObject.end() && jsonObject[DEV_TYPE].is_number_unsigned() && + jsonObject[DEV_TYPE] <= UINT32_MAX) { capability.SetDHType(jsonObject.at(DH_TYPE).get()); } - if (jsonObject.find(DH_ATTRS) != jsonObject.end()) { + if (jsonObject.find(DH_ATTRS) != jsonObject.end() && jsonObject[DH_ATTRS].is_string()) { capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get()); } } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index faf56955..556b3109 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -94,6 +94,10 @@ int32_t CapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) DHLOGE("Query data from DB by deviceId failed, id: %s", GetAnonyString(deviceId).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } + if (dataVector.size() == 0 || dataVector.size() > MAX_DB_DATA_SIZE) { + DHLOGE("DataVector size is invalid!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } for (const auto &data : dataVector) { std::shared_ptr capabilityInfo; if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) { @@ -118,6 +122,10 @@ int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos() DHLOGE("Query all data from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } + if (dataVector.size() == 0 || dataVector.size() > MAX_DB_DATA_SIZE) { + DHLOGE("DataVector size is invalid!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } for (const auto &data : dataVector) { std::shared_ptr capabilityInfo; if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) { @@ -131,8 +139,7 @@ int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos() continue; } if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { - DHLOGE("offline device, no need sync to memory, deviceId : %s ", - GetAnonyString(deviceId).c_str()); + DHLOGE("offline device, no need sync to memory, deviceId : %s ", GetAnonyString(deviceId).c_str()); continue; } globalCapInfoMap_[capabilityInfo->GetKey()] = capabilityInfo; @@ -142,6 +149,10 @@ int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos() int32_t CapabilityInfoManager::AddCapability(const std::vector> &resInfos) { + if (resInfos.size() == 0 || resInfos.size() > MAX_DB_DATA_SIZE) { + DHLOGE("ResInfos size is invalid!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); @@ -152,7 +163,7 @@ int32_t CapabilityInfoManager::AddCapability(const std::vectorGetKey(); @@ -181,7 +192,7 @@ int32_t CapabilityInfoManager::AddCapabilityInMem(const std::vector lock(capInfoMgrMutex_); for (auto &resInfo : resInfos) { - if (!resInfo) { + if (resInfo != nullptr) { continue; } const std::string key = resInfo->GetKey(); @@ -199,10 +210,6 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoInDB(const std::string &devic DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } - if (deviceId.empty()) { - DHLOGE("RemoveCapabilityInfoInDB failed, deviceId is empty"); - return ERR_DH_FWK_PARA_INVALID; - } // 1. Clear the cache in the memory. for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) { if (!CapabilityUtils::IsCapKeyMatchDeviceId(iter->first, deviceId)) { @@ -228,10 +235,6 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key) DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } - if (key.empty()) { - DHLOGE("key is empty"); - return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY; - } // 1. Clear the cache in the memory. globalCapInfoMap_.erase(key); @@ -247,10 +250,6 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoInMem(const std::string &devi { DHLOGI("remove capability device info in memory, deviceId: %s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(capInfoMgrMutex_); - if (deviceId.empty()) { - DHLOGE("RemoveCapabilityInfoInMem failed, deviceId is empty"); - return ERR_DH_FWK_PARA_INVALID; - } for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) { if (!CapabilityUtils::IsCapKeyMatchDeviceId(iter->first, deviceId)) { iter++; @@ -319,15 +318,18 @@ int32_t CapabilityInfoManager::ManualSync(const std::string &networkId) void CapabilityInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) { DHLOGI("CapabilityInfoManager: DB data OnChange"); - if (!changeNotification.GetInsertEntries().empty()) { + if (!changeNotification.GetInsertEntries().empty() || + changeNotification.GetInsertEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data add change"); HandleCapabilityAddChange(changeNotification.GetInsertEntries()); } - if (!changeNotification.GetUpdateEntries().empty()) { + if (!changeNotification.GetUpdateEntries().empty() || + changeNotification.GetUpdateEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data update change"); HandleCapabilityUpdateChange(changeNotification.GetUpdateEntries()); } - if (!changeNotification.GetDeleteEntries().empty()) { + if (!changeNotification.GetDeleteEntries().empty() || + changeNotification.GetDeleteEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data delete change"); HandleCapabilityDeleteChange(changeNotification.GetDeleteEntries()); } @@ -533,7 +535,7 @@ int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, { std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { - DHLOGI("dbAdapterPtr_ is null"); + DHLOGE("dbAdapterPtr is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } std::vector dataVector; @@ -541,6 +543,10 @@ int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, DHLOGE("Query capability info from db failed, key: %s", GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } + if (dataVector.size() == 0 || dataVector.size() > MAX_DB_DATA_SIZE) { + DHLOGE("DataVector size is invalid!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } for (const auto &data : dataVector) { std::shared_ptr capabilityInfo; if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp index c95e9399..49ff56e2 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp @@ -125,6 +125,10 @@ int32_t DBAdapter::ReInit() void DBAdapter::SyncCompleted(const std::map &results) { DHLOGI("DBAdapter SyncCompleted start"); + if (results.size() == 0 || results.size() > MAX_DB_DATA_SIZE) { + DHLOGE("Results size is invalid!"); + return; + } std::lock_guard lock(dbAdapterMutex_); for (const auto &result : results) { std::string deviceId = result.first; @@ -172,6 +176,10 @@ int32_t DBAdapter::GetDataByKey(const std::string &key, std::string &data) int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector &values) { + if (keyPrefix.empty() || keyPrefix.size() > MAX_STRING_LEN) { + DHLOGI("keyPrefix is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Get data by key prefix: %s", GetAnonyString(keyPrefix).c_str()); std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { @@ -188,6 +196,10 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_KV_STORAGE_OPERATION_FAIL; } + if (allEntries.size() == 0 || allEntries.size() > MAX_DB_DATA_SIZE) { + DHLOGE("AllEntries size is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } for (const auto& item : allEntries) { values.push_back(item.value.ToString()); } @@ -196,6 +208,10 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< int32_t DBAdapter::PutData(const std::string &key, std::string &value) { + if (key.empty() || key.size() > MAX_STRING_LEN) { + DHLOGI("key is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -218,12 +234,8 @@ int32_t DBAdapter::PutDataBatch(const std::vector &keys, const std: DHLOGE("kvStoragePtr_ is null"); return ERR_DH_FWK_RESOURCE_KV_STORAGE_POINTER_NULL; } - if (keys.size() != values.size()) { - DHLOGE("Param invalid"); - return ERR_DH_FWK_PARA_INVALID; - } - if (keys.empty() || values.empty()) { - DHLOGE("keys or values is empty!"); + if (keys.size() != values.size() || keys.empty() || values.empty()) { + DHLOGE("Param is invalid!"); return ERR_DH_FWK_PARA_INVALID; } std::vector entries; diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 649f3519..ac7cf9a0 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -63,30 +63,31 @@ void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo) void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer) { - if (jsonObject.find(NAME) != jsonObject.end()) { + if (jsonObject.find(NAME) != jsonObject.end() && jsonObject[NAME].is_string()) { compVer.name = jsonObject.at(NAME).get(); } - if (jsonObject.find(TYPE) != jsonObject.end()) { + if (jsonObject.find(TYPE) != jsonObject.end() && jsonObject[TYPE].is_number_unsigned() && + jsonObject[TYPE] < DHType::MAX_DH) { compVer.dhType = jsonObject.at(TYPE).get(); } - if (jsonObject.find(HANDLER) != jsonObject.end()) { + if (jsonObject.find(HANDLER) != jsonObject.end() && jsonObject[HANDLER].is_string()) { compVer.handlerVersion = jsonObject.at(HANDLER).get(); } - if (jsonObject.find(SOURCE_VER) != jsonObject.end()) { + if (jsonObject.find(SOURCE_VER) != jsonObject.end() && jsonObject[SOURCE_VER].is_string()) { compVer.sourceVersion = jsonObject.at(SOURCE_VER).get(); } - if (jsonObject.find(SINK_VER) != jsonObject.end()) { + if (jsonObject.find(SINK_VER) != jsonObject.end() && jsonObject[SINK_VER].is_string()) { compVer.sinkVersion = jsonObject.at(SINK_VER).get(); } } void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo) { - if (jsonObject.find(DEV_ID) != jsonObject.end()) { + if (jsonObject.find(DEV_ID) != jsonObject.end() && jsonObject[DEV_ID].is_string()) { versionInfo.deviceId = jsonObject.at(DEV_ID).get(); } - if (jsonObject.find(DH_VER) != jsonObject.end()) { + if (jsonObject.find(DH_VER) != jsonObject.end() && jsonObject[DH_VER].is_string()) { versionInfo.dhVersion = jsonObject.at(DH_VER).get(); } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp index d9e3d109..067e3d80 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -194,7 +194,10 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() DHLOGE("Query all data from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - + if (dataVector.size() == 0 || dataVector.size() > MAX_DB_DATA_SIZE) { + DHLOGE("DataVector Size is invalid!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } for (const auto &data : dataVector) { VersionInfo versionInfo; versionInfo.FromJsonString(data); @@ -252,15 +255,18 @@ int32_t VersionInfoManager::ManualSync(const std::string &networkId) void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) { DHLOGI("DB data OnChange"); - if (!changeNotification.GetInsertEntries().empty()) { + if (!changeNotification.GetInsertEntries().empty() || + changeNotification.GetInsertEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle version data add change"); HandleVersionAddChange(changeNotification.GetInsertEntries()); } - if (!changeNotification.GetUpdateEntries().empty()) { + if (!changeNotification.GetUpdateEntries().empty() || + changeNotification.GetUpdateEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle version data update change"); HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); } - if (!changeNotification.GetDeleteEntries().empty()) { + if (!changeNotification.GetDeleteEntries().empty() || + changeNotification.GetDeleteEntries().size() > MAX_DB_DATA_SIZE) { DHLOGI("Handle version data delete change"); HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); } diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 8f48c199..9ff3990b 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -15,8 +15,8 @@ #include +#include "constants.h" #include "dh_context.h" - #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -52,10 +52,12 @@ const DeviceInfo& DHContext::GetDeviceInfo() void DHContext::AddOnlineDevice(const std::string &uuid, const std::string &networkId) { std::unique_lock lock(onlineDevMutex_); - if (!uuid.empty() && !networkId.empty()) { - onlineDeviceMap_[uuid] = networkId; - deviceIdUUIDMap_[GetDeviceIdByUUID(uuid)] = uuid; + if (onlineDeviceMap_.size() >= MAX_ONLINE_DEVICE_SIZE || deviceIdUUIDMap_.size() >= MAX_ONLINE_DEVICE_SIZE) { + DHLOGE("OnlineDeviceMap or deviceIdUUIDMap is over size!"); + return; } + onlineDeviceMap_[uuid] = networkId; + deviceIdUUIDMap_[GetDeviceIdByUUID(uuid)] = uuid; } void DHContext::RemoveOnlineDevice(const std::string &uuid) diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index 0fb238b3..bc0768fb 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -82,6 +82,10 @@ std::string GetUUIDBySoftBus(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { + if (uuid.size() == 0 || uuid.size() > MAX_STRING_LEN) { + DHLOGE("uuid is invalid!"); + return ""; + } return Sha256(uuid); } -- Gitee From 9e8089396465702e45343914426e102567f3d418 Mon Sep 17 00:00:00 2001 From: tangfan Date: Mon, 26 Sep 2022 11:36:29 +0800 Subject: [PATCH 2/3] fix secure bug Signed-off-by: tangfan --- .../include/resourcemanager/db_adapter.h | 2 +- .../src/resourcemanager/capability_info.cpp | 2 +- .../src/resourcemanager/capability_info_manager.cpp | 10 +++++++--- .../src/resourcemanager/db_adapter.cpp | 10 +++------- .../src/resourcemanager/version_info.cpp | 2 +- .../src/resourcemanager/version_info_manager.cpp | 6 +++--- .../src/utils/dh_context.cpp | 2 +- utils/src/dh_utils_tool.cpp | 2 +- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h b/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h index 6a27272c..f2d052d1 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h @@ -55,7 +55,7 @@ public: void SyncCompleted(const std::map &results) override; int32_t GetDataByKey(const std::string &key, std::string &data); int32_t GetDataByKeyPrefix(const std::string &keyPrefix, std::vector &values); - int32_t PutData(const std::string &key, std::string &value); + int32_t PutData(const std::string &key, const std::string &value); int32_t PutDataBatch(const std::vector &keys, const std::vector &values); void CreateManualSyncCount(const std::string &deviceId); void RemoveManualSyncCount(const std::string &deviceId); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp index 8843fbba..21552122 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -176,7 +176,7 @@ void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability) capability.SetDeviceType(jsonObject.at(DEV_TYPE).get()); } if (jsonObject.find(DH_TYPE) != jsonObject.end() && jsonObject[DEV_TYPE].is_number_unsigned() && - jsonObject[DEV_TYPE] <= UINT32_MAX) { + jsonObject[DEV_TYPE] <= DHType::MAX_DH) { capability.SetDHType(jsonObject.at(DH_TYPE).get()); } if (jsonObject.find(DH_ATTRS) != jsonObject.end() && jsonObject[DH_ATTRS].is_string()) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index 556b3109..1016894a 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -204,6 +204,10 @@ int32_t CapabilityInfoManager::AddCapabilityInMem(const std::vector MAX_ID_LEN) { + DHLOGE("DeviceId is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Remove capability device info, deviceId: %s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -319,17 +323,17 @@ void CapabilityInfoManager::OnChange(const DistributedKv::ChangeNotification &ch { DHLOGI("CapabilityInfoManager: DB data OnChange"); if (!changeNotification.GetInsertEntries().empty() || - changeNotification.GetInsertEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetInsertEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data add change"); HandleCapabilityAddChange(changeNotification.GetInsertEntries()); } if (!changeNotification.GetUpdateEntries().empty() || - changeNotification.GetUpdateEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetUpdateEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data update change"); HandleCapabilityUpdateChange(changeNotification.GetUpdateEntries()); } if (!changeNotification.GetDeleteEntries().empty() || - changeNotification.GetDeleteEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetDeleteEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle capability data delete change"); HandleCapabilityDeleteChange(changeNotification.GetDeleteEntries()); } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp index 49ff56e2..78e461d1 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp @@ -176,10 +176,6 @@ int32_t DBAdapter::GetDataByKey(const std::string &key, std::string &data) int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector &values) { - if (keyPrefix.empty() || keyPrefix.size() > MAX_STRING_LEN) { - DHLOGI("keyPrefix is invalid!"); - return ERR_DH_FWK_PARA_INVALID; - } DHLOGI("Get data by key prefix: %s", GetAnonyString(keyPrefix).c_str()); std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { @@ -206,10 +202,10 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< return DH_FWK_SUCCESS; } -int32_t DBAdapter::PutData(const std::string &key, std::string &value) +int32_t DBAdapter::PutData(const std::string &key, const std::string &value) { - if (key.empty() || key.size() > MAX_STRING_LEN) { - DHLOGI("key is invalid!"); + if (key.empty() || key.size() > MAX_STRING_LEN || value.empty() || value.size() > MAX_STRING_LEN) { + DHLOGI("Param is invalid!"); return ERR_DH_FWK_PARA_INVALID; } std::lock_guard lock(dbAdapterMutex_); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index ac7cf9a0..c64ced43 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -67,7 +67,7 @@ void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer) compVer.name = jsonObject.at(NAME).get(); } if (jsonObject.find(TYPE) != jsonObject.end() && jsonObject[TYPE].is_number_unsigned() && - jsonObject[TYPE] < DHType::MAX_DH) { + jsonObject[TYPE] <= DHType::MAX_DH) { compVer.dhType = jsonObject.at(TYPE).get(); } if (jsonObject.find(HANDLER) != jsonObject.end() && jsonObject[HANDLER].is_string()) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp index 067e3d80..c0af0116 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -256,17 +256,17 @@ void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &chang { DHLOGI("DB data OnChange"); if (!changeNotification.GetInsertEntries().empty() || - changeNotification.GetInsertEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetInsertEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle version data add change"); HandleVersionAddChange(changeNotification.GetInsertEntries()); } if (!changeNotification.GetUpdateEntries().empty() || - changeNotification.GetUpdateEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetUpdateEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle version data update change"); HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); } if (!changeNotification.GetDeleteEntries().empty() || - changeNotification.GetDeleteEntries().size() > MAX_DB_DATA_SIZE) { + changeNotification.GetDeleteEntries().size() <= MAX_DB_DATA_SIZE) { DHLOGI("Handle version data delete change"); HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); } diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 9ff3990b..a897fd3a 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -52,7 +52,7 @@ const DeviceInfo& DHContext::GetDeviceInfo() void DHContext::AddOnlineDevice(const std::string &uuid, const std::string &networkId) { std::unique_lock lock(onlineDevMutex_); - if (onlineDeviceMap_.size() >= MAX_ONLINE_DEVICE_SIZE || deviceIdUUIDMap_.size() >= MAX_ONLINE_DEVICE_SIZE) { + if (onlineDeviceMap_.size() > MAX_ONLINE_DEVICE_SIZE || deviceIdUUIDMap_.size() > MAX_ONLINE_DEVICE_SIZE) { DHLOGE("OnlineDeviceMap or deviceIdUUIDMap is over size!"); return; } diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index bc0768fb..8b49ad54 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -82,7 +82,7 @@ std::string GetUUIDBySoftBus(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { - if (uuid.size() == 0 || uuid.size() > MAX_STRING_LEN) { + if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { DHLOGE("uuid is invalid!"); return ""; } -- Gitee From 2eb68eca77bc81235bb639f96a17e05f21cf6004 Mon Sep 17 00:00:00 2001 From: tangfan Date: Mon, 26 Sep 2022 14:15:25 +0800 Subject: [PATCH 3/3] fix secure problem Signed-off-by: tangfan --- common/utils/include/distributed_hardware_errno.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 3cfb2a12..d5aa39cc 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -61,7 +61,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; constexpr int32_t ERR_DH_FWK_RESOURCE_DEVICE_ID_NOT_EXIST = -10412; constexpr int32_t ERR_DH_FWK_RESOURCE_UUID_NOT_FOUND = -10413; - constexpr int32_t ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID = -10415; + constexpr int32_t ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID = -10414; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; @@ -86,7 +86,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL = -10805; constexpr int32_t ERR_DH_FWK_SERVICE_REMOTE_IS_NULL = -10806; constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL = -10807; - constexpr int32_t ERR_DH_FWK_SERVICE_MSG_INVALID = -10809; + constexpr int32_t ERR_DH_FWK_SERVICE_MSG_INVALID = -10808; /* AccessManager errno, range: [-10900, -10999] */ constexpr int32_t ERR_DH_FWK_ACCESS_INIT_DM_FAILED = -10900; -- Gitee