Skip to content

Commit

Permalink
fix finding arch for mach-o (#265)
Browse files Browse the repository at this point in the history
* fix finding arch for mach-o (some fat mach-o may contain mach-o slices of both arm64e.v1 and arm64e.v2)

* fix parameter and environment variable passing issues of exec*

* fix PATH for execvP
  • Loading branch information
RootHide authored Oct 14, 2023
1 parent df0a1d8 commit 269e5d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
10 changes: 6 additions & 4 deletions BaseBin/libjailbreak/src/macho.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int64_t machoFindArch(FILE *machoFile, uint32_t subtypeToSearch)
struct mach_header_64 mh;
fseek(machoFile, archOffset, SEEK_SET);
fread(&mh, sizeof(mh), 1, machoFile);
uint32_t maskedSubtype = OSSwapLittleToHostInt32(mh.cpusubtype) & ~0x80000000;
uint32_t maskedSubtype = OSSwapLittleToHostInt32(mh.cpusubtype);
if (maskedSubtype == subtypeToSearch) {
outArchOffset = archOffset;
*stop = YES;
Expand All @@ -82,11 +82,13 @@ int64_t machoFindArch(FILE *machoFile, uint32_t subtypeToSearch)
int64_t machoFindBestArch(FILE *machoFile)
{
#if __arm64e__
int64_t archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64E);
int64_t archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64E|0x80000000);
if (archOffsetCandidate < 0) {
archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64_ALL);
archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64E);
if (archOffsetCandidate < 0) {
archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64_ALL);
}
}
return archOffsetCandidate;
#else
int64_t archOffsetCandidate = machoFindArch(machoFile, CPU_SUBTYPE_ARM64_ALL);
return archOffsetCandidate;
Expand Down
9 changes: 7 additions & 2 deletions BaseBin/systemhook/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,13 @@ int resolvePath(const char *file, const char *searchPath, int (^attemptHandler)(
struct stat sb;
char path_buf[PATH_MAX];

if ((env_path = getenv("PATH")) == NULL)
env_path = _PATH_DEFPATH;
env_path = searchPath;
if (!env_path) {
env_path = getenv("PATH");
if (!env_path) {
env_path = _PATH_DEFPATH;
}
}

/* If it's an absolute or relative path name, it's easy. */
if (index(file, '/')) {
Expand Down
28 changes: 16 additions & 12 deletions BaseBin/systemhook/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ int execle_hook(const char *path, const char *arg0, ... /*, (char *)0, char *con
// Get argument count
va_list args_copy;
va_copy(args_copy, args);
int arg_count = 0;
int arg_count = 1;
for (char *arg = va_arg(args_copy, char *); arg != NULL; arg = va_arg(args_copy, char *)) {
arg_count++;
}
va_end(args_copy);

char *argv[arg_count+1];
argv[0] = (char*)arg0;
for (int i = 0; i < arg_count-1; i++) {
char *arg = va_arg(args, char*);
argv[i] = arg;
argv[i+1] = arg;
}
argv[arg_count] = NULL;

Expand All @@ -161,21 +162,22 @@ int execlp_hook(const char *file, const char *arg0, ... /*, (char *)0 */)
// Get argument count
va_list args_copy;
va_copy(args_copy, args);
int arg_count = 0;
int arg_count = 1;
for (char *arg = va_arg(args_copy, char*); arg != NULL; arg = va_arg(args_copy, char*)) {
arg_count++;
}
va_end(args_copy);

char **argv = malloc((arg_count+1) * sizeof(char *));
argv[0] = (char*)arg0;
for (int i = 0; i < arg_count-1; i++) {
char *arg = va_arg(args, char*);
argv[i] = arg;
argv[i+1] = arg;
}
argv[arg_count] = NULL;

int r = resolvePath(file, NULL, ^int(char *path) {
return execve_hook(path, argv, NULL);
return execve_hook(path, argv, environ);
});

free(argv);
Expand All @@ -191,38 +193,39 @@ int execl_hook(const char *path, const char *arg0, ... /*, (char *)0 */)
// Get argument count
va_list args_copy;
va_copy(args_copy, args);
int arg_count = 0;
int arg_count = 1;
for (char *arg = va_arg(args_copy, char*); arg != NULL; arg = va_arg(args_copy, char*)) {
arg_count++;
}
va_end(args_copy);

char *argv[arg_count+1];
argv[0] = (char*)arg0;
for (int i = 0; i < arg_count-1; i++) {
char *arg = va_arg(args, char*);
argv[i] = arg;
argv[i+1] = arg;
}
argv[arg_count] = NULL;

return execve_hook(path, argv, NULL);
return execve_hook(path, argv, environ);
}

int execv_hook(const char *path, char *const argv[])
{
return execve_hook(path, argv, NULL);
return execve_hook(path, argv, environ);
}

int execvp_hook(const char *file, char *const argv[])
{
return resolvePath(file, NULL, ^int(char *path) {
return execve_hook(path, argv, NULL);
return execve_hook(path, argv, environ);
});
}

int execvP_hook(const char *file, const char *search_path, char *const argv[])
{
return resolvePath(file, search_path, ^int(char *path) {
return execve_hook(path, argv, NULL);
return execve_hook(path, argv, environ);
});
}

Expand Down Expand Up @@ -440,6 +443,7 @@ DYLD_INTERPOSE(execve_hook, execve)
DYLD_INTERPOSE(execle_hook, execle)
DYLD_INTERPOSE(execlp_hook, execlp)
DYLD_INTERPOSE(execv_hook, execv)
DYLD_INTERPOSE(execl_hook, execl)
DYLD_INTERPOSE(execvp_hook, execvp)
DYLD_INTERPOSE(execvP_hook, execvP)
DYLD_INTERPOSE(dlopen_hook, dlopen)
Expand All @@ -451,4 +455,4 @@ 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(fork_hook, fork)
DYLD_INTERPOSE(vfork_hook, vfork)
DYLD_INTERPOSE(vfork_hook, vfork)

0 comments on commit 269e5d0

Please sign in to comment.