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 "hitrace_meter.h"
24 #include "insight_intent_execute_param.h"
25 #include "ipc_skeleton.h"
26 #include "permission_constants.h"
27 #include "permission_verification.h"
28
29 using OHOS::Security::AccessToken::AccessTokenKit;
30
31 namespace OHOS {
32 namespace AAFwk {
33 namespace {
34 constexpr const char* IS_DELEGATOR_CALL = "isDelegatorCall";
35 constexpr const char* SETTINGS = "settings";
36 constexpr int32_t BASE_USER_RANGE = 200000;
37 }
38
GetInstance()39 AbilityPermissionUtil &AbilityPermissionUtil::GetInstance()
40 {
41 static AbilityPermissionUtil instance;
42 return instance;
43 }
44
IsDelegatorCall(const AppExecFwk::RunningProcessInfo & processInfo,const AbilityRequest & abilityRequest) const45 inline bool AbilityPermissionUtil::IsDelegatorCall(const AppExecFwk::RunningProcessInfo &processInfo,
46 const AbilityRequest &abilityRequest) const
47 {
48 /* To make sure the AbilityDelegator is not counterfeited
49 * 1. The caller-process must be test-process
50 * 2. The callerToken must be nullptr
51 */
52 if (processInfo.isTestProcess &&
53 !abilityRequest.callerToken && abilityRequest.want.GetBoolParam(IS_DELEGATOR_CALL, false)) {
54 return true;
55 }
56 return false;
57 }
58
IsDominateScreen(const Want & want,bool isPendingWantCaller)59 bool AbilityPermissionUtil::IsDominateScreen(const Want &want, bool isPendingWantCaller)
60 {
61 if (!isPendingWantCaller &&
62 !PermissionVerification::GetInstance()->IsSACall() && !PermissionVerification::GetInstance()->IsShellCall()) {
63 auto callerPid = IPCSkeleton::GetCallingPid();
64 AppExecFwk::RunningProcessInfo processInfo;
65 DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo);
66 bool isDelegatorCall = processInfo.isTestProcess && want.GetBoolParam(IS_DELEGATOR_CALL, false);
67 if (isDelegatorCall || InsightIntentExecuteParam::IsInsightIntentExecute(want)) {
68 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen.");
69 return false;
70 }
71 // add temporarily
72 std::string bundleName = want.GetElement().GetBundleName();
73 std::string abilityName = want.GetElement().GetAbilityName();
74 bool withoutSettings = bundleName.find(SETTINGS) == std::string::npos &&
75 abilityName.find(SETTINGS) == std::string::npos;
76 if (withoutSettings && AppUtils::GetInstance().IsAllowStartAbilityWithoutCallerToken(bundleName, abilityName)) {
77 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen, allow.");
78 return false;
79 } else if (AppUtils::GetInstance().IsAllowStartAbilityWithoutCallerToken(bundleName, abilityName)) {
80 auto bms = AbilityUtil::GetBundleManagerHelper();
81 CHECK_POINTER_RETURN_BOOL(bms);
82 int32_t callerUid = IPCSkeleton::GetCallingUid();
83 std::string callerBundleName;
84 if (IN_PROCESS_CALL(bms->GetNameForUid(callerUid, callerBundleName)) != ERR_OK) {
85 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get caller bundle name.");
86 return false;
87 }
88 auto userId = callerUid / BASE_USER_RANGE;
89 AppExecFwk::BundleInfo info;
90 if (!IN_PROCESS_CALL(
91 bms->GetBundleInfo(callerBundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, info, userId))) {
92 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to get bundle info.");
93 return false;
94 }
95 if (info.applicationInfo.needAppDetail) {
96 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen, app detail.");
97 return false;
98 }
99 }
100 TAG_LOGE(AAFwkTag::ABILITYMGR, "dominate screen.");
101 return true;
102 }
103 TAG_LOGD(AAFwkTag::ABILITYMGR, "not dominate screen.");
104 return false;
105 }
106 } // AAFwk
107 } // OHOS