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 #include "dlp_permission_set_manager.h"
16
17 #include <fcntl.h>
18 #include <memory>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include "access_token.h"
24 #include "accesstoken_log.h"
25 #include "data_validator.h"
26 #include "securec.h"
27
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DlpPermissionSetManager"};
33 }
34
GetInstance()35 DlpPermissionSetManager& DlpPermissionSetManager::GetInstance()
36 {
37 static DlpPermissionSetManager instance;
38 return instance;
39 }
40
DlpPermissionSetManager()41 DlpPermissionSetManager::DlpPermissionSetManager()
42 {}
43
~DlpPermissionSetManager()44 DlpPermissionSetManager::~DlpPermissionSetManager()
45 {}
46
ProcessDlpPermInfos(const std::vector<PermissionDlpMode> & dlpPermInfos)47 void DlpPermissionSetManager::ProcessDlpPermInfos(const std::vector<PermissionDlpMode>& dlpPermInfos)
48 {
49 for (auto iter = dlpPermInfos.begin(); iter != dlpPermInfos.end(); iter++) {
50 auto it = dlpPermissionModeMap_.find(iter->permissionName);
51 if (it != dlpPermissionModeMap_.end()) {
52 ACCESSTOKEN_LOG_WARN(LABEL,
53 "info for permission: %{public}s dlpMode %{public}d has been insert, please check!",
54 iter->permissionName.c_str(), iter->dlpMode);
55 continue;
56 }
57 dlpPermissionModeMap_[iter->permissionName] = iter->dlpMode;
58 }
59 }
60
GetPermDlpMode(const std::string & permissionName)61 int32_t DlpPermissionSetManager::GetPermDlpMode(const std::string& permissionName)
62 {
63 auto it = dlpPermissionModeMap_.find(permissionName);
64 if (it == dlpPermissionModeMap_.end()) {
65 ACCESSTOKEN_LOG_DEBUG(LABEL, "can not find permission: %{public}s in dlp permission cfg",
66 permissionName.c_str());
67 return DLP_PERM_ALL;
68 }
69 return dlpPermissionModeMap_[permissionName];
70 }
71
UpdatePermStateWithDlpInfo(int32_t dlpType,std::vector<PermissionStateFull> & permStateList)72 int32_t DlpPermissionSetManager::UpdatePermStateWithDlpInfo(int32_t dlpType,
73 std::vector<PermissionStateFull>& permStateList)
74 {
75 ACCESSTOKEN_LOG_DEBUG(LABEL, "dlpType: %{public}d", dlpType);
76 for (auto iter = permStateList.begin(); iter != permStateList.end(); iter++) {
77 if (iter->grantStatus[0] == PERMISSION_DENIED) {
78 continue;
79 }
80 int32_t dlpMode = GetPermDlpMode(iter->permissionName);
81 bool res = IsPermStateNeedUpdate(dlpType, dlpMode);
82 if (res) {
83 iter->grantStatus[0] = PERMISSION_DENIED;
84 }
85 }
86 return RET_SUCCESS;
87 }
88
IsPermStateNeedUpdate(int32_t dlpType,int32_t dlpMode)89 bool DlpPermissionSetManager::IsPermStateNeedUpdate(int32_t dlpType, int32_t dlpMode)
90 {
91 ACCESSTOKEN_LOG_DEBUG(LABEL, "dlpType: %{public}d dlpMode %{public}d", dlpType, dlpMode);
92 /* permission is available to all dlp hap */
93 if (dlpMode == DLP_PERM_ALL) {
94 return false;
95 }
96
97 /* permission is available to full control */
98 if (dlpMode == DLP_PERM_FULL_CONTROL && dlpType == DLP_FULL_CONTROL) {
99 return false;
100 }
101 /* permission is available to none */
102 return true;
103 }
104 } // namespace AccessToken
105 } // namespace Security
106 } // namespace OHOS
107