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

@kumarak kumarak commented Aug 27, 2026

Copy link
Copy Markdown

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.
Copilot AI balanced review requested due to automatic review settings August 27, 2026 01:14
@kumarak
kumarak requested review from a team as code owners August 27, 2026 01:14
@kumarak
kumarak marked this pull request as draft August 27, 2026 01:15
@kumarak
kumarak force-pushed the kumarak/cpp-insecure-randomness-query branch from 0c867c5 to acb1923 Compare August 27, 2026 01:15

Copilot AI 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
File Description
shared/quantum/codeql/quantum/experimental/Model.qll Adds RNG security classification.
shared/quantum/change-notes/2026-08-26-random-security-classification.md Documents the shared API.
cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cpp Exercises RNG sources and sinks.
cpp/ql/test/query-tests/Security/CWE/CWE-330/options Configures test stubs.
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlref Registers the query test.
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expected Records expected results.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.ql Implements taint tracking.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelp Documents the query.
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.c Provides usage examples.
cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.md Announces the query.
cpp/ql/lib/ext/experimental.quantum.Random.model.yml Defines RNG model rows.
cpp/ql/lib/experimental/quantum/Standard/Random.qll Implements data-driven RNG models.
cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll Classifies OpenSSL RNGs.
cpp/ql/lib/experimental/quantum/Language.qll Exposes standard RNG models.
cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.md Documents 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]
Sign up for free to 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.

2 participants