1 /*
2 * Copyright (c) 2023-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 #include <fstream>
16 #include <sstream>
17 #include "appspawn_mount_permission.h"
18 #include "appspawn_server.h"
19 #include "config_policy_utils.h"
20
21 namespace OHOS {
22 namespace AppSpawn {
23 namespace {
24 const std::string APP_PERMISSION_PATH("/appdata-sandbox.json");
25 const std::string PERMISSION_FIELD("permission");
26 }
27 std::set<std::string> AppspawnMountPermission::appSandboxPremission_ = {};
28 bool AppspawnMountPermission::isLoad_ = false;
29 std::mutex AppspawnMountPermission::mutex_;
30
GetPermissionFromJson(std::set<std::string> & appSandboxPremissionSet,nlohmann::json & appSandboxPremission)31 void AppspawnMountPermission::GetPermissionFromJson(
32 std::set<std::string> &appSandboxPremissionSet, nlohmann::json &appSandboxPremission)
33 {
34 auto item = appSandboxPremission.find(PERMISSION_FIELD);
35 if (item != appSandboxPremission.end()) {
36 for (auto config : appSandboxPremission[PERMISSION_FIELD]) {
37 for (auto it : config.items()) {
38 APPSPAWN_LOGI("LoadPermissionNames %{public}s", it.key().c_str());
39 appSandboxPremissionSet.insert(it.key());
40 }
41 }
42 } else {
43 APPSPAWN_LOGI("permission does not exist");
44 }
45 }
46
LoadPermissionNames(void)47 void AppspawnMountPermission::LoadPermissionNames(void)
48 {
49 std::lock_guard<std::mutex> lock(mutex_);
50 if (isLoad_) {
51 return;
52 }
53 appSandboxPremission_.clear();
54 nlohmann::json appSandboxPremission;
55 CfgFiles *files = GetCfgFiles("etc/sandbox");
56 for (int i = 0; (files != nullptr) && (i < MAX_CFG_POLICY_DIRS_CNT); ++i) {
57 if (files->paths[i] == nullptr) {
58 continue;
59 }
60 std::string path = files->paths[i];
61 path += APP_PERMISSION_PATH;
62 APPSPAWN_LOGI("LoadAppSandboxConfig %{public}s", path.c_str());
63 std::ifstream jsonFileStream;
64 jsonFileStream.open(path.c_str(), std::ios::in);
65 APPSPAWN_CHECK_ONLY_EXPER(jsonFileStream.is_open(), return);
66 std::stringstream buffer;
67 buffer << jsonFileStream.rdbuf();
68 appSandboxPremission = nlohmann::json::parse(buffer.str(), nullptr, false);
69 APPSPAWN_CHECK(appSandboxPremission.is_structured(), return, "Parse json file into jsonObj failed.");
70 GetPermissionFromJson(appSandboxPremission_, appSandboxPremission);
71 }
72 FreeCfgFiles(files);
73 APPSPAWN_LOGI("LoadPermissionNames size: %{public}lu", static_cast<unsigned long>(appSandboxPremission_.size()));
74 isLoad_ = true;
75 }
76
GetMountPermissionList()77 std::set<std::string> AppspawnMountPermission::GetMountPermissionList()
78 {
79 if (!isLoad_) {
80 LoadPermissionNames();
81 APPSPAWN_LOGI("GetMountPermissionList LoadPermissionNames");
82 }
83 return appSandboxPremission_;
84 }
85
GenPermissionCode(const std::set<std::string> & permissions)86 uint32_t AppspawnMountPermission::GenPermissionCode(const std::set<std::string> &permissions)
87 {
88 uint32_t result = 0;
89 if (permissions.size() == 0) {
90 return result;
91 }
92 uint32_t flagIndex = 1;
93 for (std::string mountPermission : GetMountPermissionList()) {
94 for (std::string inputPermission : permissions) {
95 if (mountPermission.compare(inputPermission) == 0) {
96 result |= flagIndex;
97 }
98 }
99 flagIndex <<= 1;
100 }
101 return result;
102 }
103
IsMountPermission(uint32_t code,const std::string permission)104 bool AppspawnMountPermission::IsMountPermission(uint32_t code, const std::string permission)
105 {
106 for (std::string mountPermission : GetMountPermissionList()) {
107 if (mountPermission.compare(permission) == 0) {
108 return code & 1;
109 }
110 code >>= 1;
111 }
112 return false;
113 } // AppSpawn
114 } // OHOS
115 }
116