#include #include #include int main(){ /* main is a function defined in the code segment of the program, and we * can't change code segment memory on modern operating systems. It used * to be allowed, so that it would be possible to write programs that modify * themselves! Nowadays, if you want to do that, you have to keep the * modified code on the stack or heap instead of in the code segment. That * presents a barrier to hacking, especially when combined with a * non-executable stack. At any rate, the when the next two lines attempt * to modify the memory location where main is located, the operating system * will send us a signal, SIGSEGV (segment violation) instead of carrying out * the modification. * * The separate variable and cast is to keep the compiler from catching our * attempt early. This wouldn't compile: * main = 10; * It might compile if we left the type alone: * int (*ref)() = main; * ref = (void*)10; * There are quite a few options to cause a segfault this way, but they are * generally two lines. */ int* i = (int*)main; *i = 10; return 0; }