PDA

View Full Version : mprotect regions made with dlopen



kung foo man
23rd February 2014, 19:24
[20:53] php: do u know how to mprotect regions made with dlopen



int unprotect_lib(char *libname)
{
char buf[512];
char flags[4];
void *low, *high;
FILE *fp;
fp = fopen("/proc/self/maps", "r");
if ( ! fp)
return 0;
while (fgets(buf, sizeof(buf), fp))
{
if ( ! strstr(buf, libname))
continue;
if (sscanf (buf, "%p-%p %4c", &low, &high, flags) != 3)
continue;
//printf(buf);
//printf("%08x %08x diff:%.8p\n", low, high, (int)high-(int)low);
mprotect((void *)low, (int)high-(int)low, PROT_READ | PROT_WRITE | PROT_EXEC);
}
fclose(fp);
return 1;
}


For dlopen("bla") it should be unprotect_lib("bla");

You can look in /proc/self/maps or /proc/$pid/maps for the actual loaded maps at runtime.

php
23rd February 2014, 20:47
Thanks for this, maybe it'll help someone else too.