1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "utils/ability_permission_util.h"
17
18 #include "ability_connect_manager.h"
19 #include "ability_info.h"
20 #include "ability_util.h"
21 #include "app_utils.h"
22 #include "accesstoken_kit.h"
23 #include "global_constant.h"
24 #include "hitrace_meter.h"
25 #include "insight_intent_execute_param.h"
26 #include "ipc_skeleton.h"
27 #include "permission_constants.h"
28 #include "permission_verification.h"
29 #include "start_ability_utils.h"
30 #include "utils/app_mgr_util.h"
31 #ifdef SUPPORT_SCREEN
32 #include "scene_board_judgement.h"
33 #include "session_manager_lite.h"
34 #endif // SUPPORT_SCREEN
35
36 using OHOS::Security::AccessToken::AccessTokenKit;
37
38 namespace OHOS {
39 namespace AAFwk {
40 namespace {
41 constexpr const char* IS_DELEGATOR_CALL = "isDelegatorCall";
42 constexpr const char* SETTINGS = "settings";
43 constexpr int32_t BASE_USER_RANGE = 200000;
44 }
45
GetInstance()46 AbilityPermissionUtil &AbilityPermissionUtil::GetInstance()
47 {
48 static AbilityPermissionUtil instance;
49 return instance;
50 }
51
IsDelegatorCall(const AppExecFwk::RunningProcessInfo & processInfo,const AbilityRequest & abilityRequest) const52 inline bool AbilityPermissionUtil::IsDelegatorCall(const AppExecFwk::RunningProcessInfo &processInfo,
53 const AbilityRequest &abilityRequest) const
54 {
55 /* To make sure the AbilityDelegator is not counterfeited
56 * 1. The caller-process must be test-process
57 * 2. The callerToken must be nullptr
58 */
59 if (processInfo.isTestProcess &&
60 !abilityRequest.callerToken && abilityRequest.want.GetBoolParam(IS_DELEGATOR_CALL, false)) {
61 return true;
62 }
63 return false;
64 }
65
IsDominateScreen(const Want & want,bool isPendingWantCaller)66 bool AbilityPermissionUtil::IsDominateScreen(const Want &want, bool isPendingWantCaller)
67 {
68 if (!isPendingWantCaller &&
69 !PermissionVerification::GetInstance()->IsSACall() && !PermissionVerification::GetInstance()->IsShellCall()) {
70 auto callerPid = IPCSkeleton::GetCallingPid();
71 AppExecFwk::RunningProcessInfo processInfo;
72 DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo);
73 bool isDelegatorCall = processInfo.isTestProcess && want.GetBoolParam(IS_DELEGATOR_CALL, false);
74 if (isDelegatorCall || InsightIntentExecuteParam::IsInsightIntentExecute(want)) {
75 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen.");
76 return false;
77 }
78 // add temporarily
79 std::string bundleName = want.GetElement().GetBundleName();
80 std::string abilityName = want.GetElement().GetAbilityName();
81 bool withoutSettings = bundleName.find(SETTINGS) == std::string::npos &&
82 abilityName.find(SETTINGS) == std::string::npos;
83 if (withoutSettings && AppUtils::GetInstance().IsAllowStartAbilityWithoutCallerToken(bundleName, abilityName)) {
84 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen, allow.");
85 return false;
86 } else if (AppUtils::GetInstance().IsAllowStartAbilityWithoutCallerToken(bundleName, abilityName)) {
87 auto bms = AbilityUtil::GetBundleManagerHelper();
88 CHECK_POINTER_RETURN_BOOL(bms);
89 int32_t callerUid = IPCSkeleton::GetCallingUid();
90 std::string callerBundleName;
91 if (IN_PROCESS_CALL(bms->GetNameForUid(callerUid, callerBundleName)) != ERR_OK) {
92 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get caller bundle name.");
93 return false;
94 }
95 auto userId = callerUid / BASE_USER_RANGE;
96 AppExecFwk::BundleInfo info;
97 if (!IN_PROCESS_CALL(
98 bms->GetBundleInfo(callerBundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, info, userId))) {
99 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get bundle info.");
100 return false;
101 }
102 if (info.applicationInfo.needAppDetail) {
103 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen, app detail.");
104 return false;
105 }
106 }
107 TAG_LOGE(AAFwkTag::ABILITYMGR, "dominate screen.");
108 return true;
109 }
110 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen.");
111 return false;
112 }
113
CheckMultiInstanceAndAppClone(Want & want,int32_t userId,int32_t appIndex,sptr<IRemoteObject> callerToken)114 int32_t AbilityPermissionUtil::CheckMultiInstanceAndAppClone(Want &want, int32_t userId, int32_t appIndex,
115 sptr<IRemoteObject> callerToken)
116 {
117 auto instanceKey = want.GetStringParam(Want::APP_INSTANCE_KEY);
118 auto isSupportMultiInstance = AppUtils::GetInstance().IsSupportMultiInstance();
119 auto isCreating = want.GetBoolParam(Want::CREATE_APP_INSTANCE_KEY, false);
120 if (!isSupportMultiInstance) {
121 if (!instanceKey.empty() || isCreating) {
122 TAG_LOGE(AAFwkTag::ABILITYMGR, "Not support multi-instance");
123 return ERR_MULTI_INSTANCE_NOT_SUPPORTED;
124 }
125 }
126 return ERR_OK;
127 }
128
CheckStartByCallPermissionOrHasFloatingWindow(const PermissionVerification::VerificationInfo & verificationInfo,const sptr<IRemoteObject> & callerToken)129 int32_t AbilityPermissionUtil::CheckStartByCallPermissionOrHasFloatingWindow(
130 const PermissionVerification::VerificationInfo &verificationInfo, const sptr<IRemoteObject> &callerToken)
131 {
132 int32_t permissionRet =
133 PermissionVerification::GetInstance()->CheckStartByCallPermission(verificationInfo);
134 if (permissionRet == ERR_OK) {
135 return ERR_OK;
136 }
137 #ifdef SUPPORT_SCREEN
138 if (CheckStartCallHasFloatingWindow(callerToken) == ERR_OK) {
139 return ERR_OK;
140 }
141 #endif // SUPPORT_SCREEN
142 return permissionRet;
143 }
144
CheckCallServiceExtensionPermissionOrHasFloatingWindow(const PermissionVerification::VerificationInfo & verificationInfo,const sptr<IRemoteObject> & callerToken)145 int32_t AbilityPermissionUtil::CheckCallServiceExtensionPermissionOrHasFloatingWindow(
146 const PermissionVerification::VerificationInfo &verificationInfo, const sptr<IRemoteObject> &callerToken)
147 {
148 int32_t permissionRet =
149 PermissionVerification::GetInstance()->CheckCallServiceExtensionPermission(verificationInfo);
150 if (permissionRet == ERR_OK) {
151 return ERR_OK;
152 }
153 #ifdef SUPPORT_SCREEN
154 if (CheckStartCallHasFloatingWindow(callerToken) == ERR_OK) {
155 return ERR_OK;
156 }
157 #endif // SUPPORT_SCREEN
158 return permissionRet;
159 }
160
CheckCallAbilityPermissionOrHasFloatingWindow(const PermissionVerification::VerificationInfo & verificationInfo,const sptr<IRemoteObject> & callerToken,bool isCallByShortcut)161 int32_t AbilityPermissionUtil::CheckCallAbilityPermissionOrHasFloatingWindow(
162 const PermissionVerification::VerificationInfo &verificationInfo, const sptr<IRemoteObject> &callerToken,
163 bool isCallByShortcut)
164 {
165 int32_t permissionRet =
166 PermissionVerification::GetInstance()->CheckCallAbilityPermission(verificationInfo, isCallByShortcut);
167 if (permissionRet == ERR_OK) {
168 return ERR_OK;
169 }
170 #ifdef SUPPORT_SCREEN
171 if (CheckStartCallHasFloatingWindow(callerToken) == ERR_OK) {
172 return ERR_OK;
173 }
174 #endif // SUPPORT_SCREEN
175 return permissionRet;
176 }
177
178 #ifdef SUPPORT_SCREEN
CheckStartCallHasFloatingWindow(const sptr<IRemoteObject> & callerToken)179 int32_t AbilityPermissionUtil::CheckStartCallHasFloatingWindow(const sptr<IRemoteObject> &callerToken)
180 {
181 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
182 auto sceneSessionManager = Rosen::SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
183 CHECK_POINTER_AND_RETURN_LOG(sceneSessionManager, CHECK_PERMISSION_FAILED, "sceneSessionManager is nullptr");
184 bool hasFloatingWindow = false;
185 auto err = sceneSessionManager->HasFloatingWindowForeground(callerToken, hasFloatingWindow);
186 TAG_LOGI(AAFwkTag::ABILITYMGR,
187 "startAbility call from background, checking floatingwindow. Ret: %{public}d", static_cast<int32_t>(err));
188 if (err != Rosen::WMError::WM_OK) {
189 TAG_LOGE(AAFwkTag::ABILITYMGR,
190 "checking floatingwindow err: %{public}d", static_cast<int32_t>(err));
191 } else if (hasFloatingWindow) {
192 return ERR_OK;
193 }
194 }
195 return CHECK_PERMISSION_FAILED;
196 }
197 #endif // SUPPORT_SCREEN
198 } // AAFwk
199 } // OHOS