What is a Segmentation Fault? And Why it Occurs?

Welcome to Hacker Tips - The place where you master the skills behind hacking.

In this post we are going to learn what a segmentation fault is and the reasons behind its occurence.


Segmentation Fault 

A basic Definition: Memory is split into segments, and some memory addresses are not within the boundaries of the memory segments that program is not given to. When the program attempts to access an address that is not within the bounds, it will crash and die in what is called as "Segmentation Fault!!"
 

Before getting into segmentation fault, lets discuss about Memory Segmentation.

Memory Segmentation

Memory is divided into five segments, They are:
  1. Text Segment 
  2. Data Segment
  3. Bss Segment
  4. Heap Segment
  5. Stack Segment
1.TEXT Segment:      

Text segment is also called code segment or simply text. This where all the executable instructions of the program are stored.
This is the lowest segment and is Read-Only. Since this is a read-only segment, overwriting or changing the instructions is not possible in this segment.

2. DATA Segment:

This segments contains all the initialized variables of the program.

NOTE: Global Variables by default are initialized to 0. All the local varibles by default contains some junk value.

Example Code:




As you can see here three variables are  declared.
global - Is a global variable
local and initialized_local - are local variables.
local is an unintialized variable and initialized_local is initialized to 1.

On compilation:





You can see that, on compilation, the compiler(gcc) generated a warning of "local is used uninitialized in this function". We know that global is also uninitialized here, but the compiler did not generate any warning for that. This means global variables are initialized to zero. And depending in the compiler version, the local variables are also initialized. But mostly contain garbage values.

3. BSS Segment:

 This segment is also called as "Uninitialized Data segment".

Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing.

Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.


From the above source code :
global and initialized_local are stored in data segment and the variable local is stored in bss segment.

We'll Discuss Stack and Heap in later posts since some prior knowledge is required to understand their properties and funcitonalities.

Now back into our topic Segmentation Fault.

Causes of Segmentation Fault:
  1. Attempting to access memory addresses which are out of bounds
  2. Attempting to write to a memory which is a read-only memory, such as text segment.
  3. Trying to access something without permissions.
  4. Dereferencing a Null pointer.
  5. Accessing an unintialized pointer.
  6. Dereferencing or assaigning a freed pointer.(Freeing memory in the heap)



        

    
Segmentation Fault can be analyzed using the GNU Debuger(gdb).
In Unix Systems this is indicated by "SIGSEGV signal" and in Windows Operating Systems it is indicated by "STATUS_ACCESS_VIOLATION".

Lets explore more in future posts.... 

Comments