J
Jan Althaus
Hi,
I'm having some trouble using mprotect. A short code snippet to give
you an idea of where this is heading:
char *p;
p = malloc(1024+PAGESIZE-1);
if (!p)
exit(errno);
p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
memset( (void*)p, 0xc3, 1024 ); /* set everything to "ret" */
/* here we set p to contain some binary code */
if (mprotect(p, 1024, PROT_EXEC|PROT_WRITE|PROT_READ)) {
exit(errno);
}
__asm__ __volatile__ (
"call *%0 \n"
:
: "m"(p)
);
exit(0);
Now the problem is that the call always causes a segfault. I tried
setting the memory to just PROT_EXEC with no luck... so I'm wondering:
Is there something else I need to do in order for Linux to allow me
execution of a certain page? I'm really out of ideas here... :/
When I pass the last asm segment a pointer to a function that I
declared in c++ it works fine. So it has to be the memory protection
not doing what I expect it to...
I'm having some trouble using mprotect. A short code snippet to give
you an idea of where this is heading:
char *p;
p = malloc(1024+PAGESIZE-1);
if (!p)
exit(errno);
p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
memset( (void*)p, 0xc3, 1024 ); /* set everything to "ret" */
/* here we set p to contain some binary code */
if (mprotect(p, 1024, PROT_EXEC|PROT_WRITE|PROT_READ)) {
exit(errno);
}
__asm__ __volatile__ (
"call *%0 \n"
:
: "m"(p)
);
exit(0);
Now the problem is that the call always causes a segfault. I tried
setting the memory to just PROT_EXEC with no luck... so I'm wondering:
Is there something else I need to do in order for Linux to allow me
execution of a certain page? I'm really out of ideas here... :/
When I pass the last asm segment a pointer to a function that I
declared in c++ it works fine. So it has to be the memory protection
not doing what I expect it to...