83

What is the difference between a stack overflow and a buffer overflow in programming?

3
  • 15
    Interestingly enough, a stack overflow is a special case of buffer overflow. :hmm: Commented Aug 27, 2009 at 2:50
  • 12
    he he .. Stack Overflow reefers to website, Buffer Overflow doesn't ...
    – Xinus
    Commented Dec 30, 2009 at 16:59
  • 1
    @Spencer Ruport Why would this be on Meta?
    – orokusaki
    Commented Feb 5, 2010 at 2:06

10 Answers 10

152

Stack overflow refers specifically to the case when the execution stack grows beyond the memory that is reserved for it. For example, if you call a function which recursively calls itself without termination, you will cause a stack overflow as each function call creates a new stack frame and the stack will eventually consume more memory than is reserved for it.

Buffer overflow refers to any case in which a program writes beyond the end of the memory allocated for any buffer (including on the heap, not just on the stack). For example, if you write past the end of an array allocated from the heap, you've caused a buffer overflow.

3
  • 5
    So would it be fair to say that a stack overflow is a specific type of buffer overflow? (I'm thinking the stack is a portion of memory allocated at run-time) Commented Sep 29, 2012 at 20:36
  • Might as well add a description of a buffer overrun too, since it's closely related.
    – arkon
    Commented May 13, 2013 at 17:26
  • 4
    The term "stack overflow" is often used to describe a buffer overflow that occurs on the stack.
    – Oktalist
    Commented Jul 13, 2013 at 14:17
27

The key difference is knowing the difference between the stack and a buffer.

The stack is the space reserved for the executing program to execute in. When you call a function, it's parameter and return info are placed on the stack.

A buffer is a generic chunck of memory that is used for a single purpose. For example, a string is a buffer. It can be over run by writing more data to the string than was allocated for.

11

Stack overflow: you have put too many things on the stack for the memory allocated to the current thread

Buffer overflow: You have exceeded the size of your currently allocated buffer and have not resized it to fit (or cannot resize it further).

5

A stackoverflow is when the size of the stack for a thread exceeds the maximum allowable stack size for that thread.

A buffer overflow is when a value is written into memory that is not currently allocated by the program.

4

Don't you mean to say "what is the difference between a stack and a buffer?" -- that will lead you to more insight more quickly. Once you've gotten that far, then you can think about what it means to overflow each of these things.

3

Buffer overflow usually stands for anytime a memory buffer is accessed beyond it's bounds whether stack or heap. A stack overflow means the stack has exceed it's allocated limit and on most machines/OS is running over heap.

3

1. Stack-Based Buffer Overflow • Occur when a program writes to a memory address on the program’s call stack outside the intended data structure – fixed length buffer. • Characteristics of stack-based programming 1. “Stack” is a memory space in which automatic variables are allocated. 2. Function parameters are allocated on the stack and are not automatically initialized by the system, so they have garbage until they are initialized. 3. Once a function has completed its cycle, reference to the variable in the stack is removed. (i.e if function is called multiple times, its local variables and parameters are recreated and destroyed each time the function is called and exited.)
• The attacker exploit stack-based buffer overflows to manipulate program in various ways by overwriting
1. A local variable that is near the buffer in memory on the stack to change the behaviour of program that may benefit the attacker.
2. Return address in a stack frame. Once the function returns, execution will resume at the return address as specified by the attacker, usually a user input-filled buffer. 3. A function pointer, or exception handler, which is subsequently executed. • Factors to overcome the exploits are
1. Null bytes in addresses 2. Variability in the location of shell code 3. Differences between environment Shell code is a small piece of code used in exploitation of software vulnerability.

2. Heap Buffer Overflow

• Occurs in the heap data area. • Overflow occurs when an application copies more data into a buffer than the buffer was designed to contain. • Vulnerable to exploitation if it copies data to buffer without first verifying that source will fit into destination. • Characteristics of stack-based and heap-based programming: • “Heap” is a “free store” that is memory space, when dynamic objects are allocated. • The heap is the memory space dynamically allocated new(), malloc(), and calloc() functions. • Dynamically created variables (i.e declared variables) are created on heap before execution and stored in memory until the life cycle of object has completed. • Exploitation is performed • By corrupting data to override internal structures such as linked list pointers. • Pointer exchange to override program function

2

A buffer is a piece of contiguous memory.

The program stack, used to track function calls and returns, is also contiguous, and is technically a buffer.

Buffer overflow happens when you attempt to write large data to a small buffer, the buffer can not hold this much data so it overflows.

Stack overflow happens when you make excessive unreturned calls (common with recursion) that fill up your stack and hence, it also overflows.

0

Most people who mentions buffer overflows mean stack oveflows. However, overflows can occur in any area not just limited to the stack. Such as the heap or bss. A stack overflow is limited to overwriting return addresses on the stack, but a normal overflow that does not overwrite the return address will probably just overwrite other local variables.

1
  • Don't local variables live on the stack too usually?
    – Flexo
    Commented Sep 24, 2011 at 23:26
0

Let me explain in a simpler way with a diagram of RAM. Before jumping into it, I suggest reading about StackFrame, Heap Memory.

As you can see, the Stack grows downwards (showed by arrow) assuming it's stack. The kernel code, text, data all are static data, so they are fixed. The heap portion being dynamic grows upwards (showed by arrow).

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.