Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions shared/mad/codeql/mad/ModelValidation.qll
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ module KindValidation<KindValidationConfigSig Config> {
"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
Expand Down Expand Up @@ -134,7 +135,9 @@ module KindValidation<KindValidationConfigSig Config> {
// 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([
Expand Down
26 changes: 26 additions & 0 deletions unified/ql/lib/ext/crypto.model.json
Original file line number Diff line number Diff line change
@@ -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"]
]
}
]
}
3 changes: 0 additions & 3 deletions unified/ql/lib/ext/legacy-swift.model.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
21 changes: 21 additions & 0 deletions unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>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.</p>
</overview>

<recommendation>
<p>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.</p>
</recommendation>

<example>
<p>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.</p>
<sample src="ECBEncryption.swift" />
</example>

<references>
<li>Wikipedia, block cipher modes of operation, <a href="https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_.28ECB.29">Electronic codebook (ECB)</a>.</li>
</references>
</qhelp>
34 changes: 34 additions & 0 deletions unified/ql/src/queries/security/CWE-327/ECBEncryption.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @name Encryption using ECB
* @description Using the ECB encryption mode can reveal patterns in encrypted data.
* @kind path-problem
* @problem.severity warning
* @security-severity 7.5
* @precision high
* @id unified/swift/ecb-encryption
* @tags security
* external/cwe/cwe-327
*/

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) { Models::isSource(node, "ecb-block-mode") }

predicate isSink(DataFlow::Node node) { Models::isSink(node, "encryption-block-mode") }
}

module EcbEncryptionFlow = DataFlow::Global<EcbEncryptionConfig>;

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.getNode(),
sourceNode.getNode().toString()
16 changes: 16 additions & 0 deletions unified/ql/src/queries/security/CWE-327/ECBEncryption.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

func encrypt(key : Key, padding : Padding) {
// ...

// BAD: ECB is used for block mode
_ = 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
_ = 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: CBC(iv: Blowfish.randomIV(Blowfish.blockSize), padding: padding)

// ...
}
Original file line number Diff line number Diff line change
@@ -0,0 +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 | 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 | 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query: queries/security/CWE-327/ECBEncryption.ql
postprocess:
- utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

// --- stubs ---

// These stubs roughly follows the same structure as classes from CryptoSwift
class AES
{
init(key: Array<UInt8>, blockMode: BlockMode, padding: Padding) { }
init(key: Array<UInt8>, blockMode: BlockMode) { }
}

class Blowfish
{
init(key: Array<UInt8>, blockMode: BlockMode, padding: Padding) { }
}

protocol BlockMode { }

struct ECB: BlockMode {
init() { }
}

struct CBC: BlockMode {
init(iv: Array<UInt8>) { }
}

protocol PaddingProtocol { }

enum Padding: PaddingProtocol {
case noPadding, zeroPadding, pkcs7, pkcs5, eme_pkcs1v15, emsa_pkcs1v15, iso78164, iso10126
}

// Create some inter-procedural dependencies

func getRandomArray() -> Array<UInt8> {
(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<UInt8> = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f]
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[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
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[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
let bg3 = Blowfish(key: key, blockMode: getCBCBlockMode(), padding: padding) // GOOD
}
Loading
Loading