• 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 #include "app_spawn_stub.h"
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <linux/capability.h>
21 #include <pthread.h>
22 #include <pwd.h>
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <unistd.h>
29 
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36 
37 #include "appspawn_hook.h"
38 #include "appspawn_server.h"
39 #include "appspawn_sandbox.h"
40 #include "hilog/log.h"
41 #include "securec.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 StubNode g_stubNodes[] = {
48     {STUB_MOUNT, 0, 0, NULL},
49     {STUB_EXECV, 0, 0, NULL},
50 };
51 
GetStubNode(int type)52 StubNode *GetStubNode(int type)
53 {
54     if (type >= (int)(sizeof(g_stubNodes) / sizeof(g_stubNodes[0]))) {
55         return NULL;
56     }
57 
58     return &g_stubNodes[type];
59 }
60 
DlopenStub(const char * pathname,int mode)61 void *DlopenStub(const char *pathname, int mode)
62 {
63     UNUSED(pathname);
64     UNUSED(mode);
65     static size_t index = 0;
66     return &index;
67 }
68 
InitEnvironmentParamStub(const char * name)69 static bool InitEnvironmentParamStub(const char *name)
70 {
71     UNUSED(name);
72     return true;
73 }
74 
SetRendererSecCompPolicyStub(void)75 static bool SetRendererSecCompPolicyStub(void)
76 {
77     return true;
78 }
79 
NWebRenderMainStub(const char * cmd)80 static void NWebRenderMainStub(const char *cmd)
81 {
82     printf("NWebRenderMainStub cmd %s \n", cmd);
83 }
84 
85 uint32_t g_dlsymResultFlags = 0;
86 #define DLSYM_FAIL_SET_SEC_POLICY 0x01
87 #define DLSYM_FAIL_NWEB_MAIN 0x02
88 #define DLSYM_FAIL_INIT_ENV 0x04
SetDlsymResult(uint32_t flags,bool success)89 void SetDlsymResult(uint32_t flags, bool success)
90 {
91     if (success) {
92         g_dlsymResultFlags &= ~flags;
93     } else {
94         g_dlsymResultFlags |= flags;
95     }
96 }
97 
DlsymStub(void * handle,const char * symbol)98 void *DlsymStub(void *handle, const char *symbol)
99 {
100     printf("DlsymStub %s \n", symbol);
101     UNUSED(handle);
102     if (strcmp(symbol, "InitEnvironmentParam") == 0) {
103         return ((g_dlsymResultFlags & DLSYM_FAIL_INIT_ENV) == 0) ? (void *)(InitEnvironmentParamStub) : NULL;
104     }
105     if (strcmp(symbol, "SetRendererSeccompPolicy") == 0) {
106         return ((g_dlsymResultFlags & DLSYM_FAIL_SET_SEC_POLICY) == 0) ? (void *)(SetRendererSecCompPolicyStub) : NULL;
107     }
108     if (strcmp(symbol, "NWebRenderMain") == 0) {
109         return ((g_dlsymResultFlags & DLSYM_FAIL_NWEB_MAIN) == 0) ? (void *)(NWebRenderMainStub) : NULL;
110     }
111     if (strcmp(symbol, "GetPermissionIndex") == 0) {
112         return (void *) (GetPermissionIndex);
113     }
114     return NULL;
115 }
116 
DlcloseStub(void * handle)117 int DlcloseStub(void *handle)
118 {
119     UNUSED(handle);
120     return 0;
121 }
122 
DisallowInternet(void)123 void DisallowInternet(void)
124 {
125 }
126 
may_init_gwp_asan(bool forceInit)127 bool may_init_gwp_asan(bool forceInit)
128 {
129     return false;
130 }
131 
SetgroupsStub(size_t size,const gid_t * list)132 int SetgroupsStub(size_t size, const gid_t *list)
133 {
134     UNUSED(size);
135     UNUSED(list);
136     return 0;
137 }
138 
SetresuidStub(uid_t ruid,uid_t euid,uid_t suid)139 int SetresuidStub(uid_t ruid, uid_t euid, uid_t suid)
140 {
141     UNUSED(ruid);
142     UNUSED(euid);
143     UNUSED(suid);
144     return 0;
145 }
146 
SetresgidStub(gid_t rgid,gid_t egid,gid_t sgid)147 int SetresgidStub(gid_t rgid, gid_t egid, gid_t sgid)
148 {
149     UNUSED(rgid);
150     UNUSED(egid);
151     UNUSED(sgid);
152     return 0;
153 }
154 
CapsetStub(cap_user_header_t hdrp,const cap_user_data_t datap)155 int CapsetStub(cap_user_header_t hdrp, const cap_user_data_t datap)
156 {
157     UNUSED(hdrp);
158     UNUSED(datap);
159     return 0;
160 }
161 
UnshareStub(int flags)162 int UnshareStub(int flags)
163 {
164     printf("UnshareStub %x \n", flags);
165     return 0;
166 }
167 
MountStub(const char * originPath,const char * destinationPath,const char * fsType,unsigned long mountFlags,const char * options,mode_t mountSharedFlag)168 int MountStub(const char *originPath, const char *destinationPath,
169     const char *fsType, unsigned long mountFlags, const char *options, mode_t mountSharedFlag)
170 {
171     StubNode *node = GetStubNode(STUB_MOUNT);
172     if (node == NULL || node->arg == NULL || (node->flags & STUB_NEED_CHECK) != STUB_NEED_CHECK) {
173         return 0;
174     }
175     MountArg *args = (MountArg *)node->arg;
176 
177     printf("args->originPath %s == %s \n", args->originPath, originPath);
178     printf("args->destinationPath %s == %s \n", args->destinationPath, destinationPath);
179     printf("args->fsType %s == %s \n", args->fsType, fsType);
180     printf("args->options %s == %s \n", args->options, options);
181     printf("mountFlags %lx args->mountFlags %lx \n", mountFlags, args->mountFlags);
182     printf("mountSharedFlag 0x%x args->mountSharedFlag 0x%x \n", mountSharedFlag, args->mountSharedFlag);
183 
184     if (originPath != NULL && (strcmp(originPath, args->originPath) == 0)) {
185         int result = (destinationPath != NULL && (strcmp(destinationPath, args->destinationPath) == 0) &&
186             (mountFlags == args->mountFlags) &&
187             (args->fsType == NULL || (fsType != NULL && strcmp(fsType, args->fsType) == 0)) &&
188             (args->options == NULL || (options != NULL && strcmp(options, args->options) == 0)));
189         errno = result ? 0 : -EINVAL;
190         node->result = result ? 0 : -EINVAL;
191         printf("MountStub result %d node->result %d \n", result, node->result);
192         return errno;
193     }
194     return 0;
195 }
196 
SymlinkStub(const char * target,const char * linkName)197 int SymlinkStub(const char *target, const char *linkName)
198 {
199     return 0;
200 }
201 
ChdirStub(const char * path)202 int ChdirStub(const char *path)
203 {
204     return 0;
205 }
206 
ChrootStub(const char * path)207 int ChrootStub(const char *path)
208 {
209     return 0;
210 }
211 
SyscallStub(long int type,...)212 long int SyscallStub(long int type, ...)
213 {
214     return 0;
215 }
216 
Umount2Stub(const char * path,int type)217 int Umount2Stub(const char *path, int type)
218 {
219     return 0;
220 }
221 
UmountStub(const char * path)222 int UmountStub(const char *path)
223 {
224     return 0;
225 }
226 
mallopt(int param,int value)227 int mallopt(int param, int value)
228 {
229     return 0;
230 }
231 
AccessStub(const char * pathName,int mode)232 int AccessStub(const char *pathName, int mode)
233 {
234     if (strstr(pathName, "/data/app/el2/50/base") != NULL) {
235         return -1;
236     }
237     if (strstr(pathName, "/mnt/sandbox/50/com.example.myapplication/data/storage/el2") != NULL) {
238         return -1;
239     }
240     if (strstr(pathName, "/data/app/el5/100/base/com.example.myapplication") != NULL) {
241         return -1;
242     }
243     return 0;
244 }
245 
ExecvStub(const char * pathName,char * const argv[])246 int ExecvStub(const char *pathName, char *const argv[])
247 {
248     printf("ExecvStub %s \n", pathName);
249     StubNode *node = GetStubNode(STUB_EXECV);
250     if (node == NULL || node->arg == NULL || (node->flags & STUB_NEED_CHECK) != STUB_NEED_CHECK) {
251         return 0;
252     }
253 
254     ExecvFunc func = (ExecvFunc)node->arg;
255     func(pathName, argv);
256     return 0;
257 }
258 
ExecvpStub(const char * pathName,char * const argv[])259 int ExecvpStub(const char *pathName, char *const argv[])
260 {
261     printf("ExecvpStub %s \n", pathName);
262     return 0;
263 }
264 
ExecveStub(const char * pathName,char * const argv[],char * const env[])265 int ExecveStub(const char *pathName, char *const argv[], char *const env[])
266 {
267     printf("ExecveStub %s \n", pathName);
268     return 0;
269 }
270 
SetconStub(const char * name)271 int SetconStub(const char *name)
272 {
273     printf("SetconStub %s \n", name);
274     return 0;
275 }
276 
GetprocpidStub()277 int GetprocpidStub()
278 {
279     return 0;
280 }
281 
CloneStub(int (* fn)(void *),void * stack,int flags,void * arg,...)282 int CloneStub(int (*fn)(void *), void *stack, int flags, void *arg, ...)
283 {
284     printf("CloneStub 11 %d \n", getpid());
285     pid_t pid = fork();
286     if (pid == 0) {
287         fn(arg);
288         _exit(0x7f); // 0x7f user exit
289     }
290     return pid;
291 }
292 
SetuidStub(uid_t uid)293 int SetuidStub(uid_t uid)
294 {
295     return 0;
296 }
297 
SetgidStub(gid_t gid)298 int SetgidStub(gid_t gid)
299 {
300     return 0;
301 }
302 
IoctlStub(int fd,unsigned long request,...)303 int IoctlStub(int fd, unsigned long request, ...)
304 {
305     return 0;
306 }
307 
308 #ifdef __cplusplus
309 }
310 #endif
311