Skip to main content
Be Bitwise

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.

Integer overflow occurs when an arithmetic operation produces a result too large for the register's bit width. This CPU's registers hold 8 bits, so the largest value is 255 and 250 + 10 = 260 wraps round to 260 - 256 = 4. The CPU records the wrap in the carry flag, but it doesn't stop or warn; it's up to the program to check. If code uses the wrapped value in a security check (is this size small enough?), the tiny result passes a check that the true result would fail. This is how integer overflows become security vulnerabilities.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Load an attacker-controlled length

    Do this: Step through MOV R0, #250

    Watch for: R0 = 250 (0xFA), close to the 8-bit maximum of 255

    R0 holds a length that came from the attacker, say a field in a network packet. 250 fits comfortably in 8 bits. In real programs the same bug appears near 0xFFFFFFFF in 32-bit sizes.

  2. 2

    Add the header and overflow

    Do this: Step through ADD R0, #10

    Watch for: R0 = 4, not 260. The carry flag (C) is set

    250 + 10 = 260, which needs 9 bits. The register keeps only the low 8, so 260 becomes 4 (0x104 loses its top bit). The carry flag records that a bit fell off the top, but nothing forces the program to look at it.

  3. 3

    Run the size check

    Do this: Step through MOV R1, R0 / AND R1, #0xF0 / JNZ toobig

    Watch for: R1 = 0, the zero flag is set, and JNZ does not jump

    The buffer holds 15 bytes, so the check asks whether any of bits 4 to 7 are set: AND with 0xF0 keeps only those bits. For the real total of 260 the answer would be yes. For the wrapped value 4 the answer is no, R1 becomes 0, and JNZ falls through to the 'allowed' path.

  4. 4

    Confirm the bypass

    Do this: Step through MOV R2, #1 and HALT

    Watch for: R2 = 1, meaning the check passed

    R2 = 1 confirms the bypass. The program would now allocate a 4-byte buffer and copy 250 attacker-supplied bytes into it: an integer overflow turned into a heap overflow.

Objectives

  • •See R0 go from 250 to 4 after ADD - the 8-bit value wrapped around
  • •Notice the carry flag is set: the CPU saw the overflow but didn't stop
  • •Watch the AND check pass because 4 has none of bits 4 to 7 set
  • •Verify R2 = 1, meaning the size check was bypassed

Hints

  • 8 bits hold 0 to 255. 250 + 10 = 260 = 0x104, and dropping the 9th bit leaves 0x04. Try MOV R0, #20 instead: the honest total of 30 fails the check and R2 = 0.