From 7a97461e03bc2bfca082b54ab605a7debfbca0af Mon Sep 17 00:00:00 2001 From: libing23 Date: Tue, 8 Aug 2023 21:59:49 +0800 Subject: [PATCH] Description:add app mgr death recipient Match-id-2ac698c5c2f74ba7f9d153827cfee93314bb338a --- .../security_component_service/sa/BUILD.gn | 1 + .../sa/sa_main/app_mgr_death_recipient.cpp | 28 ++++++++++++++++ .../sa/sa_main/app_mgr_death_recipient.h | 33 +++++++++++++++++++ .../sa/sa_main/sec_comp_manager.cpp | 32 +++++++++++++++++- .../sa/sa_main/sec_comp_manager.h | 1 + .../sa/sa_main/sec_comp_service.cpp | 15 ++++++++- .../sa/test/BUILD.gn | 2 ++ .../mock/include/if_system_ability_manager.h | 7 ++++ 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp create mode 100644 services/security_component_service/sa/sa_main/app_mgr_death_recipient.h diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 3703091..bb0d7aa 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -46,6 +46,7 @@ ohos_shared_library("security_component_service") { ] sources = [ + "sa_main/app_mgr_death_recipient.cpp", "sa_main/app_state_observer.cpp", "sa_main/delay_exit_task.cpp", "sa_main/first_use_dialog.cpp", diff --git a/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp new file mode 100644 index 0000000..7e37725 --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 "app_mgr_death_recipient.h" +#include "sec_comp_manager.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +void AppMgrDeathRecipient::OnRemoteDied(const wptr& object) +{ + SecCompManager::GetInstance().ExitWhenAppMgrDied(); +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS + diff --git a/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h new file mode 100644 index 0000000..d4dc41c --- /dev/null +++ b/services/security_component_service/sa/sa_main/app_mgr_death_recipient.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 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 APP_MGR_DEATH_RECIPIENT_H +#define APP_MGR_DEATH_RECIPIENT_H + +#include "iremote_object.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient { +public: + AppMgrDeathRecipient() {} + virtual ~AppMgrDeathRecipient() = default; + void OnRemoteDied(const wptr& object) override; +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // APP_MGR_DEATH_RECIPIENT_H + diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index fc43275..1737413 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -218,6 +218,7 @@ void SecCompManager::NotifyProcessDied(int32_t pid) // notify enhance process died. SecCompEnhanceAdapter::NotifyProcessDied(pid); + RemoveAppFromMaliciousAppList(pid); OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { @@ -235,7 +236,6 @@ void SecCompManager::NotifyProcessDied(int32_t pid) SecCompEnhanceAdapter::DisableInputEnhance(); } - RemoveAppFromMaliciousAppList(pid); DelayExitTask::GetInstance().Start(); } @@ -264,6 +264,36 @@ void SecCompManager::ExitSaProcess() SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); } +void SecCompManager::ExitWhenAppMgrDied() +{ + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { + std::vector& list = iter->second.compList; + for (auto it = list.begin(); it != list.end(); ++it) { + it->RevokeTempPermission(); + } + list.clear(); + } + componentMap_.clear(); + + // no need exit enhance service, only disable input enhance. + SecCompEnhanceAdapter::DisableInputEnhance(); + isSaExit_ = true; + + SC_LOG_INFO(LABEL, "app mgr died, start sa exit"); + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + SC_LOG_ERROR(LABEL, "failed to get SystemAbilityManager."); + return; + } + int32_t ret = systemAbilityMgr->UnloadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE); + if (ret != SC_OK) { + SC_LOG_ERROR(LABEL, "failed to UnloadSystemAbility service! errcode=%{public}d", ret); + return; + } + SC_LOG_INFO(LABEL, "UnloadSystemAbility successfully!"); +} + void SecCompManager::SendCheckInfoEnhanceSysEvent(int32_t scId, SecCompType type, const std::string& scene, int32_t res) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index dcf58ee..fd974ce 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -65,6 +65,7 @@ public: void DumpSecComp(std::string& dumpStr); bool Initialize(); void ExitSaProcess(); + void ExitWhenAppMgrDied(); private: SecCompManager(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 2389aca..3cbfb5d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -17,6 +17,7 @@ #include +#include "app_mgr_death_recipient.h" #include "hisysevent.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -107,7 +108,8 @@ bool SecCompService::RegisterAppStateObserver() appStateObserver_ = nullptr; return false; } - iAppMgr_ = iface_cast(samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID)); + auto remoteObject = samgrClient->GetSystemAbility(APP_MGR_SERVICE_ID); + iAppMgr_ = iface_cast(remoteObject); if (iAppMgr_ == nullptr) { SC_LOG_ERROR(LABEL, "Failed to get ability manager service"); appStateObserver_ = nullptr; @@ -121,6 +123,17 @@ bool SecCompService::RegisterAppStateObserver() return false; } + sptr appMgrDeathRecipient = new (std::nothrow) AppMgrDeathRecipient(); + if (appMgrDeathRecipient == nullptr) { + SC_LOG_ERROR(LABEL, "Alloc appMgr death observer fail"); + return false; + } + + if (!remoteObject->AddDeathRecipient(appMgrDeathRecipient)) { + SC_LOG_ERROR(LABEL, "Add service death observer fail"); + return false; + } + std::vector list; if (iAppMgr_->GetForegroundApplications(list) == ERR_OK) { for (auto it = list.begin(); it != list.end(); ++it) { diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index c35aa14..80d1d9f 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -32,6 +32,7 @@ ohos_unittest("sec_comp_service_test") { ] sources = [ + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", @@ -94,6 +95,7 @@ ohos_unittest("sec_comp_service_mock_test") { ] sources = [ + "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/first_use_dialog.cpp", diff --git a/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h index e67a1a1..eee66c0 100644 --- a/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h +++ b/services/security_component_service/sa/test/mock/include/if_system_ability_manager.h @@ -21,6 +21,13 @@ namespace OHOS { class MockIRemoteObject : public virtual RefBase { +public: + bool AddDeathRecipient(const sptr& recipient) + { + (void)recipient; + return addResult; + } + bool addResult = true; }; template inline sptr iface_cast(const sptr &object)