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 <system_ability_definition.h>
23 #include <iservice_registry.h>
24
25 #include "window_manager_hilog.h"
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WMPermission"};
31 }
32
IsSystemServiceCalling(bool needPrintLog)33 bool Permission::IsSystemServiceCalling(bool needPrintLog)
34 {
35 Security::AccessToken::NativeTokenInfo tokenInfo;
36 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(IPCSkeleton::GetCallingTokenID(), tokenInfo);
37 if (tokenInfo.apl == Security::AccessToken::ATokenAplEnum::APL_SYSTEM_CORE ||
38 tokenInfo.apl == Security::AccessToken::ATokenAplEnum::APL_SYSTEM_BASIC) {
39 return true;
40 }
41 if (needPrintLog) {
42 WLOGFE("Is not system service calling, native apl: %{public}d", tokenInfo.apl);
43 }
44 return false;
45 }
46
IsSystemCalling()47 bool Permission::IsSystemCalling()
48 {
49 if (IsSystemServiceCalling(false)) {
50 return true;
51 }
52 int32_t uid = IPCSkeleton::GetCallingUid();
53 if (uid < 0) {
54 WLOGFE("Is not system calling, app caller uid is: %d,", uid);
55 return false;
56 }
57
58 sptr<ISystemAbilityManager> systemAbilityManager =
59 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
60 if (systemAbilityManager == nullptr) {
61 WLOGFE("Is not system calling, failed to get system ability mgr.");
62 return false;
63 }
64 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
65 if (remoteObject == nullptr) {
66 WLOGFE("Is not system calling, failed to get bundle manager proxy.");
67 return false;
68 }
69 sptr<AppExecFwk::IBundleMgr> iBundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
70 if (iBundleMgr == nullptr) {
71 WLOGFE("Is not system calling, iBundleMgr is nullptr");
72 return false;
73 }
74 bool isSystemAppCalling = iBundleMgr->CheckIsSystemAppByUid(uid);
75 if (!isSystemAppCalling) {
76 WLOGFE("Is not system calling, UID:%{public}d IsSystemApp:%{public}d", uid, isSystemAppCalling);
77 }
78 return isSystemAppCalling;
79 }
80
CheckCallingPermission(const std::string & permission)81 bool Permission::CheckCallingPermission(const std::string& permission)
82 {
83 WLOGFI("permission:%{public}s", permission.c_str());
84
85 if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission) !=
86 AppExecFwk::Constants::PERMISSION_GRANTED) {
87 WLOGFI("permission denied!");
88 return false;
89 }
90 WLOGFI("permission ok!");
91 return true;
92 }
93
IsStartByHdcd()94 bool Permission::IsStartByHdcd()
95 {
96 OHOS::Security::AccessToken::NativeTokenInfo info;
97 if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(IPCSkeleton::GetCallingTokenID(), info) != 0) {
98 return false;
99 }
100 if (info.processName.compare("hdcd") == 0) {
101 return true;
102 }
103 return false;
104 }
105 } // namespace Rosen
106 } // namespace OHOS