Skip to content

Rust: new query rust/hardcoded-crytographic-value #18943

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 39 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9a35feb
Rust: Query framework and basic tests.
geoffw0 Mar 3, 2025
bd75f01
Rust: More test cases.
geoffw0 Mar 5, 2025
9fb00da
Rust: Implement the query (with one source, one sink model).
geoffw0 Mar 3, 2025
a6e106e
Rust: Model more sinks + flows.
geoffw0 Mar 4, 2025
aacbfc0
Rust: Improve alert messages.
geoffw0 Mar 5, 2025
055baf2
Rust: Improve results on arrays (less duplication).
geoffw0 Mar 6, 2025
ac94ac6
Rust: Model even more sinks + flows.
geoffw0 Mar 6, 2025
b4a6063
Rust: Add std::mem::zeroed as a source.
geoffw0 Mar 6, 2025
95be12e
Rust: Add qhelp and examples.
geoffw0 Mar 6, 2025
e564c41
Rust: Compute security-severity tag.
geoffw0 Mar 6, 2025
952e417
Rust: Tweak some wording.
geoffw0 Mar 6, 2025
9af2d02
Rust: Add the new sinks to stats.
geoffw0 Mar 6, 2025
42e7d1e
Rust: Fix typo.
geoffw0 Mar 6, 2025
b6c9be2
Merge branch 'main' into constcrypto
geoffw0 Mar 7, 2025
19416a9
Rust: Correct test results.
geoffw0 Mar 7, 2025
c63c1be
Rust: Accept integration test .expected changes.
geoffw0 Mar 7, 2025
3dc35f1
Rust: Accept more test changes.
geoffw0 Mar 7, 2025
fdb4362
Merge remote-tracking branch 'upstream/main' into constcrypto
geoffw0 Mar 7, 2025
b4e710f
Rust: Add missing models (for some platforms???).
geoffw0 Mar 7, 2025
e84a98b
Apply suggestions from code review
geoffw0 Mar 10, 2025
a34f9be
Rust: Add a test case for getrandom.
geoffw0 Mar 10, 2025
9e54d53
Rust: Add barrier.
geoffw0 Mar 10, 2025
1ca5c59
Rust: Replace imports of internal.DataFlowImpl where possible.
geoffw0 Mar 10, 2025
e3beacb
Rust: Print models (temporary, to see how this differs on CI).
geoffw0 Mar 10, 2025
a0f4fa2
Rust: hardcoded -> hard-coded.
geoffw0 Mar 11, 2025
704b385
Rust: Fix a mistake in the test.
geoffw0 Mar 17, 2025
81edb47
Merge branch 'main' into constcrypto
geoffw0 Mar 17, 2025
f5daec9
Rust: Fix after merge.
geoffw0 Mar 17, 2025
07011f7
Rust: Fix more after merge.
geoffw0 Mar 17, 2025
898c569
Rust: Change note.
geoffw0 Jun 24, 2025
c2ddf25
Merge branch 'main' into constcrypto
geoffw0 Jul 17, 2025
0ec10e5
Rust: Corrections after the merge.
geoffw0 Jul 17, 2025
fc8a662
Rust: Update the models.
geoffw0 Jul 17, 2025
796cb19
Rust: Accept test regressions with new format MaD.
geoffw0 Jul 21, 2025
ec3ad85
Rust: Add another test case for barriers (that still functions).
geoffw0 Jul 21, 2025
d53dada
Rust: Update barrier logic to use getCanonicalPath.
geoffw0 Jul 21, 2025
43ac82f
Rust: Update consistency check .expected files.
geoffw0 Jul 21, 2025
1945fb8
Rust: Accept changes to query suites.
geoffw0 Jul 21, 2025
f7d822b
Rust: Remove empty file.
geoffw0 Jul 22, 2025
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
Prev Previous commit
Next Next commit
Rust: hardcoded -> hard-coded.
  • Loading branch information
geoffw0 committed Mar 11, 2025
commit a0f4fa28b2a50a077c380646bb2828da85b3426d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Provides classes and predicates for reasoning about hardcoded cryptographic value
* Provides classes and predicates for reasoning about hard-coded cryptographic value
* vulnerabilities.
*/

Expand Down Expand Up @@ -32,17 +32,17 @@ class CryptographicValueKind extends string {
}

/**
* Provides default sources, sinks and barriers for detecting hardcoded cryptographic
* Provides default sources, sinks and barriers for detecting hard-coded cryptographic
* value vulnerabilities, as well as extension points for adding your own.
*/
module HardcodedCryptographicValue {
/**
* A data flow source for hardcoded cryptographic value vulnerabilities.
* A data flow source for hard-coded cryptographic value vulnerabilities.
*/
abstract class Source extends DataFlow::Node { }

/**
* A data flow sink for hardcoded cryptographic value vulnerabilities.
* A data flow sink for hard-coded cryptographic value vulnerabilities.
*/
abstract class Sink extends DataFlow::Node {
/**
Expand All @@ -52,7 +52,7 @@ module HardcodedCryptographicValue {
}

/**
* A barrier for hardcoded cryptographic value vulnerabilities.
* A barrier for hard-coded cryptographic value vulnerabilities.
*/
abstract class Barrier extends DataFlow::Node { }

Expand Down Expand Up @@ -81,7 +81,7 @@ module HardcodedCryptographicValue {
}

/**
* An externally modeled sink for hardcoded cryptographic value vulnerabilities.
* An externally modeled sink for hard-coded cryptographic value vulnerabilities.
*/
private class ModelsAsDataSinks extends Sink {
CryptographicValueKind kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@

<overview>
<p>
Hardcoded passwords, keys, initialization vectors, and salts should not be used for cryptographic operations.
Hard-coded passwords, keys, initialization vectors, and salts should not be used for cryptographic operations.
</p>
<ul>
<li>
Attackers can easily recover hardcoded values if they have access to the source code or compiled executable.
Attackers can easily recover hard-coded values if they have access to the source code or compiled executable.
</li>
<li>
Some hardcoded values are easily guessable.
Some hard-coded values are easily guessable.
</li>
<li>
Use of hardcoded values may leave cryptographic operations vulnerable to dictionary attacks, rainbow tables, and other forms of cryptanalysis.
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 randomly generated key material, initialization vectors, and salts. Use strong passwords that are not hardcoded.
Use randomly generated key material, initialization vectors, and salts. Use strong passwords that are not hard-coded.
</p>

</recommendation>
<example>

<p>
The following example shows instantiating a cipher with hardcoded key material, making the encrypted data vulnerable to recovery.
The following example shows instantiating a cipher with hard-coded key material, making the encrypted data vulnerable to recovery.
</p>

<sample src="HardcodedCryptographicValueBad.rs" />

<p>
In the fixed code below, the key material is randomly generated and not hardcoded, which protects the encrypted data against recovery. A real application would also need a strategy for secure key management after the key has been generated.
In the fixed code below, the key material is randomly generated and not hard-coded, which protects the encrypted data against recovery. A real application would also need a strategy for secure key management after the key has been generated.
</p>

<sample src="HardcodedCryptographicValueGood.rs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @name Hard-coded cryptographic value
* @description Using hardcoded keys, passwords, salts or initialization
* @description Using hard-coded keys, passwords, salts or initialization
* vectors is not secure.
* @kind path-problem
* @problem.severity warning
* @security-severity 9.8
* @precision high
* @id rust/hardcoded-cryptographic-value
* @id rust/hard-coded-cryptographic-value
* @tags security
* external/cwe/cwe-259
* external/cwe/cwe-321
Expand All @@ -21,7 +21,7 @@ import codeql.rust.dataflow.TaintTracking
import codeql.rust.dataflow.internal.DataFlowImpl

/**
* A taint-tracking configuration for hardcoded cryptographic value vulnerabilities.
* A taint-tracking configuration for hard-coded cryptographic value vulnerabilities.
*/
module HardcodedCryptographicValueConfig implements DataFlow::ConfigSig {
import HardcodedCryptographicValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let key: [u8;32] = [0;32]; // BAD: Using hardcoded keys for encryption
let key: [u8;32] = [0;32]; // BAD: Using hard-coded keys for encryption
let cipher = Aes256Gcm::new(&key.into());
36 changes: 18 additions & 18 deletions rust/ql/test/query-tests/security/CWE-798/test_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ fn test_stream_cipher_rabbit(
let mut rabbit_cipher1 = RabbitKeyOnly::new(rabbit::Key::from_slice(key));
rabbit_cipher1.apply_keystream(&mut data);

let const1: &[u8;16] = &[0u8;16]; // $ Alert[rust/hardcoded-cryptographic-value]
let const1: &[u8;16] = &[0u8;16]; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher2 = RabbitKeyOnly::new(rabbit::Key::from_slice(const1)); // $ Sink
rabbit_cipher2.apply_keystream(&mut data);

let mut rabbit_cipher3 = Rabbit::new(rabbit::Key::from_slice(key), rabbit::Iv::from_slice(iv));
rabbit_cipher3.apply_keystream(&mut data);

let const4: &[u8;16] = &[0u8;16]; // $ Alert[rust/hardcoded-cryptographic-value]
let const4: &[u8;16] = &[0u8;16]; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher4 = Rabbit::new(rabbit::Key::from_slice(const4), rabbit::Iv::from_slice(iv)); // $ Sink
rabbit_cipher4.apply_keystream(&mut data);

let const5: &[u8;16] = &[0u8;16]; // $ Alert[rust/hardcoded-cryptographic-value]
let const5: &[u8;16] = &[0u8;16]; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher5 = Rabbit::new(rabbit::Key::from_slice(key), rabbit::Iv::from_slice(const5)); // $ Sink
rabbit_cipher5.apply_keystream(&mut data);

// various expressions of constant arrays

let const6: &[u8;16] = &[0u8;16]; // (unused, so good)

let const7: [u8;16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hardcoded-cryptographic-value]
let const7: [u8;16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher7 = RabbitKeyOnly::new(rabbit::Key::from_slice(&const7)); // $ Sink
rabbit_cipher7.apply_keystream(&mut data);

let const8: &[u8;16] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hardcoded-cryptographic-value]
let const8: &[u8;16] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher8 = RabbitKeyOnly::new(rabbit::Key::from_slice(const8)); // $ Sink
rabbit_cipher8.apply_keystream(&mut data);

let const9: [u16;8] = [0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hardcoded-cryptographic-value]
let const9: [u16;8] = [0, 0, 0, 0, 0, 0, 0, 0]; // $ Alert[rust/hard-coded-cryptographic-value]
let const9_conv = unsafe { const9.align_to::<u8>().1 }; // convert [u16;8] -> [u8;8]
let mut rabbit_cipher9 = RabbitKeyOnly::new(rabbit::Key::from_slice(const9_conv)); // $ Sink
rabbit_cipher9.apply_keystream(&mut data);

let const10: [u8;16] = unsafe { std::mem::zeroed() }; // $ Alert[rust/hardcoded-cryptographic-value]
let const10: [u8;16] = unsafe { std::mem::zeroed() }; // $ Alert[rust/hard-coded-cryptographic-value]
let mut rabbit_cipher10 = RabbitKeyOnly::new(rabbit::Key::from_slice(&const10)); // $ Sink
rabbit_cipher10.apply_keystream(&mut data);
}
Expand All @@ -63,25 +63,25 @@ fn test_block_cipher_aes(
let aes_cipher1 = Aes256::new(key256.into());
aes_cipher1.encrypt_block(block128.into());

let const2 = &[0u8;32]; // $ Alert[rust/hardcoded-cryptographic-value]
let const2 = &[0u8;32]; // $ Alert[rust/hard-coded-cryptographic-value]
let aes_cipher2 = Aes256::new(const2.into()); // $ Sink
aes_cipher2.encrypt_block(block128.into());

let aes_cipher3 = Aes256::new_from_slice(key256).unwrap();
aes_cipher3.encrypt_block(block128.into());

let const2 = &[0u8;32]; // $ Alert[rust/hardcoded-cryptographic-value]
let const2 = &[0u8;32]; // $ Alert[rust/hard-coded-cryptographic-value]
let aes_cipher4 = Aes256::new_from_slice(const2).unwrap(); // $ Sink
aes_cipher4.encrypt_block(block128.into());

let aes_cipher5 = cfb_mode::Encryptor::<aes::Aes256>::new(key.into(), iv.into());
_ = aes_cipher5.encrypt_b2b(input, output).unwrap();

let const6 = &[0u8;32]; // $ Alert[rust/hardcoded-cryptographic-value]
let const6 = &[0u8;32]; // $ Alert[rust/hard-coded-cryptographic-value]
let aes_cipher6 = cfb_mode::Encryptor::<aes::Aes256>::new(const6.into(), iv.into()); // $ Sink
_ = aes_cipher6.encrypt_b2b(input, output).unwrap();

let const7 = &[0u8; 16]; // $ Alert[rust/hardcoded-cryptographic-value]
let const7 = &[0u8; 16]; // $ Alert[rust/hard-coded-cryptographic-value]
let aes_cipher7 = cfb_mode::Encryptor::<aes::Aes256>::new(key.into(), const7.into()); // $ Sink
_ = aes_cipher7.encrypt_b2b(input, output).unwrap();

Expand All @@ -91,18 +91,18 @@ fn test_block_cipher_aes(
let aes_cipher8 = cfb_mode::Encryptor::<aes::Aes256>::new(key8.into(), iv.into());
_ = aes_cipher8.encrypt_b2b(input, output).unwrap();

let key9: &[u8] = "1234567890123456".as_bytes(); // $ MISSING: Alert[rust/hardcoded-cryptographic-value]
let key9: &[u8] = "1234567890123456".as_bytes(); // $ MISSING: Alert[rust/hard-coded-cryptographic-value]
let aes_cipher9 = cfb_mode::Encryptor::<aes::Aes256>::new(key9.into(), iv.into());
_ = aes_cipher9.encrypt_b2b(input, output).unwrap();

let key10: [u8; 32] = match base64::engine::general_purpose::STANDARD.decode(key_str) {
Ok(x) => x.try_into().unwrap(),
Err(_) => "1234567890123456".as_bytes().try_into().unwrap() // $ MISSING: Alert[rust/hardcoded-cryptographic-value]
Err(_) => "1234567890123456".as_bytes().try_into().unwrap() // $ MISSING: Alert[rust/hard-coded-cryptographic-value]
};
let aes_cipher10 = Aes256::new(&key10.into());
aes_cipher10.encrypt_block(block128.into());

if let Ok(const11) = base64::engine::general_purpose::STANDARD.decode("1234567890123456") { // $ MISSING: Alert[rust/hardcoded-cryptographic-value]
if let Ok(const11) = base64::engine::general_purpose::STANDARD.decode("1234567890123456") { // $ MISSING: Alert[rust/hard-coded-cryptographic-value]
let key11: [u8; 32] = const11.try_into().unwrap();
let aes_cipher11 = Aes256::new(&key11.into());
aes_cipher11.encrypt_block(block128.into());
Expand All @@ -121,14 +121,14 @@ fn test_aes_gcm(
let cipher1 = Aes256Gcm::new(&key1);
let _ = cipher1.encrypt(&nonce1, b"plaintext".as_ref()).unwrap();

let key2: [u8;32] = [0;32]; // $ Alert[rust/hardcoded-cryptographic-value]
let nonce2 = [0;12]; // $ Alert[rust/hardcoded-cryptographic-value]
let key2: [u8;32] = [0;32]; // $ Alert[rust/hard-coded-cryptographic-value]
let nonce2 = [0;12]; // $ Alert[rust/hard-coded-cryptographic-value]
let cipher2 = Aes256Gcm::new(&key2.into()); // $ Sink
let _ = cipher2.encrypt(&nonce2.into(), b"plaintext".as_ref()).unwrap(); // $ Sink

let key3_array: &[u8;32] = &[0xff;32]; // $ Alert[rust/hardcoded-cryptographic-value]
let key3_array: &[u8;32] = &[0xff;32]; // $ Alert[rust/hard-coded-cryptographic-value]
let key3 = Key::<Aes256Gcm>::from_slice(key3_array);
let nonce3: [u8;12] = [0xff;12]; // $ Alert[rust/hardcoded-cryptographic-value]
let nonce3: [u8;12] = [0xff;12]; // $ Alert[rust/hard-coded-cryptographic-value]
let cipher3 = Aes256Gcm::new(&key3); // $ Sink
let _ = cipher3.encrypt(&nonce3.into(), b"plaintext".as_ref()).unwrap(); // $ Sink

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy