From 7b744a17fb4cfecb32504af8db3ed43ec78dd583 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:35:01 +0000 Subject: [PATCH 01/12] unified: Copy Swift ECB encryption query and tests unchanged Establish a byte-for-byte baseline for the unified Swift port, including query help, its example, and all Swift test sources. Adaptations follow separately. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../security/CWE-327/ECBEncryption.qhelp | 22 ++ .../queries/security/CWE-327/ECBEncryption.ql | 21 ++ .../security/CWE-327/ECBEncryption.swift | 19 ++ .../ECBEncryption/ECBEncryption.expected | 30 +++ .../CWE-327/ECBEncryption/ECBEncryption.qlref | 2 + .../security/CWE-327/ECBEncryption/test.swift | 78 +++++++ .../ECBEncryption/test_commoncrypto.swift | 217 ++++++++++++++++++ 7 files changed, 389 insertions(+) create mode 100644 unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp create mode 100644 unified/ql/src/queries/security/CWE-327/ECBEncryption.ql create mode 100644 unified/ql/src/queries/security/CWE-327/ECBEncryption.swift create mode 100644 unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected create mode 100644 unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref create mode 100644 unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift create mode 100644 unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp new file mode 100644 index 000000000000..816becf26637 --- /dev/null +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp @@ -0,0 +1,22 @@ + + + +

ECB should not be used as a mode for encryption as it has dangerous weaknesses. Data is encrypted the same way every time, which means that the same plaintext input will always produce the same ciphertext. This behavior makes messages encrypted with ECB + more vulnerable to replay attacks.

+
+ + +

Use a different cipher mode such as CBC.

+
+ + +

The following example shows six cases of instantiating a cipher with various encryption keys and block modes. In the 'BAD' cases, the mode of encryption is ECB, making the encrypted data vulnerable to replay attacks. In the 'GOOD' cases, the encryption mode is CBC, which protects the encrypted data against replay attacks.

+ +
+ + +
  • Wikipedia, block cipher modes of operation, Electronic codebook (ECB).
  • +
    +
    diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql new file mode 100644 index 000000000000..2c4ea1cd69b2 --- /dev/null +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql @@ -0,0 +1,21 @@ +/** + * @name Encryption using ECB + * @description Using the ECB encryption mode makes code vulnerable to replay attacks. + * @kind path-problem + * @problem.severity warning + * @security-severity 7.5 + * @precision high + * @id swift/ecb-encryption + * @tags security + * external/cwe/cwe-327 + */ + +import swift +import codeql.swift.security.ECBEncryptionQuery +import EcbEncryptionFlow::PathGraph + +from EcbEncryptionFlow::PathNode sourceNode, EcbEncryptionFlow::PathNode sinkNode +where EcbEncryptionFlow::flowPath(sourceNode, sinkNode) +select sinkNode.getNode(), sourceNode, sinkNode, + "The initialization of the cipher '" + sinkNode.getNode().toString() + + "' uses the insecure ECB block mode from $@.", sourceNode, sourceNode.getNode().toString() diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift new file mode 100644 index 000000000000..429d185698fb --- /dev/null +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift @@ -0,0 +1,19 @@ + +func encrypt(key : Key, padding : Padding) { + // ... + + // BAD: ECB is used for block mode + let blockMode = ECB() + _ = try AES(key: key, blockMode: blockMode, padding: padding) + _ = try AES(key: key, blockMode: blockMode) + _ = try Blowfish(key: key, blockMode: blockMode, padding: padding) + + // GOOD: ECB is not used for block mode + let aesBlockMode = CBC(iv: AES.randomIV(AES.blockSize)) + let blowfishBlockMode = CBC(iv: Blowfish.randomIV(Blowfish.blockSize)) + _ = try AES(key: key, blockMode: aesBlockMode, padding: padding) + _ = try AES(key: key, blockMode: aesBlockMode) + _ = try Blowfish(key: key, blockMode: blowfishBlockMode, padding: padding) + + // ... +} diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected new file mode 100644 index 000000000000..32c4082abb92 --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected @@ -0,0 +1,30 @@ +edges +| test.swift:39:9:39:13 | call to ECB.init() | test.swift:60:37:60:53 | call to getECBBlockMode() | provenance | | +| test.swift:39:9:39:13 | call to ECB.init() | test.swift:61:37:61:53 | call to getECBBlockMode() | provenance | | +| test.swift:39:9:39:13 | call to ECB.init() | test.swift:73:42:73:58 | call to getECBBlockMode() | provenance | | +| test.swift:50:12:50:16 | call to ECB.init() | test.swift:56:37:56:37 | ecb | provenance | | +| test.swift:50:12:50:16 | call to ECB.init() | test.swift:57:37:57:37 | ecb | provenance | | +| test.swift:50:12:50:16 | call to ECB.init() | test.swift:71:42:71:42 | ecb | provenance | | +nodes +| test.swift:39:9:39:13 | call to ECB.init() | semmle.label | call to ECB.init() | +| test.swift:50:12:50:16 | call to ECB.init() | semmle.label | call to ECB.init() | +| test.swift:56:37:56:37 | ecb | semmle.label | ecb | +| test.swift:57:37:57:37 | ecb | semmle.label | ecb | +| test.swift:58:37:58:41 | call to ECB.init() | semmle.label | call to ECB.init() | +| test.swift:59:37:59:41 | call to ECB.init() | semmle.label | call to ECB.init() | +| test.swift:60:37:60:53 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | +| test.swift:61:37:61:53 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | +| test.swift:71:42:71:42 | ecb | semmle.label | ecb | +| test.swift:72:42:72:46 | call to ECB.init() | semmle.label | call to ECB.init() | +| test.swift:73:42:73:58 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | +subpaths +#select +| test.swift:56:37:56:37 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:56:37:56:37 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | +| test.swift:57:37:57:37 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:57:37:57:37 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | +| test.swift:58:37:58:41 | call to ECB.init() | test.swift:58:37:58:41 | call to ECB.init() | test.swift:58:37:58:41 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:58:37:58:41 | call to ECB.init() | call to ECB.init() | +| test.swift:59:37:59:41 | call to ECB.init() | test.swift:59:37:59:41 | call to ECB.init() | test.swift:59:37:59:41 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:59:37:59:41 | call to ECB.init() | call to ECB.init() | +| test.swift:60:37:60:53 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:60:37:60:53 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | +| test.swift:61:37:61:53 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:61:37:61:53 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | +| test.swift:71:42:71:42 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:71:42:71:42 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | +| test.swift:72:42:72:46 | call to ECB.init() | test.swift:72:42:72:46 | call to ECB.init() | test.swift:72:42:72:46 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:72:42:72:46 | call to ECB.init() | call to ECB.init() | +| test.swift:73:42:73:58 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:73:42:73:58 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref new file mode 100644 index 000000000000..bee507b1cd09 --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref @@ -0,0 +1,2 @@ +query: queries/Security/CWE-327/ECBEncryption.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift new file mode 100644 index 000000000000..2eb39595b938 --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift @@ -0,0 +1,78 @@ + +// --- stubs --- + +// These stubs roughly follows the same structure as classes from CryptoSwift +class AES +{ + init(key: Array, blockMode: BlockMode, padding: Padding) { } + init(key: Array, blockMode: BlockMode) { } +} + +class Blowfish +{ + init(key: Array, blockMode: BlockMode, padding: Padding) { } +} + +protocol BlockMode { } + +struct ECB: BlockMode { + init() { } +} + +struct CBC: BlockMode { + init(iv: Array) { } +} + +protocol PaddingProtocol { } + +enum Padding: PaddingProtocol { + case noPadding, zeroPadding, pkcs7, pkcs5, eme_pkcs1v15, emsa_pkcs1v15, iso78164, iso10126 +} + +// Create some inter-procedural dependencies + +func getRandomArray() -> Array { + (0..<10).map({ _ in UInt8.random(in: 0...UInt8.max) }) +} + +func getECBBlockMode() -> BlockMode { + return ECB() // $ Source +} + +func getCBCBlockMode() -> BlockMode { + return CBC(iv: getRandomArray()) +} + +// --- tests --- + +func test1() { + let key: Array = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f] + let ecb = ECB() // $ Source + let iv = getRandomArray() + let cbc = CBC(iv: iv) + let padding = Padding.noPadding + + // AES test cases + let ab1 = AES(key: key, blockMode: ecb, padding: padding) // $ Alert + let ab2 = AES(key: key, blockMode: ecb) // $ Alert + let ab3 = AES(key: key, blockMode: ECB(), padding: padding) // $ Alert + let ab4 = AES(key: key, blockMode: ECB()) // $ Alert + let ab5 = AES(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert + let ab6 = AES(key: key, blockMode: getECBBlockMode()) // $ Alert + + let ag1 = AES(key: key, blockMode: cbc, padding: padding) // GOOD + let ag2 = AES(key: key, blockMode: cbc) // GOOD + let ag3 = AES(key: key, blockMode: CBC(iv: iv), padding: padding) // GOOD + let ag4 = AES(key: key, blockMode: CBC(iv: iv)) // GOOD + let ag5 = AES(key: key, blockMode: getCBCBlockMode(), padding: padding) // GOOD + let ag6 = AES(key: key, blockMode: getCBCBlockMode()) // GOOD + + // Blowfish test cases + let bb1 = Blowfish(key: key, blockMode: ecb, padding: padding) // $ Alert + let bb2 = Blowfish(key: key, blockMode: ECB(), padding: padding) // $ Alert + let bb3 = Blowfish(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert + + let bg1 = Blowfish(key: key, blockMode: cbc, padding: padding) // GOOD + let bg2 = Blowfish(key: key, blockMode: CBC(iv: iv), padding: padding) // GOOD + let bg3 = Blowfish(key: key, blockMode: getCBCBlockMode(), padding: padding) // GOOD +} diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift new file mode 100644 index 000000000000..df32d3e477c4 --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift @@ -0,0 +1,217 @@ +// --- stubs --- + +struct Data { + func withUnsafeBytes( + _ body: (UnsafeRawBufferPointer) throws -> ResultType + ) rethrows -> ResultType { return 0 as! ResultType } + mutating func withUnsafeMutableBytes( + _ body: (UnsafeMutableRawBufferPointer) throws -> ResultType + ) rethrows -> ResultType { return 0 as! ResultType } +} + +// --- CommonCryptor --- +// (real world projects will import the CommonCryptor headers which get +// converted to Swift by the compiler; the following is an approximation +// of that derived from QL queries and the CommonCryptor header files) + +var kCCSuccess : Int = 0 +typealias CCCryptorStatus = Int32 + +typealias CCCryptorRef = OpaquePointer + +var kCCEncrypt : Int = 0 +var kCCDecrypt : Int = 1 +typealias CCOperation = UInt32 + +var kCCAlgorithmAES128 : Int = 0 +var kCCAlgorithmAES : Int = 0 +var kCCAlgorithmDES : Int = 1 +var kCCAlgorithm3DES : Int = 2 +var kCCAlgorithmCAST : Int = 3 +var kCCAlgorithmRC4 : Int = 4 +var kCCAlgorithmRC2 : Int = 5 +var kCCAlgorithmBlowfish : Int = 6 +typealias CCAlgorithm = UInt32 + +var kCCOptionPKCS7Padding : Int = 1 +var kCCOptionECBMode : Int = 2 +typealias CCOptions = UInt32 + +var kCCModeECB : Int = 1 +var kCCModeCBC : Int = 2 +var kCCModeCFB : Int = 3 +var kCCModeCTR : Int = 4 +var kCCModeOFB : Int = 7 +var kCCModeRC4 : Int = 9 +var kCCModeCFB8 : Int = 10 +typealias CCMode = UInt32 + +typealias CCPadding = UInt32 + +typealias CCModeOptions = UInt32 + +func CCCryptorCreate( + _ op: CCOperation, + _ alg: CCAlgorithm, + _ options: CCOptions, + _ key: UnsafeRawPointer?, + _ keyLength: Int, + _ iv: UnsafeRawPointer?, + _ cryptorRef: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +func CCCryptorCreateFromData( + _ op: CCOperation, + _ alg: CCAlgorithm, + _ options: CCOptions, + _ key: UnsafeRawPointer?, + _ keyLength: Int, + _ iv: UnsafeRawPointer?, + _ data: UnsafeRawPointer?, + _ dataLength: Int, + _ cryptorRef: UnsafeMutablePointer?, + _ dataUsed: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +func CCCryptorCreateWithMode( + _ op: CCOperation, + _ mode: CCMode, + _ alg: CCAlgorithm, + _ padding: CCPadding, + _ iv: UnsafeRawPointer?, + _ key: UnsafeRawPointer?, + _ keyLength: Int, + _ tweak: UnsafeRawPointer?, + _ tweakLength: Int, + _ numRounds: Int32, + _ options: CCModeOptions, + _ cryptorRef: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +func CCCryptorUpdate( + _ cryptorRef: CCCryptorRef?, + _ dataIn: UnsafeRawPointer?, + _ dataInLength: Int, + _ dataOut: UnsafeMutableRawPointer?, + _ dataOutAvailable: Int, + _ dataOutMoved: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +func CCCryptorFinal( + _ cryptorRef: CCCryptorRef?, + _ dataOut: UnsafeMutableRawPointer?, + _ dataOutAvailable: Int, + _ dataOutMoved: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +func CCCrypt( + _ op: CCOperation, + _ alg: CCAlgorithm, + _ options: CCOptions, + _ key: UnsafeRawPointer?, + _ keyLength: Int, + _ iv: UnsafeRawPointer?, + _ dataIn: UnsafeRawPointer?, + _ dataInLength: Int, + _ dataOut: UnsafeMutableRawPointer?, + _ dataOutAvailable: Int, + _ dataOutMoved: UnsafeMutablePointer? + ) -> CCCryptorStatus { return 0 } + +// --- tests --- + +func cond() -> Bool { return true } + +func test_commoncrypto1(key: Data, iv: Data, dataIn: Data, dataOut: inout Data) { + // semi-realistic test case + var myCryptor: CCCryptorRef? + var dataOutWritten = 0 + + /*key.withUnsafeBytes({ + keyPtr in + iv.withUnsafeBytes({ + // create the cryptor object + ivPtr in + let result1 = CCCryptorCreate( + CCOperation(kCCEncrypt), + CCAlgorithm(kCCAlgorithm3DES), // BAD [NOT DETECTED] + CCOptions(0), + keyPtr.baseAddress!, + keyPtr.count, + ivPtr.baseAddress!, + &myCryptor + ) + guard result1 == CCCryptorStatus(kCCSuccess) else { + return // fail + } + + dataIn.withUnsafeBytes({ + dataInPtr in + dataOut.withUnsafeMutableBytes({ + dataOutPtr in + // encrypt data + while (cond()) { + let result2 = CCCryptorUpdate( + myCryptor, + dataInPtr.baseAddress!, + dataInPtr.count, + dataOutPtr.baseAddress!, + dataOutPtr.count, + &dataOutWritten) + guard result2 == CCCryptorStatus(kCCSuccess) else { + return // fail + } + } + + // finish + let result3 = CCCryptorFinal( + myCryptor, + dataOutPtr.baseAddress!, + dataOutPtr.count, + &dataOutWritten) + guard result3 == CCCryptorStatus(kCCSuccess) else { + return // fail + } + }) + }) + }) + })*/ +} + +func test_commoncrypto2( + key: UnsafeRawPointer, keyLen: Int, + iv: UnsafeRawPointer, + dataIn: UnsafeRawPointer, dataInLen: Int, + dataOut: UnsafeMutableRawPointer, dataOutAvail: Int) { + var myCryptor: CCCryptorRef? + var dataOutWritten = 0 + + // algorithms + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmDES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmCAST), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC4), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC2), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmBlowfish), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, &myCryptor) // BAD [NOT DETECTED] + _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] + _ = CCCryptorCreateFromData(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] + + // block modes (the default is CBC) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(0), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, &myCryptor) // BAD [NOT DETECTED] + _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] + + // modes + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeECB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) // BAD [NOT DETECTED] + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCBC), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCFB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCTR), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeOFB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeRC4), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCFB8), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) +} From 231540426f1fcd9b1276aca10ad9b5c89cff774b Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:38:30 +0000 Subject: [PATCH 02/12] unified: Collect original Swift ECB configuration and models Inline the data flow configuration and query-specific models unchanged from ECBEncryptionQuery.qll and ECBEncryptionExtensions.qll. Mark their provenance before adapting to unified APIs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../queries/security/CWE-327/ECBEncryption.ql | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql index 2c4ea1cd69b2..cf61336856d4 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql @@ -11,9 +11,105 @@ */ import swift -import codeql.swift.security.ECBEncryptionQuery +import codeql.swift.dataflow.DataFlow +import codeql.swift.dataflow.TaintTracking +import codeql.swift.dataflow.ExternalFlow import EcbEncryptionFlow::PathGraph +// Copied from swift/ql/lib/codeql/swift/security/ECBEncryptionQuery.qll. +/** + * Provides a taint tracking configuration to find encryption using the + * ECB encryption mode. + */ + +/** + * A data flow configuration from a creation of an ECB mode instance to expressions that use + * it to initialize a cipher. + */ +module EcbEncryptionConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { node instanceof EcbEncryptionSource } + + predicate isSink(DataFlow::Node node) { node instanceof EcbEncryptionSink } + + predicate isBarrier(DataFlow::Node node) { node instanceof EcbEncryptionBarrier } + + predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { + any(EcbEncryptionAdditionalFlowStep s).step(nodeFrom, nodeTo) + } + + predicate observeDiffInformedIncrementalMode() { any() } +} + +module EcbEncryptionFlow = DataFlow::Global; + +// Copied from swift/ql/lib/codeql/swift/security/ECBEncryptionExtensions.qll. +/** + * Provides classes and predicates for reasoning about encryption using the + * ECB encryption mode. + */ + +/** + * A dataflow source for ECB encryption vulnerabilities. That is, + * a `DataFlow::Node` of something that specifies a block mode + * cipher. + */ +abstract class EcbEncryptionSource extends DataFlow::Node { } + +/** + * A dataflow sink for ECB encryption vulnerabilities. That is, + * a `DataFlow::Node` of something that is used as the block mode + * of a cipher. + */ +abstract class EcbEncryptionSink extends DataFlow::Node { } + +/** + * A barrier for ECB encryption vulnerabilities. + */ +abstract class EcbEncryptionBarrier extends DataFlow::Node { } + +/** + * A unit class for adding additional flow steps. + */ +class EcbEncryptionAdditionalFlowStep extends Unit { + /** + * Holds if the step from `node1` to `node2` should be considered a flow + * step for paths related to ECB encryption vulnerabilities. + */ + abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); +} + +/** + * A block mode for the CryptoSwift library. + */ +private class CryptoSwiftEcb extends EcbEncryptionSource { + CryptoSwiftEcb() { + exists(CallExpr call | + call.getStaticTarget().(Method).hasQualifiedName("ECB", "init()") and + this.asExpr() = call + ) + } +} + +private class EcbEncryptionSinks extends SinkModelCsv { + override predicate row(string row) { + row = + [ + // CryptoSwift `AES.init` block mode + ";AES;true;init(key:blockMode:);;;Argument[1];encryption-block-mode", + ";AES;true;init(key:blockMode:padding:);;;Argument[1];encryption-block-mode", + // CryptoSwift `Blowfish.init` block mode + ";Blowfish;true;init(key:blockMode:padding:);;;Argument[1];encryption-block-mode", + ] + } +} + +/** + * A sink defined in a CSV model. + */ +private class DefaultEcbEncryptionSink extends EcbEncryptionSink { + DefaultEcbEncryptionSink() { sinkNode(this, "encryption-block-mode") } +} + from EcbEncryptionFlow::PathNode sourceNode, EcbEncryptionFlow::PathNode sinkNode where EcbEncryptionFlow::flowPath(sourceNode, sinkNode) select sinkNode.getNode(), sourceNode, sinkNode, From dea88e1167f7850e1e6744ca057611c3b3e4874e Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:40:49 +0000 Subject: [PATCH 03/12] unified: Port ECB encryption flow to models-as-data Model CryptoSwift ECB construction and reuse existing AES and Blowfish sinks with value-preserving global flow. All nine original CryptoSwift cases are detected; record four pre-existing CommonCrypto misses as inline expectations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- unified/ql/lib/ext/ecb-encryption.model.yml | 6 ++ .../queries/security/CWE-327/ECBEncryption.ql | 98 ++----------------- .../ECBEncryption/ECBEncryption.expected | 56 ++++++----- .../CWE-327/ECBEncryption/ECBEncryption.qlref | 5 +- .../ECBEncryption/test_commoncrypto.swift | 8 +- 5 files changed, 50 insertions(+), 123 deletions(-) create mode 100644 unified/ql/lib/ext/ecb-encryption.model.yml diff --git a/unified/ql/lib/ext/ecb-encryption.model.yml b/unified/ql/lib/ext/ecb-encryption.model.yml new file mode 100644 index 000000000000..66e10e09fda9 --- /dev/null +++ b/unified/ql/lib/ext/ecb-encryption.model.yml @@ -0,0 +1,6 @@ +extensions: + - addsTo: + pack: codeql/unified-all + extensible: legacySourceModel + data: # namespace, type, subtypes, name, signature, ext, output, kind, provenance + - ["", "ECB", true, "init()", "", "", "ReturnValue", "ecb-block-mode", "manual"] diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql index cf61336856d4..edee13682396 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql @@ -5,113 +5,31 @@ * @problem.severity warning * @security-severity 7.5 * @precision high - * @id swift/ecb-encryption + * @id unified/swift/ecb-encryption * @tags security * external/cwe/cwe-327 */ -import swift -import codeql.swift.dataflow.DataFlow -import codeql.swift.dataflow.TaintTracking -import codeql.swift.dataflow.ExternalFlow -import EcbEncryptionFlow::PathGraph - -// Copied from swift/ql/lib/codeql/swift/security/ECBEncryptionQuery.qll. -/** - * Provides a taint tracking configuration to find encryption using the - * ECB encryption mode. - */ +import unified /** * A data flow configuration from a creation of an ECB mode instance to expressions that use * it to initialize a cipher. */ module EcbEncryptionConfig implements DataFlow::ConfigSig { - predicate isSource(DataFlow::Node node) { node instanceof EcbEncryptionSource } - - predicate isSink(DataFlow::Node node) { node instanceof EcbEncryptionSink } + predicate isSource(DataFlow::Node node) { Models::isSource(node, "ecb-block-mode") } - predicate isBarrier(DataFlow::Node node) { node instanceof EcbEncryptionBarrier } - - predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { - any(EcbEncryptionAdditionalFlowStep s).step(nodeFrom, nodeTo) - } - - predicate observeDiffInformedIncrementalMode() { any() } + predicate isSink(DataFlow::Node node) { Models::isSink(node, "encryption-block-mode") } + // TODO: Model CommonCrypto ECB options and modes, also unsupported by original Swift. } module EcbEncryptionFlow = DataFlow::Global; -// Copied from swift/ql/lib/codeql/swift/security/ECBEncryptionExtensions.qll. -/** - * Provides classes and predicates for reasoning about encryption using the - * ECB encryption mode. - */ - -/** - * A dataflow source for ECB encryption vulnerabilities. That is, - * a `DataFlow::Node` of something that specifies a block mode - * cipher. - */ -abstract class EcbEncryptionSource extends DataFlow::Node { } - -/** - * A dataflow sink for ECB encryption vulnerabilities. That is, - * a `DataFlow::Node` of something that is used as the block mode - * of a cipher. - */ -abstract class EcbEncryptionSink extends DataFlow::Node { } - -/** - * A barrier for ECB encryption vulnerabilities. - */ -abstract class EcbEncryptionBarrier extends DataFlow::Node { } - -/** - * A unit class for adding additional flow steps. - */ -class EcbEncryptionAdditionalFlowStep extends Unit { - /** - * Holds if the step from `node1` to `node2` should be considered a flow - * step for paths related to ECB encryption vulnerabilities. - */ - abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); -} - -/** - * A block mode for the CryptoSwift library. - */ -private class CryptoSwiftEcb extends EcbEncryptionSource { - CryptoSwiftEcb() { - exists(CallExpr call | - call.getStaticTarget().(Method).hasQualifiedName("ECB", "init()") and - this.asExpr() = call - ) - } -} - -private class EcbEncryptionSinks extends SinkModelCsv { - override predicate row(string row) { - row = - [ - // CryptoSwift `AES.init` block mode - ";AES;true;init(key:blockMode:);;;Argument[1];encryption-block-mode", - ";AES;true;init(key:blockMode:padding:);;;Argument[1];encryption-block-mode", - // CryptoSwift `Blowfish.init` block mode - ";Blowfish;true;init(key:blockMode:padding:);;;Argument[1];encryption-block-mode", - ] - } -} - -/** - * A sink defined in a CSV model. - */ -private class DefaultEcbEncryptionSink extends EcbEncryptionSink { - DefaultEcbEncryptionSink() { sinkNode(this, "encryption-block-mode") } -} +import EcbEncryptionFlow::PathGraph from EcbEncryptionFlow::PathNode sourceNode, EcbEncryptionFlow::PathNode sinkNode where EcbEncryptionFlow::flowPath(sourceNode, sinkNode) select sinkNode.getNode(), sourceNode, sinkNode, "The initialization of the cipher '" + sinkNode.getNode().toString() + - "' uses the insecure ECB block mode from $@.", sourceNode, sourceNode.getNode().toString() + "' uses the insecure ECB block mode from $@.", sourceNode.getNode(), + sourceNode.getNode().toString() diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected index 32c4082abb92..bfd54bef4e9a 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected @@ -1,30 +1,32 @@ +#select +| test.swift:56:37:56:39 | ecb | test.swift:50:12:50:16 | ECB(...) | test.swift:56:37:56:39 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | ECB(...) | ECB(...) | +| test.swift:57:37:57:39 | ecb | test.swift:50:12:50:16 | ECB(...) | test.swift:57:37:57:39 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | ECB(...) | ECB(...) | +| test.swift:58:37:58:41 | ECB(...) | test.swift:58:37:58:41 | ECB(...) | test.swift:58:37:58:41 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:58:37:58:41 | ECB(...) | ECB(...) | +| test.swift:59:37:59:41 | ECB(...) | test.swift:59:37:59:41 | ECB(...) | test.swift:59:37:59:41 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:59:37:59:41 | ECB(...) | ECB(...) | +| test.swift:60:37:60:53 | getECBBlockMode(...) | test.swift:39:9:39:13 | ECB(...) | test.swift:60:37:60:53 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | ECB(...) | ECB(...) | +| test.swift:61:37:61:53 | getECBBlockMode(...) | test.swift:39:9:39:13 | ECB(...) | test.swift:61:37:61:53 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | ECB(...) | ECB(...) | +| test.swift:71:42:71:44 | ecb | test.swift:50:12:50:16 | ECB(...) | test.swift:71:42:71:44 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | ECB(...) | ECB(...) | +| test.swift:72:42:72:46 | ECB(...) | test.swift:72:42:72:46 | ECB(...) | test.swift:72:42:72:46 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:72:42:72:46 | ECB(...) | ECB(...) | +| test.swift:73:42:73:58 | getECBBlockMode(...) | test.swift:39:9:39:13 | ECB(...) | test.swift:73:42:73:58 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | ECB(...) | ECB(...) | edges -| test.swift:39:9:39:13 | call to ECB.init() | test.swift:60:37:60:53 | call to getECBBlockMode() | provenance | | -| test.swift:39:9:39:13 | call to ECB.init() | test.swift:61:37:61:53 | call to getECBBlockMode() | provenance | | -| test.swift:39:9:39:13 | call to ECB.init() | test.swift:73:42:73:58 | call to getECBBlockMode() | provenance | | -| test.swift:50:12:50:16 | call to ECB.init() | test.swift:56:37:56:37 | ecb | provenance | | -| test.swift:50:12:50:16 | call to ECB.init() | test.swift:57:37:57:37 | ecb | provenance | | -| test.swift:50:12:50:16 | call to ECB.init() | test.swift:71:42:71:42 | ecb | provenance | | +| test.swift:39:9:39:13 | ECB(...) | test.swift:60:37:60:53 | getECBBlockMode(...) | provenance | | +| test.swift:39:9:39:13 | ECB(...) | test.swift:61:37:61:53 | getECBBlockMode(...) | provenance | | +| test.swift:39:9:39:13 | ECB(...) | test.swift:73:42:73:58 | getECBBlockMode(...) | provenance | | +| test.swift:50:6:50:8 | ecb | test.swift:56:37:56:39 | ecb | provenance | | +| test.swift:50:6:50:8 | ecb | test.swift:57:37:57:39 | ecb | provenance | | +| test.swift:50:6:50:8 | ecb | test.swift:71:42:71:44 | ecb | provenance | | +| test.swift:50:12:50:16 | ECB(...) | test.swift:50:6:50:8 | ecb | provenance | | nodes -| test.swift:39:9:39:13 | call to ECB.init() | semmle.label | call to ECB.init() | -| test.swift:50:12:50:16 | call to ECB.init() | semmle.label | call to ECB.init() | -| test.swift:56:37:56:37 | ecb | semmle.label | ecb | -| test.swift:57:37:57:37 | ecb | semmle.label | ecb | -| test.swift:58:37:58:41 | call to ECB.init() | semmle.label | call to ECB.init() | -| test.swift:59:37:59:41 | call to ECB.init() | semmle.label | call to ECB.init() | -| test.swift:60:37:60:53 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | -| test.swift:61:37:61:53 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | -| test.swift:71:42:71:42 | ecb | semmle.label | ecb | -| test.swift:72:42:72:46 | call to ECB.init() | semmle.label | call to ECB.init() | -| test.swift:73:42:73:58 | call to getECBBlockMode() | semmle.label | call to getECBBlockMode() | +| test.swift:39:9:39:13 | ECB(...) | semmle.label | ECB(...) | +| test.swift:50:6:50:8 | ecb | semmle.label | ecb | +| test.swift:50:12:50:16 | ECB(...) | semmle.label | ECB(...) | +| test.swift:56:37:56:39 | ecb | semmle.label | ecb | +| test.swift:57:37:57:39 | ecb | semmle.label | ecb | +| test.swift:58:37:58:41 | ECB(...) | semmle.label | ECB(...) | +| test.swift:59:37:59:41 | ECB(...) | semmle.label | ECB(...) | +| test.swift:60:37:60:53 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | +| test.swift:61:37:61:53 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | +| test.swift:71:42:71:44 | ecb | semmle.label | ecb | +| test.swift:72:42:72:46 | ECB(...) | semmle.label | ECB(...) | +| test.swift:73:42:73:58 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | subpaths -#select -| test.swift:56:37:56:37 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:56:37:56:37 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | -| test.swift:57:37:57:37 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:57:37:57:37 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | -| test.swift:58:37:58:41 | call to ECB.init() | test.swift:58:37:58:41 | call to ECB.init() | test.swift:58:37:58:41 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:58:37:58:41 | call to ECB.init() | call to ECB.init() | -| test.swift:59:37:59:41 | call to ECB.init() | test.swift:59:37:59:41 | call to ECB.init() | test.swift:59:37:59:41 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:59:37:59:41 | call to ECB.init() | call to ECB.init() | -| test.swift:60:37:60:53 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:60:37:60:53 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | -| test.swift:61:37:61:53 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:61:37:61:53 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | -| test.swift:71:42:71:42 | ecb | test.swift:50:12:50:16 | call to ECB.init() | test.swift:71:42:71:42 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:50:12:50:16 | call to ECB.init() | call to ECB.init() | -| test.swift:72:42:72:46 | call to ECB.init() | test.swift:72:42:72:46 | call to ECB.init() | test.swift:72:42:72:46 | call to ECB.init() | The initialization of the cipher 'call to ECB.init()' uses the insecure ECB block mode from $@. | test.swift:72:42:72:46 | call to ECB.init() | call to ECB.init() | -| test.swift:73:42:73:58 | call to getECBBlockMode() | test.swift:39:9:39:13 | call to ECB.init() | test.swift:73:42:73:58 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:39:9:39:13 | call to ECB.init() | call to ECB.init() | diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref index bee507b1cd09..ae476329e7a5 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref @@ -1,2 +1,3 @@ -query: queries/Security/CWE-327/ECBEncryption.ql -postprocess: utils/test/InlineExpectationsTestQuery.ql +query: queries/security/CWE-327/ECBEncryption.ql +postprocess: + - utils/test/InlineExpectationsTestQuery.ql diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift index df32d3e477c4..4555d6b02828 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift @@ -202,12 +202,12 @@ func test_commoncrypto2( // block modes (the default is CBC) _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(0), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] - _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, &myCryptor) // BAD [NOT DETECTED] - _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // $ MISSING: Alert + _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, &myCryptor) // $ MISSING: Alert + _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // $ MISSING: Alert // modes - _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeECB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) // BAD [NOT DETECTED] + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeECB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) // $ MISSING: Alert _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCBC), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCFB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCTR), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) From f0b4bda260e1cf1243c973f3e57a4b3a0d5ff416 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:10:26 +0000 Subject: [PATCH 04/12] unified: Correct ECB encryption security guidance Describe ECB's plaintext-pattern leakage rather than replay attacks. Recommend authenticated encryption and clarify that the CBC example needs ciphertext authentication and does not by itself prevent replay. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ql/src/queries/security/CWE-327/ECBEncryption.qhelp | 7 +++---- unified/ql/src/queries/security/CWE-327/ECBEncryption.ql | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp index 816becf26637..7572c614b3ad 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp @@ -3,16 +3,15 @@ "qhelp.dtd"> -

    ECB should not be used as a mode for encryption as it has dangerous weaknesses. Data is encrypted the same way every time, which means that the same plaintext input will always produce the same ciphertext. This behavior makes messages encrypted with ECB - more vulnerable to replay attacks.

    +

    ECB should not be used as a mode for encryption. With a fixed key, identical plaintext blocks always produce identical ciphertext blocks. This reveals patterns in the plaintext, even when the ciphertext cannot be decrypted.

    -

    Use a different cipher mode such as CBC.

    +

    Use an authenticated encryption mode such as GCM where available. If CBC is used, generate a fresh, unpredictable initialization vector for each encryption and use a separate message authentication mechanism to protect ciphertext integrity. Changing the cipher mode alone does not prevent replay attacks.

    -

    The following example shows six cases of instantiating a cipher with various encryption keys and block modes. In the 'BAD' cases, the mode of encryption is ECB, making the encrypted data vulnerable to replay attacks. In the 'GOOD' cases, the encryption mode is CBC, which protects the encrypted data against replay attacks.

    +

    The following example shows six cases of instantiating a cipher with different block modes. In the 'BAD' cases, the mode of encryption is ECB, which reveals patterns in the plaintext. In the 'GOOD' cases, the encryption mode is CBC with a randomly generated initialization vector, avoiding ECB's deterministic encryption of individual blocks. Authentication of the ciphertext is not shown.

    diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql index edee13682396..8a28cc78fad2 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql @@ -1,6 +1,6 @@ /** * @name Encryption using ECB - * @description Using the ECB encryption mode makes code vulnerable to replay attacks. + * @description Using the ECB encryption mode can reveal patterns in encrypted data. * @kind path-problem * @problem.severity warning * @security-severity 7.5 From 4f37e12f99b8efbbe62b284ddd757f5d16b3aa77 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:28:05 +0000 Subject: [PATCH 05/12] Unified: Clean up test annotations. --- .../security/CWE-327/ECBEncryption/test.swift | 20 ++++++++-------- .../ECBEncryption/test_commoncrypto.swift | 24 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift index 2eb39595b938..4e47ae924c66 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift @@ -47,18 +47,18 @@ func getCBCBlockMode() -> BlockMode { func test1() { let key: Array = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f] - let ecb = ECB() // $ Source + let ecb = ECB() // $ Source[unified/swift/ecb-encryption] let iv = getRandomArray() let cbc = CBC(iv: iv) let padding = Padding.noPadding // AES test cases - let ab1 = AES(key: key, blockMode: ecb, padding: padding) // $ Alert - let ab2 = AES(key: key, blockMode: ecb) // $ Alert - let ab3 = AES(key: key, blockMode: ECB(), padding: padding) // $ Alert - let ab4 = AES(key: key, blockMode: ECB()) // $ Alert - let ab5 = AES(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert - let ab6 = AES(key: key, blockMode: getECBBlockMode()) // $ Alert + let ab1 = AES(key: key, blockMode: ecb, padding: padding) // $ Alert[unified/swift/ecb-encryption] + let ab2 = AES(key: key, blockMode: ecb) // $ Alert[unified/swift/ecb-encryption] + let ab3 = AES(key: key, blockMode: ECB(), padding: padding) // $ Alert[unified/swift/ecb-encryption] + let ab4 = AES(key: key, blockMode: ECB()) // $ Alert[unified/swift/ecb-encryption] + let ab5 = AES(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert[unified/swift/ecb-encryption] + let ab6 = AES(key: key, blockMode: getECBBlockMode()) // $ Alert[unified/swift/ecb-encryption] let ag1 = AES(key: key, blockMode: cbc, padding: padding) // GOOD let ag2 = AES(key: key, blockMode: cbc) // GOOD @@ -68,9 +68,9 @@ func test1() { let ag6 = AES(key: key, blockMode: getCBCBlockMode()) // GOOD // Blowfish test cases - let bb1 = Blowfish(key: key, blockMode: ecb, padding: padding) // $ Alert - let bb2 = Blowfish(key: key, blockMode: ECB(), padding: padding) // $ Alert - let bb3 = Blowfish(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert + let bb1 = Blowfish(key: key, blockMode: ecb, padding: padding) // $ Alert[unified/swift/ecb-encryption] + let bb2 = Blowfish(key: key, blockMode: ECB(), padding: padding) // $ Alert[unified/swift/ecb-encryption] + let bb3 = Blowfish(key: key, blockMode: getECBBlockMode(), padding: padding) // $ Alert[unified/swift/ecb-encryption] let bg1 = Blowfish(key: key, blockMode: cbc, padding: padding) // GOOD let bg2 = Blowfish(key: key, blockMode: CBC(iv: iv), padding: padding) // GOOD diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift index 4555d6b02828..603d4018f5b8 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift @@ -134,7 +134,7 @@ func test_commoncrypto1(key: Data, iv: Data, dataIn: Data, dataOut: inout Data) ivPtr in let result1 = CCCryptorCreate( CCOperation(kCCEncrypt), - CCAlgorithm(kCCAlgorithm3DES), // BAD [NOT DETECTED] + CCAlgorithm(kCCAlgorithm3DES), // weak-cryptographic-algorithm CCOptions(0), keyPtr.baseAddress!, keyPtr.count, @@ -189,25 +189,25 @@ func test_commoncrypto2( // algorithms _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmDES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmDES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // weak-cryptographic-algorithm + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // weak-cryptographic-algorithm _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmCAST), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC4), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC2), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // BAD [NOT DETECTED] + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC4), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // weak-cryptographic-algorithm + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmRC2), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // weak-cryptographic-algorithm _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmBlowfish), 0, key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) - _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, &myCryptor) // BAD [NOT DETECTED] - _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] - _ = CCCryptorCreateFromData(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // BAD [NOT DETECTED] + _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, &myCryptor) // weak-cryptographic-algorithm + _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // weak-cryptographic-algorithm + _ = CCCryptorCreateFromData(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithm3DES), 0, key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // weak-cryptographic-algorithm // block modes (the default is CBC) _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(0), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) - _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // $ MISSING: Alert - _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, &myCryptor) // $ MISSING: Alert - _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // $ MISSING: Alert + _ = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, dataOut, dataOutAvail, nil) // $ MISSING: Alert[unified/swift/ecb-encryption] + _ = CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, &myCryptor) // $ MISSING: Alert[unified/swift/ecb-encryption] + _ = CCCryptorCreateFromData(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithm3DES), CCOptions(kCCOptionECBMode), key, keyLen, iv, dataIn, dataInLen, &myCryptor, &dataOutWritten) // $ MISSING: Alert[unified/swift/ecb-encryption] // modes - _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeECB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) // $ MISSING: Alert + _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeECB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) // $ MISSING: Alert[unified/swift/ecb-encryption] _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCBC), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCFB), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) _ = CCCryptorCreateWithMode(CCOperation(kCCAlgorithmAES), CCMode(kCCModeCTR), CCAlgorithm(kCCAlgorithm3DES), CCPadding(0), iv, key, keyLen, nil, 0, 0, CCModeOptions(0), &myCryptor) From ca8b7bf993a0059e1168f69a0f99831728a54cec Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:54:30 +0000 Subject: [PATCH 06/12] Unified: Uncomment a test case that apparently caused an extractor issue with original Swift. It seems fine with unified Swift. --- .../security/CWE-327/ECBEncryption/test_commoncrypto.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift index 603d4018f5b8..c99cb8d3cdda 100644 --- a/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift +++ b/unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test_commoncrypto.swift @@ -127,7 +127,7 @@ func test_commoncrypto1(key: Data, iv: Data, dataIn: Data, dataOut: inout Data) var myCryptor: CCCryptorRef? var dataOutWritten = 0 - /*key.withUnsafeBytes({ + key.withUnsafeBytes({ keyPtr in iv.withUnsafeBytes({ // create the cryptor object @@ -175,7 +175,7 @@ func test_commoncrypto1(key: Data, iv: Data, dataIn: Data, dataOut: inout Data) }) }) }) - })*/ + }) } func test_commoncrypto2( From d4516ec8d987f34a5f58444700cb54e583312d77 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:36:47 +0000 Subject: [PATCH 07/12] Unified: Move the cryptography models into a new model file, convert to newer .json format. --- unified/ql/lib/ext/crypto.model.json | 26 +++++++++++++++++++++ unified/ql/lib/ext/ecb-encryption.model.yml | 6 ----- unified/ql/lib/ext/legacy-swift.model.json | 3 --- 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 unified/ql/lib/ext/crypto.model.json delete mode 100644 unified/ql/lib/ext/ecb-encryption.model.yml diff --git a/unified/ql/lib/ext/crypto.model.json b/unified/ql/lib/ext/crypto.model.json new file mode 100644 index 000000000000..b2078d6d9b16 --- /dev/null +++ b/unified/ql/lib/ext/crypto.model.json @@ -0,0 +1,26 @@ +{ + "extensions": [ + { + "addsTo": { + "pack": "codeql/unified-all", + "extensible": "legacySourceModel" + }, + "data": [ + // namespace, type, subtypes, name, signature, ext, output, kind, provenance + ["", "ECB", true, "init()", "", "", "ReturnValue", "ecb-block-mode", "manual"] + ] + }, + { + "addsTo": { + "pack": "codeql/unified-all", + "extensible": "legacySinkModel" + }, + "data": [ + // namespace, type, subtypes, name, signature, ext, input, kind, provenance + ["", "AES", true, "init(key:blockMode:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], + ["", "AES", true, "init(key:blockMode:padding:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], + ["", "Blowfish", true, "init(key:blockMode:padding:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"] + ] + } + ] +} diff --git a/unified/ql/lib/ext/ecb-encryption.model.yml b/unified/ql/lib/ext/ecb-encryption.model.yml deleted file mode 100644 index 66e10e09fda9..000000000000 --- a/unified/ql/lib/ext/ecb-encryption.model.yml +++ /dev/null @@ -1,6 +0,0 @@ -extensions: - - addsTo: - pack: codeql/unified-all - extensible: legacySourceModel - data: # namespace, type, subtypes, name, signature, ext, output, kind, provenance - - ["", "ECB", true, "init()", "", "", "ReturnValue", "ecb-block-mode", "manual"] diff --git a/unified/ql/lib/ext/legacy-swift.model.json b/unified/ql/lib/ext/legacy-swift.model.json index 7719aa749f20..139a92f4ebb2 100644 --- a/unified/ql/lib/ext/legacy-swift.model.json +++ b/unified/ql/lib/ext/legacy-swift.model.json @@ -176,10 +176,7 @@ ["", "NSMutableString", true, "character(at:)", "", "", "Argument[at:]", "nsstring-length", "manual"], ["", "NSMutableString", true, "substring(from:)", "", "", "Argument[from:]", "nsstring-length", "manual"], ["", "NSMutableString", true, "substring(to:)", "", "", "Argument[to:]", "nsstring-length", "manual"], - ["", "AES", true, "init(key:blockMode:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], - ["", "AES", true, "init(key:blockMode:padding:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], ["", "NSRegularExpression", true, "init(pattern:options:)", "", "", "Argument[pattern:]", "regex-use", "manual"], - ["", "Blowfish", true, "init(key:blockMode:padding:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], ["", "URLSessionConfiguration", false, "tlsMaximumSupportedProtocol", "", "", "PostUpdate", "tls-protocol-version", "manual"], ["", "URLSessionConfiguration", false, "tlsMinimumSupportedProtocol", "", "", "PostUpdate", "tls-protocol-version", "manual"], ["", "URLSessionConfiguration", false, "tlsMaximumSupportedProtocolVersion", "", "", "PostUpdate", "tls-protocol-version", "manual"], From 4b3b35f34b9ac2e7b11e0b2d9d6517994cdb2450 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:51:16 +0000 Subject: [PATCH 08/12] Unified: Update ModelValidation.qll. --- shared/mad/codeql/mad/ModelValidation.qll | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shared/mad/codeql/mad/ModelValidation.qll b/shared/mad/codeql/mad/ModelValidation.qll index a5e64c434992..073701a18355 100644 --- a/shared/mad/codeql/mad/ModelValidation.qll +++ b/shared/mad/codeql/mad/ModelValidation.qll @@ -40,9 +40,10 @@ module KindValidation { "xslt-injection", // JavaScript-only currently, but may be shared in the future "cors-origin", "mongodb.sink", "system-prompt-injection", "user-prompt-injection", - // Swift-only currently, but may be shared in the future + // Swift / Unified Swift only currently, but may be shared in the future "database-store", "format-string", "hash-iteration-count", "predicate-injection", "preferences-store", "tls-protocol-version", "transmission", "webview-fetch", "xxe", + "encryption-block-mode", // Go-only currently, but may be shared in the future "jwt", // CPP-only currently @@ -134,7 +135,9 @@ module KindValidation { // JavaScript "database-access-result", "response", "request", "browser", "browser-url-query", "browser-url-fragment", "browser-url-path", "browser-url", "browser-window-name", - "browser-message-event" + "browser-message-event", + // Unified Swift + "ecb-block-mode" ] or this.matches([ From 389ab268d61460ea1b1cd255213d234c3fd74b6b Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:52:39 +0000 Subject: [PATCH 09/12] Unified: Remove the TODO comment, I'll put it in the improvements issue instead. --- unified/ql/src/queries/security/CWE-327/ECBEncryption.ql | 1 - 1 file changed, 1 deletion(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql index 8a28cc78fad2..cf8da678412f 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.ql @@ -20,7 +20,6 @@ module EcbEncryptionConfig implements DataFlow::ConfigSig { predicate isSource(DataFlow::Node node) { Models::isSource(node, "ecb-block-mode") } predicate isSink(DataFlow::Node node) { Models::isSink(node, "encryption-block-mode") } - // TODO: Model CommonCrypto ECB options and modes, also unsupported by original Swift. } module EcbEncryptionFlow = DataFlow::Global; From 9a41d8bd24401441bb676acc18a2c68e49f4ad0c Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:17:09 +0000 Subject: [PATCH 10/12] unified: Avoid CBC IV reuse in the ECB encryption example Generate a separate random IV for each AES cipher construction rather than sharing a CBC block mode between the two examples. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- unified/ql/src/queries/security/CWE-327/ECBEncryption.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift index 429d185698fb..38d7f7dcf11c 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift @@ -9,10 +9,9 @@ func encrypt(key : Key, padding : Padding) { _ = try Blowfish(key: key, blockMode: blockMode, padding: padding) // GOOD: ECB is not used for block mode - let aesBlockMode = CBC(iv: AES.randomIV(AES.blockSize)) let blowfishBlockMode = CBC(iv: Blowfish.randomIV(Blowfish.blockSize)) - _ = try AES(key: key, blockMode: aesBlockMode, padding: padding) - _ = try AES(key: key, blockMode: aesBlockMode) + _ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)), padding: padding) + _ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize))) _ = try Blowfish(key: key, blockMode: blowfishBlockMode, padding: padding) // ... From 5efe61af9c4c31313b210e1f8e5405f55c0338d3 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:42:13 +0000 Subject: [PATCH 11/12] Unified: Clean up the example. --- .../src/queries/security/CWE-327/ECBEncryption.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift index 38d7f7dcf11c..12c7fdf0ac5a 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.swift @@ -3,16 +3,14 @@ func encrypt(key : Key, padding : Padding) { // ... // BAD: ECB is used for block mode - let blockMode = ECB() - _ = try AES(key: key, blockMode: blockMode, padding: padding) - _ = try AES(key: key, blockMode: blockMode) - _ = try Blowfish(key: key, blockMode: blockMode, padding: padding) + _ = try AES(key: key, blockMode: ECB(), padding: padding) + _ = try AES(key: key, blockMode: ECB()) + _ = try Blowfish(key: key, blockMode: ECB(), padding: padding) // GOOD: ECB is not used for block mode - let blowfishBlockMode = CBC(iv: Blowfish.randomIV(Blowfish.blockSize)) _ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)), padding: padding) _ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize))) - _ = try Blowfish(key: key, blockMode: blowfishBlockMode, padding: padding) + _ = try Blowfish(key: key, blockMode: CBC(iv: Blowfish.randomIV(Blowfish.blockSize), padding: padding) // ... } From 57b298531b50f240a10d7dabb05ad443daca082d Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:07:39 +0000 Subject: [PATCH 12/12] Unified: Simplify the .qhelp a little and clarify the impact of ECB. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp index 7572c614b3ad..576ca422bff2 100644 --- a/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp +++ b/unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp @@ -3,15 +3,15 @@ "qhelp.dtd"> -

    ECB should not be used as a mode for encryption. With a fixed key, identical plaintext blocks always produce identical ciphertext blocks. This reveals patterns in the plaintext, even when the ciphertext cannot be decrypted.

    +

    ECB should not be used as a mode for encryption. With a fixed key, identical plaintext blocks always produce identical ciphertext blocks. This can expose sensitive information by revealing patterns in the plaintext.

    -

    Use an authenticated encryption mode such as GCM where available. If CBC is used, generate a fresh, unpredictable initialization vector for each encryption and use a separate message authentication mechanism to protect ciphertext integrity. Changing the cipher mode alone does not prevent replay attacks.

    +

    Use an authenticated encryption mode such as GCM where available. If CBC is used, generate a fresh, unpredictable initialization vector for each encryption and use a separate message authentication mechanism to protect ciphertext integrity.

    -

    The following example shows six cases of instantiating a cipher with different block modes. In the 'BAD' cases, the mode of encryption is ECB, which reveals patterns in the plaintext. In the 'GOOD' cases, the encryption mode is CBC with a randomly generated initialization vector, avoiding ECB's deterministic encryption of individual blocks. Authentication of the ciphertext is not shown.

    +

    The following example shows six cases of instantiating a cipher with different block modes. In the 'BAD' cases, the encryption mode is ECB, which reveals patterns in the plaintext. In the 'GOOD' cases, the encryption mode is CBC with a randomly generated initialization vector, avoiding this issue. Authentication of the ciphertext is not shown.