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
143 changes: 143 additions & 0 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,149 @@ class ExactPathMatchSanitizer extends PathInjectionSanitizer {
}
}

/** A canonical file constructed without calling an application-defined override. */
private class CanonicalFileCall extends MethodCall {
CanonicalFileCall() {
this.getMethod().hasQualifiedName("java.io", "File", "getCanonicalFile") and
this.getQualifier().(ClassInstanceExpr).getConstructedType() instanceof TypeFile
}
}

/**
* Holds if `read` reads the local definition initialized to `value`.
* Unlike may-flow, this does not accept a phi that also contains an unchecked file.
*/
private predicate readsFileValue(Expr read, Expr value) {
exists(SsaExplicitWrite def |
def.getSourceVariable().getVariable() instanceof LocalScopeVariable and
def.getDefiningExpr().(VariableAssign).getSource() = value and
read = def.getARead()
)
}

/**
* Holds if `directory` is canonicalized directly from a parameter of `helper`.
* Restricting the origin lets us preserve that parameter's taint at calls to the helper.
*/
private predicate containmentDirectory(
CanonicalFileCall directory, Method helper, Parameter parameter
) {
exists(ClassInstanceExpr file, SsaParameterInit def |
file = directory.getQualifier() and
file.getNumArgument() = 1 and
file.getArgument(0) = def.getARead() and
parameter = def.getParameter() and
parameter.getCallable() = helper
)
}

/**
* Holds if `guard` compares the parent of the canonical `candidate` to `directory`.
*/
private predicate canonicalParentGuard(
Guard guard, CanonicalFileCall candidate, CanonicalFileCall directory
) {
exists(MethodCall equals, MethodCall parent, Expr allowed |
guard = equals and
equals.getMethod().hasQualifiedName("java.io", "File", "equals") and
parent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and
readsFileValue(parent.getQualifier(), candidate) and
readsFileValue(allowed, directory)
|
equals.getQualifier() = parent and equals.getArgument(0) = allowed
or
equals.getQualifier() = allowed and equals.getArgument(0) = parent
)
}

/**
* Holds if `parent` reads a loop phi initialized to the parent of `candidate`
* and updated only by taking its own parent. Every phi input must be accounted
* for: an existential may-flow path through a loop is not sufficient.
*/
private predicate canonicalAncestor(Expr parent, CanonicalFileCall candidate) {
exists(
SsaPhiDefinition phi, SsaExplicitWrite init, SsaExplicitWrite update, MethodCall firstParent,
MethodCall nextParent
|
parent = phi.getARead() and
phi.getSourceVariable().getVariable() instanceof LocalScopeVariable and
init.getSourceVariable() = phi.getSourceVariable() and
update.getSourceVariable() = phi.getSourceVariable() and
init = phi.getAnInput() and
update = phi.getAnInput() and
init != update and
forall(SsaDefinition input | input = phi.getAnInput() | input = [init, update]) and
init.getDefiningExpr().(VariableAssign).getSource() = firstParent and
firstParent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and
readsFileValue(firstParent.getQualifier(), candidate) and
update.getDefiningExpr().(VariableAssign).getSource() = nextParent and
nextParent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and
nextParent.getQualifier() = phi.getARead()
)
}

/**
* Holds if `guard` compares a canonical ancestor of `candidate` to `directory`.
*/
private predicate canonicalAncestorGuard(
Guard guard, CanonicalFileCall candidate, CanonicalFileCall directory
) {
exists(MethodCall equals, Expr parent, Expr allowed |
guard = equals and
equals.getMethod().hasQualifiedName("java.io", "File", "equals") and
canonicalAncestor(parent, candidate) and
readsFileValue(allowed, directory)
|
equals.getQualifier() = parent and equals.getArgument(0) = allowed
or
equals.getQualifier() = allowed and equals.getArgument(0) = parent
)
}

/**
* Holds if `ret` returns the unchanged canonical candidate on a successful containment check.
* Only static helpers with a directory parameter are modeled. In particular, we do not
* assume that an arbitrary expression producing the allowed directory is trusted.
*/
private predicate canonicalContainmentReturn(ReturnStmt ret, Parameter directoryParameter) {
exists(Method helper, CanonicalFileCall candidate, CanonicalFileCall directory, Guard guard |
helper = ret.getEnclosingCallable() and
helper.isStatic() and
containmentDirectory(directory, helper, directoryParameter) and
readsFileValue(ret.getExpr(), candidate) and
(
canonicalParentGuard(guard, candidate, directory)
or
canonicalAncestorGuard(guard, candidate, directory)
) and
guard.controls(ret.getBasicBlock(), true)
)
}

/** A canonical file returned only after a successful containment check. */
private class CanonicalContainmentSanitizer extends PathInjectionSanitizer {
CanonicalContainmentSanitizer() {
this.asExpr() = any(ReturnStmt ret | canonicalContainmentReturn(ret, _)).getExpr()
}
}

/**
* Preserve the allowed directory's influence on a containment helper's result.
* The successful return removes the candidate's influence, not the directory's:
* choosing an attacker-controlled allowed directory is still path injection.
*/
private class ContainmentDirectoryTaintStep extends AdditionalTaintStep {
override predicate step(DataFlow::Node n1, DataFlow::Node n2) {
exists(ReturnStmt ret, Parameter parameter, MethodCall call |
canonicalContainmentReturn(ret, parameter) and
call.getMethod().getSourceDeclaration() = ret.getEnclosingCallable() and
n1.asExpr() = call.getArgument(parameter.getPosition()) and
n2.asExpr() = call
)
}
}

abstract private class PathGuard extends Guard {
abstract Expr getCheckedExpr();
}
Expand Down
40 changes: 40 additions & 0 deletions java/ql/test/library-tests/canonical-containment/AncestorLoop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.io.File;
import java.io.IOException;

class AncestorLoop {
static String source() { return null; }
static void sink(Object value) {}

static File descendant(String path, String base) {
try {
File directory = new File(base).getCanonicalFile();
File candidate = new File(path).getCanonicalFile();
for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) {
if (directory.equals(parent)) {
return candidate;
}
}
return null;
} catch (IOException ex) {
return null;
}
}

static File fallthrough(String path, String base) throws IOException {
File directory = new File(base).getCanonicalFile();
File candidate = new File(path).getCanonicalFile();
for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) {
if (directory.equals(parent))
return candidate;
}
return candidate;
}

static void test() throws IOException {
File file = descendant(source(), "/srv/data");
if (file != null)
sink(file); // Safe: a canonical ancestor must match the allowed directory.
sink(fallthrough(source(), "/srv/data")); // $ hasTaintFlow
sink(descendant("/srv/data/fixed", source())); // $ hasTaintFlow
}
}
35 changes: 35 additions & 0 deletions java/ql/test/library-tests/canonical-containment/DirectParent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.io.File;
import java.io.IOException;

class DirectParent {
static String source() { return null; }
static void sink(Object value) {}

static File child(String base, String name) {
final File directory;
final File candidate;
try {
directory = new File(base).getCanonicalFile();
candidate = new File(directory, name).getCanonicalFile();
} catch (IOException ex) {
return null;
}
if (!directory.equals(candidate.getParentFile()))
return null;
return candidate;
}

static File unchecked(String base, String name) throws IOException {
File directory = new File(base).getCanonicalFile();
File candidate = new File(directory, name).getCanonicalFile();
return candidate;
}

static void test() throws IOException {
File file = child("/srv/data", source());
if (file != null)
sink(file); // Safe: the canonical parent must match the allowed directory.
sink(unchecked("/srv/data", source())); // $ hasTaintFlow
sink(child(source(), "fixed")); // $ hasTaintFlow
}
}
Empty file.
13 changes: 13 additions & 0 deletions java/ql/test/library-tests/canonical-containment/test.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java
import semmle.code.java.security.PathSanitizer
import utils.test.InlineFlowTest

module ContainmentConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { DefaultFlowConfig::isSource(source) }

predicate isSink(DataFlow::Node sink) { DefaultFlowConfig::isSink(sink) }

predicate isBarrier(DataFlow::Node node) { node instanceof PathInjectionSanitizer }
}

import TaintFlowTest<ContainmentConfig>
Loading