diff --git a/services/security_component_service/sa/sa_main/app_state_observer.cpp b/services/security_component_service/sa/sa_main/app_state_observer.cpp index be4fa4e23221265154929bd36e369f0c094202e9..9a77b3a2146474eb55342fd45c049b8072124bda 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.cpp +++ b/services/security_component_service/sa/sa_main/app_state_observer.cpp @@ -22,6 +22,7 @@ namespace Security { namespace SecurityComponent { namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "AppStateObserver"}; +constexpr int32_t APP_STATE_CACHED = 100; } AppStateObserver::AppStateObserver() @@ -85,11 +86,11 @@ void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::ProcessData & AddProcessToForegroundSet(processData.pid, proc); } -void AppStateObserver::RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData &processData) +void AppStateObserver::RemoveProcessFromForegroundSet(const int32_t pid) { Utils::UniqueWriteGuard infoGuard(this->fgProcLock_); for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) { - if (processData.pid == iter->pid) { + if (pid == iter->pid) { foregrandProcList_.erase(iter); return; } @@ -102,7 +103,7 @@ void AppStateObserver::OnProcessStateChanged(const AppExecFwk::ProcessData &proc AddProcessToForegroundSet(processData); SecCompManager::GetInstance().NotifyProcessForeground(processData.pid); } else if (processData.state == AppExecFwk::AppProcessState::APP_STATE_BACKGROUND) { - RemoveProcessFromForegroundSet(processData); + RemoveProcessFromForegroundSet(processData.pid); SecCompManager::GetInstance().NotifyProcessBackground(processData.pid); } } @@ -111,8 +112,19 @@ void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData& processData) { SC_LOG_INFO(LABEL, "OnProcessDied die %{public}s pid %{public}d", processData.bundleName.c_str(), processData.pid); - RemoveProcessFromForegroundSet(processData); - SecCompManager::GetInstance().NotifyProcessDied(processData.pid); + RemoveProcessFromForegroundSet(processData.pid); + SecCompManager::GetInstance().NotifyProcessDied(processData.pid, false); +} + +void AppStateObserver::OnAppCacheStateChanged(const AppExecFwk::AppStateData &appStateData) +{ + SC_LOG_INFO(LABEL, "OnAppCacheStateChanged pid %{public}d", + appStateData.pid); + if (appStateData.state != APP_STATE_CACHED) { + return; + } + RemoveProcessFromForegroundSet(appStateData.pid); + SecCompManager::GetInstance().NotifyProcessDied(appStateData.pid, true); } void AppStateObserver::DumpProcess(std::string& dumpStr) diff --git a/services/security_component_service/sa/sa_main/app_state_observer.h b/services/security_component_service/sa/sa_main/app_state_observer.h index 56998a00217a643c25a230aec946f233f1a0b78f..ad2d13201feae81f939a00d950215bf698cd931a 100644 --- a/services/security_component_service/sa/sa_main/app_state_observer.h +++ b/services/security_component_service/sa/sa_main/app_state_observer.h @@ -43,9 +43,10 @@ public: void AddProcessToForegroundSet(const AppExecFwk::ProcessData& processData); void AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData); void DumpProcess(std::string& dumpStr); + void OnAppCacheStateChanged(const AppExecFwk::AppStateData &appStateData) override; private: - void RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData& processData); + void RemoveProcessFromForegroundSet(const int32_t pid); std::vector foregrandProcList_; OHOS::Utils::RWLock fgProcLock_; }; diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index 445c621956bec2dda92ca269d139a350ce9f4f26..600e4c73f6937997a6bcff2c89c536d6b8d22ed6 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -265,6 +265,7 @@ void FirstUseDialog::RemoveDialogWaitEntitys(int32_t pid) std::shared_ptr entity = iter->second; if ((entity != nullptr) && (entity->pid_ == pid)) { iter = dialogWaitMap_.erase(iter); + SC_LOG_ERROR(LABEL, "dialog %{public}d is removed", pid); } else { ++iter; } 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 7ec4a4e97bd346692ae336e00281b8a7ba4d6c75..2687ce1ae46ce55a108e7ac9e18453d7109c8363 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 @@ -226,12 +226,13 @@ void SecCompManager::NotifyProcessBackground(int32_t pid) SC_LOG_INFO(LABEL, "App pid %{public}d to background", pid); } -void SecCompManager::NotifyProcessDied(int32_t pid) +void SecCompManager::NotifyProcessDied(int32_t pid, bool isProcessCached) { + if (!isProcessCached) { // notify enhance process died. - SecCompEnhanceAdapter::NotifyProcessDied(pid); - - malicious_.RemoveAppFromMaliciousAppList(pid); + SecCompEnhanceAdapter::NotifyProcessDied(pid); + malicious_.RemoveAppFromMaliciousAppList(pid); + } OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); auto iter = componentMap_.find(pid); if (iter == componentMap_.end()) { 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 b57b3634d57fc816a8885194a3e3669ac30ed2f4..4d6ce0577a8e3d8ad06b8bde202c13be9d9530ad 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 @@ -61,7 +61,7 @@ public: const SecCompCallerInfo& caller, const std::vector>& remote, std::string& message); void NotifyProcessForeground(int32_t pid); void NotifyProcessBackground(int32_t pid); - void NotifyProcessDied(int32_t pid); + void NotifyProcessDied(int32_t pid, bool isProcessCached); void DumpSecComp(std::string& dumpStr); bool Initialize(); void ExitSaProcess(); diff --git a/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h b/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h index 9271160512de5d5ad6d8bd52177b88b095a2623d..35c98908370ec8db13ea485c2ba4eec9faa152fd 100644 --- a/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h +++ b/services/security_component_service/sa/test/mock/include/application_state_observer_stub.h @@ -60,6 +60,7 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.IApplicationStateObserver"); virtual void OnProcessStateChanged(const ProcessData &processData) {} virtual void OnProcessDied(const ProcessData &processData) = 0; + virtual void OnAppCacheStateChanged(const AppExecFwk::AppStateData &appStateData) = 0; }; class ApplicationStateObserverStub : public IRemoteStub { diff --git a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp index ab47da8355b67f4605d7918abe736103e0214b0c..deb2b199ae7b3adcf533de478c3cc09a8b61ab70 100644 --- a/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/app_state_observer_test.cpp @@ -105,8 +105,8 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet001, TestSize.Level }; observer_->AddProcessToForegroundSet(procData); ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); - observer_->RemoveProcessFromForegroundSet(procData); - observer_->RemoveProcessFromForegroundSet(procData); + observer_->RemoveProcessFromForegroundSet(procData.pid); + observer_->RemoveProcessFromForegroundSet(procData.pid); ASSERT_FALSE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } @@ -125,7 +125,7 @@ HWTEST_F(AppStateObserverTest, RemoveProcessFromForegroundSet002, TestSize.Level observer_->AddProcessToForegroundSet(procData); ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); procData.pid = ServiceTestCommon::TEST_PID_2; - observer_->RemoveProcessFromForegroundSet(procData); + observer_->RemoveProcessFromForegroundSet(procData.pid); ASSERT_TRUE(observer_->IsProcessForeground(ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_UID_1)); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 5f961960179b75164dbc421cf432638eb74a48e0..e061d8cc31eb9458885445c391336d98443139e2 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -277,7 +277,7 @@ HWTEST_F(SecCompManagerTest, NotifyProcessBackground001, TestSize.Level1) ASSERT_TRUE(SecCompManager::GetInstance().IsForegroundCompExist()); SecCompManager::GetInstance().NotifyProcessBackground(ServiceTestCommon::TEST_PID_1); ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); - SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1); + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1, false); ASSERT_FALSE(SecCompManager::GetInstance().IsForegroundCompExist()); } @@ -313,11 +313,11 @@ HWTEST_F(SecCompManagerTest, NotifyProcessDied001, TestSize.Level1) ASSERT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_2, 0, entity2)); - SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_3); + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_3, false); ASSERT_NE(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); - SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1); + SecCompManager::GetInstance().NotifyProcessDied(ServiceTestCommon::TEST_PID_1, false); ASSERT_EQ(nullptr, SecCompManager::GetInstance().GetSecurityComponentFromList( ServiceTestCommon::TEST_PID_1, ServiceTestCommon::TEST_SC_ID_1)); }