(30 points) In beginning programming classes, you learned about information hiding, also called encapsulation. Those rules basically state that data structures should be encapsulated so that, if they need to be changed, the interface remains the same, so the user (or calling program) does not know what representation of the abstract data type is used. This can be implemented easily using objects in object-oriented programming languages; in other languages, one must use language constructs carefully (like the use of static and files in C).
As an example, consider the queue library in the Robust Programming handout [Bis11]. The fragile version of the library does not provide information hiding or encapsulation, because the program can access the queue structures directly (through the queue pointers). The robust version does provide encapsulation, because the program cannot access the queue structures, or queue management structure, directly. No pointers or variables are ever exposed.
Please identify the application security risk in the OWASP reference [Chr11], and the weakness in the CWE reference [OWA13], that best describe the failure to encapsulate a data structure as described above.
The three documents referenced above are available on SmartSite in the Resources > Handouts area.
(40 points) This problem asks you to implement a buffer overflow attack on a program. In the Resources area of SmartSite (or the Homework area of the nob.cs.ucdavis.edu class web site) is a program bad.c (also see below). This program contains a buffer overflow vulnerability; see the call to gets(3) at line 13. Your job is to exploit the overflow by providing input to the running process that will cause the program to invoke the function trap (which, you may notice, is not called anywhere else). You will know you’ve succeeded when you run the program, give it your input, and it prints “Gotcha!”
The following questions will help guide you. Please turn in your answers to them, a hex dump of the input you use to call trap, and a typescript or screen shot of you running the program bad, giving it your input, and showing its output.
- What is the address of the function trap()? How did you determine this?
- What is the address on the stack that your input must overwrite (please give both the address of the memory location(s), and their contents)? How did you locate this address?
- What is the address of buf?
- The sled is the input you give to alter the return address stored on the stack. What is the minimum length your sled must be?
bad.c
This is a listing of bad.c.
#include <stdio.h>
#include <stdlib.h>
int trap(void)
{
printf("Gotcha!\n");
exit(0);
}
int getstr(void)
{
char buf[12];
gets(buf);
return(1);
}
int main(void)
{
getstr();
printf("Overflow failed\n");
return(1);
}