-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathArithmeticUncontrolled.ql
More file actions
69 lines (65 loc) · 2.76 KB
/
ArithmeticUncontrolled.ql
File metadata and controls
69 lines (65 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @name Uncontrolled data in arithmetic expression
* @description Arithmetic operations on uncontrolled data that is not validated can cause
* overflows.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/uncontrolled-arithmetic
* @tags security
* external/cwe/cwe-190
* external/cwe/cwe-191
*/
import java
import semmle.code.java.dataflow.TaintTracking
import semmle.code.java.security.SecurityTests
import ArithmeticCommon
class TaintSource extends DataFlow::ExprNode {
TaintSource() {
// Either this is an access to a random number generating method of the right kind, ...
exists(Method def |
def = this.getExpr().(MethodAccess).getMethod() and
(
// Some random-number methods are omitted:
// `nextDouble` and `nextFloat` are between 0 and 1,
// `nextGaussian` is extremely unlikely to hit max values.
def.getName() = "nextInt" or
def.getName() = "nextLong"
) and
def.getNumberOfParameters() = 0 and
def.getDeclaringType().hasQualifiedName("java.util", "Random")
) or
// ... or this is the array parameter of `nextBytes`, which is filled with random bytes.
exists(MethodAccess m, Method def |
m.getAnArgument() = this.getExpr() and
m.getMethod() = def and
def.getName() = "nextBytes" and
def.getNumberOfParameters() = 1 and
def.getDeclaringType().hasQualifiedName("java.util", "Random")
)
}
}
predicate sink(ArithExpr exp, VarAccess tainted, string effect) {
exp.getAnOperand() = tainted and
(
not guardedAgainstUnderflow(exp, tainted) and effect = "underflow" or
not guardedAgainstOverflow(exp, tainted) and effect = "overflow"
) and
// Exclude widening conversions of tainted values due to binary numeric promotion (JLS 5.6.2)
// unless there is an enclosing cast down to a narrower type.
narrowerThanOrEqualTo(exp, tainted.getType()) and
not overflowIrrelevant(exp) and
not exp.getEnclosingCallable().getDeclaringType() instanceof NonSecurityTestClass
}
class ArithmeticUncontrolledFlowConfig extends TaintTracking::Configuration {
ArithmeticUncontrolledFlowConfig() { this = "ArithmeticUncontrolledFlowConfig" }
override predicate isSource(DataFlow::Node source) { source instanceof TaintSource }
override predicate isSink(DataFlow::Node sink) { sink(_, sink.asExpr(), _) }
override predicate isSanitizer(DataFlow::Node n) { n.getType() instanceof BooleanType }
}
from ArithExpr exp, VarAccess tainted, TaintSource origin, string effect, ArithmeticUncontrolledFlowConfig conf
where
conf.hasFlow(origin, DataFlow::exprNode(tainted)) and
sink(exp, tainted, effect)
select exp, "$@ flows to here and is used in arithmetic, potentially causing an " + effect + ".",
origin, "Uncontrolled value"