Skip to content

Commit a3c66b6

Browse files
authored
Merge pull request github#12833 from geoffw0/addmodels
Swift: Add some sink models
2 parents 2393429 + c7ea08a commit a3c66b6

File tree

8 files changed

+270
-145
lines changed

8 files changed

+270
-145
lines changed

swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ private module Frameworks {
9797
private import codeql.swift.frameworks.Alamofire.Alamofire
9898
private import codeql.swift.security.CleartextLoggingExtensions
9999
private import codeql.swift.security.CleartextStorageDatabaseExtensions
100+
private import codeql.swift.security.ECBEncryptionExtensions
101+
private import codeql.swift.security.HardcodedEncryptionKeyExtensions
100102
private import codeql.swift.security.PathInjectionExtensions
101103
private import codeql.swift.security.PredicateInjectionExtensions
102104
private import codeql.swift.security.StringLengthConflationExtensions
105+
private import codeql.swift.security.WeakSensitiveDataHashingExtensions
103106
}
104107

105108
/**

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,24 @@ private class RnCryptorEncryptionKeySink extends HardcodedEncryptionKeySink {
5757
] and
5858
c.getAMember() = f and
5959
call.getStaticTarget() = f and
60-
call.getArgumentWithLabel(["encryptionKey", "withEncryptionKey"]).getExpr() = this.asExpr()
60+
call.getArgumentWithLabel(["encryptionKey", "withEncryptionKey", "hmacKey"]).getExpr() =
61+
this.asExpr()
6162
)
6263
}
6364
}
6465

66+
private class EncryptionKeySinks extends SinkModelCsv {
67+
override predicate row(string row) {
68+
row =
69+
[
70+
// Realm database library.
71+
";Realm.Configuration;true;init(fileURL:inMemoryIdentifier:syncConfiguration:encryptionKey:readOnly:schemaVersion:migrationBlock:deleteRealmIfMigrationNeeded:shouldCompactOnLaunch:objectTypes:);;;Argument[3];encryption-key",
72+
";Realm.Configuration;true;init(fileURL:inMemoryIdentifier:syncConfiguration:encryptionKey:readOnly:schemaVersion:migrationBlock:deleteRealmIfMigrationNeeded:shouldCompactOnLaunch:objectTypes:seedFilePath:);;;Argument[3];encryption-key",
73+
";Realm.Configuration;true;encryptionKey;;;;encryption-key",
74+
]
75+
}
76+
}
77+
6578
/**
6679
* A sink defined in a CSV model.
6780
*/

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ private class PathInjectionSinks extends SinkModelCsv {
127127
";DatabasePool;true;init(path:configuration:);;;Argument[0];path-injection",
128128
";DatabaseQueue;true;init(path:configuration:);;;Argument[0];path-injection",
129129
";DatabaseSnapshotPool;true;init(path:configuration:);;;Argument[0];path-injection",
130-
";SerializedDatabase;true;init(path:configuration:defaultLabel:purpose:);;;Argument[0];path-injection"
130+
";SerializedDatabase;true;init(path:configuration:defaultLabel:purpose:);;;Argument[0];path-injection",
131+
// Realm
132+
";Realm.Configuration;true;init(fileURL:inMemoryIdentifier:syncConfiguration:encryptionKey:readOnly:schemaVersion:migrationBlock:deleteRealmIfMigrationNeeded:shouldCompactOnLaunch:objectTypes:);;;Argument[0];path-injection",
133+
";Realm.Configuration;true;init(fileURL:inMemoryIdentifier:syncConfiguration:encryptionKey:readOnly:schemaVersion:migrationBlock:deleteRealmIfMigrationNeeded:shouldCompactOnLaunch:objectTypes:seedFilePath:);;;Argument[0];path-injection",
134+
";Realm.Configuration;true;init(fileURL:inMemoryIdentifier:syncConfiguration:encryptionKey:readOnly:schemaVersion:migrationBlock:deleteRealmIfMigrationNeeded:shouldCompactOnLaunch:objectTypes:seedFilePath:);;;Argument[10];path-injection",
135+
";Realm.Configuration;true;fileURL;;;;path-injection",
136+
";Realm.Configuration;true;seedFilePath;;;;path-injection",
131137
]
132138
}
133139
}

swift/ql/test/query-tests/Security/CWE-022/testPathInjection.swift

Lines changed: 121 additions & 80 deletions
Large diffs are not rendered by default.

swift/ql/test/query-tests/Security/CWE-321/HardcodedEncryptionKey.expected

Lines changed: 75 additions & 61 deletions
Large diffs are not rendered by default.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
// --- stubs ---
3+
4+
class Data {
5+
init<S>(_ elements: S) {}
6+
}
7+
8+
struct URL {
9+
init(fileURLWithPath path: String, isDirectory: Bool) {}
10+
}
11+
12+
class Realm {
13+
}
14+
15+
extension Realm {
16+
struct Configuration {
17+
init(
18+
fileURL: URL? = URL(fileURLWithPath: "defaultFile", isDirectory: false),
19+
inMemoryIdentifier: String? = nil,
20+
syncConfiguration: Int = 0,
21+
encryptionKey: Data? = nil,
22+
readOnly: Bool = false,
23+
schemaVersion: UInt64 = 0,
24+
migrationBlock: Int = 0,
25+
deleteRealmIfMigrationNeeded: Bool = false,
26+
shouldCompactOnLaunch: Bool = false,
27+
objectTypes: Int = 0,
28+
seedFilePath: URL? = nil) { }
29+
30+
var encryptionKey: Data?
31+
}
32+
}
33+
34+
// --- tests ---
35+
36+
func test(myVarStr: String) {
37+
let myVarKey = Data(myVarStr)
38+
let myConstKey = Data("abcdef123456")
39+
40+
_ = Realm.Configuration(encryptionKey: myVarKey) // GOOD
41+
_ = Realm.Configuration(encryptionKey: myConstKey) // BAD
42+
43+
var config = Realm.Configuration() // GOOD
44+
config.encryptionKey = myVarKey // GOOD
45+
config.encryptionKey = myConstKey // BAD [NOT DETECTED]
46+
}

swift/ql/test/query-tests/Security/CWE-321/rncryptor.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ class RNDecryptor : RNCryptor
5252

5353
// --- tests ---
5454

55-
func test() {
55+
func test(var myVarKey: Data, var myHMACKey: Data) {
5656
// RNCryptor
5757
let myEncryptor = RNEncryptor()
5858
let myDecryptor = RNDecryptor()
5959
let myData = Data(0)
6060
let myConstKey = Data("abcdef123456")
61-
let myHMACKey = Data(0)
6261
let myHandler = {}
6362
let myIV = Data(0)
6463

64+
let _ = RNEncryptor(settings: kRNCryptorAES256Settings, encryptionKey: myVarKey, hmacKey: myHMACKey, handler: myHandler) // GOOD
6565
let _ = RNEncryptor(settings: kRNCryptorAES256Settings, encryptionKey: myConstKey, hmacKey: myHMACKey, handler: myHandler) // BAD
6666
let _ = RNEncryptor(settings: kRNCryptorAES256Settings, encryptionKey: myConstKey, HMACKey: myHMACKey, handler: myHandler) // BAD
6767
let _ = RNEncryptor(settings: kRNCryptorAES256Settings, encryptionKey: myConstKey, hmacKey: myHMACKey, iv: myIV, handler: myHandler) // BAD
@@ -79,4 +79,6 @@ func test() {
7979
let _ = try? myDecryptor.decryptData(myData, withEncryptionKey: myConstKey, HMACKey: myHMACKey) // BAD
8080
let _ = try? myDecryptor.decryptData(myData, with: kRNCryptorAES256Settings, encryptionKey: myConstKey, hmacKey: myHMACKey) // BAD
8181
let _ = try? myDecryptor.decryptData(myData, withSettings: kRNCryptorAES256Settings, encryptionKey: myConstKey, HMACKey: myHMACKey) // BAD
82+
83+
let _ = RNEncryptor(settings: kRNCryptorAES256Settings, encryptionKey: myVarKey, hmacKey: myConstKey, handler: myHandler) // BAD
8284
}

0 commit comments

Comments
 (0)