Type Confusion Attacks
Integer Overflows & Type Confusion · Exploitation Fundamentals track
3 min read
In this module
When Data Wears the Wrong Mask
Type confusion occurs when a program treats a piece of memory as the wrong type. Unlike integer overflow — wrong values — this is about wrong interpretation. The program believes it's reading one kind of object; it's actually reading something else entirely.
Pointer and Integer Confusion
At the lowest level, pointers and integers are both just numbers in registers. If a program stores a user-controlled integer and later reads that same memory location as a pointer, the attacker controls where the program reads from or writes to. This gives them arbitrary memory access.
Consider this scenario: a structure has a field that should hold a pointer to a string. An attacker triggers a code path that writes an integer (say, a crafted address) into that field instead. When the program later dereferences the "string pointer," it follows the attacker's chosen address.
Type safety means the language prevents you from treating data as the wrong type. C has almost no type safety - you can cast any pointer to any other pointer type. Languages like Rust enforce strict type rules at compile time, making type confusion bugs nearly impossible to write.
Object Type Confusion in C++
C++ programs frequently use virtual function tables (vtables) to implement polymorphism. Each object contains a hidden pointer to its class's vtable, which holds addresses of the object's methods. If an attacker can make the program treat an object of type A as type B, the program will use the wrong vtable - calling attacker-influenced functions instead of the correct ones.
This often chains with use-after-free vulnerabilities, as we saw in Heap Vulnerabilities & Use-After-Free. An object is freed, the attacker reclaims that memory with controlled data, and the program still holds a pointer to the old location. When the program calls a method on the "object," it reads the attacker's fake vtable pointer.
Type Confusion in JavaScript Engines
Modern JavaScript engines use Just-In-Time (JIT) compilation and track the types of values for optimisation. If the engine's type tracking has a bug, it might compile code that assumes a value is a small integer when it is actually an object pointer, or vice versa. This type confusion in the JIT compiler gives attackers a primitive for reading and writing arbitrary memory from JavaScript - a critical step in browser exploitation.
The Chain of Exploitation
Type confusion rarely lands as a standalone bug. A typical chain: use-after-free (Heap Vulnerabilities & Use-After-Free) gives the attacker control over freed memory. They fill it with crafted data. The program reads the stale pointer and interprets that attacker-controlled data as an object with a vtable. The fake vtable steers the next virtual function call straight into their code.
Defensive Thinking
Use memory-safe languages where possible. In C++, use smart pointers and avoid raw casts between unrelated types. Runtime type checking, such as dynamic_cast in C++, adds overhead but catches confusion at the point it occurs rather than letting it propagate silently into exploitation.
Try the Simulations
Integer Overflow Bypass
Add a 10-byte header to an attacker-supplied length of 250 and watch the 8-bit total wrap round to 4. The wrapped value sails through a size check it should have failed.
Type Confusion
Store a value intended as a 'pointer' (address 15), then read it back and use it as a loop counter. The same bits mean completely different things depending on interpretation.