Skip to content

Commit edfd871

Browse files
authored
Merge pull request github#12695 from jketema/swift-configsig
Swift: Refactor a number of queries to use `DataFlow::ConfigSig`
2 parents e3e68b7 + a381aa4 commit edfd871

File tree

8 files changed

+69
-75
lines changed

8 files changed

+69
-75
lines changed

swift/ql/src/queries/Security/CWE-1204/StaticInitializationVector.ql

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import swift
1515
import codeql.swift.dataflow.DataFlow
1616
import codeql.swift.dataflow.TaintTracking
17-
import DataFlow::PathGraph
17+
import StaticInitializationVectorFlow::PathGraph
1818

1919
/**
2020
* A static IV is created through either a byte array or string literals.
@@ -56,23 +56,21 @@ class EncryptionInitializationSink extends Expr {
5656
* A dataflow configuration from the source of a static IV to expressions that use
5757
* it to initialize a cipher.
5858
*/
59-
class StaticInitializationVectorConfig extends TaintTracking::Configuration {
60-
StaticInitializationVectorConfig() { this = "StaticInitializationVectorConfig" }
61-
62-
override predicate isSource(DataFlow::Node node) {
59+
module StaticInitializationVectorConfig implements DataFlow::ConfigSig {
60+
predicate isSource(DataFlow::Node node) {
6361
node.asExpr() instanceof StaticInitializationVectorSource
6462
}
6563

66-
override predicate isSink(DataFlow::Node node) {
67-
node.asExpr() instanceof EncryptionInitializationSink
68-
}
64+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof EncryptionInitializationSink }
6965
}
7066

67+
module StaticInitializationVectorFlow = TaintTracking::Global<StaticInitializationVectorConfig>;
68+
7169
// The query itself
7270
from
73-
StaticInitializationVectorConfig config, DataFlow::PathNode sourceNode,
74-
DataFlow::PathNode sinkNode
75-
where config.hasFlowPath(sourceNode, sinkNode)
71+
StaticInitializationVectorFlow::PathNode sourceNode,
72+
StaticInitializationVectorFlow::PathNode sinkNode
73+
where StaticInitializationVectorFlow::flowPath(sourceNode, sinkNode)
7674
select sinkNode.getNode(), sourceNode, sinkNode,
7775
"The static value '" + sourceNode.getNode().toString() +
7876
"' is used as an initialization vector for encryption."

swift/ql/src/queries/Security/CWE-259/ConstantPassword.ql

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
1616
import codeql.swift.dataflow.FlowSteps
17-
import DataFlow::PathGraph
17+
import ConstantPasswordFlow::PathGraph
1818

1919
/**
2020
* A constant password is created through either a byte array or string literals.
@@ -60,18 +60,16 @@ class ConstantPasswordSink extends Expr {
6060
* A taint configuration from the source of constants passwords to expressions that use
6161
* them to initialize password-based encryption keys.
6262
*/
63-
class ConstantPasswordConfig extends TaintTracking::Configuration {
64-
ConstantPasswordConfig() { this = "ConstantPasswordConfig" }
63+
module ConstantPasswordConfig implements DataFlow::ConfigSig {
64+
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof ConstantPasswordSource }
6565

66-
override predicate isSource(DataFlow::Node node) {
67-
node.asExpr() instanceof ConstantPasswordSource
68-
}
69-
70-
override predicate isSink(DataFlow::Node node) { node.asExpr() instanceof ConstantPasswordSink }
66+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof ConstantPasswordSink }
7167
}
7268

69+
module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>;
70+
7371
// The query itself
74-
from ConstantPasswordConfig config, DataFlow::PathNode sourceNode, DataFlow::PathNode sinkNode
75-
where config.hasFlowPath(sourceNode, sinkNode)
72+
from ConstantPasswordFlow::PathNode sourceNode, ConstantPasswordFlow::PathNode sinkNode
73+
where ConstantPasswordFlow::flowPath(sourceNode, sinkNode)
7674
select sinkNode.getNode(), sourceNode, sinkNode,
7775
"The value '" + sourceNode.getNode().toString() + "' is used as a constant password."

swift/ql/src/queries/Security/CWE-321/HardcodedEncryptionKey.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
16-
import DataFlow::PathGraph
16+
import HardcodedKeyFlow::PathGraph
1717

1818
/**
1919
* An `Expr` that is used to initialize a key.
@@ -62,17 +62,17 @@ class EncryptionKeySink extends Expr {
6262
* A taint configuration from the key source to expressions that use
6363
* it to initialize a cipher.
6464
*/
65-
class HardcodedKeyConfig extends TaintTracking::Configuration {
66-
HardcodedKeyConfig() { this = "HardcodedKeyConfig" }
65+
module HardcodedKeyConfig implements DataFlow::ConfigSig {
66+
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof KeySource }
6767

68-
override predicate isSource(DataFlow::Node node) { node.asExpr() instanceof KeySource }
69-
70-
override predicate isSink(DataFlow::Node node) { node.asExpr() instanceof EncryptionKeySink }
68+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof EncryptionKeySink }
7169
}
7270

71+
module HardcodedKeyFlow = TaintTracking::Global<HardcodedKeyConfig>;
72+
7373
// The query itself
74-
from HardcodedKeyConfig config, DataFlow::PathNode sourceNode, DataFlow::PathNode sinkNode
75-
where config.hasFlowPath(sourceNode, sinkNode)
74+
from HardcodedKeyFlow::PathNode sourceNode, HardcodedKeyFlow::PathNode sinkNode
75+
where HardcodedKeyFlow::flowPath(sourceNode, sinkNode)
7676
select sinkNode.getNode(), sourceNode, sinkNode,
7777
"The key '" + sinkNode.getNode().toString() +
7878
"' has been initialized with hard-coded values from $@.", sourceNode,

swift/ql/src/queries/Security/CWE-327/ECBEncryption.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
16-
import DataFlow::PathGraph
16+
import EcbEncryptionFlow::PathGraph
1717

1818
/**
1919
* An `Expr` that is used to initialize the block mode of a cipher.
@@ -54,22 +54,22 @@ class Blowfish extends BlockMode {
5454
* A taint configuration from the constructor of ECB mode to expressions that use
5555
* it to initialize a cipher.
5656
*/
57-
class EcbEncryptionConfig extends DataFlow::Configuration {
58-
EcbEncryptionConfig() { this = "EcbEncryptionConfig" }
59-
60-
override predicate isSource(DataFlow::Node node) {
57+
module EcbEncryptionConfig implements DataFlow::ConfigSig {
58+
predicate isSource(DataFlow::Node node) {
6159
exists(CallExpr call |
6260
call.getStaticTarget().(MethodDecl).hasQualifiedName("ECB", "init()") and
6361
node.asExpr() = call
6462
)
6563
}
6664

67-
override predicate isSink(DataFlow::Node node) { node.asExpr() instanceof BlockMode }
65+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof BlockMode }
6866
}
6967

68+
module EcbEncryptionFlow = DataFlow::Global<EcbEncryptionConfig>;
69+
7070
// The query itself
71-
from EcbEncryptionConfig config, DataFlow::PathNode sourceNode, DataFlow::PathNode sinkNode
72-
where config.hasFlowPath(sourceNode, sinkNode)
71+
from EcbEncryptionFlow::PathNode sourceNode, EcbEncryptionFlow::PathNode sinkNode
72+
where EcbEncryptionFlow::flowPath(sourceNode, sinkNode)
7373
select sinkNode.getNode(), sourceNode, sinkNode,
7474
"The initialization of the cipher '" + sinkNode.getNode().toString() +
7575
"' uses the insecure ECB block mode from $@.", sourceNode, sourceNode.getNode().toString()

swift/ql/src/queries/Security/CWE-328/WeakSensitiveDataHashing.ql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import swift
1515
import codeql.swift.security.SensitiveExprs
1616
import codeql.swift.dataflow.DataFlow
1717
import codeql.swift.dataflow.TaintTracking
18-
import DataFlow::PathGraph
18+
import WeakHashingFlow::PathGraph
1919

20-
class WeakHashingConfig extends TaintTracking::Configuration {
21-
WeakHashingConfig() { this = "WeakHashingConfig" }
20+
module WeakHashingConfig implements DataFlow::ConfigSig {
21+
predicate isSource(DataFlow::Node node) { node instanceof WeakHashingConfigImpl::Source }
2222

23-
override predicate isSource(DataFlow::Node node) { node instanceof WeakHashingConfig::Source }
24-
25-
override predicate isSink(DataFlow::Node node) { node instanceof WeakHashingConfig::Sink }
23+
predicate isSink(DataFlow::Node node) { node instanceof WeakHashingConfigImpl::Sink }
2624
}
2725

28-
module WeakHashingConfig {
26+
module WeakHashingFlow = TaintTracking::Global<WeakHashingConfig>;
27+
28+
module WeakHashingConfigImpl {
2929
class Source extends DataFlow::Node {
3030
Source() { this.asExpr() instanceof SensitiveExpr }
3131
}
@@ -52,11 +52,11 @@ module WeakHashingConfig {
5252
}
5353

5454
from
55-
WeakHashingConfig config, DataFlow::PathNode source, DataFlow::PathNode sink, string algorithm,
55+
WeakHashingFlow::PathNode source, WeakHashingFlow::PathNode sink, string algorithm,
5656
SensitiveExpr expr
5757
where
58-
config.hasFlowPath(source, sink) and
59-
algorithm = sink.getNode().(WeakHashingConfig::Sink).getAlgorithm() and
58+
WeakHashingFlow::flowPath(source, sink) and
59+
algorithm = sink.getNode().(WeakHashingConfigImpl::Sink).getAlgorithm() and
6060
expr = source.getNode().asExpr()
6161
select sink.getNode(), source, sink,
6262
"Insecure hashing algorithm (" + algorithm + ") depends on $@.", source.getNode(),

swift/ql/src/queries/Security/CWE-757/InsecureTLS.ql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,24 @@ import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
1616
import codeql.swift.dataflow.FlowSources
17-
import DataFlow::PathGraph
17+
import InsecureTlsFlow::PathGraph
1818

1919
/**
2020
* A taint config to detect insecure configuration of `NSURLSessionConfiguration`
2121
*/
22-
class InsecureTlsConfig extends TaintTracking::Configuration {
23-
InsecureTlsConfig() { this = "InsecureTLSConfig" }
24-
22+
module InsecureTlsConfig implements DataFlow::ConfigSig {
2523
/**
2624
* Holds for enum values that represent an insecure version of TLS
2725
*/
28-
override predicate isSource(DataFlow::Node node) {
26+
predicate isSource(DataFlow::Node node) {
2927
node.asExpr().(MethodLookupExpr).getMember().(EnumElementDecl).getName() =
3028
["TLSv10", "TLSv11", "tlsProtocol10", "tlsProtocol11"]
3129
}
3230

3331
/**
3432
* Holds for assignment of TLS-related properties of `NSURLSessionConfiguration`
3533
*/
36-
override predicate isSink(DataFlow::Node node) {
34+
predicate isSink(DataFlow::Node node) {
3735
exists(AssignExpr assign |
3836
assign.getSource() = node.asExpr() and
3937
assign.getDest().(MemberRefExpr).getMember().(ConcreteVarDecl).getName() =
@@ -45,6 +43,8 @@ class InsecureTlsConfig extends TaintTracking::Configuration {
4543
}
4644
}
4745

48-
from InsecureTlsConfig config, DataFlow::PathNode sourceNode, DataFlow::PathNode sinkNode
49-
where config.hasFlowPath(sourceNode, sinkNode)
46+
module InsecureTlsFlow = TaintTracking::Global<InsecureTlsConfig>;
47+
48+
from InsecureTlsFlow::PathNode sourceNode, InsecureTlsFlow::PathNode sinkNode
49+
where InsecureTlsFlow::flowPath(sourceNode, sinkNode)
5050
select sinkNode.getNode(), sourceNode, sinkNode, "This TLS configuration is insecure."

swift/ql/src/queries/Security/CWE-760/ConstantSalt.ql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
1616
import codeql.swift.dataflow.FlowSteps
17-
import DataFlow::PathGraph
17+
import ConstantSaltFlow::PathGraph
1818

1919
/**
2020
* A constant salt is created through either a byte array or string literals.
@@ -52,19 +52,19 @@ class ConstantSaltSink extends Expr {
5252

5353
/**
5454
* A taint configuration from the source of constants salts to expressions that use
55-
* them to initialize password-based enecryption keys.
55+
* them to initialize password-based encryption keys.
5656
*/
57-
class ConstantSaltConfig extends TaintTracking::Configuration {
58-
ConstantSaltConfig() { this = "ConstantSaltConfig" }
57+
module ConstantSaltConfig implements DataFlow::ConfigSig {
58+
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof ConstantSaltSource }
5959

60-
override predicate isSource(DataFlow::Node node) { node.asExpr() instanceof ConstantSaltSource }
61-
62-
override predicate isSink(DataFlow::Node node) { node.asExpr() instanceof ConstantSaltSink }
60+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof ConstantSaltSink }
6361
}
6462

63+
module ConstantSaltFlow = TaintTracking::Global<ConstantSaltConfig>;
64+
6565
// The query itself
66-
from ConstantSaltConfig config, DataFlow::PathNode sourceNode, DataFlow::PathNode sinkNode
67-
where config.hasFlowPath(sourceNode, sinkNode)
66+
from ConstantSaltFlow::PathNode sourceNode, ConstantSaltFlow::PathNode sinkNode
67+
where ConstantSaltFlow::flowPath(sourceNode, sinkNode)
6868
select sinkNode.getNode(), sourceNode, sinkNode,
6969
"The value '" + sourceNode.getNode().toString() +
7070
"' is used as a constant salt, which is insecure for hashing passwords."

swift/ql/src/queries/Security/CWE-916/InsufficientHashIterations.ql

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import codeql.swift.dataflow.TaintTracking
16-
import DataFlow::PathGraph
16+
import InsufficientHashIterationsFlow::PathGraph
1717

1818
/**
1919
* An `Expr` that is used to initialize a password-based encryption key.
@@ -46,21 +46,19 @@ class InsufficientHashIterationsSink extends Expr {
4646
* A dataflow configuration from the hash iterations source to expressions that use
4747
* it to initialize hash functions.
4848
*/
49-
class InsufficientHashIterationsConfig extends TaintTracking::Configuration {
50-
InsufficientHashIterationsConfig() { this = "InsufficientHashIterationsConfig" }
49+
module InsufficientHashIterationsConfig implements DataFlow::ConfigSig {
50+
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof IterationsSource }
5151

52-
override predicate isSource(DataFlow::Node node) { node.asExpr() instanceof IterationsSource }
53-
54-
override predicate isSink(DataFlow::Node node) {
55-
node.asExpr() instanceof InsufficientHashIterationsSink
56-
}
52+
predicate isSink(DataFlow::Node node) { node.asExpr() instanceof InsufficientHashIterationsSink }
5753
}
5854

55+
module InsufficientHashIterationsFlow = TaintTracking::Global<InsufficientHashIterationsConfig>;
56+
5957
// The query itself
6058
from
61-
InsufficientHashIterationsConfig config, DataFlow::PathNode sourceNode,
62-
DataFlow::PathNode sinkNode
63-
where config.hasFlowPath(sourceNode, sinkNode)
59+
InsufficientHashIterationsFlow::PathNode sourceNode,
60+
InsufficientHashIterationsFlow::PathNode sinkNode
61+
where InsufficientHashIterationsFlow::flowPath(sourceNode, sinkNode)
6462
select sinkNode.getNode(), sourceNode, sinkNode,
6563
"The value '" + sourceNode.getNode().toString() +
6664
"' is an insufficient number of iterations for secure password hashing."

0 commit comments

Comments
 (0)