Last active
September 23, 2018 18:29
-
-
Save isilence/5f39ac761c72cda8120459c63c7d7740 to your computer and use it in GitHub Desktop.
test code for indirect call elimination in runtime with runtime code generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/mman.h> | |
#include <error.h> | |
#include <stdint.h> | |
// int mprotect(void *addr, size_t len, int prot); | |
__attribute__((noinline)) | |
int foo(int a1, int a2, int a3) | |
{ | |
return a1 + a2 + a3; | |
} | |
int (* const pfoo)(int a1, int a2, int a3) = foo; | |
__attribute__((noinline)) | |
int indirect(int a1, int a2, int a3) | |
{ | |
return pfoo(a1, a2, a3); | |
} | |
void unlock_mem(void *ptr) | |
{ | |
long vaddr = (long)ptr; | |
vaddr = vaddr / 4096 * 4096; | |
int res = mprotect((void *)(vaddr), 4096 * 2, PROT_WRITE | PROT_READ | PROT_EXEC); | |
if (res) { | |
perror("!"); | |
exit(1); | |
} | |
} | |
int main() | |
{ | |
int64_t stub = (int64_t)&indirect; | |
int64_t real = (int64_t)&foo; | |
unlock_mem((void *)stub); | |
ssize_t diff = stub - real; | |
char *pre = (char *)stub; | |
pre += 16; | |
pre[0] = 0xE9; | |
uint32_t *offset = (uint32_t *)(pre + 1); | |
offset[0] = diff + 1 + 4; | |
fprintf(stderr, "Yep!"); | |
printf("%i\n", indirect(13, 66, 666)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment