Buffer Overflow - ”Data Gone Wild” – CS0
Buffer overflow occurs when data is input or written beyond the allocated bounds of an object, causing a program crash or creating a vulnerability that attackers might exploit.
A buffer overflow occurs when data is written beyond the boundaries of a fixed length buffer overwriting adjacent memory locations which may include other buffers, variables and program flow data. Considered the “nuclear bomb” of the software industry, the buffer overflow is one of the most persistent security vulnerabilities and frequently used attacks.
Risk: How Can It Happen?
Writing outside the bounds of a block of allocated memory can corrupt data, crash the program, or cause the execution of malicious code. C++ is particularly vulnerable to buffer overflow.
Example of Occurrence:
Buffer overflow vulnerabilities were exploited by the the first major attack on the Internet. Known as the Morris worm, this attack infected more than 60,000 machines and shut down much of the Internet for several days in 1988. Carolyn Duffy Marsan Morris Worm Turns 20: Look what it’s Done Network World, October 30, 2008 http://www.networkworld.com/news/2008/103008-morris-worm.html?page=1
Example in Code:
Though Python allows various ways to create and manipulate arrays, if you use arrays of a predetermined size you may encounter a buffer overflow.
importantData = 1 buffer=[None]*10 for i in range(0,14): buffer[i]=7 print "after buffer overfow" importantData
In the code above, buffer has 10 elements but the loop attempts to writes through 15 elements, which results in a buffer overflow.
Code Responsibly– How Can I Avoid Buffer Overflow?
Make sure you have enough space: Before copying data to a fixed size block, make sure it is large enough to hold the data that you are going to copy. If it is not large enough, do not copy more data than your available space can hold. If your program is not able to continue properly after filling the available space, you may have to find some way to recover from the error.
Validate indices: If you have an integer variable, verify that it is within the proper bounds before you use it as an index to an array. This validation is particularly important for any values that might have been provided as user input or other untrusted input, such as information that might be read from a file or from a network connection.
Use alternative data structures that reduce the risk of overflows: When possible, use lists in Python without defining the initial size and use the .append method to add elements which can reduce your risk of buffer overflow vulnerabilities.
tests=[None]*10 count = input("How many numbers? ") for i in range (0,count-1): test = input ("Please type a number: ") tests[i]=test
- Type* the example in code from the Background section above. Compile and run.
- What happed? Why? Fix the error and run again.
- Type* Program 1 and compile.
- Complete the security checklist for this program (print the checklist)
- The V indicates where the potential buffer could occur. How could we prevent this?
- Revise the program to eliminate potential buffer overflow problems.
- Turn in program (marked after completing checklist), output, and questions
*Copying and pasting programs may result in syntax errors and other inconsistencies. It is recommended you type each program.
|Vulnerability:Buffer Overflow Course: CS0|
|Task – Check each line of code||Completed|
|1. Finding Arrays:|
|1.1 Underline each array declaration|
|1.2 For each array, underline all subsequent references|
|2. Index Variables – legal range for an array of size n is 0 <= i < n|
|2.1 For each underlined access that uses a variable as an index, write the legal range next to it.|
|2.2 For each index marked in 2.1, underline all occurrences of that variable.|
|2.3. Circle any assignments, inputs or operations that may modify these index variables.|
|2.4. Mark with a V any array that is indexed by a circled index variable.|
|Highlighted areas indicate a buffer overflow vulnerability.
- Describe the buffer overflow problem.
- How could you prevent a buffer overflow from occurring in your program?
Further Work (optional – check with your instructor if you need to answer the following questions)
- Give three real life examples of buffer overflow attacks (research on the web).
- What can result from a buffer overflow?
- Provide three different examples of code that contains buffer overflows.