@@ -97,22 +97,25 @@ int __posix_spawn_orig(pid_t *restrict pid, const char *restrict path, struct _p
97
97
return syscall (SYS_posix_spawn , pid , path , desc , argv , envp );
98
98
}
99
99
100
+ int __execve_orig (const char * path , char * const argv [], char * const envp [])
101
+ {
102
+ return syscall (SYS_execve , path , argv , envp );
103
+ }
104
+
100
105
// 1. Ensure the binary about to be spawned and all of it's dependencies are trust cached
101
106
// 2. Insert "DYLD_INSERT_LIBRARIES=/usr/lib/systemhook.dylib" into all binaries spawned
102
107
// 3. Increase Jetsam limit to more sane value (Multipler defined as JETSAM_MULTIPLIER)
103
108
104
- int spawn_hook_common (pid_t * restrict pid , const char * restrict path ,
105
- struct _posix_spawn_args_desc * desc ,
106
- char * const argv [restrict],
107
- char * const envp [restrict],
108
- void * orig ,
109
- int (* trust_binary )(const char * path , xpc_object_t preferredArchsArray ),
110
- int (* set_process_debugged )(uint64_t pid , bool fullyDebugged ),
111
- double jetsamMultiplier )
109
+ static int spawn_exec_hook_common (const char * path ,
110
+ char * const argv [restrict],
111
+ char * const envp [restrict],
112
+ struct _posix_spawn_args_desc * desc ,
113
+ int (* trust_binary )(const char * path , xpc_object_t preferredArchsArray ),
114
+ double jetsamMultiplier ,
115
+ int (^orig )(char * const envp [restrict]))
112
116
{
113
- int (* pspawn_orig )(pid_t * restrict, const char * restrict, struct _posix_spawn_args_desc * , char * const [restrict], char * const [restrict]) = orig ;
114
117
if (!path ) {
115
- return pspawn_orig ( pid , path , desc , argv , envp );
118
+ return orig ( envp );
116
119
}
117
120
118
121
posix_spawnattr_t attr = NULL ;
@@ -230,60 +233,14 @@ int spawn_hook_common(pid_t *restrict pid, const char *restrict path,
230
233
* (int * )(attrStruct + POSIX_SPAWNATTR_OFF_MEMLIMIT_INACTIVE ) = memlimit_inactive * jetsamMultiplier ;
231
234
}
232
235
}
233
-
234
- // On iOS 16, disable launch constraints
235
- // Not working, doesn't seem feasable
236
- // if (__builtin_available(iOS 16.0, *)) {
237
- // uint32_t bufsize = PATH_MAX;
238
- // char executablePath[PATH_MAX];
239
- // _NSGetExecutablePath(executablePath, &bufsize);
240
- // // We could do the following here
241
- // // posix_spawnattr_set_launch_type_np(*attrp, 0);
242
- // // But I don't know how to get the compiler to weak link it
243
- // // So we just set it by offset
244
- // if (getpid() == 1) {
245
- // FILE *f = fopen("/var/mobile/launch_type.txt", "a");
246
- // const char *toLog = path;
247
- // if (!strcmp(path, "/usr/libexec/xpcproxy") && argv) {
248
- // if (argv[0]) {
249
- // if (argv[1]) {
250
- // toLog = argv[1];
251
- // }
252
- // }
253
- // }
254
- // fprintf(f, "%s has launch type %u\n", toLog, *(uint8_t *)(attrStruct + POSIX_SPAWNATTR_OFF_LAUNCH_TYPE));
255
- // fclose(f);
256
- // }
257
- // else if (!strcmp(executablePath, "/usr/libexec/xpcproxy")) {
258
- // FILE *f = fopen("/tmp/launch_type_xpcproxy.txt", "a");
259
- // if (f) {
260
- // fprintf(f, "%s has launch type %u\n", path, *(uint8_t *)(attrStruct + POSIX_SPAWNATTR_OFF_LAUNCH_TYPE));
261
- // fclose(f);
262
- // }
263
- // }
264
- // else {
265
- // os_log(OS_LOG_DEFAULT, "systemhook %{public}s has launch type %u\n", path, *(uint8_t *)(attrStruct + POSIX_SPAWNATTR_OFF_LAUNCH_TYPE));
266
- // }
267
-
268
- // *(uint8_t *)(attrStruct + POSIX_SPAWNATTR_OFF_LAUNCH_TYPE) = ...
269
- // if (!strcmp(path, "/usr/libexec/xpcproxy") && argv) {
270
- // if (argv[0]) {
271
- // if (argv[1]) {
272
- // if (string_has_prefix(argv[1], "com.apple.WebKit.WebContent.")) {
273
- // *(uint8_t *)(attrStruct + POSIX_SPAWNATTR_OFF_LAUNCH_TYPE) = 0;
274
- // }
275
- // }
276
- // }
277
- // }
278
- // }
279
236
}
280
237
}
281
238
282
- int retval = -1 ;
239
+ int r = -1 ;
283
240
284
241
if ((shouldInsertJBEnv && JBEnvAlreadyInsertedCount == 1 ) || (!shouldInsertJBEnv && JBEnvAlreadyInsertedCount == 0 && !hasSafeModeVariable )) {
285
242
// we're already good, just call orig
286
- retval = pspawn_orig ( pid , path , desc , argv , envp );
243
+ r = orig ( envp );
287
244
}
288
245
else {
289
246
// the state we want to be in is not the state we are in right now
@@ -332,11 +289,32 @@ int spawn_hook_common(pid_t *restrict pid, const char *restrict path,
332
289
envbuf_unsetenv (& envc , "_MSSafeMode" );
333
290
}
334
291
335
- retval = pspawn_orig (pid , path , desc , argv , envc );
292
+ r = orig (envc );
293
+
336
294
envbuf_free (envc );
337
295
}
338
296
339
- if (retval == 0 && pid != NULL ) {
297
+ return r ;
298
+ }
299
+
300
+ int posix_spawn_hook_shared (pid_t * restrict pid ,
301
+ const char * restrict path ,
302
+ struct _posix_spawn_args_desc * desc ,
303
+ char * const argv [restrict],
304
+ char * const envp [restrict],
305
+ void * orig ,
306
+ int (* trust_binary )(const char * path , xpc_object_t preferredArchsArray ),
307
+ int (* set_process_debugged )(uint64_t pid , bool fullyDebugged ),
308
+ double jetsamMultiplier )
309
+ {
310
+ int (* posix_spawn_orig )(pid_t * restrict, const char * restrict, struct _posix_spawn_args_desc * , char * const [restrict], char * const [restrict]) = orig ;
311
+
312
+ int r = spawn_exec_hook_common (path , argv , envp , desc , trust_binary , jetsamMultiplier , ^int (char * const envp_patched [restrict ]){
313
+ return posix_spawn_orig (pid , path , desc , argv , envp_patched );
314
+ });
315
+
316
+ if (r == 0 && pid && desc ) {
317
+ posix_spawnattr_t attr = desc -> attrp ;
340
318
short flags = 0 ;
341
319
if (posix_spawnattr_getflags (& attr , & flags ) == 0 ) {
342
320
if (flags & POSIX_SPAWN_START_SUSPENDED ) {
@@ -348,5 +326,20 @@ int spawn_hook_common(pid_t *restrict pid, const char *restrict path,
348
326
}
349
327
}
350
328
351
- return retval ;
329
+ return r ;
330
+ }
331
+
332
+ int execve_hook_shared (const char * path ,
333
+ char * const argv [],
334
+ char * const envp [],
335
+ void * orig ,
336
+ int (* trust_binary )(const char * path , xpc_object_t preferredArchsArray ))
337
+ {
338
+ int (* execve_orig )(const char * , char * const [], char * const []) = orig ;
339
+
340
+ int r = spawn_exec_hook_common (path , argv , envp , NULL , trust_binary , 0 , ^int (char * const envp_patched [restrict ]){
341
+ return execve_orig (path , argv , envp_patched );
342
+ });
343
+
344
+ return r ;
352
345
}
0 commit comments