Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does function stack save the return address?

I'm learning things about function stack based on assembly using the system Linux x86.

I've read some article, which told me that a function stack (callee) would save the return address where it is called by the caller so that the computer could know the point where to continue when the function returns.

This is why there is a kind of attack: stack smashing. The stack smashing means that if we can overflow a function stack, especially overflow the return address with a designed address, the program will execute the codes of hackers.

However, today I'm trying to use gdb to check a simple c++ program as below but I can't find any saved return address in any function stack. Here is the code:

void func(int x)
{
    int a = x;
    int b = 0;  // set a breakpoint
}

int main()
{
    func(10);  // set a breakpoint
    return 0;
}

Then I use gdb to get its assembly:

main:

enter image description here

func:

enter image description here

Now we can see that there is no return address being saved in the two function stacks (at least this is my view).

If a hacker wants to hack this program with stack smashing, which address in the function stack will be edited illegally by him?

like image 903
Yves Avatar asked Dec 19 '17 16:12

Yves


People also ask

Is the return address stored on the stack?

The function return address is placed on the stack by the x86 CALL instruction, which stores the current value of the EIP register. Then, the frame pointer that is the previous value of the EBP register is placed on the stack.

What happens to the stack when a function returns?

At function return, the stack pointer is instead restored to the frame pointer, the value of the stack pointer just before the function was called. Each stack frame contains a stack pointer to the top of the frame immediately below.

In which register is the return address saved?

A link register (LR for short) is a register which holds the address to return to when a subroutine call completes.

What is return address and where is it stored?

In computer programming, the return address is the location directly after where a subroutine is called. When a return statement is called in a subroutine or the subroutine completes the program goes to the return address and continues running the program.


1 Answers

Yes there is. Examine the stack immediately before the callq and immediately after it. You will find that the address of the instruction following the callq now appears on the top of the stack and RSP has decremented by 8.

The callq instruction causes the address of the following instruction to be pushed onto the stack. Inversely, the retq instruction at the end of the function causes the address on the stack to be popped into the RIP.

like image 86
David Hoelzer Avatar answered Sep 24 '22 15:09

David Hoelzer