Skip to content

Commit 716e0f1

Browse files
authored
Merge pull request github#5517 from tausbn/python-prevent-potentially-bad-join-order
Python: Prevent potentially bad join order
2 parents 28fb0ed + 0ae8b69 commit 716e0f1

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ abstract class ClassObjectInternal extends ObjectInternal {
6161
pragma[noinline]
6262
override predicate binds(ObjectInternal instance, string name, ObjectInternal descriptor) {
6363
instance = this and
64-
PointsToInternal::attributeRequired(this, name) and
65-
this.lookup(name, descriptor, _) and
64+
PointsToInternal::attributeRequired(this, pragma[only_bind_into](name)) and
65+
this.lookup(pragma[only_bind_into](name), descriptor, _) and
6666
descriptor.isDescriptor() = true
6767
}
6868

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ abstract class ConstantObjectInternal extends ObjectInternal {
3434

3535
pragma[noinline]
3636
override predicate attribute(string name, ObjectInternal value, CfgOrigin origin) {
37-
PointsToInternal::attributeRequired(this, name) and
37+
PointsToInternal::attributeRequired(pragma[only_bind_into](this), pragma[only_bind_into](name)) and
3838
exists(ObjectInternal cls_attr, CfgOrigin attr_orig |
39-
this.getClass().(ClassObjectInternal).lookup(name, cls_attr, attr_orig) and
39+
this.getClass()
40+
.(ClassObjectInternal)
41+
.lookup(pragma[only_bind_into](name), cls_attr, attr_orig) and
4042
cls_attr.isDescriptor() = true and
4143
cls_attr.descriptorGetInstance(this, value, origin)
4244
)

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ abstract class InstanceObject extends ObjectInternal {
3030

3131
pragma[noinline]
3232
private predicate classAttribute(string name, ObjectInternal cls_attr) {
33-
PointsToInternal::attributeRequired(this, name) and
34-
this.getClass().(ClassObjectInternal).lookup(name, cls_attr, _)
33+
PointsToInternal::attributeRequired(this, pragma[only_bind_into](name)) and
34+
this.getClass().(ClassObjectInternal).lookup(pragma[only_bind_into](name), cls_attr, _)
3535
}
3636

3737
pragma[noinline]
3838
private predicate selfAttribute(string name, ObjectInternal value, CfgOrigin origin) {
39-
PointsToInternal::attributeRequired(this, name) and
39+
PointsToInternal::attributeRequired(this, pragma[only_bind_into](name)) and
4040
exists(EssaVariable self, PythonFunctionObjectInternal init, Context callee |
4141
this.initializer(init, callee) and
4242
self_variable_reaching_init_exit(self) and
4343
self.getScope() = init.getScope() and
44-
AttributePointsTo::variableAttributePointsTo(self, callee, name, value, origin)
44+
AttributePointsTo::variableAttributePointsTo(self, callee, pragma[only_bind_into](name),
45+
value, origin)
4546
)
4647
}
4748

@@ -316,9 +317,11 @@ class UnknownInstanceInternal extends TUnknownInstance, ObjectInternal {
316317

317318
pragma[noinline]
318319
override predicate attribute(string name, ObjectInternal value, CfgOrigin origin) {
319-
PointsToInternal::attributeRequired(this, name) and
320+
PointsToInternal::attributeRequired(this, pragma[only_bind_into](name)) and
320321
exists(ObjectInternal cls_attr, CfgOrigin attr_orig |
321-
this.getClass().(ClassObjectInternal).lookup(name, cls_attr, attr_orig)
322+
this.getClass()
323+
.(ClassObjectInternal)
324+
.lookup(pragma[only_bind_into](name), cls_attr, attr_orig)
322325
|
323326
cls_attr.isDescriptor() = false and value = cls_attr and origin = attr_orig
324327
or
@@ -456,8 +459,8 @@ class SuperInstance extends TSuperInstance, ObjectInternal {
456459
/* Helper for `attribute` */
457460
pragma[noinline]
458461
private predicate attribute_descriptor(string name, ObjectInternal cls_attr, CfgOrigin attr_orig) {
459-
PointsToInternal::attributeRequired(this, name) and
460-
this.lookup(name, cls_attr, attr_orig)
462+
PointsToInternal::attributeRequired(this, pragma[only_bind_into](name)) and
463+
this.lookup(pragma[only_bind_into](name), cls_attr, attr_orig)
461464
}
462465

463466
private predicate lookup(string name, ObjectInternal value, CfgOrigin origin) {

python/ql/src/semmle/python/pointsto/PointsTo.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ module PointsToInternal {
524524
)
525525
}
526526

527+
pragma[noinline]
527528
private boolean ssa_filter_definition_bool(
528529
PyEdgeRefinement def, PointsToContext context, ObjectInternal value, ControlFlowNode origin
529530
) {

python/ql/src/semmle/python/pointsto/PointsToContext.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ class PointsToContext extends TPointsToContext {
184184

185185
/** Holds if this context can apply to the CFG node `n`. */
186186
pragma[inline]
187-
predicate appliesTo(ControlFlowNode n) { this.appliesToScope(n.getScope()) }
187+
predicate appliesTo(ControlFlowNode n) {
188+
exists(Scope s |
189+
this.appliesToScope(pragma[only_bind_into](s)) and pragma[only_bind_into](s) = n.getScope()
190+
)
191+
}
188192

189193
/** Holds if this context is a call context. */
190194
predicate isCall() { this = TCallContext(_, _, _) }

0 commit comments

Comments
 (0)