Course contentsShow
← Java
Lesson 2160 of 2,87042. SerializationFree lesson

The Billion Laughs Attack and Resource Exhaustion

How deeply nested or circular object graphs can cause memory exhaustion and denial of service.

The Billion Laughs Attack and Resource Exhaustion

You'll learn how maliciously crafted object graphs can exhaust memory and crash your application through nested or circular references.

The Threat

When deserializing untrusted data, an attacker can create deeply nested or circular object structures that consume enormous amounts of memory, causing your JVM to run out of heap space and triggering a denial-of-service (DoS).

The "Billion Laughs" attack (originally from XML) applies to Java serialization too: a small serialized payload can expand exponentially during deserialization. Imagine a serialized object that references itself 10 times, each reference doing the same. Each level multiplies the memory footprint—a 1KB payload becomes gigabytes.

How It Works

Consider a serialized object graph where:

  • Object A contains a list of 10 references to Object B
  • Object B contains a list of 10 references to Object C
  • This nesting continues 10 levels deep

During deserialization, the JVM must reconstruct every object instance. With 10 branches at each of 10 levels, you're instantiating 10^10 objects from a tiny file. The attacker sends a compact payload, but your application consumes gigabytes of RAM and grinds to a halt.

Circular references are equally dangerous: Object X references Y, Y references X. Without safeguards, the deserialization logic can loop indefinitely or create massive redundant structures.

The Real Danger

Unlike other exploits that require specific vulnerable classes, resource exhaustion works with any serializable object structure. You don't need a gadget chain—just malicious nesting. This makes it a universal DoS vector against systems accepting serialized input.

Key Takeaway: Deeply nested or circular object graphs in serialized data can cause exponential memory consumption during deserialization, leading to OutOfMemoryError and application crashes—always validate and limit deserialization of untrusted sources.