Created
April 6, 2011 13:03
-
-
Save gobenji/905600 to your computer and use it in GitHub Desktop.
A wrapper around mprotect(2)
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
/* | |
* [email protected] | |
* | |
* A wrapper around mprotect(2) | |
* build with: | |
* cc -Wall -g -ldl wrapper.c -shared -fPIC -o wrapper.so | |
* | |
* run with: | |
* LD_PRELOAD=./somepath/wrapper.so ./otherprogram | |
* | |
*/ | |
#define _GNU_SOURCE | |
#include <execinfo.h> | |
#include <dlfcn.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
int (*fp)(void *, size_t, int); | |
void __attribute__ ((constructor)) init(void) | |
{ | |
fp = dlsym(RTLD_NEXT, "mprotect"); | |
} | |
/* Change the memory protection of the region starting at ADDR and | |
extending LEN bytes to PROT. Returns 0 if successful, -1 for errors | |
(and sets errno). */ | |
int mprotect(void *__addr, size_t __len, int __prot) | |
{ | |
void *buffer[40]; | |
char **strings; | |
int retval; | |
int i; | |
printf("Calling mprotect(%p, %zd, %d)\n", __addr, __len, __prot); | |
retval = backtrace(buffer, ARRAY_SIZE(buffer)); | |
printf(" Backtrace (%d entries):\n", retval); | |
strings = backtrace_symbols(buffer, retval); | |
for (i = 0; i < retval; i++) { | |
printf(" %s\n", strings[i]); | |
} | |
free(strings); | |
return fp(__addr, __len, __prot); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment