What happens when executing program tries to change its text section ?

 If a process mistakenly attempts to overwrite its text space, it incurs a protection fault that typically results in termination of the process.

For example, the program in shown below assigns the pointer ip to the address of the function f() and then arranges to catch all signals. If the program is compiled so that text and data are in separate regions, the process executing the program incurs a protection fault when it attempts to write the contents of ip, because it is writing write-protected text region. The kernel sends a SIGBUS signal to the process on an AT&T 3820 computer, although other implementations may send other signals. The process catches the signal and exits without executing the print statement in main. However, if the program were compiled so that the program text and data were part of one region (the data region), the kernel would not realize that a process was overwriting the address of the function f. The address off contains the value 1! The process executes the print statement in main but executes an illegal instruction when it calls f. The kernel sends it a SIGILL signal, and the process exits.

#include <signal.h>main(){int i, *ip;extern f(), sigcateh();ip=(int *)f();for (i=0; i < 20; i++)signal (i, sigcatch);*ip=1;printf("after assign to ip\n");f();}f(){}sigeatch(n)int n;{printf("caught sig %d\n", n);exit (1);}

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+