From 910029988be809923d0fd59102b91a377404331e Mon Sep 17 00:00:00 2001 From: opa334 Date: Fri, 4 Aug 2023 16:00:41 +0200 Subject: [PATCH] Only load forkfix when absolutely neccessary --- BaseBin/systemhook/src/common.c | 13 ------------- BaseBin/systemhook/src/common.h | 2 ++ BaseBin/systemhook/src/main.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/BaseBin/systemhook/src/common.c b/BaseBin/systemhook/src/common.c index e5be05ae9..066eeb864 100644 --- a/BaseBin/systemhook/src/common.c +++ b/BaseBin/systemhook/src/common.c @@ -63,14 +63,6 @@ bool stringEndsWith(const char* str, const char* suffix) return !strcmp(str + str_len - suffix_len, suffix); } -void loadForkFix(void) -{ - static dispatch_once_t onceToken; - dispatch_once (&onceToken, ^{ - dlopen(JB_ROOT_PATH("/basebin/forkfix.dylib"), RTLD_NOW); - }); -} - extern char **environ; kern_return_t bootstrap_look_up(mach_port_t port, const char *service, mach_port_t *server_port); @@ -213,11 +205,6 @@ int64_t jbdswDebugMe(void) } if (result == 0) { swh_is_debugged = true; - // Once this process has wx_allowed, we need to load forkfix to ensure forking will work - // Optimization: If the process cannot fork at all due to sandbox, we don't need to load forkfix - if (sandbox_check(getpid(), "process-fork", SANDBOX_CHECK_NO_REPORT, NULL) == 0) { - loadForkFix(); - } } return result; } diff --git a/BaseBin/systemhook/src/common.h b/BaseBin/systemhook/src/common.h index 051fdfdd6..96cd905eb 100644 --- a/BaseBin/systemhook/src/common.h +++ b/BaseBin/systemhook/src/common.h @@ -3,6 +3,8 @@ extern char *JB_SandboxExtensions; extern char *JB_RootPath; +extern bool swh_is_debugged; + #define JB_ROOT_PATH(path) ({ \ char *outPath = alloca(PATH_MAX); \ strlcpy(outPath, JB_RootPath, PATH_MAX); \ diff --git a/BaseBin/systemhook/src/main.c b/BaseBin/systemhook/src/main.c index df324d1ea..e4fbce64f 100644 --- a/BaseBin/systemhook/src/main.c +++ b/BaseBin/systemhook/src/main.c @@ -6,8 +6,6 @@ #include #include "sandbox.h" -extern bool swh_is_debugged; - int ptrace(int request, pid_t pid, caddr_t addr, int data); #define PT_ATTACH 10 /* trace some running process */ #define PT_ATTACHEXC 14 /* attach to running process with signal exception */ @@ -319,6 +317,32 @@ int ptrace_hook(int request, pid_t pid, caddr_t addr, int data) return retval; } +void loadForkFix(void) +{ + if (swh_is_debugged) { + static dispatch_once_t onceToken; + dispatch_once (&onceToken, ^{ + // Once this process has wx_allowed, we need to load forkfix to ensure forking will work + // Optimization: If the process cannot fork at all due to sandbox, we don't need to load forkfix + if (sandbox_check(getpid(), "process-fork", SANDBOX_CHECK_NO_REPORT, NULL) == 0) { + dlopen(JB_ROOT_PATH("/basebin/forkfix.dylib"), RTLD_NOW); + } + }); + } +} + +pid_t fork_hook(void) +{ + loadForkFix(); + return fork(); +} + +pid_t vfork_hook(void) +{ + loadForkFix(); + return vfork(); +} + bool shouldEnableTweaks(void) { if (access(JB_ROOT_PATH("/basebin/.safe_mode"), F_OK) == 0) { @@ -425,4 +449,6 @@ DYLD_INTERPOSE(dlopen_preflight_hook, dlopen_preflight) DYLD_INTERPOSE(sandbox_init_hook, sandbox_init) DYLD_INTERPOSE(sandbox_init_with_parameters_hook, sandbox_init_with_parameters) DYLD_INTERPOSE(sandbox_init_with_extensions_hook, sandbox_init_with_extensions) -DYLD_INTERPOSE(ptrace_hook, ptrace) \ No newline at end of file +DYLD_INTERPOSE(ptrace_hook, ptrace) +DYLD_INTERPOSE(fork_hook, fork) +DYLD_INTERPOSE(vfork_hook, vfork) \ No newline at end of file