Skip to content

Commit

Permalink
fix JIT may not work on forked process correctly when tweak disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
roothider committed May 14, 2024
1 parent e0258b4 commit 97ec59f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 36 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ jobs:
runs-on: macos-13
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0
fetch-tags: true

- name: Tags
run: |
set -x
git fetch --tags https://github.com/roothide/Dopamine2-roothide
git tag
git log --first-parent --pretty=%d
git describe --tags --abbrev=0
git clone --recursive https://github.com/roothide/Dopamine2-roothide ${{ github.workspace }}
- name: Install Procursus
uses: dhinakg/procursus-action@main
Expand Down
4 changes: 4 additions & 0 deletions BaseBin/systemhook/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <spawn.h>
#include <xpc/xpc.h>

#include <stdlib.h>
#include <sys/syslog.h>
#define SYSLOG(progname, ...) do {if(strcmp(getprogname(),progname)!=0)break;openlog(progname,LOG_PID,LOG_AUTH);syslog(LOG_DEBUG, __VA_ARGS__);closelog();} while(0)

extern char HOOK_DYLIB_PATH[];
extern char *JB_BootUUID;
extern char *JB_RootPath;
Expand Down
113 changes: 89 additions & 24 deletions BaseBin/systemhook/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,25 +364,41 @@ void loadForkFix(void)
pid_t fork_hook(void)
{
loadForkFix();
return fork();
pid_t ret = fork();
if(ret==0 && !gShouldFixFork) {
jbclient_platform_set_process_debugged(getpid(), false);
}
return ret;
}

pid_t vfork_hook(void)
{
loadForkFix();
return vfork();
pid_t ret = vfork();
if(ret==0 && !gShouldFixFork) {
jbclient_platform_set_process_debugged(getpid(), false);
}
return ret;
}

pid_t forkpty_hook(int *amaster, char *name, struct termios *termp, struct winsize *winp)
{
loadForkFix();
return forkpty(amaster, name, termp, winp);
int ret = forkpty(amaster, name, termp, winp);
if(ret==0 && !gShouldFixFork) {
jbclient_platform_set_process_debugged(getpid(), false);
}
return ret;
}

int daemon_hook(int __nochdir, int __noclose)
{
loadForkFix();
return daemon(__nochdir, __noclose);
int ret = daemon(__nochdir, __noclose);
if(ret==0 && !gShouldFixFork) {
jbclient_platform_set_process_debugged(getpid(), false);
}
return ret;
}

#else
Expand Down Expand Up @@ -635,6 +651,33 @@ void redirectDirs(const char* rootdir)
}


extern void* _dyld_get_shared_cache_range(size_t* length);

int syscall_issetugid();
int new_issetugidhook()
{
void* caller = __builtin_return_address(0);

size_t length=0;
void* start = _dyld_get_shared_cache_range(&length);

if((uint64_t)caller >= (uint64_t)start && (uint64_t)caller < ((uint64_t)start+length))
{
return 0;
}

return syscall_issetugid();
}

void loadPathFix(void)
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
gShouldFixFork = true;
litehook_hook_function((void *)&issetugid, (void *)&new_issetugidhook);
});
}

char HOOK_DYLIB_PATH[PATH_MAX] = {0};

__attribute__((constructor)) static void initializer(void)
Expand All @@ -645,6 +688,11 @@ __attribute__((constructor)) static void initializer(void)

jbclient_process_checkin(&JB_RootPath, &JB_BootUUID, &JB_SandboxExtensions, &gFullyDebugged);

if(issetugid()==1) {
//for persona?
loadPathFix();
}

redirectDirs(JB_RootPath);

// Apply sandbox extensions
Expand Down Expand Up @@ -674,7 +722,6 @@ __attribute__((constructor)) static void initializer(void)
// On arm64, writing to executable pages removes CS_VALID from the csflags of the process
// These hooks are neccessary to get the system to behave with this
// They are ugly but needed
gShouldFixFork = true;
litehook_hook_function(csops, csops_hook);
litehook_hook_function(csops_audittoken, csops_audittoken_hook);
if (__builtin_available(iOS 16.0, *)) {
Expand Down Expand Up @@ -731,21 +778,6 @@ DYLD_INTERPOSE(forkpty_hook, forkpty)
DYLD_INTERPOSE(daemon_hook, daemon)
#endif


int new_issetugidhook()
{
return 0;
}

void loadPathFix(void)
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
gShouldFixFork = true;
litehook_hook_function((void *)&issetugid, (void *)&new_issetugidhook);
});
}

uid_t setuid_hook(uid_t uid) {
loadPathFix();
return setuid(uid);
Expand All @@ -754,16 +786,49 @@ uid_t seteuid_hook(uid_t uid) {
loadPathFix();
return seteuid(uid);
}
uid_t setgid_hook(uid_t uid) {
uid_t setruid_hook(uid_t uid) {
loadPathFix();
return setruid(uid);
}
uid_t setreuid_hook(uid_t ruid, uid_t euid) {
loadPathFix();
return setreuid(ruid,euid);
}

gid_t setgid_hook(gid_t gid) {
loadPathFix();
return setgid(gid);
}
gid_t setegid_hook(gid_t gid) {
loadPathFix();
return setegid(gid);
}
gid_t setrgid_hook(gid_t gid) {
loadPathFix();
return setgid(uid);
return setrgid(gid);
}
uid_t setegid_hook(uid_t uid) {
gid_t setregid_hook(gid_t rgid, gid_t egid) {
loadPathFix();
return setegid(uid);
return setregid(rgid,egid);
}

int initgroups_hook(const char * user, int group) {
loadPathFix();
return initgroups(user,group);
}
int setgroups_hook(int n, const gid_t* ids) {
loadPathFix();
return setgroups(n,ids);
}

DYLD_INTERPOSE(setuid_hook, setuid)
DYLD_INTERPOSE(seteuid_hook, seteuid)
DYLD_INTERPOSE(setruid_hook, setruid)
DYLD_INTERPOSE(setreuid_hook, setreuid)
DYLD_INTERPOSE(setgid_hook, setgid)
DYLD_INTERPOSE(setegid_hook, setegid)
DYLD_INTERPOSE(setrgid_hook, setrgid)
DYLD_INTERPOSE(setregid_hook, setregid)
DYLD_INTERPOSE(initgroups_hook, initgroups)
DYLD_INTERPOSE(setgroups_hook, setgroups)

12 changes: 12 additions & 0 deletions BaseBin/systemhook/src/syscall.S
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ _syscall__posix_spawn:
LDP X29, X30, [SP],#0x10
_spawn_ret:
ret

.global _syscall_issetugid
_syscall_issetugid:
MOV X16, #0x147
SVC 0x80
B.CC _ret
STP X29, X30, [SP,#-0x10]!
MOV X29, SP
BL _cerror_nocancel
LDP X29, X30, [SP],#0x10
_ret:
ret
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Discord: https://discord.com/invite/scqCkumAYp


# How To Build
# How To Build tipa

fork this repo then goto tab [Actions] -> [build Dopamine2-roothide] -> [Run Workflow] to generate tipa file.

Expand Down

0 comments on commit 97ec59f

Please sign in to comment.