From 373adfe97087b0ce2017eb68f3031b832cfa8a00 Mon Sep 17 00:00:00 2001 From: wangzhen Date: Wed, 30 Apr 2025 15:46:35 +0800 Subject: [PATCH] Add permission check for collaborator kill Signed-off-by: wangzhen Change-Id: I0197346f51850b121fff3080ef658011adda55cc --- .../include/ability_manager_service.h | 2 + .../src/ability_manager_service.cpp | 60 +++++++++++-------- services/common/include/event_handler_wrap.h | 4 +- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 79b3e74c31..0d263c29fd 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2076,6 +2076,8 @@ protected: const std::string &bundleName, const std::vector &abilityNames, const std::vector &uiExtensionNames) override; + int32_t GetCollaboratorType(const std::string &codePath) const; + int32_t KillProcessForCollaborator(int32_t collaboratorType, const std::string &bundleName, int32_t userId); private: int TerminateAbilityWithFlag(const sptr &token, int resultCode = DEFAULT_INVAL_VALUE, const Want *resultWant = nullptr, bool flag = true); diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 6242a23294..b65d166a67 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -6860,12 +6860,7 @@ int AbilityManagerService::GenerateAbilityRequest(const Want &want, int requestC request.abilityInfo = abilityInfo->abilityInfo; request.extensionProcessMode = abilityInfo->extensionProcessMode; request.customProcess = abilityInfo->customProcess; - - if (request.abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::RESERVE_TYPE)) { - request.collaboratorType = CollaboratorType::RESERVE_TYPE; - } else if (request.abilityInfo.applicationInfo.codePath == std::to_string(CollaboratorType::OTHERS_TYPE)) { - request.collaboratorType = CollaboratorType::OTHERS_TYPE; - } + request.collaboratorType = GetCollaboratorType(request.abilityInfo.applicationInfo.codePath); if (request.abilityInfo.type == AppExecFwk::AbilityType::SERVICE && request.abilityInfo.isStageBasedModel) { TAG_LOGI(AAFwkTag::ABILITYMGR, "stage mode, abilityInfo SERVICE type reset EXTENSION"); @@ -7120,23 +7115,9 @@ int AbilityManagerService::KillProcess(const std::string &bundleName, bool clear return GET_BUNDLE_INFO_FAILED; } - int32_t collaboratorType = -1; - if (bundleInfo.applicationInfo.codePath == std::to_string(CollaboratorType::RESERVE_TYPE)) { - collaboratorType = CollaboratorType::RESERVE_TYPE; - } else if (bundleInfo.applicationInfo.codePath == std::to_string(CollaboratorType::OTHERS_TYPE)) { - collaboratorType = CollaboratorType::OTHERS_TYPE; - } - if (collaboratorType != -1) { - TAG_LOGI(AAFwkTag::ABILITYMGR, "Collaborator kill"); - auto collaborator = GetCollaborator(collaboratorType); - if (collaborator == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "Collaborator null"); - return KILL_PROCESS_FAILED; - } - if (collaborator->NotifyKillProcesses(bundleName, userId) != ERR_OK) { - return KILL_PROCESS_FAILED; - } - return ERR_OK; + int32_t collaboratorType = GetCollaboratorType(bundleInfo.applicationInfo.codePath); + if (CheckCollaboratorType(collaboratorType)) { + return KillProcessForCollaborator(collaboratorType, bundleName, userId); } KeepAliveType type; @@ -7147,10 +7128,29 @@ int AbilityManagerService::KillProcess(const std::string &bundleName, bool clear } int ret = DelayedSingleton::GetInstance()->KillApplication(bundleName, clearPageStack, appIndex); - if (ret != ERR_OK) { + return ret == ERR_OK ? ERR_OK : KILL_PROCESS_FAILED; +} + +int32_t AbilityManagerService::KillProcessForCollaborator(int32_t collaboratorType, + const std::string &bundleName, int32_t userId) +{ + TAG_LOGI(AAFwkTag::ABILITYMGR, "Collaborator kill"); + auto collaborator = GetCollaborator(collaboratorType); + if (collaborator == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "Collaborator null"); + return KILL_PROCESS_FAILED; + } + auto isSaCall = PermissionVerification::GetInstance()->IsSACall(); + auto isShellCall = PermissionVerification::GetInstance()->IsShellCall(); + auto isCallingPerm = PermissionVerification::GetInstance()->VerifyCallingPermission( + PermissionConstants::PERMISSION_KILL_APP_PROCESSES); + if (!isCallingPerm && !isSaCall && !isShellCall) { + TAG_LOGE(AAFwkTag::APPMGR, "KillProcess permission verification fail"); + return ERR_PERMISSION_DENIED; + } + if (collaborator->NotifyKillProcesses(bundleName, userId) != ERR_OK) { return KILL_PROCESS_FAILED; } - return ERR_OK; } @@ -11222,6 +11222,16 @@ int32_t AbilityManagerService::UnregisterIAbilityManagerCollaborator(int32_t typ return ERR_OK; } +int32_t AbilityManagerService::GetCollaboratorType(const std::string &codePath) const +{ + if (codePath == std::to_string(CollaboratorType::RESERVE_TYPE)) { + return CollaboratorType::RESERVE_TYPE; + } else if (codePath == std::to_string(CollaboratorType::OTHERS_TYPE)) { + return CollaboratorType::OTHERS_TYPE; + } + return 0; +} + sptr AbilityManagerService::GetCollaborator(int32_t type) { if (!CheckCollaboratorType(type)) { diff --git a/services/common/include/event_handler_wrap.h b/services/common/include/event_handler_wrap.h index c657fa4eaf..ee43770a41 100644 --- a/services/common/include/event_handler_wrap.h +++ b/services/common/include/event_handler_wrap.h @@ -110,10 +110,10 @@ public: } private: bool isExtension_ = false; - uint32_t eventId_; + uint32_t eventId_ = 0; uint32_t timeout_ = 0; int runCount_ = 0; - int64_t param_; + int64_t param_ = 0; std::shared_ptr eventData_; TaskHandle eventTask_; std::string taskName_;