Various techniques have been used to detect or prevent buffer overflows, with various tradeoffs. The following sections describe the choices and implementations available.
Choice of programming language Assembly,
C, and
C++ are popular programming languages that are vulnerable to buffer overflow in part because they allow direct access to memory and are not
strongly typed. C provides no built-in protection against accessing or overwriting data in any part of memory. More specifically, it does not check that data written to a buffer is within the boundaries of that buffer. The standard C++ libraries provide many ways of safely buffering data, and C++'s
Standard Template Library (STL) provides containers that can optionally perform bounds checking if the programmer explicitly calls for checks while accessing data. For example, a vector's member function at() performs a bounds check and throws an out_of_range
exception if the bounds check fails. However, C++ behaves just like C if the bounds check is not explicitly called. Techniques to avoid buffer overflows also exist for C. Languages that are strongly typed and do not allow direct memory access, such as COBOL, Java, Eiffel, Python, and others, prevent buffer overflow in most cases. Well-written and tested abstract data type libraries that centralize and automatically perform buffer management, including bounds checking, can reduce the occurrence and impact of buffer overflows. The primary data types in languages in which buffer overflows are common are strings and arrays. Thus, libraries preventing buffer overflows in these data types can provide the vast majority of the necessary coverage. However, failure to use these safe libraries correctly can result in buffer overflows and other vulnerabilities, and naturally any
bug in the library is also a potential vulnerability. "Safe" library implementations include "The Better String Library", Vstr and Erwin. The
OpenBSD operating system's
C library provides the
strlcpy and
strlcat functions, but these are more limited than full safe library implementations. In September 2007, Technical Report 24731, prepared by the C standards committee, was published. It specifies a set of functions that are based on the standard C library's string and IO functions, with additional buffer-size parameters. However, the efficacy of these functions for reducing buffer overflows is disputable. They require programmer intervention on a per function call basis that is equivalent to intervention that could make the analogous older standard library functions buffer overflow safe.
Buffer overflow protection Buffer overflow protection is used to detect the most common buffer overflows by checking that the
stack has not been altered when a function returns. If it has been altered, the program exits with a
segmentation fault. Three such systems are Libsafe, and the
StackGuard and
ProPolice gcc patches. Microsoft's implementation of
Data Execution Prevention (DEP) mode explicitly protects the pointer to the
Structured Exception Handler (SEH) from being overwritten. Stronger stack protection is possible by splitting the stack in two: one for data and one for function returns. This split is present in the
Forth language, though it was not a security-based design decision. Regardless, this is not a complete solution to buffer overflows, as sensitive data other than the return address may still be overwritten. This type of protection is also not entirely accurate because it does not detect all attacks. Systems like StackGuard are more centered around the behavior of the attacks, which makes them efficient and faster in comparison to range-check systems.
Pointer protection Buffer overflows work by manipulating
pointers, including stored addresses. PointGuard was proposed as a compiler-extension to prevent attackers from reliably manipulating pointers and addresses. The approach works by having the compiler add code to automatically XOR-encode pointers before and after they are used. Theoretically, because the attacker does not know what value will be used to encode and decode the pointer, one cannot predict what the pointer will point to if it is overwritten with a new value. PointGuard was never released, but Microsoft implemented a similar approach beginning in
Windows XP SP2 and
Windows Server 2003 SP1. Rather than implement pointer protection as an automatic feature, Microsoft added an API routine that can be called. This allows for better performance (because it is not used all of the time), but places the burden on the programmer to know when its use is necessary. Because XOR is linear, an attacker may be able to manipulate an encoded pointer by overwriting only the lower bytes of an address. This can allow an attack to succeed if the attacker can attempt the exploit multiple times or complete an attack by causing a pointer to point to one of several locations (such as any location within a NOP sled). Microsoft added a random rotation to their encoding scheme to address this weakness to partial overwrites.
Executable-space protection Executable-space protection is an approach to buffer overflow protection that prevents execution of code on the stack or the heap. An attacker may use buffer overflows to insert arbitrary code into the memory of a program, but with executable-space protection, any attempt to execute that code will cause an exception. Some CPUs support a feature called
NX ("No eXecute") or
XD ("eXecute Disabled") bit, which in conjunction with software, can be used to mark
pages of data (such as those containing the stack and the heap) as readable and writable but not executable. Some Unix operating systems (e.g.
OpenBSD,
macOS) ship with executable-space protection (e.g.
W^X). Some optional packages include: •
PaX •
Exec Shield •
Openwall Newer variants of Microsoft Windows also support executable-space protection, called
Data Execution Prevention.
Proprietary add-ons include: • BufferShield • StackDefender Executable-space protection does not generally protect against
return-to-libc attacks, or any other attack that does not rely on the execution of the attackers code. However, on
64-bit systems using
ASLR, as described below, executable-space protection makes it far more difficult to execute such attacks.
Capability Hardware Enhanced RISC Instructions CHERI (Capability Hardware Enhanced RISC Instructions) is a computer processor technology designed to improve security. It operates at a hardware level by providing a hardware-enforced type (a CHERI capability) that authorises access to memory. Traditional pointers are replaced by addresses accompanied by metadata that limit what can be accessed through any given pointer.
Address space layout randomization Address space layout randomization (ASLR) is a computer security feature that involves arranging the positions of key data areas, usually including the base of the executable and position of libraries, heap, and stack, randomly in a process' address space. Randomization of the
virtual memory addresses at which functions and variables can be found can make exploitation of a buffer overflow more difficult, but not impossible. It also forces the attacker to tailor the exploitation attempt to the individual system, which foils the attempts of
internet worms. A similar but less effective method is to
rebase processes and libraries in the virtual address space.
Deep packet inspection The use of deep packet inspection (DPI) can detect, at the network perimeter, very basic remote attempts to exploit buffer overflows by use of attack signatures and
heuristics. This technique can block packets that have the signature of a known attack. It was formerly used in situations in which a long series of No-Operation instructions (known as a NOP-sled) was detected and the location of the exploit's
payload was slightly variable. Packet scanning is not an effective method since it can only prevent known attacks and there are many ways that a NOP-sled can be encoded.
Shellcode used by attackers can be made
alphanumeric,
metamorphic, or
self-modifying to evade detection by heuristic packet scanners and
intrusion detection systems.
Testing Checking for buffer overflows and patching the bugs that cause them helps prevent buffer overflows. One common automated technique for discovering them is
fuzzing. Edge case testing can also uncover buffer overflows, as can static analysis. Once a potential buffer overflow is detected it should be patched. This makes the testing approach useful for software that is in development, but less useful for legacy software that is no longer maintained or supported. ==History==