1 /*
2 * Copyright (C) 2021 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 "telephony_permission.h"
17
18 #include "accesstoken_kit.h"
19 #include "bundle_mgr_interface.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "privacy_kit.h"
24 #include "system_ability_definition.h"
25 #include "telephony_log_wrapper.h"
26 #include "tokenid_kit.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 using namespace Security::AccessToken;
31
32 /**
33 * @brief Get bundleName by callingUid.
34 * @param callingUid.
35 * @param bundleName.
36 * @return Returns true on success, false on failure.
37 */
GetBundleNameByUid(int32_t uid,std::string & bundleName)38 bool TelephonyPermission::GetBundleNameByUid(int32_t uid, std::string &bundleName)
39 {
40 OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
41 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 OHOS::sptr<OHOS::IRemoteObject> remoteObject =
43 systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
44
45 sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
46 if (iBundleMgr == nullptr) {
47 return false;
48 }
49 std::string identity = IPCSkeleton::ResetCallingIdentity();
50 ErrCode errCode = iBundleMgr->GetNameForUid(uid, bundleName);
51 IPCSkeleton::SetCallingIdentity(identity);
52 if (errCode != ERR_OK) {
53 TELEPHONY_LOGE("get name for uid failed, error code : %{public}d", errCode);
54 return false;
55 }
56 return true;
57 }
58
59 /**
60 * @brief Permission check by callingUid.
61 * @param permissionName permission name.
62 * @return Returns true on success, false on failure.
63 */
CheckPermission(const std::string & permissionName)64 bool TelephonyPermission::CheckPermission(const std::string &permissionName)
65 {
66 if (permissionName.empty()) {
67 TELEPHONY_LOGD("permission check failed, permission name is empty.");
68 return false;
69 }
70
71 auto callerToken = IPCSkeleton::GetCallingTokenID();
72 auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
73 int result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
74
75 if (permissionName == Permission::ANSWER_CALL || permissionName == Permission::READ_CALL_LOG
76 || permissionName == Permission::READ_CONTACTS || permissionName == Permission::WRITE_CONTACTS
77 || permissionName == Permission::SEND_MESSAGES) {
78 if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
79 bool status = result == PermissionState::PERMISSION_GRANTED;
80 int32_t successCount = status ? 1 : 0;
81 int32_t failCount = status ? 0 : 1;
82 int32_t ret = PrivacyKit::AddPermissionUsedRecord(callerToken, permissionName, successCount, failCount);
83 if (ret != 0) {
84 TELEPHONY_LOGD("AddPermissionUsedRecord failed, permission:%{public}s, "
85 "successCount:%{public}d, failCount:%{public}d",
86 permissionName.c_str(), successCount, failCount);
87 }
88 }
89 }
90
91 if (result != PermissionState::PERMISSION_GRANTED) {
92 TELEPHONY_LOGD("permission check failed, permission:%{public}s, callerToken:%{public}u, tokenType:%{public}d",
93 permissionName.c_str(), callerToken, tokenType);
94 return false;
95 }
96 return true;
97 }
98
CheckCallerIsSystemApp()99 bool TelephonyPermission::CheckCallerIsSystemApp()
100 {
101 auto callerToken = IPCSkeleton::GetCallingTokenID();
102 auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
103 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
104 tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
105 TELEPHONY_LOGI("tokenType check passed.");
106 return true;
107 }
108
109 auto selfToken = IPCSkeleton::GetCallingFullTokenID();
110 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
111 }
112 } // namespace Telephony
113 } // namespace OHOS
114