Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A pseudorandom number generator (PRNG) is a deterministic algorithm capable of generating sequences of numbers that approximate the properties of random numbers. Each sequence is completely determined by the initial state of the PRNG and the algorithm for changing the state. Most PRNGs make it possible to set the initial state, also called the seed state. Setting the initial state is called seeding the PRNG.

Calling a PRNG in the same initial state, either without seeding it explicitly or by seeding it with the same value, results 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 Consider a PRNG function that is seeded with some initial seed value and is consecutively called 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 . If the PRNG is subsequently seeded with the same initial seed value, then it will generate the same sequence S.

As a result, after the first run of the RNGan improperly seeded PRNG, an attacker will know can predict 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  Improperly seeding or failing to seed the PRNG can lead to vulnerabilities, especially when in 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.

The solution is to ensure that the PRNG is always properly seeded. A properly seeded PRNG will generate a different sequence of random numbers each time it is run.

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 that can be seeded.

Noncompliant Code Example (POSIX)

...

This noncompliant code example generates a sequence of 10 pseudorandom numbers using the randrandom() function. When randrandom() is not seeded, it uses 1 as a default seed. No matter how many times this code is executed, it always produces the same sequencebehaves like rand(), producing the same sequence of random numbers each time any program that uses it is run.

Code Block
bgColor#FFCCCC
langc

int i=0;
for (i=0; i<10; i++#include <stdio.h>
#include <stdlib.h>
 
void func(void) {
  for (unsigned int i = 0; i < 10; ++i) {
    printf("%d, ", rand()); /* Always generates the same sequence */
    printf("%ld, ", random());
  }
}

The output is as follows:

Code Block
:
1st run: 411804289383, 18467846930886, 63341681692777, 265001714636915, 191691957747793, 15724424238335, 11478719885386, 293581649760492, 26962596516649, 24464
         1189641421,
2nd run: 411804289383, 18467846930886, 63341681692777, 265001714636915, 191691957747793, 15724424238335, 11478719885386, 293581649760492, 26962, 24464596516649,
         1189641421,
...
nth run: 411804289383, 18467846930886, 63341681692777, 265001714636915, 191691957747793, 15724424238335, 11478719885386, 293581649760492, 26962, 24464
596516649,
         1189641421,

Compliant Solution (

...

POSIX)

Use srandCall srandom() before randinvoking random() to seed the random sequence generated by randrandom(). The code This compliant solution produces different random number sequences at different calls.each time the function is called, depending on the resolution of the system clock:

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void func(void) {
  struct timespec ts;
  if (timespec_get(&ts, TIME_UTC) == 0) {
   
srand(time(NULL)); /* CreateHandle seederror based*/
 on current} timeelse counted{
 as seconds from 01/01/1970 */
int i=0;
for (i=0; i<10; i++ srandom(ts.tv_nsec ^ ts.tv_sec);
    for (unsigned int i = 0; i < 10; ++i) {
    printf("%d, ", rand()); /* Generates different sequences at different runs */
      printf("%ld, ", random());
    }
  }
}

The output is as follows:

Code Block
:
1st run: 25121198682410, 155712076262355, 29839910374899, 2454428635843, 68442084827500, 101861558698420, 275344459146, 6693733695321, 124562044378618, 57561649046624,
2nd run: 251341127071427, 25796252907983, 29921358798372, 4032101446505, 153341514711759, 25893229790273, 7216954268511, 277521116446419, 12966368192457, 13931
         1297948050,
3rd run: 255032052868434, 279501645663878, 22795731874735, 325821624006793, 1233938447420, 108621046134947, 312431901136083, 24650418123888, 11000, 7328
...

Noncompliant Code Example

This noncompliant code example generates a sequence of 10 pseudorandom numbers using the random() function. When random()}}is not seeded, it behaves like {{rand(), thus produces the same sequence of random numbers at different calls.

Code Block
bgColor#FFCCCC

int i=0;
for (i=0; i<10; i++) {
    printf("%d, ", random()); /* Always generates the same sequence */
}
output:
1st run: 1804289383, 846930886, 1681692777, 1714636915, 1957747793, 424238335, 719885386, 1649760492, 596516649, 1189641421
2nd run: 1804289383, 846930886, 1681692777, 1714636915, 1957747793, 424238335, 719885386, 1649760492, 596516649, 1189641421
...
nth run: 1804289383, 846930886, 1681692777, 1714636915, 1957747793, 424238335, 719885386, 1649760492, 596516649, 1189641421

Compliant Solution (POSIX)

Use srandom() before random() to seed the random sequence generated by random(). The code produces different random number sequences at different calls.

836428296,
         2017467418,

This may not be sufficiently random for concurrent execution, which may lead to correlated generated series in different threads. Depending on the application and the desired level of security, a programmer may choose alternative ways to seed PRNGs. In general, hardware is more capable than software of generating real random numbers (for example, by sampling the thermal noise of a diode).

Compliant Solution (Windows)

The BCryptGenRandom() function does not run the risk of not being properly seeded because its arguments serve as seeders:

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <Windows.h>
#include <Bcrypt.h>
#include <Ntstatus.h>
#include <Wincrypt.h>

void func(void) {
  BCRYPT_ALG_HANDLE hAlgorithm = NULL;
  long rand_buf;
  PUCHAR pbBuffer = (PUCHAR) &rand_buf;
  ULONG cbBuffer = sizeof(rand_buf);
  ULONG dwFlags = BCRYPT_USE_SYSTEM_PREFERRED_RNG;
  NTSTATUS status;
  for (unsigned int i = 0; i < 10; ++i) {
    status = BCryptGenRandom(hAlgorithm, pbBuffer, cbBuffer, dwFlags);
    if (status == STATUS_SUCCESS) {
      printf("%ld, ", rand_buf);
    } else {
      /* Handle Error */
    }
  }
}

The output is as follows:

Code Block
1st run: -683378946, 1957231690, 1933176011, -1745403355, -883473417, 882992405, 169629816, 1824800038, 899851668, 1702784647, 
2nd run: -58750553, -1921870721, -1973269161, 1512649964, -673518452, 234003619, -1622633366, 1312389688, -2125631172, 2067680022, 
3rd run: -189899579, 1220698973, 752205360, -1826365616, 79310867, 1430950090, -283206168, -941773185, 129633665, 543448789, 
Code Block
bgColor#ccccff

srandom(time(NULL)); /* Create seed based on current time counted as seconds from 01/01/1970 */
int i=0;
for (i=0; i<10; i++) {
    printf("%d, ", random()); /* Generates different sequences at different runs */
}

output:
1st run: 198682410, 2076262355, 910374899, 428635843, 2084827500, 1558698420, 4459146, 733695321, 2044378618, 1649046624
2nd run: 1127071427, 252907983, 1358798372, 2101446505, 1514711759, 229790273, 954268511, 1116446419, 368192457, 1297948050
3rd run: 2052868434, 1645663878, 731874735, 1624006793, 938447420, 1046134947, 1901136083, 418123888, 836428296, 2017467418
...

Risk Assessment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC18

MSC32-C

 

Medium

likely

Likely

 

Low

 

P18

 

L1

Automated Detection

...

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported, but no explicit checker
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-MSC32
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

HARDCODED.SEED
MISC.CRYPTO.TIMESEED

Hardcoded Seed in PRNG
Predictable Seed in PRNG

Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-msc32-cFully implemented
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C5031

C++5036


Klocwork
Include Page
Klocwork_V
Klocwork_V

CERT.MSC.SEED_RANDOM


PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

2460, 2461, 2760

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule MSC32-C


Checks for:

  • Deterministic random output from constant seed
  • Predictable random output from predictable seed

Rule fully covered.

Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-MSC32-d

Properly seed pseudorandom number generators

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

...

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

CERT C Secure Coding StandardMSC30-C. Do not use the rand() function for generating pseudorandom numbersPrior to 2018-01-12: CERT: Unspecified Relationship
CERT CMSC51

...

-CPP. Ensure your random number generator is properly seededPrior to 2018-01-12: CERT: Unspecified Relationship
CWE 2.11CWE-327, Use of a Broken or Risky Cryptographic Algorithm2017-05-16: CERT: Rule subset of CWE
CWE 2.

...

11CWE-330, Use of Insufficiently Random Values2017-06-28: CERT: Rule subset of CWE
CWE 2.11CWE-331, Insufficient Entropy2017-06-28: CERT: Exact

CERT-CWE Mapping Notes

Key here for mapping notes

CWE-327 and MSC32-C


  • Intersection( MSC30-C, MSC32-C) = Ø



  • MSC32-C says to properly seed pseudorandom number generators. For example, if you call rand(), make sure to seed it properly by calling srand() first. So far, we haven’t found any calls to rand().



  • Failure to seed a PRNG causes it to produce reproducible (hence insecure) series of random numbers.



  • CWE-327 = Union( MSC32-C, list) where list =



  • Invocation of broken/risky crypto algorithms that are not properly seeded




CWE-330 and MSC32-C

Independent( MSC30-C, MSC32-C, CON33-C)

CWE-330 = Union( MSC30-C, MSC32-C, CON33-C, list) where list = other improper use or creation of random values. (EG the would qualify)

MSC30-C, MSC32-C and CON33-C are independent, they have no intersections. They each specify distinct errors regarding PRNGs.

Bibliography


...

Image Added Image Added Image Added

References

C++Reference