From a3028b8b58022252c7b51d9b38157671193a21f1 Mon Sep 17 00:00:00 2001 From: daniel-teuchert-sonarsource <141642369+daniel-teuchert-sonarsource@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:36:18 +0100 Subject: [PATCH] Modify rule S2245: Clarify the naming of random number generators (#4446) * Clarify the naming of random number generators --- rules/S2245/ask-yourself.adoc | 2 +- rules/S2245/cfamily/rule.adoc | 19 ++++++------------- rules/S2245/csharp/rule.adoc | 13 +++---------- rules/S2245/description.adoc | 8 ++++++-- rules/S2245/go/rule.adoc | 4 ++-- rules/S2245/java/rule.adoc | 18 +++++------------- rules/S2245/javascript/rule.adoc | 15 ++++----------- rules/S2245/kotlin/rule.adoc | 2 +- rules/S2245/php/rule.adoc | 20 +++++++------------- rules/S2245/see.adoc | 3 ++- 10 files changed, 37 insertions(+), 67 deletions(-) diff --git a/rules/S2245/ask-yourself.adoc b/rules/S2245/ask-yourself.adoc index 57e9d5bc9ec..8040e53ab88 100644 --- a/rules/S2245/ask-yourself.adoc +++ b/rules/S2245/ask-yourself.adoc @@ -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. diff --git a/rules/S2245/cfamily/rule.adoc b/rules/S2245/cfamily/rule.adoc index 8a1288cc702..fc71c968cd7 100644 --- a/rules/S2245/cfamily/rule.adoc +++ b/rules/S2245/cfamily/rule.adoc @@ -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. @@ -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] diff --git a/rules/S2245/csharp/rule.adoc b/rules/S2245/csharp/rule.adoc index 1ddea8760c1..38cd69f59b0 100644 --- a/rules/S2245/csharp/rule.adoc +++ b/rules/S2245/csharp/rule.adoc @@ -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[] @@ -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); diff --git a/rules/S2245/description.adoc b/rules/S2245/description.adoc index be5b73c0094..6108c2931bf 100644 --- a/rules/S2245/description.adoc +++ b/rules/S2245/description.adoc @@ -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. diff --git a/rules/S2245/go/rule.adoc b/rules/S2245/go/rule.adoc index 4ad13cd6fa2..c4307764a6f 100644 --- a/rules/S2245/go/rule.adoc +++ b/rules/S2245/go/rule.adoc @@ -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) } @@ -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) } diff --git a/rules/S2245/java/rule.adoc b/rules/S2245/java/rule.adoc index 65966f352cb..7f803eaa030 100644 --- a/rules/S2245/java/rule.adoc +++ b/rules/S2245/java/rule.adoc @@ -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. @@ -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] @@ -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[] diff --git a/rules/S2245/javascript/rule.adoc b/rules/S2245/javascript/rule.adoc index aaab7159e60..455879b7c5c 100644 --- a/rules/S2245/javascript/rule.adoc +++ b/rules/S2245/javascript/rule.adoc @@ -1,11 +1,4 @@ -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. @@ -13,7 +6,7 @@ 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. @@ -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[] diff --git a/rules/S2245/kotlin/rule.adoc b/rules/S2245/kotlin/rule.adoc index 099e5773f3e..5ecb09225d8 100644 --- a/rules/S2245/kotlin/rule.adoc +++ b/rules/S2245/kotlin/rule.adoc @@ -16,7 +16,7 @@ random.nextBytes(bytes) [source,kotlin] ---- -val random = SecureRandom() // Compliant +val random = SecureRandom() val bytes = ByteArray(20) random.nextBytes(bytes) ---- diff --git a/rules/S2245/php/rule.adoc b/rules/S2245/php/rule.adoc index b73f370c842..c87812af9d9 100644 --- a/rules/S2245/php/rule.adoc +++ b/rules/S2245/php/rule.adoc @@ -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[] diff --git a/rules/S2245/see.adoc b/rules/S2245/see.adoc index f725b4ca8ba..de5e28b4ccb 100644 --- a/rules/S2245/see.adoc +++ b/rules/S2245/see.adoc @@ -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]