DEV Community

Cover image for Understanding Heap Overflow & Use After Free
Prasun Chakraborty
Prasun Chakraborty

Posted on

Understanding Heap Overflow & Use After Free

You’ve likely seen the headlines: CERT-In (Indian Computer Emergency Response Team) just issued a high-severity alert for Google Chrome (versions prior to 131.0.6778.83).

If you mainly write high-level JavaScript or Python, you might think memory management isn't your problem. You are wrong. Understanding these flaws is crucial to understanding how modern software fails and how to architect systems that don't.

The Stage: Stack vs. Heap

To understand the exploit, you must understand the battlefield. In languages like C++ (which Chrome is built on), memory is divided into two primary zones:

Stack Vs Heap

1. The Stack (Ordered & Fast)

Think of this like a stack of dinner plates. You put a plate on top, you take it off the top. It is highly organized. This is where local variables and function calls live. It is safe, but rigid.

2. The Heap (Dynamic)

Think of this like a massive warehouse floor. You can put a box anywhere there is space. This is where dynamic data lives (e.g., the DOM nodes of a website, the images you load, the tabs you open).

Pros: Flexible size, accessible globally.

Cons: You have to manage it manually. If you forget where you put a box, or if you put a big box in a small space, things break.

Both vulnerabilities in the Chrome alert happen here: inside the Heap.

Heap Buffer Overflow

A "Buffer" is simply a reserved chunk of memory. A "Heap Buffer Overflow" occurs when a program tries to write more data into a buffer than it was allocated to hold.

The Analogy: The Spilling Bucket
Imagine you have two buckets sitting next to each other on the warehouse floor (The Heap).

Bucket A: Holds 1 Liter (Your Data).

Bucket B: Holds specific instructions (e.g., "Return to System").

If you pour 2 Liters of water into Bucket A, it doesn't just disappear. It overflows and spills directly into Bucket B, corrupting whatever was inside.
The Spilling Bucket

Why is this dangerous? It's not just about crashing the browser. Smart attackers craft the "overflow" data specifically. They don't just spill random water; they spill malicious code. By overflowing into the adjacent memory space, they can overwrite function pointers.

Instead of the browser thinking, "Okay, render this image," the overflow changes the instruction to: "Okay, open a backdoor for the attacker."

Use After Free (UAF)

It is a temporal memory error that is notoriously hard to debug.

The Concept
In manual memory management (C/C++), the lifecycle of an object is:

  • Allocate (Get memory).

  • Use (Read/Write).

  • Free (Release memory back to the OS).

A UAF error happens when the program continues to use a pointer (reference) to the memory after it has already been freed.

The Analogy: The Hotel Key

Check-in: You get a keycard (Pointer) to Room 777 (Memory Address).

Check-out: You leave. The hotel marks Room 777 as "Empty" (Freed).

The Flaw: The hotel forgot to take your keycard back.

The Exploit: A new guest (Attacker) checks into Room 777. You (the old code) still have the key. You unlock the door and mess with the new guest's luggage.

The Hotel Key

In a complex engine like Chrome's V8, objects are created and destroyed thousands of times per second. If the browser frees a Javascript object but keeps a reference to it somewhere in the rendering pipeline, an attacker can force the browser to allocate their malicious object in that exact same memory slot.

When the browser tries to access the "old" object, it inadvertently executes the attacker's payload. This is often how Remote Code Execution (RCE) is achieved.

As a user, your job is simple: Update Chrome. As a developer, your job is harder: Write code that respects the boundaries of memory.
This is why there is a massive push to rewrite core infrastructure in Rust. Rust’s "Borrow Checker" makes Use-After-Free bugs impossible at compile time. It strictly manages who holds the "key" to the memory. Tools like AddressSanitizer (ASan) inject checks into the code to detect if a program accesses memory it shouldn't.

Top comments (0)