Skip to content

Commit

Permalink
Merge branch 'opa334:master' into 1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
wwg135 authored Aug 4, 2023
2 parents b736663 + 9100299 commit f7eadf6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
13 changes: 0 additions & 13 deletions BaseBin/systemhook/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions BaseBin/systemhook/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down
32 changes: 29 additions & 3 deletions BaseBin/systemhook/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <sys/stat.h>
#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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
DYLD_INTERPOSE(ptrace_hook, ptrace)
DYLD_INTERPOSE(fork_hook, fork)
DYLD_INTERPOSE(vfork_hook, vfork)

0 comments on commit f7eadf6

Please sign in to comment.