|
| 1 | +import cpp |
| 2 | +import semmle.code.cpp.dataflow.new.DataFlow |
| 3 | +import experimental.quantum.OpenSSL.AlgorithmInstances.KnownAlgorithmConstants |
| 4 | +import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers |
| 5 | + |
| 6 | +/** |
| 7 | + * Traces 'known algorithms' to AVCs, specifically |
| 8 | + * algorithms that are in the set of known algorithm constants. |
| 9 | + * Padding-specific consumers exist that have their own values that |
| 10 | + * overlap with the known algorithm constants. |
| 11 | + * Padding consumers (specific padding consumers) are excluded from the set of sinks. |
| 12 | + */ |
| 13 | +module KnownOpenSSLAlgorithmToAlgorithmValueConsumerConfig implements DataFlow::ConfigSig { |
| 14 | + predicate isSource(DataFlow::Node source) { |
| 15 | + source.asExpr() instanceof KnownOpenSSLAlgorithmConstant |
| 16 | + } |
| 17 | + |
| 18 | + predicate isSink(DataFlow::Node sink) { |
| 19 | + exists(OpenSSLAlgorithmValueConsumer c | |
| 20 | + c.getInputNode() = sink and |
| 21 | + not c instanceof PaddingAlgorithmValueConsumer |
| 22 | + ) |
| 23 | + } |
| 24 | + |
| 25 | + predicate isBarrier(DataFlow::Node node) { |
| 26 | + // False positive reducer, don't flow out through argv |
| 27 | + exists(VariableAccess va, Variable v | |
| 28 | + v.getAnAccess() = va and va = node.asExpr() |
| 29 | + or |
| 30 | + va = node.asIndirectExpr() |
| 31 | + | |
| 32 | + v.getName().matches("%argv") |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 37 | + knownPassThroughStep(node1, node2) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +module KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow = |
| 42 | + DataFlow::Global<KnownOpenSSLAlgorithmToAlgorithmValueConsumerConfig>; |
| 43 | + |
| 44 | +module RSAPaddingAlgorithmToPaddingAlgorithmValueConsumerConfig implements DataFlow::ConfigSig { |
| 45 | + predicate isSource(DataFlow::Node source) { |
| 46 | + source.asExpr() instanceof KnownOpenSSLAlgorithmConstant |
| 47 | + } |
| 48 | + |
| 49 | + predicate isSink(DataFlow::Node sink) { |
| 50 | + exists(PaddingAlgorithmValueConsumer c | c.getInputNode() = sink) |
| 51 | + } |
| 52 | + |
| 53 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 54 | + knownPassThroughStep(node1, node2) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +module RSAPaddingAlgorithmToPaddingAlgorithmValueConsumerFlow = |
| 59 | + DataFlow::Global<RSAPaddingAlgorithmToPaddingAlgorithmValueConsumerConfig>; |
| 60 | + |
| 61 | +class OpenSSLAlgorithmAdditionalFlowStep extends AdditionalFlowInputStep { |
| 62 | + OpenSSLAlgorithmAdditionalFlowStep() { exists(AlgorithmPassthroughCall c | c.getInNode() = this) } |
| 63 | + |
| 64 | + override DataFlow::Node getOutput() { |
| 65 | + exists(AlgorithmPassthroughCall c | c.getInNode() = this and c.getOutNode() = result) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +abstract class AlgorithmPassthroughCall extends Call { |
| 70 | + abstract DataFlow::Node getInNode(); |
| 71 | + |
| 72 | + abstract DataFlow::Node getOutNode(); |
| 73 | +} |
| 74 | + |
| 75 | +class CopyAndDupAlgorithmPassthroughCall extends AlgorithmPassthroughCall { |
| 76 | + DataFlow::Node inNode; |
| 77 | + DataFlow::Node outNode; |
| 78 | + |
| 79 | + CopyAndDupAlgorithmPassthroughCall() { |
| 80 | + // Flow out through any return or other argument of the same type |
| 81 | + // Assume flow in and out is asIndirectExpr or asDefinitingArgument since a pointer is assumed |
| 82 | + // to be involved |
| 83 | + // NOTE: not attempting to detect openssl specific copy/dup functions, but anything suspected to be copy/dup |
| 84 | + this.getTarget().getName().toLowerCase().matches(["%_dup%", "%_copy%"]) and |
| 85 | + exists(Expr inArg, Type t | |
| 86 | + inArg = this.getAnArgument() and t = inArg.getUnspecifiedType().stripType() |
| 87 | + | |
| 88 | + inNode.asIndirectExpr() = inArg and |
| 89 | + ( |
| 90 | + // Case 1: flow through another argument as an out arg of the same type |
| 91 | + exists(Expr outArg | |
| 92 | + outArg = this.getAnArgument() and |
| 93 | + outArg != inArg and |
| 94 | + outArg.getUnspecifiedType().stripType() = t |
| 95 | + | |
| 96 | + outNode.asDefiningArgument() = outArg |
| 97 | + ) |
| 98 | + or |
| 99 | + // Case 2: flow through the return value if the result is the same as the intput type |
| 100 | + exists(Expr outArg | outArg = this and outArg.getUnspecifiedType().stripType() = t | |
| 101 | + outNode.asIndirectExpr() = outArg |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + override DataFlow::Node getInNode() { result = inNode } |
| 108 | + |
| 109 | + override DataFlow::Node getOutNode() { result = outNode } |
| 110 | +} |
| 111 | + |
| 112 | +class NIDToPointerPassthroughCall extends AlgorithmPassthroughCall { |
| 113 | + DataFlow::Node inNode; |
| 114 | + DataFlow::Node outNode; |
| 115 | + |
| 116 | + NIDToPointerPassthroughCall() { |
| 117 | + this.getTarget().getName() in ["OBJ_nid2obj", "OBJ_nid2ln", "OBJ_nid2sn"] and |
| 118 | + inNode.asExpr() = this.getArgument(0) and |
| 119 | + outNode.asExpr() = this |
| 120 | + //outNode.asIndirectExpr() = this |
| 121 | + } |
| 122 | + |
| 123 | + override DataFlow::Node getInNode() { result = inNode } |
| 124 | + |
| 125 | + override DataFlow::Node getOutNode() { result = outNode } |
| 126 | +} |
| 127 | + |
| 128 | +class PointerToPointerPassthroughCall extends AlgorithmPassthroughCall { |
| 129 | + DataFlow::Node inNode; |
| 130 | + DataFlow::Node outNode; |
| 131 | + |
| 132 | + PointerToPointerPassthroughCall() { |
| 133 | + this.getTarget().getName() = "OBJ_txt2obj" and |
| 134 | + inNode.asIndirectExpr() = this.getArgument(0) and |
| 135 | + outNode.asIndirectExpr() = this |
| 136 | + or |
| 137 | + //outNode.asExpr() = this |
| 138 | + this.getTarget().getName() in ["OBJ_obj2txt", "i2t_ASN1_OBJECT"] and |
| 139 | + inNode.asIndirectExpr() = this.getArgument(2) and |
| 140 | + outNode.asDefiningArgument() = this.getArgument(0) |
| 141 | + } |
| 142 | + |
| 143 | + override DataFlow::Node getInNode() { result = inNode } |
| 144 | + |
| 145 | + override DataFlow::Node getOutNode() { result = outNode } |
| 146 | +} |
| 147 | + |
| 148 | +class PointerToNIDPassthroughCall extends AlgorithmPassthroughCall { |
| 149 | + DataFlow::Node inNode; |
| 150 | + DataFlow::Node outNode; |
| 151 | + |
| 152 | + PointerToNIDPassthroughCall() { |
| 153 | + this.getTarget().getName() in ["OBJ_obj2nid", "OBJ_ln2nid", "OBJ_sn2nid", "OBJ_txt2nid"] and |
| 154 | + ( |
| 155 | + inNode.asIndirectExpr() = this.getArgument(0) |
| 156 | + or |
| 157 | + inNode.asExpr() = this.getArgument(0) |
| 158 | + ) and |
| 159 | + outNode.asExpr() = this |
| 160 | + } |
| 161 | + |
| 162 | + override DataFlow::Node getInNode() { result = inNode } |
| 163 | + |
| 164 | + override DataFlow::Node getOutNode() { result = outNode } |
| 165 | +} |
| 166 | + |
| 167 | +// TODO: pkeys pass through EVP_PKEY_CTX_new and any similar variant |
| 168 | +predicate knownPassThroughStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 169 | + exists(AlgorithmPassthroughCall c | c.getInNode() = node1 and c.getOutNode() = node2) |
| 170 | +} |
0 commit comments