473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function pointer results in segmentation fault.

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.

--
Chris - eXtc - Van Extergem
May 1 '06 #1
5 3490

Chris Van Extergem wrote:
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.
If you are using the same system and compiler as the book then you
should probably ask in a newsgroup relevant to that compiler.
Otherwise you shouldn't expect it to work.

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;
}
It might be helpful for you to look at assembly code produced by your
compiler when you actually call a void function with no arguments. It
could be that the calling is more complicated that your book suggests.

Oh, and I don't know if this is relevant, I'm doing this on a RedHat 9 box.


If it is relevant then the question is off-topic in this group.

-thomas

May 1 '06 #2
On Mon, 1 May 2006 14:15:11 UTC, "Chris Van Extergem"
<ex*****@yahoo.com> wrote:
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.


Ask in a linux group. This has nothing to do with standard C.

Hint: The OS is catiching you to call code you not allow to execute.
To learn to write a virus you have to learn something more than to
copy illegal code from a book you've found somewhere.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
May 1 '06 #3

"Chris Van Extergem" <ex*****@yahoo.com> wrote in message
news:op***************@frodo.telenet.be...
Hi all,

I'm just trying some examples from a book I'm reading and I just keep
getting these seg faults.
<snip>
xoreax,eax
moval,1
int0x80


This question comes up in a number of assembly language groups. I'm not
sure if the following is the correct answer. You said you're using Linux
and not FreeBSD, so your 'int 0x80' kernel interface may have been replaced
by a 'syscall' wrapper. The first suggests you can use 'call 0xFFFFF000'
instead of 'int 0x80'.

http://lkml.org/lkml/2002/12/17/5
http://lkml.org/lkml/2002/12/18/218
Rod Pemberton
May 1 '06 #4
"Herbert Rosenau" <os****@pc-rosenau.de> writes:
On Mon, 1 May 2006 14:15:11 UTC, "Chris Van Extergem"
<ex*****@yahoo.com> wrote:
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.
Ask in a linux group. This has nothing to do with standard C.


Agreed.
Hint: The OS is catiching you to call code you not allow to execute.
Perhaps.

The original code declares a void* pointer to what is supposed to be
some machine code, then converts the void* to a pointer-to-function.
This assumes that such a conversion is meaningful *and* that an actual
function pointer simply points to the first byte of the function's
code. That may or may not be the case.

That's just one of a number of reasons why this might not work.

Someone suggested looking at the code generated by your compiler for a
function call. Instead, it would be more useful to look at the code
generated for an *indirect* call (a call through a function pointer
object.
To learn to write a virus you have to learn something more than to
copy illegal code from a book you've found somewhere.


I'm sure that writing a virus is one possible use for this technique.
I see no basis for assuming that that's what the OP is trying to do.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 2 '06 #5
On Mon, 01 May 2006 17:02:36 +0200, Thomas Lumley <th****@drizzle.net>
wrote:

[problem with C program calling assembly]
It might be helpful for you to look at assembly code produced by your
compiler when you actually call a void function with no arguments. It
could be that the calling is more complicated that your book suggests.


I did that and as it turns out the problem is not in the C program,
apparently the string \x66\x31\xc0 translates to xor ax,ax instead of xor
eax,eax
How this happened is something I am trying to figure out right now but I
guess that's a question better asked in one of the assembly groups...

Anyway, thanks to you and all the others for your help.

--
Chris - eXtc - Van Extergem
May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: E G | last post by:
Hi, I have a class similar to this: class Matrix { private: float **_A; unsigned _rows,_cols; public:
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
25
by: No Such Luck | last post by:
Hi all: Below are two pieces of code that basically perform the same task. Version A produces a segmentation fault, while version B works correctly. I understand why version B works correctly,...
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
10
by: paytam | last post by:
hi all can you tell me what's the wrong with this code? I use gcc compiler,but when I wanted to use gets() function in my code but it takes a dangerous warning(the gets function is dangerous...
25
by: dis_is_eagle | last post by:
Hi.I have a question on the following statement. char* a="hello"; The question is where "hello" gets stored.Is it in some static area ,stack or heap.I have observed that attempting to modify...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
10
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.