What is Buffer Overflow?

Buffer overflow – also sometimes known as buffer overrun – is a bug or anomaly in the software that occurs when a program writing to a buffer in memory overshoots its allocated buffer space and ends up writing into an adjacent or nearby memory space (buffer).

Buffer overflows typically are just bugs in the software systems which do not do any harm on their own.

If a buffer overflow exists in the software, the software will crash itself, and any software that uses the buffer has overrun.

But in cases where buffer overflows are exploited maliciously, they may – and in most cases, result in a security breach. Most of the vulnerabilities in software worldwide result from a buffer overflow error.

Programs built using C and C++ languages are often prone to buffer overflow attacks as they do not have any built-in functions that provide error checking regarding buffer overflows. The programmer may have accidentally developed that due to some error in logic or to lack of manual error checking.

Hackers can cause a buffer overflow and overwrite part of the buffer with malicious code’s instructions, causing that code to execute on your device. Running this code may delete essential files from your disk, connect to remote systems, download malware from the Internet, or take complete remote control of your system.

There are two basic techniques through which buffer overflows are exploited: stack-based exploitation and heap-based exploitation. Stack-based buffer overflows can trigger several methods of attack.

Stack overflows can overwrite the values of variables adjacent to the program’s allocated buffer and, therefore, change the program’s logic. Stack overflow errors can also target data containing a return address or data stored in a register and thus change the program execution flow.

Buffer overflows based on heap exploitation function in an entirely different manner. Heaps are dynamically allocated memory spaces and are typically known to contain program data. The heap-based exploitation occurs so that the internal program structures, such as linked list pointers, are overwritten.

This method overwrites the linkage to the next instruction and hence overwrites the program function pointer.