Skip to main content
Be Bitwise

Buffer Overflow

Write one element too many into a 3-slot buffer. The fourth write clobbers the return address - the CPU would jump to the wrong place.

A buffer overflow occurs when code writes more data than a buffer can hold. Because the return address is stored in memory right after the buffer, an extra write overwrites it. When the function later executes RET, the CPU pops the corrupted value and jumps to a completely unintended address. This single off-by-one error is the root cause of countless security vulnerabilities.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Set up the stack frame

    Do this: Step through the setup: buffer base, values, and return address at address 203

    Watch for: Return address 15 stored at address 203. Counter is 4 (one more than the buffer can hold)

    The layout is identical to the safe-buffer-write scenario, but the counter is 4 instead of 3. That single difference is the entire vulnerability.

  2. 2

    Watch the first three writes (safe)

    Do this: Step through three iterations of the loop

    Watch for: Values 1, 2, 3 fill addresses 200, 201, 202. Address 203 still holds 15

    The first three writes land within the buffer bounds. So far everything is safe. R0 is now pointing at address 203 — the return address location.

  3. 3

    The overflow write

    Do this: Step through the fourth iteration of the loop

    Watch for: Value 4 overwrites address 203 — the return address changes from 15 to 4

    The fourth STORE writes to address 203, overwriting the return address with 4. The CPU has no way to know this address was special — STORE just writes to whatever address R0 holds. The return address is now corrupted.

  4. 4

    Verify the corruption

    Do this: Step through the LOAD and HALT

    Watch for: R5 = 4 (not 15). The return address was overwritten

    If this function executed RET, the CPU would jump to address 4 instead of address 15. In a real program, this could crash the process or, worse, let an attacker redirect execution to code they control.

Objectives

  • •Watch values 1, 2, 3 fill the buffer at addresses 200, 201, 202
  • •See the 4th write land at address 203 - overwriting the return address
  • •Verify R5 = 4 (corrupted) instead of 15 (original return address)
  • •Understand that one extra loop iteration caused the overflow

Hints

  • The loop counter is 4 but the buffer only has 3 slots - watch what happens on iteration 4