-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNumericCastTaintedLocal.ql
More file actions
45 lines (39 loc) · 1.5 KB
/
NumericCastTaintedLocal.ql
File metadata and controls
45 lines (39 loc) · 1.5 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
/**
* @name Local-user-controlled data in numeric cast
* @description Casting user-controlled numeric data to a narrower type without validation
* can cause unexpected truncation.
* @kind problem
* @problem.severity recommendation
* @precision medium
* @id java/tainted-numeric-cast-local
* @tags security
* external/cwe/cwe-197
* external/cwe/cwe-681
*/
import java
import semmle.code.java.dataflow.FlowSources
import NumericCastCommon
private class NumericCastFlowConfig extends TaintTracking::Configuration {
NumericCastFlowConfig() {
this = "NumericCastTaintedLocal::LocalUserInputToNumericNarrowingCastExpr"
}
override predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
override predicate isSink(DataFlow::Node sink) {
sink.asExpr() = any(NumericNarrowingCastExpr cast).getExpr()
}
override predicate isSanitizer(DataFlow::Node node) {
boundedRead(node.asExpr()) or
castCheck(node.asExpr()) or
node.getType() instanceof SmallType or
smallExpr(node.asExpr()) or
node.getEnclosingCallable() instanceof HashCodeMethod
}
}
from
NumericNarrowingCastExpr exp, VarAccess tainted, LocalUserInput origin, NumericCastFlowConfig conf
where
exp.getExpr() = tainted and
conf.hasFlow(origin, DataFlow::exprNode(tainted)) and
not exists(RightShiftOp e | e.getShiftedVariable() = tainted.getVariable())
select exp, "$@ flows to here and is cast to a narrower type, potentially causing truncation.",
origin, "User-provided value"