A buffer is a transient area in memory used for data storage.
If a buffer is designed to hold “x” bytes of data, and “x+y” bytes are pushed into it, “y” bytes of data will “overflow” beyond the buffer.
This is known as buffer overflow.
It creates critical vulnerabilities in software programs as malicious hackers can cause the buffer to overflow with code which can trigger security nightmares like damaging databases, deleting files, tampering the registry, or mining private information.
Three strategies to defend against buffer overflow in programming are-
- Safely handling strings
Strings are one of the most common forms of input. Most string handling functions “have no built-in checks for string length, strings are frequently the source of exploitable buffer overflows.”
Strings should be-
- initialized explicitly to null before using
- truncated to one byte smaller than the buffer size before filling memory
- released safely after using – the pointers should point to null and the memory should be flushed
- Calculating buffer sizes
Coders should use functions like “sizeof” to calculate the size of a “fixed-length” buffers. This needs to be documented diligently with generous comments throughout the code where the buffer is being utilized. In future when other developers inherit the code they will be aware of the size and not cause it to overflow.
Also, unsigned variables (such as size_t) should be used when calculating the size of the buffer. Signed variables store negative numbers as large positive numbers, and an attacker may be able to “cause a miscalculation in the size of the buffer or data” by writing a large number to your program.
- Avoiding library files
Library files, are used in programming language and are “inherently insecure”. They are an easy target for hackers attacking applications. If a vulnerability is detected by a hacker in a library file, he will use it to exploit all the applications that call the functions of that library file. This sets the ground for a wide-spread security compromise.