diff --git a/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn b/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn deleted file mode 100644 index 93bb21bc3ce2157e61f30ba3750eebb27ea6028d..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (c) 2025 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import("//build/ohos.gni") -import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") - -ohos_source_set("distributedata_bundlemgr") { - branch_protector_ret = "pac_ret" - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false - boundary_sanitize = true - ubsan = true - } - sources = [ "bundlemgr_adapter.cpp" ] - - cflags_cc = [ "-fvisibility=hidden" ] - - if (build_public_version) { - cflags_cc += [ "-DCONFIG_PUBLIC_VERSION" ] - } - - include_dirs = [ "${data_service_path}/adapter/include/bundle_mgr" ] - ldflags = [ "-Wl,--exclude-libs,ALL" ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_core", - "hilog:libhilog", - "kv_store:datamgr_common", - ] - subsystem_name = "distributeddatamgr" - part_name = "datamgr_service" - defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] -} diff --git a/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp b/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp deleted file mode 100644 index fd17f542764623eb737e1411ee49372e5760e30c..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -#define LOG_TAG "BundleMgrAdapter" - -#include "bundlemgr_adapter.h" -#include -#include "accesstoken_kit.h" -#include "bundlemgr/bundle_mgr_proxy.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "log_print.h" -#include "system_ability_definition.h" - -namespace OHOS { -namespace DistributedData { -BundleMgrAdapter::BundleMgrAdapter() -{ -} -BundleMgrAdapter::~BundleMgrAdapter() -{ -} -BundleMgrAdapter& BundleMgrAdapter::GetInstance() -{ - static BundleMgrAdapter instance; - return instance; -} - -std::string BundleMgrAdapter::GetKey(const std::string &bundleName, int32_t userId) -{ - return bundleName + "###" + std::to_string(userId); -} - -std::string BundleMgrAdapter::GetAppidFromCache(const std::string &bundleName, int32_t userId) -{ - std::string appId; - std::string key = GetKey(bundleName, userId); - appIds_.Get(key, appId); - return appId; -} - -std::string BundleMgrAdapter::GetBundleAppId(const std::string &bundleName, int32_t userId) -{ - std::string appId = GetAppidFromCache(bundleName, userId); - if (!appId.empty()) { - return appId; - } - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - ZLOGE("Failed to get system ability mgr."); - return ""; - } - auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundleMgrProxy == nullptr) { - ZLOGE("Failed to Get BMS SA."); - return ""; - } - auto bundleManager = iface_cast(bundleMgrProxy); - if (bundleManager == nullptr) { - ZLOGE("Failed to get bundle manager"); - return ""; - } - appId = bundleManager->GetAppIdByBundleName(bundleName, userId); - if (appId.empty()) { - ZLOGE("GetAppIdByBundleName failed appId:%{public}s, bundleName:%{public}s, uid:%{public}d", - appId.c_str(), bundleName.c_str(), userId); - } else { - appIds_.Set(GetKey(bundleName, userId), appId); - } - return appId; -} - -void BundleMgrAdapter::DeleteCache(const std::string &bundleName, int32_t user) -{ - std::string key = GetKey(bundleName, user); - appIds_.Delete(key); -} - -void BundleMgrAdapter::ClearCache() -{ - appIds_.ResetCapacity(0); - appIds_.ResetCapacity(CACHE_SIZE); -} -} // namespace DistributedData -} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h b/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h deleted file mode 100644 index 5519c9b105b12093cbf1ee67e69b56470e3d047d..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -* Copyright (c) 2025 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H -#include "lru_bucket.h" -#include "visibility.h" -namespace OHOS { -namespace DistributedData { -class BundleMgrAdapter { -public: - BundleMgrAdapter(); - ~BundleMgrAdapter(); - API_EXPORT static BundleMgrAdapter& GetInstance(); - API_EXPORT std::string GetBundleAppId(const std::string &bundleName, int32_t userId); - API_EXPORT void DeleteCache(const std::string &bundleName, int32_t user); - API_EXPORT void ClearCache(); - -private: - std::string GetAppidFromCache(const std::string &bundleName, int32_t userId); - std::string GetKey(const std::string &bundleName, int32_t userId); - static constexpr int32_t CACHE_SIZE = 32; - LRUBucket appIds_ {CACHE_SIZE}; -}; -} // namespace DistributedData -} // namespace OHOS -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H \ No newline at end of file diff --git a/services/distributeddataservice/app/src/checker/BUILD.gn b/services/distributeddataservice/app/src/checker/BUILD.gn index 0ad405bb94ee9c00e84585aa25c2561e1d55fb58..f0fa8dd258432a3254d58b3173dd84958c6fa2d7 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -32,10 +32,7 @@ ohos_source_set("distributeddata_checker") { "-Oz", ] - include_dirs = [ - "${data_service_path}/adapter/include", - "${data_service_path}/framework/include", - ] + include_dirs = [ "${data_service_path}/framework/include" ] if (build_public_version) { cflags_cc += [ "-DCONFIG_PUBLIC_VERSION" ] diff --git a/services/distributeddataservice/app/src/checker/bundle_checker.cpp b/services/distributeddataservice/app/src/checker/bundle_checker.cpp index 1bfc0fd2187febc30e4630d7bf1ba727ba7071e5..b726ab915be6d6ba3efe6693eb0825b2b93122bb 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.cpp +++ b/services/distributeddataservice/app/src/checker/bundle_checker.cpp @@ -18,7 +18,6 @@ #include #include "accesstoken_kit.h" #include "bundlemgr/bundle_mgr_proxy.h" -#include "bundle_mgr/bundlemgr_adapter.h" #include "hap_token_info.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -62,13 +61,63 @@ bool BundleChecker::SetSwitchesInfo(const CheckerManager::Switches &switches) return true; } +std::string BundleChecker::GetKey(const std::string &bundleName, int32_t userId) +{ + return bundleName + "###" + std::to_string(userId); +} + +std::string BundleChecker::GetAppidFromCache(const std::string &bundleName, int32_t userId) +{ + std::string appId; + std::string key = GetKey(bundleName, userId); + appIds_.Get(key, appId); + return appId; +} + std::string BundleChecker::GetBundleAppId(const CheckerManager::StoreInfo &info) { int32_t userId = info.uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = BundleMgrAdapter::GetInstance().GetBundleAppId(info.bundleName, userId); + std::string appId = GetAppidFromCache(info.bundleName, userId); + if (!appId.empty()) { + return appId; + } + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return ""; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + return ""; + } + auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); + return ""; + } + appId = bundleManager->GetAppIdByBundleName(info.bundleName, userId); + if (appId.empty()) { + ZLOGE("GetAppIdByBundleName failed appId:%{public}s, bundleName:%{public}s, uid:%{public}d", + appId.c_str(), info.bundleName.c_str(), userId); + } else { + appIds_.Set(GetKey(info.bundleName, userId), appId); + } return appId; } +void BundleChecker::DeleteCache(const std::string &bundleName, int32_t user, int32_t index) +{ + std::string key = GetKey(bundleName, user); + appIds_.Delete(key); +} + +void BundleChecker::ClearCache() +{ + appIds_.ResetCapacity(0); + appIds_.ResetCapacity(CACHE_SIZE); +} + std::string BundleChecker::GetAppId(const CheckerManager::StoreInfo &info) { if (AccessTokenKit::GetTokenTypeFlag(info.tokenId) != TOKEN_HAP) { diff --git a/services/distributeddataservice/app/src/checker/bundle_checker.h b/services/distributeddataservice/app/src/checker/bundle_checker.h index b0c63431a207cd374abb37605c3bebaf3e1227ed..d5c655bb92add0fc2b61a427e906c18e33c19c2a 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.h +++ b/services/distributeddataservice/app/src/checker/bundle_checker.h @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_BUNDLE_CHECKER_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_BUNDLE_CHECKER_H #include "checker/checker_manager.h" +#include "lru_bucket.h" namespace OHOS { namespace DistributedData { class BundleChecker : public CheckerManager::Checker { @@ -36,7 +37,12 @@ public: std::vector GetStaticStores() override; bool IsDynamic(const CheckerManager::StoreInfo &info) override; bool IsStatic(const CheckerManager::StoreInfo &info) override; + void DeleteCache(const std::string &bundleName, int32_t user, int32_t index) override; + void ClearCache() override; private: + static constexpr int32_t CACHE_SIZE = 32; + std::string GetAppidFromCache(const std::string &bundleName, int32_t userId); + std::string GetKey(const std::string &bundleName, int32_t userId); static BundleChecker instance_; std::map trusts_; std::map distrusts_; @@ -44,6 +50,7 @@ private: std::vector dynamicStores_; std::vector staticStores_; std::string GetBundleAppId(const CheckerManager::StoreInfo &info); + LRUBucket appIds_ { CACHE_SIZE }; }; } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 9d505889925ad35805c945fb680a6bbaf6fba55d..51af0c61e09fed7a55bceae2b34aa11a97ed40ce 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -29,7 +29,6 @@ #include "auth_delegate.h" #include "auto_launch_export.h" #include "bootstrap.h" -#include "bundle_mgr/bundlemgr_adapter.h" #include "checker/checker_manager.h" #include "communication_provider.h" #include "communicator_context.h" @@ -931,7 +930,7 @@ void KvStoreDataService::AccountEventChanged(const AccountEventInfo &eventInfo) MetaDataManager::GetInstance().DelMeta(StoreMetaMapping(meta).GetKey(), true); PermitDelegate::GetInstance().DelCache(meta.GetKeyWithoutPath()); } - BundleMgrAdapter::GetInstance().ClearCache(); + CheckerManager::GetInstance().ClearCache(); g_kvStoreAccountEventStatus = 0; break; } @@ -1051,7 +1050,7 @@ void KvStoreDataService::OnSessionReady(const AppDistributedKv::DeviceInfo &info int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t user, int32_t index) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUninstall(bundleName, user, index); @@ -1062,7 +1061,7 @@ int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t u int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user, int32_t index) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUpdate(bundleName, user, index); @@ -1073,7 +1072,7 @@ int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user int32_t KvStoreDataService::OnInstall(const std::string &bundleName, int32_t user, int32_t index) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppInstall(bundleName, user, index); @@ -1094,7 +1093,7 @@ int32_t KvStoreDataService::OnScreenUnlocked(int32_t user) int32_t KvStoreDataService::ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, userId); + CheckerManager::GetInstance().DeleteCache(bundleName, userId, appIndex); auto callerToken = IPCSkeleton::GetCallingTokenID(); NativeTokenInfo nativeTokenInfo; if (AccessTokenKit::GetNativeTokenInfo(callerToken, nativeTokenInfo) != RET_SUCCESS || diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 80725c641980d70476245e9e674f7dd0e17ceedc..d79c567b82e43464b623d1a8631c8cd23ef2f959 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -560,6 +560,30 @@ ohos_unittest("RouteHeadHandlerImplTest") { part_name = "datamgr_service" } +ohos_unittest("BundleCheckerTest") { + module_out_path = module_output_path + sources = [ "unittest/bundle_checker_test.cpp" ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "openssl:libcrypto_shared", + ] + + deps = [ + "${data_service_path}/app/src/checker:distributeddata_checker", + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] +} + ############################################################################### group("unittest") { @@ -567,6 +591,7 @@ group("unittest") { deps = [] deps += [ + ":BundleCheckerTest", ":FeatureStubImplTest", ":KvStoreDataServiceClearTest", ":KvStoreDataServiceTest", diff --git a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deb5c7def0c2f786a65477ee618ac6e1146cd0b4 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp @@ -0,0 +1,118 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "accesstoken_kit.h" +#include "checker/bundle_checker.h" +#include "bootstrap.h" +#include "hap_token_info.h" +#include "nativetoken_kit.h" +#include "utils/crypto.h" +using namespace testing::ext; +using namespace OHOS::DistributedData; +using namespace OHOS::Security::AccessToken; +class BundleCheckerTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + void SetUp(); + void TearDown(); + NativeTokenInfoParams infoInstance{0}; +}; + +void BundleCheckerTest::SetUp(void) +{ + infoInstance.dcapsNum = 0; + infoInstance.permsNum = 0; + infoInstance.aclsNum = 0; + infoInstance.dcaps = nullptr; + infoInstance.perms = nullptr; + infoInstance.acls = nullptr; + infoInstance.processName = "BundleCheckerTest"; + infoInstance.aplStr = "system_core"; + + HapInfoParams info = { + .userID = 100, + .bundleName = "com.ohos.dlpmanager", + .instIndex = 0, + .appIDDesc = "com.ohos.dlpmanager" + }; + PermissionDef infoManagerTestPermDef = { + .permissionName = "ohos.permission.test", + .bundleName = "ohos.test.demo", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "open the door", + .descriptionId = 1 + }; + PermissionStateFull infoManagerTestState = { + .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = {"local"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1} + }; + HapPolicyParams policy = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {infoManagerTestPermDef}, + .permStateList = {infoManagerTestState} + }; + AccessTokenKit::AllocHapToken(info, policy); + + Bootstrap::GetInstance().LoadComponents(); + Bootstrap::GetInstance().LoadDirectory(); + Bootstrap::GetInstance().LoadCheckers(); + Bootstrap::GetInstance().LoadNetworks(); +} + +void BundleCheckerTest::TearDown() +{ + auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); + AccessTokenKit::DeleteToken(tokenId); +} + +/** +* @tc.name: GetAppIdTest001 +* @tc.desc: get appId from cache. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(BundleCheckerTest, GetAppIdTest001, TestSize.Level0) +{ + CheckerManager::StoreInfo info; + auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); + info.uid = 100 * 200000; + info.tokenId = tokenId; + info.bundleName = "com.ohos.dlpmanager"; + BundleChecker checker; + std::string appIdInCache; + ASSERT_TRUE(!checker.GetAppId(info).empty()); + checker.appIds_.Get("com.ohos.dlpmanager###100", appIdInCache); + ASSERT_FALSE(appIdInCache.empty()); + ASSERT_EQ(Crypto::Sha256(appIdInCache), checker.GetAppId(info)); + + checker.DeleteCache(info.bundleName, 100, 0); + std::string appIdInCache1; + checker.appIds_.Get("com.ohos.dlpmanager###100", appIdInCache1); + ASSERT_TRUE(appIdInCache1.empty()); + + checker.GetAppId(info); + ASSERT_GE(checker.appIds_.Size(), 1); + checker.ClearCache(); + ASSERT_EQ(checker.appIds_.Size(), 0); +} \ No newline at end of file diff --git a/services/distributeddataservice/framework/checker/checker_manager.cpp b/services/distributeddataservice/framework/checker/checker_manager.cpp index e02f50c64a219711e8456453445ac5c0bb8d33d3..739ff5ce6fec69087428a17f5ccc03e48ccf1093 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -47,6 +47,26 @@ void CheckerManager::RegisterPlugin(const std::string &checker, std::functionDeleteCache(bundleName, user, index); + } +} + +void CheckerManager::ClearCache() +{ + for (auto &[name, checker] : checkers_) { + if (checker == nullptr) { + continue; + } + checker->ClearCache(); + } +} + std::string CheckerManager::GetAppId(const StoreInfo &info) { for (auto &[name, checker] : checkers_) { diff --git a/services/distributeddataservice/framework/include/checker/checker_manager.h b/services/distributeddataservice/framework/include/checker/checker_manager.h index b0ad878e150a71fd97d486cdf014da85be637f33..f42f43c616b4a4f1b5626e6a8aa79f0ccf038270 100644 --- a/services/distributeddataservice/framework/include/checker/checker_manager.h +++ b/services/distributeddataservice/framework/include/checker/checker_manager.h @@ -53,6 +53,8 @@ public: virtual std::vector GetStaticStores() = 0; virtual bool IsDynamic(const StoreInfo &info) = 0; virtual bool IsStatic(const StoreInfo &info) = 0; + virtual void DeleteCache(const std::string &bundleName, int32_t user, int32_t index){}; + virtual void ClearCache(){}; protected: API_EXPORT ~Checker() = default; }; @@ -68,6 +70,8 @@ public: API_EXPORT bool IsSwitches(const StoreInfo &info); API_EXPORT void LoadCheckers(std::vector &checkers); API_EXPORT Checker *GetChecker(const std::string &checker); + API_EXPORT void DeleteCache(const std::string &bundleName, int32_t user, int32_t index); + API_EXPORT void ClearCache(); private: std::map checkers_; diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 2f754c7c10efd11e33e9ec191100cb747fafa073..37a5bafddd07269758be7cb49b3601c027d7f709 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -366,7 +366,6 @@ ohos_unittest("ServiceMetaDataTest") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", - "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/utils:distributeddata_utils", "${data_service_path}/app/src/checker:distributeddata_checker", diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index b0cfab3778c3c9456474daa915167f2216b42daf..799699962afa099106f9e7689d8452ef1fc65e57 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -71,7 +71,6 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", - "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/dfx:distributeddata_dfx", "${data_service_path}/adapter/network:distributeddata_network", diff --git a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn index e40e04ef32966d50d40d7750f22de44ebe97e7e0..398162fcc317b76037c3d3ce7bb6fe90e603c982 100644 --- a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn @@ -44,7 +44,6 @@ ohos_fuzztest("UdmfServiceFuzzTest") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", - "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/dfx:distributeddata_dfx", "${data_service_path}/framework:distributeddatasvcfwk", diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index 7b7e648f109939bf02c65bcc6b71d6192f45bb4a..c1d117ddafee12231f90979be7ac0f49b16ddd33 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -188,8 +188,10 @@ bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32 return true; } -bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName) { + specificBundleName = "specificBundleName"; bundleName = "bundleName"; return true; } diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h index e79e2bb6c6ea9e034cf3aa6930f685bf63c04c70..92af6e73e34043d5b05cc21bfb9ed75439cb3c8a 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h @@ -43,7 +43,8 @@ public: static void SetRecordUid(UnifiedData &data); static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); - static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); + static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName); static std::string GetAppId(const std::string &bundleName); static sptr GetBundleMgr(); }; diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp index 93ea1516b8451fc3df93eaf167de190a0c963390..b09a55ffff0a1d66bc7278e188a5cbe2694b028b 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -88,8 +88,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId001, TestSi EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_FAILED)); EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_SHELL)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, false); } @@ -106,8 +107,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId002, TestSi EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_NATIVE)); EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, true); } @@ -122,8 +124,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId003, TestSi uint32_t tokenId = 9999; EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, true); } }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index d1d7d3889b20f83f2aade1ca3ce2b8352b5c837b..6ac317304aabb349a8f08a37283c9d21f49a8e01 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -17,7 +17,6 @@ config("module_public_config") { visibility = [ ":*" ] include_dirs = [ - "${data_service_path}/adapter/include", "${data_service_path}/adapter/include/communicator", "${data_service_path}/service/matrix/include", "${data_service_path}/service/permission/include", diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index f50c0f1c184ab5cb38202f13c6bef85c006aeb6e..597cc9a7a7bca939e2e6d1b25bd5ed3364033347 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -23,6 +23,7 @@ #include "dds_trace.h" #include "udmf_radar_reporter.h" #include "accesstoken_kit.h" +#include "checker/checker_manager.h" #include "device_manager_adapter.h" #include "file_mount_manager.h" #include "iservice_registry.h" @@ -33,7 +34,6 @@ #include "utils/crypto.h" #include "uri_permission_manager_client.h" #include "ipc_skeleton.h" -#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -63,16 +63,17 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option return E_ERROR; } std::string bundleName; - if (!GetSpecificBundleNameByTokenId(option.tokenId, bundleName)) { + std::string specificBundleName; + if (!GetSpecificBundleNameByTokenId(option.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId); return E_ERROR; } std::string intention = it->second; - UnifiedKey key(intention, bundleName, GenerateId()); + UnifiedKey key(intention, specificBundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; - int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = DistributedData::BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId( + { IPCSkeleton::GetCallingUid(), option.tokenId, bundleName }); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); @@ -500,18 +501,21 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) return std::to_string(hapTokenInfo.apiVersion); } -bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName) { Security::AccessToken::HapTokenInfo hapInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { - return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, bundleName); + bundleName = hapInfo.bundleName; + return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, specificBundleName); } if (UTILS::IsTokenNative()) { ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); std::string processName; if (GetNativeProcessNameByToken(tokenId, processName)) { bundleName = std::move(processName); + specificBundleName = bundleName; return true; } } diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 8cd126f55ded11878fdafe991bf0419d26575d2f..e378592c8d9685eea64fe07acf61687200fb5008 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -43,7 +43,8 @@ public: static void SetRecordUid(UnifiedData &data); static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); - static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); + static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName); static sptr GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector& uris, uint32_t tokenId); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 770a10c314cd1625203aae2819465649b28ea30a..36e4deba58ef0d86f6020e2ab934f2999782057d 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -25,6 +25,7 @@ #include "bootstrap.h" #include "bundle_info.h" #include "bundlemgr/bundle_mgr_proxy.h" +#include "checker/checker_manager.h" #include "checker_manager.h" #include "device_manager_adapter.h" #include "device_matrix.h" @@ -43,7 +44,6 @@ #include "unified_data_helper.h" #include "utils/anonymous.h" #include "permission_validator.h" -#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { @@ -417,11 +417,12 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + std::string specificBundleName; + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return E_ERROR; } - if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { + if (key.bundleName != specificBundleName && !HasDatahubPriviledge(bundleName)) { ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str()); return E_INVALID_PARAMETERS; } @@ -437,8 +438,27 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi HandleDbError(key.intention, res); return res; } + auto verifyRes = VerifyUpdatePermission(query, data, bundleName); + if (verifyRes != E_OK) { + ZLOGE("VerifyUpdatePermission failed:%{public}d, key: %{public}s.", verifyRes, query.key.c_str()); + return verifyRes; + } + std::shared_ptr runtime = data.GetRuntime(); + runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); + unifiedData.SetRuntime(*runtime); + PreProcessUtils::SetRecordUid(unifiedData); + if ((res = store->Update(unifiedData)) != E_OK) { + ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); + HandleDbError(key.intention, res); + return E_DB_ERROR; + } + return E_OK; +} + +int32_t UdmfServiceImpl::VerifyUpdatePermission(const QueryOption &query, UnifiedData &data, std::string &bundleName) +{ if (data.IsEmpty()) { - ZLOGE("Invalid parameter, unified data has no record; intention: %{public}s.", key.intention.c_str()); + ZLOGE("Invalid parameter, unified data has no record"); return E_INVALID_PARAMETERS; } std::shared_ptr runtime = data.GetRuntime(); @@ -451,14 +471,6 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi ZLOGE("Update failed: tokenId or appId mismatch, bundleName: %{public}s", bundleName.c_str()); return E_INVALID_PARAMETERS; } - runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); - unifiedData.SetRuntime(*runtime); - PreProcessUtils::SetRecordUid(unifiedData); - if ((res = store->Update(unifiedData)) != E_OK) { - ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); - HandleDbError(key.intention, res); - return E_DB_ERROR; - } return E_OK; } @@ -468,8 +480,8 @@ int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr runtime, const std: ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); return E_INVALID_PARAMETERS; } - int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId( + { IPCSkeleton::GetCallingUid(), runtime->tokenId, bundleName }); if (appId.empty() || appId != runtime->appId) { ZLOGE("Update failed: runtime->appId %{public}s and bundleName appId %{public}s mismatch", runtime->appId.c_str(), appId.c_str()); @@ -538,7 +550,8 @@ bool UdmfServiceImpl::CheckDeleteDataPermission(std::string &appId, const std::s return true; } std::string bundleName; - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + std::string specificBundleName; + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return false; } @@ -1156,12 +1169,13 @@ bool UdmfServiceImpl::IsValidOptionsNonDrag(UnifiedKey &key, const std::string & int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr iUdmfNotifier, std::string &key) { std::string bundleName; + std::string specificBundleName; auto tokenId = static_cast(IPCSkeleton::GetCallingTokenID()); - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(tokenId, bundleName)) { + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", tokenId); return E_ERROR; } - UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); + UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), specificBundleName, dataLoadInfo.sequenceKey); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 8b03412f1f88836c1e38b2fda375134f304fbba7..0260d75fdba8aa8a8dbce25d6736d9bc8fc9a8e6 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -91,6 +91,7 @@ private: const UnifiedData &unifiedData); std::vector ProcessResult(const std::map &results); DistributedData::StoreMetaData BuildMeta(const std::string &storeId, int userId); + int32_t VerifyUpdatePermission(const QueryOption &query, UnifiedData &unifiedData, std::string &bundleName); class Factory { public: