Skip to content

Commit

Permalink
koekeishiya#725 cleanup mach bootstrap code
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed Nov 23, 2020
1 parent d290835 commit 75bca43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
21 changes: 5 additions & 16 deletions src/osax/mach_bootstrap.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <mach/mach.h>
#include <pthread.h>
#include <pthread_spis.h>
#include <unistd.h>
#include <dlfcn.h>

extern void _pthread_set_self(char *);
extern void _pthread_set_self(pthread_t *);

static void drop_privileges(void)
{
Expand All @@ -23,22 +24,10 @@ static void *mach_load_payload(void *context)
return NULL;
}

void mach_bootstrap_entry_point(char *param)
void mach_bootstrap_entry_point(void)
{
int policy;
pthread_t thread;
pthread_attr_t attr;
struct sched_param sched;

_pthread_set_self(param);
pthread_attr_init(&attr);
pthread_attr_getschedpolicy(&attr, &policy);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
sched.sched_priority = sched_get_priority_max(policy);
pthread_attr_setschedparam(&attr, &sched);
pthread_create(&thread, &attr, &mach_load_payload, NULL);
pthread_attr_destroy(&attr);

_pthread_set_self(&thread);
pthread_create_from_mach_thread(&thread, NULL, &mach_load_payload, NULL);
thread_suspend(mach_thread_self());
}
4 changes: 1 addition & 3 deletions src/osax/mach_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ bool mach_loader_inject_payload(pid_t pid)

#ifdef __arm64__
arm_thread_state64_t thread_state = {};
thread_state.__x[0] = (uint64_t) stack;
thread_state.__pc = (uint64_t) code + (uint64_t)(((void *) bootstrap_entry) - image);
thread_state.__sp = (uint64_t) (stack + (stack_size / 2) - 8);
thread_state.__sp = (uint64_t) (stack + (stack_size / 2));

kern_return_t error = thread_create_running(task, ARM_THREAD_STATE64, (thread_state_t)&thread_state, ARM_THREAD_STATE64_COUNT, &thread);
if (error != KERN_SUCCESS) {
Expand All @@ -108,7 +107,6 @@ bool mach_loader_inject_payload(pid_t pid)
}
#else
x86_thread_state64_t thread_state = {};
thread_state.__rdi = (uint64_t) stack;
thread_state.__rip = (uint64_t) code + (uint64_t)(((void *) bootstrap_entry) - image);
thread_state.__rsp = (uint64_t) (stack + (stack_size / 2) - 8);

Expand Down

0 comments on commit 75bca43

Please sign in to comment.