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