ArticleslgStudy

computer science

Buffer overflow

Buffer overflow is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Buffer overflow rather than just read about it. In short: In programming and information security, a buffer overflow or buffer overrun is a bug whereby a program writes data to a buffer beyond the buffer's allocated memory, overwriting adjacent memory locations. Buffers are areas of memory set aside to hold data, often while moving it from one section of a program to another, or between programs.

Buffer overflow — main illustration
Buffer overflow — illustration

Key takeaways

  • Buffer overflow belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Buffer overflow to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Buffer overflow from memory before moving on to harder problems.

Reference excerpt

In programming and information security, a buffer overflow or buffer overrun is a bug whereby a program writes data to a buffer beyond the buffer's allocated memory, overwriting adjacent memory locations. Buffers are areas of memory set aside to hold data, often while moving it from one section of a program to another, or between programs. Buffer overflows can often be triggered by malformed inputs; if one assumes all inputs will be smaller than a certain size and the buffer is created to be that size, then an anomalous transaction that produces more data could cause it to write past the end of the buffer. If this overwrites adjacent data or executable code, this may result in erratic program behavior, including memory access errors, incorrect results, and crashes. Exploiting the behavior of a buffer overflow is a well-known security exploit. Files specifically created to exploit buffer overflow vulnerabilities are often called 'crafted' files. On many systems, the memory layout of a program, or the system as a whole, is well defined. By sending in data designed to cause a buffer overflow, it is possible to write into areas known to hold executable code and replace it with malicious code, or to selectively overwrite data pertaining to the program's state, therefore causing behavior that was not intended by the original programmer. Buffers are widespread in operating system (OS) code, so it is possible to make attacks that perform privilege escalation and gain unlimited access to the computer's resources. The famed Morris worm in 1988 used this as one of its attack techniques. Programming languages commonly associated with buffer overflows include C and C++, which provide no built-in protection against accessing or overwriting data in any part of memory and do not automatically check that data written to an array (the built-in buffer type) is within the boundaries of that array. Bounds checking can prevent buffer overflows, but requires additional code and processing time. Modern operating systems use a variety of techniques to combat malicious buffer overflows, notably by randomizing the layout of memory, or deliberately leaving space between buffers and looking for actions that write into those areas ("canaries").

Technical description A buffer overflow occurs when data written to a buffer also corrupts data values in memory addresses adjacent to the destination buffer due to insufficient bounds checking. This can occur when copying data from one buffer to another without first checking that the data fits within the destination buffer.

Example

In the following example expressed in C, a program has two variables which are adjacent in memory: an 8-byte-long string buffer, a, and a two-byte big-endian integer, b.

Initially, a contains nothing but zero bytes, and b contains the number 1979.

Now, the program attempts to store the null-terminated string "excessive" with ASCII encoding in the A buffer.

"excessive" is 9 characters long and encodes to 10 bytes including the null terminator, but a can take only 8 bytes. By failing to check the length of the string, it also overwrites the value of b:

b's value has now been inadvertently replaced by a number formed from part of the character string. In this example "e" followed by a zero byte would become 25856. Writing data past the end of allocated memory can sometimes be detected by the operating system to generate a segmentation fault error that terminates the process. To prevent the buffer overflow from happening in this example, the call to strcpy could be replaced with strlcpy, which takes the maximum capacity of a (including a null-termination character) as an additional parameter and ensures that no more than this amount of data is written to a:

When available, the strlcpy library function is preferred over strncpy which does not null-terminate the destination buffer if the source string's length is greater than or equal to the size of the buffer (the third argument passed to the function). Therefore a may not be null-terminated and cannot be treated as a valid C-style string.

Exploitation The techniques to exploit a buffer overflow vulnerability vary by architecture, operating system, and memory region. For example, exploitation on the heap (used for dynamically allocated memory), differs markedly from exploitation on the call stack. In general, heap exploitation depends on the heap manager used on the target system, while stack exploitation depends on the calling convention used by the architecture and compiler.

Stack-based exploitation

There are several ways in which one can manipulate a program by exploiting stack-based buffer overflows:

Changing program behavior by overwriting a local variable located near the vulnerable buffer on the stack; By overwriting the return address in a stack frame to point to code selected by the attacker, usually called the shellcode. Once the function returns, execution will resume at the attacker's shellcode; By overwriting a function pointer or exception handler to point to the shellcode, which is subsequently executed; By overwriting a local variable (or pointer) of a different stack frame, which will later be used by the function that owns that frame. The attacker designs data to cause one of these exploits, then places this data in a buffer supplied to users by the vulnerable code. If the address of the user-supplied data used to affect the stack buffer overflow is unpredictable, exploiting a stack buffer overflow to cause remote code execution becomes much more difficult. One technique that can be used to exploit such a buffer overflow is called "trampolining". Here, an attacker will find a pointer to the vulnerable stack buffer and compute the location of their shellcode relative to that pointer. The attacker will then use the overwrite to jump to an instruction already in memory which will make a second jump, this time relative to the pointer. That second jump will branch execution into the shellcode. Suitable instructions are often present in large code. The Metasploit Project, for example, maintains a database of suitable opcodes, though it lists only those found in the Windows operating system.

Heap-based exploitation

… excerpt ends here. Continue reading the full article.

Illustrations

Buffer overflow: Visualization of a software buffer overflow. Data is written into A, but is too large to fit within A, so it overflows into B.
Visualization of a software buffer overflow. Data is written into A, but is too large to fit within A, so it overflows into B.
Buffer overflow: Illustration of a NOP-sled payload on the stack.
Illustration of a NOP-sled payload on the stack.
Buffer overflow: An instruction from ntdll.dll to call the DbgPrint() routine contains the i386 machine opcode for jmp esp.
An instruction from ntdll.dll to call the DbgPrint() routine contains the i386 machine opcode for jmp esp.

Worked examples

Example 1 — a first encounter with Buffer overflow

Start with the simplest possible case. Write down what Buffer overflow claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Buffer overflow before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Buffer overflow ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Buffer overflow

In research
Buffer overflow appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Buffer overflow in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Buffer overflow is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer memory, Computer security exploits, Software bugs, so understanding it makes those chapters shorter.
In everyday life
Look for Buffer overflow outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Buffer overflow” →

How to study Buffer overflow in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Buffer overflow means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Buffer overflow out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Buffer overflow in simple terms?

In programming and information security, a buffer overflow or buffer overrun is a bug whereby a program writes data to a buffer beyond the buffer's allocated memory, overwriting adjacent memory locations. Buffers are areas of memory set aside to hold data, often while moving it from one section of…

Why does Buffer overflow matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Buffer overflow?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Buffer overflow.

Tags

  • Computer memory
  • Computer security exploits
  • Software bugs

Keep exploring