• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/update_caller_info_util.h"
17 
18 #include "ability_util.h"
19 #include "ability_record.h"
20 #include "accesstoken_kit.h"
21 #include "app_scheduler.h"
22 #include "ams_configuration_parameter.h"
23 #include "dialog_session_manager.h"
24 #include "hilog_tag_wrapper.h"
25 #include "hitrace_meter.h"
26 #include "ipc_skeleton.h"
27 #include "permission_verification.h"
28 #include "scene_board_judgement.h"
29 #include "start_ability_utils.h"
30 #include "startup_util.h"
31 
32 namespace OHOS {
33 namespace AAFwk {
34 namespace {
35 constexpr const char* DMS_CALLER_BUNDLE_NAME = "ohos.dms.param.sourceCallerBundleName";
36 constexpr const char* DMS_CALLER_ABILITY_NAME = "ohos.dms.param.sourceCallerAbilityName";
37 constexpr const char* DMS_CALLER_NATIVE_NAME = "ohos.dms.param.sourceCallerNativeName";
38 constexpr const char* DMS_CALLER_APP_ID = "ohos.dms.param.sourceCallerAppId";
39 constexpr const char* DMS_CALLER_APP_IDENTIFIER = "ohos.dms.param.sourceCallerAppIdentifier";
40 constexpr const char* PARAM_RESV_ANCO_CALLER_UID = "ohos.anco.param.callerUid";
41 constexpr const char* PARAM_RESV_ANCO_CALLER_BUNDLENAME = "ohos.anco.param.callerBundleName";
42 constexpr const char* WANT_PARAMS_APP_RESTART_FLAG = "ohos.aafwk.app.restart";
43 constexpr const char* CALLER_REQUEST_CODE = "ohos.extra.param.key.callerRequestCode";
44 constexpr const char* IS_SHELL_CALL = "isShellCall";
45 }
46 
GetInstance()47 UpdateCallerInfoUtil &UpdateCallerInfoUtil::GetInstance()
48 {
49     static UpdateCallerInfoUtil instance;
50     return instance;
51 }
52 
UpdateCallerInfo(Want & want,const sptr<IRemoteObject> & callerToken)53 void UpdateCallerInfoUtil::UpdateCallerInfo(Want& want, const sptr<IRemoteObject> &callerToken)
54 {
55     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
56     if (!StartAbilityUtils::IsCallFromAncoShellOrBroker(callerToken)) {
57         TAG_LOGD(AAFwkTag::ABILITYMGR, "not call from anco or broker.");
58         want.RemoveParam(PARAM_RESV_ANCO_CALLER_UID);
59         want.RemoveParam(PARAM_RESV_ANCO_CALLER_BUNDLENAME);
60     }
61     int32_t tokenId = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
62     int32_t callerUid = IPCSkeleton::GetCallingUid();
63     int32_t callerPid = IPCSkeleton::GetCallingPid();
64     want.RemoveParam(Want::PARAM_RESV_CALLER_TOKEN);
65     want.SetParam(Want::PARAM_RESV_CALLER_TOKEN, tokenId);
66     want.RemoveParam(Want::PARAM_RESV_CALLER_UID);
67     want.SetParam(Want::PARAM_RESV_CALLER_UID, callerUid);
68     want.RemoveParam(Want::PARAM_RESV_CALLER_PID);
69     want.SetParam(Want::PARAM_RESV_CALLER_PID, callerPid);
70     want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
71     want.RemoveParam(IS_SHELL_CALL);
72     want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
73 
74     auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
75     if (!abilityRecord) {
76         std::string bundleName;
77         auto bundleMgr = AbilityUtil::GetBundleManagerHelper();
78         if (bundleMgr != nullptr) {
79             IN_PROCESS_CALL(bundleMgr->GetNameForUid(callerUid, bundleName));
80         }
81         if (bundleName == "") {
82             std::string nativeName;
83             Security::AccessToken::NativeTokenInfo nativeTokenInfo;
84             int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, nativeTokenInfo);
85             if (result == ERR_OK) {
86                 nativeName = "_" + nativeTokenInfo.processName;
87             }
88             want.RemoveParam(Want::PARAM_RESV_CALLER_NATIVE_NAME);
89             want.SetParam(Want::PARAM_RESV_CALLER_NATIVE_NAME, nativeName);
90         }
91         UpdateCallerBundleName(want, bundleName);
92         UpdateCallerAbilityName(want, "");
93         UpdateCallerAppCloneIndex(want, 0);
94         return;
95     }
96     std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
97     UpdateCallerBundleName(want, callerBundleName);
98     std::string callerAbilityName = abilityRecord->GetAbilityInfo().name;
99     UpdateCallerAbilityName(want, callerAbilityName);
100     UpdateCallerAppCloneIndex(want, abilityRecord->GetAppIndex());
101     UpdateSignatureInfo(callerBundleName, want);
102 }
103 
UpdateSignatureInfo(std::string bundleName,Want & want,bool isRemote)104 void UpdateCallerInfoUtil::UpdateSignatureInfo(std::string bundleName, Want& want, bool isRemote)
105 {
106     auto bundleMgr = AbilityUtil::GetBundleManagerHelper();
107     if (bundleMgr != nullptr) {
108         AppExecFwk::SignatureInfo signatureInfo;
109         TAG_LOGD(AAFwkTag::ABILITYMGR, "bundleName: %{public}s", bundleName.c_str());
110         IN_PROCESS_CALL(bundleMgr->GetSignatureInfoByBundleName(bundleName, signatureInfo));
111         std::string callerAppId = isRemote ? DMS_CALLER_APP_ID : Want::PARAM_RESV_CALLER_APP_ID;
112         std::string callerAppIdentifier = isRemote ? DMS_CALLER_APP_IDENTIFIER : Want::PARAM_RESV_CALLER_APP_IDENTIFIER;
113         want.RemoveParam(callerAppId);
114         want.SetParam(callerAppId, signatureInfo.appId);
115         want.RemoveParam(callerAppIdentifier);
116         want.SetParam(callerAppIdentifier, signatureInfo.appIdentifier);
117     }
118 }
119 
UpdateAsCallerSourceInfo(Want & want,sptr<IRemoteObject> asCallerSourceToken,sptr<IRemoteObject> callerToken)120 void UpdateCallerInfoUtil::UpdateAsCallerSourceInfo(Want& want, sptr<IRemoteObject> asCallerSourceToken,
121     sptr<IRemoteObject> callerToken)
122 {
123     if (!StartAbilityUtils::IsCallFromAncoShellOrBroker(callerToken)) {
124         TAG_LOGD(AAFwkTag::ABILITYMGR, "not call from anco or broker.");
125         want.RemoveParam(PARAM_RESV_ANCO_CALLER_UID);
126         want.RemoveParam(PARAM_RESV_ANCO_CALLER_BUNDLENAME);
127     }
128     want.RemoveParam(Want::PARAM_RESV_CALLER_TOKEN);
129     want.RemoveParam(Want::PARAM_RESV_CALLER_UID);
130     want.RemoveParam(Want::PARAM_RESV_CALLER_PID);
131     want.RemoveParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
132     want.RemoveParam(Want::PARAM_RESV_CALLER_ABILITY_NAME);
133     want.RemoveParam(Want::PARAM_RESV_CALLER_NATIVE_NAME);
134     want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
135     want.RemoveParam(IS_SHELL_CALL);
136     want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
137     want.RemoveParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX);
138 #ifdef SUPPORT_SCREEN
139     if (UpdateAsCallerInfoFromDialog(want)) {
140         TAG_LOGD(AAFwkTag::ABILITYMGR, "Update as caller source info from dialog.");
141         return;
142     }
143 #endif // SUPPORT_SCREEN
144     if (AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
145         TAG_LOGD(AAFwkTag::ABILITYMGR, "Update as caller source info from token.");
146         UpdateAsCallerInfoFromToken(want, asCallerSourceToken);
147     } else if (callerToken != nullptr) {
148         TAG_LOGD(AAFwkTag::ABILITYMGR, "Update as caller source info from callerRecord.");
149         UpdateAsCallerInfoFromCallerRecord(want, callerToken);
150     }
151 }
152 
UpdateAsCallerInfoFromToken(Want & want,sptr<IRemoteObject> asCallerSourceToken)153 void UpdateCallerInfoUtil::UpdateAsCallerInfoFromToken(Want& want, sptr<IRemoteObject> asCallerSourceToken)
154 {
155     auto abilityRecord = Token::GetAbilityRecordByToken(asCallerSourceToken);
156     if (abilityRecord == nullptr) {
157         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed update caller info from token");
158         return;
159     }
160     AppExecFwk::RunningProcessInfo processInfo = {};
161     DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByToken(asCallerSourceToken, processInfo);
162     int32_t tokenId = abilityRecord->GetApplicationInfo().accessTokenId;
163     want.SetParam(Want::PARAM_RESV_CALLER_TOKEN, tokenId);
164     want.SetParam(Want::PARAM_RESV_CALLER_UID, processInfo.uid_);
165     want.SetParam(Want::PARAM_RESV_CALLER_PID, processInfo.pid_);
166 
167     std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
168     want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
169     std::string callerAbilityName = abilityRecord->GetAbilityInfo().name;
170     want.SetParam(Want::PARAM_RESV_CALLER_ABILITY_NAME, callerAbilityName);
171     UpdateCallerAppCloneIndex(want, abilityRecord->GetAppIndex());
172     UpdateSignatureInfo(callerBundleName, want);
173 }
174 
UpdateAsCallerInfoFromCallerRecord(Want & want,sptr<IRemoteObject> callerToken)175 void UpdateCallerInfoUtil::UpdateAsCallerInfoFromCallerRecord(Want& want, sptr<IRemoteObject> callerToken)
176 {
177     auto callerRecord = Token::GetAbilityRecordByToken(callerToken);
178     CHECK_POINTER(callerRecord);
179     auto sourceInfo = callerRecord->GetCallerInfo();
180     CHECK_POINTER(sourceInfo);
181     std::string callerBundleName = sourceInfo->callerBundleName;
182     want.SetParam(Want::PARAM_RESV_CALLER_TOKEN, sourceInfo->callerTokenId);
183     want.SetParam(Want::PARAM_RESV_CALLER_UID, sourceInfo->callerUid);
184     want.SetParam(Want::PARAM_RESV_CALLER_PID, sourceInfo->callerPid);
185     want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
186     want.SetParam(Want::PARAM_RESV_CALLER_ABILITY_NAME, sourceInfo->callerAbilityName);
187     want.SetParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX, sourceInfo->callerAppCloneIndex);
188     if (callerBundleName == "") {
189         want.SetParam(Want::PARAM_RESV_CALLER_NATIVE_NAME, sourceInfo->callerNativeName);
190         return;
191     }
192     UpdateSignatureInfo(callerBundleName, want);
193 }
194 
UpdateAsCallerInfoFromDialog(Want & want)195 bool UpdateCallerInfoUtil::UpdateAsCallerInfoFromDialog(Want& want)
196 {
197     std::string dialogSessionId = want.GetStringParam("dialogSessionId");
198     auto dialogCallerInfo = DialogSessionManager::GetInstance().GetDialogCallerInfo(dialogSessionId);
199     if (dialogCallerInfo == nullptr) {
200         TAG_LOGW(AAFwkTag::ABILITYMGR, "failed get dialog caller info");
201         return false;
202     }
203     Want dialogCallerWant = dialogCallerInfo->targetWant;
204     int32_t tokenId = dialogCallerWant.GetIntParam(Want::PARAM_RESV_CALLER_TOKEN, 0);
205     int32_t uid = dialogCallerWant.GetIntParam(Want::PARAM_RESV_CALLER_UID, 0);
206     int32_t pid = dialogCallerWant.GetIntParam(Want::PARAM_RESV_CALLER_PID, 0);
207     std::string callerBundleName = dialogCallerWant.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
208     std::string callerAbilityName = dialogCallerWant.GetStringParam(Want::PARAM_RESV_CALLER_ABILITY_NAME);
209     int32_t callerAppCloneIndex = dialogCallerWant.GetIntParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX, 0);
210     want.SetParam(Want::PARAM_RESV_CALLER_TOKEN, tokenId);
211     want.SetParam(Want::PARAM_RESV_CALLER_UID, uid);
212     want.SetParam(Want::PARAM_RESV_CALLER_PID, pid);
213     want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
214     want.SetParam(Want::PARAM_RESV_CALLER_ABILITY_NAME, callerAbilityName);
215     want.SetParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX, callerAppCloneIndex);
216     if (callerBundleName == "") {
217         want.SetParam(Want::PARAM_RESV_CALLER_NATIVE_NAME,
218             dialogCallerWant.GetStringParam(Want::PARAM_RESV_CALLER_NATIVE_NAME));
219         return true;
220     }
221     UpdateSignatureInfo(callerBundleName, want);
222     return true;
223 }
224 
UpdateCallerInfoFromToken(Want & want,const sptr<IRemoteObject> & token)225 void UpdateCallerInfoUtil::UpdateCallerInfoFromToken(Want& want, const sptr<IRemoteObject> &token)
226 {
227     auto abilityRecord = Token::GetAbilityRecordByToken(token);
228     if (!abilityRecord) {
229         TAG_LOGW(AAFwkTag::ABILITYMGR, "caller abilityRecord null");
230         return;
231     }
232 
233     int32_t tokenId = abilityRecord->GetApplicationInfo().accessTokenId;
234     int32_t callerUid = abilityRecord->GetUid();
235     int32_t callerPid = abilityRecord->GetPid();
236     want.RemoveParam(Want::PARAM_RESV_CALLER_TOKEN);
237     want.SetParam(Want::PARAM_RESV_CALLER_TOKEN, tokenId);
238     want.RemoveParam(Want::PARAM_RESV_CALLER_UID);
239     want.SetParam(Want::PARAM_RESV_CALLER_UID, callerUid);
240     want.RemoveParam(Want::PARAM_RESV_CALLER_PID);
241     want.SetParam(Want::PARAM_RESV_CALLER_PID, callerPid);
242     want.RemoveParam(WANT_PARAMS_APP_RESTART_FLAG);
243     want.RemoveParam(IS_SHELL_CALL);
244     want.RemoveParam(Want::PARAMS_REAL_CALLER_KEY);
245 
246     std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
247     want.RemoveParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
248     want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, callerBundleName);
249     std::string callerAbilityName = abilityRecord->GetAbilityInfo().name;
250     want.RemoveParam(Want::PARAM_RESV_CALLER_ABILITY_NAME);
251     want.SetParam(Want::PARAM_RESV_CALLER_ABILITY_NAME, callerAbilityName);
252     UpdateCallerAppCloneIndex(want, abilityRecord->GetAppIndex());
253     UpdateSignatureInfo(callerBundleName, want);
254 }
255 
UpdateBackToCallerFlag(const sptr<IRemoteObject> & callerToken,Want & want,int32_t requestCode,bool backFlag)256 void UpdateCallerInfoUtil::UpdateBackToCallerFlag(const sptr<IRemoteObject> &callerToken, Want &want,
257     int32_t requestCode, bool backFlag)
258 {
259     if (want.HasParameter(CALLER_REQUEST_CODE)) {
260         want.RemoveParam(CALLER_REQUEST_CODE);
261     }
262     auto callerAbilityRecord = Token::GetAbilityRecordByToken(callerToken);
263     if (requestCode > 0 && callerAbilityRecord != nullptr) {
264         // default return true on oh
265         if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
266             backFlag = AmsConfigurationParameter::GetInstance().IsSupportBackToCaller();
267         }
268         auto fullRequestCode = AbilityRuntime::StartupUtil::GenerateFullRequestCode(
269             callerAbilityRecord->GetPid(), backFlag, requestCode);
270         want.SetParam(CALLER_REQUEST_CODE, std::to_string(fullRequestCode));
271         TAG_LOGI(AAFwkTag::ABILITYMGR,
272             "pid: %{public}d, backFlag:%{private}d, requestCode: %{private}d, fullRequestCode: %{private}s",
273             callerAbilityRecord->GetPid(), backFlag, requestCode, std::to_string(fullRequestCode).c_str());
274     }
275 }
276 
UpdateDmsCallerInfo(Want & want,const sptr<IRemoteObject> & callerToken)277 void UpdateCallerInfoUtil::UpdateDmsCallerInfo(Want& want, const sptr<IRemoteObject> &callerToken)
278 {
279     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
280     int32_t tokenId = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
281     int32_t callerUid = IPCSkeleton::GetCallingUid();
282 
283     auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
284     if (!abilityRecord) {
285         std::string bundleName;
286         auto bundleMgr = AbilityUtil::GetBundleManagerHelper();
287         if (bundleMgr != nullptr) {
288             IN_PROCESS_CALL(bundleMgr->GetNameForUid(callerUid, bundleName));
289         }
290         if (bundleName == "") {
291             std::string nativeName;
292             Security::AccessToken::NativeTokenInfo nativeTokenInfo;
293             int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, nativeTokenInfo);
294             if (result == ERR_OK) {
295                 nativeName = "_" + nativeTokenInfo.processName;
296             }
297             want.RemoveParam(DMS_CALLER_NATIVE_NAME);
298             want.SetParam(DMS_CALLER_NATIVE_NAME, nativeName);
299         }
300         want.RemoveParam(DMS_CALLER_BUNDLE_NAME);
301         want.SetParam(DMS_CALLER_BUNDLE_NAME, bundleName);
302         want.RemoveParam(DMS_CALLER_ABILITY_NAME);
303         want.SetParam(DMS_CALLER_ABILITY_NAME, std::string(""));
304         return;
305     }
306     std::string callerBundleName = abilityRecord->GetAbilityInfo().bundleName;
307     want.RemoveParam(DMS_CALLER_BUNDLE_NAME);
308     want.SetParam(DMS_CALLER_BUNDLE_NAME, callerBundleName);
309     std::string callerAbilityName = abilityRecord->GetAbilityInfo().name;
310     want.RemoveParam(DMS_CALLER_ABILITY_NAME);
311     want.SetParam(DMS_CALLER_ABILITY_NAME, callerAbilityName);
312     UpdateSignatureInfo(callerBundleName, want, true);
313 }
314 
UpdateCallerBundleName(Want & want,const std::string & bundleName)315 void UpdateCallerInfoUtil::UpdateCallerBundleName(Want& want, const std::string &bundleName)
316 {
317     want.RemoveParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
318     want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, bundleName);
319 }
320 
UpdateCallerAbilityName(Want & want,const std::string & abilityName)321 void UpdateCallerInfoUtil::UpdateCallerAbilityName(Want& want, const std::string &abilityName)
322 {
323     want.RemoveParam(Want::PARAM_RESV_CALLER_ABILITY_NAME);
324     want.SetParam(Want::PARAM_RESV_CALLER_ABILITY_NAME, abilityName);
325 }
326 
UpdateCallerAppCloneIndex(Want & want,int32_t appIndex)327 void UpdateCallerInfoUtil::UpdateCallerAppCloneIndex(Want& want, int32_t appIndex)
328 {
329     want.RemoveParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX);
330     want.SetParam(Want::PARAM_RESV_CALLER_APP_CLONE_INDEX, appIndex);
331 }
332 }  // namespace AAFwk
333 }  // namespace OHOS
334