Repository navigation
Unified: Port ECBEncryption query #23234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geoffw0
wants to merge
17
commits into
github:main
Choose a base branch
from
geoffw0:unifiedecb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7b744a1
unified: Copy Swift ECB encryption query and tests unchanged
geoffw0 2315404
unified: Collect original Swift ECB configuration and models
geoffw0 dea88e1
unified: Port ECB encryption flow to models-as-data
geoffw0 f0b4bda
unified: Correct ECB encryption security guidance
geoffw0 4f37e12
Unified: Clean up test annotations.
geoffw0 ca8b7bf
Unified: Uncomment a test case that apparently caused an extractor is…
geoffw0 d4516ec
Unified: Move the cryptography models into a new model file, convert …
geoffw0 4b3b35f
Unified: Update ModelValidation.qll.
geoffw0 389ab26
Unified: Remove the TODO comment, I'll put it in the improvements iss…
geoffw0 9a41d8b
unified: Avoid CBC IV reuse in the ECB encryption example
geoffw0 5efe61a
Unified: Clean up the example.
geoffw0 57b2985
Unified: Simplify the .qhelp a little and clarify the impact of ECB.
geoffw0 bfa65fb
Merge branch 'main' into unifiedecb
geoffw0 dddf31e
Unified: Remove duplicate ECB sink models
geoffw0 d6acf69
Unified: Mark defaulted crypto parameters optional
geoffw0 22f94e1
Unified: Fix example code and copy it into test.
geoffw0 33a7642
Unified: Fix the example still not compiling.
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "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:padding?:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"], | ||
| ["", "Blowfish", true, "init(key:blockMode?:padding:)", "", "", "Argument[blockMode:]", "encryption-block-mode", "manual"] | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
16
unified/ql/src/queries/security/CWE-327/ECBEncryption.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: randomIV(AES.blockSize)), padding: padding) | ||
| _ = try AES(key: key, blockMode: CBC(iv: randomIV(AES.blockSize))) | ||
| _ = try Blowfish(key: key, blockMode: CBC(iv: randomIV(Blowfish.blockSize)), padding: padding) | ||
|
|
||
| // ... | ||
| } |
38 changes: 38 additions & 0 deletions
38
unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #select | ||
| | test.swift:60:37:60:39 | ecb | test.swift:54:12:54:16 | ECB(...) | test.swift:60:37:60:39 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:54:12:54:16 | ECB(...) | ECB(...) | | ||
| | test.swift:61:37:61:39 | ecb | test.swift:54:12:54:16 | ECB(...) | test.swift:61:37:61:39 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:54:12:54:16 | ECB(...) | ECB(...) | | ||
| | test.swift:62:37:62:41 | ECB(...) | test.swift:62:37:62:41 | ECB(...) | test.swift:62:37:62:41 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:62:37:62:41 | ECB(...) | ECB(...) | | ||
| | test.swift:63:37:63:41 | ECB(...) | test.swift:63:37:63:41 | ECB(...) | test.swift:63:37:63:41 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:63:37:63:41 | ECB(...) | ECB(...) | | ||
| | test.swift:64:37:64:53 | getECBBlockMode(...) | test.swift:43:9:43:13 | ECB(...) | test.swift:64:37:64:53 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:43:9:43:13 | ECB(...) | ECB(...) | | ||
| | test.swift:65:37:65:53 | getECBBlockMode(...) | test.swift:43:9:43:13 | ECB(...) | test.swift:65:37:65:53 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:43:9:43:13 | ECB(...) | ECB(...) | | ||
| | test.swift:75:42:75:44 | ecb | test.swift:54:12:54:16 | ECB(...) | test.swift:75:42:75:44 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:54:12:54:16 | ECB(...) | ECB(...) | | ||
| | test.swift:76:42:76:46 | ECB(...) | test.swift:76:42:76:46 | ECB(...) | test.swift:76:42:76:46 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:76:42:76:46 | ECB(...) | ECB(...) | | ||
| | test.swift:77:42:77:58 | getECBBlockMode(...) | test.swift:43:9:43:13 | ECB(...) | test.swift:77:42:77:58 | getECBBlockMode(...) | The initialization of the cipher 'getECBBlockMode(...)' uses the insecure ECB block mode from $@. | test.swift:43:9:43:13 | ECB(...) | ECB(...) | | ||
| | test.swift:94:35:94:39 | ECB(...) | test.swift:94:35:94:39 | ECB(...) | test.swift:94:35:94:39 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:94:35:94:39 | ECB(...) | ECB(...) | | ||
| | test.swift:95:35:95:39 | ECB(...) | test.swift:95:35:95:39 | ECB(...) | test.swift:95:35:95:39 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:95:35:95:39 | ECB(...) | ECB(...) | | ||
| | test.swift:96:40:96:44 | ECB(...) | test.swift:96:40:96:44 | ECB(...) | test.swift:96:40:96:44 | ECB(...) | The initialization of the cipher 'ECB(...)' uses the insecure ECB block mode from $@. | test.swift:96:40:96:44 | ECB(...) | ECB(...) | | ||
| edges | ||
| | test.swift:43:9:43:13 | ECB(...) | test.swift:64:37:64:53 | getECBBlockMode(...) | provenance | | | ||
| | test.swift:43:9:43:13 | ECB(...) | test.swift:65:37:65:53 | getECBBlockMode(...) | provenance | | | ||
| | test.swift:43:9:43:13 | ECB(...) | test.swift:77:42:77:58 | getECBBlockMode(...) | provenance | | | ||
| | test.swift:54:6:54:8 | ecb | test.swift:60:37:60:39 | ecb | provenance | | | ||
| | test.swift:54:6:54:8 | ecb | test.swift:61:37:61:39 | ecb | provenance | | | ||
| | test.swift:54:6:54:8 | ecb | test.swift:75:42:75:44 | ecb | provenance | | | ||
| | test.swift:54:12:54:16 | ECB(...) | test.swift:54:6:54:8 | ecb | provenance | | | ||
| nodes | ||
| | test.swift:43:9:43:13 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:54:6:54:8 | ecb | semmle.label | ecb | | ||
| | test.swift:54:12:54:16 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:60:37:60:39 | ecb | semmle.label | ecb | | ||
| | test.swift:61:37:61:39 | ecb | semmle.label | ecb | | ||
| | test.swift:62:37:62:41 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:63:37:63:41 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:64:37:64:53 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | | ||
| | test.swift:65:37:65:53 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | | ||
| | test.swift:75:42:75:44 | ecb | semmle.label | ecb | | ||
| | test.swift:76:42:76:46 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:77:42:77:58 | getECBBlockMode(...) | semmle.label | getECBBlockMode(...) | | ||
| | test.swift:94:35:94:39 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:95:35:95:39 | ECB(...) | semmle.label | ECB(...) | | ||
| | test.swift:96:40:96:44 | ECB(...) | semmle.label | ECB(...) | | ||
| subpaths |
3 changes: 3 additions & 0 deletions
3
unified/ql/test/query-tests/security/CWE-327/ECBEncryption/ECBEncryption.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
104 changes: 104 additions & 0 deletions
104
unified/ql/test/query-tests/security/CWE-327/ECBEncryption/test.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
|
|
||
| // --- stubs --- | ||
|
|
||
| // These stubs roughly follows the same structure as classes from CryptoSwift | ||
| class AES | ||
| { | ||
| static let blockSize = 16 | ||
|
|
||
| init(key: Array<UInt8>, blockMode: BlockMode, padding: Padding) { } | ||
| init(key: Array<UInt8>, blockMode: BlockMode) { } | ||
| } | ||
|
|
||
| class Blowfish | ||
| { | ||
| static let blockSize = 8 | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| typealias Key = Array<UInt8> | ||
|
|
||
| func randomIV(_ blockSize: Int) -> Array<UInt8> { | ||
| return (0..<blockSize).map({ _ in UInt8.random(in: 0...UInt8.max) }) | ||
| } | ||
|
|
||
| func doc_example_encrypt(key : Key, padding : Padding) { | ||
| // ... | ||
|
|
||
| // BAD: ECB is used for block mode | ||
| _ = try AES(key: key, blockMode: ECB(), padding: padding) // $ Alert[unified/swift/ecb-encryption] | ||
| _ = try AES(key: key, blockMode: ECB()) // $ Alert[unified/swift/ecb-encryption] | ||
| _ = try Blowfish(key: key, blockMode: ECB(), padding: padding) // $ Alert[unified/swift/ecb-encryption] | ||
|
|
||
| // GOOD: ECB is not used for block mode | ||
| _ = try AES(key: key, blockMode: CBC(iv: randomIV(AES.blockSize)), padding: padding) | ||
| _ = try AES(key: key, blockMode: CBC(iv: randomIV(AES.blockSize))) | ||
| _ = try Blowfish(key: key, blockMode: CBC(iv: randomIV(Blowfish.blockSize)), padding: padding) | ||
|
|
||
| // ... | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.