Skip to content

Stack memory refresher

The stack stores call state and temporary data. On x86-64 Linux it usually grows toward lower addresses as space is reserved.

Simplified x86-64 stack frameHigher addresses appear above lower addresses. The saved return address and saved frame pointer sit above local variables. The stack pointer is near the lower end of the frame.HIGHER ADDRESSESLOWERADDRESSESCaller data / argumentsSome arguments are passed in registersSaved return addressThe address used when this function returnsSaved frame pointer, if usedLocal buffer and variablesAn unchecked write may pass the buffer boundaryPadding / other localsRSP nearbyreturn slotTeaching sketch only. Exact frames vary by compiler, architecture, and function.
Register Typical x86-64 Linux role
RSP Stack pointer; points to the current top of stack.
RBP Often a frame pointer; optimized code may use it as a general register.
RIP Instruction pointer: the next instruction to execute.
RDI, RSI, RDX, RCX, R8, R9 First integer or pointer arguments under the System V AMD64 ABI.
RAX Common integer return-value register.

That argument-register order is for the common System V ABI on Linux and other Unix-like systems. Windows x64 uses a different convention.

If a function copies more bytes into a local buffer than it holds, later stack data may be overwritten. In a challenge, determine whether the bytes can reach saved control data and whether the protections change what is useful.

  1. Find the input buffer and the operation that writes to it.
  2. Send a cyclic pattern and measure the distance to the saved return value.
  3. Inspect the stack at the return point and confirm which bytes are controlled.
  4. Build a payload using the measured offset and target-appropriate packed addresses.

Each stop shows the current state. After an instruction pushes, pops, reserves, or releases data, inspect the new address instead of relying on an old dump.

x/24gx $rsp # 24 eight-byte values from the current stack pointer
x/64bx $rsp # 64 individual bytes
x/8i $rip # instructions at the current instruction pointer
info registers # fresh register values after a stop or step

With GDB, x/nfu ADDRESS takes a repeat count, format, and unit size. Common units are b (byte), h (2 bytes), w (4 bytes), and g (8 bytes).

  • Alignment: under the System V AMD64 ABI, the stack is 16-byte aligned immediately before a call; at function entry, RSP is 8 modulo 16 because the call pushed a return address.
  • Endianness: x86-64 stores multi-byte integers little-endian. Pack addresses with p64() or flat(); do not type them as text.
  • ASLR and PIE: stack, library, and PIE addresses can change between runs. A value from one run is not proof that it is stable.
  • Canaries: a stack protector may stop the function before it returns. A crash in __stack_chk_fail is not the same as controlling RIP.
  • Input boundaries: newline, NUL bytes, or a length limit can change which payload bytes arrive.

The System V AMD64 ABI documents the calling convention, stack frame, and alignment rules.