Skip to content

Commit 28d6cad

Browse files
committed
Python: Prevent joining on name as the first thing
Many instances of `lookup` are restricted by the presence of `attributeRequired`, but this does not work well if we join on `name`. A few instances of `only_bind_into` prevents this.
1 parent ed8ffab commit 28d6cad

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
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) {

0 commit comments

Comments
 (0)