Skip to content
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

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Mar 6, 2025

Adds a "Hard-coded cryptographic value" (rust/hardcoded-crytographic-value) query for Rust, detecting use of hardcoded keys, passwords, salts and initialization vectors. In other languages these have sometimes been implemented as separate queries (e.g. swift/constant-password, swift/hardcoded-key, swift/constant-salt, swift/static-initialization-vector) but I see no benefit to them being separate.

Note that a few sinks have been defined for the query in this pull request but they're a bit sparse. Improving that will be follow-up work.

TODO:

  • review DCA
  • code review
  • docs review

@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code labels Mar 6, 2025
@Copilot Copilot bot review requested due to automatic review settings March 6, 2025 18:51
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request adds a new query to detect hardcoded cryptographic values in Rust code along with corresponding test cases and configuration updates for dependencies and library models.

  • Introduces query tests for detecting hardcoded keys, salts, and initialization vectors in various cipher configurations.
  • Provides both "bad" (hardcoded) and "good" (randomly generated) examples for cryptographic key usage.
  • Updates dependency options and model files to support the new query.

Reviewed Changes

File Description
rust/ql/test/query-tests/security/CWE-798/test_cipher.rs Adds various test cases for hardcoded cryptographic values in cipher operations.
rust/ql/test/query-tests/security/CWE-798/options.yml Specifies dependencies and cargo check options required for the tests.
rust/ql/src/queries/security/CWE-798/HardcodedCryptographicValueBad.rs Introduces an example of bad usage with hardcoded keys.
rust/ql/src/queries/security/CWE-798/HardcodedCryptographicValueGood.rs Provides an example of good usage using randomly generated keys.
rust/ql/lib/codeql/rust/frameworks/genericarray.model.yml Updates model to include generic-array methods relevant to the query.
rust/ql/lib/codeql/rust/frameworks/rustcrypto/rustcrypto.model.yml Enhances rustcrypto model with new mappings for cryptographic key and IV usage.
rust/ql/lib/codeql/rust/frameworks/stdlib/lang-core.model.yml Adds conversions and constant source taint tracking relevant to key initialization.

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

Copy link
Contributor

github-actions bot commented Mar 7, 2025

QHelp previews:

rust/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.qhelp

Hard-coded cryptographic value

Hardcoded passwords, keys, initialization vectors and salts should not be used for cryptographic operations.

  • Attackers can easily recover hardcoded values if they have access to the source code or compiled executable.
  • Some hardcoded values are easily guessable.
  • Use of hardcoded values may leave cryptographic operations vulnerable to dictionary attacks, rainbow tables, and other forms of cryptanalysis.

Recommendation

Use randomly generated key material, initialization vectors and salts. Use strong passwords that are not hardcoded.

Example

The following example shows instantiating a cipher with hardcoded key material, making the encrypted data vulnerable to recovery.

let key: [u8;32] = [0;32]; // BAD: Using hardcoded keys for encryption
let cipher = Aes256Gcm::new(&key.into());

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.

let key = Aes256Gcm::generate_key(aes_gcm::aead::OsRng); // GOOD: Using randomly generated keys for encryption
let cipher = Aes256Gcm::new(&key);

References

@geoffw0 geoffw0 added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Mar 7, 2025
- ["repo:https://github.com/RustCrypto/traits:cipher", "<crate::stream_wrapper::StreamCipherCoreWrapper as crate::KeyIvInit>::new", "Argument[1]", "credentials-iv", "manual"]
- ["repo:https://github.com/RustCrypto/traits:cipher", "<crate::stream_wrapper::StreamCipherCoreWrapper as crate::KeyIvInit>::new_from_slice", "Argument[0]", "credentials-key", "manual"]
- ["repo:https://github.com/RustCrypto/traits:cipher", "<crate::stream_wrapper::StreamCipherCoreWrapper as crate::KeyIvInit>::new_from_slice", "Argument[1]", "credentials-iv", "manual"]
- ["repo:https://github.com/RustCrypto/block-ciphers:aes", "<crate::soft::Aes128 as crate::KeyInit>::new", "Argument[0]", "credentials-key", "manual"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these models are bothering me. They're all based on data from getResolvedPath + getResolvedCrateOrigin and they demonstrably work but:

  • it would be cleaner and easier to implement the models if they could be expressed in more general terms, e.g. in terms of traits rather than implementations.
  • it's also likely (well, certain) we're missing some implementations with these models as a result (AES is not he only block cipher in existence).
  • I also observed some weird platform dependence, where everything worked on my local Mac but for CI I had to add a few additional variants of models to get the same results as I was already getting locally - that's what this commit is. I don't know if there was a known quirk affecting me here or something potentially serious and undiagnosed.

@hvitved I would really appreciate your opinion on these issues. I think we will need to address them to model sources and sinks effectively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update on the third bullet points: the MaD IDs still don't match what I generate locally. That's surely a clue to what's going on. Hopefully I just need to update something I haven't updated on my machine??? 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation no-change-note-required This PR does not need a change note ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant