From dc55b81456e408850a44f8368819d41d50cc02d8 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Thu, 22 Nov 2018 10:11:01 +0200 Subject: refactor trampoline to use relative jmp/call --- src/wrapper/wrapper.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/wrapper/wrapper.c b/src/wrapper/wrapper.c index 5b319f3..8ce6ffb 100644 --- a/src/wrapper/wrapper.c +++ b/src/wrapper/wrapper.c @@ -34,9 +34,12 @@ verbose_log(const char *fmt, ...) # ifdef ANDROID_X86_LINKER __asm__( "wrapper_start: nop\n" - "wrapper_symbol: pushl $0xFAFBFCFD\n" - "wrapper_trace: movl $0xFAFBFCFD, %eax\ncall *%eax\npop %eax\n" - "wrapper_call: movl $0xFAFBFCFD, %eax\njmp *%eax\n" + "wrapper_store: push %edi\npush %esp\npush %ebp\npush %ebx\npush %eax\npush %ecx\npush %edx\n" + "wrapper_symbol: pushl $0xFAFBFCFD\n" // arg1 for trace + "wrapper_trace: .byte 0xE8, 0xFA, 0xFB, 0xFC, 0xFD\n" // CALL (trace) + "wrapper_restore: pop %eax\npop %edx\npop %ecx\npop %eax\npop %ebx\npop %ebp\npop %esp\npop %edi\n" + "wrapper_jmp: .byte 0xE9, 0xFA, 0xFB, 0xFC, 0xFD\n" // JMP + "wrapper_ud: .byte 0x0F, 0xFF\n" // UD "wrapper_end: nop\n" ); # define WRAPPER_TRACE @@ -46,7 +49,7 @@ __asm__( #endif #ifdef WRAPPER_TRACE -extern char wrapper_start, wrapper_symbol, wrapper_trace, wrapper_call, wrapper_end; +extern unsigned char wrapper_start, wrapper_symbol, wrapper_trace, wrapper_restore, wrapper_jmp, wrapper_ud, wrapper_end; static union { void *ptr; @@ -104,9 +107,16 @@ wrapper_create(const char *const symbol, void *function) return NULL; } + return function; + #ifdef WRAPPER_TRACE + static const union { + void *ptr; + void (*fun)(const char*); + } tracefun = { .fun = trace }; + const size_t len = strlen(symbol) + 1; - char *copy = malloc(len); + const char *copy = malloc(len); assert(copy && "welp, malloc failed"); memcpy(copy, symbol, len); const size_t sz = &wrapper_end - &wrapper_start; @@ -114,9 +124,16 @@ wrapper_create(const char *const symbol, void *function) assert(fun != MAP_FAILED); memcpy(fun, &wrapper_start, sz); #ifdef ANDROID_X86_LINKER - memcpy(fun + (&wrapper_symbol - &wrapper_start) + 1, ©, sizeof(symbol)); - memcpy(fun + (&wrapper_trace - &wrapper_start) + 1, (uintptr_t[]){ (uintptr_t)trace }, sizeof(uintptr_t)); - memcpy(fun + (&wrapper_call - &wrapper_start) + 1, &function, sizeof(function)); + memcpy(fun + (&wrapper_symbol - &wrapper_start) + 1, ©, sizeof(copy)); + { + const unsigned char *from = fun + (&wrapper_restore - &wrapper_start); + const unsigned char *to = (unsigned char*)tracefun.ptr - from; + memcpy(fun + (&wrapper_trace - &wrapper_start) + 1, &to, sizeof(to)); + }{ + const unsigned char *from = fun + (&wrapper_ud - &wrapper_start); + const unsigned char *to = (unsigned char*)function - from; + memcpy(fun + (&wrapper_jmp - &wrapper_start) + 1, &to, sizeof(to)); + } #else # error "you forgot to implement the pointer setups for your asm platform" #endif -- cgit v1.2.3