Calling a Random Number Generator (RNG) that is not seeded, will result in generating the same sequence of random numbers in different runs of the program.
Suppose there is a code that calls 10 times an RNG function to produce a sequence of 10 random numbers. Suppose, also, that this RNG is not seeded. Running the code for the first time will produce the sequence S = <r1, r2, r3, r4, r5, r6, r7, r8, r9, r10>. Running the code again for a second time will produce the exact same sequence S. Generally, any subsequent runs of the code will generate the same sequence S.
As a result, after the first run of the RNG, an attacker will know the sequence of random numbers that will be generated in the future runs. Knowing the sequence of random numbers that will be generated beforehand can lead to many vulnerabilities, especially when security protocols are concerned.
As a solution, you should always ensure that your RNG is properly seeded. Seeding an RNG means that it will generate different sequences of random numbers at any call.
Rule MSC30-C. Do not use the rand() function for generating pseudorandom numbers addresses RNGs from a different perspective, i.e. the time till first collision occurs. In other words, during a single run of an RNG, the time interval after which, the RNG generates the same random numbers. The rule MSC30-C deprecates the rand()
function as it generates numbers which have a comparatively short cycle. The same rule proposes the use of random() function for POSIX and CryptGenRandom() for Windows.
The current rule (MSC32-C) examines these three RNGs in terms of seeding. Noncompliant code examples correspond to the use of an RNG without a seed, while compliant solutions correspond to the same RNG being properly seeded. Rule MSC32-C addresses all three RNGs mentioned in rule MSC30-C for completeness. Rule MSC32-C complies to MSC30-C and does not recommend the use of the rand()
function. Nevertheless, if it is unavoidable to use rand()
, at least, it should be properly seeded.
Noncompliant Code Example
This noncompliant code example generates a sequence of 10 pseudorandom numbers using the rand()
function. When rand()
is not seeded, it uses 1 as a default seed. No matter how many times this code is executed, it always produces the same sequence.
Code Block | ||
---|---|---|
| ||
for (int i=0; i<10; i++) { printf("%d\n", rand()); /* Always generates the same sequence */ } output: 1st run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464 2nd run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464 ... nth run: 41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464 |
Compliant Solution (C Standard)
Use srand()
before rand()
to seed the random sequence generated by rand()
. The code produces a different 10 random number sequence at different calls.
Code Block | ||
---|---|---|
| ||
srand(time(NULL)); /* Create seed based on current time counted as seconds from 01/01/1970 */ for (int i=0; i<10; i++) { printf("%d\n", rand()); /* Generates different sequences at different runs */ } output: 1st run: 25121, 15571, 29839, 2454, 6844, 10186, 27534, 6693, 12456, 5756 2nd run: 25134, 25796, 2992, 403, 15334, 25893, 7216, 27752, 12966, 13931 3rd run: 25503, 27950, 22795, 32582, 1233, 10862, 31243, 24650, 11000, 7328 ... |
Risk Assessment
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MSC18-C |
| likely |
|
|
|
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
This recommendation appears in the C++ Secure Coding Standard as MSC32-CPP. Ensure your random number generator is properly seeded.
References
C++Reference