Skip to content

Commit 8b926f6

Browse files
authored
Merge pull request #7873 from RasmusWL/fix-attribute-taint
Python: Fix attribute taint
2 parents a8bfeba + 3e01816 commit 8b926f6

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed taint propagation for attribute assignment. In the assignment `x.foo = tainted` we no longer treat the entire object `x` as tainted, just because the attribute `foo` contains tainted data. This leads to slightly fewer false positives.

python/ql/lib/semmle/python/dataflow/new/internal/TaintTrackingPrivate.qll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,25 @@ predicate stringManipulation(DataFlow::CfgNode nodeFrom, DataFlow::CfgNode nodeT
167167
*/
168168
predicate containerStep(DataFlow::CfgNode nodeFrom, DataFlow::Node nodeTo) {
169169
// construction by literal
170-
// TODO: Not limiting the content argument here feels like a BIG hack, but we currently get nothing for free :|
171-
DataFlowPrivate::storeStep(nodeFrom, _, nodeTo)
170+
//
171+
// TODO: once we have proper flow-summary modeling, we might not need this step any
172+
// longer -- but there needs to be a matching read-step for the store-step, and we
173+
// don't provide that right now.
174+
DataFlowPrivate::listStoreStep(nodeFrom, _, nodeTo)
175+
or
176+
DataFlowPrivate::setStoreStep(nodeFrom, _, nodeTo)
177+
or
178+
DataFlowPrivate::tupleStoreStep(nodeFrom, _, nodeTo)
179+
or
180+
DataFlowPrivate::dictStoreStep(nodeFrom, _, nodeTo)
181+
or
182+
// comprehension, so there is taint-flow from `x` in `[x for x in xs]` to the
183+
// resulting list of the list-comprehension.
184+
//
185+
// TODO: once we have proper flow-summary modeling, we might not need this step any
186+
// longer -- but there needs to be a matching read-step for the store-step, and we
187+
// don't provide that right now.
188+
DataFlowPrivate::comprehensionStoreStep(nodeFrom, _, nodeTo)
172189
or
173190
// constructor call
174191
exists(DataFlow::CallCfgNode call | call = nodeTo |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Add taintlib to PATH so it can be imported during runtime without any hassle
2+
import sys; import os; sys.path.append(os.path.dirname(os.path.dirname((__file__))))
3+
from taintlib import *
4+
5+
# This has no runtime impact, but allows autocomplete to work
6+
from typing import TYPE_CHECKING
7+
if TYPE_CHECKING:
8+
from ..taintlib import *
9+
10+
11+
# Actual tests
12+
13+
class Foo:
14+
def __init__(self, arg):
15+
self.arg = arg
16+
self.other_arg = "other_arg"
17+
18+
19+
def test_tainted_attr():
20+
# The following demonstrates how tainting an attribute affected the taintedness of
21+
# the object.
22+
#
23+
# Previously we would (wrongly) treat the object as tainted if we noticed a write of
24+
# a tainted value to any of its' attributes. This lead to FP, highlighted in
25+
# https://github.com/github/codeql/issues/7786
26+
27+
f = Foo(TAINTED_STRING)
28+
ensure_not_tainted(f)
29+
ensure_tainted(f.arg) # $ tainted
30+
ensure_not_tainted(f.other_arg)
31+
32+
33+
x = Foo("x")
34+
ensure_not_tainted(x, x.arg, x.other_arg)
35+
36+
x.arg = TAINTED_STRING
37+
ensure_not_tainted(x)
38+
ensure_tainted(x.arg) # $ tainted
39+
ensure_not_tainted(f.other_arg)
40+
41+
42+
b = Foo("bar")
43+
ensure_not_tainted(b, b.arg, b.other_arg)

python/ql/test/experimental/dataflow/tainttracking/defaultAdditionalTaintStep/test_with.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __exit__(self, exc_type, exc, tb):
4949
def test_with_arg():
5050
ctx = Context_arg(TAINTED_STRING)
5151
with ctx as tainted:
52-
ensure_tainted(tainted) # $ tainted
52+
ensure_tainted(tainted) # $ MISSING: tainted
5353

5454

5555

0 commit comments

Comments
 (0)