|
| 1 | +/** Provides classes and predicates to reason about sanitization of path injection vulnerabilities. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.controlflow.Guards |
| 5 | +private import semmle.code.java.dataflow.ExternalFlow |
| 6 | +private import semmle.code.java.dataflow.FlowSources |
| 7 | +private import semmle.code.java.dataflow.SSA |
| 8 | + |
| 9 | +/** A sanitizer that protects against path injection vulnerabilities. */ |
| 10 | +abstract class PathInjectionSanitizer extends DataFlow::Node { } |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides a set of nodes validated by a method that uses a validation guard. |
| 14 | + */ |
| 15 | +private module ValidationMethod<DataFlow::guardChecksSig/3 validationGuard> { |
| 16 | + /** Gets a node that is safely guarded by a method that uses the given guard check. */ |
| 17 | + DataFlow::Node getAValidatedNode() { |
| 18 | + exists(MethodAccess ma, int pos, RValue rv | |
| 19 | + validationMethod(ma.getMethod(), pos) and |
| 20 | + ma.getArgument(pos) = rv and |
| 21 | + adjacentUseUseSameVar(rv, result.asExpr()) and |
| 22 | + ma.getBasicBlock().bbDominates(result.asExpr().getBasicBlock()) |
| 23 | + ) |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Holds if `m` validates its `arg`th parameter by using `validationGuard`. |
| 28 | + */ |
| 29 | + private predicate validationMethod(Method m, int arg) { |
| 30 | + exists( |
| 31 | + Guard g, SsaImplicitInit var, ControlFlowNode exit, ControlFlowNode normexit, boolean branch |
| 32 | + | |
| 33 | + validationGuard(g, var.getAUse(), branch) and |
| 34 | + var.isParameterDefinition(m.getParameter(arg)) and |
| 35 | + exit = m and |
| 36 | + normexit.getANormalSuccessor() = exit and |
| 37 | + 1 = strictcount(ControlFlowNode n | n.getANormalSuccessor() = exit) |
| 38 | + | |
| 39 | + g.(ConditionNode).getABranchSuccessor(branch) = exit or |
| 40 | + g.controls(normexit.getBasicBlock(), branch) |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Holds if `g` is guard that compares a path to a trusted value. |
| 47 | + */ |
| 48 | +private predicate exactPathMatchGuard(Guard g, Expr e, boolean branch) { |
| 49 | + exists(MethodAccess ma, RefType t | |
| 50 | + t instanceof TypeString or |
| 51 | + t instanceof TypeUri or |
| 52 | + t instanceof TypePath or |
| 53 | + t instanceof TypeFile or |
| 54 | + t.hasQualifiedName("android.net", "Uri") |
| 55 | + | |
| 56 | + ma.getMethod().getDeclaringType() = t and |
| 57 | + ma = g and |
| 58 | + ma.getMethod().getName() = ["equals", "equalsIgnoreCase"] and |
| 59 | + e = ma.getQualifier() and |
| 60 | + branch = true |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +private class ExactPathMatchSanitizer extends PathInjectionSanitizer { |
| 65 | + ExactPathMatchSanitizer() { |
| 66 | + this = DataFlow::BarrierGuard<exactPathMatchGuard/3>::getABarrierNode() |
| 67 | + or |
| 68 | + this = ValidationMethod<exactPathMatchGuard/3>::getAValidatedNode() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +abstract private class PathGuard extends Guard { |
| 73 | + abstract Expr getCheckedExpr(); |
| 74 | +} |
| 75 | + |
| 76 | +private predicate anyNode(DataFlow::Node n) { any() } |
| 77 | + |
| 78 | +private predicate pathGuardNode(DataFlow::Node n) { n.asExpr() = any(PathGuard g).getCheckedExpr() } |
| 79 | + |
| 80 | +private predicate localTaintFlowToPathGuard(Expr e, PathGuard g) { |
| 81 | + TaintTracking::LocalTaintFlow<anyNode/1, pathGuardNode/1>::hasExprFlow(e, g.getCheckedExpr()) |
| 82 | +} |
| 83 | + |
| 84 | +private class AllowedPrefixGuard extends PathGuard instanceof MethodAccess { |
| 85 | + AllowedPrefixGuard() { |
| 86 | + (isStringPrefixMatch(this) or isPathPrefixMatch(this)) and |
| 87 | + not isDisallowedWord(super.getAnArgument()) |
| 88 | + } |
| 89 | + |
| 90 | + override Expr getCheckedExpr() { result = super.getQualifier() } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Holds if `g` is a guard that considers a path safe because it is checked against trusted prefixes. |
| 95 | + * This requires additional protection against path traversal, either another guard (`PathTraversalGuard`) |
| 96 | + * or a sanitizer (`PathNormalizeSanitizer`), to ensure any internal `..` components are removed from the path. |
| 97 | + */ |
| 98 | +private predicate allowedPrefixGuard(Guard g, Expr e, boolean branch) { |
| 99 | + branch = true and |
| 100 | + // Local taint-flow is used here to handle cases where the validated expression comes from the |
| 101 | + // expression reaching the sink, but it's not the same one, e.g.: |
| 102 | + // File file = source(); |
| 103 | + // String strPath = file.getCanonicalPath(); |
| 104 | + // if (strPath.startsWith("/safe/dir")) |
| 105 | + // sink(file); |
| 106 | + g instanceof AllowedPrefixGuard and |
| 107 | + localTaintFlowToPathGuard(e, g) and |
| 108 | + exists(Expr previousGuard | |
| 109 | + localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g) |
| 110 | + or |
| 111 | + previousGuard |
| 112 | + .(PathTraversalGuard) |
| 113 | + .controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch()) |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +private class AllowedPrefixSanitizer extends PathInjectionSanitizer { |
| 118 | + AllowedPrefixSanitizer() { |
| 119 | + this = DataFlow::BarrierGuard<allowedPrefixGuard/3>::getABarrierNode() or |
| 120 | + this = ValidationMethod<allowedPrefixGuard/3>::getAValidatedNode() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Holds if `g` is a guard that considers a path safe because it is checked for `..` components, having previously |
| 126 | + * been checked for a trusted prefix. |
| 127 | + */ |
| 128 | +private predicate dotDotCheckGuard(Guard g, Expr e, boolean branch) { |
| 129 | + // Local taint-flow is used here to handle cases where the validated expression comes from the |
| 130 | + // expression reaching the sink, but it's not the same one, e.g.: |
| 131 | + // Path path = source(); |
| 132 | + // String strPath = path.toString(); |
| 133 | + // if (!strPath.contains("..") && strPath.startsWith("/safe/dir")) |
| 134 | + // sink(path); |
| 135 | + branch = g.(PathTraversalGuard).getBranch() and |
| 136 | + localTaintFlowToPathGuard(e, g) and |
| 137 | + exists(Guard previousGuard | |
| 138 | + previousGuard.(AllowedPrefixGuard).controls(g.getBasicBlock(), true) |
| 139 | + or |
| 140 | + previousGuard.(BlockListGuard).controls(g.getBasicBlock(), false) |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +private class DotDotCheckSanitizer extends PathInjectionSanitizer { |
| 145 | + DotDotCheckSanitizer() { |
| 146 | + this = DataFlow::BarrierGuard<dotDotCheckGuard/3>::getABarrierNode() or |
| 147 | + this = ValidationMethod<dotDotCheckGuard/3>::getAValidatedNode() |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +private class BlockListGuard extends PathGuard instanceof MethodAccess { |
| 152 | + BlockListGuard() { |
| 153 | + (isStringPartialMatch(this) or isPathPrefixMatch(this)) and |
| 154 | + isDisallowedWord(super.getAnArgument()) |
| 155 | + } |
| 156 | + |
| 157 | + override Expr getCheckedExpr() { result = super.getQualifier() } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Holds if `g` is a guard that considers a string safe because it is checked against a blocklist of known dangerous values. |
| 162 | + * This requires additional protection against path traversal, either another guard (`PathTraversalGuard`) |
| 163 | + * or a sanitizer (`PathNormalizeSanitizer`), to ensure any internal `..` components are removed from the path. |
| 164 | + */ |
| 165 | +private predicate blockListGuard(Guard g, Expr e, boolean branch) { |
| 166 | + branch = false and |
| 167 | + // Local taint-flow is used here to handle cases where the validated expression comes from the |
| 168 | + // expression reaching the sink, but it's not the same one, e.g.: |
| 169 | + // File file = source(); |
| 170 | + // String strPath = file.getCanonicalPath(); |
| 171 | + // if (!strPath.contains("..") && !strPath.startsWith("/dangerous/dir")) |
| 172 | + // sink(file); |
| 173 | + g instanceof BlockListGuard and |
| 174 | + localTaintFlowToPathGuard(e, g) and |
| 175 | + exists(Expr previousGuard | |
| 176 | + localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g) |
| 177 | + or |
| 178 | + previousGuard |
| 179 | + .(PathTraversalGuard) |
| 180 | + .controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch()) |
| 181 | + ) |
| 182 | +} |
| 183 | + |
| 184 | +private class BlockListSanitizer extends PathInjectionSanitizer { |
| 185 | + BlockListSanitizer() { |
| 186 | + this = DataFlow::BarrierGuard<blockListGuard/3>::getABarrierNode() or |
| 187 | + this = ValidationMethod<blockListGuard/3>::getAValidatedNode() |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +private predicate isStringPrefixMatch(MethodAccess ma) { |
| 192 | + exists(Method m | m = ma.getMethod() and m.getDeclaringType() instanceof TypeString | |
| 193 | + m.hasName("startsWith") |
| 194 | + or |
| 195 | + m.hasName("regionMatches") and |
| 196 | + ma.getArgument(0).(CompileTimeConstantExpr).getIntValue() = 0 |
| 197 | + or |
| 198 | + m.hasName("matches") and |
| 199 | + not ma.getArgument(0).(CompileTimeConstantExpr).getStringValue().matches(".*%") |
| 200 | + ) |
| 201 | +} |
| 202 | + |
| 203 | +/** |
| 204 | + * Holds if `ma` is a call to a method that checks a partial string match. |
| 205 | + */ |
| 206 | +private predicate isStringPartialMatch(MethodAccess ma) { |
| 207 | + isStringPrefixMatch(ma) |
| 208 | + or |
| 209 | + ma.getMethod().getDeclaringType() instanceof TypeString and |
| 210 | + ma.getMethod().hasName(["contains", "matches", "regionMatches", "indexOf", "lastIndexOf"]) |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Holds if `ma` is a call to a method that checks whether a path starts with a prefix. |
| 215 | + */ |
| 216 | +private predicate isPathPrefixMatch(MethodAccess ma) { |
| 217 | + exists(RefType t | |
| 218 | + t instanceof TypePath |
| 219 | + or |
| 220 | + t.hasQualifiedName("kotlin.io", "FilesKt") |
| 221 | + | |
| 222 | + t = ma.getMethod().getDeclaringType() and |
| 223 | + ma.getMethod().hasName("startsWith") |
| 224 | + ) |
| 225 | +} |
| 226 | + |
| 227 | +private predicate isDisallowedWord(CompileTimeConstantExpr word) { |
| 228 | + word.getStringValue().matches(["/", "\\", "%WEB-INF%", "%/data%"]) |
| 229 | +} |
| 230 | + |
| 231 | +/** A complementary guard that protects against path traversal, by looking for the literal `..`. */ |
| 232 | +private class PathTraversalGuard extends PathGuard { |
| 233 | + PathTraversalGuard() { |
| 234 | + exists(MethodAccess ma | |
| 235 | + ma.getMethod().getDeclaringType() instanceof TypeString and |
| 236 | + ma.getAnArgument().(CompileTimeConstantExpr).getStringValue() = ".." |
| 237 | + | |
| 238 | + this = ma and |
| 239 | + ma.getMethod().hasName("contains") |
| 240 | + or |
| 241 | + exists(EqualityTest eq | |
| 242 | + this = eq and |
| 243 | + ma.getMethod().hasName(["indexOf", "lastIndexOf"]) and |
| 244 | + eq.getAnOperand() = ma and |
| 245 | + eq.getAnOperand().(CompileTimeConstantExpr).getIntValue() = -1 |
| 246 | + ) |
| 247 | + ) |
| 248 | + } |
| 249 | + |
| 250 | + override Expr getCheckedExpr() { |
| 251 | + exists(MethodAccess ma | ma = this.(EqualityTest).getAnOperand() or ma = this | |
| 252 | + result = ma.getQualifier() |
| 253 | + ) |
| 254 | + } |
| 255 | + |
| 256 | + boolean getBranch() { |
| 257 | + this instanceof MethodAccess and result = false |
| 258 | + or |
| 259 | + result = this.(EqualityTest).polarity() |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +/** A complementary sanitizer that protects against path traversal using path normalization. */ |
| 264 | +private class PathNormalizeSanitizer extends MethodAccess { |
| 265 | + PathNormalizeSanitizer() { |
| 266 | + exists(RefType t | |
| 267 | + t instanceof TypePath or |
| 268 | + t.hasQualifiedName("kotlin.io", "FilesKt") |
| 269 | + | |
| 270 | + this.getMethod().getDeclaringType() = t and |
| 271 | + this.getMethod().hasName("normalize") |
| 272 | + ) |
| 273 | + or |
| 274 | + this.getMethod().getDeclaringType() instanceof TypeFile and |
| 275 | + this.getMethod().hasName(["getCanonicalPath", "getCanonicalFile"]) |
| 276 | + } |
| 277 | +} |
0 commit comments