Buffer Overflow Attack Explained with a C Program Example

Buffer overflow attack has been there for a long time. It still exist today partly because of programmers carelessness while writing a code. The reason I said “partly” because sometimes a well-written code can be exploited with buffer overflow attacks, as it also depends upon the dedication and intelligence level of the attacker.

The least we can do is to avoid writing bad code that gives a chance to even script kiddies to attack your program and exploit it.

In this buffer overflow tutorial, we’ll discuss the basics of the following:

  • What is buffer overflow?

  • How a buffer overflow happens?

  • How a buffer overflow attack takes place?

  • How to avoid buffer overrun?

We’ll keep the explanation and examples simple enough for you to understand the concept completely. We’ll also use C programming language to explain the buffer overflow concept.

What is Buffer Overflow?

A buffer, in terms of a program in execution, can be thought of as a region of computer’s main memory that has certain boundaries in context with the program variable that references this memory.

For example:

char buff[10]

In the above example, “buff” represents an array of 10 bytes where buff[0] is the left boundary and buff[9] is the right boundary of the buffer.

Let’s take another example:

int arr[10]

In the above example, “arr” represents an array of 10 integers. Now assuming that the size of the integer is 4 bytes, the total buffer size of “arr” is 10*4 = 40 bytes. Similar to the first example, arr[0] refers to the left boundary while arr[9] refers to the right boundary.

By now it should be clear what a buffer means. Moving on let’s understand when a buffer overflow.

A buffer is said to be overflowed when the data(meant to be written into memory buffer) gets written past the left or right boundary of the buffer. This way the data gets written to a portion of memory which does not belong to the program variable that references the buffer.

Here is an example:

char buff[10];
buff[10] = 'a';

In the above example, we declared an array of size 10 bytes. Please note that index 0 to 9 can use to refer these 10 bytes of the buffer. But, in the second line, index 10 was used to store the value “a”. This is a point where buffer overrun happens because data gets written beyond the right boundary of the buffer.

It is also important for you to understand how GCC compilation process works to create a C execuable.

Why are buffer overflows harmful?

Some of us may think that though a buffer overflow is a bad programming practice but so is an unused variable on stack, then why there is so much hullabaloo around it? What is the harm buffer overrun can cause to the application?

Well, if in one line we have to summarize the answer to these questions then it would be:

Buffer overflows, if undetected, can cause your program to crash or produce unexpected results.

Let’s understand a couple of scenarios which justify the answer mentioned above.

  1. Consider a scenario where you have allocated 10 bytes on heap memory:
char *ptr  = (char*) malloc(10);

Now, if you try to do something like this:

ptr[10] = 'c';

Then this may lead to a crash in most of the cases. The reason being, a pointer is not allowed to access heap memory that does not belong to it.

  1. Consider another scenario where you try to fill a buffer(on stack) beyond it’s capacity:
char buff[10] = {0};
strcpy(buff, "This String Will Overflow the Buffer");

As you can see that the srtcpy() function will write the complete string in the array “buff” but size of “buff” is less than the size of string so the data will get written past the right boundary of array “buff”. Now, depending on the compiler you are using, chances are high that this will get unnoticed during compilation and would not crash during execution. The simple reason being that stack memory belongs to program so any buffer overflow in this memory could get unnoticed.

So in these kinds of scenarios, buffer overflow quietly corrupts the neighboring memory and if the corrupted memory is being used by the program then it can cause unexpected results.

You also need to understand how you can prevent stack smashing attacks with GCC.

Buffer Overflow Attacks

Until now we discussed what buffer overflows can do to your programs. We learned how a program could crash or give unexpected results due to buffer overflows. Horrifying isn’t it? But, that it is not the worst part.

It gets worse when an attacker comes to know about a buffer overflow in your program and he/she exploits it. Confused? Consider this example:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char buff[15];
    int pass = 0;

    printf("\n Enter the password : \n");
    gets(buff);

    if(strcmp(buff, "thegeekstuff"))
    {
        printf ("\n Wrong Password \n");
    }
    else
    {
        printf ("\n Correct Password \n");
        pass = 1;
    }

    if(pass)
    {
       /* Now Give root or admin rights to user*/
        printf ("\n Root privileges given to the user \n");
    }

    return 0;
}

The program above simulates a scenario where a program expects a password from user and if the password is correct then it grants root privileges to the user.

Let’s the run program with correct password ie “thegeekstuff”:

$ ./bfrovrflw 

 Enter the password :
thegeekstuff

 Correct Password 

 Root privileges given to the user

This work as expected. The passwords match and root privileges are given.

But do you know that there is a possibility of buffer overflow in this program? The get() function does not check the array bounds and can even write string of length greater than the size of the buffer to which the string is written. Now, can you even imagine what can attacker do with this kind of a loophole?

Here is an example:

$ ./bfrovrflw 

 Enter the password :
hhhhhhhhhhhhhhhhhhhh

 Wrong Password 

 Root privileges given to the user

In the above example, even after entering a wrong password, the program worked as if you gave the correct password.

There is a logic behind the output above. What attacker did was, he/she  supplied an input of length greater than what buffer can hold and at a particular length of input the buffer overflow so took place that it overwrote the memory of integer “pass”. So despite of a wrong password, the value of “pass” became non-zero and hence root privileges were granted to an attacker.

There are several other advanced techniques(like code injection and execution) through which buffer overflow attacks can be done but it is always important to first know about the basics of the buffer, it’s overflow and why it is harmful.

To avoid buffer overflow attacks, the general advice that is given to programmers is to follow good programming practices. For example:

  • Make sure that memory auditing is done properly in the program using utilities like “valgrind memcheck”

  • Use fgets() instead of gets().

  • Use strncmp() instead of strcmp(), strncpy() instead of strcpy() and so on.