1 /*
2 * Copyright (c) 2022 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 "permission.h"
17
18 #include <accesstoken_kit.h>
19 #include <bundle_constants.h>
20 #include <ipc_skeleton.h>
21 #include <bundle_mgr_proxy.h>
22 #include <bundle_mgr_interface.h>
23 #include <system_ability_definition.h>
24 #include <iservice_registry.h>
25 #include <tokenid_kit.h>
26
27 #include "window_manager_hilog.h"
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "Permission"};
33 }
34
IsSystemServiceCalling(bool needPrintLog)35 bool Permission::IsSystemServiceCalling(bool needPrintLog)
36 {
37 const auto tokenId = IPCSkeleton::GetCallingTokenID();
38 const auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
39 if (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
40 flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
41 WLOGFD("system service calling, tokenId: %{public}u, flag: %{public}u", tokenId, flag);
42 return true;
43 }
44 if (needPrintLog) {
45 WLOGFE("not system service calling, tokenId: %{public}u, flag: %{public}u", tokenId, flag);
46 }
47 return false;
48 }
49
IsSystemCalling()50 bool Permission::IsSystemCalling()
51 {
52 if (IsSystemServiceCalling(false)) {
53 return true;
54 }
55 uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
56 bool isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx);
57 return isSystemApp;
58 }
59
CheckCallingPermission(const std::string & permission)60 bool Permission::CheckCallingPermission(const std::string& permission)
61 {
62 WLOGFD("permission:%{public}s", permission.c_str());
63
64 if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission) !=
65 AppExecFwk::Constants::PERMISSION_GRANTED) {
66 WLOGW("permission denied!");
67 return false;
68 }
69 WLOGFD("permission ok!");
70 return true;
71 }
72
IsStartByHdcd()73 bool Permission::IsStartByHdcd()
74 {
75 OHOS::Security::AccessToken::NativeTokenInfo info;
76 if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(IPCSkeleton::GetCallingTokenID(), info) != 0) {
77 return false;
78 }
79 if (info.processName.compare("hdcd") == 0) {
80 return true;
81 }
82 return false;
83 }
84
IsStartByInputMethod()85 bool Permission::IsStartByInputMethod()
86 {
87 sptr<ISystemAbilityManager> systemAbilityManager =
88 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
89 if (!systemAbilityManager) {
90 WLOGFE("Failed to get system ability mgr.");
91 return false;
92 }
93 sptr<IRemoteObject> remoteObject
94 = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
95 if (!remoteObject) {
96 WLOGFE("Failed to get display manager service.");
97 return false;
98 }
99 auto bundleManagerServiceProxy_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
100 if ((!bundleManagerServiceProxy_) || (!bundleManagerServiceProxy_->AsObject())) {
101 WLOGFE("Failed to get system display manager services");
102 return false;
103 }
104
105 int uid = IPCSkeleton::GetCallingUid();
106 // reset ipc identity
107 std::string identity = IPCSkeleton::ResetCallingIdentity();
108 std::string bundleName;
109 bundleManagerServiceProxy_->GetNameForUid(uid, bundleName);
110 AppExecFwk::BundleInfo bundleInfo;
111 // 200000 use uid to caculate userId
112 int userId = uid / 200000;
113 bool result = bundleManagerServiceProxy_->GetBundleInfo(bundleName,
114 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
115 // set ipc identity to raw
116 IPCSkeleton::SetCallingIdentity(identity);
117 if (!result) {
118 WLOGFE("failed to query extension ability info");
119 return false;
120 }
121
122 auto extensionInfo = std::find_if(bundleInfo.extensionInfos.begin(), bundleInfo.extensionInfos.end(),
123 [](AppExecFwk::ExtensionAbilityInfo extensionInfo) {
124 return (extensionInfo.type == AppExecFwk::ExtensionAbilityType::INPUTMETHOD);
125 });
126 if (extensionInfo != bundleInfo.extensionInfos.end()) {
127 return true;
128 } else {
129 return false;
130 }
131 }
132
CheckIsCallingBundleName(const std::string name)133 bool Permission::CheckIsCallingBundleName(const std::string name)
134 {
135 sptr<ISystemAbilityManager> systemAbilityManager =
136 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
137 if (!systemAbilityManager) {
138 WLOGFE("Failed to get system ability mgr.");
139 return false;
140 }
141 sptr<IRemoteObject> remoteObject
142 = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
143 if (!remoteObject) {
144 WLOGFE("Failed to get display manager service.");
145 return false;
146 }
147 auto bundleManagerServiceProxy_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
148 if ((!bundleManagerServiceProxy_) || (!bundleManagerServiceProxy_->AsObject())) {
149 WLOGFE("Failed to get system display manager services");
150 return false;
151 }
152 int uid = IPCSkeleton::GetCallingUid();
153 // reset ipc identity
154 std::string identity = IPCSkeleton::ResetCallingIdentity();
155 WLOGFI("resetCallingIdentity:%{public}s", identity.c_str());
156 std::string callingBundleName;
157 bundleManagerServiceProxy_->GetNameForUid(uid, callingBundleName);
158 WLOGFI("get the bundle name:%{public}s", callingBundleName.c_str());
159 std::string::size_type idx = callingBundleName.find(name);
160 if (idx != std::string::npos) {
161 return true;
162 }
163 return false;
164 }
165 } // namespace Rosen
166 } // namespace OHOS
167