Buffer overflow

anomaly in computer security and programming that could be exploited as a security vulnerability

A buffer overflow is a kind of vulnerability of software. It occurs when a program writes data into a buffer that is longer than the buffer's allocated size. Part of the data will then be written into an area which is not part of the buffer. Because the buffer overflow changes other data as well, this can cause a program to malfunction. Buffer overflows are commonly used as a security exploit. When planned properly, it is possible to write data into memory, which will then be executed as a program, possibly with a different privilege level. This is known as privilege escalation. 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 Morris worm of 1988 used this.

Basic example: Two variables, one a character array (string), the other a number. Writing more characters than the array's length changes the value of the other variable.

There are certain programming languages, such as C or C++ that do not protect well against accessing or overwriting data in any part of memory. They also do not check the that the data written into an array is not larger than the array. Bounds checking can prevent buffer overflows, but it needs more code, and processing time.

Modern operating systems protect themselves against buffer overflows. One technique is to use a random location in memory, by leaving some spacce between buffers, and by looking ofr actions that write to those areas.


The so-called off-by-one error can also cause a buffer overflow. Suppose an array has a size of 50 elements. The first element is at position 0, the second at position one, etc. The last one is at position 49. Using position 50 will cause a buffer overflow.


Buffer overflows cause the following problems:

  • Data corruption.Overflowing data can overwrite and corrupt important program data, causing errors, crashes, or unintended behavior.
  • Code Execution: In some cases, the overflowed data may contain malicious instructions that an attacker can manipulate to execute arbitrary code within the program's context. This can give attackers unauthorized access to the system or the ability to run malicious software.
  • Denial of Service (DoS): If a buffer overflow occurs in a critical system component, it can lead to a crash or unresponsive behavior, causing a DoS attack and rendering the system unavailable to legitimate users.
  • Privilege Escalation: Attackers can use buffer overflows to elevate their privileges within a system, gaining access to sensitive resources or administrative privileges they should not have.

Buffer overflows are a serious security concern and have been used to exploit vulnerabilities in various software applications, including operating systems, web servers, and network protocols. To prevent buffer overflows, developers need to implement secure coding practices, such as bounds checking and input validation, and use memory-safe programming languages or libraries that provide built-in protections against this type of vulnerability. Additionally, regular security audits and updates are essential to identify and patch potential buffer overflow vulnerabilities in software.