Predictable random output from predictable seed
Seeding routine uses a predictable seed making the output predictable
Description
This defect occurs when you use standard random number generator functions with a nonconstant
but predictable seed. Examples of predictable seed generators are
time, gettimeofday, and
getpid.
The checker detects this issue with the following random number generator functions:
C Standard Library functions such as
srand,srandomandinitstateC++ Standard Library functions such as
std::linear_congruential_engine<>::seed()andstd::mersenne_twister_engine<>::seed()(and also the constructors of these class templates)
Risk
When you use predictable seed values for random number generation, your random numbers are also predictable. A hacker can disrupt your program if they know how your program behaves.
Fix
You can use a different function to generate less predictable seeds.
You can also use a different random number generator that does
not require a seed. For example, the Windows® API function rand_s seeds
itself by default. It uses information from the entire system, for
example, system time, thread ids, system counter, and memory clusters.
This information is more random and a user cannot access this information.
Some standard random routines are inherently cryptographically weak, and should not be used for security purposes.
Examples
Result Information
| Group: Security |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: RAND_SEED_PREDICTABLE |
| Impact: Medium |
Version History
Introduced in R2015b
See Also
Deterministic random output from constant seed | Unsafe standard encryption function | Vulnerable pseudo-random number generator | Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)