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

Skip to content

Commit c4f6113

Browse files
committed
include the source of cryptographically random number in alert message
1 parent 7e8fd80 commit c4f6113

2 files changed

Lines changed: 37 additions & 30 deletions

File tree

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,54 @@ private DataFlow::SourceNode randomBufferSource() {
4242
private string prop() { result = DataFlow::PseudoProperties::setElement() }
4343

4444
/**
45-
* Gets a reference to a cryptographically secure random number, type tracked using `t`.
45+
* Gets a reference to a cryptographically secure random number produced by `source` and type tracked using `t`.
4646
*/
47-
private DataFlow::Node goodRandom(DataFlow::TypeTracker t) {
47+
private DataFlow::Node goodRandom(DataFlow::TypeTracker t, DataFlow::SourceNode source) {
4848
t.startInProp(prop()) and
49-
result = randomBufferSource()
49+
result = randomBufferSource() and
50+
result = source
5051
or
5152
// Loading a number from a `Buffer`.
5253
exists(DataFlow::TypeTracker t2 | t = t2.append(LoadStep(prop())) |
5354
// the random generators return arrays/Buffers of random numbers, we therefore track through an indexed read.
5455
exists(DataFlow::PropRead read | result = read |
55-
read.getBase() = goodRandom(t2) and
56+
read.getBase() = goodRandom(t2, source) and
5657
not read.getPropertyNameExpr() instanceof Label
5758
)
5859
or
5960
// reading a number from a Buffer.
6061
exists(DataFlow::MethodCallNode call | result = call |
61-
call.getReceiver() = goodRandom(t2) and
62+
call.getReceiver() = goodRandom(t2, source) and
6263
call
6364
.getMethodName()
6465
.regexpMatch("read(BigInt|BigUInt|Double|Float|Int|UInt)(8|16|32|64)?(BE|LE)?")
6566
)
6667
)
6768
or
68-
exists(DataFlow::TypeTracker t2 | t = t2.smallstep(goodRandom(t2), result))
69+
exists(DataFlow::TypeTracker t2 | t = t2.smallstep(goodRandom(t2, source), result))
6970
or
7071
// re-using the collection steps for `Set`.
7172
exists(DataFlow::TypeTracker t2 |
72-
result = CollectionsTypeTracking::collectionStep(goodRandom(t2), t, t2)
73+
result = CollectionsTypeTracking::collectionStep(goodRandom(t2, source), t, t2)
7374
)
7475
or
75-
InsecureRandomness::isAdditionalTaintStep(goodRandom(t.continue()), result)
76+
InsecureRandomness::isAdditionalTaintStep(goodRandom(t.continue(), source), result)
7677
}
7778

7879
/**
79-
* Gets a reference to a cryptographically random number.
80+
* Gets a reference to a cryptographically random number produced by `source`.
8081
*/
81-
DataFlow::Node goodRandom() { result = goodRandom(DataFlow::TypeTracker::end()) }
82+
DataFlow::Node goodRandom(DataFlow::SourceNode source) { result = goodRandom(DataFlow::TypeTracker::end(), source) }
8283

8384
/**
84-
* Gets a node that that produces a biased result from otherwise cryptographically secure random numbers.
85+
* Gets a node that that produces a biased result from otherwise cryptographically secure random numbers produced by `source`.
8586
*/
86-
DataFlow::Node badCrypto(string description) {
87+
DataFlow::Node badCrypto(string description, DataFlow::SourceNode source) {
8788
// addition and multiplication - always bad when both the lhs and rhs are random.
8889
exists(BinaryExpr binop | result.asExpr() = binop |
89-
goodRandom().asExpr() = binop.getLeftOperand() and
90-
goodRandom().asExpr() = binop.getRightOperand() and
90+
goodRandom(_).asExpr() = binop.getLeftOperand() and
91+
goodRandom(_).asExpr() = binop.getRightOperand() and
92+
(goodRandom(source).asExpr() = binop.getAnOperand()) and
9193
(
9294
binop.getOperator() = "+" and description = "addition"
9395
or
@@ -97,14 +99,14 @@ DataFlow::Node badCrypto(string description) {
9799
or
98100
// division - always bad
99101
exists(DivExpr div | result.asExpr() = div |
100-
goodRandom().asExpr() = div.getLeftOperand() and
102+
goodRandom(source).asExpr() = div.getLeftOperand() and
101103
description = "division"
102104
)
103105
or
104106
// modulo - only bad if not by a power of 2 - and the result is not checked for bias
105107
exists(ModExpr mod, DataFlow::Node random | result.asExpr() = mod and mod.getOperator() = "%" |
106108
description = "modulo" and
107-
goodRandom() = random and
109+
goodRandom(source) = random and
108110
random.asExpr() = mod.getLeftOperand() and
109111
// division by a power of 2 is OK. E.g. if `x` is uniformly random is in the range [0..255] then `x % 32` is uniformly random in the range [0..31].
110112
not mod.getRightOperand().getIntValue() = [2, 4, 8, 16, 32, 64, 128] and
@@ -125,13 +127,13 @@ DataFlow::Node badCrypto(string description) {
125127
exists(DataFlow::CallNode number, StringOps::ConcatenationRoot root | result = number |
126128
number = DataFlow::globalVarRef(["Number", "parseInt", "parseFloat"]).getACall() and
127129
root = number.getArgument(0) and
128-
goodRandom() = root.getALeaf() and
130+
goodRandom(source) = root.getALeaf() and
129131
exists(root.getALeaf().getStringValue()) and
130132
description = "string concatenation"
131133
)
132134
}
133135

134-
from DataFlow::Node node, string description
135-
where node = badCrypto(description)
136+
from DataFlow::Node node, string description, DataFlow::SourceNode source
137+
where node = badCrypto(description, source)
136138
select node,
137-
"Using " + description + " on cryptographically random numbers produces biased results."
139+
"Using " + description + " on a $@ produces biased results.", source, "cryptographically random number"
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
| bad-random.js:3:11:3:61 | crypto. ... s(1)[0] | Using addition on cryptographically random numbers produces biased results. |
2-
| bad-random.js:4:11:4:61 | crypto. ... s(1)[0] | Using multiplication on cryptographically random numbers produces biased results. |
3-
| bad-random.js:9:28:9:43 | buffer[i] / 25.6 | Using division on cryptographically random numbers produces biased results. |
4-
| bad-random.js:11:17:11:31 | buffer[i] % 100 | Using modulo on cryptographically random numbers produces biased results. |
5-
| bad-random.js:14:11:14:63 | Number( ... (0, 3)) | Using string concatenation on cryptographically random numbers produces biased results. |
6-
| bad-random.js:73:32:73:42 | byte / 25.6 | Using division on cryptographically random numbers produces biased results. |
7-
| bad-random.js:75:21:75:30 | byte % 100 | Using modulo on cryptographically random numbers produces biased results. |
8-
| bad-random.js:81:11:81:51 | secureR ... (10)[0] | Using addition on cryptographically random numbers produces biased results. |
9-
| bad-random.js:85:11:85:35 | goodRan ... Random2 | Using addition on cryptographically random numbers produces biased results. |
10-
| bad-random.js:87:16:87:24 | bad + bad | Using addition on cryptographically random numbers produces biased results. |
1+
| bad-random.js:3:11:3:61 | crypto. ... s(1)[0] | Using addition on a $@ produces biased results. | bad-random.js:3:11:3:31 | crypto. ... ytes(1) | cryptographically random numbers |
2+
| bad-random.js:3:11:3:61 | crypto. ... s(1)[0] | Using addition on a $@ produces biased results. | bad-random.js:3:38:3:58 | crypto. ... ytes(1) | cryptographically random numbers |
3+
| bad-random.js:4:11:4:61 | crypto. ... s(1)[0] | Using multiplication on a $@ produces biased results. | bad-random.js:4:11:4:31 | crypto. ... ytes(1) | cryptographically random numbers |
4+
| bad-random.js:4:11:4:61 | crypto. ... s(1)[0] | Using multiplication on a $@ produces biased results. | bad-random.js:4:38:4:58 | crypto. ... ytes(1) | cryptographically random numbers |
5+
| bad-random.js:9:28:9:43 | buffer[i] / 25.6 | Using division on a $@ produces biased results. | bad-random.js:6:16:6:40 | crypto. ... (bytes) | cryptographically random numbers |
6+
| bad-random.js:11:17:11:31 | buffer[i] % 100 | Using modulo on a $@ produces biased results. | bad-random.js:6:16:6:40 | crypto. ... (bytes) | cryptographically random numbers |
7+
| bad-random.js:14:11:14:63 | Number( ... (0, 3)) | Using string concatenation on a $@ produces biased results. | bad-random.js:14:25:14:45 | crypto. ... ytes(3) | cryptographically random numbers |
8+
| bad-random.js:73:32:73:42 | byte / 25.6 | Using division on a $@ produces biased results. | bad-random.js:70:20:70:44 | crypto. ... (bytes) | cryptographically random numbers |
9+
| bad-random.js:75:21:75:30 | byte % 100 | Using modulo on a $@ produces biased results. | bad-random.js:70:20:70:44 | crypto. ... (bytes) | cryptographically random numbers |
10+
| bad-random.js:81:11:81:51 | secureR ... (10)[0] | Using addition on a $@ produces biased results. | bad-random.js:81:11:81:26 | secureRandom(10) | cryptographically random numbers |
11+
| bad-random.js:81:11:81:51 | secureR ... (10)[0] | Using addition on a $@ produces biased results. | bad-random.js:81:33:81:48 | secureRandom(10) | cryptographically random numbers |
12+
| bad-random.js:85:11:85:35 | goodRan ... Random2 | Using addition on a $@ produces biased results. | bad-random.js:83:23:83:38 | secureRandom(10) | cryptographically random numbers |
13+
| 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 random numbers |
14+
| 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 random numbers |
15+
| 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 random numbers |

0 commit comments

Comments
 (0)