|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about use of inappropriate |
| 3 | + * cryptographic hashing algorithms on passwords. |
| 4 | + */ |
| 5 | + |
| 6 | +import swift |
| 7 | +import codeql.swift.dataflow.DataFlow |
| 8 | +import codeql.swift.dataflow.ExternalFlow |
| 9 | +private import codeql.swift.security.WeakSensitiveDataHashingExtensions |
| 10 | + |
| 11 | +/** |
| 12 | + * A dataflow sink for weak password hashing vulnerabilities. That is, |
| 13 | + * a `DataFlow::Node` that is passed into a weak password hashing function. |
| 14 | + */ |
| 15 | +abstract class WeakPasswordHashingSink extends DataFlow::Node { |
| 16 | + /** |
| 17 | + * Gets the name of the hashing algorithm, for display. |
| 18 | + */ |
| 19 | + abstract string getAlgorithm(); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * A barrier for weak password hashing vulnerabilities. |
| 24 | + */ |
| 25 | +abstract class WeakPasswordHashingBarrier extends DataFlow::Node { } |
| 26 | + |
| 27 | +/** |
| 28 | + * A unit class for adding additional flow steps. |
| 29 | + */ |
| 30 | +class WeakPasswordHashingAdditionalFlowStep extends Unit { |
| 31 | + /** |
| 32 | + * Holds if the step from `node1` to `node2` should be considered a flow |
| 33 | + * step for paths related to weak password hashing vulnerabilities. |
| 34 | + */ |
| 35 | + abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * A sink inherited from weak sensitive data hashing. Password hashing has |
| 40 | + * stronger requirements than sensitive data hashing, since (in addition to |
| 41 | + * its particular qualities) a password *is* sensitive data. Thus, any sink |
| 42 | + * for the weak sensitive data hashing query is a sink for weak password |
| 43 | + * hashing as well. |
| 44 | + */ |
| 45 | +private class InheritedWeakPasswordHashingSink extends WeakPasswordHashingSink instanceof WeakSensitiveDataHashingSink |
| 46 | +{ |
| 47 | + override string getAlgorithm() { result = this.(WeakSensitiveDataHashingSink).getAlgorithm() } |
| 48 | +} |
| 49 | + |
| 50 | +private class WeakSensitiveDataHashingSinks extends SinkModelCsv { |
| 51 | + override predicate row(string row) { |
| 52 | + row = |
| 53 | + [ |
| 54 | + // CryptoKit |
| 55 | + // (SHA-256, SHA-384 and SHA-512 are all variants of the SHA-2 algorithm) |
| 56 | + ";SHA256;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA256", |
| 57 | + ";SHA256;true;update(data:);;;Argument[0];weak-password-hash-input-SHA256", |
| 58 | + ";SHA256;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA256", |
| 59 | + ";SHA384;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA384", |
| 60 | + ";SHA384;true;update(data:);;;Argument[0];weak-password-hash-input-SHA384", |
| 61 | + ";SHA384;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA384", |
| 62 | + ";SHA512;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA512", |
| 63 | + ";SHA512;true;update(data:);;;Argument[0];weak-password-hash-input-SHA512", |
| 64 | + ";SHA512;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA512", |
| 65 | + // CryptoSwift |
| 66 | + ";SHA2;true;calculate(for:);;;Argument[0];weak-password-hash-input-SHA2", |
| 67 | + ";SHA2;true;callAsFunction(_:);;;Argument[0];weak-password-hash-input-SHA2", |
| 68 | + ";SHA2;true;process64(block:currentHash:);;;Argument[0];weak-password-hash-input-SHA2", |
| 69 | + ";SHA2;true;process32(block:currentHash:);;;Argument[0];weak-password-hash-input-SHA2", |
| 70 | + ";SHA2;true;update(withBytes:isLast:);;;Argument[0];weak-password-hash-input-SHA2", |
| 71 | + ";SHA3;true;calculate(for:);;;Argument[0];weak-password-hash-input-SHA2", |
| 72 | + ";SHA3;true;callAsFunction(_:);;;Argument[0];weak-password-hash-input-SHA2", |
| 73 | + ";SHA3;true;process(block:currentHash:);;;Argument[0];weak-password-hash-input-SHA2", |
| 74 | + ";SHA3;true;update(withBytes:isLast:);;;Argument[0];weak-password-hash-input-SHA2", |
| 75 | + ";Digest;true;sha2(_:variant:);;;Argument[0];weak-password-hash-input-SHA2", |
| 76 | + ";Digest;true;sha3(_:variant:);;;Argument[0];weak-password-hash-input-SHA3", |
| 77 | + ";Digest;true;sha224(_:);;;Argument[0];weak-password-hash-input-SHA224", |
| 78 | + ";Digest;true;sha256(_:);;;Argument[0];weak-password-hash-input-SHA256", |
| 79 | + ";Digest;true;sha384(_:);;;Argument[0];weak-password-hash-input-SHA384", |
| 80 | + ";Digest;true;sha512(_:);;;Argument[0];weak-password-hash-input-SHA512", |
| 81 | + ";Array;true;sha2(_:);;;Argument[-1];weak-password-hash-input-SHA2", |
| 82 | + ";Array;true;sha3(_:);;;Argument[-1];weak-password-hash-input-SHA3", |
| 83 | + ";Array;true;sha224();;;Argument[-1];weak-password-hash-input-SHA224", |
| 84 | + ";Array;true;sha256();;;Argument[-1];weak-password-hash-input-SHA256", |
| 85 | + ";Array;true;sha384();;;Argument[-1];weak-password-hash-input-SHA384", |
| 86 | + ";Array;true;sha512();;;Argument[-1];weak-password-hash-input-SHA512", |
| 87 | + ";Data;true;sha2(_:);;;Argument[-1];weak-password-hash-input-SHA2", |
| 88 | + ";Data;true;sha3(_:);;;Argument[-1];weak-password-hash-input-SHA3", |
| 89 | + ";Data;true;sha224();;;Argument[-1];weak-password-hash-input-SHA224", |
| 90 | + ";Data;true;sha256();;;Argument[-1];weak-password-hash-input-SHA256", |
| 91 | + ";Data;true;sha384();;;Argument[-1];weak-password-hash-input-SHA384", |
| 92 | + ";Data;true;sha512();;;Argument[-1];weak-password-hash-input-SHA512", |
| 93 | + ";String;true;sha2(_:);;;Argument[-1];weak-password-hash-input-SHA2", |
| 94 | + ";String;true;sha3(_:);;;Argument[-1];weak-password-hash-input-SHA3", |
| 95 | + ";String;true;sha224();;;Argument[-1];weak-password-hash-input-SHA224", |
| 96 | + ";String;true;sha256();;;Argument[-1];weak-password-hash-input-SHA256", |
| 97 | + ";String;true;sha384();;;Argument[-1];weak-password-hash-input-SHA384", |
| 98 | + ";String;true;sha512();;;Argument[-1];weak-password-hash-input-SHA512", |
| 99 | + ] |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * A sink defined in a CSV model. |
| 105 | + */ |
| 106 | +private class DefaultWeakPasswordHashingSink extends WeakPasswordHashingSink { |
| 107 | + string algorithm; |
| 108 | + |
| 109 | + DefaultWeakPasswordHashingSink() { sinkNode(this, "weak-password-hash-input-" + algorithm) } |
| 110 | + |
| 111 | + override string getAlgorithm() { result = algorithm } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * A barrier for weak password hashing, when it occurs inside of |
| 116 | + * certain cryptographic algorithms as part of their design. |
| 117 | + */ |
| 118 | +class WeakPasswordHashingImplementationBarrier extends WeakPasswordHashingBarrier { |
| 119 | + WeakPasswordHashingImplementationBarrier() { |
| 120 | + this.asParameter() |
| 121 | + .getDeclaringFunction() |
| 122 | + .(Function) |
| 123 | + .getDeclaringDecl*() |
| 124 | + .(NominalTypeDecl) |
| 125 | + .getName() = ["HMAC", "PBKDF1", "PBKDF2"] |
| 126 | + } |
| 127 | +} |
0 commit comments