LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-31-2008, 11:00 AM   #1
Filipe
LQ Newbie
 
Registered: Dec 2005
Posts: 23

Rep: Reputation: 15
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...)

Last edited by Filipe; 10-31-2008 at 11:42 AM.
 
Old 10-31-2008, 01:34 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.
 
Old 11-03-2008, 11:53 AM   #3
Filipe
LQ Newbie
 
Registered: Dec 2005
Posts: 23

Original Poster
Rep: Reputation: 15
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 !! ;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault in assembly code tanoatlq Programming 1 09-19-2008 09:26 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
C++ Program, Segmentation Fault Fireball7 Programming 6 12-07-2005 04:22 PM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 12:47 PM
x86 Assembly - segmentation fault? jrtayloriv Programming 1 07-05-2004 12:52 AM


All times are GMT -5. The time now is 06:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration