• 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 "user/user_manager.h"
17 #include <cstdlib>
18 #include "crypto/key_manager.h"
19 #include "ipc/istorage_daemon.h"
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "utils/string_utils.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace StorageDaemon {
28 std::shared_ptr<UserManager> UserManager::instance_ = nullptr;
UserManager()29 UserManager::UserManager()
30     : rootDirVec_{{"/data/app/%s/%d", 0711, OID_ROOT, OID_ROOT},
31                   {"/data/service/%s/%d", 0711, OID_ROOT, OID_ROOT},
32                   {"/data/chipset/%s/%d", 0711, OID_ROOT, OID_ROOT}},
33       subDirVec_{{"/data/app/%s/%d/base", 0711, OID_ROOT, OID_ROOT},
34                  {"/data/app/%s/%d/database", 0711, OID_ROOT, OID_ROOT}}
35 {
36 }
37 
GetInstance()38 std::shared_ptr<UserManager> UserManager::GetInstance()
39 {
40     static std::once_flag onceFlag;
41     std::call_once(onceFlag, [&]() { instance_ = std::make_shared<UserManager>(); });
42 
43     return instance_;
44 }
45 
StartUser(int32_t userId)46 int32_t UserManager::StartUser(int32_t userId)
47 {
48     LOGI("start user %{public}d", userId);
49     std::lock_guard<std::mutex> lock(mutex_);
50     return MountManager::GetInstance()->MountByUser(userId);
51 }
52 
StopUser(int32_t userId)53 int32_t UserManager::StopUser(int32_t userId)
54 {
55     LOGI("stop user %{public}d", userId);
56     std::lock_guard<std::mutex> lock(mutex_);
57     return MountManager::GetInstance()->UmountByUser(userId);
58 }
59 
PrepareUserDirs(int32_t userId,uint32_t flags)60 int32_t UserManager::PrepareUserDirs(int32_t userId, uint32_t flags)
61 {
62     LOGI("prepare user dirs for %{public}d, flags %{public}u", userId, flags);
63     std::lock_guard<std::mutex> lock(mutex_);
64     int32_t err = E_OK;
65 
66     if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
67         err = PrepareDirsFromIdAndLevel(userId, el1_);
68         if (err != E_OK) {
69             return err;
70         }
71 
72         err = PrepareEl1BundleDir(userId);
73         if (err != E_OK) {
74             return err;
75         }
76     }
77 
78     if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
79         err = PrepareDirsFromIdAndLevel(userId, el2_);
80         if (err != E_OK) {
81             return err;
82         }
83 
84         err = MountManager::GetInstance()->PrepareHmdfsDirs(userId);
85         if (err != E_OK) {
86             LOGE("Prepare hmdfs dir error");
87             return err;
88         }
89     }
90 
91     return E_OK;
92 }
93 
DestroyUserDirs(int32_t userId,uint32_t flags)94 int32_t UserManager::DestroyUserDirs(int32_t userId, uint32_t flags)
95 {
96     LOGI("destroy user dirs for %{public}d, flags %{public}u", userId, flags);
97     std::lock_guard<std::mutex> lock(mutex_);
98     int32_t ret = E_OK;
99     int32_t err;
100 
101     if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
102         err = DestroyDirsFromIdAndLevel(userId, el1_);
103         ret = (err != E_OK) ? err : ret;
104 
105         err = DestroyEl1BundleDir(userId);
106         ret = (err != E_OK) ? err : ret;
107     }
108 
109     if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
110         err = DestroyDirsFromIdAndLevel(userId, el2_);
111         ret = (err != E_OK) ? err : ret;
112 
113         err = MountManager::GetInstance()->DestroyHmdfsDirs(userId);
114         ret = (err != E_OK) ? err : ret;
115     }
116 
117     return ret;
118 }
119 
PrepareDirsFromVec(int32_t userId,const std::string & level,const std::vector<DirInfo> & vec)120 inline bool PrepareDirsFromVec(int32_t userId, const std::string &level, const std::vector<DirInfo> &vec)
121 {
122     for (const DirInfo &dir : vec) {
123         if (!PrepareDir(StringPrintf(dir.path.c_str(), level.c_str(), userId), dir.mode, dir.uid, dir.gid)) {
124             return false;
125         }
126     }
127 
128     return true;
129 }
130 
DestroyDirsFromVec(int32_t userId,const std::string & level,const std::vector<DirInfo> & vec)131 inline bool DestroyDirsFromVec(int32_t userId, const std::string &level, const std::vector<DirInfo> &vec)
132 {
133     bool err = true;
134 
135     for (const DirInfo &dir : vec) {
136         if (IsEndWith(dir.path.c_str(), "%d")) {
137             err &= RmDirRecurse(StringPrintf(dir.path.c_str(), level.c_str(), userId));
138         }
139     }
140 
141     return err;
142 }
143 
PrepareDirsFromIdAndLevel(int32_t userId,const std::string & level)144 int32_t UserManager::PrepareDirsFromIdAndLevel(int32_t userId, const std::string &level)
145 {
146     if (!PrepareDirsFromVec(userId, level, rootDirVec_)) {
147         LOGE("failed to prepare %{public}s root dirs for userid %{public}d", level.c_str(), userId);
148         return E_PREPARE_DIR;
149     }
150 
151     // set policy here
152     std::vector<FileList> list;
153     for (auto item : rootDirVec_) {
154         FileList temp;
155         temp.userId = userId;
156         temp.path = StringPrintf(item.path.c_str(), level.c_str(), userId);
157         list.push_back(temp);
158     }
159     int ret = SetElDirFscryptPolicy(userId, level, list);
160     if (ret != E_OK) {
161         LOGE("Set el poilcy failed");
162         return ret;
163     }
164 
165     if (!PrepareDirsFromVec(userId, level, subDirVec_)) {
166         LOGE("failed to prepare %{public}s sub dirs for userid %{public}d", level.c_str(), userId);
167         return E_PREPARE_DIR;
168     }
169 
170     return E_OK;
171 }
172 
DestroyDirsFromIdAndLevel(int32_t userId,const std::string & level)173 int32_t UserManager::DestroyDirsFromIdAndLevel(int32_t userId, const std::string &level)
174 {
175     if (!DestroyDirsFromVec(userId, level, rootDirVec_)) {
176         LOGE("failed to destroy %{public}s dirs for userid %{public}d", level.c_str(), userId);
177         return E_DESTROY_DIR;
178     }
179 
180     return E_OK;
181 }
182 
PrepareEl1BundleDir(int32_t userId)183 int32_t UserManager::PrepareEl1BundleDir(int32_t userId)
184 {
185     if (!PrepareDir(StringPrintf(bundle_.c_str(), userId), 0711, OID_ROOT, OID_ROOT)) {
186         return E_PREPARE_DIR;
187     }
188 
189     // set policy here
190     std::vector<FileList> list;
191     FileList temp;
192     temp.userId = userId;
193     temp.path = StringPrintf(bundle_.c_str(), userId);
194     list.push_back(temp);
195     int ret = SetElDirFscryptPolicy(userId, el1_, list);
196     if (ret != E_OK) {
197         LOGE("Set el1 poilcy failed");
198         return ret;
199     }
200 
201     return E_OK;
202 }
203 
DestroyEl1BundleDir(int32_t userId)204 int32_t UserManager::DestroyEl1BundleDir(int32_t userId)
205 {
206     if (!RmDirRecurse(StringPrintf(bundle_.c_str(), userId))) {
207         return E_DESTROY_DIR;
208     }
209 
210     return E_OK;
211 }
212 
SetElDirFscryptPolicy(int32_t userId,const std::string & level,const std::vector<FileList> & list)213 int32_t UserManager::SetElDirFscryptPolicy(int32_t userId, const std::string &level,
214                                            const std::vector<FileList> &list)
215 {
216     if (level == el1_) {
217         if (KeyManager::GetInstance()->SetDirectoryElPolicy(userId, EL1_KEY, list)) {
218             LOGE("Set user dir el1 policy error");
219             return E_SET_POLICY;
220         }
221     } else if (level == el2_) {
222         if (KeyManager::GetInstance()->SetDirectoryElPolicy(userId, EL2_KEY, list)) {
223             LOGE("Set user dir el1 policy error");
224             return E_SET_POLICY;
225         }
226     }
227 
228     return E_OK;
229 }
230 } // namespace StorageDaemon
231 } // namespace OHOS
232