• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_manager.h"
17 
18 #include "bundle_constants.h"
19 #include "bundle_info.h"
20 #include "bundle_mgr_client.h"
21 #include "bundle_mgr_interface.h"
22 #include "bundle_mgr_proxy.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #include "ipc_skeleton.h"
27 #include "access_token.h"
28 #include "hap_token_info.h"
29 #include "native_token_info.h"
30 #include "accesstoken_kit.h"
31 #include "dm_log.h"
32 
33 using namespace OHOS::AppExecFwk;
34 using namespace OHOS::AppExecFwk::Constants;
35 using namespace OHOS::Security::AccessToken;
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 IMPLEMENT_SINGLE_INSTANCE(PermissionManager);
40 
GetAppId()41 const std::string PermissionManager::GetAppId()
42 {
43     AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
44     LOGI("PermissionManager::tokenCaller ID == %d", tokenId);
45     if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
46         return "";
47     }
48     HapTokenInfo tokenInfo;
49     auto result = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
50     if (result != RET_SUCCESS) {
51         LOGE("GetHapTokenInfo, tokenId = %d, result = %d, ", tokenId, result);
52         return "";
53     }
54 
55     return tokenInfo.appID;
56 }
57 
CheckPermission(void)58 bool PermissionManager::CheckPermission(void)
59 {
60     LOGI("Enter PermissionManager::CheckPermission");
61     AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
62     if (tokenCaller == 0) {
63         LOGI("PermissionManager::tokenCaller == 0");
64         return false;
65     }
66     LOGI("PermissionManager::tokenCaller ID == %d", tokenCaller);
67 
68     ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
69     if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
70         int32_t uid = IPCSkeleton::GetCallingUid();
71         if (uid < 0) {
72             LOGI("app caller uid is: %d,", uid);
73             return false;
74         }
75 
76         sptr<ISystemAbilityManager> systemAbilityManager =
77             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78         if (systemAbilityManager == nullptr) {
79             LOGE("failed to get system ability mgr.");
80             return false;
81         }
82         sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
83         if (remoteObject == nullptr) {
84             LOGE("failed to get bundle manager proxy.");
85             return false;
86         }
87 
88         LOGI("get bundle manager proxy success.");
89         sptr<IBundleMgr> iBundleMgr = iface_cast<IBundleMgr>(remoteObject);
90         if (iBundleMgr == nullptr) {
91             LOGI("iBundleMgr is nullptr, caller may be a process");
92             return false;
93         }
94         LOGI("PermissionManager::tokenTypeFlag is hap process");
95         return iBundleMgr->CheckIsSystemAppByUid(uid);
96     } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE || tokenTypeFlag == ATokenTypeEnum::TOKEN_SHELL) {
97         LOGI("PermissionManager::tokenTypeFlag is native process");
98         return true;
99     } else {
100         LOGI("PermissionManager::invalid tokenTypeFlag");
101         return false;
102     }
103 }
104 } // namespace DistributedHardware
105 } // namespace OHOS
106