Skip to content
Pwnversity
PWN-INTEGERintermediate · 6 min · upd. 2026-10

Integer Overflow and Integer Bugs

CWE-190

Integers in C have a fixed size (32 or 64 bits) and a limited range. When an operation produces a value outside that range, it “wraps around” (overflow/underflow), and if a signed number is treated as unsigned (or vice versa), its value changes drastically (signedness). These bugs are dangerous because those integers are often used as buffer sizes or indices: a wrong calculation leads to allocating too little memory, reading/writing out of bounds, or skipping a check — and from there to a classic overflow.

The attacker controls an integer the program uses to size a buffer, index an array, or compare against a limit. The integer bug is rarely the final goal: it’s the first link that enables a memory corruption (stack/heap overflow, OOB write) that is then exploited. Authorized lab/CTF material.

  • Integer overflow — x + y exceeds the maximum and wraps to a small value. In 32-bit unsigned: 0xFFFFFFFF + 1 = 0.
  • Integer underflow — 0 - 1 in unsigned → 0xFFFFFFFF (huge).
  • Signedness — a negative int treated as unsigned: -1 interpreted as unsigned = 4294967295.
  • Truncation — assigning an int (32) to a short (16) loses the high bits.
  • Multiplication overflow — len * sizeof(x) overflows and allocates less than expected.

The danger isn’t the integer itself but what it’s used for. Three classic patterns:

// 1) controlled allocation size -> heap overflow
int n = user_input; // the attacker feeds a value that overflows
char *buf = malloc(n * 8); // n*8 overflows -> malloc allocates little
// then n elements are written -> heap overflow
// 2) skip a bounds check (signedness)
if (index < MAX) { array[index] = x; } // a negative index (signed) passes the check
// but array[index] with a negative index writes OUTSIDE the array
// 3) underflow in a size -> gigantic copy
size_t len = header_len - 20; // if header_len < 20, len = huge -> massive memcpy

The key pattern: an attacker-controlled integer feeding a malloc, a memcpy, an array index, or an if (x < limit) check.

flowchart TD
    A["attacker-controlled integer"] --> B{"used for what?"}
    B -->|"malloc(n*k)"| C["product overflows → small allocation"]
    B -->|"signed index"| D["negative passes 'index < MAX' → OOB"]
    B -->|"unsigned subtraction"| E["underflow → huge size"]
    C --> F["heap overflow"]
    D --> G["out-of-bounds write"]
    E --> H["massive memcpy"]
    F --> I["control of the flow (RIP)"]
    G --> I
    H --> I
  • In reversing/code: arithmetic on sizes/indices just before malloc, memcpy, read, or a loop.
  • Bounds checks using signed types where they should be unsigned.
  • Fuzzing with extreme values: 0, -1, 0xFFFFFFFF, INT_MAX, INT_MIN, large sizes.

Exploitation (lab): from integer bug to flow control

Section titled “Exploitation (lab): from integer bug to flow control”
  1. Find the controlled integer that feeds a size/index/check.
  2. Choose a value that produces the desired effect:
    • overflow → small allocation → heap/stack overflow (see Stack / Heap).
    • signedness/negative → skip the limit → out-of-bounds write.
    • underflow → huge size → massive copy.
  3. Turn the resulting memory corruption into control of the flow, as in a classic overflow.
from pwn import *
context.binary = './vuln'
io = process('./vuln')
# the program asks for an "element count" and does malloc(n*8)
io.sendlineafter(b'count: ', str(0x20000001).encode()) # 0x20000001*8 overflows 32 bits -> 8
# now the real buffer is tiny, but the program thinks 0x20000001 elements fit
io.sendafter(b'data: ', b'A'*200) # heap overflow
io.interactive()

It’s common for the integer bug to be the link enabling the classic overflow; from there you apply the corresponding technique.

  • Correct range checks: validate before operating; use unsigned types for sizes (size_t) and verify upper and lower bounds.
  • Overflow detection: safe functions (__builtin_mul_overflow, C23’s ckd_add), -ftrapv, and UBSan (-fsanitize=integer) in development.
  • Don’t compute sizes with arithmetic without checking for overflow (if (n > MAX/8) error; before n*8).
  • Fuzzing with boundary values; code review focused on size arithmetic.
  • Identify controlled integers feeding sizes/indices/checks
  • Test boundary values (0, -1, 0xFFFFFFFF, INT_MAX/MIN)
  • Overflow in a size calculation → small allocation → overflow
  • Signedness/negative → skip a bounds check
  • Underflow → huge size → massive copy
  • Chain with Stack / Heap overflow
  • Turn into control of the flow
  • Blue: range validation, size_t, UBSan?