I'm starting to tinker with buffer overflows, and wrote the following program:
#include <unistd.h>
void g() {
execve("/bin/sh", NULL, NULL);
}
void f() {
long *return_address;
char instructions[] = "\xb8\x01\x00\x00\x00\xcd\x80"; // exit(1)
return_address = (long*) (&return_address + 2);
*return_address = (long)&g; // or (long)instructions
}
int main() {
f();
}
It does what I expect it to do : return_address overwrite the return address of f with the address of g, which opens a shell. However, if I set the return address to instructions, I got a segmentation fault, and none of the instructions in instructions is executed.
I compile with GCC, using -fno-stack-protector.
How could I prevent this segmentation fault occurring ?
instructionsis saved in the data segment and therefore it trips the Data Execution Prevention (or its name under Linux) and therefore the program is terminated? If I understand correctly, this is not the same thing as a stack overflow, and it isn't disabled by-fno-stack-protector. But I'm not sure at all!instructionsis stored in the stack, I've checked with GDB