Skip to content

Commit c478287

Browse files
authored
Merge pull request #9294 from aschackmull/java/barrierguard-parammod
Java: Add support for BarrierGuards as parameterised modules.
2 parents b0c66dd + 4e6e595 commit c478287

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

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

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

307+
/**
308+
* Holds if the guard `g` validates the expression `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+
/**
317+
* Provides a set of barrier nodes for a guard that validates an expression.
318+
*
319+
* This is expected to be used in `isBarrier`/`isSanitizer` definitions
320+
* in data flow and taint tracking.
321+
*/
322+
module BarrierGuard<guardChecksSig/3 guardChecks> {
323+
/** Gets a node that is safely guarded by the given guard check. */
324+
Node getABarrierNode() {
325+
exists(Guard g, SsaVariable v, boolean branch, RValue use |
326+
guardChecks(g, v.getAUse(), branch) and
327+
use = v.getAUse() and
328+
g.controls(use.getBasicBlock(), branch) and
329+
result.asExpr() = use
330+
)
331+
}
332+
}
333+
307334
/**
308335
* A guard that validates some expression.
309336
*

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)