...
Calling a PRNG in the same initial state, either without seeding it explicitly or by seeding it with the same value, results in generating the same sequence of random numbers in different runs of the program. Consider a PRNG function that is seeded with some initial seed value and is consecutively called to produce a sequence of random numbers, S
. If the PRNG is subsequently seeded with the same initial seed value, then it will generate the same sequence S
.
As a resultConsequently, after the first run of an improperly seeded PRNG, an attacker can predict the sequence of random numbers that will be generated in the future runs. Improperly seeding or failing to seed the PRNG can lead to vulnerabilities, especially in security protocols.
...
Not all random number generators can be seeded. True random number generators that rely on hardware to produce completely unpredictable results do not need to be and cannot be seeded. Some high-quality PRNGs, such as the /dev/random
device on some UNIX systems, also cannot be seeded. This rule applies only to algorithmic pseudorandom number generators PRNGs that can be seeded.
Noncompliant Code Example
...
In this noncompliant code example, the random number generation engine is seeded with the current time. This code is an improvement over the previous noncompliant code example, but is still unsuitable when an attacker can control the time at which the seeding is executed. Predictable seed values can result in exploits when the subverted PRNG is used.
...
This compliant solution uses std::random_device
to generate a random seed value to see seed the Mersenne Twister engine object. The values generated by std::random_device
are nondeterministic random numbers when possible, relying on random number generation devices, like such as /dev/random
. When such a device is not available, std::random_device
may employ a random number engine; however, the initial value generated should have sufficient randomness to serve as a seed value.
...
Using a predictable seed value, such as the current time, result in numerous vulnerabilities, such as the one described by CVE-2008-1637.
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
SEI CERT C Coding Standard | MSC32-C. Properly seed pseudorandom number generators |
MITRE CWE | CWE-327, Use of a Broken or Risky Cryptographic Algorithm |
...
[ISO/IEC 14882-2014] | 26.5, "Random Number Generation" |
[ISO/IEC 9899:2011] | 7.22.2, "Pseudo-random Sequence Generation Functions" |
...