• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ipc_skeleton.h"
20 #include "tokenid_kit.h"
21 #include "devattest_log.h"
22 
23 using namespace OHOS;
24 using namespace OHOS::Security::AccessToken;
25 
26 namespace OHOS {
27 namespace DevAttest {
Permission()28 Permission::Permission()
29 {
30 }
31 
~Permission()32 Permission::~Permission()
33 {
34 }
35 
IsSystem()36 bool Permission::IsSystem()
37 {
38     AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
39     pid_t pid = IPCSkeleton::GetCallingPid();
40     pid_t uid = IPCSkeleton::GetCallingUid();
41     ATokenTypeEnum type = AccessTokenKit::GetTokenTypeFlag(tokenId);
42     HILOGD("[IsSystem] check permission, type=%{public}d, pid=%{public}d,uid=%{public}d",
43         static_cast<int32_t>(type), pid, uid);
44     bool result = false;
45     switch (type) {
46         case ATokenTypeEnum::TOKEN_HAP:
47             result = TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID());
48             break;
49         case ATokenTypeEnum::TOKEN_NATIVE:
50             HILOGD("[IsSystem] type switch in ATokenTypeEnum.TOKEN_NATIVE");
51             result = true;
52             break;
53         case ATokenTypeEnum::TOKEN_SHELL:
54             HILOGD("[IsSystem] type switch in ATokenTypeEnum.TOKEN_SHELL");
55             result = true;
56             break;
57         case ATokenTypeEnum::TOKEN_INVALID:
58             HILOGD("[IsSystem] type switch in ATokenTypeEnum.TOKEN_INVALID");
59             break;
60         case ATokenTypeEnum::TOKEN_TYPE_BUTT:
61             HILOGD("[IsSystem] type switch in ATokenTypeEnum.TOKEN_TYPE_BUTT");
62             break;
63     }
64     if (!result) {
65         HILOGE("[IsSystem] system denied, type=%{public}d, pid=%{public}d, uid=%{public}d",
66             static_cast<int32_t>(type), pid, uid);
67         return false;
68     }
69     return true;
70 }
71 
IsPermissionGranted(const std::string & perm)72 bool Permission::IsPermissionGranted(const std::string& perm)
73 {
74     AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
75     pid_t pid = IPCSkeleton::GetCallingPid();
76     pid_t uid = IPCSkeleton::GetCallingUid();
77     ATokenTypeEnum type = AccessTokenKit::GetTokenTypeFlag(tokenId);
78     HILOGD("[IsPermissionGranted] check permission, perm=%{public}s type=%{public}d, pid=%{public}d,uid=%{public}d",
79         perm.c_str(), static_cast<int32_t>(type), pid, uid);
80     int32_t result = PermissionState::PERMISSION_DENIED;
81     switch (type) {
82         case ATokenTypeEnum::TOKEN_HAP:
83             HILOGD("[IsPermissionGranted] type switch in ATokenTypeEnum.TOKEN_HAP");
84             result = AccessTokenKit::VerifyAccessToken(tokenId, perm);
85             break;
86         case ATokenTypeEnum::TOKEN_NATIVE:
87             HILOGD("[IsPermissionGranted] type switch in ATokenTypeEnum.TOKEN_NATIVE");
88             result = PermissionState::PERMISSION_GRANTED;
89             break;
90         case ATokenTypeEnum::TOKEN_SHELL:
91             HILOGD("[IsPermissionGranted] type switch in ATokenTypeEnum.TOKEN_SHELL");
92             result = PermissionState::PERMISSION_GRANTED;
93             break;
94         case ATokenTypeEnum::TOKEN_INVALID:
95             HILOGD("[IsPermissionGranted] type switch in ATokenTypeEnum.TOKEN_INVALID");
96             break;
97         case ATokenTypeEnum::TOKEN_TYPE_BUTT:
98             HILOGD("[IsPermissionGranted] type switch in ATokenTypeEnum.TOKEN_TYPE_BUTT");
99             break;
100     }
101     if (result == PermissionState::PERMISSION_DENIED) {
102         HILOGE("[IsPermissionGranted] permis denied, perm=%{public}s type=%{public}d, pid=%{public}d, uid=%{public}d",
103             perm.c_str(), static_cast<int32_t>(type), pid, uid);
104         return false;
105     }
106     return true;
107 }
108 } // namespace DevAttest
109 } // namespace OHOS
110