Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer array overflow in for loop in c

Tags:

c++

c

When would a program crash in a buffer overrun case

#include<stdio.h>
#include<stdlib.h>

main() {
    char buff[50];
    int i=0;
    for( i=0; i <100; i++ )
    {
        buff[i] = i;
        printf("buff[%d]=%d\n",i,buff[i]);
    }
}

What will happen to first 50 bytes assigned, when would the program crash?

I see in my UBUNTU with gcc a.out it is crashing when i 99

>>
buff[99]=99
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped)
<<

I would like to know why this is not crashing when assignment happening at buff[51] in the for loop?

like image 925
Suresh Kandukuru Avatar asked Oct 10 '13 10:10

Suresh Kandukuru


People also ask

What is a buffer overflow in C?

A buffer overflow occurs when the size of information written to a memory location exceeds what it was allocated. This can cause data corruption, program crashes, or even the execution of malicious code.

Which C function can cause buffer overflow and why?

That is why the safest basic method in C is to avoid the following five unsafe functions that can lead to a buffer overflow vulnerability: printf , sprintf , strcat , strcpy , and gets .

What happens when array overflow?

Overflow is a condition that arises When we want to insert new data into the array data structure but there is no available space in that data structure. It means that there are no any empty list to store a new data, this situation is called overflow.

How does buffer overflow works?

Also known as a buffer overrun, buffer overflow occurs when the amount of data in the buffer exceeds its storage capacity. That extra data overflows into adjacent memory locations and corrupts or overwrites the data in those locations.


1 Answers

It is undefined behavior. You can never predict when (or if at all) it crashes, but you cannot rely upon it 'not crashing' and code an application.

Reasoning

The rationale is that there is no compile or run time 'index out of bound checking' in c arrays. That is present in STL vectors or arrays in other higher level languages. So whenever your program accesses memory beyond the allocated range, it depends whether it simply corrupts another field on your program's stack or affects memory of another program or something else, so one can never predict a crash which only occurs in extreme cases. It only crashes in a state that forces the OS to intervene OR when it no longer remains possible for your program to function correctly.

Example

Say you were inside a function call, and immediately next to your array was, the RETURN address i.e. the address your program uses to return to the function it was called from. Suppose you corrupted that and now your program tries to return to the corrupted value, which is not a valid address. Hence it would crash in such a situation.

The worst happens when you silently modified another field's value and didn't even discover what was wrong assuming no crash occurred.

like image 115
fkl Avatar answered Oct 01 '22 18:10

fkl