Buffer overflow occurs when a program attempts to write more data into a buffer than it can hold, or when a program attempts to put data in a memory area past buffer. Such conditions could be used by an attacker to cause program malfunctions or even to execute his own programs (or lines of code). Buffer overflow vulnerabilities are usually found in programs written in C and C++ programming language.
One of the most basic examples of buffer overflow:
If the first main function's argument is greater than 4 bytes, everything from fifth byte will be written to memory adjacent to buffer.
- Write programs in programming language that performs automatic bounds checking.
- Use abstraction library to abstract away risky APIs (avoid standard library functions which are not bounds checked).
- Use compiler-based canary mechanisms (commercial or open source buffer overflow protection solutions).
- Use preventive functionality at operation system level.