• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef APPSPAWN_TEST_STUB_H
17 #define APPSPAWN_TEST_STUB_H
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <sys/signalfd.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
25 
26 #include "cJSON.h"
27 #include "appspawn_client.h"
28 #include "appspawn_hook.h"
29 #include "appspawn_encaps.h"
30 
31 void SetBoolParamResult(const char *key, bool flag);
32 int SetSelinuxConNweb(const AppSpawnMgr *content, const AppSpawningCtx *property);
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef struct TagMountTestArg {
39     const char *originPath;
40     const char *destinationPath;
41     const char *fsType;
42     unsigned long mountFlags;
43     const char *options;
44     mode_t mountSharedFlag;
45 } MountTestArg;
46 
47 typedef struct AppSpawnContent AppSpawnContent;
48 typedef struct AppSpawnClient AppSpawnClient;
49 typedef struct TagAppSpawnReqMsgNode AppSpawnReqMsgNode;
50 typedef void *AppSpawnClientHandle;
51 typedef struct TagAppSpawnReqMsgMgr AppSpawnReqMsgMgr;
52 typedef struct TagAppSpawningCtx AppSpawningCtx;
53 typedef struct TagAppSpawnMsg AppSpawnMsg;
54 typedef struct TagAppSpawnSandboxCfg  AppSpawnSandboxCfg;
55 typedef struct TagAppSpawnExtData AppSpawnExtData;
56 typedef struct TagSandboxContext SandboxContext;
57 typedef struct TagAppSpawnedProcess AppSpawnedProcess;
58 typedef struct TagAppSpawnForkArg AppSpawnForkArg;
59 typedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
60 typedef struct TagAppSpawnMgr AppSpawnMgr;
61 typedef struct TagPathMountNode PathMountNode;
62 typedef struct TagMountTestArg MountTestArg;
63 typedef struct TagVarExtraData VarExtraData;
64 typedef struct TagSandboxSection SandboxSection;
65 typedef struct TagAppSpawnNamespace {
66     AppSpawnExtData extData;
67     int nsSelfPidFd;
68     int nsInitPidFd;
69 } AppSpawnNamespace;
70 typedef struct TagAppSpawnedProcess AppSpawnedProcessInfo;
71 
72 int AppSpawnExtDataCompareDataId(ListNode *node, void *data);
73 AppSpawnNamespace *GetAppSpawnNamespace(const AppSpawnMgr *content);
74 int SetPidNamespace(int nsPidFd, int nsType);
75 AppSpawnNamespace *CreateAppSpawnNamespace(void);
76 void DeleteAppSpawnNamespace(AppSpawnNamespace *ns);
77 void FreeAppSpawnNamespace(struct TagAppSpawnExtData *data);
78 int PreForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property);
79 int PostForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property);
80 int ProcessMgrRemoveApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo);
81 int ProcessMgrAddApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo);
82 void TryCreateSocket(AppSpawnReqMsgMgr *reqMgr);
83 
84 int MountAllGroup(const SandboxContext *context, const AppSpawnSandboxCfg *appSandbox,
85                   const cJSON *groups);
86 int MountAllHsp(const SandboxContext *context, const cJSON *hsps);
87 
88 void CheckAndCreateSandboxFile(const char *file);
89 int VarPackageNameReplace(const SandboxContext *context,
90     const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
91 int ReplaceVariableForDepSandboxPath(const SandboxContext *context,
92     const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
93 int ReplaceVariableForDepSrcPath(const SandboxContext *context,
94     const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
95 int ReplaceVariableForDepPath(const SandboxContext *context,
96     const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
97 int SpawnPrepareSandboxCfg(AppSpawnMgr *content, AppSpawningCtx *property);
98 unsigned long GetMountModeFromConfig(const cJSON *config, const char *key, unsigned long def);
99 uint32_t GetFlagIndexFromJson(const cJSON *config);
100 int ParseMountPathsConfig(AppSpawnSandboxCfg *sandbox,
101     const cJSON *mountConfigs, SandboxSection *section, uint32_t type);
102 int ParseSymbolLinksConfig(AppSpawnSandboxCfg *sandbox, const cJSON *symbolLinkConfigs,
103     SandboxSection *section);
104 int ParseGidTableConfig(AppSpawnSandboxCfg *sandbox, const cJSON *configs, SandboxSection *section);
105 
106 int AppSpawnColdStartApp(struct AppSpawnContent *content, AppSpawnClient *client);
107 void ProcessSignal(const struct signalfd_siginfo *siginfo);
108 int CreateClientSocket(uint32_t type, int block);
109 void CloseClientSocket(int socketId);
110 int ParseAppSandboxConfig(const cJSON *appSandboxConfig, AppSpawnSandboxCfg *sandbox);
111 AppSpawnSandboxCfg *CreateAppSpawnSandbox(ExtDataType type);
112 void AddDefaultVariable(void);
113 bool CheckDirRecursive(const char *path);
114 void CreateDemandSrc(const SandboxContext *context, const PathMountNode *sandboxNode, const MountTestArg *args);
115 int CheckSandboxMountNode(const SandboxContext *context,
116     const SandboxSection *section, const PathMountNode *sandboxNode, uint32_t operation);
117 int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property);
118 int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
119 int WriteMsgToChild(AppSpawningCtx *property, bool isNweb);
120 int WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count);
121 int GetCgroupPath(const AppSpawnedProcess *appInfo, char *buffer, uint32_t buffLen);
122 void SetDeveloperMode(bool mode);
123 int LoadPermission(AppSpawnClientType type);
124 void DeletePermission(AppSpawnClientType type);
125 int SetProcessName(const AppSpawnMgr *content, const AppSpawningCtx *property);
126 int SetIsolateDir(const AppSpawningCtx *property);
127 int SetCapabilities(const AppSpawnMgr *content, const AppSpawningCtx *property);
128 int SetFdEnv(AppSpawnMgr *content, AppSpawningCtx *property);
129 int PreLoadEnablePidNs(AppSpawnMgr *content);
130 int NsInitFunc();
131 int GetNsPidFd(pid_t pid);
132 int PreLoadEnablePidNs(AppSpawnMgr *content);
133 pid_t GetPidByName(const char *name);
134 int RunBegetctlBootApp(AppSpawnMgr *content, AppSpawningCtx *property);
135 void SetSystemEnv(void);
136 void RunAppSandbox(const char *ptyName);
137 HOOK_MGR *GetAppSpawnHookMgr(void);
138 int SpawnKickDogStart(AppSpawnMgr *mgrContent);
139 int AddMembersToEncapsInfo(cJSON *extInfoJson, UserEncaps *encapsInfo);
140 int SpawnSetPermissions(AppSpawningCtx *property, UserEncaps *encapsInfo);
141 int AddPermissionItemToEncapsInfo(UserEncap *encap, cJSON *permissionItem);
142 void FreeEncapsInfo(UserEncaps *encapsInfo);
143 int SpawnSetEncapsPermissions(AppSpawnMgr *content, AppSpawningCtx *property);
144 int WriteEncapsInfo(int fd, AppSpawnEncapsBaseType encapsType, const void *encapsInfo, uint32_t flag);
145 int AddPermissionIntArrayToValue(cJSON *arrayItem, UserEncap *encap, uint32_t arraySize);
146 int AddPermissionBoolArrayToValue(cJSON *arrayItem, UserEncap *encap, uint32_t arraySize);
147 int AddPermissionStrArrayToValue(cJSON *arrayItem, UserEncap *encap);
148 int AddPermissionArrayToValue(cJSON *permissionItemArr, UserEncap *encap);
149 
150 #ifdef APPSPAWN_HITRACE_OPTION
151 int FilterAppSpawnTrace(AppSpawnMgr *content, AppSpawningCtx *property);
152 #endif
153 
154 #define STUB_NEED_CHECK 0x01
155 typedef int (*ExecvFunc)(const char *pathname, char *const argv[]);
156 enum {
157     STUB_MOUNT,
158     STUB_EXECV,
159     STUB_MAX,
160 };
161 
162 typedef struct {
163     uint16_t type;
164     uint16_t flags;
165     int result;
166     void *arg;
167 } StubNode;
168 StubNode *GetStubNode(int type);
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif // APPSPAWN_TEST_STUB_H
174