Finding buffer overflows with Valgrind
Valgrind can also help us find buffer overflows. That is when we put more data in a buffer than it can hold. Buffer overflows are the cause of many security bugs and are hard to detect. But with Valgrind, it gets a little easier. It might not be 100% accurate at all times, but it's a really good help along the way.
Knowing how to find buffer overflows will make your program more secure.
Getting ready
For this recipe, you'll need the GCC compiler, the Make tool, and the Makefile from the Starting GDB recipe in this chapter.
How to do it…
In this recipe, we'll write a small program that copies too much data into a buffer. We'll then run the program through Valgrind and see how it points out the problem:
- Write the following code in a file and save it as
overflow.c
. The program allocates 20 bytes withcalloc()
, then copies a string of 26 bytes into that buffer. It then frees up the memory usingfree...