LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 01-02-2009, 02:37 AM   #1
v_sharma
LQ Newbie
 
Registered: Jan 2009
Distribution: Scientific Linux, Fedora
Posts: 4

Rep: Reputation: 0
mmap call return value


Hi all, I am an new member and this is my first post.
Hope I am posting at the correct forum.


I am using SLC 4.5 (Kernel 2.6.9-55.EL.cernsmp) on a 64-bit machine. I am doing programming in GNU C.

The thing that I am trying to do is this...

The C program forks a new process and execs an executable file. The newly forked process allows the parent (C program) to trace it by passing the PTRACE_TRACEME request in a ptrace call. Now the parent has to map a certain memory area into the child's address space. The parent process does this in the following manner:

1. Save the original registers contents of the child.
2. Write 0x80cd (INT 80 instruction) at the stack top and point the eip to this location.
3. Load the registers contents as
rax = __NR_mmap ; //System call No. This stores 0x9 in rax as specified in <asm/unistd.h>
rbx = 0x501000; //Start address of the area to be mapped.
rcx = 0x21000; //Size of the area to be mapped.
rdx = PROT_READ | PROT_WRITE | PROT_EXEC; //Permissions of the area to be mapped
rsi = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS; // flags

During this time rip equals 0xBFFFF028
4. Parent process makes the child execute the current instruction i.e. INT 80 (using PTRACE_SINGLESTEP)
5. The parent restores the original register contents of the child.

But what really is happening is different. After step 4 the register contents are:

rax = 0xFFFFFFF2
rbx = 0x501000;
rcx = 0x21000;
rdx = 0x7 (i.e. same as earlier PROT_READ | PROT_WRITE | PROT_EXEC)
rsi = 0x32 (i.e. same as earlier MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS)

Here rip equal 0xBFFFF02A ( an increment of two bytes i.e. 0x80CD)

After Step 4 i.e. after execution of ptrace(PTRACE_SINGLESTEP,.....) by the parent the errno is 0. So this means that execution of ptrace was successful. The return value of mmap call (which is supposed to be in register rax) is 0xFFFFFFF2 (i.e. -14).

Since the return value is not MAP_FAILED, it should be assumed that mmap call was successful. But since the value is completely outside the address range to be mapped, it is difficult to know what happens during the mmap system call.

Does anybody have any idea that why such a value is being returned in the register rax???
Is it garbage value or some error code?? (although I saw the error codes but there was no macro corresponding to value -14)
Is it related to memory pointers???

And I must tell you that the above thing when implemented on a 32 bit machine with kernel version 2.4 works well (of course, there the registers are eax, ebx etc. in place of rax, rbx....)

What can be the changes in kernel which prevent in from working on a higher kernel version??

Thanks in advance for any help..........
 
Old 01-02-2009, 08:20 PM   #2
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
can you please try to run it on an updated kernel and post results? (>=2.6.27).

it does seem odd, but alot of changes were made since 2.6.9 that might be relevant here./

waiting for your answer.
 
Old 01-02-2009, 11:46 PM   #3
v_sharma
LQ Newbie
 
Registered: Jan 2009
Distribution: Scientific Linux, Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
First of all thanks for the quick reply.

Well, it will be difficult to test on a 2.6.27 kernel version as I am working on a production system and finally the application is to be run on a similar (version 2.6.9) system.

My doubt is that why it could work on 2.4 kernel and not on a 2.6 kernel? Since Linux 2.6 also provides the facility of making system calls using the INT 80 instruction and the calling procedure as well as return value of mmap system call remains same. Although I am not sure whether the actual implementation between the mmap call and return has been modified.

Also, is there any other way by which a parent process can map a private memory area to its child address space in such a way that the start address of this area is specified by the parent and the mapping is not from a file (i.e. page is MAP_ANONYMOUS)???

Hope I have made myself clear.....
 
Old 01-03-2009, 09:03 AM   #4
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
will this help you?
Shell :

# man 2 clone
 
Old 01-05-2009, 12:19 AM   #5
v_sharma
LQ Newbie
 
Registered: Jan 2009
Distribution: Scientific Linux, Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
I think clone cannot be used in this particular scenario because of the following reasons:

1)Clone is mainly used to share execution context between the parent and the child. But in our case, the address space of the child has to be private, it need not (and mostly should not) be shared with the parent.

2) A child created with clone(int (*fn) void*,...) will execute the function fn and then terminates. But in our case, the child has to exec to a particular program and continue execution even if the parent dies.
 
  


Reply



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
gtk_init_check() call fails and return -1 deedhnd Programming 3 10-21-2008 08:09 AM
does a system call return value?? docetes Programming 2 03-16-2006 05:40 PM
ALSA & mmap -- Return to Castle Wolfenstein The Burninator Linux - General 2 02-05-2004 06:47 AM
How can get values return from a system call quenn Programming 1 09-16-2003 11:05 PM
Problem with accept() call which doesn't return jph Linux - General 0 07-28-2003 04:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration