|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for detecting "use of a |
| 3 | + * broken or weak cryptographic hashing algorithm on sensitive data" |
| 4 | + * vulnerabilities, as well as extension points for adding your own. This is |
| 5 | + * divided into two general cases: |
| 6 | + * - hashing sensitive data |
| 7 | + * - hashing passwords (which requires the hashing algorithm to be |
| 8 | + * sufficiently computationally expensive in addition to other requirements) |
| 9 | + */ |
| 10 | + |
| 11 | +import rust |
| 12 | +private import codeql.rust.Concepts |
| 13 | +private import codeql.rust.security.SensitiveData |
| 14 | +private import codeql.rust.dataflow.DataFlow |
| 15 | +private import codeql.rust.dataflow.FlowSource |
| 16 | +private import codeql.rust.dataflow.FlowSink |
| 17 | +private import codeql.rust.dataflow.internal.DataFlowImpl |
| 18 | + |
| 19 | +/** |
| 20 | + * Provides default sources, sinks and sanitizers for detecting "use of a broken or weak |
| 21 | + * cryptographic hashing algorithm on sensitive data" vulnerabilities on sensitive data that does |
| 22 | + * NOT require computationally expensive hashing, as well as extension points for adding your own. |
| 23 | + * |
| 24 | + * Also see the `ComputationallyExpensiveHashFunction` module. |
| 25 | + */ |
| 26 | +module NormalHashFunction { |
| 27 | + /** |
| 28 | + * A data flow source for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 29 | + * data" vulnerabilities that does not require computationally expensive hashing. That is, a |
| 30 | + * piece of sensitive data that is not a password. |
| 31 | + */ |
| 32 | + abstract class Source extends DataFlow::Node { |
| 33 | + Source() { not this instanceof ComputationallyExpensiveHashFunction::Source } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the classification of the sensitive data. |
| 37 | + */ |
| 38 | + abstract string getClassification(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * A data flow sink for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 43 | + * data" vulnerabilities that applies to data that does not require computationally expensive |
| 44 | + * hashing. That is, a broken or weak hashing algorithm. |
| 45 | + */ |
| 46 | + abstract class Sink extends DataFlow::Node { |
| 47 | + /** |
| 48 | + * Gets the name of the weak hashing algorithm. |
| 49 | + */ |
| 50 | + abstract string getAlgorithmName(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * A barrier for "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 55 | + * vulnerabilities that applies to data that does not require computationally expensive hashing. |
| 56 | + */ |
| 57 | + abstract class Barrier extends DataFlow::Node { } |
| 58 | + |
| 59 | + /** |
| 60 | + * A flow source modeled by the `SensitiveData` library. |
| 61 | + */ |
| 62 | + class SensitiveDataAsSource extends Source instanceof SensitiveData { |
| 63 | + SensitiveDataAsSource() { |
| 64 | + not SensitiveData.super.getClassification() = SensitiveDataClassification::password() and // (covered in ComputationallyExpensiveHashFunction) |
| 65 | + not SensitiveData.super.getClassification() = SensitiveDataClassification::id() // (not accurate enough) |
| 66 | + } |
| 67 | + |
| 68 | + override SensitiveDataClassification getClassification() { |
| 69 | + result = SensitiveData.super.getClassification() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * A flow sink modeled by the `Cryptography` module. |
| 75 | + */ |
| 76 | + class WeakHashingOperationInputAsSink extends Sink { |
| 77 | + Cryptography::HashingAlgorithm algorithm; |
| 78 | + |
| 79 | + WeakHashingOperationInputAsSink() { |
| 80 | + exists(Cryptography::CryptographicOperation operation | |
| 81 | + algorithm.isWeak() and |
| 82 | + algorithm = operation.getAlgorithm() and |
| 83 | + this = operation.getAnInput() |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Provides default sources, sinks and sanitizers for detecting "use of a broken or weak |
| 93 | + * cryptographic hashing algorithm on sensitive data" vulnerabilities on sensitive data that DOES |
| 94 | + * require computationally expensive hashing, as well as extension points for adding your own. |
| 95 | + * |
| 96 | + * Also see the `NormalHashFunction` module. |
| 97 | + */ |
| 98 | +module ComputationallyExpensiveHashFunction { |
| 99 | + /** |
| 100 | + * A data flow source for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 101 | + * data" vulnerabilities that does require computationally expensive hashing. That is, a |
| 102 | + * password. |
| 103 | + */ |
| 104 | + abstract class Source extends DataFlow::Node { |
| 105 | + /** |
| 106 | + * Gets the classification of the sensitive data. |
| 107 | + */ |
| 108 | + abstract string getClassification(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A data flow sink for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 113 | + * data" vulnerabilities that applies to data that does require computationally expensive |
| 114 | + * hashing. That is, a broken or weak hashing algorithm or one that is not computationally |
| 115 | + * expensive enough for password hashing. |
| 116 | + */ |
| 117 | + abstract class Sink extends DataFlow::Node { |
| 118 | + /** |
| 119 | + * Gets the name of the weak hashing algorithm. |
| 120 | + */ |
| 121 | + abstract string getAlgorithmName(); |
| 122 | + |
| 123 | + /** |
| 124 | + * Holds if this sink is for a computationally expensive hash function (meaning that hash |
| 125 | + * function is just weak in some other regard. |
| 126 | + */ |
| 127 | + abstract predicate isComputationallyExpensive(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * A barrier for "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 132 | + * vulnerabilities that applies to data that does require computationally expensive hashing. |
| 133 | + */ |
| 134 | + abstract class Barrier extends DataFlow::Node { } |
| 135 | + |
| 136 | + /** |
| 137 | + * A flow source modeled by the `SensitiveData` library. |
| 138 | + */ |
| 139 | + class PasswordAsSource extends Source instanceof SensitiveData { |
| 140 | + PasswordAsSource() { |
| 141 | + SensitiveData.super.getClassification() = SensitiveDataClassification::password() |
| 142 | + } |
| 143 | + |
| 144 | + override SensitiveDataClassification getClassification() { |
| 145 | + result = SensitiveData.super.getClassification() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * A flow sink modeled by the `Cryptography` module. |
| 151 | + */ |
| 152 | + class WeakPasswordHashingOperationInputSink extends Sink { |
| 153 | + Cryptography::CryptographicAlgorithm algorithm; |
| 154 | + |
| 155 | + WeakPasswordHashingOperationInputSink() { |
| 156 | + exists(Cryptography::CryptographicOperation operation | |
| 157 | + ( |
| 158 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm and |
| 159 | + algorithm.isWeak() |
| 160 | + or |
| 161 | + algorithm instanceof Cryptography::HashingAlgorithm // Note that HashingAlgorithm and PasswordHashingAlgorithm are disjoint |
| 162 | + ) and |
| 163 | + algorithm = operation.getAlgorithm() and |
| 164 | + this = operation.getAnInput() |
| 165 | + ) |
| 166 | + } |
| 167 | + |
| 168 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 169 | + |
| 170 | + override predicate isComputationallyExpensive() { |
| 171 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * An externally modeled operation that hashes data, for example a call to `md5::Md5::digest(data)`. |
| 178 | + */ |
| 179 | +class ModeledHashOperation extends Cryptography::CryptographicOperation::Range { |
| 180 | + DataFlow::Node input; |
| 181 | + string algorithmName; |
| 182 | + |
| 183 | + ModeledHashOperation() { |
| 184 | + exists(CallExpr call | |
| 185 | + sinkNode(input, "hasher-input") and |
| 186 | + call = input.(Node::FlowSummaryNode).getSinkElement().getCall() and |
| 187 | + call = this.asExpr().getExpr() and |
| 188 | + algorithmName = |
| 189 | + call.getFunction().(PathExpr).getPath().getQualifier().getPart().getNameRef().getText() |
| 190 | + ) |
| 191 | + } |
| 192 | + |
| 193 | + override DataFlow::Node getInitialization() { result = this } |
| 194 | + |
| 195 | + override Cryptography::HashingAlgorithm getAlgorithm() { result.matchesName(algorithmName) } |
| 196 | + |
| 197 | + override DataFlow::Node getAnInput() { result = input } |
| 198 | + |
| 199 | + override Cryptography::BlockMode getBlockMode() { none() } // (does not apply for hashing) |
| 200 | +} |
0 commit comments