• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "softbus_permission.h"
17 
18 #include <sys/types.h>
19 #include <unistd.h>
20 
21 #include "accesstoken_kit.h"
22 #include "comm_log.h"
23 #include "ipc_skeleton.h"
24 #include "permission_entry.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_def.h"
27 #include "softbus_errcode.h"
28 #include "system_ability_definition.h"
29 #include "trans_session_manager.h"
30 
31 namespace {
32     using namespace OHOS::Security;
33 
34     const std::string PERMISSION_JSON_FILE = "/system/etc/communication/softbus/softbus_trans_permission.json";
35     const std::string DANGER_APP_PERMISSION = "ohos.permission.DISTRIBUTED_DATASYNC";
36     const int32_t SYSTEM_UID = 1000;
37     const int32_t MULTE_USER_RADIX = 100000;
38     const std::string SAMGR_PROCESS_NAME = "samgr";
39 }
40 
TransPermissionInit(void)41 int32_t TransPermissionInit(void)
42 {
43     int32_t ret = LoadPermissionJson(PERMISSION_JSON_FILE.c_str());
44     if (ret != SOFTBUS_OK) {
45         COMM_LOGI(COMM_PERM, "load permission json fail");
46         return ret;
47     }
48     return InitDynamicPermission();
49 }
50 
TransPermissionDeinit(void)51 void TransPermissionDeinit(void)
52 {
53     DeinitPermissionJson();
54 }
55 
CalcPermType(pid_t callingUid,pid_t callingPid)56 int32_t CalcPermType(pid_t callingUid, pid_t callingPid)
57 {
58     using namespace AccessToken;
59     if (callingUid == (pid_t)getuid() && callingPid == getpid()) {
60         COMM_LOGI(COMM_PERM, "self app");
61         return SELF_APP;
62     }
63 
64     uint32_t callingToken = OHOS::IPCSkeleton::GetCallingTokenID();
65     auto tokenType = AccessTokenKit::GetTokenTypeFlag(callingToken);
66     if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) {
67         return NATIVE_APP;
68     } else if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
69         HapTokenInfo hapTokenInfo;
70         AccessTokenKit::GetHapTokenInfo(callingToken, hapTokenInfo);
71         if ((hapTokenInfo.apl == ATokenAplEnum::APL_SYSTEM_CORE) ||
72             (hapTokenInfo.apl == ATokenAplEnum::APL_SYSTEM_BASIC)) {
73             return SYSTEM_APP;
74         }
75     }
76     return NORMAL_APP;
77 }
78 
CheckTransPermission(pid_t callingUid,pid_t callingPid,const char * pkgName,const char * sessionName,uint32_t actions)79 int32_t CheckTransPermission(pid_t callingUid, pid_t callingPid,
80     const char *pkgName, const char *sessionName, uint32_t actions)
81 {
82     if (sessionName == nullptr || pkgName == nullptr) {
83         COMM_LOGI(COMM_PERM, "invalid param");
84         return SOFTBUS_PERMISSION_DENIED;
85     }
86     int32_t permType = CalcPermType(callingUid, callingPid);
87     SoftBusPermissionItem *pItem = CreatePermissionItem(permType, callingUid, callingPid, pkgName, actions);
88     if (pItem == nullptr) {
89         COMM_LOGI(COMM_PERM, "pItem is null");
90         return SOFTBUS_MALLOC_ERR;
91     }
92     int32_t ret = CheckPermissionEntry(sessionName, pItem);
93     SoftBusFree(pItem);
94     if (ret >= SYSTEM_APP) {
95         return SOFTBUS_OK;
96     }
97     return SOFTBUS_PERMISSION_DENIED;
98 }
99 
CheckTransSecLevel(const char * mySessionName,const char * peerSessionName)100 int32_t CheckTransSecLevel(const char *mySessionName, const char *peerSessionName)
101 {
102     if (mySessionName == nullptr || peerSessionName == nullptr) {
103         COMM_LOGI(COMM_PERM, "invalid param");
104         return SOFTBUS_INVALID_PARAM;
105     }
106     if (strcmp(mySessionName, peerSessionName) == 0) {
107         return SOFTBUS_OK;
108     }
109     if (!PermIsSecLevelPublic(mySessionName)) {
110         COMM_LOGI(COMM_PERM, "mySessionName isn't seclevel");
111         return SOFTBUS_PERMISSION_DENIED;
112     }
113     if (!PermIsSecLevelPublic(peerSessionName)) {
114         COMM_LOGI(COMM_PERM, "peerSessionName isn't seclevel");
115         return SOFTBUS_PERMISSION_DENIED;
116     }
117     return SOFTBUS_OK;
118 }
119 
CheckDiscPermission(pid_t callingUid,const char * pkgName)120 bool CheckDiscPermission(pid_t callingUid, const char *pkgName)
121 {
122     std::string pkg = "";
123     if (pkgName != nullptr) {
124         pkg = std::string(pkgName);
125     } else {
126         return false;
127     }
128     if (callingUid == SYSTEM_UID || callingUid % MULTE_USER_RADIX == SYSTEM_UID) {
129         return true;
130     }
131     return false;
132 }
133 
CheckBusCenterPermission(pid_t callingUid,const char * pkgName)134 bool CheckBusCenterPermission(pid_t callingUid, const char *pkgName)
135 {
136     std::string pkg = "";
137     if (pkgName != nullptr) {
138         pkg = std::string(pkgName);
139     } else {
140         return false;
141     }
142     if (callingUid == SYSTEM_UID || callingUid % MULTE_USER_RADIX == SYSTEM_UID) {
143         return true;
144     }
145     return false;
146 }
147 
GrantTransPermission(int32_t callingUid,int32_t callingPid,const char * sessionName)148 int32_t GrantTransPermission(int32_t callingUid, int32_t callingPid, const char *sessionName)
149 {
150     if (sessionName == nullptr) {
151         return SOFTBUS_INVALID_PARAM;
152     }
153     return AddDynamicPermission(callingUid, callingPid, sessionName);
154 }
155 
RemoveTransPermission(const char * sessionName)156 int32_t RemoveTransPermission(const char *sessionName)
157 {
158     if (sessionName == nullptr) {
159         return SOFTBUS_INVALID_PARAM;
160     }
161     return DeleteDynamicPermission(sessionName);
162 }
163 
CheckDynamicPermission(void)164 int32_t CheckDynamicPermission(void)
165 {
166     uint32_t callingToken = OHOS::IPCSkeleton::GetCallingTokenID();
167 
168     auto tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(callingToken);
169     if (tokenType != AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
170         COMM_LOGE(COMM_PERM, "not native call");
171         return SOFTBUS_ERR;
172     }
173     AccessToken::NativeTokenInfo nativeTokenInfo;
174     int32_t result = AccessToken::AccessTokenKit::GetNativeTokenInfo(callingToken, nativeTokenInfo);
175     if (result == SOFTBUS_OK && nativeTokenInfo.processName == SAMGR_PROCESS_NAME) {
176         return SOFTBUS_OK;
177     }
178     COMM_LOGE(COMM_PERM,
179         "check dynamic permission failed, processName=%{private}s", nativeTokenInfo.processName.c_str());
180     return SOFTBUS_ERR;
181 }
182