Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Path injection sanitizers in `PathSanitizer.qll` that combine a prefix or blocklist check with a `..` traversal check now require both checks to apply to related values (connected by local taint flow), rather than allowing an unrelated check on another expression in the same method. This may result in additional true-positive alerts in `java/path-injection` and related queries.
140 changes: 126 additions & 14 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,116 @@ private predicate anyNode(DataFlow::Node n) { any() }

private predicate pathGuardNode(DataFlow::Node n) { n.asExpr() = any(PathGuard g).getCheckedExpr() }

private module FlowToPathGuard = TaintTracking::LocalTaintFlow<anyNode/1, pathGuardNode/1>;

private predicate localTaintFlowToPathGuard(Expr e, PathGuard g) {
TaintTracking::LocalTaintFlow<anyNode/1, pathGuardNode/1>::hasExprFlow(e, g.getCheckedExpr())
FlowToPathGuard::hasExprFlow(e, g.getCheckedExpr())
}

/**
* Holds if `n2` is a `String`, `File`, or `Path` representation of `n1` that
* preserves path-traversal components.
*/
private predicate pathRepresentationStep(DataFlow::Node n1, DataFlow::Node n2) {
exists(MethodCall mc, Method m |
n2.asExpr() = mc and
m = mc.getMethod()
|
n1.asExpr() = mc.getQualifier() and
mc.getNumArgument() = 0 and
(
m.hasName("toString") and
exists(RefType qt | qt = mc.getQualifier().getType().(RefType).getAnAncestor() |
qt instanceof TypePath or
qt instanceof TypeFile or
qt instanceof TypeString
)
or
m.getDeclaringType().getAnAncestor() instanceof TypePath and
m.hasName(["toAbsolutePath", "toFile"])
or
m.getDeclaringType().getAnAncestor() instanceof TypeFile and
m.hasName(["getAbsoluteFile", "getAbsolutePath", "getPath", "toPath"])
)
or
n1.asExpr() = mc.getArgument(0) and
m.getParameter(0).getType() instanceof TypeString and
(
mc.getNumArgument() = 1
or
mc.getNumArgument() = 2 and
mc.getArgument(1).(ArrayCreationExpr).getFirstDimensionSize() = 0
) and
(
m.getDeclaringType() instanceof TypePaths and m.hasName("get")
or
m.getDeclaringType() instanceof TypePath and m.hasName("of")
or
m.getDeclaringType().getAnAncestor() instanceof TypeFileSystem and m.hasName("getPath")
)
)
or
exists(ClassInstanceExpr cc |
n2.asExpr() = cc and
n1.asExpr() = cc.getArgument(0) and
cc.getConstructedType().getAnAncestor() instanceof TypeFile and
cc.getNumArgument() = 1 and
cc.getCallee().getParameter(0).getType() instanceof TypeString
)
}

private predicate localFlowToPathGuard(DataFlow::Node n, PathGuard g) {
n.asExpr() = g.getCheckedExpr()
or
exists(DataFlow::Node mid |
localFlowToPathGuard(mid, g) and
(
DataFlow::localFlowStep(n, mid) or
pathRepresentationStep(n, mid)
)
)
}

private predicate fileConstructorChildStep(DataFlow::Node n1, DataFlow::Node n2) {
exists(ConstructorCall cc |
cc.getConstructedType().getAnAncestor() instanceof TypeFile and
n1.asExpr() = cc.getArgument(1) and
n2.asExpr() = cc
)
}

private predicate reachesPathGuard(DataFlow::Node n) {
pathGuardNode(n)
or
exists(DataFlow::Node mid |
reachesPathGuard(mid) and
(
TaintTracking::localTaintStep(n, mid) or
fileConstructorChildStep(n, mid)
)
)
}

private predicate flowsFromGuard(DataFlow::Node n, PathGuard g) {
localFlowToPathGuard(n, g)
or
exists(DataFlow::Node mid |
flowsFromGuard(mid, g) and
reachesPathGuard(n) and
(
TaintTracking::localTaintStep(mid, n) or
fileConstructorChildStep(mid, n)
)
)
}

/**
* Holds if `e` (which itself flows to a `PathGuard`) and the expression checked by
* `previousGuard` are connected by local taint flow, possibly from a common value.
*/
private predicate checksRelatedValue(Expr e, PathGuard previousGuard) {
localTaintFlowToPathGuard(e, previousGuard) or
flowsFromGuard(DataFlow::exprNode(e), previousGuard)
}

private class AllowedPrefixGuard extends PathGuard instanceof MethodCall {
Expand All @@ -82,12 +190,13 @@ private predicate allowedPrefixGuard(Guard g, Expr e, boolean branch) {
// sink(file);
g instanceof AllowedPrefixGuard and
localTaintFlowToPathGuard(e, g) and
exists(Expr previousGuard |
localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g)
(
localTaintFlowToPathGuard(any(PathNormalizeSanitizer p), g)
or
previousGuard
.(PathTraversalGuard)
.controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch())
exists(PathTraversalGuard previousGuard |
previousGuard.controls(g.getBasicBlock(), previousGuard.getBranch()) and
checksRelatedValue(e, previousGuard)
)
)
}

Expand All @@ -108,12 +217,14 @@ private class FileGetNameCall extends MethodCall {
*/
private predicate dotDotCheckGuard(Guard g, Expr e, boolean branch) {
pathTraversalGuard(g, e, branch) and
exists(Guard previousGuard |
exists(PathGuard previousGuard |
previousGuard.(AllowedPrefixGuard).controls(g.getBasicBlock(), true)
or
previousGuard
.(BlockListGuard)
.controls(g.getBasicBlock(), previousGuard.(BlockListGuard).getBranch())
|
checksRelatedValue(e, previousGuard)
)
or
// `File.getName` strips any directory prefix, returning only the final path
Expand All @@ -137,7 +248,7 @@ private class BlockListGuard extends PathGuard {
BlockListGuard() {
exists(StringSubstringMatch match, boolean matchingBranch |
this = match and
checkedExpr = match.getInputString() and
checkedExpr = match.getInputString().getUnderlyingExpr() and
isDisallowedWord(match.getMatchExpression()) and
match.hasSubstringOnBranch(matchingBranch) and
branch = matchingBranch.booleanNot()
Expand Down Expand Up @@ -172,12 +283,13 @@ private predicate blockListGuard(Guard g, Expr e, boolean branch) {
// sink(file);
g instanceof BlockListGuard and
localTaintFlowToPathGuard(e, g) and
exists(Expr previousGuard |
localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g)
(
localTaintFlowToPathGuard(any(PathNormalizeSanitizer p), g)
or
previousGuard
.(PathTraversalGuard)
.controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch())
exists(PathTraversalGuard previousGuard |
previousGuard.controls(g.getBasicBlock(), previousGuard.getBranch()) and
checksRelatedValue(e, previousGuard)
)
)
}

Expand Down Expand Up @@ -250,7 +362,7 @@ private class PathTraversalGuard extends PathGuard {
PathTraversalGuard() {
exists(StringSubstringMatch match, boolean matchingBranch |
this = match and
checkedExpr = match.getInputString() and
checkedExpr = match.getInputString().getUnderlyingExpr() and
match.getMatchExpression().(CompileTimeConstantExpr).getStringValue() = ".." and
match.hasSubstringOnBranch(matchingBranch) and
branch = matchingBranch.booleanNot()
Expand Down
Loading
Loading