|
| 1 | +# Functions and the Stack |
| 2 | + |
| 3 | +Every function has two classes of values, usually stored on stack, extremely important for its well-being: |
| 4 | + |
| 5 | +1. the return address |
| 6 | + |
| 7 | +1. the parameters / arguments |
| 8 | + |
| 9 | +Meddling with these might get you to a big fat **SEGFAULT** or to great power. |
| 10 | + |
| 11 | +## `ebp`, the Stack Frame |
| 12 | + |
| 13 | +But before discussing that, we have to bring light to another obscure register, `ebp`. |
| 14 | +We kind of used it before, in our journey, as it has a great advantage. |
| 15 | +It stores the stack pointer value right before the stack begins to hold local variables and preserved register values. |
| 16 | +In other words, it keeps a pointer to the stack at the beginning of the function, enabling us to actually move freely through the stack. |
| 17 | +We will, now, refer to values stored on it, even though they are not the last ones. |
| 18 | + |
| 19 | +```asm |
| 20 | +push ebp |
| 21 | +mov ebp, esp |
| 22 | +
|
| 23 | +push dword 3 |
| 24 | +push dword 4 |
| 25 | +push dword 5 |
| 26 | +
|
| 27 | +; at this point esp decreased its value with 3 * 4 = 12 bytes |
| 28 | +; traditionally we can access the last value only, |
| 29 | +; however the stack is like an array, so we will use the pointers |
| 30 | +; it offers us |
| 31 | +
|
| 32 | +mov eax, [esp + 8] ; eax = 3 |
| 33 | +mov eax, [ebp - 4] ; eax = 3 |
| 34 | +``` |
| 35 | + |
| 36 | +## The Return Address |
| 37 | + |
| 38 | +The return address of a function is one of the **most targeted** piece of information in an attack. |
| 39 | +There is even a special class of attacks that takes its name from it, [ROP](https://security-summer-school.github.io/binary/return-oriented-programming/) (Return Oriented Programming). |
| 40 | +Moreover, the return address can also be defined as a **code pointer**, a pointer that stores the address of an instruction. |
| 41 | +Remember how the instructions were stored in the code or text section, hence the **code pointer** label. |
| 42 | + |
| 43 | +The reason for this kind of popularity is obvious: it represents one of the rare instances when the program **performs a jump to a code pointer saved on stack**, which, combined with the stupidity or the laziness of the programmer, can result in a nasty backdoor to the system. |
| 44 | + |
| 45 | +The address at which the return address is usually stored on x86 systems is `[ebp + 4]`. |
| 46 | + |
| 47 | +## The Parameters |
| 48 | + |
| 49 | +The parameters follow a similar story to that of the return address, with a slight modification, though. |
| 50 | +On 64-bit x86 they are placed in special registers, if possible. |
| 51 | +If the number of parameters is high, they would get transmitted using the stack, just as it happens, on 32-bit x86. |
| 52 | + |
| 53 | +The address at which the first parameter gets stored on 32-bit x86 systems is `ebp + 8`. |
| 54 | + |
| 55 | +The address at which the second parameter gets stored on 32-bit x86 systems is `ebp + 12`. |
| 56 | + |
| 57 | +The address at which the third parameter gets stored on 32-bit x86 systems is `ebp + 16`. |
| 58 | + |
| 59 | +And so on. |
| 60 | + |
| 61 | + |
0 commit comments