|
| 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.dataflow.DataFlow |
| 14 | + |
| 15 | +/** |
| 16 | + * Provides default sources, sinks and sanitizers for detecting "use of a broken or weak |
| 17 | + * cryptographic hashing algorithm on sensitive data" vulnerabilities on sensitive data that does |
| 18 | + * NOT require computationally expensive hashing, as well as extension points for adding your own. |
| 19 | + * |
| 20 | + * Also see the `ComputationallyExpensiveHashFunction` module. |
| 21 | + */ |
| 22 | +module NormalHashFunction { |
| 23 | + /** |
| 24 | + * A data flow source for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 25 | + * data" vulnerabilities that does not require computationally expensive hashing. That is, a |
| 26 | + * piece of sensitive data. |
| 27 | + */ |
| 28 | + abstract class Source extends DataFlow::Node { |
| 29 | + Source() { not this instanceof ComputationallyExpensiveHashFunction::Source } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the classification of the sensitive data. |
| 33 | + */ |
| 34 | + abstract string getClassification(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * A data flow sink for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 39 | + * data" vulnerabilities that applies to data that does not require computationally expensive |
| 40 | + * hashing. That is, a broken or weak hashing algorithm. |
| 41 | + */ |
| 42 | + abstract class Sink extends DataFlow::Node { |
| 43 | + /** |
| 44 | + * Gets the name of the weak hashing algorithm. |
| 45 | + */ |
| 46 | + abstract string getAlgorithmName(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * A barrier for "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 51 | + * vulnerabilities that applies to data that does not require computationally expensive hashing. |
| 52 | + */ |
| 53 | + abstract class Barrier extends DataFlow::Node { } |
| 54 | + |
| 55 | + // TODO: SensitiveDataSourceAsSource |
| 56 | + |
| 57 | + /** |
| 58 | + * A flow sink modelled by the `Cryptography` module. |
| 59 | + */ |
| 60 | + class WeakHashingOperationInputAsSink extends Sink { |
| 61 | + Cryptography::HashingAlgorithm algorithm; |
| 62 | + |
| 63 | + WeakHashingOperationInputAsSink() { |
| 64 | + exists(Cryptography::CryptographicOperation operation | |
| 65 | + algorithm.isWeak() and |
| 66 | + algorithm = operation.getAlgorithm() and |
| 67 | + this = operation.getAnInput() |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Provides default sources, sinks and sanitizers for detecting "use of a broken or weak |
| 77 | + * cryptographic hashing algorithm on sensitive data" vulnerabilities on sensitive data that DOES |
| 78 | + * require computationally expensive hashing, as well as extension points for adding your own. |
| 79 | + * |
| 80 | + * Also see the `NormalHashFunction` module. |
| 81 | + */ |
| 82 | +module ComputationallyExpensiveHashFunction { |
| 83 | + /** |
| 84 | + * A data flow source for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 85 | + * data" vulnerabilities that does require computationally expensive hashing. That is, a |
| 86 | + * password. |
| 87 | + */ |
| 88 | + abstract class Source extends DataFlow::Node { |
| 89 | + /** |
| 90 | + * Gets the classification of the sensitive data. |
| 91 | + */ |
| 92 | + abstract string getClassification(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * A data flow sink for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 97 | + * data" vulnerabilities that applies to data that does require computationally expensive |
| 98 | + * hashing. That is, a broken or weak hashing algorithm or one that is not computationally |
| 99 | + * expensive enough for password hashing. |
| 100 | + */ |
| 101 | + abstract class Sink extends DataFlow::Node { |
| 102 | + /** |
| 103 | + * Gets the name of the weak hashing algorithm. |
| 104 | + */ |
| 105 | + abstract string getAlgorithmName(); |
| 106 | + |
| 107 | + /** |
| 108 | + * Holds if this sink is for a computationally expensive hash function (meaning that hash |
| 109 | + * function is just weak in some other regard. |
| 110 | + */ |
| 111 | + abstract predicate isComputationallyExpensive(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A barrier for "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 116 | + * vulnerabilities that applies to data that does require computationally expensive hashing. |
| 117 | + */ |
| 118 | + abstract class Barrier extends DataFlow::Node { } |
| 119 | + |
| 120 | + // TODO: PasswordSourceAsSource |
| 121 | + |
| 122 | + /** |
| 123 | + * A flow sink modelled by the `Cryptography` module. |
| 124 | + */ |
| 125 | + class WeakPasswordHashingOperationInputSink extends Sink { |
| 126 | + Cryptography::CryptographicAlgorithm algorithm; |
| 127 | + |
| 128 | + WeakPasswordHashingOperationInputSink() { |
| 129 | + exists(Cryptography::CryptographicOperation operation | |
| 130 | + ( |
| 131 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm and |
| 132 | + algorithm.isWeak() |
| 133 | + or |
| 134 | + algorithm instanceof Cryptography::HashingAlgorithm // Note that HashingAlgorithm and PasswordHashingAlgorithm are disjoint |
| 135 | + ) and |
| 136 | + algorithm = operation.getAlgorithm() and |
| 137 | + this = operation.getAnInput() |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 142 | + |
| 143 | + override predicate isComputationallyExpensive() { |
| 144 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments