You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[New Queries - githubsecuritylab/rust/hardcoded-cryptographic-key, githubsecuritylab/rust/cleartext-logging-of-key-material] - detect hard-coded and logged cryptographic keys in Rust - #259
rust/src/common/CryptoKeyMaterial.qll (key sinks, random buffers and key conversions)
Why
The standard rust/hard-coded-cryptographic-value query knows the RustCrypto key sinks, but not the key and passphrase constructors of age, orion and ring, and a key that is wrapped with GenericArray::from or secrecy::Secret::new loses the flow. The first query adds those, and does not repeat a result that the standard query already reports.
The standard rust/cleartext-logging query recognizes sensitive data by variable names, so a key called key_bytes, or a buffer that a random number generator filled, is missed. The second query reports generated key material that reaches a log or the console, also after hex encoding.
What is reported
Hard-coded key: a constant (a literal, a constant array or a repeated value array) that reaches the key or passphrase argument of a cryptographic API: the sinks of the standard query, age passphrase encryption and decryption, orionSecretKey::from_slice and Password::from_slice, and ring AEAD, HMAC and HKDF keys. A buffer that is overwritten with random bytes, or filled in place (passed to a call as &mut buffer, or the target of copy_from_slice, fill and similar methods), is a barrier.
Logged key: a key from a key generation API (generate_key), or a buffer that a random number generator filled (ring, rand, getrandom) and that is also used as a key, that reaches a log macro or the console. A property of the key such as its length is not reported.
Evidence
Tests use real crates (aes, aes-gcm, age, camellia, orion, ring, rand, getrandom, hex, log). The hard-coded key test has 19 alerts and the logging test has 12, each with safe counterparts (including independent calls of one helper, or of a helper that wraps it, that fill a key and a logged request id, and a ring HKDF key that is hard-coded or generated and logged). They pass with the pinned CLI (codeql test run), and both queries compile with --warnings=error.
On a random sample of 11 public Rust repositories (1,521 source files, plus the crates they depend on) the logging query reports 0 alerts. The hard-coded key query reports 3 alerts, all in the test code of one project: two fixed keys in unit tests, and a key of the wrong length that a test passes on purpose to check that it is rejected. So there is no finding in production code yet.
Cost: the queries' own predicates take about 2 to 3 seconds each on the largest database (about 19,000 Rust files, almost all of them dependency sources).
Known limitations
The logging query also reports a tool whose purpose is to generate a key and print it. The query help says to dismiss the alert in that case.
A buffer that is filled in place is treated as filled at every use, and what it is filled with is not checked. A constant key that is used before the fill, or that is filled with another constant, is not reported.
A helper that returns a randomly filled buffer, or fills one of its parameters in place, is recognized only when the fill is of the returned buffer or of the parameter itself. A fill of a part of a parameter (for example &mut out[..16]) keeps the source in the helper, shared by every call, and nested helpers are not promoted to sources (see the query help).
The age, orion and ring call targets are matched by their canonical paths in CryptoKeyMaterial.qll. They could become credentials-key and credentials-password model rows later.
Notes
Metadata: @precision medium for both queries, tags security, experimental and the CWE tags. Both are selected by rust.qls.
The shared library is in rust/src/common/ because the Rust packs have no lib pack yet.
The pack version is not changed here. It changes when a release is cut.
Add githubsecuritylab/rust/hardcoded-cryptographic-key (CWE-321, CWE-798),
which extends the standard rust/hard-coded-cryptographic-value query with the
key and passphrase arguments of the age, orion and ring crates, and
githubsecuritylab/rust/cleartext-logging-of-key-material (CWE-312, CWE-532),
which reports a generated key that is written to a log or to the console. The
two queries share common/CryptoKeyMaterial.qll.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The module-path regex misses Orion’s chacha20 key constructor, causing detection gaps, and the documentation overstates the safety guarantee of the order- and content-insensitive fill check.
Adds two experimental Rust security queries that complement the standard CodeQL queries by detecting hard-coded cryptographic keys and generated key material written to logs or the console.
Changes:
Adds shared models for cryptographic APIs, random-buffer fills, key conversions, and hex encoding.
Implements both path queries with filtering for duplicate reports and non-sensitive properties.
Adds query help, examples, and real-crate regression fixtures with expected results.
…ill barrier
The orion sink patterns used a module path class without digits, so
orion::hazardous::stream::chacha20::SecretKey::from_slice and similar
constructors were not recognized. Allow digits and add a regression.
State in the query help that the in-place fill check ignores the order of
the statements and the content that is written.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The pinned codeql/rust-all implementation was unavailable for inspection, leaving source, barrier, sink, and standard-query deduplication behavior dependent on human validation against the library and pinned-CLI results.
The Test Queries step splits the tests of a language into four slices and
starts one codeql test run per slice. The slices are made by test directory,
so a pull request with two Rust test directories extracts them at the same
time on a fresh runner.
The Rust extractor installs the toolchain that it needs with rustup on
demand. Both slices download it into the same ~/.rustup, and one of them
fails at extraction with "could not rename 'downloaded' file", which fails
Validate test results. Running the job again is unlikely to help, because
every runner starts without the toolchain.
Run Rust as a single slice, so that its test directories are extracted one
after the other. The other languages keep four slices.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Mutable-output helpers can share a source across calls, causing a separately generated and logged request ID to be falsely reported as leaked key material.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Track random sources per mutable-output helper call
A helper such as fill_random(out: &mut [u8]) that calls getrandom::getrandom(out) retains a shared source here because it does not return the buffer. If separate calls fill a key and a printed request ID, the key-use and logging checks can both succeed from that source, incorrectly reporting the request ID as leaked key material. Give mutable-output helpers call-site-specific sources too, and add a regression test with independent key and request-ID calls.
…ach call
A helper that fills a mutable parameter with random bytes, such as
fn fill_random(out: &mut [u8]), kept one source at the fill in its body,
shared by every call. A key and a logged request id that were filled by
separate calls shared it, so the request id was reported as key material.
Use the buffer that each call passes as the source instead, as is done for
a helper that returns the bytes, and drop the shared source in the helper.
A call that passes on a parameter of its own function is not a source, like
a nested returning wrapper.
Add regressions for independent calls that fill a key and a logged request
id, for a key that is filled by the helper and logged, and for a helper that
wraps the filling helper.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
Thanks for the third pass. The finding in the review body was valid and is fixed in f2237e8 with a failing test first.
With fn fill_random(out: &mut [u8]) called once for a key and once for a request id that is logged, the request id was reported as key material (test_independent_fill_helper_results), because the source was the fill inside the helper, shared by every call.
A call of a helper that fills a parameter in place is now the source, as it already was for a helper that returns the bytes: the argument that the call passes. The key that is filled by the helper and logged is still reported (test_logged_fill_helper_key).
Two limits are documented in the query help and the pull request body: a fill of only a part of a parameter (for example &mut out[..16]) keeps its source in the helper, and a call that passes on a parameter of the function that contains it is not a source (nested wrappers are not promoted, which test_independent_nested_fill_helper_results pins).
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because neither test suite exercises the new HKDF sink, leaving its constructor recognition and role in the logging query unverified.
Both queries treat the key that is passed to ring::hkdf::Prk::new_less_safe
as a key sink, but no test used it. Add a hard-coded HKDF key and a safe
counterpart to the hard-coded key test, and a generated key that is used for
HKDF and logged to the logging test. No query change: the sink was already
recognized.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
Thanks. The HKDF key sink was already recognized, but you were right that no test used it. b371bac adds test_ring_hkdf_bad (a hard-coded key for ring::hkdf::Prk::new_less_safe) and a safe counterpart to the hard-coded key test, and test_logged_hkdf_key (a generated key that is used for HKDF and logged) to the logging test. Both queries report the bad cases, so there is no query change.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Subtle source-suppression gaps involving conversions and aliased parameters can conflate independent calls and produce false key-leak reports, requiring human assessment.
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
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.
Summary
Adds two experimental Rust queries about cryptographic key material, and the library they share:
githubsecuritylab/rust/hardcoded-cryptographic-key(CWE-321, CWE-798)githubsecuritylab/rust/cleartext-logging-of-key-material(CWE-312, CWE-532)rust/src/common/CryptoKeyMaterial.qll(key sinks, random buffers and key conversions)Why
rust/hard-coded-cryptographic-valuequery knows the RustCrypto key sinks, but not the key and passphrase constructors ofage,orionandring, and a key that is wrapped withGenericArray::fromorsecrecy::Secret::newloses the flow. The first query adds those, and does not repeat a result that the standard query already reports.rust/cleartext-loggingquery recognizes sensitive data by variable names, so a key calledkey_bytes, or a buffer that a random number generator filled, is missed. The second query reports generated key material that reaches a log or the console, also after hex encoding.What is reported
agepassphrase encryption and decryption,orionSecretKey::from_sliceandPassword::from_slice, andringAEAD, HMAC and HKDF keys. A buffer that is overwritten with random bytes, or filled in place (passed to a call as&mut buffer, or the target ofcopy_from_slice,filland similar methods), is a barrier.generate_key), or a buffer that a random number generator filled (ring,rand,getrandom) and that is also used as a key, that reaches a log macro or the console. A property of the key such as its length is not reported.Evidence
ringHKDF key that is hard-coded or generated and logged). They pass with the pinned CLI (codeql test run), and both queries compile with--warnings=error.Known limitations
&mut out[..16]) keeps the source in the helper, shared by every call, and nested helpers are not promoted to sources (see the query help).age,orionandringcall targets are matched by their canonical paths inCryptoKeyMaterial.qll. They could becomecredentials-keyandcredentials-passwordmodel rows later.Notes
@precision mediumfor both queries, tagssecurity,experimentaland the CWE tags. Both are selected byrust.qls.rust/src/common/because the Rust packs have nolibpack yet.