Skip to content

Commit

Permalink
Modify rule S2245: Clarify the naming of random number generators (#4446
Browse files Browse the repository at this point in the history
)

* Clarify the naming of random number generators
  • Loading branch information
daniel-teuchert-sonarsource authored Oct 29, 2024
1 parent 57b08f6 commit a3028b8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 67 deletions.
2 changes: 1 addition & 1 deletion rules/S2245/ask-yourself.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
== Ask Yourself Whether

* the code using the generated value requires it to be unpredictable. It is the case for all encryption mechanisms or when a secret value, such as a password, is hashed.
* the function you use generates a value which can be predicted (pseudo-random).
* the function you use is a non-cryptographic PRNG.
* the generated value is used multiple times.
* an attacker can access the generated value.
Expand Down
19 changes: 6 additions & 13 deletions rules/S2245/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:

* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.

As the functions rely on a pseudorandom number generator, they should not be used for security-critical applications or for protecting sensitive data.
include::../description.adoc[]

include::../ask-yourself.adoc[]

== Recommended Secure Coding Practices

* Use functions which rely on a strong random number generator such as ``++randombytes_uniform()++`` or ``++randombytes_buf()++`` from ``++libsodium++``, or ``++randomize()++`` from Botan.
* Use functions which rely on a cryptographically secure pseudorandom number generator (CSPRNG) such as ``++randombytes_uniform()++`` or ``++randombytes_buf()++`` from ``++libsodium++``, or ``++randomize()++`` from Botan.
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.

Expand All @@ -37,17 +29,18 @@ void f() {
void f() {
char random_chars[10];
randombytes_buf(random_chars, 10); // Compliant
uint32_t random_int = randombytes_uniform(10); // Compliant
randombytes_buf(random_chars, 10);
uint32_t random_int = randombytes_uniform(10);
uint8_t random_chars[10];
Botan::System_RNG system;
system.randomize(random_chars, 10); // Compliant
system.randomize(random_chars, 10);
}
----

== See

* OWASP - https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#secure-random-number-generation[Secure Random Number Generation Cheat Sheet]
* OWASP - https://owasp.org/Top10/A02_2021-Cryptographic_Failures/[Top 10 2021 Category A2 - Cryptographic Failures]
* OWASP - https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure[Top 10 2017 Category A3 - Sensitive Data Exposure]
* OWASP - https://mas.owasp.org/checklists/MASVS-CRYPTO/[Mobile AppSec Verification Standard - Cryptography Requirements]
Expand Down
13 changes: 3 additions & 10 deletions rules/S2245/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:
include::../description.adoc[]

* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.


As the ``++System.Random++`` class relies on a pseudorandom number generator, it should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++System.Cryptography.RandomNumberGenerator++`` class which relies on a cryptographically strong random number generator (RNG) should be used in place.
As the ``++System.Random++`` class relies on a non-cryptographic pseudorandom number generator, it should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++System.Cryptography.RandomNumberGenerator++`` class which relies on a CSPRNG should be used in place.

include::../ask-yourself.adoc[]

Expand All @@ -28,7 +21,7 @@ return BitConverter.ToString(data); // Check if this value is used for hashing o
----
using System.Security.Cryptography;
...
var randomGenerator = RandomNumberGenerator.Create(); // Compliant for security-sensitive use cases
var randomGenerator = RandomNumberGenerator.Create();
byte[] data = new byte[16];
randomGenerator.GetBytes(data);
return BitConverter.ToString(data);
Expand Down
8 changes: 6 additions & 2 deletions rules/S2245/description.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:
PRNGs are algorithms that produce sequences of numbers that only approximate true randomness. While they are suitable for applications like simulations or modeling, they are not appropriate for security-sensitive contexts because their outputs can be predictable if the internal state is known.

In contrast, cryptographically secure pseudorandom number generators (CSPRNGs) are designed to be secure against prediction attacks. CSPRNGs use cryptographic algorithms to ensure that the generated sequences are not only random but also unpredictable, even if part of the sequence or the internal state becomes known. This unpredictability is crucial for security-related tasks such as generating encryption keys, tokens, or any other values that must remain confidential and resistant to guessing attacks.

For example, the use of non-cryptographic PRNGs has led to vulnerabilities such as:

* https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information. Therefore, it is critical to use CSPRNGs in any security-sensitive application to ensure the robustness and security of the system.
4 changes: 2 additions & 2 deletions rules/S2245/go/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ num := rand.Intn(100) // Sensitive
import "crypto/rand"
a := make([]byte, 10)
_, err := rand.Read(a) // Compliant
_, err := rand.Read(a)
if err != nil {
panic(err)
}
Expand All @@ -40,7 +40,7 @@ if err != nil {
----
import "crypto/rand"
temp, err := rand.Int(rand.Reader, big.NewInt(100)) // Compliant
temp, err := rand.Int(rand.Reader, big.NewInt(100))
if err != nil {
panic(err)
}
Expand Down
18 changes: 5 additions & 13 deletions rules/S2245/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:
include::../description.adoc[]

* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.


As the ``++java.util.Random++`` class relies on a pseudorandom number generator, this class and relating ``++java.lang.Math.random()++`` method should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++java.security.SecureRandom++`` class which relies on a cryptographically strong random number generator (RNG) should be used in place.
As the ``++java.util.Random++`` class relies on a non-cryptographic pseudorandom number generator, this class and relating ``++java.lang.Math.random()++`` method should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++java.security.SecureRandom++`` class which relies on a CSPRNG should be used in place.

include::../ask-yourself.adoc[]

== Recommended Secure Coding Practices

* Use a cryptographically strong random number generator (RNG) like "java.security.SecureRandom" in place of this PRNG.
* Use a cryptographically secure pseudo random number generator (CSPRNG) like "java.security.SecureRandom" in place of a non-cryptographic PRNG.
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.

Expand All @@ -29,13 +22,14 @@ random.nextBytes(bytes); // Check if bytes is used for hashing, encryption, etc.

[source,java]
----
SecureRandom random = new SecureRandom(); // Compliant for security-sensitive use cases
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
----

== See

* OWASP - https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#secure-random-number-generation[Secure Random Number Generation Cheat Sheet]
* OWASP - https://owasp.org/Top10/A02_2021-Cryptographic_Failures/[Top 10 2021 Category A2 - Cryptographic Failures]
* OWASP - https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure[Top 10 2017 Category A3 - Sensitive Data Exposure]
* OWASP - https://mas.owasp.org/checklists/MASVS-CRYPTO/[Mobile AppSec Verification Standard - Cryptography Requirements]
Expand All @@ -45,8 +39,6 @@ random.nextBytes(bytes);
* CWE - https://cwe.mitre.org/data/definitions/326[CWE-326 - Inadequate Encryption Strength]
* CWE - https://cwe.mitre.org/data/definitions/1241[CWE-1241 - Use of Predictable Algorithm in Random Number Generator]
* https://wiki.sei.cmu.edu/confluence/x/oTdGBQ[CERT, MSC02-J.] - Generate strong random numbers
* https://wiki.sei.cmu.edu/confluence/x/UNcxBQ[CERT, MSC30-C.] - Do not use the rand() function for generating pseudorandom numbers
* https://wiki.sei.cmu.edu/confluence/x/2ns-BQ[CERT, MSC50-CPP.] - Do not use std::rand() for generating pseudorandom numbers
* Derived from FindSecBugs rule https://h3xstream.github.io/find-sec-bugs/bugs.htm#PREDICTABLE_RANDOM[Predictable Pseudo Random Number Generator]

ifdef::env-github,rspecator-view[]
Expand Down
15 changes: 4 additions & 11 deletions rules/S2245/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:

* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.

include::../description.adoc[]

As the ``++Math.random()++`` function relies on a weak pseudorandom number generator, this function should not be used for security-critical applications or for protecting sensitive data. In such context, a cryptographically strong pseudorandom number generator (CSPRNG) should be used instead.

include::../ask-yourself.adoc[]

== Recommended Secure Coding Practices

* Use a cryptographically strong pseudorandom number generator (CSPRNG) like ``++crypto.getRandomValues()++``.
* Use a cryptographically secure pseudorandom number generator (CSPRNG) like ``++crypto.getRandomValues()++``.
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.

Expand All @@ -31,11 +24,11 @@ const val = Math.random(); // Sensitive
// === Client side ===
const crypto = window.crypto || window.msCrypto;
var array = new Uint32Array(1);
crypto.getRandomValues(array); // Compliant for security-sensitive use cases
crypto.getRandomValues(array);
// === Server side ===
const crypto = require('crypto');
const buf = crypto.randomBytes(1); // Compliant for security-sensitive use cases
const buf = crypto.randomBytes(1);
----

include::../see.adoc[]
Expand Down
2 changes: 1 addition & 1 deletion rules/S2245/kotlin/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ random.nextBytes(bytes)

[source,kotlin]
----
val random = SecureRandom() // Compliant
val random = SecureRandom()
val bytes = ByteArray(20)
random.nextBytes(bytes)
----
Expand Down
20 changes: 7 additions & 13 deletions rules/S2245/php/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:
include::../description.adoc[]

* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.

As the ``++rand()++`` and ``++mt_rand()++`` functions rely on a pseudorandom number generator, it should not be used for security-critical applications or for protecting sensitive data.
As the ``++rand()++`` and ``++mt_rand()++`` functions are no CSPRNGs, they should not be used for security-critical applications or for protecting sensitive data.

include::../ask-yourself.adoc[]

== Recommended Secure Coding Practices

* Use functions which rely on a cryptographically strong random number generator such as ``++random_int()++`` or ``++random_bytes()++`` or ``++openssl_random_pseudo_bytes()++``
* When using ``++openssl_random_pseudo_bytes()++``, provide and check the ``++crypto_strong++`` parameter
* Use functions which rely on a cryptographically secure pseudo random number generator (CSPRNG) such as ``++random_int()++`` or ``++random_bytes()++`` or ``++openssl_random_pseudo_bytes()++``.
* When using ``++openssl_random_pseudo_bytes()++``, provide and check the ``++crypto_strong++`` parameter.
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.

== Sensitive Code Example

----
$random = rand();
$random2 = mt_rand(0, 99);
$random = rand(); // Sensitive
$random2 = mt_rand(0, 99); // Sensitive
----

== Compliant Solution

[source,php]
----
$randomInt = random_int(0,99); // Compliant; generates a cryptographically secure random integer
$randomInt = random_int(0,99);
----

include::../see.adoc[]
Expand Down
3 changes: 2 additions & 1 deletion rules/S2245/see.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
== See

* OWASP - https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#secure-random-number-generation[Secure Random Number Generation Cheat Sheet]
* OWASP - https://owasp.org/Top10/A02_2021-Cryptographic_Failures/[Top 10 2021 Category A2 - Cryptographic Failures]
* OWASP - https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure[Top 10 2017 Category A3 - Sensitive Data Exposure]
* https://mas.owasp.org/checklists/MASVS-CRYPTO/[Mobile AppSec Verification Standard - Cryptography Requirements]
* OWASP - https://mas.owasp.org/checklists/MASVS-CRYPTO/[Mobile AppSec Verification Standard - Cryptography Requirements]
* OWASP - https://owasp.org/www-project-mobile-top-10/2016-risks/m5-insufficient-cryptography[Mobile Top 10 2016 Category M5 - Insufficient Cryptography]
* CWE - https://cwe.mitre.org/data/definitions/338[CWE-338 - Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)]
* CWE - https://cwe.mitre.org/data/definitions/330[CWE-330 - Use of Insufficiently Random Values]
Expand Down

0 comments on commit a3028b8

Please sign in to comment.