Skip to content

Commit 5050758

Browse files
committed
Refactor output artifact type
1 parent bec69ca commit 5050758

File tree

3 files changed

+24
-53
lines changed

3 files changed

+24
-53
lines changed

java/ql/lib/experimental/Quantum/JCA.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ module JCAModel {
419419
src.asExpr() instanceof CipherGetInstanceCall
420420
}
421421

422-
predicate isSink(DataFlow::Node sink, FlowState state) { none() }
422+
predicate isSink(DataFlow::Node sink, FlowState state) { none() } // TODO: document this, but this is intentional (avoid cross products?)
423423

424424
predicate isSink(DataFlow::Node sink) {
425425
exists(CipherOperationCall c | c.getQualifier() = sink.asExpr())
@@ -786,7 +786,7 @@ module JCAModel {
786786
type instanceof Crypto::TAsymmetricKeyType
787787
}
788788

789-
override DataFlow::Node getOutputKeyArtifact() { result.asExpr() = this }
789+
override Crypto::ArtifactOutputDataFlowNode getOutputKeyArtifact() { result.asExpr() = this }
790790

791791
override Crypto::KeyArtifactType getOutputKeyType() { result = type }
792792

@@ -1139,7 +1139,7 @@ module JCAModel {
11391139
result.asExpr() = this.getInstantiation().getIterationCountArg()
11401140
}
11411141

1142-
override DataFlow::Node getOutputKeyArtifact() {
1142+
override Crypto::ArtifactOutputDataFlowNode getOutputKeyArtifact() {
11431143
result.asExpr() = this and
11441144
super.getMethod().getReturnType().hasName("SecretKey")
11451145
}

java/ql/lib/experimental/Quantum/Language.qll

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,5 @@ module ArtifactUniversalFlowConfig implements DataFlow::ConfigSig {
191191

192192
module GenericDataSourceUniversalFlow = TaintTracking::Global<GenericDataSourceUniversalFlowConfig>;
193193

194-
/*
195-
* class LiteralOrGenericDataSource extends Element {
196-
* DataFlow::Node node;
197-
*
198-
* LiteralOrGenericDataSource() {
199-
* node = this.(Crypto::GenericSourceInstance).getOutputNode() or
200-
* node.asExpr() = this.(Literal)
201-
* }
202-
*
203-
* bindingset[other]
204-
* predicate localFlowsTo(DataFlow::Node other) { DataFlow::localFlow(node, other) }
205-
* }
206-
*/
207-
208194
// Import library-specific modeling
209195
import JCA

shared/cryptography/codeql/cryptography/Model.qll

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
4141
ConsumerElement getConsumer() { result.getInputNode() = this }
4242
}
4343

44+
class ArtifactOutputDataFlowNode extends DataFlowNode {
45+
OutputArtifactInstance getArtifact() { result.getOutputNode() = this }
46+
}
47+
4448
final class UnknownPropertyValue extends string {
4549
UnknownPropertyValue() { this = "<unknown>" }
4650
}
@@ -461,7 +465,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
461465
this = Input::dfn_to_element(inputNode)
462466
}
463467

464-
override KeyArtifactType getKeyType() { result instanceof TUnknownKeyType }
468+
override KeyArtifactType getKeyType() { result instanceof TUnknownKeyType } // A consumer node does not have a key type, refer to source (TODO: refine, should this be none())
465469

466470
final override ConsumerInputDataFlowNode getInputNode() { result = inputNode }
467471
}
@@ -651,7 +655,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
651655
/**
652656
* Gets the key artifact produced by this operation.
653657
*/
654-
abstract DataFlowNode getOutputKeyArtifact();
658+
abstract ArtifactOutputDataFlowNode getOutputKeyArtifact();
655659

656660
/**
657661
* Gets the key artifact type produced.
@@ -898,29 +902,8 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
898902
predicate isExcludedFromGraph() { none() }
899903
}
900904

901-
signature string getDefaultValueSig();
902-
903-
signature ConsumerInputDataFlowNode getConsumerSig();
904-
905-
signature class NodeBaseSig instanceof NodeBase;
906-
907-
module PropertyOutput<getDefaultValueSig/0 getDefault, getConsumerSig/0 getConsumer> {
908-
bindingset[root]
909-
predicate get(NodeBase root, string value, Location location) {
910-
if not exists(getDefault()) and not exists(getConsumer().getConsumer().getASource())
911-
then value instanceof UnknownPropertyValue and location instanceof UnknownLocation
912-
else (
913-
if exists(getDefault())
914-
then
915-
value = "Default:" + getDefault() and
916-
location = root.getLocation()
917-
else node_as_property(getConsumer().getConsumer().getAGenericSourceNode(), value, location)
918-
)
919-
}
920-
}
921-
922905
/**
923-
* A generic source node is a source of data that is not resolvable to a specific value or type.
906+
* A generic source node is a source of data that is not resolvable to a specific asset.
924907
*/
925908
private class GenericSourceNode extends NodeBase, TGenericSourceNode {
926909
GenericSourceInstance instance;
@@ -956,7 +939,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
956939
* Holds if `node` is a potential candidate for a known algorithm node.
957940
* This predicate should be used to restrict the set of candidate algorithm node types.
958941
*/
959-
abstract predicate isCandidateKnownAlgorithmNode(AlgorithmNode node);
942+
abstract predicate isCandidateAlgorithmNode(AlgorithmNode node);
960943

961944
/**
962945
* Gets the algorithm or generic source nodes consumed as an algorithm associated with this operation.
@@ -968,12 +951,12 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
968951
}
969952

970953
/**
971-
* Gets a known algorithm associated with this operation, subject to `isCandidateKnownAlgorithmNode`.
954+
* Gets a known algorithm associated with this operation, subject to `isCandidateAlgorithmNode`.
972955
*/
973956
AlgorithmNode getAKnownAlgorithm() {
974957
result =
975958
this.asElement().(OperationInstance).getAnAlgorithmValueConsumer().getAKnownSourceNode() and
976-
this.isCandidateKnownAlgorithmNode(result)
959+
this.isCandidateAlgorithmNode(result)
977960
}
978961

979962
override NodeBase getChild(string edgeName) {
@@ -1147,9 +1130,11 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
11471130
// [KNOWN_OR_UNKNOWN] - only if asymmetric
11481131
edgeName = "Algorithm" and
11491132
instance.getKeyType() instanceof TAsymmetricKeyType and
1150-
if exists(this.getAKnownAlgorithmOrGenericSourceNode())
1151-
then result = this.getAKnownAlgorithmOrGenericSourceNode()
1152-
else result = this
1133+
(
1134+
if exists(this.getAKnownAlgorithmOrGenericSourceNode())
1135+
then result = this.getAKnownAlgorithmOrGenericSourceNode()
1136+
else result = this
1137+
)
11531138
}
11541139

11551140
override predicate properties(string key, string value, Location location) {
@@ -1217,7 +1202,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
12171202

12181203
override LocatableElement asElement() { result = instance }
12191204

1220-
override predicate isCandidateKnownAlgorithmNode(AlgorithmNode node) {
1205+
override predicate isCandidateAlgorithmNode(AlgorithmNode node) {
12211206
node instanceof MACAlgorithmNode
12221207
}
12231208

@@ -1292,7 +1277,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
12921277

12931278
KeyGenerationOperationNode() { keyGenInstance = instance }
12941279

1295-
override predicate isCandidateKnownAlgorithmNode(AlgorithmNode node) {
1280+
override predicate isCandidateAlgorithmNode(AlgorithmNode node) {
12961281
node instanceof CipherAlgorithmNode
12971282
}
12981283

@@ -1326,7 +1311,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
13261311
result.asElement() = kdfInstance.getOutputKeySizeConsumer().getConsumer().getAGenericSource()
13271312
}
13281313

1329-
override predicate isCandidateKnownAlgorithmNode(AlgorithmNode node) {
1314+
override predicate isCandidateAlgorithmNode(AlgorithmNode node) {
13301315
node instanceof KeyDerivationAlgorithmNode
13311316
}
13321317

@@ -1585,7 +1570,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
15851570

15861571
override string getInternalType() { result = "CipherOperation" }
15871572

1588-
override predicate isCandidateKnownAlgorithmNode(AlgorithmNode node) {
1573+
override predicate isCandidateAlgorithmNode(AlgorithmNode node) {
15891574
node instanceof CipherAlgorithmNode
15901575
}
15911576

@@ -1904,7 +1889,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
19041889
type instanceof SM4 and name = "SM4" and s = Block()
19051890
or
19061891
type instanceof OtherCipherType and
1907-
name instanceof UnknownPropertyValue and
1892+
name instanceof UnknownPropertyValue and // TODO: get rid of this hack to bind structure and type
19081893
s = UnknownCipherStructureType()
19091894
}
19101895

@@ -1959,7 +1944,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
19591944

19601945
override LocatableElement asElement() { result = instance }
19611946

1962-
override predicate isCandidateKnownAlgorithmNode(AlgorithmNode node) {
1947+
override predicate isCandidateAlgorithmNode(AlgorithmNode node) {
19631948
node instanceof HashAlgorithmNode
19641949
}
19651950

0 commit comments

Comments
 (0)