C
Chris Van Extergem
Hi all,
I'm just trying some examples from a book I'm reading and I just keep
getting these 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
assemly lines:
xoreax,eax
moval,1
int0x80
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 pretty sure the problem lies with the way fptr is declared/assigned a
value/called but I don't have any
experience with this particular use of function pointers so I would
appreciate your input.
Oh, and I don't know if this is relevant, I'm doing this on a RedHat 9 box.
I'm just trying some examples from a book I'm reading and I just keep
getting these 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
assemly lines:
xoreax,eax
moval,1
int0x80
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 pretty sure the problem lies with the way fptr is declared/assigned a
value/called but I don't have any
experience with this particular use of function pointers so I would
appreciate your input.
Oh, and I don't know if this is relevant, I'm doing this on a RedHat 9 box.