diff --git a/BaseBin/launchdhook/Makefile b/BaseBin/launchdhook/Makefile index 8d609b3d9..90daefc1e 100644 --- a/BaseBin/launchdhook/Makefile +++ b/BaseBin/launchdhook/Makefile @@ -11,7 +11,7 @@ endif sign: $(TARGET) @ldid -S $^ -$(TARGET): $(wildcard src/*.m src/*.c src/jbserver/*.c ../systemhook/src/common.c ../systemhook/src/envbuf.c) +$(TARGET): $(wildcard src/*.m src/*.c src/jbserver/*.c ../systemhook/src/common.c ../systemhook/src/envbuf.c ../systemhook/src/syscall.S) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ clean: diff --git a/BaseBin/launchdhook/src/main.m b/BaseBin/launchdhook/src/main.m index 1c6b0f9c5..a0adbe822 100644 --- a/BaseBin/launchdhook/src/main.m +++ b/BaseBin/launchdhook/src/main.m @@ -17,6 +17,7 @@ #import "boomerang.h" #import "update.h" #import "exec_patch.h" +#include "../systemhook/src/common.h" char HOOK_DYLIB_PATH[PATH_MAX] = {0}; @@ -193,7 +194,10 @@ void lockAllDSCText(void) initJetsamHook(); initSpawnExecPatch(); - + void* __sysctl_orig = NULL; + void* __sysctlbyname_orig = NULL; + MSHookFunction(&__sysctl, (void *) __sysctl_hook, &__sysctl_orig); + MSHookFunction(&__sysctlbyname, (void *) __sysctlbyname_hook, &__sysctlbyname_orig); // This will ensure launchdhook is always reinjected after userspace reboots // As this launchd will pass environ to the next launchd... diff --git a/BaseBin/systemhook/src/common.c b/BaseBin/systemhook/src/common.c index 4dfa94436..7cb903e58 100644 --- a/BaseBin/systemhook/src/common.c +++ b/BaseBin/systemhook/src/common.c @@ -501,3 +501,47 @@ int spawn_hook_common(pid_t *restrict pid, const char *restrict path, return retval; } + + +#include +int cached_namelen = 0; +int cached_name[CTL_MAXNAME+2]={0}; +int syscall__sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); +int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); +int __sysctl_hook(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen) +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + int mib[] = {0, 3}; //https://github.com/apple-oss-distributions/Libc/blob/899a3b2d52d95d75e05fb286a5e64975ec3de757/gen/FreeBSD/sysctlbyname.c#L24 + size_t namelen = sizeof(cached_name); + const char* query = "security.mac.amfi.developer_mode_status"; + if(syscall__sysctl(mib, sizeof(mib)/sizeof(mib[0]), cached_name, &namelen, (void*)query, strlen(query))==0) { + cached_namelen = namelen / sizeof(cached_name[0]); + } + }); + + if(name && namelen && cached_namelen && + namelen==cached_namelen && memcmp(cached_name, name, namelen)==0) { + if(oldp && oldlenp && *oldlenp>=sizeof(int)) { + *(int*)oldp = 1; + *oldlenp = sizeof(int); + return 0; + } + } + + return syscall__sysctl(name,namelen,oldp,oldlenp,newp,newlen); +} + +int syscall__sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); +int __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); +int __sysctlbyname_hook(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) +{ + if(name && namelen && strncmp(name, "security.mac.amfi.developer_mode_status", namelen)==0) { + if(oldp && oldlenp && *oldlenp>=sizeof(int)) { + *(int*)oldp = 1; + *oldlenp = sizeof(int); + return 0; + } + } + return syscall__sysctlbyname(name,namelen,oldp,oldlenp,newp,newlen); +} diff --git a/BaseBin/systemhook/src/common.h b/BaseBin/systemhook/src/common.h index 8802865fa..a26291c2d 100644 --- a/BaseBin/systemhook/src/common.h +++ b/BaseBin/systemhook/src/common.h @@ -21,4 +21,9 @@ int spawn_hook_common(pid_t *restrict pid, const char *restrict path, char *const envp[restrict], void *orig, int (*trust_binary)(const char *path, xpc_object_t preferredArchsArray), - int (*set_process_debugged)(uint64_t pid, bool fullyDebugged)); \ No newline at end of file + int (*set_process_debugged)(uint64_t pid, bool fullyDebugged)); + +int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); +int __sysctl_hook(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); +int __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); +int __sysctlbyname_hook(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); diff --git a/BaseBin/systemhook/src/main.c b/BaseBin/systemhook/src/main.c index b34cbb8e5..6f1e8f1fc 100644 --- a/BaseBin/systemhook/src/main.c +++ b/BaseBin/systemhook/src/main.c @@ -685,52 +685,6 @@ void loadPathFix(void) }); } -int syscall__sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); -int __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); -int __sysctlbyname_hook(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) -{ - if(name && strncmp(name, "security.mac.amfi.developer_mode_status", namelen)==0) { - if(oldp && oldlenp && *oldlenp>=sizeof(int)) { - *(int*)oldp = 1; - *oldlenp = sizeof(int); - return 0; - } - } - return syscall__sysctlbyname(name,namelen,oldp,oldlenp,newp,newlen); -} - -#include -int cached_namelen = 0; -int cached_name[CTL_MAXNAME+2]={0}; -int syscall__sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); -int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen); -int __sysctl_hook(int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen) -{ - //https://github.com/apple-oss-distributions/Libc/blob/899a3b2d52d95d75e05fb286a5e64975ec3de757/gen/FreeBSD/sysctlbyname.c#L24 - if(name && namelen==2 && name[0]==0 && name[1]==3) { - if(newp && newlen && strncmp(newp,"security.mac.amfi.developer_mode_status",newlen)==0) { - if(syscall__sysctl(name,namelen,oldp,oldlenp,newp,newlen)==0) { - if(oldp && oldlenp && *oldlenp<=sizeof(cached_name)) { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - memcpy(cached_name, oldp, *oldlenp); - cached_namelen = *oldlenp / sizeof (int); - }); - } - } - } - } - if(name && namelen && cached_namelen && - namelen==cached_namelen && memcmp(cached_name, name, namelen)==0) { - if(oldp && oldlenp && *oldlenp>=sizeof(int)) { - *(int*)oldp = 1; - *oldlenp = sizeof(int); - return 0; - } - } - return syscall__sysctl(name,namelen,oldp,oldlenp,newp,newlen); -} - char HOOK_DYLIB_PATH[PATH_MAX] = {0}; __attribute__((constructor)) static void initializer(void)