Repository navigation
Unified: New query for hard-coded cryptographic values #23301
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3fbb8cf
unified: Copy constant salt query and tests unchanged
geoffw0 3240754
unified: Collect original constant salt configuration and models
geoffw0 cd4c928
unified: Port constant salt query to unified Swift
geoffw0 dd8a4dd
unified: Detect integer-literal byte-array constant salts
geoffw0 aaf8131
unified: Fix redeclaration and zero key lengths in ConstantSalt sample
geoffw0 4f15eee
unified: Restructure ConstantSalt sample as a 6-case switch
geoffw0 c4070af
unified: Add ConstantSalt.swift query-help sample to test.swift
geoffw0 0afabad
unified: Rename constant-salt.model.json to crypto.model.json
geoffw0 f119be9
unified: Simplify inline expectation comments in ConstantSalt tests
geoffw0 3da7eb7
unified: Rename ConstantSalt files to CWE-798/HardcodedCryptographicV…
geoffw0 0ddf6d2
unified: Generalize ConstantSalt query to HardcodedCryptographicValue
geoffw0 2a3547e
unified: Generalize HardcodedCryptographicValue query help
geoffw0 76d0d7c
Unified: Move the .qhelp closer to what we have for Rust.
geoffw0 bc574fc
Unified: Limit the qhelp / metadata to salts and IVs, as that's all w…
geoffw0 1636e7b
Unified: Add support for IVs in the query.
geoffw0 5a95649
unified: Port constant IV test cases to HardcodedCryptographicValue
geoffw0 bc47b6b
Unified: Minor edits to the tests.
geoffw0 b7c9d27
unified: Add CryptoSwift and RNCryptor IV sink models
geoffw0 e9d99b4
Unified: Remove TODO comment (tracked elsewhere).
geoffw0 a77b9b7
Unified: Support nonces (which are a rarer variant not quite the same…
geoffw0 4ddf92a
Unified: Switch sink models from encryption- to credentials- pattern …
geoffw0 4371898
Unified: Autoformat.
geoffw0 3631fc6
Unified: Accept Copilot suggestion.
geoffw0 4652e39
Unified: Recommended metadata changes.
geoffw0 86a21f2
Merge branch 'main' into unifiedconstant
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
Some comments aren't visible on the classic Files Changed page.
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
28 changes: 28 additions & 0 deletions
28
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.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,28 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p>Hard-coded salts and initialization vectors should not be used for cryptographic operations.</p> | ||
| <ul> | ||
| <li>Attackers can easily recover hard-coded values if they have access to the source code or compiled binary.</li> | ||
| <li>Some hard-coded values are easily guessable.</li> | ||
| <li>Use of hard-coded values may leave cryptographic operations vulnerable to dictionary attacks, rainbow tables, and other forms of cryptanalysis.</li> | ||
| </ul> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p>Use cryptographically secure random number generation to produce fresh salts, initialization vectors, and key material, and never hard-code passwords or keys in source code.</p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p>The following example shows several key-derivation calls using salts. In the 'BAD' cases, the salt is hard-coded, making the derived outputs vulnerable to precomputation and dictionary attacks. In the 'GOOD' cases, the salt is randomly generated, protecting against precomputation.</p> | ||
| <sample src="HardcodedCryptographicValue.swift" /> | ||
| </example> | ||
|
|
||
| <references> | ||
| <li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Key_Management_Cheat_Sheet.html">Key Management Cheat Sheet</a>.</li> | ||
| <li><a href="https://www.okta.com/blog/identity-security/what-are-salted-passwords-and-password-hashing/">What are Salted Passwords and Password Hashing?</a></li> | ||
| <li>O'Reilly: <a href="https://www.oreilly.com/library/view/secure-programming-cookbook/0596003943/ch04s09.html">Using Salts, Nonces, and Initialization Vectors</a>.</li> | ||
| </references> | ||
| </qhelp> |
152 changes: 152 additions & 0 deletions
152
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.ql
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,152 @@ | ||
| /** | ||
| * @name Hard-coded cryptographic value | ||
| * @description Using hard-coded salts or initialization | ||
| * vectors is not secure. | ||
| * @kind path-problem | ||
| * @problem.severity warning | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id unified/swift/hard-coded-cryptographic-value | ||
| * @tags security | ||
| * external/cwe/cwe-760 | ||
| * external/cwe/cwe-1204 | ||
| */ | ||
|
|
||
| import unified | ||
| private import codeql.unified.internal.typeinference.TypeInference as TypeInference | ||
|
|
||
| /** | ||
| * A kind of cryptographic value modeled as a sink for this query. | ||
| */ | ||
| class CryptographicValueKind extends string { | ||
| CryptographicValueKind() { this = ["salt", "iv", "nonce"] } | ||
|
|
||
| /** | ||
| * Gets the models-as-data sink kind string for this cryptographic value kind. | ||
| */ | ||
| string getSinkModelKind() { result = "credentials-" + this } | ||
|
|
||
| /** | ||
| * Gets a description of this value kind for alert messages. | ||
| */ | ||
| string getDescription() { | ||
| this = "salt" and result = "a salt" | ||
| or | ||
| this = "iv" and result = "an initialization vector" | ||
| or | ||
| this = "nonce" and result = "a nonce" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A hard-coded cryptographic value created through a byte-array, string, or number literal. | ||
| */ | ||
| class HardcodedCryptographicValueSource extends Expr { | ||
| HardcodedCryptographicValueSource() { | ||
| exists(ArrayLiteral arr | this = arr | | ||
| forex(Expr elem | elem = arr.getAnElement() | elem instanceof IntLiteral) | ||
| or | ||
| exists(TypeInference::Type arrayType, TypeInference::Type elemType | | ||
| arrayType = TypeInference::inferType(arr) and | ||
| arrayType.asClass().getName() = "Array" and | ||
| elemType = | ||
| TypeInference::inferType(arr, | ||
| TypeInference::TypePath::singleton(arrayType.getPositionalTypeParameter(0))) and | ||
| elemType.asClass().getName() = "UInt8" | ||
|
geoffw0 marked this conversation as resolved.
Resolved
|
||
| ) | ||
| ) | ||
| or | ||
| this instanceof StringLiteral | ||
| or | ||
| this instanceof IntLiteral | ||
| or | ||
| this instanceof FloatLiteral | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A data flow sink for hard-coded cryptographic value vulnerabilities. | ||
| */ | ||
| class HardcodedCryptographicValueSink extends DataFlow::Node { | ||
| HardcodedCryptographicValueSink() { | ||
| exists(CryptographicValueKind kind | Models::isSink(this, kind.getSinkModelKind())) | ||
| } | ||
|
|
||
| /** Gets a kind of cryptographic value represented by this sink. */ | ||
| CryptographicValueKind getAKind() { Models::isSink(this, result.getSinkModelKind()) } | ||
| } | ||
|
|
||
| /** | ||
| * A barrier for appending, since appending to a hard-coded string or array | ||
| * may (and often does) result in a non-constant value. | ||
| * | ||
| * Ideally, these would not be a barrier when there is flow to *all* | ||
| * inputs. | ||
| */ | ||
| private class AppendBarrier extends DataFlow::Node { | ||
| AppendBarrier() { | ||
| exists(BinaryExpr be | | ||
| be.getOperator().getValue() = ["+", "+="] and | ||
| this.asExpr() = be.getAnOperand() | ||
| ) | ||
| or | ||
| this.asExpr() = any(StringInterpolationExpr sie).getAnElement() | ||
| or | ||
| exists(CallExpr ce, string methodName | | ||
| methodName = ["append", "appending"] and | ||
|
geoffw0 marked this conversation as resolved.
Resolved
|
||
| ce.getNumberOfArguments() = 1 and | ||
| ce.getArgument(0).isPositional() and | ||
| ( | ||
| ce.getCallee().(IdentifierExpr).getValue() = methodName or | ||
| ce.getCallee().(MemberAccessExpr).getMemberName() = methodName | ||
| ) | ||
| | | ||
| this.asExpr() = ce.getArgument(0).getValue() | ||
| or | ||
| exists(Expr qual | qual = ce.getCallee().(MemberAccessExpr).getBase() | | ||
| this.asExpr() = qual or | ||
| this.isLocalVariableRead(qual, _) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for hard-coded cryptographic value vulnerabilities. | ||
| */ | ||
| module HardcodedCryptographicValueConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| node.asExpr() instanceof HardcodedCryptographicValueSource | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { node instanceof HardcodedCryptographicValueSink } | ||
|
|
||
| predicate isBarrier(DataFlow::Node node) { node instanceof AppendBarrier } | ||
|
|
||
| predicate isBarrierIn(DataFlow::Node node) { | ||
| // make sources barriers so that we only report the closest instance | ||
| isSource(node) | ||
| } | ||
|
|
||
| predicate isBarrierOut(DataFlow::Node node) { | ||
| // make sinks barriers so that we only report the closest instance | ||
| isSink(node) | ||
| } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module HardcodedCryptographicValueFlow = TaintTracking::Global<HardcodedCryptographicValueConfig>; | ||
|
|
||
| import HardcodedCryptographicValueFlow::PathGraph | ||
|
|
||
| from | ||
| HardcodedCryptographicValueFlow::PathNode source, HardcodedCryptographicValueFlow::PathNode sink, | ||
| DataFlow::Node sourceNode, CryptographicValueKind kind | ||
| where | ||
| HardcodedCryptographicValueFlow::flowPath(source, sink) and | ||
| sourceNode = source.getNode() and | ||
| kind = sink.getNode().(HardcodedCryptographicValueSink).getAKind() | ||
| select sink.getNode(), source, sink, | ||
| "The hard-coded value $@ is used as " + kind.getDescription() + ".", sourceNode, | ||
| sourceNode.toString() | ||
28 changes: 28 additions & 0 deletions
28
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.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,28 @@ | ||
|
|
||
| func encrypt(choice: Int) throws { | ||
| // ... | ||
|
|
||
| let badSalt: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05] | ||
| let goodSalt = (0..<16).map({ _ in UInt8.random(in: 0...UInt8.max) }) | ||
| let randomArray = (0..<10).map({ _ in UInt8.random(in: 0...UInt8.max) }) | ||
|
|
||
| switch choice { | ||
| // BAD: Using constant salts for hashing | ||
|
geoffw0 marked this conversation as resolved.
Resolved
|
||
| case 0: | ||
| _ = try HKDF(password: randomArray, salt: badSalt, info: randomArray, keyLength: 32, variant: Variant.sha2) | ||
| case 1: | ||
| _ = try PKCS5.PBKDF2(password: randomArray, salt: badSalt, iterations: 120120, keyLength: 32) | ||
| case 2: | ||
| _ = try Scrypt(password: randomArray, salt: badSalt, dkLen: 64, N: 16384, r: 8, p: 1) | ||
|
|
||
| // GOOD: Using randomly generated salts for hashing | ||
| case 3: | ||
| _ = try HKDF(password: randomArray, salt: goodSalt, info: randomArray, keyLength: 32, variant: Variant.sha2) | ||
| case 4: | ||
| _ = try PKCS5.PBKDF2(password: randomArray, salt: goodSalt, iterations: 120120, keyLength: 32) | ||
| default: | ||
| _ = try Scrypt(password: randomArray, salt: goodSalt, dkLen: 64, N: 16384, r: 8, p: 1) | ||
| } | ||
|
|
||
| // ... | ||
| } | ||
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.