Skip to content

Commit a317736

Browse files
committed
Java: Add support for BarrierGuards as parameterised modules.
1 parent 1075a14 commit a317736

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowUtil.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,27 @@ class ContentSet instanceof Content {
304304
}
305305
}
306306

307+
/**
308+
* Holds if `g` validates the `e` upon evaluating to `branch`.
309+
*
310+
* The expression `e` is expected to be a syntactic part of the guard `g`.
311+
* For example, the guard `g` might be a call `isSafe(x)` and the expression `e`
312+
* the argument `x`.
313+
*/
314+
signature predicate guardChecksSig(Guard g, Expr e, boolean branch);
315+
316+
module BarrierGuard<guardChecksSig/3 guardChecks> {
317+
/** Gets a node that is safely guarded by the given guard. */
318+
Node getABarrierNode() {
319+
exists(Guard g, SsaVariable v, boolean branch, RValue use |
320+
guardChecks(g, v.getAUse(), branch) and
321+
use = v.getAUse() and
322+
g.controls(use.getBasicBlock(), branch) and
323+
result.asExpr() = use
324+
)
325+
}
326+
}
327+
307328
/**
308329
* A guard that validates some expression.
309330
*

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import semmle.code.java.security.PathCreation
1919
import DataFlow::PathGraph
2020
import TaintedPathCommon
2121

22-
class ContainsDotDotSanitizer extends DataFlow::BarrierGuard {
23-
ContainsDotDotSanitizer() {
24-
this.(MethodAccess).getMethod().hasName("contains") and
25-
this.(MethodAccess).getAnArgument().(StringLiteral).getValue() = ".."
26-
}
27-
28-
override predicate checks(Expr e, boolean branch) {
29-
e = this.(MethodAccess).getQualifier() and branch = false
30-
}
22+
predicate containsDotDotSanitizer(Guard g, Expr e, boolean branch) {
23+
exists(MethodAccess contains | g = contains |
24+
contains.getMethod().hasName("contains") and
25+
contains.getAnArgument().(StringLiteral).getValue() = ".." and
26+
e = contains.getQualifier() and
27+
branch = false
28+
)
3129
}
3230

3331
class TaintedPathConfig extends TaintTracking::Configuration {
@@ -41,10 +39,8 @@ class TaintedPathConfig extends TaintTracking::Configuration {
4139

4240
override predicate isSanitizer(DataFlow::Node node) {
4341
exists(Type t | t = node.getType() | t instanceof BoxedType or t instanceof PrimitiveType)
44-
}
45-
46-
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
47-
guard instanceof ContainsDotDotSanitizer
42+
or
43+
node = DataFlow::BarrierGuard<containsDotDotSanitizer/3>::getABarrierNode()
4844
}
4945
}
5046

0 commit comments

Comments
 (0)