Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions assets/opengrep_rules/client/cryptography-random-usage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Test cases for cryptography-random-usage rule
#include <cstdlib>
#include <random>
#include "base/rand_util.h"
#include "crypto/random.h"
#include "third_party/boringssl/src/include/openssl/rand.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

class CryptoUsageExamples {
public:
void BadRandomUsage() {
// SHOULD TRIGGER: Weak C-style random functions (insecure)
// ruleid: chromium-cryptography-random-usage
int weak_random1 = rand();
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
srand(time(nullptr));
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] The detected function is not sufficient at generating security-related random numbers,
such as those used in key and nonce creation. Consider using the libsodium library's
randombytes_random function instead. More information on libsodium's random number
generators can be found here: https://libsodium.gitbook.io/doc/generating_random_data.

If FIPS validation is required, consider using OpenSSLs RAND_bytes family of functions after
enabling the FIPS_mode_set.

For more information on OpenSSL random numbers please see:
https://wiki.openssl.org/index.php/Random_Numbers


Source: https://semgrep.dev/r/gitlab.flawfinder.drand48-1.erand48-1.jrand48-1.lcong48-1.lrand48-1.mrand48-1.nrand48-1.random-1.seed48-1.setstate-1.srand-1.strfry-1.srandom-1.g_rand_boolean-1.g_rand_int-1.g_rand_int_range-1.g_rand_double-1.g_rand_double_range-1.g_random_boolean-1.g_random_int-1.g_random_int_range-1.g_random_double-1.g_random_double_range-1


Cc @thypon @kdenhartog

Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
int weak_random2 = random();
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] The detected function is not sufficient at generating security-related random numbers,
such as those used in key and nonce creation. Consider using the libsodium library's
randombytes_random function instead. More information on libsodium's random number
generators can be found here: https://libsodium.gitbook.io/doc/generating_random_data.

If FIPS validation is required, consider using OpenSSLs RAND_bytes family of functions after
enabling the FIPS_mode_set.

For more information on OpenSSL random numbers please see:
https://wiki.openssl.org/index.php/Random_Numbers


Source: https://semgrep.dev/r/gitlab.flawfinder.drand48-1.erand48-1.jrand48-1.lcong48-1.lrand48-1.mrand48-1.nrand48-1.random-1.seed48-1.setstate-1.srand-1.strfry-1.srandom-1.g_rand_boolean-1.g_rand_int-1.g_rand_int_range-1.g_rand_double-1.g_rand_double_range-1.g_random_boolean-1.g_random_int-1.g_random_int_range-1.g_random_double-1.g_random_double_range-1


Cc @thypon @kdenhartog

Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
srandom(12345);
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] The detected function is not sufficient at generating security-related random numbers,
such as those used in key and nonce creation. Consider using the libsodium library's
randombytes_random function instead. More information on libsodium's random number
generators can be found here: https://libsodium.gitbook.io/doc/generating_random_data.

If FIPS validation is required, consider using OpenSSLs RAND_bytes family of functions after
enabling the FIPS_mode_set.

For more information on OpenSSL random numbers please see:
https://wiki.openssl.org/index.php/Random_Numbers


Source: https://semgrep.dev/r/gitlab.flawfinder.drand48-1.erand48-1.jrand48-1.lcong48-1.lrand48-1.mrand48-1.nrand48-1.random-1.seed48-1.setstate-1.srand-1.strfry-1.srandom-1.g_rand_boolean-1.g_rand_int-1.g_rand_int_range-1.g_rand_double-1.g_rand_double_range-1.g_random_boolean-1.g_random_int-1.g_random_int_range-1.g_random_double-1.g_random_double_range-1


Cc @thypon @kdenhartog

Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier


// SHOULD TRIGGER: C++ std random functions (potentially weak)
// ruleid: chromium-cryptography-random-usage
int weak_random3 = std::rand();
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
std::srand(42);
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

}

void ChromiumRandomUsage() {
// SHOULD TRIGGER: Chromium random functions (need security review)
// ruleid: chromium-cryptography-random-usage
int random_int = base::RandInt(1, 100);
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
uint64_t random_uint64 = base::RandUint64();
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier


uint8_t buffer[32];
// ruleid: chromium-cryptography-random-usage
base::RandGenerator(sizeof(buffer));
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

}

void CryptographicUsage() {
// SHOULD TRIGGER: Cryptographic random functions (need security review)
uint8_t crypto_buffer[32];
// ruleid: chromium-cryptography-random-usage
crypto::RandBytes(crypto_buffer, sizeof(crypto_buffer));
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier


// SHOULD TRIGGER: OpenSSL random functions (need security review)
uint8_t ssl_buffer[16];
// ruleid: chromium-cryptography-random-usage
RAND_bytes(ssl_buffer, sizeof(ssl_buffer));
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier


// SHOULD TRIGGER: Encryption/Decryption operations (need security review)
// ruleid: chromium-cryptography-random-usage
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
EVP_EncryptInit(ctx, EVP_aes_256_gcm(), key_, iv_);
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
EVP_DecryptInit(ctx, EVP_aes_256_gcm(), key_, iv_);
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

}

void BadStdRandomUsage() {
// SHOULD TRIGGER: std::random engines/generators are banned per Chromium style guide
// Use base::RandomBitGenerator instead
// ruleid: chromium-cryptography-random-usage
std::random_device rd;
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

// ruleid: chromium-cryptography-random-usage
std::mt19937 gen(rd());
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

std::uniform_int_distribution<> dis(1, 6);
int dice_roll = dis(gen);

// ruleid: chromium-cryptography-random-usage
std::default_random_engine engine;
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

}

void AcceptableUsage() {
// SHOULD NOT TRIGGER: Correct usage with base::RandomBitGenerator
base::RandomBitGenerator rng;
std::uniform_int_distribution<> dis(1, 6);
int dice_roll = dis(rng);

// SHOULD NOT TRIGGER: Hash functions (different security concern)
std::hash<std::string> hasher;
size_t hash = hasher("some string");

// SHOULD NOT TRIGGER: Time-based operations
auto now = std::chrono::steady_clock::now();
auto timestamp = now.time_since_epoch().count();
}

void TestHelperUsage() {
// This would be excluded by test path filter
// ruleid: chromium-cryptography-random-usage
int test_random = rand(); // OK in tests
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[opengrep] Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.


Source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml


Cc @thypon @cdesouza-chromium @fmarier

}

private:
uint8_t key_[32] = {};
uint8_t iv_[16] = {};
};
47 changes: 47 additions & 0 deletions assets/opengrep_rules/client/cryptography-random-usage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
rules:
- id: chromium-cryptography-random-usage
metadata:
author: Andrea Brancaleoni <[email protected]>
references:
- https://github.com/brave/brave-browser/wiki/Security-reviews
- https://chromium.googlesource.com/chromium/src/+/main/docs/security/web-platform-security-guidelines.md
- https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md#engines-and-generators-from-random_banned
source: https://github.com/brave/security-action/blob/main/assets/opengrep_rules/client/cryptography-random-usage.yaml
assignees: |
thypon
cdesouza-chromium
fmarier
category: security
languages: [cpp, c]
message: |
Usage of cryptographic functions or random number generation requires
security review according to Brave security guidelines. Use crypto::RandBytes()
for cryptographic purposes and base::RandomBitGenerator for non-cryptographic
randomness. Do not use engines/generators from <random> - use base::RandomBitGenerator
instead. Weak randomness can lead to serious security vulnerabilities.
severity: WARNING
patterns:
- pattern-either:
- pattern: rand()
- pattern: srand($SEED)
- pattern: random()
- pattern: srandom($SEED)
- pattern: std::rand()
- pattern: std::srand($SEED)
- pattern-regex: std::random_device\s+\w+
- pattern-regex: std::mt19937\s+\w+
- pattern-regex: std::mt19937_64\s+\w+
- pattern-regex: std::minstd_rand\s+\w+
- pattern-regex: std::minstd_rand0\s+\w+
- pattern-regex: std::ranlux24\s+\w+
- pattern-regex: std::ranlux48\s+\w+
- pattern-regex: std::knuth_b\s+\w+
- pattern-regex: std::default_random_engine\s+\w+
- pattern: base::RandInt($MIN, $MAX)
- pattern: base::RandUint64()
- pattern: base::RandGenerator($SIZE)
- pattern: crypto::RandBytes($BUFFER, $SIZE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

there is also RandBytesAsVector and RandBytesAsArray

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there's also an existing presubmit for this (r'/\bstd::(?:' r'linear_congruential_engine|mersenne_twister_engine|' r'subtract_with_carry_engine|discard_block_engine|' r'independent_bits_engine|shuffle_order_engine|' r'minstd_rand0?|mt19937(_64)?|ranlux(24|48)(_base)?|knuth_b|' r'default_random_engine|' r'random_device|' r'seed_seq' r')\b'), ( 'STL random number engines and generators are banned. Use the ', 'helpers in base/rand_util.h instead, e.g. base::RandBytes() or ', 'base::RandomBitGenerator.' '', 'Please reach out to [email protected] if the base APIs are ', 'insufficient for your needs.', ),

- pattern: RAND_bytes($BUFFER, $SIZE)
- pattern: EVP_CIPHER_CTX_new()
- pattern: EVP_EncryptInit($CTX, ...)
- pattern: EVP_DecryptInit($CTX, ...)