diff --git a/cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.md b/cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.md new file mode 100644 index 000000000000..ea54b98ed7d4 --- /dev/null +++ b/cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Added C++ random number generator models for the C standard library, POSIX/BSD, Windows, OpenSSL, and C++ engines. The models identify the output and its cryptographic security and are also available through the quantum library. Data extensions can register additional generators with `randomNumberGeneratorModel`. diff --git a/cpp/ql/lib/experimental/quantum/Language.qll b/cpp/ql/lib/experimental/quantum/Language.qll index d3feb3fe0d98..7b5b15498693 100644 --- a/cpp/ql/lib/experimental/quantum/Language.qll +++ b/cpp/ql/lib/experimental/quantum/Language.qll @@ -110,3 +110,4 @@ private class ConstantDataSource extends Crypto::GenericConstantSourceInstance i } import OpenSSL.OpenSSL +import Standard.Random diff --git a/cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll b/cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll index d39087bcbce0..82448ae2f6cf 100644 --- a/cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll +++ b/cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll @@ -1,17 +1,9 @@ -import cpp -private import experimental.quantum.Language -private import LibraryDetector -private import semmle.code.cpp.dataflow.new.DataFlow +private import cpp +private import experimental.quantum.Standard.Random -class OpenSslRandomNumberGeneratorInstance extends Crypto::RandomNumberGenerationInstance instanceof Call -{ +/** A call to an OpenSSL random byte generator. */ +class OpenSslRandomNumberGeneratorInstance extends ModeledRandomNumberGeneratorInstance { OpenSslRandomNumberGeneratorInstance() { - this.(Call).getTarget().getName() in ["RAND_bytes", "RAND_pseudo_bytes"] + this.(Call).getTarget().hasGlobalName(["RAND_bytes", "RAND_priv_bytes", "RAND_pseudo_bytes"]) } - - override Crypto::DataFlowNode getOutputNode() { - result.asDefiningArgument() = this.(Call).getArgument(0) - } - - override string getGeneratorName() { result = this.(Call).getTarget().getName() } } diff --git a/cpp/ql/lib/experimental/quantum/Standard/Random.qll b/cpp/ql/lib/experimental/quantum/Standard/Random.qll new file mode 100644 index 000000000000..28d8d9cec0f8 --- /dev/null +++ b/cpp/ql/lib/experimental/quantum/Standard/Random.qll @@ -0,0 +1,19 @@ +/** Exposes the C++ random number generator models as cryptographic artifacts. */ + +private import experimental.quantum.Language +private import semmle.code.cpp.security.RandomNumberGeneration as Random + +class ModeledRandomNumberGeneratorInstance extends Crypto::RandomNumberGenerationInstance instanceof Random::RandomNumberGeneration +{ + override Crypto::DataFlowNode getOutputNode() { + result = this.(Random::RandomNumberGeneration).getOutputNode() + } + + override string getGeneratorName() { + result = this.(Random::RandomNumberGeneration).getGeneratorName() + } + + override predicate isCryptographicallySecure() { + this.(Random::RandomNumberGeneration).isCryptographicallySecure() + } +} diff --git a/cpp/ql/lib/ext/random.model.yml b/cpp/ql/lib/ext/random.model.yml new file mode 100644 index 000000000000..6a4145dbc160 --- /dev/null +++ b/cpp/ql/lib/ext/random.model.yml @@ -0,0 +1,37 @@ +extensions: + - addsTo: + pack: codeql/cpp-all + extensible: randomNumberGeneratorModel + # namespace, type, name, output, secure + data: + # C standard library / POSIX / BSD generators returning the value (insecure). + - ["", "", "rand", "", false] + - ["", "", "random", "", false] + - ["", "", "drand48", "", false] + - ["", "", "erand48", "", false] + - ["", "", "lrand48", "", false] + - ["", "", "nrand48", "", false] + - ["", "", "mrand48", "", false] + - ["", "", "jrand48", "", false] + - ["", "", "rand_r", "", false] + # POSIX/BSD generators returning the value (secure). + - ["", "", "arc4random", "", true] + - ["", "", "arc4random_uniform", "", true] + # Generators writing to a buffer argument (secure). + - ["", "", "arc4random_buf", "0", true] + - ["", "", "getentropy", "0", true] + - ["", "", "RtlGenRandom", "0", true] + - ["", "", "BCryptGenRandom", "1", true] + - ["", "", "CryptGenRandom", "2", true] + # OpenSSL byte generators. + - ["", "", "RAND_bytes", "0", true] + - ["", "", "RAND_priv_bytes", "0", true] + - ["", "", "RAND_pseudo_bytes", "0", false] + # C++ engines (insecure) and std::random_device (secure). + - ["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] + - ["std", "shuffle_order_engine", "operator()", "", false] + - ["std", "independent_bits_engine", "operator()", "", false] + - ["std", "random_device", "operator()", "", true] diff --git a/cpp/ql/lib/semmle/code/cpp/security/RandomNumberGeneration.qll b/cpp/ql/lib/semmle/code/cpp/security/RandomNumberGeneration.qll new file mode 100644 index 000000000000..9269ccbd3671 --- /dev/null +++ b/cpp/ql/lib/semmle/code/cpp/security/RandomNumberGeneration.qll @@ -0,0 +1,57 @@ +/** Provides models of random number generator calls and their outputs. */ + +private import cpp +private import semmle.code.cpp.ir.dataflow.DataFlow + +/** + * Models a generator named `name` in `namespace` and declaring class `type`. + * An empty namespace matches the global or std namespace. An empty type selects free functions. + * An empty `output` selects the return value, otherwise it identifies the output buffer argument. + * `secure` holds for cryptographically secure generators. + */ +extensible predicate randomNumberGeneratorModel( + string namespace, string type, string name, string output, boolean secure +); + +private predicate generator(Call call, string generatorName, string output, boolean secure) { + exists(string ns, string type, string name, string base | + randomNumberGeneratorModel(ns, type, name, output, secure) and + ( + call.getTarget().hasQualifiedName(ns, type, name) + or + ns = "" and call.getTarget().hasQualifiedName("std", type, name) + ) and + (if type = "" then base = name else base = type) and + (if ns = "" then generatorName = base else generatorName = ns + "::" + base) + ) + or + call.getTarget().hasGlobalName("getrandom") and + generatorName = "getrandom" and + output = "0" and + // Linux UAPI defines GRND_INSECURE as 0x0004. Unknown flags do not establish security. + exists(int flags | flags = call.getArgument(2).getValue().toInt() | + if flags.bitAnd(4) = 0 then secure = true else secure = false + ) +} + +/** A call producing random output with a known security classification. */ +class RandomNumberGeneration extends Call { + string generatorName; + string output; + boolean secure; + + RandomNumberGeneration() { generator(this, generatorName, output, secure) } + + /** Gets the name of the generator. */ + string getGeneratorName() { result = generatorName } + + /** Gets the generated value, or the contents of the buffer immediately after the call. */ + DataFlow::Node getOutputNode() { + output = "" and result.asExpr() = this + or + result.asDefiningArgument(1) = this.getArgument(output.toInt()) + } + + /** Holds if this generator produces cryptographically secure output. */ + predicate isCryptographicallySecure() { secure = true } +} diff --git a/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.c b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.c new file mode 100644 index 000000000000..f99c282e6ffb --- /dev/null +++ b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.c @@ -0,0 +1,19 @@ +#include +#include +#include + +void encrypt(EVP_CIPHER_CTX *ctx, unsigned char *iv) { + unsigned char key[16]; + + // BAD: the key is derived from a cryptographically weak generator, so an + // attacker may be able to predict it. + for (int i = 0; i < 16; i++) { + key[i] = (unsigned char)rand(); + } + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, key, iv); + + // GOOD: the key is filled from a cryptographically secure generator. + if (RAND_bytes(key, sizeof(key)) != 1) + return; + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, key, iv); +} diff --git a/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelp b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelp new file mode 100644 index 000000000000..31f45ff01f89 --- /dev/null +++ b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelp @@ -0,0 +1,52 @@ + + + +

+Using a cryptographically weak pseudo-random number generator to produce a security-sensitive value, +such as an encryption key, an initialization vector, a nonce, or a session token, may allow an attacker +to predict the value. +

+ +

+A pseudo-random number generator produces a sequence of numbers that only approximates the properties of +random numbers. The sequence is completely determined by a relatively small seed value. Generators such as +rand, the drand48 family, and the C++ <random> engines +(for example std::mt19937) are not designed to resist prediction, so an attacker who observes +some output, or who can reconstruct the seed, may be able to predict future values. +

+
+ + +

+Use a cryptographically secure random number generator when the output is used in a security-sensitive +context. Suitable choices include getrandom without GRND_INSECURE, getentropy, the +arc4random family, OpenSSL's RAND_bytes, the Windows +BCryptGenRandom function, and C++'s std::random_device (where it is backed by a +secure source). +

+
+ + +

+The following example seeds an AES key with rand. Because rand is not +cryptographically secure, an attacker may be able to predict the key. +

+ + + +

+Instead, fill the key from a cryptographically secure generator such as RAND_bytes. +

+
+ + +
  • Wikipedia: +Pseudorandom number generator.
  • +
  • Common Weakness Enumeration: +CWE-330: Use of Insufficiently Random Values.
  • +
  • Common Weakness Enumeration: +CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG).
  • +
    +
    diff --git a/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.ql b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.ql new file mode 100644 index 000000000000..b8a174703b33 --- /dev/null +++ b/cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.ql @@ -0,0 +1,82 @@ +/** + * @name Insecure randomness + * @description Using a cryptographically insecure pseudo-random number generator to generate a + * security-sensitive value may allow an attacker to predict what value will + * be generated. + * @kind path-problem + * @problem.severity warning + * @security-severity 7.8 + * @precision medium + * @id cpp/insecure-randomness + * @tags security + * external/cwe/cwe-330 + * external/cwe/cwe-338 + */ + +import cpp +import semmle.code.cpp.ir.dataflow.TaintTracking +import semmle.code.cpp.security.RandomNumberGeneration +import InsecureRandomnessFlow::PathGraph + +/** Holds if argument `index` supplies a key or IV to an OpenSSL operation. */ +private predicate keyOrIvArgument(Function f, int index) { + f.hasGlobalName(["EVP_EncryptInit_ex", "EVP_DecryptInit_ex", "EVP_CipherInit_ex"]) and + index = [3, 4] + or + f.hasGlobalName([ + "EVP_EncryptInit", "EVP_DecryptInit", "EVP_CipherInit", "EVP_EncryptInit_ex2", + "EVP_DecryptInit_ex2", "EVP_CipherInit_ex2", "EVP_CipherInit_SKEY" + ]) and + index = [2, 3] + or + f.hasGlobalName(["EVP_PKEY_new_mac_key", "DSA_do_sign"]) and index = 2 + or + f.hasGlobalName(["EVP_PKEY_CTX_set_mac_key", "EVP_PKEY_CTX_new_from_pkey"]) and index = 1 + or + f.hasGlobalName("EVP_PKEY_CTX_new") and index = 0 + or + f.hasGlobalName(["EVP_DigestSignInit", "EVP_DigestVerifyInit"]) and index = 4 + or + f.hasGlobalName(["EVP_DigestSignInit_ex", "EVP_DigestVerifyInit_ex", "RSA_sign", "RSA_verify"]) and + index = 5 + or + f.hasGlobalName([ + "EVP_SignFinal", "EVP_SignFinal_ex", "EVP_VerifyFinal", "EVP_VerifyFinal_ex", "DSA_do_verify" + ]) and + index = 3 +} + +/** + * A taint-tracking configuration for flow from a cryptographically insecure + * random number generator to security-sensitive value such as a key, IV, or nonce. + */ +module InsecureRandomnessConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node source) { + exists(RandomNumberGeneration generator | + not generator.isCryptographicallySecure() and + source = generator.getOutputNode() + ) + } + + predicate isSink(DataFlow::Node sink) { + exists(FunctionCall call, int i | + keyOrIvArgument(call.getTarget(), i) and sink.asIndirectExpr(1) = call.getArgument(i) + ) + } + + predicate isBarrierIn(DataFlow::Node node) { + node = any(RandomNumberGeneration generator).getOutputNode() + } + + predicate isBarrierOut(DataFlow::Node node) { isSink(node) } + + predicate observeDiffInformedIncrementalMode() { any() } +} + +module InsecureRandomnessFlow = TaintTracking::Global; + +from InsecureRandomnessFlow::PathNode source, InsecureRandomnessFlow::PathNode sink +where InsecureRandomnessFlow::flowPath(source, sink) +select sink.getNode(), source, sink, + "This security-sensitive value depends on $@, which is not cryptographically secure.", + source.getNode(), "a randomly generated number" diff --git a/cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.md b/cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.md new file mode 100644 index 000000000000..56f1231367fe --- /dev/null +++ b/cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.md @@ -0,0 +1,4 @@ +--- +category: newQuery +--- +* Added a new query, `cpp/insecure-randomness` ("Insecure randomness"), which flags cryptographically insecure random numbers (for example from `rand` or `std::mt19937`) that are used as security-sensitive values such as encryption keys, IVs, or nonces. diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.expected new file mode 100644 index 000000000000..2e7fb23cd586 --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.expected @@ -0,0 +1,39 @@ +| test.cpp:133:36:133:39 | call to rand | rand | test.cpp:133:21:133:41 | call to rand | false | +| test.cpp:141:36:141:44 | call to rand | rand | test.cpp:141:21:141:46 | call to rand | false | +| test.cpp:149:36:149:41 | call to random | random | test.cpp:149:21:149:43 | call to random | false | +| test.cpp:157:36:157:42 | call to drand48 | drand48 | test.cpp:157:21:157:44 | call to drand48 | false | +| test.cpp:166:36:166:42 | call to erand48 | erand48 | test.cpp:166:21:166:45 | call to erand48 | false | +| test.cpp:174:36:174:42 | call to lrand48 | lrand48 | test.cpp:174:21:174:44 | call to lrand48 | false | +| test.cpp:183:36:183:42 | call to nrand48 | nrand48 | test.cpp:183:21:183:45 | call to nrand48 | false | +| test.cpp:191:36:191:42 | call to mrand48 | mrand48 | test.cpp:191:21:191:44 | call to mrand48 | false | +| test.cpp:200:36:200:42 | call to jrand48 | jrand48 | test.cpp:200:21:200:45 | call to jrand48 | false | +| test.cpp:209:36:209:41 | call to rand_r | rand_r | test.cpp:209:21:209:48 | call to rand_r | false | +| test.cpp:221:37:221:40 | call to rand | rand | test.cpp:221:22:221:42 | call to rand | false | +| test.cpp:227:36:227:39 | call to rand | rand | test.cpp:227:21:227:41 | call to rand | false | +| test.cpp:239:39:239:39 | call to operator() | std::mersenne_twister_engine | test.cpp:239:21:239:40 | call to operator() | false | +| test.cpp:248:39:248:39 | call to operator() | std::linear_congruential_engine | test.cpp:248:21:248:40 | call to operator() | false | +| test.cpp:257:39:257:39 | call to operator() | std::discard_block_engine | test.cpp:257:21:257:40 | call to operator() | false | +| test.cpp:266:39:266:39 | call to operator() | std::shuffle_order_engine | test.cpp:266:21:266:40 | call to operator() | false | +| test.cpp:275:39:275:39 | call to operator() | std::independent_bits_engine | test.cpp:275:21:275:40 | call to operator() | false | +| test.cpp:284:39:284:39 | call to operator() | std::linear_congruential_engine | test.cpp:284:21:284:40 | call to operator() | false | +| test.cpp:297:3:297:19 | call to RAND_pseudo_bytes | RAND_pseudo_bytes | test.cpp:297:21:297:22 | RAND_pseudo_bytes output argument | false | +| test.cpp:306:3:306:12 | call to RAND_bytes | RAND_bytes | test.cpp:306:14:306:15 | RAND_bytes output argument | true | +| test.cpp:318:36:318:45 | call to arc4random | arc4random | test.cpp:318:21:318:47 | call to arc4random | true | +| test.cpp:326:36:326:53 | call to arc4random_uniform | arc4random_uniform | test.cpp:326:21:326:58 | call to arc4random_uniform | true | +| test.cpp:335:3:335:16 | call to arc4random_buf | arc4random_buf | test.cpp:335:18:335:19 | arc4random_buf output argument | true | +| test.cpp:344:3:344:11 | call to getrandom | getrandom | test.cpp:344:13:344:14 | getrandom output argument | true | +| test.cpp:353:3:353:12 | call to getentropy | getentropy | test.cpp:353:14:353:15 | getentropy output argument | true | +| test.cpp:362:3:362:14 | call to RtlGenRandom | RtlGenRandom | test.cpp:362:16:362:17 | RtlGenRandom output argument | true | +| test.cpp:371:3:371:16 | call to CryptGenRandom | CryptGenRandom | test.cpp:371:24:371:25 | CryptGenRandom output argument | true | +| test.cpp:380:3:380:17 | call to BCryptGenRandom | BCryptGenRandom | test.cpp:380:22:380:23 | BCryptGenRandom output argument | true | +| test.cpp:389:38:389:38 | call to operator() | std::random_device | test.cpp:389:21:389:39 | call to operator() | true | +| test.cpp:399:3:399:11 | call to getrandom | getrandom | test.cpp:399:13:399:15 | getrandom output argument | false | +| test.cpp:401:3:401:11 | call to getrandom | getrandom | test.cpp:401:13:401:15 | getrandom output argument | false | +| test.cpp:403:3:403:11 | call to getrandom | getrandom | test.cpp:403:13:403:15 | getrandom output argument | true | +| test.cpp:410:3:410:19 | call to RAND_pseudo_bytes | RAND_pseudo_bytes | test.cpp:410:21:410:23 | RAND_pseudo_bytes output argument | false | +| test.cpp:412:7:412:16 | call to RAND_bytes | RAND_bytes | test.cpp:412:18:412:20 | RAND_bytes output argument | true | +| test.cpp:414:7:414:21 | call to RAND_priv_bytes | RAND_priv_bytes | test.cpp:414:23:414:25 | RAND_priv_bytes output argument | true | +| test.cpp:432:10:432:19 | call to arc4random | arc4random | test.cpp:432:10:432:19 | call to arc4random | true | +| test.cpp:432:47:432:50 | call to rand | rand | test.cpp:432:25:432:53 | call to rand | false | +| test.cpp:437:7:437:16 | call to RAND_bytes | RAND_bytes | test.cpp:437:18:437:20 | RAND_bytes output argument | true | +| test.cpp:439:27:439:30 | call to rand | rand | test.cpp:439:12:439:32 | call to rand | false | diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.ql b/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.ql new file mode 100644 index 000000000000..9b3238f39923 --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/Generators.ql @@ -0,0 +1,6 @@ +import cpp +import semmle.code.cpp.security.RandomNumberGeneration + +from RandomNumberGeneration generator, boolean secure +where if generator.isCryptographicallySecure() then secure = true else secure = false +select generator, generator.getGeneratorName(), generator.getOutputNode(), secure diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expected new file mode 100644 index 000000000000..e8ee837aad4f --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expected @@ -0,0 +1,134 @@ +#select +| test.cpp:134:49:134:50 | *& ... | test.cpp:133:21:133:41 | call to rand | test.cpp:134:49:134:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:133:21:133:41 | call to rand | a randomly generated number | +| test.cpp:142:49:142:50 | *& ... | test.cpp:141:21:141:46 | call to rand | test.cpp:142:49:142:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:141:21:141:46 | call to rand | a randomly generated number | +| test.cpp:150:49:150:50 | *& ... | test.cpp:149:21:149:43 | call to random | test.cpp:150:49:150:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:149:21:149:43 | call to random | a randomly generated number | +| test.cpp:158:49:158:50 | *& ... | test.cpp:157:21:157:44 | call to drand48 | test.cpp:158:49:158:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:157:21:157:44 | call to drand48 | a randomly generated number | +| test.cpp:167:49:167:50 | *& ... | test.cpp:166:21:166:45 | call to erand48 | test.cpp:167:49:167:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:166:21:166:45 | call to erand48 | a randomly generated number | +| test.cpp:175:49:175:50 | *& ... | test.cpp:174:21:174:44 | call to lrand48 | test.cpp:175:49:175:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:174:21:174:44 | call to lrand48 | a randomly generated number | +| test.cpp:184:49:184:50 | *& ... | test.cpp:183:21:183:45 | call to nrand48 | test.cpp:184:49:184:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:183:21:183:45 | call to nrand48 | a randomly generated number | +| test.cpp:192:49:192:50 | *& ... | test.cpp:191:21:191:44 | call to mrand48 | test.cpp:192:49:192:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:191:21:191:44 | call to mrand48 | a randomly generated number | +| test.cpp:201:49:201:50 | *& ... | test.cpp:200:21:200:45 | call to jrand48 | test.cpp:201:49:201:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:200:21:200:45 | call to jrand48 | a randomly generated number | +| test.cpp:210:49:210:50 | *& ... | test.cpp:209:21:209:48 | call to rand_r | test.cpp:210:49:210:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:209:21:209:48 | call to rand_r | a randomly generated number | +| test.cpp:222:54:222:56 | *& ... | test.cpp:221:22:221:42 | call to rand | test.cpp:222:54:222:56 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:221:22:221:42 | call to rand | a randomly generated number | +| test.cpp:228:42:228:43 | *& ... | test.cpp:227:21:227:41 | call to rand | test.cpp:228:42:228:43 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:227:21:227:41 | call to rand | a randomly generated number | +| test.cpp:240:49:240:50 | *& ... | test.cpp:239:21:239:40 | call to operator() | test.cpp:240:49:240:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:239:21:239:40 | call to operator() | a randomly generated number | +| test.cpp:249:49:249:50 | *& ... | test.cpp:248:21:248:40 | call to operator() | test.cpp:249:49:249:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:248:21:248:40 | call to operator() | a randomly generated number | +| test.cpp:258:49:258:50 | *& ... | test.cpp:257:21:257:40 | call to operator() | test.cpp:258:49:258:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:257:21:257:40 | call to operator() | a randomly generated number | +| test.cpp:267:49:267:50 | *& ... | test.cpp:266:21:266:40 | call to operator() | test.cpp:267:49:267:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:266:21:266:40 | call to operator() | a randomly generated number | +| test.cpp:276:49:276:50 | *& ... | test.cpp:275:21:275:40 | call to operator() | test.cpp:276:49:276:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:275:21:275:40 | call to operator() | a randomly generated number | +| test.cpp:285:49:285:50 | *& ... | test.cpp:284:21:284:40 | call to operator() | test.cpp:285:49:285:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:284:21:284:40 | call to operator() | a randomly generated number | +| test.cpp:298:49:298:50 | *& ... | test.cpp:297:21:297:22 | RAND_pseudo_bytes output argument | test.cpp:298:49:298:50 | *& ... | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:297:21:297:22 | RAND_pseudo_bytes output argument | a randomly generated number | +| test.cpp:400:42:400:44 | *key | test.cpp:399:13:399:15 | getrandom output argument | test.cpp:400:42:400:44 | *key | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:399:13:399:15 | getrandom output argument | a randomly generated number | +| test.cpp:402:42:402:44 | *key | test.cpp:401:13:401:15 | getrandom output argument | test.cpp:402:42:402:44 | *key | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:401:13:401:15 | getrandom output argument | a randomly generated number | +| test.cpp:411:42:411:44 | *key | test.cpp:410:21:410:23 | RAND_pseudo_bytes output argument | test.cpp:411:42:411:44 | *key | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:410:21:410:23 | RAND_pseudo_bytes output argument | a randomly generated number | +| test.cpp:440:42:440:44 | *key | test.cpp:439:12:439:32 | call to rand | test.cpp:440:42:440:44 | *key | This security-sensitive value depends on $@, which is not cryptographically secure. | test.cpp:439:12:439:32 | call to rand | a randomly generated number | +edges +| test.cpp:133:21:133:41 | call to rand | test.cpp:133:21:133:41 | call to rand | provenance | | +| test.cpp:133:21:133:41 | call to rand | test.cpp:134:49:134:50 | *& ... | provenance | | +| test.cpp:141:21:141:46 | call to rand | test.cpp:141:21:141:46 | call to rand | provenance | | +| test.cpp:141:21:141:46 | call to rand | test.cpp:142:49:142:50 | *& ... | provenance | | +| test.cpp:149:21:149:43 | call to random | test.cpp:149:21:149:43 | call to random | provenance | | +| test.cpp:149:21:149:43 | call to random | test.cpp:150:49:150:50 | *& ... | provenance | | +| test.cpp:157:21:157:44 | call to drand48 | test.cpp:157:21:157:44 | call to drand48 | provenance | | +| test.cpp:157:21:157:44 | call to drand48 | test.cpp:158:49:158:50 | *& ... | provenance | | +| test.cpp:166:21:166:45 | call to erand48 | test.cpp:166:21:166:45 | call to erand48 | provenance | | +| test.cpp:166:21:166:45 | call to erand48 | test.cpp:167:49:167:50 | *& ... | provenance | | +| test.cpp:174:21:174:44 | call to lrand48 | test.cpp:174:21:174:44 | call to lrand48 | provenance | | +| test.cpp:174:21:174:44 | call to lrand48 | test.cpp:175:49:175:50 | *& ... | provenance | | +| test.cpp:183:21:183:45 | call to nrand48 | test.cpp:183:21:183:45 | call to nrand48 | provenance | | +| test.cpp:183:21:183:45 | call to nrand48 | test.cpp:184:49:184:50 | *& ... | provenance | | +| test.cpp:191:21:191:44 | call to mrand48 | test.cpp:191:21:191:44 | call to mrand48 | provenance | | +| test.cpp:191:21:191:44 | call to mrand48 | test.cpp:192:49:192:50 | *& ... | provenance | | +| test.cpp:200:21:200:45 | call to jrand48 | test.cpp:200:21:200:45 | call to jrand48 | provenance | | +| test.cpp:200:21:200:45 | call to jrand48 | test.cpp:201:49:201:50 | *& ... | provenance | | +| test.cpp:209:21:209:48 | call to rand_r | test.cpp:209:21:209:48 | call to rand_r | provenance | | +| test.cpp:209:21:209:48 | call to rand_r | test.cpp:210:49:210:50 | *& ... | provenance | | +| test.cpp:221:22:221:42 | call to rand | test.cpp:221:22:221:42 | call to rand | provenance | | +| test.cpp:221:22:221:42 | call to rand | test.cpp:222:54:222:56 | *& ... | provenance | | +| test.cpp:227:21:227:41 | call to rand | test.cpp:227:21:227:41 | call to rand | provenance | | +| test.cpp:227:21:227:41 | call to rand | test.cpp:228:42:228:43 | *& ... | provenance | | +| test.cpp:239:21:239:40 | call to operator() | test.cpp:239:21:239:40 | call to operator() | provenance | | +| test.cpp:239:21:239:40 | call to operator() | test.cpp:240:49:240:50 | *& ... | provenance | | +| test.cpp:248:21:248:40 | call to operator() | test.cpp:248:21:248:40 | call to operator() | provenance | | +| test.cpp:248:21:248:40 | call to operator() | test.cpp:249:49:249:50 | *& ... | provenance | | +| test.cpp:257:21:257:40 | call to operator() | test.cpp:257:21:257:40 | call to operator() | provenance | | +| test.cpp:257:21:257:40 | call to operator() | test.cpp:258:49:258:50 | *& ... | provenance | | +| test.cpp:266:21:266:40 | call to operator() | test.cpp:266:21:266:40 | call to operator() | provenance | | +| test.cpp:266:21:266:40 | call to operator() | test.cpp:267:49:267:50 | *& ... | provenance | | +| test.cpp:275:21:275:40 | call to operator() | test.cpp:275:21:275:40 | call to operator() | provenance | | +| test.cpp:275:21:275:40 | call to operator() | test.cpp:276:49:276:50 | *& ... | provenance | | +| test.cpp:284:21:284:40 | call to operator() | test.cpp:284:21:284:40 | call to operator() | provenance | | +| test.cpp:284:21:284:40 | call to operator() | test.cpp:285:49:285:50 | *& ... | provenance | | +| test.cpp:297:21:297:22 | RAND_pseudo_bytes output argument | test.cpp:298:49:298:50 | *& ... | provenance | | +| test.cpp:399:13:399:15 | getrandom output argument | test.cpp:400:42:400:44 | *key | provenance | | +| test.cpp:401:13:401:15 | getrandom output argument | test.cpp:402:42:402:44 | *key | provenance | | +| test.cpp:410:21:410:23 | RAND_pseudo_bytes output argument | test.cpp:411:42:411:44 | *key | provenance | | +| test.cpp:439:3:439:32 | ... = ... | test.cpp:440:42:440:44 | *key | provenance | | +| test.cpp:439:12:439:32 | call to rand | test.cpp:439:3:439:32 | ... = ... | provenance | | +nodes +| test.cpp:133:21:133:41 | call to rand | semmle.label | call to rand | +| test.cpp:133:21:133:41 | call to rand | semmle.label | call to rand | +| test.cpp:134:49:134:50 | *& ... | semmle.label | *& ... | +| test.cpp:141:21:141:46 | call to rand | semmle.label | call to rand | +| test.cpp:141:21:141:46 | call to rand | semmle.label | call to rand | +| test.cpp:142:49:142:50 | *& ... | semmle.label | *& ... | +| test.cpp:149:21:149:43 | call to random | semmle.label | call to random | +| test.cpp:149:21:149:43 | call to random | semmle.label | call to random | +| test.cpp:150:49:150:50 | *& ... | semmle.label | *& ... | +| test.cpp:157:21:157:44 | call to drand48 | semmle.label | call to drand48 | +| test.cpp:157:21:157:44 | call to drand48 | semmle.label | call to drand48 | +| test.cpp:158:49:158:50 | *& ... | semmle.label | *& ... | +| test.cpp:166:21:166:45 | call to erand48 | semmle.label | call to erand48 | +| test.cpp:166:21:166:45 | call to erand48 | semmle.label | call to erand48 | +| test.cpp:167:49:167:50 | *& ... | semmle.label | *& ... | +| test.cpp:174:21:174:44 | call to lrand48 | semmle.label | call to lrand48 | +| test.cpp:174:21:174:44 | call to lrand48 | semmle.label | call to lrand48 | +| test.cpp:175:49:175:50 | *& ... | semmle.label | *& ... | +| test.cpp:183:21:183:45 | call to nrand48 | semmle.label | call to nrand48 | +| test.cpp:183:21:183:45 | call to nrand48 | semmle.label | call to nrand48 | +| test.cpp:184:49:184:50 | *& ... | semmle.label | *& ... | +| test.cpp:191:21:191:44 | call to mrand48 | semmle.label | call to mrand48 | +| test.cpp:191:21:191:44 | call to mrand48 | semmle.label | call to mrand48 | +| test.cpp:192:49:192:50 | *& ... | semmle.label | *& ... | +| test.cpp:200:21:200:45 | call to jrand48 | semmle.label | call to jrand48 | +| test.cpp:200:21:200:45 | call to jrand48 | semmle.label | call to jrand48 | +| test.cpp:201:49:201:50 | *& ... | semmle.label | *& ... | +| test.cpp:209:21:209:48 | call to rand_r | semmle.label | call to rand_r | +| test.cpp:209:21:209:48 | call to rand_r | semmle.label | call to rand_r | +| test.cpp:210:49:210:50 | *& ... | semmle.label | *& ... | +| test.cpp:221:22:221:42 | call to rand | semmle.label | call to rand | +| test.cpp:221:22:221:42 | call to rand | semmle.label | call to rand | +| test.cpp:222:54:222:56 | *& ... | semmle.label | *& ... | +| test.cpp:227:21:227:41 | call to rand | semmle.label | call to rand | +| test.cpp:227:21:227:41 | call to rand | semmle.label | call to rand | +| test.cpp:228:42:228:43 | *& ... | semmle.label | *& ... | +| test.cpp:239:21:239:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:239:21:239:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:240:49:240:50 | *& ... | semmle.label | *& ... | +| test.cpp:248:21:248:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:248:21:248:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:249:49:249:50 | *& ... | semmle.label | *& ... | +| test.cpp:257:21:257:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:257:21:257:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:258:49:258:50 | *& ... | semmle.label | *& ... | +| test.cpp:266:21:266:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:266:21:266:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:267:49:267:50 | *& ... | semmle.label | *& ... | +| test.cpp:275:21:275:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:275:21:275:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:276:49:276:50 | *& ... | semmle.label | *& ... | +| test.cpp:284:21:284:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:284:21:284:40 | call to operator() | semmle.label | call to operator() | +| test.cpp:285:49:285:50 | *& ... | semmle.label | *& ... | +| test.cpp:297:21:297:22 | RAND_pseudo_bytes output argument | semmle.label | RAND_pseudo_bytes output argument | +| test.cpp:298:49:298:50 | *& ... | semmle.label | *& ... | +| test.cpp:399:13:399:15 | getrandom output argument | semmle.label | getrandom output argument | +| test.cpp:400:42:400:44 | *key | semmle.label | *key | +| test.cpp:401:13:401:15 | getrandom output argument | semmle.label | getrandom output argument | +| test.cpp:402:42:402:44 | *key | semmle.label | *key | +| test.cpp:410:21:410:23 | RAND_pseudo_bytes output argument | semmle.label | RAND_pseudo_bytes output argument | +| test.cpp:411:42:411:44 | *key | semmle.label | *key | +| test.cpp:439:3:439:32 | ... = ... | semmle.label | ... = ... | +| test.cpp:439:12:439:32 | call to rand | semmle.label | call to rand | +| test.cpp:440:42:440:44 | *key | semmle.label | *key | +subpaths diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlref b/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlref new file mode 100644 index 000000000000..6ebdd1d7668f --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlref @@ -0,0 +1,2 @@ +query: Security/CWE/CWE-330/InsecureRandomness.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/openssl.h b/cpp/ql/test/query-tests/Security/CWE/CWE-330/openssl.h new file mode 100644 index 000000000000..a39e950291f9 --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/openssl.h @@ -0,0 +1,17 @@ +// OpenSSL declarations used by the randomness query tests. +struct EVP_CIPHER_CTX; +struct EVP_CIPHER; +struct EVP_PKEY; +struct ENGINE; +enum { EVP_PKEY_HMAC = 855 }; + +EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(); +const EVP_CIPHER *EVP_aes_128_cbc(); +int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, ENGINE *, + const unsigned char *key, const unsigned char *iv); +int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *, const unsigned char *, int); +int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *); +EVP_PKEY *EVP_PKEY_new_mac_key(int, ENGINE *, const unsigned char *, int); +int RAND_bytes(unsigned char *, int); +int RAND_priv_bytes(unsigned char *, int); +int RAND_pseudo_bytes(unsigned char *, int); diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cpp b/cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cpp new file mode 100644 index 000000000000..06e55096163f --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cpp @@ -0,0 +1,441 @@ +#include "openssl.h" + +// Standalone declarations keep the fixtures independent of system headers. + +typedef unsigned long size_t; + +// --- C standard library / POSIX / BSD random number generators (declarations) --- +extern "C" { +// Not cryptographically secure. +int rand(void); +long random(void); +double drand48(void); +double erand48(unsigned short xsubi[3]); +long lrand48(void); +long nrand48(unsigned short xsubi[3]); +long mrand48(void); +long jrand48(unsigned short xsubi[3]); +int rand_r(unsigned int *seedp); +// Cryptographically secure. +unsigned int arc4random(void); +unsigned int arc4random_uniform(unsigned int upper_bound); +void arc4random_buf(void *buf, size_t nbytes); +int getrandom(void *buf, size_t buflen, unsigned int flags); +int getentropy(void *buffer, size_t length); +} + +// --- Windows CryptoAPI / CNG (cryptographically secure, simplified signatures) --- +typedef unsigned long DWORD; +typedef int BOOL; +typedef unsigned char BYTE; +typedef void *HCRYPTPROV; +typedef long NTSTATUS; +typedef void *BCRYPT_ALG_HANDLE; +typedef unsigned long ULONG; +typedef unsigned char BOOLEAN; +BOOLEAN RtlGenRandom(void *RandomBuffer, ULONG RandomBufferLength); +BOOL CryptGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer); +NTSTATUS BCryptGenRandom(BCRYPT_ALG_HANDLE hAlgorithm, unsigned char *pbBuffer, ULONG cbBuffer, + ULONG dwFlags); + +// --- C++ engines (faithful subset of the standard library) --- +namespace std { +int rand(void); + +template +class linear_congruential_engine { +public: + typedef UIntType result_type; + linear_congruential_engine(); + result_type operator()(); +}; + +template +class mersenne_twister_engine { +public: + typedef UIntType result_type; + mersenne_twister_engine(); + result_type operator()(); +}; + +template +class subtract_with_carry_engine { +public: + typedef UIntType result_type; + subtract_with_carry_engine(); + result_type operator()(); +}; + +template +class discard_block_engine { +public: + typedef typename Engine::result_type result_type; + discard_block_engine(); + result_type operator()(); +}; + +template +class shuffle_order_engine { +public: + typedef typename Engine::result_type result_type; + shuffle_order_engine(); + result_type operator()(); +}; + +template +class independent_bits_engine { +public: + typedef UIntType result_type; + independent_bits_engine(); + result_type operator()(); +}; + +typedef mersenne_twister_engine mt19937; +typedef mersenne_twister_engine mt19937_64; +typedef linear_congruential_engine minstd_rand0; +typedef linear_congruential_engine minstd_rand; +typedef subtract_with_carry_engine ranlux24_base; +typedef subtract_with_carry_engine ranlux48_base; +typedef discard_block_engine ranlux24; +typedef discard_block_engine ranlux48; +typedef shuffle_order_engine knuth_b; +typedef minstd_rand0 default_random_engine; + +class random_device { +public: + typedef unsigned int result_type; + random_device(); + result_type operator()(); +}; +} + +// Run an OpenSSL cipher operation so the key/IV arguments of `EVP_EncryptInit_ex` +// become key/nonce consumers. `finish` provides the (dominated) final step. +static void finish(EVP_CIPHER_CTX *ctx) { + unsigned char in[16] = {0}; + unsigned char out[64]; + int outlen = 0; + EVP_EncryptUpdate(ctx, out, &outlen, in, 16); + EVP_EncryptFinal_ex(ctx, out, &outlen); +} + +// =========================================================================== +// Insecure generators feeding a cipher key (positive). +// =========================================================================== + +void test_rand() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)rand(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_std_rand() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)std::rand(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_random() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)random(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_drand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)drand48(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_erand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned short s[3] = {0}; + unsigned char k = (unsigned char)erand48(s); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_lrand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)lrand48(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_nrand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned short s[3] = {0}; + unsigned char k = (unsigned char)nrand48(s); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_mrand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)mrand48(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_jrand48() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned short s[3] = {0}; + unsigned char k = (unsigned char)jrand48(s); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_rand_r() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned int seed = 0; + unsigned char k = (unsigned char)rand_r(&seed); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +// =========================================================================== +// Insecure output reaching an IV (nonce) sink and a raw MAC key sink (positive). +// =========================================================================== + +void test_rand_iv() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char key[16] = {0}; + unsigned char iv = (unsigned char)rand(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, key, &iv); // $ Alert + finish(ctx); +} + +void test_rand_mac_key() { + unsigned char k = (unsigned char)rand(); // $ Source + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, &k, 16); // $ Alert +} + +// =========================================================================== +// Insecure C++ engines feeding a cipher key (positive). +// =========================================================================== + +void test_mt19937() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::mt19937 gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_minstd_rand() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::minstd_rand gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_ranlux24() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::ranlux24 gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_knuth_b() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::knuth_b gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_independent_bits_engine() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::independent_bits_engine gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_default_random_engine() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::default_random_engine gen; + unsigned char k = (unsigned char)gen(); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +// =========================================================================== +// RAND_pseudo_bytes is insecure (positive); RAND_bytes is secure (negative). +// =========================================================================== + +void test_rand_pseudo_bytes() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + RAND_pseudo_bytes(&k, 1); // $ Source + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); // $ Alert + finish(ctx); +} + +void test_rand_bytes() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + RAND_bytes(&k, 1); // GOOD: cryptographically secure + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +// =========================================================================== +// Secure generators must NOT be flagged (negative). +// =========================================================================== + +void test_arc4random() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)arc4random(); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_arc4random_uniform() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = (unsigned char)arc4random_uniform(256); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_arc4random_buf() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + arc4random_buf(&k, 1); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_getrandom() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + getrandom(&k, 1, 0); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_getentropy() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + getentropy(&k, 1); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_rtlgenrandom() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + RtlGenRandom(&k, 1); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_cryptgenrandom() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + CryptGenRandom(0, 1, &k); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_bcryptgenrandom() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + unsigned char k = 0; + BCryptGenRandom(0, &k, 1, 0); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +void test_random_device() { + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + unsigned char iv[16] = {0}; + std::random_device rd; + unsigned char k = (unsigned char)rd(); // GOOD + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), 0, &k, iv); + finish(ctx); +} + +#define GRND_NONBLOCK 0x0001 +#define GRND_INSECURE 0x0004 + +void test_getrandom_flags(unsigned flags) { + unsigned char key[16]; + getrandom(key, sizeof(key), GRND_INSECURE); // $ Source + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); // $ Alert + getrandom(key, sizeof(key), GRND_INSECURE | GRND_NONBLOCK); // $ Source + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); // $ Alert + getrandom(key, sizeof(key), GRND_NONBLOCK); + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); + getrandom(key, sizeof(key), flags); // Unknown security classification. +} + +void test_overwrite() { + unsigned char key[16]; + RAND_pseudo_bytes(key, sizeof(key)); // $ Source + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); // $ Alert + if (RAND_bytes(key, sizeof(key)) == 1) + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); + if (RAND_priv_bytes(key, sizeof(key)) == 1) + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); +} + +namespace other { +int rand(); +struct independent_bits_engine { int operator()(); }; +} + +void test_lookalikes(other::independent_bits_engine gen) { + unsigned char key = other::rand(); + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, &key, sizeof(key)); + key = gen(); + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, &key, sizeof(key)); +} + +// A body-visible secure generator exercises flow into the modeled return node. +unsigned std::random_device::operator()() { + return arc4random() ^ static_cast(rand()); +} + +void test_rewrite() { + unsigned char key[16]; + if (RAND_bytes(key, sizeof(key)) != 1) + return; + key[0] = (unsigned char)rand(); // $ Source + EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, 0, key, sizeof(key)); // $ Alert +} diff --git a/shared/quantum/change-notes/2026-08-26-random-security-classification.md b/shared/quantum/change-notes/2026-08-26-random-security-classification.md new file mode 100644 index 000000000000..47334d374134 --- /dev/null +++ b/shared/quantum/change-notes/2026-08-26-random-security-classification.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Added a `isCryptographicallySecure()` predicate to `Crypto::RandomNumberGenerationInstance`, allowing models of random number generators to record whether the generator is cryptographically secure. It defaults to holding for no generator, so existing subclasses are unaffected until they classify themselves. diff --git a/shared/quantum/codeql/quantum/experimental/Model.qll b/shared/quantum/codeql/quantum/experimental/Model.qll index f5ab5190e1ff..37e2a4457f82 100644 --- a/shared/quantum/codeql/quantum/experimental/Model.qll +++ b/shared/quantum/codeql/quantum/experimental/Model.qll @@ -457,6 +457,16 @@ module CryptographyBase Input> { */ abstract class RandomNumberGenerationInstance extends OutputArtifactInstance { abstract string getGeneratorName(); + + /** + * Holds if this generator is cryptographically secure, i.e., suitable for + * generating security-sensitive values such as keys, IVs, nonces, and salts. + * + * Defaults to holding for no generator (i.e., "not known to be secure"), so that + * a generator is only treated as secure when a model explicitly classifies it as + * such. Models of insecure generators (e.g. libc `rand`) leave this as-is. + */ + predicate isCryptographicallySecure() { none() } } /**