Search code examples
cgccstack-overflowbuffer-overflow

How is temp data stored on the stack frame


Int test(){
    char buff[10]

    printf("Enter text: ");

    gets(buff);

    puts(buff);
}

I don't know how to phrase this question but I'm trying to understand how values are stored on on the buffer allocated for local variables on the stack frame.

---------------
Return Address
---------------
old ebp
---------------
local variables
--------------- 

Is each block 4 bytes and divided in to 4 1 bytes?

------------------
 00 | 40 | 06 | 06
------------------
 ff | ff | de | 70 
------------------

Through gdb I saw the buffer allocated for test was 0x10

I entered sssssssss:

x/x $rbp = 0xffffde70
x/x $rbp + 8 = 0x00400606
x/s $rbp - 16 = "sssssssss"
x/s $rbp - 8 = "\ns"
x/s $rbp - 4 = ""

So how are the characters(in hex) that I entered stored? like how many on each block. It's a 64 system.

 -----------------
 00 | 40 | 06 | 06
------------------
 ff | ff | de | 70 
------------------
    |    |    | 
------------------
    |    |    | s
------------------
 s  | s  | s  | s
------------------
 s  | s  | s  | s 
------------------

Solution

  • From a strict C point of view we don't know.

    The standard doesn't specify such things. The standard doesn't even mention the concept of a stack. From a standard point of view the code is executed on an abstract machine (i.e. no description of how the machine does it. Only what the machine must do).

    So how it is done depends on the specific implementation and it (may) differ from system to system.

    You need to find the ABI document for the system you are using. The ABI document will describe how it is done on your system.

    See en.wikipedia.org/wiki/Application_binary_interface

    Maybe read Where is the x86-64 System V ABI documented?