COMPUTER SCIENCE // CRYPTOLOGY
In addition to stack-based buffer overflow attacks (i.e., smashing the stack), heap overflows can also be exploited. Consider the following C code, which illustrates a heap overflow:
int main() {
int diff, size = 8;
char *buf1, *buf2;
buf1 = (char *)malloc(size);
buf2 = (char *)malloc(size);
diff = buf2 - buf1;
memset(buf2, '2', size);
printf("BEFORE: buf2 = %s\n", buf2);
memset(buf1, '\0', diff + 3);
printf("AFTER: buf2 = %s\n", buf2);
return 0;
}
a. Compile and execute this program. What is printed?
b. Explain the results you obtained in part a.
c. Explain how a heap overflow might be exploited by Trudy.