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