-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Add heuristic sinks for passwords, initialization vectors etc #20939
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
+156
−9
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e09948
Rust: Add tests for heuristics.
geoffw0 bb50e9f
Rust: Add heuristic sinks for rust/hard-coded-cryptographic-value.
geoffw0 faf69b8
Rust: Add sinks as barriers to prevent duplicate results.
geoffw0 e834e86
Rust: Remove one of the cases that is causing FP results in MRVA.
geoffw0 4504038
Rust: Add test cases for a small number of FPs we see.
geoffw0 c64f19f
Rust: Change note.
geoffw0 3028e5d
Rust: CallExpr -> Call.
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
4 changes: 4 additions & 0 deletions
4
rust/ql/src/change-notes/2025-12-01-hard-coded-cryptographic-value.md
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,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * The `rust/hard-coded-cryptographic-value` query has been extended with new heuristic sinks identifying passwords, initialization vectors, nonces and salts. |
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
71 changes: 71 additions & 0 deletions
71
rust/ql/test/query-tests/security/CWE-798/test_heuristic.rs
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,71 @@ | ||
|
|
||
| // --- tests --- | ||
|
|
||
| fn encrypt_with(plaintext: &str, key: &[u8;16], iv: &[u8;16]) { | ||
| // ... | ||
| } | ||
|
|
||
| fn encrypt2(plaintext: &str, crypto_key: &[u8;16], iv_bytes: &[u8;16]) { | ||
| // ... | ||
| } | ||
|
|
||
| fn database_op(text: &str, primary_key: &str, pivot: &str) { | ||
| // note: this one has nothing to do with encryption, but has | ||
| // `key` and `iv` contained within the parameter names. | ||
| } | ||
|
|
||
| struct MyCryptor { | ||
| } | ||
|
|
||
| impl MyCryptor { | ||
| fn new(password: &str) -> MyCryptor { | ||
| MyCryptor { } | ||
| } | ||
|
|
||
| fn set_nonce(&self, nonce: &[u8;16]) { | ||
| // ... | ||
| } | ||
|
|
||
| fn encrypt(&self, plaintext: &str, salt: &[u8;16]) { | ||
| // ... | ||
| } | ||
|
|
||
| fn set_salt_u64(&self, salt: u64) { | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| fn test(var_string: &str, var_data: &[u8;16], var_u64: u64) { | ||
| encrypt_with("plaintext", var_data, var_data); | ||
|
|
||
| let const_key: &[u8;16] = &[0u8;16]; // $ MISSING: Alert[rust/hard-coded-cryptographic-value] | ||
| encrypt_with("plaintext", const_key, var_data); // $ MISSING: Sink | ||
|
|
||
| let const_iv: &[u8;16] = &[0u8;16]; // $ Alert[rust/hard-coded-cryptographic-value] | ||
| encrypt_with("plaintext", var_data, const_iv); // $ Sink | ||
|
|
||
| encrypt2("plaintext", var_data, var_data); | ||
|
|
||
| let const_key2: &[u8;16] = &[1u8;16]; // $ MISSING: Alert[rust/hard-coded-cryptographic-value] | ||
| encrypt2("plaintext", const_key2, var_data); // $ MISSING: Sink | ||
|
|
||
| let const_iv: &[u8;16] = &[1u8;16]; // $ MISSING: Alert[rust/hard-coded-cryptographic-value] | ||
| encrypt2("plaintext", var_data, const_iv); // $ MISSING: Sink | ||
|
|
||
| let const_key_str = "primary_key"; | ||
| let const_pivot_str = "pivot"; | ||
| database_op("text", const_key_str, const_pivot_str); | ||
|
|
||
| let mc1 = MyCryptor::new(var_string); | ||
| mc1.set_nonce(var_data); | ||
| mc1.encrypt("plaintext", var_data); | ||
|
|
||
| let mc2 = MyCryptor::new("secret"); // $ Alert[rust/hard-coded-cryptographic-value] | ||
| mc2.set_nonce(&[0u8;16]); // $ Alert[rust/hard-coded-cryptographic-value] | ||
| mc2.encrypt("plaintext", &[0u8;16]); // $ Alert[rust/hard-coded-cryptographic-value] | ||
|
|
||
| mc2.set_salt_u64(0); // $ Alert[rust/hard-coded-cryptographic-value] | ||
| mc2.set_salt_u64(var_u64); | ||
| mc2.set_salt_u64(var_u64 + 1); // $ SPURIOUS: Alert[rust/hard-coded-cryptographic-value] | ||
| mc2.set_salt_u64((var_u64 << 32) ^ (var_u64 & 0xFFFFFFFF)); // $ SPURIOUS: Alert[rust/hard-coded-cryptographic-value] | ||
| } |
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.