943,841 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1735
  • C RSS
Jul 31st, 2009
0

Getting the return address

Expand Post »
Hello all.
I want to find the return address where execution should jump after returning from a function.
Say this is my code:
  1. void foo(){
  2. }
  3. int main(){
  4. foo();
  5. printf("After foo..");
  6. return 0;
  7. }

After foo() returns, how to find the rturn address where control will be passed?

Thank you....
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Jul 31st, 2009
0

Re: Getting the return address

First, this is HIGHLY dependent on your current implementation. Change anything at all, and it might break.
It also means you need to tell us exactly what you have if you ever hope of a useful answer.

Second, why do you need to know, and what are you going to use the answer for (there's probably a better way).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 31st, 2009
0

Re: Getting the return address

Alright then.
I m trying to smash the stack.
I am trying to modify the return address so that i divert the execution elsewhere.
This is my code :
  1. #include<stdio.h>
  2.  
  3. void function() {
  4. char buffer1[5];
  5. buffer1[0] = 'Z';
  6. char *ret = &buffer1[0];
  7. printf("\nbuffer1 is pointing at : %p", &buffer1);
  8. printf("\nret is pointing at address : %p and contains: %c",ret, *ret);
  9. ret = buffer1 + 8;//point to where return address is stored..hope this is correct
  10. printf("\nret is now pointing at address: %p",ret);
  11.  
  12. (*ret) += 8;//modify return address
  13.  
  14. }
  15.  
  16. int main() {
  17. int x;
  18. x = 0;
  19. function();
  20. x = 5;//we are skipping the execution of this assignment....
  21. printf("\nx = %d\n",x);//shud print 0
  22. return 0;
  23. }

heres the op:
  1. buffer1 is pointing at : 0xbf85bd4f
  2. ret is pointing at address : 0xbf85bd4f and contains: Z
  3. ret is now pointing at address: 0xbf85bd57
  4. x = 5
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Jul 31st, 2009
0

Re: Getting the return address

oh! and i am usng gcc on ubuntu
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Jul 31st, 2009
0

Re: Getting the return address

Good luck with that
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 3rd, 2009
0

Re: Getting the return address

You will have to know how a stack frame is implemented on your system. I don't know if it is the same for all (or most) x86 systems, but from looking at this article on wikipedia it looks like it could be this:

  1. +==========================+-+
  2. | locals for function() | |
  3. +==========================+ |
  4. | return address | --> stack frame for function()
  5. +==========================+ |
  6. | parameters for function | |
  7. +==========================+-+

I did some toying around with C, and came up with this:

  1. #include<stdio.h>
  2.  
  3. void somefunct();
  4. void anotherfunct();
  5.  
  6. int main(){
  7.  
  8. somefunct();
  9.  
  10. printf("returned to main!\n");
  11.  
  12. return 0;
  13.  
  14. }
  15.  
  16. void somefunct(){
  17.  
  18. int *ptr = (int*)&ptr + 2;
  19. *ptr = (int)&anotherfunct;
  20.  
  21. }
  22.  
  23. void anotherfunct(){
  24.  
  25. printf("I win!\n\n");
  26.  
  27. }

this prints the output:

  1. ~/c/t $ ./retaddr
  2. I win!
  3.  
  4. Segmentation Fault
  5. ~/c/t $

The segmentation fault is because the stack is set up to return to main, and then return to the system once main completes. But since the return address is bashed to returned to yet another function, that function get's main's call stack. I have no idea what this does, but I'm assuming it is bad.

I had to play with the magic number (which turned out to be 2) for a bit, but I'm not exactly sure about why 2. I could play with it more and find out just how the stack frame looks, but I'll leave that up to you.

For fun, compile my code on your machine and see if you get identical output. Try it on a windows box. The behavior of this sort of thing depends heavily on the compiler and system.

I would play with this more but it's 2AM and time to get some rest.
Last edited by winrawr; Aug 3rd, 2009 at 5:16 am.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Aug 3rd, 2009
0

Re: Getting the return address

Check out the <csetjmp> library.

Don't mess with the stack. Otherwise you invite death.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Sep 28th, 2010
0
Re: Getting the return address
Your function should be like this

  1. void function() {
  2. int a;
  3. *(&a+2)+=0x07;
  4. }
Last edited by arajak; Sep 28th, 2010 at 4:50 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arajak is offline Offline
1 posts
since Sep 2010
Sep 29th, 2010
0
Re: Getting the return address
Yes I know the thread is almost a year old but this seemed like an interesting questions and there was no follow up

@winrawr I ran your code in a Windows machine and it worked. No seg fault no nothing
Reputation Points: 114
Solved Threads: 104
Master Poster
abhimanipal is offline Offline
736 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Shift-Add vs Multiplication // Bit-Wise vs. Logical Operators
Next Thread in C Forum Timeline: String to function name





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC