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     return NULL;
112 }
113 
DlcloseStub(void * handle)114 int DlcloseStub(void *handle)
115 {
116     UNUSED(handle);
117     return 0;
118 }
119 
DisallowInternet(void)120 void DisallowInternet(void)
121 {
122 }
123 
may_init_gwp_asan(bool forceInit)124 bool may_init_gwp_asan(bool forceInit)
125 {
126     return false;
127 }
128 
SetgroupsStub(size_t size,const gid_t * list)129 int SetgroupsStub(size_t size, const gid_t *list)
130 {
131     UNUSED(size);
132     UNUSED(list);
133     return 0;
134 }
135 
SetresuidStub(uid_t ruid,uid_t euid,uid_t suid)136 int SetresuidStub(uid_t ruid, uid_t euid, uid_t suid)
137 {
138     UNUSED(ruid);
139     UNUSED(euid);
140     UNUSED(suid);
141     return 0;
142 }
143 
SetresgidStub(gid_t rgid,gid_t egid,gid_t sgid)144 int SetresgidStub(gid_t rgid, gid_t egid, gid_t sgid)
145 {
146     UNUSED(rgid);
147     UNUSED(egid);
148     UNUSED(sgid);
149     return 0;
150 }
151 
CapsetStub(cap_user_header_t hdrp,const cap_user_data_t datap)152 int CapsetStub(cap_user_header_t hdrp, const cap_user_data_t datap)
153 {
154     UNUSED(hdrp);
155     UNUSED(datap);
156     return 0;
157 }
158 
UnshareStub(int flags)159 int UnshareStub(int flags)
160 {
161     printf("UnshareStub %x \n", flags);
162     return 0;
163 }
164 
MountStub(const char * originPath,const char * destinationPath,const char * fsType,unsigned long mountFlags,const char * options,mode_t mountSharedFlag)165 int MountStub(const char *originPath, const char *destinationPath,
166     const char *fsType, unsigned long mountFlags, const char *options, mode_t mountSharedFlag)
167 {
168     StubNode *node = GetStubNode(STUB_MOUNT);
169     if (node == NULL || node->arg == NULL || (node->flags & STUB_NEED_CHECK) != STUB_NEED_CHECK) {
170         return 0;
171     }
172     MountArg *args = (MountArg *)node->arg;
173 
174     printf("args->originPath %s == %s \n", args->originPath, originPath);
175     printf("args->destinationPath %s == %s \n", args->destinationPath, destinationPath);
176     printf("args->fsType %s == %s \n", args->fsType, fsType);
177     printf("args->options %s == %s \n", args->options, options);
178     printf("mountFlags %lx args->mountFlags %lx \n", mountFlags, args->mountFlags);
179     printf("mountSharedFlag 0x%x args->mountSharedFlag 0x%x \n", mountSharedFlag, args->mountSharedFlag);
180 
181     if (originPath != NULL && (strcmp(originPath, args->originPath) == 0)) {
182         int result = (destinationPath != NULL && (strcmp(destinationPath, args->destinationPath) == 0) &&
183             (mountFlags == args->mountFlags) &&
184             (args->fsType == NULL || (fsType != NULL && strcmp(fsType, args->fsType) == 0)) &&
185             (args->options == NULL || (options != NULL && strcmp(options, args->options) == 0)));
186         errno = result ? 0 : -EINVAL;
187         node->result = result ? 0 : -EINVAL;
188         printf("MountStub result %d node->result %d \n", result, node->result);
189         return errno;
190     }
191     return 0;
192 }
193 
SymlinkStub(const char * target,const char * linkName)194 int SymlinkStub(const char *target, const char *linkName)
195 {
196     return 0;
197 }
198 
ChdirStub(const char * path)199 int ChdirStub(const char *path)
200 {
201     return 0;
202 }
203 
ChrootStub(const char * path)204 int ChrootStub(const char *path)
205 {
206     return 0;
207 }
208 
SyscallStub(long int type,...)209 long int SyscallStub(long int type, ...)
210 {
211     return 0;
212 }
213 
Umount2Stub(const char * path,int type)214 int Umount2Stub(const char *path, int type)
215 {
216     return 0;
217 }
218 
UmountStub(const char * path)219 int UmountStub(const char *path)
220 {
221     return 0;
222 }
223 
mallopt(int param,int value)224 int mallopt(int param, int value)
225 {
226     return 0;
227 }
228 
AccessStub(const char * pathName,int mode)229 int AccessStub(const char *pathName, int mode)
230 {
231     if (strstr(pathName, "/data/app/el2/50/base") != NULL) {
232         return -1;
233     }
234     if (strstr(pathName, "/mnt/sandbox/50/com.example.myapplication/data/storage/el2") != NULL) {
235         return -1;
236     }
237     if (strstr(pathName, "/data/app/el5/100/base/com.example.myapplication") != NULL) {
238         return -1;
239     }
240     return 0;
241 }
242 
ExecvStub(const char * pathName,char * const argv[])243 int ExecvStub(const char *pathName, char *const argv[])
244 {
245     printf("ExecvStub %s \n", pathName);
246     StubNode *node = GetStubNode(STUB_EXECV);
247     if (node == NULL || node->arg == NULL || (node->flags & STUB_NEED_CHECK) != STUB_NEED_CHECK) {
248         return 0;
249     }
250 
251     ExecvFunc func = (ExecvFunc)node->arg;
252     func(pathName, argv);
253     return 0;
254 }
255 
ExecvpStub(const char * pathName,char * const argv[])256 int ExecvpStub(const char *pathName, char *const argv[])
257 {
258     printf("ExecvpStub %s \n", pathName);
259     return 0;
260 }
261 
ExecveStub(const char * pathName,char * const argv[],char * const env[])262 int ExecveStub(const char *pathName, char *const argv[], char *const env[])
263 {
264     printf("ExecveStub %s \n", pathName);
265     return 0;
266 }
267 
SetconStub(const char * name)268 int SetconStub(const char *name)
269 {
270     printf("SetconStub %s \n", name);
271     return 0;
272 }
273 
GetprocpidStub()274 int GetprocpidStub()
275 {
276     return 0;
277 }
278 
CloneStub(int (* fn)(void *),void * stack,int flags,void * arg,...)279 int CloneStub(int (*fn)(void *), void *stack, int flags, void *arg, ...)
280 {
281     printf("CloneStub 11 %d \n", getpid());
282     pid_t pid = fork();
283     if (pid == 0) {
284         fn(arg);
285         _exit(0x7f); // 0x7f user exit
286     }
287     return pid;
288 }
289 
SetuidStub(uid_t uid)290 int SetuidStub(uid_t uid)
291 {
292     return 0;
293 }
294 
SetgidStub(gid_t gid)295 int SetgidStub(gid_t gid)
296 {
297     return 0;
298 }
299 
IoctlStub(int fd,unsigned long request,...)300 int IoctlStub(int fd, unsigned long request, ...)
301 {
302     return 0;
303 }
304 
305 #ifdef __cplusplus
306 }
307 #endif
308