Skip to content

Rust: Update StreamCipherInit to use getCanonicalPath. #20238

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 8 commits into from
Aug 19, 2025

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Aug 18, 2025

Update StreamCipherInit to use getCanonicalPath.

@hvitved this change introduces a lot of missing results for calls to crypto_common::KeyInit::new. This is a bit odd as it's an associated function (so no receiver to worry about type inference on) and we do find a getStaticTarget() for the new call - just no getCanonicalPath() for it. The provided method new_from_slice in the same trait seems to work fine. If possible I'd really like to have this fixed before we merge the change (🤞).

@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code labels Aug 18, 2025
.getCanonicalPath()
.matches(["%::new", "%::new_from_slice", "%::new_with_eff_key_len", "%::new_from_slices"]) and
// extract the algorithm name from the type of `ce` or its receiver.
exists(Type t, TypePath tp |

Check warning

Code scanning / CodeQL

Omittable 'exists' variable Warning

This exists variable can be omitted by using a don't-care expression
in this argument
.
@hvitved
Copy link
Contributor

hvitved commented Aug 18, 2025

@hvitved this change introduces a lot of missing results for calls to crypto_common::KeyInit::new. This is a bit odd as it's an associated function (so no receiver to worry about type inference on) and we do find a getStaticTarget() for the new call - just no getCanonicalPath() for it.

I believe #20243 should fix this.

@geoffw0 geoffw0 marked this pull request as ready for review August 19, 2025 07:58
@geoffw0 geoffw0 requested a review from a team as a code owner August 19, 2025 07:58
@Copilot Copilot AI review requested due to automatic review settings August 19, 2025 07:58
@geoffw0
Copy link
Contributor Author

geoffw0 commented Aug 19, 2025

Thanks for the quick fix - results are much better now!

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.

Pull Request Overview

This pull request updates the StreamCipherInit class in RustCrypto.qll to use getCanonicalPath instead of the previous path-based approach for identifying cryptographic algorithm instances. The change aims to improve detection accuracy by leveraging type inference rather than parsing path expressions.

  • Refactors algorithm detection to use type inference and canonical paths
  • Updates test expectations to reflect improved detection of weak cryptographic algorithms
  • Addresses missing results for crypto_common::KeyInit::new calls

Reviewed Changes

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

File Description
rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll Replaces path parsing with type inference approach using getCanonicalPath and adds validation against known cryptographic algorithms
rust/ql/test/query-tests/security/CWE-327/test_cipher.rs Adds new test case and updates expected results comments to reflect detection changes
rust/ql/test/query-tests/security/CWE-327/BrokenCryptoAlgorithm.expected Updates expected test results with additional detected weak algorithm instances
rust/ql/test/query-tests/security/CWE-327/CONSISTENCY/PathResolutionConsistency.expected Updates line number reference for consistency test

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@@ -129,15 +133,15 @@ fn test_cbc(
_ = aes_cipher1.encrypt_padded_mut::<aes::cipher::block_padding::Pkcs7>(data, data_len).unwrap();

// des (broken)
let des_cipher1 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ Alert[rust/weak-cryptographic-algorithm]
let des_cipher1 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
Copy link
Contributor

Choose a reason for hiding this comment

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

I note that this call and the two below (also) do not resolve because we currently do not support blanket implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, and I note the issue tracking this. 👍

Co-authored-by: Tom Hvitved <hvitved@github.com>
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

I have started a DCA run.

@geoffw0 geoffw0 merged commit 963e028 into github:main Aug 19, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants