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.
Quadratic parsing. For each new item, the parser does work proportional to all the earlier items, for example by checking each new key against every earlier key in a list. Then items cost steps.
Catastrophic backtracking. A regular expression §12.44 is a pattern that describes a set of strings;
a+means one or more letters a. A backtracking matcher, as in Perl, Java, JavaScript and Python’sremodule, tries one way of matching and, when that way fails, goes back and tries the next. Some patterns leave exponentially many ways of matching to try, and exploiting them is called regular expression denial of service (ReDoS).Hash collisions. Two keys collide when they fall into the same bucket. A table that keeps the keys of a bucket in a list, called a chain, compares each new key with every key in the chain, so colliding keys cost comparisons instead of about .
Size amplification. A short input describes a huge output. Examples are a decompression bomb, and the “billion laughs” document of XML (Extensible Markup Language), whose nested entities, named pieces of text, expand from under 1 kilobyte to about 3 gigabytes.
Defenses act on the algorithm, the input and the work done.
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.
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.
Limit the dimensions that drive cost: the number of fields, the nesting depth, the number of entity expansions, the decompressed size.
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.
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 characters , each taken as its numeric character code, it computes in 32-bit integers that wrap around on overflow. The codes of A, a and B are 65, 97 and 66, so and . For a string of two such blocks , the formula gives , so AaAa, AaBB, BBAa and BBBB all have the hash . With 16 blocks, the same reasoning gives 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 -th key is compared with the keys already in the chain, so the total is comparisons, against about 65,536 for keys spread evenly over the buckets. The input is 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 letters a followed by !, so no match exists. A run of letters can be cut into groups in ways, because each of the 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, , before it reports failure: attempts. For that is attempts, caused by 31 bytes of input, and each further letter doubles the count.
Stated precisely
Let be the cost of processing input , in a stated unit such as comparisons, processor steps or bytes of memory, and let be the length of in bytes. The worst-case cost for inputs of at most bytes is , and the average cost is the mean of over a stated distribution of typical inputs of at most bytes. A program is open to an algorithmic complexity attack at size when it accepts inputs of bytes, is much larger than , and an attacker can construct an input whose cost is close to .
A defense is a contract over every admitted input : , where is the cost the system can afford for one request. With at most requests admitted per second, the total cost per second is then at most . 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 items or letters can cost on the order of or 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 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.
Quadratic parsing: comparing each new item with all earlier items costs comparisons for items, so 10,000 items cost 49,995,000 comparisons.
Catastrophic backtracking: a backtracking matcher running the regular expression
^(a+)+$on 30 letters a followed by one other character makes , about , attempts before it fails.Hash collisions: when the hash function is public and has no secret key, an attacker computes keys that all fall into one bucket of a hash table, so 65,536 such keys, 2 MiB of text, cost about comparisons instead of about 65,536.
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].