|
| 1 | +/** Provides dataflow configurations for tainted path queries. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.frameworks.Networking |
| 5 | +import semmle.code.java.dataflow.DataFlow |
| 6 | +import semmle.code.java.dataflow.FlowSources |
| 7 | +private import semmle.code.java.dataflow.ExternalFlow |
| 8 | +import semmle.code.java.security.PathCreation |
| 9 | +import semmle.code.java.security.PathSanitizer |
| 10 | + |
| 11 | +/** |
| 12 | + * A unit class for adding additional taint steps. |
| 13 | + * |
| 14 | + * Extend this class to add additional taint steps that should apply to tainted path flow configurations. |
| 15 | + */ |
| 16 | +class TaintedPathAdditionalTaintStep extends Unit { |
| 17 | + abstract predicate step(DataFlow::Node n1, DataFlow::Node n2); |
| 18 | +} |
| 19 | + |
| 20 | +private class DefaultTaintedPathAdditionalTaintStep extends TaintedPathAdditionalTaintStep { |
| 21 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 22 | + exists(Argument a | |
| 23 | + a = n1.asExpr() and |
| 24 | + a.getCall() = n2.asExpr() and |
| 25 | + a = any(TaintPreservingUriCtorParam tpp).getAnArgument() |
| 26 | + ) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +private class TaintPreservingUriCtorParam extends Parameter { |
| 31 | + TaintPreservingUriCtorParam() { |
| 32 | + exists(Constructor ctor, int idx, int nParams | |
| 33 | + ctor.getDeclaringType() instanceof TypeUri and |
| 34 | + this = ctor.getParameter(idx) and |
| 35 | + nParams = ctor.getNumberOfParameters() |
| 36 | + | |
| 37 | + // URI(String scheme, String ssp, String fragment) |
| 38 | + idx = 1 and nParams = 3 |
| 39 | + or |
| 40 | + // URI(String scheme, String host, String path, String fragment) |
| 41 | + idx = [1, 2] and nParams = 4 |
| 42 | + or |
| 43 | + // URI(String scheme, String authority, String path, String query, String fragment) |
| 44 | + idx = 2 and nParams = 5 |
| 45 | + or |
| 46 | + // URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment) |
| 47 | + idx = 4 and nParams = 7 |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * A taint-tracking configuration for tracking flow from remote sources to the creation of a path. |
| 54 | + */ |
| 55 | +module TaintedPathConfig implements DataFlow::ConfigSig { |
| 56 | + predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 57 | + |
| 58 | + predicate isSink(DataFlow::Node sink) { |
| 59 | + sink.asExpr() = any(PathCreation p).getAnInput() |
| 60 | + or |
| 61 | + sinkNode(sink, ["create-file", "read-file"]) |
| 62 | + } |
| 63 | + |
| 64 | + predicate isBarrier(DataFlow::Node sanitizer) { |
| 65 | + sanitizer.getType() instanceof BoxedType or |
| 66 | + sanitizer.getType() instanceof PrimitiveType or |
| 67 | + sanitizer.getType() instanceof NumberType or |
| 68 | + sanitizer instanceof PathInjectionSanitizer |
| 69 | + } |
| 70 | + |
| 71 | + predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 72 | + any(TaintedPathAdditionalTaintStep s).step(n1, n2) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** Tracks flow from remote sources to the creation of a path. */ |
| 77 | +module TaintedPathFlow = TaintTracking::Global<TaintedPathConfig>; |
| 78 | + |
| 79 | +/** |
| 80 | + * A taint-tracking configuration for tracking flow from user input to the creation of a path. |
| 81 | + */ |
| 82 | +module TaintedPathLocalConfig implements DataFlow::ConfigSig { |
| 83 | + predicate isSource(DataFlow::Node source) { source instanceof LocalUserInput } |
| 84 | + |
| 85 | + predicate isSink(DataFlow::Node sink) { |
| 86 | + sink.asExpr() = any(PathCreation p).getAnInput() |
| 87 | + or |
| 88 | + sinkNode(sink, "create-file") |
| 89 | + } |
| 90 | + |
| 91 | + predicate isBarrier(DataFlow::Node sanitizer) { |
| 92 | + sanitizer.getType() instanceof BoxedType or |
| 93 | + sanitizer.getType() instanceof PrimitiveType or |
| 94 | + sanitizer.getType() instanceof NumberType or |
| 95 | + sanitizer instanceof PathInjectionSanitizer |
| 96 | + } |
| 97 | + |
| 98 | + predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 99 | + any(TaintedPathAdditionalTaintStep s).step(n1, n2) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** Tracks flow from user input to the creation of a path. */ |
| 104 | +module TaintedPathLocalFlow = TaintTracking::Global<TaintedPathLocalConfig>; |
0 commit comments