|
6 | 6 | * the information from the source is preserved at the sink. For example, taint |
7 | 7 | * propagates from `x` to `x + 100`, but it does not propagate from `x` to `x > |
8 | 8 | * 100` since we consider a single bit of information to be too little. |
| 9 | + * |
| 10 | + * To use global (interprocedural) taint tracking, extend the class |
| 11 | + * `TaintTracking::Configuration` as documented on that class. To use local |
| 12 | + * (intraprocedural) taint tracking between expressions, call |
| 13 | + * `TaintTracking::localExprTaint`. For more general cases of local taint |
| 14 | + * tracking, call `TaintTracking::localTaint` or |
| 15 | + * `TaintTracking::localTaintStep` with arguments of type `DataFlow::Node`. |
9 | 16 | */ |
10 | 17 |
|
11 | 18 | import semmle.code.cpp.ir.dataflow.DataFlow |
12 | 19 | import semmle.code.cpp.ir.dataflow.DataFlow2 |
13 | | -private import semmle.code.cpp.ir.IR |
14 | 20 |
|
15 | 21 | module TaintTracking { |
16 | | - /** |
17 | | - * A configuration of interprocedural taint tracking analysis. This defines |
18 | | - * sources, sinks, and any other configurable aspect of the analysis. Each |
19 | | - * use of the taint tracking library must define its own unique extension of |
20 | | - * this abstract class. |
21 | | - * |
22 | | - * A taint-tracking configuration is a special data flow configuration |
23 | | - * (`DataFlow::Configuration`) that allows for flow through nodes that do not |
24 | | - * necessarily preserve values but are still relevant from a taint-tracking |
25 | | - * perspective. (For example, string concatenation, where one of the operands |
26 | | - * is tainted.) |
27 | | - * |
28 | | - * To create a configuration, extend this class with a subclass whose |
29 | | - * characteristic predicate is a unique singleton string. For example, write |
30 | | - * |
31 | | - * ``` |
32 | | - * class MyAnalysisConfiguration extends TaintTracking::Configuration { |
33 | | - * MyAnalysisConfiguration() { this = "MyAnalysisConfiguration" } |
34 | | - * // Override `isSource` and `isSink`. |
35 | | - * // Optionally override `isSanitizer`. |
36 | | - * // Optionally override `isAdditionalTaintStep`. |
37 | | - * } |
38 | | - * ``` |
39 | | - * |
40 | | - * Then, to query whether there is flow between some `source` and `sink`, |
41 | | - * write |
42 | | - * |
43 | | - * ``` |
44 | | - * exists(MyAnalysisConfiguration cfg | cfg.hasFlow(source, sink)) |
45 | | - * ``` |
46 | | - * |
47 | | - * Multiple configurations can coexist, but it is unsupported to depend on a |
48 | | - * `TaintTracking::Configuration` or a `DataFlow::Configuration` in the |
49 | | - * overridden predicates that define sources, sinks, or additional steps. |
50 | | - * Instead, the dependency should go to a `TaintTracking::Configuration2` or |
51 | | - * a `DataFlow{2,3,4}::Configuration`. |
52 | | - */ |
53 | | - abstract class Configuration extends DataFlow::Configuration { |
54 | | - bindingset[this] |
55 | | - Configuration() { any() } |
56 | | - |
57 | | - /** Holds if `source` is a taint source. */ |
58 | | - // overridden to provide taint-tracking specific qldoc |
59 | | - abstract override predicate isSource(DataFlow::Node source); |
60 | | - |
61 | | - /** Holds if `sink` is a taint sink. */ |
62 | | - // overridden to provide taint-tracking specific qldoc |
63 | | - abstract override predicate isSink(DataFlow::Node sink); |
64 | | - |
65 | | - /** |
66 | | - * Holds if taint should not flow into `node`. |
67 | | - */ |
68 | | - predicate isSanitizer(DataFlow::Node node) { none() } |
69 | | - |
70 | | - /** |
71 | | - * Holds if the additional taint propagation step |
72 | | - * from `source` to `target` must be taken into account in the analysis. |
73 | | - * This step will only be followed if `target` is not in the `isSanitizer` |
74 | | - * predicate. |
75 | | - */ |
76 | | - predicate isAdditionalTaintStep(DataFlow::Node source, DataFlow::Node target) { none() } |
77 | | - |
78 | | - final override predicate isBarrier(DataFlow::Node node) { isSanitizer(node) } |
79 | | - |
80 | | - final override predicate isAdditionalFlowStep(DataFlow::Node source, DataFlow::Node target) { |
81 | | - this.isAdditionalTaintStep(source, target) |
82 | | - or |
83 | | - localTaintStep(source, target) |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * A taint-tracking configuration that is backed by the `DataFlow2` library |
89 | | - * instead of `DataFlow`. Use this class when taint-tracking configurations |
90 | | - * or data-flow configurations must depend on each other. |
91 | | - * |
92 | | - * See `TaintTracking::Configuration` for the full documentation. |
93 | | - */ |
94 | | - abstract class Configuration2 extends DataFlow2::Configuration { |
95 | | - bindingset[this] |
96 | | - Configuration2() { any() } |
97 | | - |
98 | | - /** Holds if `source` is a taint source. */ |
99 | | - // overridden to provide taint-tracking specific qldoc |
100 | | - abstract override predicate isSource(DataFlow::Node source); |
101 | | - |
102 | | - /** Holds if `sink` is a taint sink. */ |
103 | | - // overridden to provide taint-tracking specific qldoc |
104 | | - abstract override predicate isSink(DataFlow::Node sink); |
105 | | - |
106 | | - /** |
107 | | - * Holds if taint should not flow into `node`. |
108 | | - */ |
109 | | - predicate isSanitizer(DataFlow::Node node) { none() } |
110 | | - |
111 | | - /** |
112 | | - * Holds if the additional taint propagation step |
113 | | - * from `source` to `target` must be taken into account in the analysis. |
114 | | - * This step will only be followed if `target` is not in the `isSanitizer` |
115 | | - * predicate. |
116 | | - */ |
117 | | - predicate isAdditionalTaintStep(DataFlow::Node source, DataFlow::Node target) { none() } |
118 | | - |
119 | | - final override predicate isBarrier(DataFlow::Node node) { isSanitizer(node) } |
120 | | - |
121 | | - final override predicate isAdditionalFlowStep(DataFlow::Node source, DataFlow::Node target) { |
122 | | - this.isAdditionalTaintStep(source, target) |
123 | | - or |
124 | | - localTaintStep(source, target) |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - /** |
129 | | - * Holds if taint propagates from `nodeFrom` to `nodeTo` in exactly one local |
130 | | - * (intra-procedural) step. |
131 | | - */ |
132 | | - predicate localTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
133 | | - // Taint can flow into using ordinary data flow. |
134 | | - DataFlow::localFlowStep(nodeFrom, nodeTo) |
135 | | - or |
136 | | - localInstructionTaintStep(nodeFrom.asInstruction(), nodeTo.asInstruction()) |
137 | | - } |
138 | | - |
139 | | - /** |
140 | | - * Holds if taint propagates from `nodeFrom` to `nodeTo` in exactly one local |
141 | | - * (intra-procedural) step. |
142 | | - */ |
143 | | - private predicate localInstructionTaintStep(Instruction nodeFrom, Instruction nodeTo) { |
144 | | - // Taint can flow through expressions that alter the value but preserve |
145 | | - // more than one bit of it _or_ expressions that follow data through |
146 | | - // pointer indirections. |
147 | | - nodeTo.getAnOperand().getAnyDef() = nodeFrom and |
148 | | - ( |
149 | | - nodeTo instanceof ArithmeticInstruction |
150 | | - or |
151 | | - nodeTo instanceof BitwiseInstruction |
152 | | - or |
153 | | - nodeTo instanceof PointerArithmeticInstruction |
154 | | - or |
155 | | - nodeTo instanceof FieldAddressInstruction |
156 | | - or |
157 | | - // The `CopyInstruction` case is also present in non-taint data flow, but |
158 | | - // that uses `getDef` rather than `getAnyDef`. For taint, we want flow |
159 | | - // from a definition of `myStruct` to a `myStruct.myField` expression. |
160 | | - nodeTo instanceof CopyInstruction |
161 | | - ) |
162 | | - or |
163 | | - nodeTo.(LoadInstruction).getSourceAddress() = nodeFrom |
164 | | - } |
| 22 | + import semmle.code.cpp.ir.dataflow.internal.tainttracking1.TaintTrackingImpl |
| 23 | + private import semmle.code.cpp.ir.dataflow.TaintTracking2 |
165 | 24 |
|
166 | 25 | /** |
167 | | - * Holds if taint may propagate from `source` to `sink` in zero or more local |
168 | | - * (intra-procedural) steps. |
| 26 | + * DEPRECATED: Use TaintTracking2::Configuration instead. |
169 | 27 | */ |
170 | | - predicate localTaint(DataFlow::Node source, DataFlow::Node sink) { localTaintStep*(source, sink) } |
| 28 | + deprecated class Configuration2 = TaintTracking2::Configuration; |
171 | 29 | } |
0 commit comments