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     if (systemAbilityManager == nullptr) {
43         TELEPHONY_LOGE("systemAbilityManager is nullptr");
44         return false;
45     }
46     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
47         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48 
49     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
50     if (iBundleMgr == nullptr) {
51         TELEPHONY_LOGE("iBundleMgr is nullptr");
52         return false;
53     }
54     std::string identity = IPCSkeleton::ResetCallingIdentity();
55     ErrCode errCode = iBundleMgr->GetNameForUid(uid, bundleName);
56     IPCSkeleton::SetCallingIdentity(identity);
57     if (errCode != ERR_OK) {
58         TELEPHONY_LOGE("get name for uid failed, error code : %{public}d", errCode);
59         return false;
60     }
61     return true;
62 }
63 
64 /**
65  * @brief Permission check by callingUid.
66  * @param permissionName permission name.
67  * @return Returns true on success, false on failure.
68  */
CheckPermission(const std::string & permissionName)69 bool TelephonyPermission::CheckPermission(const std::string &permissionName)
70 {
71     if (permissionName.empty()) {
72         TELEPHONY_LOGD("permission check failed, permission name is empty.");
73         return false;
74     }
75 
76     auto callerToken = IPCSkeleton::GetCallingTokenID();
77     auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
78     int result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
79 
80     if (permissionName == Permission::ANSWER_CALL || permissionName == Permission::READ_CALL_LOG
81         || permissionName == Permission::READ_CONTACTS || permissionName == Permission::WRITE_CONTACTS
82         || permissionName == Permission::SEND_MESSAGES) {
83         if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
84             bool status = result == PermissionState::PERMISSION_GRANTED;
85             int32_t successCount = status ? 1 : 0;
86             int32_t failCount = status ? 0 : 1;
87             int32_t ret = PrivacyKit::AddPermissionUsedRecord(callerToken, permissionName, successCount, failCount);
88             if (ret != 0) {
89                 TELEPHONY_LOGD("AddPermissionUsedRecord failed, permission:%{public}s, "
90                     "successCount:%{public}d, failCount:%{public}d",
91                     permissionName.c_str(), successCount, failCount);
92             }
93         }
94     }
95 
96     if (result != PermissionState::PERMISSION_GRANTED) {
97         TELEPHONY_LOGE("permission check failed, permission:%{public}s, callerToken:%{public}u, "
98             "tokenType:%{public}d, pid:%{public}d",
99             permissionName.c_str(), callerToken, tokenType, IPCSkeleton::GetCallingPid());
100         return false;
101     }
102     return true;
103 }
104 
CheckCallerIsSystemApp()105 bool TelephonyPermission::CheckCallerIsSystemApp()
106 {
107     auto callerToken = IPCSkeleton::GetCallingTokenID();
108     auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
109     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
110         tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
111         TELEPHONY_LOGD("tokenType check passed.");
112         return true;
113     }
114 
115     auto selfToken = IPCSkeleton::GetCallingFullTokenID();
116     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
117 }
118 } // namespace Telephony
119 } // namespace OHOS
120