Skip to main content
Be Bitwise

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.

At the hardware level, memory holds raw bytes with no type information. Whether a value is a pointer, an integer, a character, or a boolean depends entirely on how the code interprets it. Type confusion occurs when code reads a value as one type that was written as a different type. If an attacker can control the value, they can exploit the misinterpretation — for example, making a 'size' value that is actually a carefully crafted pointer, or vice versa.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Store a value as a 'pointer'

    Do this: Step through MOV R0, #128 / MOV R1, #15 / STORE [R0], R1

    Watch for: Value 15 appears at address 128. It was written with pointer semantics

    We store 15 at address 128, intending it to be a memory address (a pointer). But the CPU does not tag it as 'pointer type' — it is just the number 15 sitting in memory.

  2. 2

    Read it back as a 'counter'

    Do this: Step through LOAD R2, [R0]

    Watch for: R2 = 15. The same bytes, but now the code will treat them as a loop counter

    The code loads the value from address 128 into R2 and proceeds to use it as a loop counter. The bits have not changed — only the code's interpretation has. This is the type confusion.

  3. 3

    Watch the confused loop

    Do this: Step through or Run the loop

    Watch for: The loop runs 15 times, incrementing R3 from 0 to 15

    The loop iterates 15 times because R2 = 15. If the code expected a small counter (say, 3), the loop runs 5x more iterations than intended. In a real vulnerability, this could mean writing 15 bytes into a 3-byte buffer.

  4. 4

    Verify the confusion

    Do this: Wait for HALT and check R3

    Watch for: R3 = 15 — the 'pointer' value became loop iterations

    R3 = 15 proves the type confusion: what was stored as a pointer was used as an iteration count. An attacker who controls the stored value controls how many times the loop runs.

Objectives

  • •See value 15 stored at address 128 as a 'pointer'
  • •Watch the same value 15 get loaded and used as a loop counter
  • •Count the loop executing 15 times - far more than expected
  • •Verify R3 = 15 at the end, proving the type confusion

Hints

  • The bits in memory are just 0x0F - whether it is a pointer or counter depends on how the code interprets it