Skip to content

Commit 97c98f2

Browse files
committed
Python taint-tracking: Support iterables of taint.
1 parent 83ec5f1 commit 97c98f2

File tree

8 files changed

+72
-0
lines changed

8 files changed

+72
-0
lines changed

python/ql/src/semmle/python/security/TaintTracking.qll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ abstract class TaintKind extends string {
159159

160160
string repr() { result = this }
161161

162+
/** Gets the taint resulting from iterating over this kind of taint.
163+
* For example iterating over a text file produces lines. So iterating
164+
* over a tainted file would result in tainted strings
165+
*/
166+
TaintKind getTaintForIteration() { none() }
167+
162168
}
163169

164170
/** Taint kinds representing collections of other taint kind.
@@ -212,6 +218,10 @@ class SequenceKind extends CollectionKind {
212218
result = "sequence of " + itemKind
213219
}
214220

221+
override TaintKind getTaintForIteration() {
222+
result = itemKind
223+
}
224+
215225
}
216226

217227

@@ -863,6 +873,8 @@ library module TaintFlowImplementation {
863873
or
864874
call_taint_step(fromnode, totaint, tocontext, tonode)
865875
or
876+
iteration_step(fromnode, totaint, tocontext, tonode)
877+
or
866878
exists(DataFlowNode fromnodenode |
867879
fromnodenode = fromnode.getNode() and
868880
(
@@ -1067,6 +1079,13 @@ library module TaintFlowImplementation {
10671079
)
10681080
}
10691081

1082+
/** Holds if `v` is defined by a `for` statement, the definition being `defn` */
1083+
cached predicate iteration_step(TaintedNode fromnode, TrackedValue totaint, CallContext tocontext, ControlFlowNode iter) {
1084+
exists(ForNode for | for.iterates(iter, fromnode.getNode())) and
1085+
totaint = TTrackedTaint(fromnode.getTaintKind().getTaintForIteration()) and
1086+
tocontext = fromnode.getContext()
1087+
}
1088+
10701089
predicate self_init_end_transfer(EssaVariable self, CallContext callee, CallNode call, CallContext caller) {
10711090
exists(ClassValue cls, Function init |
10721091
call.getFunction().pointsTo(cls) and
@@ -1161,6 +1180,9 @@ library module TaintFlowImplementation {
11611180
tainted_with(def, context, origin)
11621181
or
11631182
tainted_exception_capture(def, context, origin)
1183+
or
1184+
tainted_iteration(def, context, origin)
1185+
11641186
}
11651187

11661188
predicate tainted_scope_entry(ScopeEntryDefinition def, CallContext context, TaintedNode origin) {
@@ -1363,6 +1385,12 @@ library module TaintFlowImplementation {
13631385
context = fromnode.getContext()
13641386
}
13651387

1388+
pragma [noinline]
1389+
private predicate tainted_iteration(IterationDefinition def, CallContext context, TaintedNode fromnode) {
1390+
def.getDefiningNode() = fromnode.getNode() and
1391+
context = fromnode.getContext()
1392+
}
1393+
13661394
/* A call that returns a copy (or similar) of the argument */
13671395
predicate copyCall(ControlFlowNode fromnode, CallNode tonode) {
13681396
tonode.getFunction().(AttrNode).getObject("copy") = fromnode

python/ql/test/library-tests/taint/general/TaintLib.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,29 @@ class FalseySource extends TaintSource {
362362

363363
}
364364

365+
class TaintIterable extends TaintKind {
365366

367+
TaintIterable() {
368+
this = "iterable.simple"
369+
}
370+
371+
override TaintKind getTaintForIteration() {
372+
result instanceof SimpleTest
373+
}
374+
375+
}
376+
377+
class TaintIterableSource extends TaintSource {
378+
379+
TaintIterableSource() {
380+
this.(NameNode).getId() = "ITERABLE_SOURCE"
381+
}
382+
383+
override predicate isSourceOf(TaintKind kind) {
384+
kind instanceof TaintIterable
385+
}
386+
387+
}
366388

367389

368390

python/ql/test/library-tests/taint/general/TestDefn.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,6 @@
180180
| test.py:197 | Pi(t_0) [true] | test.py:195 | Taint simple.test | SOURCE |
181181
| test.py:199 | ArgumentRefinement(t_3) | test.py:195 | Taint simple.test | SOURCE |
182182
| test.py:199 | Pi(t_0) [false] | test.py:195 | Taint simple.test | SOURCE |
183+
| test.py:202 | ITERABLE_SOURCE | test.py:202 | Taint iterable.simple | ITERABLE_SOURCE |
184+
| test.py:203 | IterationDefinition | test.py:203 | Taint simple.test | i |
185+
| test.py:203 | phi(i_0, i_2) | test.py:203 | Taint simple.test | i |

python/ql/test/library-tests/taint/general/TestNode.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
| Taint explicit.carrier | carrier.py:35 | x | |
9797
| Taint falsey | test.py:189 | FALSEY | |
9898
| Taint falsey | test.py:190 | t | |
99+
| Taint iterable.simple | test.py:202 | ITERABLE_SOURCE | |
100+
| Taint iterable.simple | test.py:203 | t | |
99101
| Taint paper | rockpaperscissors.py:6 | arg | rockpaperscissors.py:32 |
100102
| Taint paper | rockpaperscissors.py:9 | arg | rockpaperscissors.py:26 |
101103
| Taint paper | rockpaperscissors.py:25 | Attribute() | |
@@ -226,6 +228,9 @@
226228
| Taint simple.test | test.py:196 | t | |
227229
| Taint simple.test | test.py:197 | t | |
228230
| Taint simple.test | test.py:199 | t | |
231+
| Taint simple.test | test.py:203 | i | |
232+
| Taint simple.test | test.py:204 | i | |
233+
| Taint simple.test | test.py:205 | i | |
229234
| Taint {simple.test} | test.py:169 | Dict | |
230235
| Taint {simple.test} | test.py:171 | d | |
231236
| Taint {simple.test} | test.py:173 | y | |

python/ql/test/library-tests/taint/general/TestSource.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@
4343
| test.py:178 | SOURCE | simple.test |
4444
| test.py:189 | FALSEY | falsey |
4545
| test.py:195 | SOURCE | simple.test |
46+
| test.py:202 | ITERABLE_SOURCE | iterable.simple |

python/ql/test/library-tests/taint/general/TestStep.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
| Taint explicit.carrier | carrier.py:34 | Attribute | | --> | Taint explicit.carrier | carrier.py:35 | x | |
8484
| Taint explicit.carrier | carrier.py:35 | x | | --> | Taint simple.test | carrier.py:35 | Attribute() | |
8585
| Taint falsey | test.py:189 | FALSEY | | --> | Taint falsey | test.py:190 | t | |
86+
| Taint iterable.simple | test.py:202 | ITERABLE_SOURCE | | --> | Taint iterable.simple | test.py:203 | t | |
87+
| Taint iterable.simple | test.py:203 | t | | --> | Taint simple.test | test.py:203 | i | |
8688
| Taint paper | rockpaperscissors.py:25 | Attribute() | | --> | Taint paper | rockpaperscissors.py:26 | y | |
8789
| Taint paper | rockpaperscissors.py:26 | y | | --> | Taint paper | rockpaperscissors.py:9 | arg | rockpaperscissors.py:26 |
8890
| Taint paper | rockpaperscissors.py:30 | Attribute() | | --> | Taint paper | rockpaperscissors.py:32 | y | |
@@ -181,6 +183,8 @@
181183
| Taint simple.test | test.py:195 | SOURCE | | --> | Taint simple.test | test.py:196 | t | |
182184
| Taint simple.test | test.py:195 | SOURCE | | --> | Taint simple.test | test.py:197 | t | |
183185
| Taint simple.test | test.py:195 | SOURCE | | --> | Taint simple.test | test.py:199 | t | |
186+
| Taint simple.test | test.py:203 | i | | --> | Taint simple.test | test.py:204 | i | |
187+
| Taint simple.test | test.py:203 | i | | --> | Taint simple.test | test.py:205 | i | |
184188
| Taint {simple.test} | test.py:169 | Dict | | --> | Taint {simple.test} | test.py:171 | d | |
185189
| Taint {simple.test} | test.py:169 | Dict | | --> | Taint {simple.test} | test.py:175 | d | |
186190
| Taint {simple.test} | test.py:171 | d | | --> | Taint {simple.test} | test.py:173 | y | |

python/ql/test/library-tests/taint/general/TestVar.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,6 @@
182182
| test.py:197 | t_2 | test.py:195 | Taint simple.test | SOURCE |
183183
| test.py:199 | t_3 | test.py:195 | Taint simple.test | SOURCE |
184184
| test.py:199 | t_4 | test.py:195 | Taint simple.test | SOURCE |
185+
| test.py:202 | t_0 | test.py:202 | Taint iterable.simple | ITERABLE_SOURCE |
186+
| test.py:203 | i_1 | test.py:203 | Taint simple.test | i |
187+
| test.py:203 | i_2 | test.py:203 | Taint simple.test | i |

python/ql/test/library-tests/taint/general/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,9 @@ def flow_through_type_test_if_no_class():
198198
else:
199199
SINK(t)
200200

201+
def flow_in_iteration():
202+
t = ITERABLE_SOURCE
203+
for i in t:
204+
i
205+
return i
206+

0 commit comments

Comments
 (0)