A common software coding mistake that an attacker could exploit to gain access and are responsible for many vulnerabilities in operating systems and application programs. It is also known as buffer overrun causing data to overflow to adjacent memory space and overwriting the information which leads to crashes and exploitable conditions. There are two types of buffer overflow which are stack-based and heap-based. Heap-based attacks an application by flooding the memory space reserved for a program. Whereas, stack-based exploit application and programs by a stack which is a memory space used to store user input.
Buffer applications are written in programming languages like C and C++ .
From the above example, ‘buff’ represents an array of 10 bytes where buff is the left boundary and buff 4 is the right boundary, and ‘arr’ represents an array of 5 integers. A buffer is said to be overflown when the data (meant to be written into memory buffer) gets written past the left or the right boundary of the buffer. From fig.2, we declared an array of size 5 bytes, but in the next line, we index 5 was used to store the value ‘a’ and this is exactly the point where buffer overflows happen as data gets overwritten due to memory shortage.
A buffer overflow often results in a crash which may lead to Denial of Service (DoS) and uncertain cases, buffer overflows allows attackers to overwrite critical settings in application’s memory. Furthermore, sensitive data gets exposed to attackers which are now out-of bound reads. Buffer overflows are the one of the oldest and most common causes for arbitrary code execution vulnerabilities.
How to prevent Buffer Overflows?
The root cause of the buffer overflows is the result of programming error. Thus, it is the best way to train developers and make sure to test again and again in order to avoid such errors. Also, developers are encouraged to adopt features like ASLR and position-independent executables (PIE) to limit the potential impact of buffer overflows. Another way to prevent the attacks is by making sure that the memory auditing is done properly in the program using utilities like valgrind memcheck. Developers should use fgets() instead of gets() and by using strncmp() instead of strcmp(), strncpy() instead of strcpy() and so on.
The CERT Coordination Center and Carnegie Mellon University’s Software Engineering Institute have developed coding standards for several programming languages, including C and C++.
The main cause of Buffer Overflow is when a program puts data into a buffer and forgets to check the buffer boundary which leads to crashing the program and many other possible vulnerabilities. Buffer is located on the stack and a Buffer Overflow can cause the return address on the stack to be overwritten. This results in the program to jump to the new specified location by the new return address in which attackers take the benefits of the new locations by putting malicious code. Thus, the malicious code in the programs leads to security breaches.
Many countermeasures have been developed which are already incorporated in operating systems, compilers, software tools, and libraries. However, not all countermeasures are fool proof and none of these solutions provide complete protection. Therefore, it is important to review code periodically and application security testing with both internal and external security teams as well.