|
| 1 | +import java |
| 2 | +import experimental.quantum.Language |
| 3 | +import codeql.util.Option |
| 4 | + |
| 5 | +/** |
| 6 | + * Holds when the src node is the output artifact of a decrypt operation |
| 7 | + * that flows to the input artifact of a mac operation. |
| 8 | + */ |
| 9 | +predicate isDecryptToMacFlow(ArtifactFlow::PathNode src, ArtifactFlow::PathNode sink) { |
| 10 | + ArtifactFlow::flowPath(src, sink) and |
| 11 | + exists(Crypto::CipherOperationNode cipherOp | |
| 12 | + cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and |
| 13 | + cipherOp.getAnOutputArtifact().asElement() = src.getNode().asExpr() |
| 14 | + ) and |
| 15 | + exists(Crypto::MacOperationNode macOp | |
| 16 | + macOp.getAnInputArtifact().asElement() = sink.getNode().asExpr() |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Holds when the src node is used as plaintext input to both |
| 22 | + * an encryption operation and a mac operation, via the |
| 23 | + * argument represented by InterimArg. |
| 24 | + */ |
| 25 | +predicate isPlaintextInEncryptionAndMac( |
| 26 | + PlaintextUseAsMacAndCipherInputFlow::PathNode src, |
| 27 | + PlaintextUseAsMacAndCipherInputFlow::PathNode sink, InterimArg arg |
| 28 | +) { |
| 29 | + PlaintextUseAsMacAndCipherInputFlow::flowPath(src, sink) and |
| 30 | + arg = sink.getState().asSome() |
| 31 | +} |
| 32 | + |
| 33 | +module ArgToSinkConfig implements DataFlow::ConfigSig { |
| 34 | + predicate isSource(DataFlow::Node source) { exists(Call c | c.getAnArgument() = source.asExpr()) } |
| 35 | + |
| 36 | + predicate isSink(DataFlow::Node sink) { targetSinks(sink) } |
| 37 | + |
| 38 | + // Don't go in to a known out node, this will prevent the plaintext |
| 39 | + // from tracing out of cipher operations for example, we just want to trace |
| 40 | + // the plaintext to uses. |
| 41 | + // NOTE: we are not using a barrier out on input nodes, because |
| 42 | + // that would remove 'use-use' flows, which we need |
| 43 | + predicate isBarrierIn(DataFlow::Node node) { |
| 44 | + node = any(Crypto::FlowAwareElement element).getOutputNode() |
| 45 | + } |
| 46 | + |
| 47 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 48 | + node1.(AdditionalFlowInputStep).getOutput() = node2 |
| 49 | + or |
| 50 | + exists(MethodCall m | |
| 51 | + m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and |
| 52 | + node1.asExpr() = m.getQualifier() and |
| 53 | + node2.asExpr() = m |
| 54 | + ) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +module ArgToSinkFlow = TaintTracking::Global<ArgToSinkConfig>; |
| 59 | + |
| 60 | +/** |
| 61 | + * Target sinks for this query are either encryption operations or mac operation message inputs |
| 62 | + */ |
| 63 | +predicate targetSinks(DataFlow::Node n) { |
| 64 | + exists(Crypto::CipherOperationNode cipherOp | |
| 65 | + cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and |
| 66 | + cipherOp.getAnInputArtifact().asElement() = n.asExpr() |
| 67 | + ) |
| 68 | + or |
| 69 | + exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = n.asExpr()) |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * An argument of a target sink or a parent call whose parameter flows to a target sink |
| 74 | + */ |
| 75 | +class InterimArg extends DataFlow::Node { |
| 76 | + DataFlow::Node targetSink; |
| 77 | + |
| 78 | + InterimArg() { |
| 79 | + targetSinks(targetSink) and |
| 80 | + ( |
| 81 | + this = targetSink |
| 82 | + or |
| 83 | + ArgToSinkFlow::flow(this, targetSink) and |
| 84 | + this.getEnclosingCallable().calls+(targetSink.getEnclosingCallable()) |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + DataFlow::Node getTargetSink() { result = targetSink } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * A wrapper class to represent a target argument dataflow node. |
| 93 | + */ |
| 94 | +class TargetArg extends DataFlow::Node { |
| 95 | + TargetArg() { targetSinks(this) } |
| 96 | + |
| 97 | + predicate isCipher() { |
| 98 | + exists(Crypto::CipherOperationNode cipherOp | |
| 99 | + cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and |
| 100 | + cipherOp.getAnInputArtifact().asElement() = this.asExpr() |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + predicate isMac() { |
| 105 | + exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = this.asExpr()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module PlaintextUseAsMacAndCipherInputConfig implements DataFlow::StateConfigSig { |
| 110 | + class FlowState = Option<TargetArg>::Option; |
| 111 | + |
| 112 | + // TODO: can we approximate a message source better? |
| 113 | + predicate isSource(DataFlow::Node source, FlowState state) { |
| 114 | + // TODO: can we find the 'closest' parameter to the sinks? |
| 115 | + // i.e., use a generic source if we have it, but also isolate the |
| 116 | + // lowest level in the flow to the closest parameter node in the call graph? |
| 117 | + exists(Crypto::GenericSourceNode other | |
| 118 | + other.asElement() = CryptoInput::dfn_to_element(source) |
| 119 | + ) and |
| 120 | + state.isNone() |
| 121 | + } |
| 122 | + |
| 123 | + predicate isSink(DataFlow::Node sink, FlowState state) { |
| 124 | + sink instanceof TargetArg and |
| 125 | + ( |
| 126 | + sink.(TargetArg).isMac() and state.asSome().isCipher() |
| 127 | + or |
| 128 | + sink.(TargetArg).isCipher() and state.asSome().isMac() |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + predicate isBarrierOut(DataFlow::Node node, FlowState state) { |
| 133 | + // Stop at the first sink for now |
| 134 | + isSink(node, state) |
| 135 | + } |
| 136 | + |
| 137 | + // Don't go in to a known out node, this will prevent the plaintext |
| 138 | + // from tracing out of cipher operations for example, we just want to trace |
| 139 | + // the plaintext to uses. |
| 140 | + // NOTE: we are not using a barrier out on input nodes, because |
| 141 | + // that would remove 'use-use' flows, which we need |
| 142 | + predicate isBarrierIn(DataFlow::Node node) { |
| 143 | + node = any(Crypto::FlowAwareElement element).getOutputNode() |
| 144 | + } |
| 145 | + |
| 146 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 147 | + node1.(AdditionalFlowInputStep).getOutput() = node2 |
| 148 | + or |
| 149 | + exists(MethodCall m | |
| 150 | + m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and |
| 151 | + node1.asExpr() = m.getQualifier() and |
| 152 | + node2.asExpr() = m |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + predicate isAdditionalFlowStep( |
| 157 | + DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2 |
| 158 | + ) { |
| 159 | + (exists(state1.asSome()) or state1.isNone()) and |
| 160 | + targetSinks(node1) and |
| 161 | + node1 instanceof TargetArg and |
| 162 | + //use-use flow, either flow directly from the node1 use |
| 163 | + //or find a parent call in the call in the call stack |
| 164 | + //and continue flow from that parameter |
| 165 | + node2.(InterimArg).getTargetSink() = node1 and |
| 166 | + state2.asSome() = node1 |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +module PlaintextUseAsMacAndCipherInputFlow = |
| 171 | + TaintTracking::GlobalWithState<PlaintextUseAsMacAndCipherInputConfig>; |
0 commit comments