I'm creating an exercise that demonstrates different concepts in buffer overflow. I've encountered the following strange problem - I can't get the argument address. I have the following simple function:
void vuln(char *str) {
char buf[64];
strcpy(buf, str);
dump_stack((void **) buf, 32, (void **) &str);
}
The dump_stack is defined as: void dump_stack(void **stack, size_t n, void **arg0)
When I compile and run the code for x86 using gcc or clang I see the same issue: the compiler creates a stack variable for str, copies the argument to it and &str points to this local stack variable instead of the argument!
The disassembly of the call to vuln looks like:
mov eax, [ebp+argv]
add eax, 4
mov eax, [eax]
mov [esp], eax
call vuln
The disassembly of the vuln function looks as follows:
public vuln
vuln proc near
src= dword ptr -5Ch
dest= byte ptr -4Ch
var_C= dword ptr -0Ch
arg_0= dword ptr 8
push ebp
mov ebp, esp
sub esp, 78h
mov eax, [ebp+arg_0]
mov [ebp+src], eax
mov eax, large gs:14h
mov [ebp+var_C], eax
xor eax, eax
mov eax, [ebp+src]
mov [esp+4], eax ; src
lea eax, [ebp+dest]
mov [esp], eax ; dest
call _strcpy
As one can see the function takes the first argument and stores it in a local variable. So there is no way in my C code to get the address of the argument on the stack.
I tried different optimization settings, but can't get this to compile to what I expect it to!!! What I need is just a way to get the stack address where the first argument is sitting