Section 15.35

Algorithmic complexity attack

Definition

An algorithmic complexity attack is a denial-of-service §1.28 attack in which the attacker sends input chosen to make a program’s algorithm §14.1 do its worst-case amount of work. A small or moderate input then costs far more processor time or memory than ordinary input of the same size. No memory-safety rule is broken: every access stays inside its buffer. The harm is to availability §1.10, the property that a system keeps serving its legitimate users. Crosby and Wallach named and demonstrated the attack in 2003, against hash tables §14.7 [241].

The idea

The running time of an algorithm depends on how large its input is and on which input of that size it receives. Developers measure typical inputs, so the typical cost is the cost they see. An attacker instead picks, among all the inputs a program accepts, the one that costs the most. When the worst case is far worse than the typical case, one request occupies a processor far longer than any ordinary request, and a few such requests keep a server busy.

A concrete case. A web server stores the named fields of each submitted form in a hash table: a structure that places each key in one of many slots, called buckets, chosen by a hash function of the key. Ordinary names spread out, about one per bucket. If the attacker sends names that all have the same hash value, they all land in one bucket, each new name is compared with every name already there, and the work grows with the square of the number of names. In December 2011, researchers showed that several widely used web application platforms and programming languages, Python §12.23 among them, were open to exactly this attack, because their hash functions were public and had no secret part [242]. That observation is the core of the main defense: with a secret key in the hash function, the attacker cannot predict which inputs collide.

How it works

The attacker finds an operation whose cost depends on the content of the input, not only on its length, constructs an input of acceptable length that drives the operation to its worst case, and sends that input, repeatedly if needed. The classic sources of such costs are these.

Defenses act on the algorithm, the input and the work done.

  1. State the worst case of every operation on attacker-supplied data, in every dimension that drives it: item count, nesting depth, key length, pattern length, expanded size. Byte length alone is not such a dimension.

  2. Choose algorithms with an acceptable worst case: a hash function with a secret random key, such as SipHash, which Python and Rust use for this purpose; a balanced search tree §14.10, whose operations need a number of comparisons that grows with the logarithm of the number of keys; a regular expression engine that runs in time linear in the length of the text, such as RE2, a library published by Google, or the regular expression libraries of Go and Rust.

  3. Limit the dimensions that drive cost: the number of fields, the nesting depth, the number of entity expansions, the decompressed size.

  4. Bound the work: give each request a step budget §2.32 or a deadline, and stop the work and release its memory when it is exceeded.

  5. Limit the rate of expensive requests per client.

Worked example

Java’s built-in hash function for strings shows how cheaply colliding keys are made. For a string of mm characters s0s1…sm−1s_0 s_1 \dots s_{m-1}, each taken as its numeric character code, it computes h=s0⋅31m−1+s1⋅31m−2+…+sm−1h = s_0 \cdot 31^{m-1} + s_1 \cdot 31^{m-2} + \dots + s_{m-1} in 32-bit integers that wrap around on overflow. The codes of A, a and B are 65, 97 and 66, so h(𝙰𝚊)=65×31+97=2112h(\texttt{Aa}) = 65 \times 31 + 97 = 2112 and h(𝙱𝙱)=66×31+66=2112h(\texttt{BB}) = 66 \times 31 + 66 = 2112. For a string of two such blocks b1b2b_1 b_2, the formula gives h(b1b2)=h(b1)×312+h(b2)h(b_1 b_2) = h(b_1) \times 31^2 + h(b_2), so AaAa, AaBB, BBAa and BBBB all have the hash 2112×961+2112=2,031,7442112 \times 961 + 2112 = 2{,}031{,}744. With 16 blocks, the same reasoning gives 216=65,5362^{16} = 65{,}536 different strings of 32 characters with one hash value.

Insert these keys into a hash table that chains colliding keys. Their full hash values are equal, so they share one bucket however many buckets the table has, and enlarging the table does not help. The ii-th key is compared with the i−1i - 1 keys already in the chain, so the total is 0+1+…+65,535=65,536×65,5352=2,147,450,8800 + 1 + \dots + 65{,}535 = \frac{65{,}536 \times 65{,}535}{2} = 2{,}147{,}450{,}880 comparisons, against about 65,536 for keys spread evenly over the buckets. The input is 65,536×32=2,097,15265{,}536 \times 32 = 2{,}097{,}152 bytes, 2 MiB, an ordinary size for an upload. Since version 8, Java’s own HashMap turns a long chain into a balanced tree, which limits this particular attack.

Backtracking grows faster still. The pattern ^(a+)+$ asks for one or more groups of letters a that fill the whole text, where ^ marks the start of the text and $ its end. Give it nn letters a followed by !, so no match exists. A run of kk letters can be cut into groups in 2k−12^{k-1} ways, because each of the k−1k - 1 gaps between neighboring letters either is a cut or is not. A simple backtracking matcher, such as Python’s, tries every way of cutting every run that starts at the beginning, k=1,…,nk = 1, \dots, n, before it reports failure: 20+21+…+2n−1=2n−12^0 + 2^1 + \dots + 2^{n-1} = 2^n - 1 attempts. For n=30n = 30 that is 1,073,741,8231{,}073{,}741{,}823 attempts, caused by 31 bytes of input, and each further letter doubles the count.

Stated precisely

Let c(x)c(x) be the cost of processing input xx, in a stated unit such as comparisons, processor steps or bytes of memory, and let |x||x| be the length of xx in bytes. The worst-case cost for inputs of at most nn bytes is W(n)=max|x|≤nc(x)W(n) = \max_{|x| \le n} c(x), and the average cost A(n)A(n) is the mean of c(x)c(x) over a stated distribution of typical inputs of at most nn bytes. A program is open to an algorithmic complexity attack at size nn when it accepts inputs of nn bytes, W(n)W(n) is much larger than A(n)A(n), and an attacker can construct an input whose cost is close to W(n)W(n).

A defense is a contract over every admitted input xx: c(x)≤Bc(x) \le B, where BB is the cost the system can afford for one request. With at most rr requests admitted per second, the total cost per second is then at most rBr B. The contract assumes three things: the cost unit counts all the dominant work, memory included; the attacker knows the algorithm, the code and every public constant, and only secret keys are hidden; and, for a keyed hash, the key is random and secret and the keyed hash behaves as a pseudorandom function §15.1, a keyed function whose outputs look random to anyone without the key.

Limits and common mistakes

Memory safety bounds where a program reads and writes, not how long it runs. A limit on input length does not bound the cost either: an input of nn items or letters can cost on the order of n2n^2 or 2n2^n steps.

Tests with ordinary inputs and benchmarks of average speed do not reveal the problem, because the attacker supplies the worst case.

A cryptographic hash function §5.1 without a key does not prevent bucket collisions. The table uses only a few bits of the hash: with 2162^{16} buckets, about 65,536 random trials find each further key for a chosen bucket, whatever the collision resistance §5.5 of the full hash. A secret key does prevent it, but only while the key is random and stays secret.

A timeout §9.34 that merely stops waiting leaves the processor busy while the computation continues; the work must be canceled.

Rate limiting §9.38 bounds the number of requests, not the cost of each, and many attacking clients together evade limits set per client.

A limit on output size does not bound processor time, and a limit on processor time does not bound memory. State which resource each control bounds, and on which code paths.

Linear-time regular expression engines do not support backreferences, which require a repeat of text matched earlier in the same pattern.

Questions answered

Can a small input induce quadratic parsing, catastrophic regex backtracking, or hash collisions?

Yes. The cost of all three depends on the content of the input, not only on its length, so a small input can trigger each of them.

No memory-safety bug is needed, and a limit on input length does not prevent any of them. The defenses are to state each operation’s worst-case cost, to choose algorithms whose worst case is acceptable (a hash function with a secret key, a balanced tree, a linear-time regular expression engine), to limit item counts and nesting depth, and to bound each request’s work with a budget that really stops the work.

Related entries: Worst-case versus average-case complexity (§14.3), Regular expression (§12.44), XML parsing (§15.31), zlib module (§15.32), Timeout versus cancellation (§13.25), Asymptotic complexity (§14.2), Hash table (§14.7), Hash collision versus cryptographic collision (§14.8), Denial of service (DoS) (§1.28), Rate limiting (§9.38). Further reading: [146, 241, 242].