Skip to content

Commit f962eac

Browse files
committed
Swift: Fill the simple gaps in modelling.
1 parent c6c3e14 commit f962eac

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

swift/ql/lib/codeql/swift/security/WeakPasswordHashingExtensions.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ private class WeakSensitiveDataHashingSinks extends SinkModelCsv {
5454
// CryptoKit
5555
// (SHA-256, SHA-384 and SHA-512 are all variants of the SHA-2 algorithm)
5656
";SHA256;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA256",
57+
";SHA256;true;hash(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA256",
5758
";SHA256;true;update(data:);;;Argument[0];weak-password-hash-input-SHA256",
5859
";SHA256;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA256",
5960
";SHA384;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA384",
61+
";SHA384;true;hash(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA384",
6062
";SHA384;true;update(data:);;;Argument[0];weak-password-hash-input-SHA384",
6163
";SHA384;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA384",
6264
";SHA512;true;hash(data:);;;Argument[0];weak-password-hash-input-SHA512",
65+
";SHA512;true;hash(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA512",
6366
";SHA512;true;update(data:);;;Argument[0];weak-password-hash-input-SHA512",
6467
";SHA512;true;update(bufferPointer:);;;Argument[0];weak-password-hash-input-SHA512",
6568
// CryptoSwift
@@ -122,7 +125,7 @@ private class WeakPasswordHashingMetatypeSink extends WeakPasswordHashingSink {
122125
c.getAnArgument().getExpr() = this.asExpr() and
123126
algorithm = ["SHA256", "SHA384", "SHA512"] and
124127
c.getQualifier().getType().getFullName() = algorithm + ["", ".Type"] and
125-
c.getStaticTarget().getName() = ["hash(data:)", "update(data:)", "update(bufferPointer:)"]
128+
c.getStaticTarget().getName() = ["hash(data:)", "hash(bufferPointer:)", "update(data:)", "update(bufferPointer:)"]
126129
)
127130
}
128131

swift/ql/lib/codeql/swift/security/WeakSensitiveDataHashingExtensions.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ private class WeakSensitiveDataHashingSinks extends SinkModelCsv {
4040
[
4141
// CryptoKit
4242
";Insecure.MD5;true;hash(data:);;;Argument[0];weak-hash-input-MD5",
43+
";Insecure.MD5;true;hash(bufferPointer:);;;Argument[0];weak-hash-input-MD5",
4344
";Insecure.MD5;true;update(data:);;;Argument[0];weak-hash-input-MD5",
4445
";Insecure.MD5;true;update(bufferPointer:);;;Argument[0];weak-hash-input-MD5",
4546
";Insecure.SHA1;true;hash(data:);;;Argument[0];weak-hash-input-SHA1",
47+
";Insecure.SHA1;true;hash(bufferPointer:);;;Argument[0];weak-hash-input-SHA1",
4648
";Insecure.SHA1;true;update(data:);;;Argument[0];weak-hash-input-SHA1",
4749
";Insecure.SHA1;true;update(bufferPointer:);;;Argument[0];weak-hash-input-SHA1",
4850
// CryptoSwift
@@ -88,7 +90,7 @@ private class WeakSenitiveDataHashingMetatypeSink extends WeakSensitiveDataHashi
8890
c.getAnArgument().getExpr() = this.asExpr() and
8991
algorithm = ["MD5", "SHA1"] and
9092
c.getQualifier().getType().getFullName() = "Insecure." + algorithm + ["", ".Type"] and
91-
c.getStaticTarget().getName() = ["hash(data:)", "update(data:)", "update(bufferPointer:)"]
93+
c.getStaticTarget().getName() = ["hash(data:)", "hash(bufferPointer:)", "update(data:)", "update(bufferPointer:)"]
9294
)
9395
}
9496

swift/ql/test/query-tests/Security/CWE-328/WeakPasswordHashing.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ edges
33
| testCryptoKit.swift:224:38:224:53 | .utf8 | testCryptoKit.swift:224:33:224:57 | call to Data.init(_:) | provenance | |
44
nodes
55
| testCryptoKit.swift:84:47:84:47 | passwd | semmle.label | passwd |
6+
| testCryptoKit.swift:85:52:85:52 | passwd | semmle.label | passwd |
67
| testCryptoKit.swift:91:36:91:36 | passwd | semmle.label | passwd |
8+
| testCryptoKit.swift:92:45:92:45 | passwd | semmle.label | passwd |
79
| testCryptoKit.swift:98:44:98:44 | passwd | semmle.label | passwd |
10+
| testCryptoKit.swift:99:53:99:53 | passwd | semmle.label | passwd |
811
| testCryptoKit.swift:105:37:105:37 | passwd | semmle.label | passwd |
12+
| testCryptoKit.swift:106:46:106:46 | passwd | semmle.label | passwd |
913
| testCryptoKit.swift:112:37:112:37 | passwd | semmle.label | passwd |
14+
| testCryptoKit.swift:113:46:113:46 | passwd | semmle.label | passwd |
1015
| testCryptoKit.swift:119:37:119:37 | passwd | semmle.label | passwd |
16+
| testCryptoKit.swift:120:46:120:46 | passwd | semmle.label | passwd |
1117
| testCryptoKit.swift:129:23:129:23 | passwd | semmle.label | passwd |
1218
| testCryptoKit.swift:138:23:138:23 | passwd | semmle.label | passwd |
1319
| testCryptoKit.swift:147:23:147:23 | passwd | semmle.label | passwd |
@@ -49,11 +55,17 @@ nodes
4955
subpaths
5056
#select
5157
| testCryptoKit.swift:84:47:84:47 | passwd | testCryptoKit.swift:84:47:84:47 | passwd | testCryptoKit.swift:84:47:84:47 | passwd | Insecure hashing algorithm (MD5) depends on $@. | testCryptoKit.swift:84:47:84:47 | passwd | password (passwd) |
58+
| testCryptoKit.swift:85:52:85:52 | passwd | testCryptoKit.swift:85:52:85:52 | passwd | testCryptoKit.swift:85:52:85:52 | passwd | Insecure hashing algorithm (MD5) depends on $@. | testCryptoKit.swift:85:52:85:52 | passwd | password (passwd) |
5259
| testCryptoKit.swift:91:36:91:36 | passwd | testCryptoKit.swift:91:36:91:36 | passwd | testCryptoKit.swift:91:36:91:36 | passwd | Insecure hashing algorithm (MD5) depends on $@. | testCryptoKit.swift:91:36:91:36 | passwd | password (passwd) |
60+
| testCryptoKit.swift:92:45:92:45 | passwd | testCryptoKit.swift:92:45:92:45 | passwd | testCryptoKit.swift:92:45:92:45 | passwd | Insecure hashing algorithm (MD5) depends on $@. | testCryptoKit.swift:92:45:92:45 | passwd | password (passwd) |
5361
| testCryptoKit.swift:98:44:98:44 | passwd | testCryptoKit.swift:98:44:98:44 | passwd | testCryptoKit.swift:98:44:98:44 | passwd | Insecure hashing algorithm (SHA1) depends on $@. | testCryptoKit.swift:98:44:98:44 | passwd | password (passwd) |
62+
| testCryptoKit.swift:99:53:99:53 | passwd | testCryptoKit.swift:99:53:99:53 | passwd | testCryptoKit.swift:99:53:99:53 | passwd | Insecure hashing algorithm (SHA1) depends on $@. | testCryptoKit.swift:99:53:99:53 | passwd | password (passwd) |
5463
| testCryptoKit.swift:105:37:105:37 | passwd | testCryptoKit.swift:105:37:105:37 | passwd | testCryptoKit.swift:105:37:105:37 | passwd | Insecure hashing algorithm (SHA256) depends on $@. | testCryptoKit.swift:105:37:105:37 | passwd | password (passwd) |
64+
| testCryptoKit.swift:106:46:106:46 | passwd | testCryptoKit.swift:106:46:106:46 | passwd | testCryptoKit.swift:106:46:106:46 | passwd | Insecure hashing algorithm (SHA256) depends on $@. | testCryptoKit.swift:106:46:106:46 | passwd | password (passwd) |
5565
| testCryptoKit.swift:112:37:112:37 | passwd | testCryptoKit.swift:112:37:112:37 | passwd | testCryptoKit.swift:112:37:112:37 | passwd | Insecure hashing algorithm (SHA384) depends on $@. | testCryptoKit.swift:112:37:112:37 | passwd | password (passwd) |
66+
| testCryptoKit.swift:113:46:113:46 | passwd | testCryptoKit.swift:113:46:113:46 | passwd | testCryptoKit.swift:113:46:113:46 | passwd | Insecure hashing algorithm (SHA384) depends on $@. | testCryptoKit.swift:113:46:113:46 | passwd | password (passwd) |
5667
| testCryptoKit.swift:119:37:119:37 | passwd | testCryptoKit.swift:119:37:119:37 | passwd | testCryptoKit.swift:119:37:119:37 | passwd | Insecure hashing algorithm (SHA512) depends on $@. | testCryptoKit.swift:119:37:119:37 | passwd | password (passwd) |
68+
| testCryptoKit.swift:120:46:120:46 | passwd | testCryptoKit.swift:120:46:120:46 | passwd | testCryptoKit.swift:120:46:120:46 | passwd | Insecure hashing algorithm (SHA512) depends on $@. | testCryptoKit.swift:120:46:120:46 | passwd | password (passwd) |
5769
| testCryptoKit.swift:129:23:129:23 | passwd | testCryptoKit.swift:129:23:129:23 | passwd | testCryptoKit.swift:129:23:129:23 | passwd | Insecure hashing algorithm (MD5) depends on $@. | testCryptoKit.swift:129:23:129:23 | passwd | password (passwd) |
5870
| testCryptoKit.swift:138:23:138:23 | passwd | testCryptoKit.swift:138:23:138:23 | passwd | testCryptoKit.swift:138:23:138:23 | passwd | Insecure hashing algorithm (SHA1) depends on $@. | testCryptoKit.swift:138:23:138:23 | passwd | password (passwd) |
5971
| testCryptoKit.swift:147:23:147:23 | passwd | testCryptoKit.swift:147:23:147:23 | passwd | testCryptoKit.swift:147:23:147:23 | passwd | Insecure hashing algorithm (SHA256) depends on $@. | testCryptoKit.swift:147:23:147:23 | passwd | password (passwd) |

swift/ql/test/query-tests/Security/CWE-328/testCryptoKit.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,42 +82,42 @@ enum Insecure {
8282

8383
func testHashMethods(passwd : UnsafeRawBufferPointer, cert: String, encrypted_passwd : String, account_no : String, credit_card_no : String) {
8484
var hash = Crypto.Insecure.MD5.hash(data: passwd) // BAD
85-
hash = Crypto.Insecure.MD5.hash(bufferPointer: passwd) // BAD [NOT DETECTED]
85+
hash = Crypto.Insecure.MD5.hash(bufferPointer: passwd) // BAD
8686
hash = Crypto.Insecure.MD5.hash(data: cert) // BAD
8787
hash = Crypto.Insecure.MD5.hash(data: encrypted_passwd) // GOOD (not sensitive)
8888
hash = Crypto.Insecure.MD5.hash(data: account_no) // BAD
8989
hash = Crypto.Insecure.MD5.hash(data: credit_card_no) // BAD
9090

9191
hash = Insecure.MD5.hash(data: passwd) // BAD
92-
hash = Insecure.MD5.hash(bufferPointer: passwd) // BAD [NOT DETECTED]
92+
hash = Insecure.MD5.hash(bufferPointer: passwd) // BAD
9393
hash = Insecure.MD5.hash(data: cert) // BAD
9494
hash = Insecure.MD5.hash(data: encrypted_passwd) // GOOD (not sensitive)
9595
hash = Insecure.MD5.hash(data: account_no) // BAD
9696
hash = Insecure.MD5.hash(data: credit_card_no) // BAD
9797

9898
hash = Crypto.Insecure.SHA1.hash(data: passwd) // BAD
99-
hash = Crypto.Insecure.SHA1.hash(bufferPointer: passwd) // BAD [NOT DETECTED]
99+
hash = Crypto.Insecure.SHA1.hash(bufferPointer: passwd) // BAD
100100
hash = Crypto.Insecure.SHA1.hash(data: cert) // BAD
101101
hash = Crypto.Insecure.SHA1.hash(data: encrypted_passwd) // GOOD (not sensitive)
102102
hash = Crypto.Insecure.SHA1.hash(data: account_no) // BAD
103103
hash = Crypto.Insecure.SHA1.hash(data: credit_card_no) // BAD
104104

105105
hash = Crypto.SHA256.hash(data: passwd) // BAD, not a computationally expensive hash
106-
hash = Crypto.SHA256.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash [NOT DETECTED]
106+
hash = Crypto.SHA256.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash
107107
hash = Crypto.SHA256.hash(data: cert) // GOOD, computationally expensive hash not required
108108
hash = Crypto.SHA256.hash(data: encrypted_passwd) // GOOD, not sensitive
109109
hash = Crypto.SHA256.hash(data: account_no) // GOOD, computationally expensive hash not required
110110
hash = Crypto.SHA256.hash(data: credit_card_no) // GOOD, computationally expensive hash not required
111111

112112
hash = Crypto.SHA384.hash(data: passwd) // BAD, not a computationally expensive hash
113-
hash = Crypto.SHA384.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash [NOT DETECTED]
113+
hash = Crypto.SHA384.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash
114114
hash = Crypto.SHA384.hash(data: cert) // GOOD, computationally expensive hash not required
115115
hash = Crypto.SHA384.hash(data: encrypted_passwd) // GOOD, not sensitive
116116
hash = Crypto.SHA384.hash(data: account_no) // GOOD, computationally expensive hash not required
117117
hash = Crypto.SHA384.hash(data: credit_card_no) // GOOD, computationally expensive hash not required
118118

119119
hash = Crypto.SHA512.hash(data: passwd) // BAD, not a computationally expensive hash
120-
hash = Crypto.SHA512.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash [NOT DETECTED]
120+
hash = Crypto.SHA512.hash(bufferPointer: passwd) // BAD, not a computationally expensive hash
121121
hash = Crypto.SHA512.hash(data: cert) // GOOD, computationally expensive hash not required
122122
hash = Crypto.SHA512.hash(data: encrypted_passwd) // GOOD, not sensitive
123123
hash = Crypto.SHA512.hash(data: account_no) // GOOD, computationally expensive hash not required

0 commit comments

Comments
 (0)