1 /*
2 * Copyright (c) 2024 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 "mock_sandbox_manager.h"
17
18 #include "hilog_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AccessControl {
22 namespace SandboxManager {
23 namespace {
24 constexpr uint32_t MOCK_READ_MODE = 1;
25 constexpr uint32_t MOCK_WRITE_MODE = 2;
26 constexpr uint32_t MOCK_READ_WRITE_MODE = 3;
27 constexpr uint32_t MOCK_FLAG_PERSIST_URI = 64;
28 }
29
SetPolicy(uint32_t tokenid,const std::vector<PolicyInfo> & policys,uint64_t policyFlag,std::vector<uint32_t> & results)30 int32_t SandboxManagerKit::SetPolicy(uint32_t tokenid, const std::vector<PolicyInfo> &policys, uint64_t policyFlag,
31 std::vector<uint32_t> &results)
32 {
33 TAG_LOGI(AAFwkTag::URIPERMMGR, "SetPolicy called, size of policys is %{public}zu", policys.size());
34 if (SetPolicyRet_ != ERR_OK) {
35 return SetPolicyRet_;
36 }
37 results.clear();
38 std::lock_guard<std::mutex> guard(mutex_);
39 for (auto &policyInfo: policys) {
40 auto key = std::to_string(tokenid) + ":" + policyInfo.path;
41 auto keySearchIter = policyMap_.find(key);
42 uint32_t mode = policyInfo.mode;
43 if (policyFlag > 0) {
44 mode |= MOCK_FLAG_PERSIST_URI;
45 }
46 if (keySearchIter == policyMap_.end()) {
47 policyMap_.emplace(key, mode);
48 } else {
49 keySearchIter->second |= mode;
50 }
51 }
52 results = std::vector<uint32_t>(policys.size(), ERR_OK);
53 return ERR_OK;
54 }
55
CheckPolicy(uint32_t tokenid,const std::vector<PolicyInfo> & policys,std::vector<bool> & results)56 int32_t SandboxManagerKit::CheckPolicy(uint32_t tokenid, const std::vector<PolicyInfo> &policys,
57 std::vector<bool> &results)
58 {
59 TAG_LOGI(AAFwkTag::URIPERMMGR, "CheckPolicy called.");
60 std::lock_guard<std::mutex> guard(mutex_);
61 results.clear();
62 for (auto &policyInfo: policys) {
63 auto key = std::to_string(tokenid) + ":" + policyInfo.path;
64 TAG_LOGI(AAFwkTag::URIPERMMGR, "key is %{public}s.", key.c_str());
65 auto keySearchIter = policyMap_.find(key);
66 if (keySearchIter == policyMap_.end()) {
67 TAG_LOGI(AAFwkTag::URIPERMMGR, "no policy record.");
68 results.emplace_back(false);
69 continue;
70 }
71 auto itemMode = (keySearchIter->second & (~MOCK_FLAG_PERSIST_URI));
72 // rw
73 if (itemMode == MOCK_READ_WRITE_MODE) {
74 results.emplace_back(true);
75 continue;
76 }
77 // w
78 if (itemMode == MOCK_WRITE_MODE) {
79 results.emplace_back(policyInfo.mode == MOCK_WRITE_MODE);
80 continue;
81 }
82 // r
83 if (itemMode == MOCK_READ_MODE) {
84 results.emplace_back(policyInfo.mode == MOCK_READ_MODE);
85 continue;
86 }
87 results.emplace_back(false);
88 }
89 return ERR_OK;
90 }
91
CheckPersistPolicy(uint32_t tokenid,const std::vector<PolicyInfo> & policys,std::vector<bool> & results)92 int32_t SandboxManagerKit::CheckPersistPolicy(uint32_t tokenid, const std::vector<PolicyInfo> &policys,
93 std::vector<bool> &results)
94 {
95 TAG_LOGI(AAFwkTag::URIPERMMGR, "CheckPersistPolicy called.");
96 std::lock_guard<std::mutex> guard(persistMutex_);
97 results.clear();
98 for (auto &policyInfo: policys) {
99 auto key = std::to_string(tokenid) + ":" + policyInfo.path;
100 auto keySearchIter = persistPolicyMap_.find(key);
101 if (keySearchIter == persistPolicyMap_.end()) {
102 results.emplace_back(false);
103 continue;
104 }
105 // rw
106 if (keySearchIter->second == MOCK_READ_WRITE_MODE) {
107 results.emplace_back(true);
108 continue;
109 }
110 // w
111 if (keySearchIter->second == MOCK_WRITE_MODE) {
112 results.emplace_back(policyInfo.mode == MOCK_WRITE_MODE);
113 continue;
114 }
115 // r
116 if (keySearchIter->second == MOCK_READ_MODE) {
117 results.emplace_back(policyInfo.mode == MOCK_READ_MODE);
118 continue;
119 }
120 results.emplace_back(false);
121 }
122 return ERR_OK;
123 }
124
PersistPolicy(uint32_t tokenid,const std::vector<PolicyInfo> & policys,std::vector<uint32_t> & results)125 int32_t SandboxManagerKit::PersistPolicy(uint32_t tokenid, const std::vector<PolicyInfo> &policys,
126 std::vector<uint32_t> &results)
127 {
128 TAG_LOGI(AAFwkTag::URIPERMMGR, "PersistPolicy called, size of policys is %{public}zu", policys.size());
129 results.clear();
130 std::lock_guard<std::mutex> guard(persistMutex_);
131 for (auto &policyInfo: policys) {
132 auto key = std::to_string(tokenid) + ":" + policyInfo.path;
133 auto keySearchIter = persistPolicyMap_.find(key);
134 if (keySearchIter == persistPolicyMap_.end()) {
135 persistPolicyMap_.emplace(key, policyInfo.mode);
136 } else {
137 keySearchIter->second |= policyInfo.mode;
138 }
139 }
140 results = std::vector<uint32_t>(policys.size(), ERR_OK);
141 return ERR_OK;
142 }
143
UnSetPolicy(uint32_t tokenid,const PolicyInfo & policy)144 int32_t SandboxManagerKit::UnSetPolicy(uint32_t tokenid, const PolicyInfo &policy)
145 {
146 TAG_LOGI(AAFwkTag::URIPERMMGR, "UnSetPolicy called.");
147 if (UnSetPolicyRet_ != ERR_OK) {
148 return UnSetPolicyRet_;
149 }
150 std::lock_guard<std::mutex> guard(mutex_);
151 auto key = std::to_string(tokenid) + ":" + policy.path;
152 auto keySearchIter = policyMap_.find(key);
153 if (keySearchIter == policyMap_.end()) {
154 return ERR_OK;
155 }
156 policyMap_.erase(keySearchIter);
157 return ERR_OK;
158 }
159
StartAccessingByTokenId(uint32_t tokenid)160 int32_t SandboxManagerKit::StartAccessingByTokenId(uint32_t tokenid)
161 {
162 return ERR_OK;
163 }
164
Init()165 void SandboxManagerKit::Init()
166 {
167 {
168 std::lock_guard<std::mutex> guard(mutex_);
169 policyMap_.clear();
170 }
171 {
172 std::lock_guard<std::mutex> guard(persistMutex_);
173 persistPolicyMap_.clear();
174 }
175 }
176
177 std::mutex SandboxManagerKit::mutex_;
178 std::map<std::string, int32_t> SandboxManagerKit::policyMap_;
179 std::mutex SandboxManagerKit::persistMutex_;
180 std::map<std::string, int32_t> SandboxManagerKit::persistPolicyMap_;
181 int32_t SandboxManagerKit::SetPolicyRet_ = ERR_OK;
182 int32_t SandboxManagerKit::UnSetPolicyRet_ = ERR_OK;
183 } // OHOS
184 } // AccessControl
185 } // SandboxManager