Writing data to buffers is a routine part of any program's functioning, and also a major source of potential problems. When a program tries to put more data in a buffer than its programmer designed it to hold, the consequent overflow of data can corrupt other program data. This can lead to software crashes, or potentially a means for hackers to hijack the program and execute malicious code.
Buffers and Overflows
- A buffer is an array variable designed to store data for the program to use at a later point in its execution. This could be information a user types into the program, the contents of a file that the program reads in, or anything else the program may need to execute. Buffers are created with a specific size, which is reserved in a computer's memory to store the buffer's data. Buffer overflows occur when a program writes an amount of data to a buffer that exceeds its actual size, causing the program to write data past the buffer's location in the computer's memory and into memory reserved for other buffers or variables.
Crashes
- A program crashes when it finds itself unable to execute the task it has been coded to perform at a particular point in time, and its developer did not include a mechanism for recovering from such a failure. Buffer overflows can cause programs to crash when the overflow corrupts the variable's data that is stored in a nearby memory slot. For example, if an overflow of a text buffer may write text characters over the numbers stored in an integer variable's memory slot. When the program accesses this variable's data to perform a calculation, it finds the text characters instead of the numbers. The program cannot perform mathematical computations on text characters, so the program would react by crashing.
Security Vulnerabilities
- In advertentm buffer overloads can cause programs to crash, but hackers could deliberately force a buffer overload to compromise a program's function. For example, a buffer's memory slot could be near the memory slot that holds a particular command the program executes. A hacker can discover the size of the buffer, and the distance to the command's memory slot from the end of the buffer. He could then force a buffer overflow that would over-write the data between the buffer and the command's memory slot, then over-write the command's memory slot to replace the command with one of his own design. When the program calls that command's memory slot and executes its contents, it would be executing the hacker's code instead of the one the programmer intended.
Preventing Buffer Overflows
- Programmers can prevent buffer overflows by instituting security mechanisms to ensure that the program does not try to write more data to a buffer than he designed it to hold. These can include checking the size of the data users try to put into it, through typing or selecting an input file, to ensure it does not exceed the size of the buffer. Programmers should also try to minimize the opportunities which users have to write data directly to a buffer, as every input that writes directly to a buffer is a potential vector for a buffer overload attack.