C program calling assembly - results in segmentation fault
I'm trying some examples from a book and I just keep getting seg faults.
Below is the smallest possible program that still exhibits this behaviour. The full program is written as a means to 'inject' assembly code into a running program and would, among other things, malloc enough memory, read the 'code' from a file ,then point the function pointer fptr to it and call it. The hardcoded bytes in this example are the equivalent of the following assembly lines: xor eax,eax mov al,1 int 0x80 which basically translates to exit() in C. This is the program: #include <stdio.h> int main(int argc,char **argv) { void*code="\x66\x31\xc0\xb0\x01\xcd\x80"; void(*fptr)(void); printf("Calling code...\n"); fptr=(void(*)(void))code; (*fptr)(); return 0; } And this is the output: Calling code... Segmentation fault I'm doing this on a Fedora 8 (2.6.23) box The Last thing i did was /sbin/sysctl -w kernel.exec-shield=0 /sbin/sysctl -w kernel.randomize_va_space=0 or echo 0 > /proc/sys/kernel/randomize_va_space echo 0 > /proc/sys/kernel/exec-shield echo 0 > /proc/sys/kernel/exec-shield-randomize (same segmentation fault...) |
gcc documentation explains how to call assembly pieces from "C"; you are trying to execute code from constant data (that "\x66\x31\xc0\xb0\x01\xcd\x80" piece of your code), so, I guess, it's a good thing the OS doesn't let you do it, i.e. doesn't
let you execute code from a non-code segment. |
It is working now...
i didnīt execute this: echo 0 > /proc/sys/kernel/exec-shield in the box where i was working on... (sory,sory) let me say that: it's a good thing the OS DOES let me do what i want !! ;-) |
All times are GMT -5. The time now is 10:52 PM. |