Lecture 10: Stack, Buffer Overflow
» Lecture video (Brown ID required)
» Lecture code
» Post-Lecture Quiz (due 11:59pm Sunday, March 6).
Calling Convention, continued
Functions with more than six arguments
There is a limited number of registers in the x86-64 architecture, and you can write functions in C that
take any number of arguments! The calling convention says that the first six arguments max be passed in registers,
but that the 7th and above arguments are always passed in memory on the stack. Specifically, these
arguments go into the caller's stack frame, so they are stored above the entry %rsp
at the point where the function is called (see call05.{c,s}
and call06.{c,s}
).
The Stack
You will recall the stack segment of memory from earlier lectures: it is where all variables with automatic lifetime are stored. These include local variables declared inside functions, but importantly also function arguments.
The arguments and local variables of f()
live inside f()
's stack frame. Subsequent
arguments (second, third, fourth, etc.) are stored at subsequently lower addresses below %rsp
(see call02.s
and call03.s
for examples with more arguments), followed eventually by
any local variables in the caller.
How does%rsp
change?
The convention is that
%rsp
always points to the lowest (leftmost) stack address that is currently used. This means that when a function declares a new local variable,%rsp
has to move down (left) and if a function returns,%rsp
has to move up (right) and back to where it was when the function was originally called.Moving
%rsp
happens in two ways: explicit modification via arithmetic instructions, and implicit modification as a side effect of special instructions. The former happens when the compiler knows exactly how many bytes a function requires%rsp
to move by, and involves instructions likesubq $0x10, %rsp
, which moves the stack pointer down by 16 bytes. The latter, side-effect modification happens when instructionpush
andpop
run. These instructions write the contents of a register onto the stack memory immediately to the left of the current%rsp
and also modify%rsp
to point to the beginning of this new data. For example,pushq %rax
would write the 8 bytes from register%rax
at address%rsp - 8
and set%rsp
to that address; it is equivalent tomovq %rax, -8(%rsp); subq $8, %rsp
orsubq $8, %rsp; movq %rax, (%rsp)
.
Return Address
As a function executes, it eventually reaches a ret
instruction in its assembly. The effect of
ret
is to return to the caller (a form a control flow, as the next instruction needs to change).
But how does the processor know what instruction to execute next, and what to set %rip
to?
It turns out that the stack plays a role here, too. In a nutshell, each function call stores the return address as the very first (i.e., rightmost) data in the callee's stack frame. (If the function called takes more than six arguments, the return address is to the left of the 7th argument in the caller's stack frame.)
The stored return address makes it possible for each function to know exactly where to continue execution once it returns to its caller. (However, storing the return address on the stack also has some dangerous consequences, as we will see shortly.)
We can now define the full function entry and exit sequence. Both the caller and the callee have responsibilities in this sequence.
To prepare for a function call, the caller performs the following tasks:
The caller stores the first six arguments in the corresponding registers.
If the callee takes more than six arguments, or if some of its arguments are large, the caller must store the surplus arguments on its stack frame (in increasing order). The 7th argument must be stored at
(%rsp)
(that is, the top of the stack) when the caller executes itscallq
instruction.The caller saves any caller-saved registers (see last lecture's list). These are registers whose values the callee might overwrite, but which the caller needs to retain for later use.
The caller executes
callq FUNCTION
. This has an effect likepushq $NEXT_INSTRUCTION; jmp FUNCTION
(or, equivalently,subq $8, %rsp; movq $NEXT_INSTRUCTION, (%rsp); jmp FUNCTION
), whereNEXT_INSTRUCTION
is the address of the instruction immediately followingcallq
.
To return from a function, the callee does the following:
The callee places its return value in
%rax
.The callee restores the stack pointer to its value at entry ("entry
%rsp
"), if necessary.The callee executes the
retq
instruction. This has an effect likepopq %rip
, which removes the return address from the stack and jumps to that address (because the instruction writes it into the special%rip
register).Finally, the caller then cleans up any space it prepared for arguments and restores caller-saved registers if necessary.
Base Pointers and Buffer Overflow
Base Pointers and the %rbp
Register
Keeping track of the entry %rsp
can be tricky with more complex functions that allocate lots of
local variables and modify the stack in complex ways. For these cases, the x86-64 Linux calling convention allows
for the use of another register, %rbp
as a special-purpose register.
%rbp
holds the address of the base of the current stack frame: that is, the address of
the rightmost (highest) address that points to a value still part of the current stack frame. This corresponds the
rightmost address of an object in the callee's stack, and to the first address that isn't part of an argument to
the callee or one of its local variables. It is called the base pointer, since the address
points at the "base" of the callee's stack frame (if %rsp
points to the "top",
%rbp
points to the "base" (= bottom). The %rbp
register maintains this value for
the whole execution of the function (i.e., the function may not overwrite the value in that register), even as
%rsp
changes.
This scheme has the advantage that when the function exits, it can restore its original entry %rsp
by loading it from %rbp
. In addition, it also facilitates debugging because each function stores the
old value of %rbp
to the stack at its point of entry. The 8 bytes holding the caller's
%rbp
are the very first thing stored inside the callee's stack frame, and they are right below the
return address in the caller's stack frame. This mean that the saved %rbp
s form a chain that allows
each function to locate the base of its caller's stack frame, where it will find the %rbp
of the
"grand-caller's" stack frame, etc. The backtraces you see in GDB and in Address Sanitizer error messages
are generated precisely using this chain!
Therefore, with a base pointer, the function entry sequence becomes:
The first instruction executed by the callee on function entry is
pushq %rbp
. This saves the caller's value for%rbp
into the callee's stack. (Since%rbp
is callee-saved, the callee is responsible for saving it.)The second instruction is
movq %rsp, %rbp
. This saves the current stack pointer in%rbp
(so%rbp
= entry%rsp
- 8).This adjusted value of
%rbp
is the callee's "frame pointer" or base pointer. The callee will not change this value until it returns. The frame pointer provides a stable reference point for local variables and caller arguments. (Complex functions may need a stable reference point because they reserve varying amounts of space.)Note, also, that the value stored at
(%rbp)
is the caller's%rbp
, and the value stored at8(%rbp)
is the return address. This information can be used to trace backwards by debuggers (a process called "stack unwinding").The function ends with
movq %rbp, %rsp; popq %rbp; retq
, or, equivalently,leave; retq
. This sequence is the last thing the callee does, and it restores the caller's%rbp
and entry%rsp
before returning.
You can find an example of this in call07.s
. Lab 3 also uses the %rbp
-based calling
convention, so make sure you keep the extra 8 bytes for storing the caller's %rbp
on the stack in mind!
Buffer overflow attacks
Now that we understand the calling convention and the stack, let's take a step back and think of some of the consequences of this well-defined memory layout. While a callee is not supposed to access its caller's stack frame (unless it's explicitly passed a pointer to an object within it), there is no principled mechanism in the x86-64 architecture that prevents such access.
In particular, if you can guess the address of a variable on the stack (either a local within the current function or a local/argument in a caller of the current function), your program can just write data to that address and overwrite whatever is there.
This can happen accidentally (due to bugs), but it becomes a much bigger problem if done deliberately by malicious actors: a user might provide input that causes a program to overwrite important data on the stack. This kind of attack is called a buffer overflow attack.
Summary
Today, we also understood in more detail how the stack segment of memory is structured and managed, and discussed how it grows and shrinks. We learned about how the compiler manages the stack pointer and how base pointers help it "unwind" the stack for debugging.
We then looked into how the very well-defined memory layout of the stack can become a danger if a program is compromised through a malicious input: by carefully crafting inputs that overwrite part of the stack memory via a buffer overflow, we can change important data and cause a program to execute arbitrary code.
In Lab 3, you will craft and execute buffer overflow attacks on a program yourself!