S
Sontu
Hi all,
Consider the following code:
#include<signal.h>
#include<stdio.h>
#include<sys/mman.h>
void handler(int sig)
{
printf("abhay: caught SIGSEGV\n\n");
}
void func(char *buffer)
{
unsigned int start=0;
//to make the starting address in mprotect as page aligned
asm("andl $-4096, %esp");
asm("movl %%esp, %0":"=r"(start));
//raise(SIGSEGV);
printf("Mprotect worked:
%d\n\n",mprotect((void*)start,4096,PROT_READ));
buffer[3]='c';
printf("Mprotect worked:
%d\n\n",mprotect((void*)start,4096,PROT_WRITE|PROT_READ|PROT_EXEC));
}
int main(void)
{
char buffer[10];
if( signal(SIGSEGV, handler)== SIG_ERR )
printf("problem installing new signal handler\n\n");
func(buffer);
printf("into main\n\n");
return 0;
}
My program makes the previous frame as write protected, thus when i am
in func() and i will try to write into the buffer that is allocated in
main(), it will generate SIGSEGV signal that is handled by my handler()
There is something wierd going on that i am not able to understand:
1. If both the mprotect functions are uncommented and i try to write
into the buffer[3]='c', SIGSEGV is generated
handler is called and it starts printing "abhay: caught SIGSEGV"
continuously on the screen until stack overflows. But it should have
printed it once and should have returned back into the func()
2. But if i comment both the mprotect and uncomment "raise(SIGSEGV)" to
generate the SIGSEGV signal explicitly, then this doesn't happen.
I am running the program on RedHat linux 8.0 and using GCC compiler.
Can anyone help me out?
Thanks.
Consider the following code:
#include<signal.h>
#include<stdio.h>
#include<sys/mman.h>
void handler(int sig)
{
printf("abhay: caught SIGSEGV\n\n");
}
void func(char *buffer)
{
unsigned int start=0;
//to make the starting address in mprotect as page aligned
asm("andl $-4096, %esp");
asm("movl %%esp, %0":"=r"(start));
//raise(SIGSEGV);
printf("Mprotect worked:
%d\n\n",mprotect((void*)start,4096,PROT_READ));
buffer[3]='c';
printf("Mprotect worked:
%d\n\n",mprotect((void*)start,4096,PROT_WRITE|PROT_READ|PROT_EXEC));
}
int main(void)
{
char buffer[10];
if( signal(SIGSEGV, handler)== SIG_ERR )
printf("problem installing new signal handler\n\n");
func(buffer);
printf("into main\n\n");
return 0;
}
My program makes the previous frame as write protected, thus when i am
in func() and i will try to write into the buffer that is allocated in
main(), it will generate SIGSEGV signal that is handled by my handler()
There is something wierd going on that i am not able to understand:
1. If both the mprotect functions are uncommented and i try to write
into the buffer[3]='c', SIGSEGV is generated
handler is called and it starts printing "abhay: caught SIGSEGV"
continuously on the screen until stack overflows. But it should have
printed it once and should have returned back into the func()
2. But if i comment both the mprotect and uncomment "raise(SIGSEGV)" to
generate the SIGSEGV signal explicitly, then this doesn't happen.
I am running the program on RedHat linux 8.0 and using GCC compiler.
Can anyone help me out?
Thanks.