Definition
Taint analysis is a program analysis that tracks which values in a program are influenced by data from designated untrusted inputs, called sources, and reports any such value that reaches a designated sensitive operation, called a sink, without first passing a check or transformation, called a sanitizer, that makes the value safe for that sink. A value influenced by a source is tainted. The analysis can follow data while the program runs, which is dynamic analysis §3.2, or reason about the code without running it, which is static analysis §3.1.
The idea
A device receives a packet, takes one byte of it as a position i, and reads table[i], where table has 4 entries numbered 0 to 3. The sender chose i. If i is 200, the read fetches memory far beyond the end of table. No single line is wrong on its own. The defect is a flow: a value that the sender controls reaches an operation that accepts only the values 0 to 3, along a path with no check in between. Many classic security defects have this shape: input reaching the text of a database query (SQL injection, where SQL stands for Structured Query Language), the text of a shell command, the format string of printf, or the length of a memory copy.
Taint analysis traces such flows the way a plumber traces water with dye: dye poured in at one inlet shows up wherever that inlet’s water arrives. The analysis marks every value that comes from a source and watches whether a marked value arrives at a sink. A sanitizer is like a filter in front of one particular tap. The picture fails in two places. A sanitizer removes nothing physical; it establishes one property that one kind of sink needs, and the same value can still be unsafe for another sink. And in a program, influence also travels through decisions: a branch on a tainted value decides what is written in each arm, though no marked value is copied.
How it works
An analyst first writes a policy with three lists.
Sources, where untrusted data enters: bytes received from a network, the contents of a file, command-line arguments, environment variables.
Sinks, operations that need their input to have a property: an array §14.5 index (within the array), a copy length (no larger than the destination), a query string (no unintended query syntax), a function pointer (one of the permitted targets).
Sanitizers: for each sink, the checks or transformations that establish the property that sink needs.
The analysis then follows the data.
Marking. Each value read from a source receives a taint label, a tag that travels with the value. The label can be a single bit, tainted or not, or a set that names the sources involved.
Explicit flows. An operation with a tainted operand gives a tainted result: if
xis tainted, so isyaftery = x + 1. Storing a tainted value taints the memory location, and loading from that location gives a tainted value. A static taint analysis needs alias analysis §3.18 here, to know which locations a store through a pointer can change.Implicit flows, if the policy includes them. After
if (x > 100) y = 1; else y = 0;the value ofyreveals whetherxexceeds 100, although no tainted value was copied intoy. Following such flows marksyas tainted too. Many practical taint tools leave implicit flows out by default, because following them marks a large part of the program as tainted.Sanitizing. When a value passes a sanitizer for a sink, the analysis records that the value now has the property that sink needs. A transforming sanitizer computes a new value that always has the property, so it clears the taint for that sink on every path through it. Examples are
x & 3, the bitwise AND ofxwith 3, which keeps only the two lowest bits and so always lies between 0 and 3, and a string in which every quote character has been escaped. A validating sanitizer is a test, such asx < 4, whose true branch is taken only when the property holds, so it clears the taint only on that branch.Reporting. At each sink, a value that is tainted and not sanitized for that sink produces a report that names the source, the sink and the path between them.
A dynamic taint tracker keeps a shadow memory, one label for each byte or word of program memory, and updates the labels as each instruction runs; every flow it reports happened in an observed run, and it misses flows that no run exercised. TaintCheck, by Newsome and Song (2005) [260], is an early tracker of this kind that detected attacks on unmodified programs. A static taint analysis reasons over all paths through the code, so it can find flows that no test exercised, and it can also report flows along paths that no execution takes.
Worked example
Let x be one byte received from the network, so x is tainted and can be any value from 0 to 255. Indexing table, which has 4 entries, is the sink; it needs an index from 0 to 3. The program is:
1 x = read_byte();
2 if (x < 4) {
3 a = table[x];
4 b = table[x + 4];
5 }
6 c = table[x];
The analysis treats the three uses of table differently.
Line 3. Line 3 runs only on the true branch of the test on line 2, where
xis 0, 1, 2 or 3. Every such value is a valid index, so the test is a validating sanitizer for this sink, and no report is made.Line 4. The index
x + 4is a new value. Forxequal to 0, 1, 2 and 3 it is , , and , and none of these is a valid index. The test on line 2 constrainedx, notx + 4. A correct analysis reports this flow; an analysis that copied the sanitized status ofxto every value computed fromxwould miss it.Line 6. Line 6 runs after the two paths from line 2 meet again. On the path that skips lines 3 to 5,
xis any value from 4 to 255; withxequal to 9, line 6 readstable[9], outside the 4 entries. The analysis must report this flow. An analysis that merges the facts of both paths at line 6 has to drop the sanitized status there, because the status held on only one of the two paths.
Replacing the test on line 2 by the transforming sanitizer x = x & 3; makes line 6 safe on every path. In binary, 9 is 00001001 and 3 is 00000011; their bitwise AND is 00000001, which is 1. The value 200 is 11001000, and its AND with 3 is 00000000, which is 0. The index now always lies between 0 and 3, but the input 9 has become 1: the transformation made the access safe, not the program correct.
Stated precisely
Explicit-flow taint is defined by three rules: a value read from a source is tainted; the result of an operation with a tainted operand is tainted; and a value loaded from a memory location is tainted when the last store to that location wrote a tainted value. A static taint analysis is sound for explicit flows when it reports a source-to-sink flow wherever, in some execution it models, a value tainted by these rules reaches a sink and no sanitizer for that sink has established the sink’s property for that very value.
A validating check with the condition sanitizes the variable for a sink that needs the property only when implies for every value that can take, and only at program points where two conditions hold: every path from the program start to passes through the true edge of the check, and no path from the check to changes . In the example, is over the values 0 to 255, is , and implies . Line 3 meets both conditions; line 6 fails the first.
Taint approximates dependence. A value at a point depends on a source when two executions that differ only in the data the source supplies give different values at . A program in which no value at a sink depends on a source in this sense satisfies noninterference §1.42, the formal no-flow property. Explicit-flow taint misses dependence through branches (implicit flows) and through how long an operation takes, which is a side channel §1.29, so a clean explicit-flow report does not establish noninterference.
Each guarantee rests on five assumptions: the policy lists every source and every sink; each listed sanitizer really establishes its sink’s property; library functions whose code the analysis does not see have correct models of how they pass taint; the alias analysis is sound; and the value that was checked is the value that is used.
Limits and common mistakes
Tainted describes where a value came from, not whether it is harmful. Most tainted values are harmless, and an untainted value, such as a constant or a setting that a trusted party wrote, can still be wrong.
Overtainting marks values that the source cannot really influence and produces false alarms §1.22; following every implicit flow and labeling a whole buffer for one tainted byte are common causes. Undertainting misses real flows; common causes are a missing source, a library function without a model, ignored implicit flows, and table lookups: y = t[x] with a tainted x and an untainted table t gives an untainted y unless the tool also follows taint through addresses, which many tools do not do by default.
A sanitizer serves one sink. The test x < 4 makes x safe as an index into a 4-entry table; it says nothing about whether the sender may use the record at that index, which is a question of authorization §1.7. Escaping a string for a database query does not make it safe inside a shell command.
The type of the comparison matters. If x is a signed integer, the test x < 4 also admits , which is not a valid index. An index check needs , or an unsigned comparison §2.12 in place of the signed comparison; the unsigned comparison reads as a large positive number, which fails the test.
The checked value must be the used value. If a program checks a copy of an input and then reads the input again from a buffer that the sender can still change, the check does not cover the second read. This is a time-of-check to time-of-use §9.26 (TOCTOU) error, and taint analysis detects it only if it treats the second read as a new tainted value.
Taint can protect either of two properties. With untrusted inputs as sources and trusted operations as sinks, it protects integrity §1.9. With secret data as sources and public outputs as sinks, the same machinery protects confidentiality §1.8. Deliberately approving a tainted value for a trusted use is called integrity endorsement §1.49; deliberately approving a secret value for release is called declassification §1.43. Both are exceptions that the policy must state and justify.
Questions answered
Does sanitization remove taint only under specific branch conditions?
It depends on the kind of sanitizer. A validating sanitizer, a test such as if (x < 4), removes taint only on the branch where the test succeeded. A transforming sanitizer, which computes a new safe value such as x & 3 (always between 0 and 3), removes it on every path that runs the transformation. In both cases the removal holds only for the property established (a range check on an index does not make the value safe in a database query), only for the value checked (x + 4 computed after the check is a new, unchecked value), and only until that value changes or is read again from memory that someone else can change.
For a validating check the branch matters as follows. A tainted byte x can be 0 to 255, and the program reads table[x] from a 4-entry table. Inside the true branch of if (x < 4), x is 0 to 3 and the access is safe. On the false branch, x is still 4 to 255, so after the two branches meet, x can again be any value from 0 to 255, and an access there must be reported. An analysis that merges facts where paths meet must drop the sanitized status at that point; path sensitivity §3.11, keeping the facts of different paths apart, keeps the status exactly on the paths where the test succeeded.
Related entries: Path sensitivity (§3.11), Program slicing (§3.20), Deserialization (§4.29), Alias analysis (§3.18), Trust boundary (§1.3), Noninterference (§1.42), Declassification (§1.43), Integrity endorsement: when untrusted input may influence trusted state (§1.49). Further reading: [260].