Buffer Overflow

What is it

Imagine pouring water through the topmost glass on a stack of glasses placed in the shape of a pyramid.

What happens after the topmost glass is full? It spills over to other glasses, right!

This is similar to what happens in buffer overflow (albeit a very simplistic analogy). Data goes out of the boundaries of data structures like Arrays, Stack, Heaps etc. and overwrites the portion of memory beside it causing the program and its underlying system to behave in an unexpected manner. Examples include BSOD – Blue Screen of Death, executing other programs, crashing etc.

How serious is it

As explained above buffer overflows allows a program to execute or access other memory areas resulting in various other exploits such as running some arbitrary code in the same execution environment and privileges, modifying system state or crashing the system.

The impact of buffer overflow may result in

  • Execution of another program in the same user security context
  • Modifying state of the system on which the application with this vulnerability is running
  • Crashing the memory stack and the system along with it
  • Gaining access to the data contained in your system (loss of Confidentiality)
  • Possibility of loss of data (loss of Availability)

The amount of data an attacker can retrieve depends on multiple factors like his creative skills, system design and implementation and the underlying operating systems’ weaknesses.

How to prevent

  • Check bounds of data structures
  • Check bounds of every data structure while using them. This is important mainly in the context of coding routines that are developed in-house rather than using existing libraries.

Use a language or library that supportsor does bound checking

These are also known as safe libraries. They help in preventing buffer overflows by checking the bounds before allocating memory and performing other operations.

PHP Example
Java Example
C# Example

Use Secure Coding Principles

By implementing secure coding guidelines one can not only prevent buffer overflows but also other serious security flaws.

Implement Secure Software Development Lifecycle (SSDLC)

Secure Coding guidelines although critical form ONLY a piece of the puzzle. In order to ensure that applications are secure, security need to be bolted right from the requirements stage and should extrapolate across every other stage be it Design, Coding, Review, Testing, Release or implementation.