liveBook · Manning

Numerical overflow

As explained in Hacking Cryptography

Integer overflow is the condition in which an arithmetic result exceeds the range representable by an integer type.

Integer overflow occurs when an arithmetic result exceeds the range representable by a fixed-width integer. Instead of expanding, the value wraps: an unsigned all-ones value becomes zero, while signed two’s-complement 127 becomes −128 in a byte. These unexpected values can undermine security-critical calculations. section 10.7

OpenSSH challenge-response allocation

OpenSSH 3.1 parsed attacker-controlled nresp, then allocated nresp * sizeof(char*) bytes. If that multiplication overflowed, the allocation size could be wrong while the following loop still processed the original nresp responses, creating a dangerous memory-handling failure. section 10.7

Figure 10.10 Integer overflows can be illustrated as a ring.
figure

Use the ring diagram to visualize fixed-width arithmetic wrapping around its representable range. section 10.7

Reduce overflow risk in security-sensitive implementations by:

  • Checking arithmetic assumptions and avoiding undefined behavior during mathematical operations. section 10.9
  • Testing lower-level code extensively, including maximal coverage and fuzzing, because overflow bugs can remain subtle. section 10.9
  • Treating integer overflow and type-safety failures as cryptographic implementation risks rather than ordinary arithmetic mistakes. section 10.9

How Hacking Cryptography covers it

How deep it goes
Explains wrapping, two’s-complement boundaries, and an allocation vulnerability, then stops short of broader overflow-analysis techniques.
Examples
Uses signed-byte wraparound, a ring visualization, and OpenSSH’s nresp allocation and response-processing code.
What it assumes
Assumes basic programming knowledge while establishing fixed-width representation, unsigned wrapping, and signed two’s-complement behavior.
What you can do after
Diagnose overflow-prone arithmetic in low-level security code and prioritize coverage, fuzzing, and undefined-behavior avoidance.
How it connects
Connects integer overflow with type safety, fixed-width integers, signed integers, two’s complement, memory safety, and cryptographic implementations.

Also explained in

Compare how 7 books explain numerical overflow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.