Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 782311f

Browse files
committed
Python: Update taint-tracking to use new points-to API.
1 parent 3c30480 commit 782311f

5 files changed

Lines changed: 56 additions & 42 deletions

File tree

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class ModuleValue extends Value {
9494
result = this.(ModuleObjectInternal).getName()
9595
}
9696

97+
Module getScope() {
98+
result = this.(ModuleObjectInternal).getSourceModule()
99+
}
100+
97101
}
98102

99103
module Module {
@@ -128,6 +132,10 @@ class CallableValue extends Value {
128132
none()
129133
}
130134

135+
Function getScope() {
136+
result = this.(PythonFunctionObjectInternal).getScope()
137+
}
138+
131139
}
132140

133141
class ClassValue extends Value {

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

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,17 @@ abstract class TaintKind extends string {
133133
)
134134
}
135135

136+
/** DEPRECATED -- Use getType() instead */
137+
ClassObject getClass() {
138+
none()
139+
}
140+
136141
/** Gets the class of this kind of taint.
137142
* For example, if this were a kind of string taint
138143
* the `result` would be `theStrType()`.
139144
*/
140-
ClassObject getClass() {
141-
none()
145+
ClassValue getType() {
146+
result.getSource() = this.getClass()
142147
}
143148

144149
/** Gets the boolean values (may be one, neither, or both) that
@@ -194,7 +199,7 @@ class SequenceKind extends CollectionKind {
194199
mod.getOp() instanceof Mod and
195200
mod.getAnOperand() = fromnode and
196201
result = this.getItem() and
197-
result.getClass() = theStrType()
202+
result.getType() = Value::named("str")
198203
)
199204
}
200205

@@ -279,7 +284,7 @@ module DictKind {
279284
predicate flowStep(ControlFlowNode fromnode, ControlFlowNode tonode) {
280285
TaintFlowImplementation::copyCall(fromnode, tonode)
281286
or
282-
tonode.(CallNode).getFunction().refersTo(theDictType()) and
287+
tonode.(CallNode).getFunction().pointsTo(Value::named("dict")) and
283288
tonode.(CallNode).getArg(0) = fromnode
284289
}
285290

@@ -947,8 +952,8 @@ library module TaintFlowImplementation {
947952

948953
pragma [noinline]
949954
private predicate import_flow(TaintedNode fromnode, ImportExprNode tonode, CallContext tocontext, string name) {
950-
exists(ModuleObject mod |
951-
tonode.refersTo(mod) and
955+
exists(ModuleValue mod |
956+
tonode.pointsTo(mod) and
952957
module_attribute_tainted(mod, name, fromnode) and
953958
tocontext.appliesTo(tonode)
954959
)
@@ -965,9 +970,9 @@ library module TaintFlowImplementation {
965970

966971
pragma [noinline]
967972
predicate from_import_step(TaintedNode fromnode, TrackedValue totaint, CallContext tocontext, ControlFlowNode tonode) {
968-
exists(string name, ImportExprNode fmod, ModuleObject mod |
973+
exists(string name, ImportExprNode fmod, ModuleValue mod |
969974
fmod = tonode.(ImportMemberNode).getModule(name) and
970-
fmod.refersTo(mod) and
975+
fmod.pointsTo(mod) and
971976
tocontext.appliesTo(tonode) and
972977
module_attribute_tainted(mod, name, fromnode) and
973978
totaint = fromnode.getTrackedValue()
@@ -977,7 +982,7 @@ library module TaintFlowImplementation {
977982
pragma [noinline]
978983
predicate getattr_step(TaintedNode fromnode, TrackedValue totaint, CallContext tocontext, CallNode tonode) {
979984
exists(ControlFlowNode arg, string name |
980-
tonode.getFunction().refersTo(Object::builtin("getattr")) and
985+
tonode.getFunction().pointsTo(Value::named("getattr")) and
981986
arg = tonode.getArg(0) and
982987
name = tonode.getArg(1).getNode().(StrConst).getText() and
983988
arg = fromnode.getNode() and
@@ -1017,11 +1022,11 @@ library module TaintFlowImplementation {
10171022
)
10181023
}
10191024

1020-
predicate module_attribute_tainted(ModuleObject m, string name, TaintedNode origin) {
1025+
predicate module_attribute_tainted(ModuleValue m, string name, TaintedNode origin) {
10211026
exists(EssaVariable var, CallContext c |
10221027
var.getName() = name and
10231028
BaseFlow::reaches_exit(var) and
1024-
var.getScope() = m.getModule() and
1029+
var.getScope() = m.getScope() and
10251030
tainted_var(var, c, origin) and
10261031
c = TTop()
10271032
)
@@ -1062,9 +1067,9 @@ library module TaintFlowImplementation {
10621067
}
10631068

10641069
predicate self_init_end_transfer(EssaVariable self, CallContext callee, CallNode call, CallContext caller) {
1065-
exists(ClassObject cls, Function init |
1066-
PointsTo::instantiation(call, _, cls) and
1067-
init = cls.lookupAttribute("__init__").(FunctionObject).getFunction() and
1070+
exists(ClassValue cls, Function init |
1071+
call.getFunction().pointsTo(cls) and
1072+
init = cls.attr("__init__").(CallableValue).getScope() and
10681073
self.getSourceVariable().(Variable).isSelf() and self.getScope() = init
10691074
|
10701075
callee = caller.getCallee(call)
@@ -1198,10 +1203,10 @@ library module TaintFlowImplementation {
11981203
predicate parameter_step(CallContext caller, ControlFlowNode argument, CallContext callee, NameNode param) {
11991204
exists(ParameterDefinition def |
12001205
def.getDefiningNode() = param and
1201-
exists(FunctionObject func, CallNode call |
1202-
exists(int n | argument = func.getArgumentForCall(call, n) and param.getNode() = func.getFunction().getArg(n))
1206+
exists(CallableValue func, CallNode call |
1207+
exists(int n | argument = func.getArgumentForCall(call, n) and param.getNode() = func.getScope().getArg(n))
12031208
or
1204-
exists(string name | argument = func.getNamedArgumentForCall(call, name) and param.getNode() = func.getFunction().getArgByName(name))
1209+
exists(string name | argument = func.getNamedArgumentForCall(call, name) and param.getNode() = func.getScope().getArgByName(name))
12051210
or
12061211
class_initializer_argument(_, _, call, func, argument, param)
12071212
|
@@ -1211,11 +1216,11 @@ library module TaintFlowImplementation {
12111216
}
12121217

12131218
pragma [noinline]
1214-
predicate class_initializer_argument(ClassObject cls, int n, CallNode call, FunctionObject func, ControlFlowNode argument, NameNode param) {
1215-
PointsTo::instantiation(call, _, cls) and
1216-
cls.lookupAttribute("__init__") = func and
1219+
predicate class_initializer_argument(ClassValue cls, int n, CallNode call, CallableValue func, ControlFlowNode argument, NameNode param) {
1220+
call.getFunction().pointsTo(cls) and
1221+
cls.attr("__init__") = func and
12171222
call.getArg(n) = argument and
1218-
param.getNode() = func.getFunction().getArg(n+1)
1223+
param.getNode() = func.getScope().getArg(n+1)
12191224
}
12201225

12211226
pragma [noinline]
@@ -1257,15 +1262,15 @@ library module TaintFlowImplementation {
12571262
not Filters::isinstance(test.getTest(), _, var.getSourceVariable().getAUse()) and
12581263
not boolean_filter(test.getTest(), var.getSourceVariable().getAUse())
12591264
or
1260-
exists(ControlFlowNode c, ClassObject cls |
1265+
exists(ControlFlowNode c, ClassValue cls |
12611266
Filters::isinstance(test.getTest(), c, var.getSourceVariable().getAUse())
1262-
and c.refersTo(cls)
1267+
and c.pointsTo(cls)
12631268
|
12641269
test.getSense() = true and not exists(kind.getClass())
12651270
or
1266-
test.getSense() = true and kind.getClass().getAnImproperSuperType() = cls
1271+
test.getSense() = true and kind.getClass().getASuperType() = cls
12671272
or
1268-
test.getSense() = false and not kind.getClass().getAnImproperSuperType() = cls
1273+
test.getSense() = false and not kind.getType().getASuperType() = cls
12691274
)
12701275
or
12711276
test.getSense() = test_evaluates(test.getTest(), var.getSourceVariable().getAUse(), kind)
@@ -1311,8 +1316,9 @@ library module TaintFlowImplementation {
13111316

13121317
pragma [noinline]
13131318
predicate tainted_import_star(ImportStarRefinement def, CallContext context, TaintedNode origin) {
1314-
exists(ModuleObject mod, string name |
1315-
PointsTo::Flow::module_and_name_for_import_star(mod, name, def, _) |
1319+
exists(ModuleValue mod, string name |
1320+
PointsTo::pointsTo(def.getDefiningNode().(ImportStarNode).getModule(), _, mod, _) and
1321+
name = def.getSourceVariable().getName() |
13161322
if mod.exports(name) then (
13171323
/* Attribute from imported module */
13181324
module_attribute_tainted(mod, name, origin) and
@@ -1362,7 +1368,7 @@ library module TaintFlowImplementation {
13621368
tonode.getArg(0) = fromnode
13631369
)
13641370
or
1365-
tonode.getFunction().refersTo(Object::builtin("reversed")) and
1371+
tonode.getFunction().pointsTo(Value::named("reversed")) and
13661372
tonode.getArg(0) = fromnode
13671373
}
13681374

@@ -1501,11 +1507,11 @@ class CallContext extends TCallContext {
15011507
f.getFunction() = s and f.getACall() = call
15021508
)
15031509
or
1504-
exists(ClassObject cls,CallNode call |
1510+
exists(ClassValue cls,CallNode call |
15051511
this = TCalleeContext(call, _, _) and
1506-
PointsTo::instantiation(call, _, cls) and
1507-
s = cls.lookupAttribute("__init__").(FunctionObject).getFunction() and
1508-
call.getFunction().refersTo(cls)
1512+
call.getFunction().pointsTo(cls) and
1513+
s = cls.attr("__init__").(CallableValue).getScope() and
1514+
call.getFunction().pointsTo(cls)
15091515
)
15101516
}
15111517

@@ -1625,7 +1631,7 @@ pragma [noinline]
16251631
private predicate dict_construct(ControlFlowNode itemnode, ControlFlowNode dictnode) {
16261632
dictnode.(DictNode).getAValue() = itemnode
16271633
or
1628-
dictnode.(CallNode).getFunction().refersTo(theDictType()) and
1634+
dictnode.(CallNode).getFunction().pointsTo(Value::named("dict")) and
16291635
dictnode.(CallNode).getArgByName(_) = itemnode
16301636
}
16311637

@@ -1648,11 +1654,11 @@ private predicate sequence_call(ControlFlowNode fromnode, CallNode tonode) {
16481654
tonode.getArg(0) = fromnode and
16491655
exists(ControlFlowNode cls |
16501656
cls = tonode.getFunction() |
1651-
cls.refersTo(theListType())
1657+
cls.pointsTo(Value::named("list"))
16521658
or
1653-
cls.refersTo(theTupleType())
1659+
cls.pointsTo(Value::named("tuple"))
16541660
or
1655-
cls.refersTo(theSetType())
1661+
cls.pointsTo(Value::named("set"))
16561662
)
16571663
}
16581664

python/ql/src/semmle/python/security/strings/Basic.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ abstract class StringKind extends TaintKind {
2727
result = this and copy_call(fromnode, tonode)
2828
}
2929

30-
override ClassObject getClass() {
31-
result = theStrType() or result = theUnicodeType()
30+
override ClassValue getType() {
31+
result = Value::named("bytes") or result = Value::named("str") or result = Value::named("unicode")
3232
}
3333

3434
}

python/ql/src/semmle/python/web/pyramid/Request.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class PyramidRequest extends BaseWebobRequest {
1111
this = "pyramid.request"
1212
}
1313

14-
override ClassObject getClass() {
15-
result = ModuleObject::named("pyramid.request").attr("Request")
14+
override ClassValue getType() {
15+
result = Value::named("pyramid.request.Request")
1616
}
1717

1818
}

python/ql/src/semmle/python/web/webob/Request.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class WebobRequest extends BaseWebobRequest {
4444
this = "webob.Request"
4545
}
4646

47-
override ClassObject getClass() {
48-
result = ModuleObject::named("webob.request").attr("Request")
47+
override ClassValue getType() {
48+
result = Value::named("webob.request.Request")
4949
}
5050

5151
}

0 commit comments

Comments
 (0)