Integer Overflow & Underflow
Integer Overflows & Type Confusion · Exploitation Fundamentals track
5 min read
In this module
When Numbers Wrap Around
In Binary & Number Systems, you learned that an 8-bit register can hold values from 0 to 255. But what happens when you try to go beyond that range? The answer is deceptively simple - and dangerously exploitable.
Add 1 to 255 in an 8-bit register: the result isn't 256. The value wraps around to 0. That's integer overflow. Go the other direction — subtract 1 from 0 — and you get 255. That's integer underflow. The CPU doesn't raise an error. It discards the carry bit and keeps going as if nothing happened.
Why Wrapping Is Dangerous
Consider a program that receives a length value from user input and uses it to allocate a buffer. The program adds a small amount (say, 4 bytes for a header) to the user-provided length before allocating:
The size calculation overflows, the program allocates a tiny buffer, and then copies the full data into it. This is how an integer overflow leads directly to a buffer overflow - the same class of vulnerability you studied in Stack-Based Buffer Overflows.
Integer overflow is especially dangerous because the CPU treats it as normal arithmetic. There is no exception, no error message, and no crash - just a silently wrong value. The program continues executing with the corrupted result, which can have cascading effects far from the original arithmetic.
Seeing It in Action
The simulator below runs the same mistake on our 8-bit teaching CPU. It builds the largest possible length, 255, by flipping every bit of zero. It keeps a copy of that length for the copy step, then adds a 4-byte header. Step through and watch R0 land on 3 while R1 still says 255: the program would allocate 3 bytes and copy 255 into them.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Look at the flags after the ADD: the carry flag is set. The CPU did notice the overflow. It just leaves it to the program to check, and this program never does.
Worked Example: The Allocation Size Bug in C
Real code makes the same mistake with bigger numbers. A file format says "here come count records, each 16 bytes", and the parser does this:
uint32_t count = read_u32(file); /* attacker controls this */uint32_t total = count * 16; /* 32-bit multiply */struct record *recs = malloc(total);for (uint32_t i = 0; i < count; i++) read_record(file, &recs[i]); /* writes count records */Send count = 0x10000001. The true product is 0x100000010, which needs 33 bits. The top bit falls off, leaving total = 0x10: 16 bytes. malloc happily returns 16 bytes, and the loop then tries to write 268 million records into space for one. That's a heap overflow controlled by a single field in the file.
There are three standard fixes:
- Check before multiplying: reject
count > UINT32_MAX / 16. - Let the compiler check:
if (__builtin_mul_overflow(count, 16, &total)) reject();(gcc and Clang). - Use an API that checks for you:
calloc(count, 16)is required to fail if the multiplication overflows.
Underflow bites the same way in the other direction. size_t body = len - HEADER_SIZE; with a packet shorter than the header gives a gigantic body, and every copy that trusts it reads far past the packet.
Predict: in the code above, what does count = 0x20000000 make total, and how many records fit?
total is 0, and no records fit. 0x20000000 × 16 = 0x200000000, which is exactly 2³³: the low 32 bits are all zero. malloc(0) is allowed to return a valid but tiny (or null) pointer, and the very first read_record already writes out of bounds.
Historical Impact
Integer overflow vulnerabilities have surfaced in virtually every major OS and application. They're particularly thick in code that parses file formats or network protocols, where length fields arrive from untrusted sources and feed directly into allocation calculations. The wrapped number does the damage when it goes on to drive a security-critical decision: how much memory to allocate, how many bytes to copy.
Defensive Thinking
Safe code checks for overflow before performing the arithmetic. If adding two values would exceed the maximum, the operation should be rejected rather than allowed to wrap. Many modern languages and compilers offer overflow-checked arithmetic for exactly this reason, and sanitisers such as -fsanitize=integer (Clang) or UBSan's signed-overflow checks will catch many of these during testing.
Try the Simulations
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.
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.