mirror of
https://github.com/github/codeql.git
synced 2026-01-08 12:10:22 +01:00
66 lines
2.5 KiB
XML
66 lines
2.5 KiB
XML
<!DOCTYPE qhelp PUBLIC
|
|
"-//Semmle//qhelp//EN"
|
|
"qhelp.dtd">
|
|
<qhelp>
|
|
<overview>
|
|
<p>
|
|
Generating secure random numbers can be an important part of creating a
|
|
secure software system. This can be done using APIs that create
|
|
cryptographically secure random numbers.
|
|
</p>
|
|
<p>
|
|
However, using some mathematical operations on these cryptographically
|
|
secure random numbers can create biased results, where some outcomes
|
|
are more likely than others.
|
|
Such biased results can make it easier for an attacker to guess the random
|
|
numbers, and thereby break the security of the software system.
|
|
</p>
|
|
</overview>
|
|
<recommendation>
|
|
<p>
|
|
Be very careful not to introduce bias when performing mathematical operations
|
|
on cryptographically secure random numbers.
|
|
</p>
|
|
<p>
|
|
If possible, avoid performing mathematical operations on cryptographically secure
|
|
random numbers at all, and use a preexisting library instead.
|
|
</p>
|
|
</recommendation>
|
|
<example>
|
|
<p>
|
|
The example below uses the modulo operator to create an array of 10 random digits
|
|
using random bytes as the source for randomness.
|
|
</p>
|
|
<sample src="examples/bad-random.js" />
|
|
<p>
|
|
The random byte is a uniformly random value between 0 and 255, and thus the result
|
|
from using the modulo operator is slightly more likely to be between 0 and 5 than
|
|
between 6 and 9.
|
|
</p>
|
|
<p>
|
|
The issue has been fixed in the code below by using a library that correctly generates
|
|
cryptographically secure random values.
|
|
</p>
|
|
<sample src="examples/bad-random-fixed.js" />
|
|
<p>
|
|
Alternatively, the issue can be fixed by fixing the math in the original code.
|
|
In the code below the random byte is discarded if the value is greater than or equal to 250.
|
|
Thus the modulo operator is used on a uniformly random number between 0 and 249, which
|
|
results in a uniformly random digit between 0 and 9.
|
|
</p>
|
|
<sample src="examples/bad-random-fixed2.js" />
|
|
|
|
</example>
|
|
|
|
|
|
<references>
|
|
<li>Stack Overflow: <a href="https://stackoverflow.com/questions/3956478/understanding-randomness">Understanding “randomness”</a>.</li>
|
|
<li>OWASP: <a href="https://owasp.org/www-community/vulnerabilities/Insecure_Randomness">Insecure Randomness</a>.</li>
|
|
<li>OWASP: <a
|
|
href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#rule---use-strong-approved-authenticated-encryption">Rule
|
|
- Use strong approved cryptographic algorithms</a>.
|
|
</li>
|
|
</references>
|
|
|
|
</qhelp>
|