Skip to content

Commit bb7faf0

Browse files
committed
C++: Introduce the coarse upper bound check from default taint tracking
1 parent d3cccca commit bb7faf0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

cpp/ql/src/Security/CWE/CWE-022/TaintedPath.ql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ Expr asSinkExpr(DataFlow::Node node) {
6565
.getUnconvertedResultExpression()
6666
}
6767

68+
/**
69+
* A variable that has any kind of upper-bound check anywhere in the program. This is
70+
* biased towards being inclusive and being a coarse overapproximation because there are
71+
* a lot of valid ways of doing an upper bounds checks if we don't consider where it
72+
* occurs, for example:
73+
* ```
74+
* if (x < 10) { sink(x); }
75+
*
76+
* if (10 > y) { sink(y); }
77+
*
78+
* if (z > 10) { z = 10; }
79+
* sink(z);
80+
* ```
81+
*/
82+
predicate hasUpperBoundsCheck(Variable var) {
83+
exists(RelationalOperation oper, VariableAccess access |
84+
oper.getAnOperand() = access and
85+
access.getTarget() = var and
86+
// Comparing to 0 is not an upper bound check
87+
not oper.getAnOperand().getValue() = "0"
88+
)
89+
}
90+
6891
class TaintedPathConfiguration extends TaintTracking::Configuration {
6992
TaintedPathConfiguration() { this = "TaintedPathConfiguration" }
7093

@@ -80,6 +103,8 @@ class TaintedPathConfiguration extends TaintTracking::Configuration {
80103

81104
override predicate isSanitizer(DataFlow::Node node) {
82105
node.asExpr().(Call).getTarget().getUnspecifiedType() instanceof ArithmeticType
106+
or
107+
hasUpperBoundsCheck(node.asExpr().(VariableAccess).getTarget())
83108
}
84109

85110
predicate hasFilteredFlowPath(DataFlow::PathNode source, DataFlow::PathNode sink) {

0 commit comments

Comments
 (0)