logo

Database

Rust Hardcoded Rng Seed

Description

This vulnerability detector identifies Rust code that uses hardcoded seeds for random number generators. Using a hardcoded seed makes the random number generation predictable and deterministic, which compromises cryptographic security and makes applications vulnerable to attacks that rely on unpredictable randomness.

Weakness:

034 - Insecure generation of random numbers

Category: Probabilistic Techniques

Detection Strategy

    Scan Rust source code files that import random number generation libraries with specific crate prefixes

    Identify function calls that appear to be key generation or random number generation operations

    Check if the first argument to these RNG functions is a hardcoded seed value rather than a dynamically generated one

    Report a vulnerability when a key generation function uses a static, predictable seed value that could compromise the randomness of generated keys or random numbers

Vulnerable code example

use rand::rngs::StdRng;
use rand::SeedableRng;
use ed25519_dalek::{Signer, SigningKey};

fn main() {
    // Vulnerable: StdRng seeded with hardcoded literal
    let seed: u64 = 987654321;
    let mut rng = StdRng::seed_from_u64(seed);...

✅ Secure code example

use rand::rngs::OsRng;
use ed25519_dalek::{Signer, SigningKey};

fn main() {
    // Safe: Using OsRng for cryptographically secure randomness
    let mut rng = OsRng;
    
    let signing_key = SigningKey::generate(&mut rng); // Cryptographic key from secure RNG...