LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C program calling assembly - results in segmentation fault (https://www.linuxquestions.org/questions/programming-9/c-program-calling-assembly-results-in-segmentation-fault-680300/)

Filipe 10-31-2008 11:00 AM

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...)

Sergei Steshenko 10-31-2008 01:34 PM

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.

Filipe 11-03-2008 11:53 AM

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.