So I have this simple piece of code which demonstrates a simple buffer overflow:
#include <stdio.h>
int main(void)
{
char c[4] = { 'A', 'B', 'C', 'D' };
char d[4] = { 'W', 'X', 'Y', 'Z' };
printf("c[0] is '%c'\n", c[0]);
d[4] = 'Z'; /* Overflow that overwrites c[0] */
printf("c[0] is '%c'\n", c[0]);
return 0;
}
The output:
$ ./a.out
c[0] is 'A'
c[0] is 'Z'
I have tried compiling this code with the following gcc options and it passed with flying colors:
gcc -Wall -Wextra -Wformat=2 -Wswitch-default -Wcast-align -Wpointer-arith \
-Wbad-function-cast -Wstrict-prototypes -Winline -Wundef -Wnested-externs \
-Wcast-qual -Wshadow -Wwrite-strings -Wconversion -Wunreachable-code \
-Wstrict-aliasing=2 -ffloat-store -fno-common -fstrict-aliasing \
-Wstack-protector -fstack-protector-all -std=c99 -pedantic -O0 -ggdb3
I also tried libefence and valgrind. I expected libefence to pass since it's made to catch out of bounds read/writes on the heap, but I was surprised that valgrind passed.
This code does not produce a Segfault since c[4] and d[0] happen to overlap and I think it is this that is causing tools to miss it.
So, what out there CAN catch this? Something free that works on Linux would be nice.