Skip to content

cpp: add insecure randomness query with RNG security models - #22437

Draft
kumarak wants to merge 1 commit into
github:mainfrom
trail-of-forks:kumarak/cpp-insecure-randomness-query
Draft

cpp: add insecure randomness query with RNG security models#22437
kumarak wants to merge 1 commit into
github:mainfrom
trail-of-forks:kumarak/cpp-insecure-randomness-query

Conversation

@kumarak

Copy link
Copy Markdown
Contributor

Add the cpp/insecure-randomness query (CWE-330/338) that flags cryptographically insecure random numbers used as security-sensitive values (keys, IVs, nonces).

  • shared quantum: add isCryptographicallySecure() to Crypto::RandomNumberGenerationInstance (defaults to none).
  • cpp quantum lib: model C stdlib, POSIX/BSD, Windows CNG, and C++ generators via the new randomNumberGeneratorModel extensible predicate; classify OpenSSL RAND_bytes/RAND_priv_bytes as secure and RAND_pseudo_bytes as insecure.
  • MaD: populate randomNumberGeneratorModel with the generator rows.

Add the cpp/insecure-randomness query (CWE-330/338) that flags
cryptographically insecure random numbers used as security-sensitive
values (keys, IVs, nonces).
- shared quantum: add isCryptographicallySecure() to
Crypto::RandomNumberGenerationInstance (defaults to none).
- cpp quantum lib: model C stdlib, POSIX/BSD, Windows CNG, and C++
<random> generators via the new randomNumberGeneratorModel extensible
predicate; classify OpenSSL RAND_bytes/RAND_priv_bytes as secure and
RAND_pseudo_bytes as insecure.
- MaD: populate randomNumberGeneratorModel with the generator rows.
CopilotAI balanced review requested due to automatic review settings August 27, 2026 01:14
@kumarak
kumarak requested review from a team as code ownersAugust 27, 2026 01:14
@kumarak
kumarak marked this pull request as draft August 27, 2026 01:15
@kumarak
kumarakforce-pushed the kumarak/cpp-insecure-randomness-query branch from 0c867c5 to acb1923CompareAugust 27, 2026 01:15

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a C++ insecure-randomness query using shared cryptography models to track weak RNG output into keys, IVs, and nonces.

Changes:

  • Adds cryptographic-security classification for RNG models.
  • Models standard, platform, C++, and OpenSSL generators.
  • Adds the query, documentation, change notes, and tests.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.

Show a summary per file
FileDescription
shared/quantum/codeql/quantum/experimental/Model.qllAdds RNG security classification.
shared/quantum/change-notes/2026-08-26-random-security-classification.mdDocuments the shared API.
cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cppExercises RNG sources and sinks.
cpp/ql/test/query-tests/Security/CWE/CWE-330/optionsConfigures test stubs.
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlrefRegisters the query test.
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expectedRecords expected results.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qlImplements taint tracking.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelpDocuments the query.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.cProvides usage examples.
cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.mdAnnounces the query.
cpp/ql/lib/ext/experimental.quantum.Random.model.ymlDefines RNG model rows.
cpp/ql/lib/experimental/quantum/Standard/Random.qllImplements data-driven RNG models.
cpp/ql/lib/experimental/quantum/OpenSSL/Random.qllClassifies OpenSSL RNGs.
cpp/ql/lib/experimental/quantum/Language.qllExposes standard RNG models.
cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.mdDocuments model additions.
Suppressed comments (1)

cpp/ql/lib/ext/experimental.quantum.Random.model.yml:24

  • In Windows headers, RtlGenRandom is a macro alias for SystemFunction036, so preprocessing makes the call target's function name SystemFunction036. The direct RtlGenRandom declaration in this test masks that behavior, and this row will not model normal uses through the Windows API headers. Model the exported name as well and make the regression fixture use the real alias shape.
 - ["", "", "RtlGenRandom", "0", true]

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

sink = any(Crypto::KeyGenerationOperationInstance op).getKeyValueConsumer()
}

predicate isBarrierIn(DataFlow::Node node) { isSource(node) }
Comment on lines +51 to +60
// A global or `std` free function, e.g. `rand` or `std::rand`.
type = "" and
f.hasGlobalOrStdName(name) and
generatorName = name
or
// A member function of a class (template), e.g. `std::mt19937::operator()`.
type != "" and
f.getName() = name and
f.getDeclaringType().getSimpleName() = type and
(if namespace = "" then generatorName = type else generatorName = namespace + "::" + type)
- ["std", "mersenne_twister_engine", "operator()", "", false]
- ["std", "linear_congruential_engine", "operator()", "", false]
- ["std", "subtract_with_carry_engine", "operator()", "", false]
- ["std", "discard_block_engine", "operator()", "", false]
- ["", "", "jrand48", "", false]
- ["", "", "rand_r", "", false]
# POSIX/BSD generators returning the value (secure).
- ["", "", "arc4random", "", true]
{
OpenSslRandomNumberGeneratorInstance() {
this.(Call).getTarget().getName() in ["RAND_bytes", "RAND_pseudo_bytes"]
this.(Call).getTarget().getName() in ["RAND_bytes", "RAND_priv_bytes", "RAND_pseudo_bytes"]
- ["", "", "arc4random_uniform", "", true]
# Generators writing to a buffer argument (secure).
- ["", "", "arc4random_buf", "0", true]
- ["", "", "getrandom", "0", true]

@jketemajketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see you turned this into draft after opening this PR. I just want to note that we will not accept any queries in our main line query directory that depend on query libraries from experimental directories.

@kumarak

Copy link
Copy Markdown
ContributorAuthor

I see you turned this into draft after opening this PR. I just want to note that we will not accept any queries in our main line query directory that depend on query libraries from experimental directories.

Thanks, @jketema, for letting me know. I will remove the use of libraries from experimental directories.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@kumarak@jketema