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 <sys/mount.h>
17 #include <sys/stat.h>
18 #include <sys/syscall.h>
19 #include <sys/types.h>
20
21 #include "appspawn_msg.h"
22 #include "appspawn_sandbox.h"
23 #include "appspawn_utils.h"
24 #include "json_utils.h"
25 #include "securec.h"
26
27 #define SANDBOX_GROUP_PATH "/data/storage/el2/group/"
28 #define SANDBOX_INSTALL_PATH "/data/storage/el2/group/"
29 #define SANDBOX_OVERLAY_PATH "/data/storage/overlay/"
30
CheckPath(const char * name)31 static inline bool CheckPath(const char *name)
32 {
33 return name != NULL && strcmp(name, ".") != 0 && strcmp(name, "..") != 0 && strstr(name, "/") == NULL;
34 }
35
MountAllHsp(const SandboxContext * context,const cJSON * hsps)36 static int MountAllHsp(const SandboxContext *context, const cJSON *hsps)
37 {
38 APPSPAWN_CHECK(context != NULL && hsps != NULL, return -1, "Invalid context or hsps");
39
40 int ret = 0;
41 cJSON *bundles = cJSON_GetObjectItemCaseSensitive(hsps, "bundles");
42 cJSON *modules = cJSON_GetObjectItemCaseSensitive(hsps, "modules");
43 cJSON *versions = cJSON_GetObjectItemCaseSensitive(hsps, "versions");
44 APPSPAWN_CHECK(bundles != NULL && cJSON_IsArray(bundles), return -1, "MountAllHsp: invalid bundles");
45 APPSPAWN_CHECK(modules != NULL && cJSON_IsArray(modules), return -1, "MountAllHsp: invalid modules");
46 APPSPAWN_CHECK(versions != NULL && cJSON_IsArray(versions), return -1, "MountAllHsp: invalid versions");
47 int count = cJSON_GetArraySize(bundles);
48 APPSPAWN_CHECK(count == cJSON_GetArraySize(modules), return -1, "MountAllHsp: sizes are not same");
49 APPSPAWN_CHECK(count == cJSON_GetArraySize(versions), return -1, "MountAllHsp: sizes are not same");
50
51 APPSPAWN_LOGI("MountAllHsp app: %{public}s, count: %{public}d", context->bundleName, count);
52 for (int i = 0; i < count; i++) {
53 char *libBundleName = cJSON_GetStringValue(cJSON_GetArrayItem(bundles, i));
54 char *libModuleName = cJSON_GetStringValue(cJSON_GetArrayItem(modules, i));
55 char *libVersion = cJSON_GetStringValue(cJSON_GetArrayItem(versions, i));
56 APPSPAWN_CHECK(CheckPath(libBundleName) && CheckPath(libModuleName) && CheckPath(libVersion),
57 return -1, "MountAllHsp: path error");
58
59 // src path
60 int len = sprintf_s(context->buffer[0].buffer, context->buffer[0].bufferLen, "%s%s/%s/%s",
61 PHYSICAL_APP_INSTALL_PATH, libBundleName, libVersion, libModuleName);
62 APPSPAWN_CHECK(len > 0, return -1, "Failed to format install path");
63 // sandbox path
64 len = sprintf_s(context->buffer[1].buffer, context->buffer[1].bufferLen, "%s%s%s/%s",
65 context->rootPath, SANDBOX_INSTALL_PATH, libBundleName, libModuleName);
66 APPSPAWN_CHECK(len > 0, return -1, "Failed to format install path");
67
68 CreateSandboxDir(context->buffer[1].buffer, FILE_MODE);
69 MountArg mountArg = {
70 context->buffer[0].buffer, context->buffer[1].buffer, NULL, MS_REC | MS_BIND, NULL, MS_SLAVE
71 };
72 ret = SandboxMountPath(&mountArg);
73 APPSPAWN_CHECK(ret == 0, return ret, "mount library failed %{public}d", ret);
74 }
75 return ret;
76 }
77
GetLastPath(const char * libPhysicalPath)78 static inline char *GetLastPath(const char *libPhysicalPath)
79 {
80 char *tmp = GetLastStr(libPhysicalPath, "/");
81 return tmp + 1;
82 }
83
MountAllGroup(const SandboxContext * context,const cJSON * groups)84 static int MountAllGroup(const SandboxContext *context, const cJSON *groups)
85 {
86 APPSPAWN_CHECK(context != NULL && groups != NULL, return -1, "Invalid context or group");
87 int ret = 0;
88 cJSON *dataGroupIds = cJSON_GetObjectItemCaseSensitive(groups, "dataGroupId");
89 cJSON *gids = cJSON_GetObjectItemCaseSensitive(groups, "gid");
90 cJSON *dirs = cJSON_GetObjectItemCaseSensitive(groups, "dir");
91 APPSPAWN_CHECK(dataGroupIds != NULL && cJSON_IsArray(dataGroupIds),
92 return -1, "MountAllGroup: invalid dataGroupIds");
93 APPSPAWN_CHECK(gids != NULL && cJSON_IsArray(gids), return -1, "MountAllGroup: invalid gids");
94 APPSPAWN_CHECK(dirs != NULL && cJSON_IsArray(dirs), return -1, "MountAllGroup: invalid dirs");
95 int count = cJSON_GetArraySize(dataGroupIds);
96 APPSPAWN_CHECK(count == cJSON_GetArraySize(gids), return -1, "MountAllGroup: sizes are not same");
97 APPSPAWN_CHECK(count == cJSON_GetArraySize(dirs), return -1, "MountAllGroup: sizes are not same");
98
99 APPSPAWN_LOGI("MountAllGroup: app: %{public}s, count: %{public}d", context->bundleName, count);
100 for (int i = 0; i < count; i++) {
101 cJSON *dirJson = cJSON_GetArrayItem(dirs, i);
102 APPSPAWN_CHECK(dirJson != NULL && cJSON_IsString(dirJson), return -1, "MountAllGroup: invalid dirJson");
103 const char *libPhysicalPath = cJSON_GetStringValue(dirJson);
104 APPSPAWN_CHECK(!CheckPath(libPhysicalPath), return -1, "MountAllGroup: path error");
105
106 char *dataGroupUuid = GetLastPath(libPhysicalPath);
107 int len = sprintf_s(context->buffer[0].buffer, context->buffer[0].bufferLen, "%s%s%s",
108 context->rootPath, SANDBOX_GROUP_PATH, dataGroupUuid);
109 APPSPAWN_CHECK(len > 0, return -1, "Failed to format install path");
110 APPSPAWN_LOGV("MountAllGroup src: '%{public}s' =>'%{public}s'", libPhysicalPath, context->buffer[0].buffer);
111
112 CreateSandboxDir(context->buffer[0].buffer, FILE_MODE);
113 MountArg mountArg = {libPhysicalPath, context->buffer[0].buffer, NULL, MS_REC | MS_BIND, NULL, MS_SLAVE};
114 ret = SandboxMountPath(&mountArg);
115 APPSPAWN_CHECK(ret == 0, return ret, "mount library failed %{public}d", ret);
116 }
117 return ret;
118 }
119
120 typedef struct {
121 const SandboxContext *sandboxContext;
122 uint32_t srcSetLen;
123 char *mountedSrcSet;
124 } OverlayContext;
125
SetOverlayAppPath(const char * hapPath,void * context)126 static int SetOverlayAppPath(const char *hapPath, void *context)
127 {
128 APPSPAWN_LOGV("SetOverlayAppPath '%{public}s'", hapPath);
129 OverlayContext *overlayContext = (OverlayContext *)context;
130 const SandboxContext *sandboxContext = overlayContext->sandboxContext;
131
132 // src path
133 char *tmp = GetLastStr(hapPath, "/");
134 if (tmp == NULL) {
135 return 0;
136 }
137 int ret = strncpy_s(sandboxContext->buffer[0].buffer,
138 sandboxContext->buffer[0].bufferLen, hapPath, tmp - (char *)hapPath);
139 APPSPAWN_CHECK(ret == 0, return ret, "mount library failed %{public}d", ret);
140
141 if (strstr(overlayContext->mountedSrcSet, sandboxContext->buffer[0].buffer) != NULL) {
142 APPSPAWN_LOGV("%{public}s have mounted before, no need to mount twice.", sandboxContext->buffer[0].buffer);
143 return 0;
144 }
145 ret = strcat_s(overlayContext->mountedSrcSet, overlayContext->srcSetLen, "|");
146 APPSPAWN_CHECK(ret == 0, return ret, "Fail to add src path to set %{public}s", "|");
147 ret = strcat_s(overlayContext->mountedSrcSet, overlayContext->srcSetLen, sandboxContext->buffer[0].buffer);
148 APPSPAWN_CHECK(ret == 0, return ret, "Fail to add src path to set %{public}s", sandboxContext->buffer[0].buffer);
149
150 // sandbox path
151 tmp = GetLastStr(sandboxContext->buffer[0].buffer, "/");
152 if (tmp == NULL) {
153 return 0;
154 }
155 int len = sprintf_s(sandboxContext->buffer[1].buffer, sandboxContext->buffer[1].bufferLen, "%s%s",
156 sandboxContext->rootPath, SANDBOX_OVERLAY_PATH);
157 APPSPAWN_CHECK(len > 0, return -1, "Failed to format install path");
158 ret = strcat_s(sandboxContext->buffer[1].buffer, sandboxContext->buffer[1].bufferLen - len, tmp + 1);
159 APPSPAWN_CHECK(ret == 0, return ret, "mount library failed %{public}d", ret);
160 APPSPAWN_LOGV("SetOverlayAppPath path: '%{public}s' => '%{public}s'",
161 sandboxContext->buffer[0].buffer, sandboxContext->buffer[1].buffer);
162
163 MountArg mountArg = {
164 sandboxContext->buffer[0].buffer, sandboxContext->buffer[1].buffer, NULL, MS_REC | MS_BIND, NULL, MS_SHARED
165 };
166 int retMount = SandboxMountPath(&mountArg);
167 if (retMount != 0) {
168 APPSPAWN_LOGE("Fail to mount overlay path, src is %{public}s.", hapPath);
169 ret = retMount;
170 }
171 return ret;
172 }
173
SetOverlayAppSandboxConfig(const SandboxContext * context,const char * overlayInfo)174 static int SetOverlayAppSandboxConfig(const SandboxContext *context, const char *overlayInfo)
175 {
176 APPSPAWN_CHECK(context != NULL && overlayInfo != NULL, return -1, "Invalid context or overlayInfo");
177 OverlayContext overlayContext;
178 overlayContext.sandboxContext = context;
179 overlayContext.srcSetLen = strlen(overlayInfo);
180 overlayContext.mountedSrcSet = (char *)calloc(1, overlayContext.srcSetLen + 1);
181 APPSPAWN_CHECK(overlayContext.mountedSrcSet != NULL, return -1, "Failed to create mountedSrcSet");
182 *(overlayContext.mountedSrcSet + overlayContext.srcSetLen) = '\0';
183 int ret = StringSplit(overlayInfo, "|", (void *)&overlayContext, SetOverlayAppPath);
184 APPSPAWN_LOGV("overlayContext->mountedSrcSet: '%{public}s'", overlayContext.mountedSrcSet);
185 free(overlayContext.mountedSrcSet);
186 return ret;
187 }
188
GetJsonObjFromProperty(const SandboxContext * context,const char * name)189 static inline cJSON *GetJsonObjFromProperty(const SandboxContext *context, const char *name)
190 {
191 uint32_t size = 0;
192 char *extInfo = (char *)(GetAppSpawnMsgExtInfo(context->message, name, &size));
193 if (size == 0 || extInfo == NULL) {
194 return NULL;
195 }
196 APPSPAWN_LOGV("Get json name %{public}s value %{public}s", name, extInfo);
197 cJSON *root = cJSON_Parse(extInfo);
198 APPSPAWN_CHECK(root != NULL, return NULL, "Invalid ext info %{public}s for %{public}s", extInfo, name);
199 return root;
200 }
201
ProcessHSPListConfig(const SandboxContext * context,const AppSpawnSandboxCfg * appSandBox,const char * name)202 static int ProcessHSPListConfig(const SandboxContext *context, const AppSpawnSandboxCfg *appSandBox, const char *name)
203 {
204 cJSON *root = GetJsonObjFromProperty(context, name);
205 APPSPAWN_CHECK_ONLY_EXPER(root != NULL, return 0);
206 int ret = MountAllHsp(context, root);
207 cJSON_Delete(root);
208 return ret;
209 }
210
ProcessDataGroupConfig(const SandboxContext * context,const AppSpawnSandboxCfg * appSandBox,const char * name)211 static int ProcessDataGroupConfig(const SandboxContext *context, const AppSpawnSandboxCfg *appSandBox, const char *name)
212 {
213 cJSON *root = GetJsonObjFromProperty(context, name);
214 APPSPAWN_CHECK_ONLY_EXPER(root != NULL, return 0);
215 int ret = MountAllGroup(context, root);
216 cJSON_Delete(root);
217 return ret;
218 }
219
ProcessOverlayAppConfig(const SandboxContext * context,const AppSpawnSandboxCfg * appSandBox,const char * name)220 static int ProcessOverlayAppConfig(const SandboxContext *context,
221 const AppSpawnSandboxCfg *appSandBox, const char *name)
222 {
223 uint32_t size = 0;
224 char *extInfo = (char *)GetAppSpawnMsgExtInfo(context->message, name, &size);
225 if (size == 0 || extInfo == NULL) {
226 return 0;
227 }
228 APPSPAWN_LOGV("ProcessOverlayAppConfig name %{public}s value %{public}s", name, extInfo);
229 return SetOverlayAppSandboxConfig(context, extInfo);
230 }
231
232 struct ListNode g_sandboxExpandCfgList = {&g_sandboxExpandCfgList, &g_sandboxExpandCfgList};
AppSandboxExpandAppCfgCompareName(ListNode * node,void * data)233 static int AppSandboxExpandAppCfgCompareName(ListNode *node, void *data)
234 {
235 AppSandboxExpandAppCfgNode *varNode = ListEntry(node, AppSandboxExpandAppCfgNode, node);
236 return strncmp((char *)data, varNode->name, strlen(varNode->name));
237 }
238
AppSandboxExpandAppCfgComparePrio(ListNode * node1,ListNode * node2)239 static int AppSandboxExpandAppCfgComparePrio(ListNode *node1, ListNode *node2)
240 {
241 AppSandboxExpandAppCfgNode *varNode1 = ListEntry(node1, AppSandboxExpandAppCfgNode, node);
242 AppSandboxExpandAppCfgNode *varNode2 = ListEntry(node2, AppSandboxExpandAppCfgNode, node);
243 return varNode1->prio - varNode2->prio;
244 }
245
GetAppSandboxExpandAppCfg(const char * name)246 static const AppSandboxExpandAppCfgNode *GetAppSandboxExpandAppCfg(const char *name)
247 {
248 ListNode *node = OH_ListFind(&g_sandboxExpandCfgList, (void *)name, AppSandboxExpandAppCfgCompareName);
249 if (node == NULL) {
250 return NULL;
251 }
252 return ListEntry(node, AppSandboxExpandAppCfgNode, node);
253 }
254
RegisterExpandSandboxCfgHandler(const char * name,int prio,ProcessExpandSandboxCfg handleExpandCfg)255 int RegisterExpandSandboxCfgHandler(const char *name, int prio, ProcessExpandSandboxCfg handleExpandCfg)
256 {
257 APPSPAWN_CHECK_ONLY_EXPER(name != NULL && handleExpandCfg != NULL, return APPSPAWN_ARG_INVALID);
258 if (GetAppSandboxExpandAppCfg(name) != NULL) {
259 return APPSPAWN_NODE_EXIST;
260 }
261
262 size_t len = APPSPAWN_ALIGN(strlen(name) + 1);
263 AppSandboxExpandAppCfgNode *node = (AppSandboxExpandAppCfgNode *)(malloc(sizeof(AppSandboxExpandAppCfgNode) + len));
264 APPSPAWN_CHECK(node != NULL, return APPSPAWN_SYSTEM_ERROR, "Failed to create sandbox");
265 // ext data init
266 OH_ListInit(&node->node);
267 node->cfgHandle = handleExpandCfg;
268 node->prio = prio;
269 int ret = strcpy_s(node->name, len, name);
270 APPSPAWN_CHECK(ret == 0, free(node);
271 return -1, "Failed to copy name %{public}s", name);
272 OH_ListAddWithOrder(&g_sandboxExpandCfgList, &node->node, AppSandboxExpandAppCfgComparePrio);
273 return 0;
274 }
275
ProcessExpandAppSandboxConfig(const SandboxContext * context,const AppSpawnSandboxCfg * appSandBox,const char * name)276 int ProcessExpandAppSandboxConfig(const SandboxContext *context, const AppSpawnSandboxCfg *appSandBox, const char *name)
277 {
278 APPSPAWN_CHECK_ONLY_EXPER(context != NULL && appSandBox != NULL, return APPSPAWN_ARG_INVALID);
279 APPSPAWN_CHECK_ONLY_EXPER(name != NULL, return APPSPAWN_ARG_INVALID);
280 APPSPAWN_LOGV("ProcessExpandAppSandboxConfig %{public}s.", name);
281 const AppSandboxExpandAppCfgNode *node = GetAppSandboxExpandAppCfg(name);
282 if (node != NULL && node->cfgHandle != NULL) {
283 return node->cfgHandle(context, appSandBox, name);
284 }
285 return 0;
286 }
287
AddDefaultExpandAppSandboxConfigHandle(void)288 void AddDefaultExpandAppSandboxConfigHandle(void)
289 {
290 RegisterExpandSandboxCfgHandler("HspList", 0, ProcessHSPListConfig);
291 RegisterExpandSandboxCfgHandler("DataGroup", 1, ProcessDataGroupConfig);
292 RegisterExpandSandboxCfgHandler("Overlay", 2, ProcessOverlayAppConfig); // 2 priority
293 }
294
ClearExpandAppSandboxConfigHandle(void)295 void ClearExpandAppSandboxConfigHandle(void)
296 {
297 OH_ListRemoveAll(&g_sandboxExpandCfgList, NULL);
298 }
299