Integer Overflow / Underflow

Detects arithmetic operations that can overflow or underflow integer bounds, leading to heap overflows, logic bypasses, or unexpected behavior.

zakirkun 11adf12 2 files · 3.2 KB Updated

File contents

Integer Overflow / Underflow

Overview

Integer overflow occurs when arithmetic results exceed the data type's maximum value. In C/C++:

  • Signed integer overflow is undefined behavior (UB) — compilers can optimize it away
  • Unsigned overflow wraps around (e.g., 0 - 1 = UINT_MAX)

Common exploitable scenarios:

  • Heap overflow: malloc(user_len + 4) — if user_len = UINT_MAX - 3, result wraps to small allocation
  • Loop condition bypass: for (i = 0; i <= MAX_INT; i++) — wraps to 0
  • Signed/unsigned mismatch: if (len < MAX) where len is unsigned and MAX is signed

Remediation

  • Validate input sizes before arithmetic operations
  • Use safe integer libraries (SafeInt, checked_arithmetic)
  • Use compiler sanitizers (UBSan, ASan) to detect overflow

zakirkun/ice-tea/tree/main/skills/memory/integer-overflow commit 11adf12621

Frequently asked questions

npx skillmds@latest add zakirkun/integer-overflow-underflow