|
1 | 1 | private import codeql.cryptography.Model |
| 2 | +import semmle.code.cpp.ir.IR |
| 3 | +import semmle.code.cpp.security.FlowSources as FlowSources |
2 | 4 | private import cpp as Lang |
3 | 5 |
|
| 6 | + |
4 | 7 | module CryptoInput implements InputSig<Lang::Location> { |
| 8 | + class DataFlowNode = DataFlow::Node; |
5 | 9 | class LocatableElement = Lang::Locatable; |
6 | | - |
7 | 10 | class UnknownLocation = Lang::UnknownDefaultLocation; |
8 | 11 | } |
9 | 12 |
|
10 | 13 | module Crypto = CryptographyBase<Lang::Location, CryptoInput>; |
11 | 14 |
|
12 | | -import OpenSSL |
| 15 | +/** |
| 16 | + * Artifact output to node input configuration |
| 17 | + */ |
| 18 | +abstract class AdditionalFlowInputStep extends DataFlow::Node { |
| 19 | + abstract DataFlow::Node getOutput(); |
| 20 | + |
| 21 | + final DataFlow::Node getInput() { result = this } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * Generic data source to node input configuration |
| 27 | + */ |
| 28 | +module GenericDataSourceUniversalFlowConfig implements DataFlow::ConfigSig { |
| 29 | + predicate isSource(DataFlow::Node source) { |
| 30 | + source = any(Crypto::GenericDataSourceInstance i).getOutputNode() |
| 31 | + } |
| 32 | + |
| 33 | + predicate isSink(DataFlow::Node sink) { |
| 34 | + sink = any(Crypto::FlowAwareElement other).getInputNode() |
| 35 | + } |
| 36 | + |
| 37 | + predicate isBarrierOut(DataFlow::Node node) { |
| 38 | + node = any(Crypto::FlowAwareElement element).getInputNode() |
| 39 | + } |
| 40 | + |
| 41 | + predicate isBarrierIn(DataFlow::Node node) { |
| 42 | + node = any(Crypto::FlowAwareElement element).getOutputNode() |
| 43 | + } |
| 44 | + |
| 45 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 46 | + node1.(AdditionalFlowInputStep).getOutput() = node2 |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +// TODO: I think this will be inefficient, no? |
| 53 | +class ConstantDataSource extends Crypto::GenericConstantOrAllocationSource instanceof Literal { |
| 54 | + override DataFlow::Node getOutputNode() { |
| 55 | + result.asExpr() = this |
| 56 | + } |
| 57 | + |
| 58 | + override predicate flowsTo(Crypto::FlowAwareElement other) { |
| 59 | + // TODO: separate config to avoid blowing up data-flow analysis |
| 60 | + GenericDataSourceUniversalFlow::flow(this.getOutputNode(), other.getInputNode()) |
| 61 | + } |
| 62 | + |
| 63 | + override string getAdditionalDescription() { result = this.toString() } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Definitions of various generic data sources |
| 68 | + */ |
| 69 | +// final class DefaultFlowSource = SourceNode; |
| 70 | + |
| 71 | +// final class DefaultRemoteFlowSource = RemoteFlowSource; |
| 72 | + |
| 73 | +// class GenericLocalDataSource extends Crypto::GenericLocalDataSource { |
| 74 | +// GenericLocalDataSource() { |
| 75 | +// any(DefaultFlowSource src | not src instanceof DefaultRemoteFlowSource).asExpr() = this |
| 76 | +// } |
| 77 | + |
| 78 | +// override DataFlow::Node getOutputNode() { result.asExpr() = this } |
| 79 | + |
| 80 | +// override predicate flowsTo(Crypto::FlowAwareElement other) { |
| 81 | +// GenericDataSourceUniversalFlow::flow(this.getOutputNode(), other.getInputNode()) |
| 82 | +// } |
| 83 | + |
| 84 | +// override string getAdditionalDescription() { result = this.toString() } |
| 85 | +// } |
| 86 | + |
| 87 | +// class GenericRemoteDataSource extends Crypto::GenericRemoteDataSource { |
| 88 | +// GenericRemoteDataSource() { any(DefaultRemoteFlowSource src).asExpr() = this } |
| 89 | + |
| 90 | +// override DataFlow::Node getOutputNode() { result.asExpr() = this } |
| 91 | + |
| 92 | +// override predicate flowsTo(Crypto::FlowAwareElement other) { |
| 93 | +// GenericDataSourceUniversalFlow::flow(this.getOutputNode(), other.getInputNode()) |
| 94 | +// } |
| 95 | + |
| 96 | +// override string getAdditionalDescription() { result = this.toString() } |
| 97 | +// } |
| 98 | + |
| 99 | + |
| 100 | +module GenericDataSourceUniversalFlow = DataFlow::Global<GenericDataSourceUniversalFlowConfig>; |
| 101 | + |
| 102 | +module ArtifactUniversalFlowConfig implements DataFlow::ConfigSig { |
| 103 | + predicate isSource(DataFlow::Node source) { |
| 104 | + source = any(Crypto::ArtifactElement artifact).getOutputNode() |
| 105 | + } |
| 106 | + |
| 107 | + predicate isSink(DataFlow::Node sink) { |
| 108 | + sink = any(Crypto::FlowAwareElement other).getInputNode() |
| 109 | + } |
| 110 | + |
| 111 | + predicate isBarrierOut(DataFlow::Node node) { |
| 112 | + node = any(Crypto::FlowAwareElement element).getInputNode() |
| 113 | + } |
| 114 | + |
| 115 | + predicate isBarrierIn(DataFlow::Node node) { |
| 116 | + node = any(Crypto::FlowAwareElement element).getOutputNode() |
| 117 | + } |
| 118 | + |
| 119 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 120 | + node1.(AdditionalFlowInputStep).getOutput() = node2 |
| 121 | + } |
| 122 | +} |
| 123 | +module ArtifactUniversalFlow = DataFlow::Global<ArtifactUniversalFlowConfig>; |
| 124 | +abstract class CipherOutputArtifact extends Crypto::CipherOutputArtifactInstance { |
| 125 | + override predicate flowsTo(Crypto::FlowAwareElement other) { |
| 126 | + ArtifactUniversalFlow::flow(this.getOutputNode(), other.getInputNode()) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +import OpenSSL.OpenSSL |
0 commit comments