Skip to content

Commit 15afc3e

Browse files
authored
Merge pull request #14491 from egregius313/egregius313/java/mad/convert-iv
Java: Refactor `java/static-initialization-vector` to use Models as Data
2 parents b93442a + 8ed5bfb commit 15afc3e

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/java-all
4+
extensible: sinkModel
5+
data:
6+
- ["javax.crypto", "Cipher", True, "init", "(int,Key,AlgorithmParameterSpec)", "", "Argument[2]", "encryption-iv", "manual"]
7+
- ["javax.crypto", "Cipher", True, "init", "(int,Key,AlgorithmParameterSpec,SecureRandom)", "", "Argument[2]", "encryption-iv", "manual"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/java-all
4+
extensible: summaryModel
5+
data:
6+
- ["javax.crypto.spec", "IvParameterSpec", True, "IvParameterSpec", "", "", "Argument[0]", "Argument[this]", "taint", "manual"]
7+
- ["javax.crypto.spec", "GCMParameterSpec", True, "GCMParameterSpec", "", "", "Argument[1]", "Argument[this]", "taint", "manual"]
8+
- ["javax.crypto.spec", "RC2ParameterSpec", True, "RC2ParameterSpec", "", "", "Argument[1]", "Argument[this]", "taint", "manual"]
9+
- ["javax.crypto.spec", "RC5ParameterSpec", True, "RC5ParameterSpec", "", "", "Argument[3]", "Argument[this]", "taint", "manual"]

java/ql/lib/semmle/code/java/security/StaticInitializationVectorQuery.qll

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java
44
import semmle.code.java.dataflow.TaintTracking
5-
import semmle.code.java.dataflow.DataFlow2
5+
private import semmle.code.java.dataflow.ExternalFlow
66

77
/**
88
* Holds if `array` is initialized only with constants.
@@ -99,7 +99,7 @@ private module ArrayUpdateFlow = DataFlow::Global<ArrayUpdateConfig>;
9999
private class StaticInitializationVectorSource extends DataFlow::Node {
100100
StaticInitializationVectorSource() {
101101
exists(StaticByteArrayCreation array | array = this.asExpr() |
102-
not ArrayUpdateFlow::flow(DataFlow2::exprNode(array), _) and
102+
not ArrayUpdateFlow::flow(DataFlow::exprNode(array), _) and
103103
// Reduce FPs from utility methods that return an empty array in an exceptional case
104104
not exists(ReturnStmt ret |
105105
array.getADimension().(CompileTimeConstantExpr).getIntValue() = 0 and
@@ -113,34 +113,7 @@ private class StaticInitializationVectorSource extends DataFlow::Node {
113113
* A sink that initializes a cipher with unsafe parameters.
114114
*/
115115
private class EncryptionInitializationSink extends DataFlow::Node {
116-
EncryptionInitializationSink() {
117-
exists(MethodAccess ma, Method m | m = ma.getMethod() |
118-
m.hasQualifiedName("javax.crypto", "Cipher", "init") and
119-
m.getParameterType(2)
120-
.(RefType)
121-
.hasQualifiedName("java.security.spec", "AlgorithmParameterSpec") and
122-
ma.getArgument(2) = this.asExpr()
123-
)
124-
}
125-
}
126-
127-
/**
128-
* Holds if `fromNode` to `toNode` is a dataflow step
129-
* that creates cipher's parameters with initialization vector.
130-
*/
131-
private predicate createInitializationVectorSpecStep(DataFlow::Node fromNode, DataFlow::Node toNode) {
132-
exists(ConstructorCall cc, RefType type |
133-
cc = toNode.asExpr() and type = cc.getConstructedType()
134-
|
135-
type.hasQualifiedName("javax.crypto.spec", "IvParameterSpec") and
136-
cc.getArgument(0) = fromNode.asExpr()
137-
or
138-
type.hasQualifiedName("javax.crypto.spec", ["GCMParameterSpec", "RC2ParameterSpec"]) and
139-
cc.getArgument(1) = fromNode.asExpr()
140-
or
141-
type.hasQualifiedName("javax.crypto.spec", "RC5ParameterSpec") and
142-
cc.getArgument(3) = fromNode.asExpr()
143-
)
116+
EncryptionInitializationSink() { sinkNode(this, "encryption-iv") }
144117
}
145118

146119
/**
@@ -156,10 +129,6 @@ deprecated class StaticInitializationVectorConfig extends TaintTracking::Configu
156129
}
157130

158131
override predicate isSink(DataFlow::Node sink) { sink instanceof EncryptionInitializationSink }
159-
160-
override predicate isAdditionalTaintStep(DataFlow::Node fromNode, DataFlow::Node toNode) {
161-
createInitializationVectorSpecStep(fromNode, toNode)
162-
}
163132
}
164133

165134
/**
@@ -169,10 +138,6 @@ module StaticInitializationVectorConfig implements DataFlow::ConfigSig {
169138
predicate isSource(DataFlow::Node source) { source instanceof StaticInitializationVectorSource }
170139

171140
predicate isSink(DataFlow::Node sink) { sink instanceof EncryptionInitializationSink }
172-
173-
predicate isAdditionalFlowStep(DataFlow::Node fromNode, DataFlow::Node toNode) {
174-
createInitializationVectorSpecStep(fromNode, toNode)
175-
}
176141
}
177142

178143
/** Tracks the flow from a static initialization vector to the initialization of a cipher */

0 commit comments

Comments
 (0)