Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ef5dc7

Browse files
committed
add backtracking to find division that end up being rounded
1 parent b6b8387 commit 3ef5dc7

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

javascript/ql/src/Security/CWE-327/BadRandomness.ql

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ DataFlow::Node goodRandom(DataFlow::SourceNode source) {
132132
result = goodRandom(DataFlow::TypeTracker::end(), source)
133133
}
134134

135+
/**
136+
* Gets a node that is passed to a rounding function from `Math`, using type-backtracker `t`.
137+
*/
138+
DataFlow::Node isRounded(DataFlow::TypeBackTracker t) {
139+
t.start() and
140+
result = DataFlow::globalVarRef("Math").getAMemberCall(["round", "floor", "ceil"]).getArgument(0)
141+
or
142+
exists(DataFlow::TypeBackTracker t2 | t2 = t.smallstep(result, isRounded(t2)))
143+
or
144+
InsecureRandomness::isAdditionalTaintStep(result, isRounded(t.continue()))
145+
}
146+
135147
/**
136148
* Gets a node that that produces a biased result from otherwise cryptographically secure random numbers produced by `source`.
137149
*/
@@ -153,10 +165,7 @@ DataFlow::Node badCrypto(string description, DataFlow::SourceNode source) {
153165
goodRandom(source).asExpr() = div.getLeftOperand() and
154166
description = "division and rounding the result" and
155167
not div.getRightOperand() = isPowerOfTwoMinusOne().asExpr() and // division by (2^n)-1 most of the time produces a uniformly random number between 0 and 1.
156-
DataFlow::globalVarRef("Math")
157-
.getAMemberCall(["round", "floor", "ceil"])
158-
.getArgument(0)
159-
.asExpr() = div
168+
div = isRounded(DataFlow::TypeBackTracker::end()).asExpr()
160169
)
161170
or
162171
// modulo - only bad if not by a power of 2 - and the result is not checked for bias

javascript/ql/test/query-tests/Security/CWE-327/BadRandomness.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
| bad-random.js:85:11:85:35 | goodRan ... Random2 | Using addition on a $@ produces biased results. | bad-random.js:84:23:84:38 | secureRandom(10) | cryptographically secure random number |
1414
| bad-random.js:87:16:87:24 | bad + bad | Using addition on a $@ produces biased results. | bad-random.js:83:23:83:38 | secureRandom(10) | cryptographically secure random number |
1515
| bad-random.js:87:16:87:24 | bad + bad | Using addition on a $@ produces biased results. | bad-random.js:84:23:84:38 | secureRandom(10) | cryptographically secure random number |
16+
| bad-random.js:90:29:90:54 | secureR ... / 25.6 | Using division and rounding the result on a $@ produces biased results. | bad-random.js:90:29:90:44 | secureRandom(10) | cryptographically secure random number |
17+
| bad-random.js:96:29:96:58 | crypto. ... ] / 100 | Using division and rounding the result on a $@ produces biased results. | bad-random.js:96:29:96:49 | crypto. ... ytes(1) | cryptographically secure random number |

javascript/ql/test/query-tests/Security/CWE-327/bad-random.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ var bad = goodRandom1 + goodRandom2; // NOT OK
8787
var dontFlag = bad + bad; // OK - the operands have already been flagged - but flagged anyway due to us not detecting that [INCONSISTENCY].
8888

8989
var good = secureRandom(10)[0] / 0xff; // OK - result is not rounded.
90-
var good = Math.ceil(0.5 - (secureRandom(10)[0] / 25.6)); // NOT OK - division generally introduces bias - but not flagged due to not looking through nested arithmetic [INCONSISTENCY].
90+
var good = Math.ceil(0.5 - (secureRandom(10)[0] / 25.6)); // NOT OK - division generally introduces bias - but not flagged due to not looking through nested arithmetic.
9191

9292
var good = (crypto.randomBytes(1)[0] << 8) + crypto.randomBytes(3)[0]; // OK - bit shifts are usually used to construct larger/smaller numbers,
9393

9494
var good = Math.floor(max * (crypto.randomBytes(1)[0] / 0xff)); // OK - division by 0xff (255) gives a uniformly random number between 0 and 1.
9595

96-
var bad = Math.floor(max * (crypto.randomBytes(1)[0] / 100)); // NOT OK - division by 100 gives bias - but not flagged due to not looking through nested arithmetic [INCONSISTENCY].
96+
var bad = Math.floor(max * (crypto.randomBytes(1)[0] / 100)); // NOT OK - division by 100 gives bias - but not flagged due to not looking through nested arithmetic.
9797

9898
var crb = crypto.randomBytes(4);
9999
var cryptoRand = 0x01000000 * crb[0] + 0x00010000 * crb[1] + 0x00000100 * crb[2] + 0x00000001 * crb[3]; // OK - producing a larger number from smaller numbers.

0 commit comments

Comments
 (0)