|
| 1 | +/** |
| 2 | + * @name Everything trusting `TrustManager` |
| 3 | + * @description Trusting all certificates allows an attacker to perform a machine-in-the-middle attack. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id java/insecure-trustmanager |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-295 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.controlflow.Guards |
| 14 | +import semmle.code.java.dataflow.DataFlow |
| 15 | +import semmle.code.java.dataflow.FlowSources |
| 16 | +import semmle.code.java.dataflow.TaintTracking2 |
| 17 | +import semmle.code.java.security.Encryption |
| 18 | +import DataFlow::PathGraph |
| 19 | + |
| 20 | +/** |
| 21 | + * Models an insecure `X509TrustManager`. |
| 22 | + * An `X509TrustManager` is considered insecure if it never throws a `CertificatException` thereby accepting any certificate as valid. |
| 23 | + */ |
| 24 | +class InsecureX509TrustManager extends RefType { |
| 25 | + InsecureX509TrustManager() { |
| 26 | + getASupertype*() instanceof X509TrustManager and |
| 27 | + exists(Method m | |
| 28 | + m.getDeclaringType() = this and |
| 29 | + m.hasName("checkServerTrusted") and |
| 30 | + not mayThrowCertificateException(m) |
| 31 | + ) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** The `java.security.cert.CertificateException` class. */ |
| 36 | +private class CertificatException extends RefType { |
| 37 | + CertificatException() { hasQualifiedName("java.security.cert", "CertificateException") } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + *Holds if: |
| 42 | + * - `m` may `throw` an `CertificatException` |
| 43 | + * - `m` calls another method that may throw |
| 44 | + * - `m` calls a method that declares to throw an `CertificatExceptio`, but for which no source is available |
| 45 | + */ |
| 46 | +private predicate mayThrowCertificateException(Method m) { |
| 47 | + exists(Stmt stmt | m.getBody().getAChild*() = stmt | |
| 48 | + stmt.(ThrowStmt).getThrownExceptionType().getASupertype*() instanceof CertificatException |
| 49 | + ) |
| 50 | + or |
| 51 | + exists(Method otherMethod | m.polyCalls(otherMethod) | |
| 52 | + mayThrowCertificateException(otherMethod) |
| 53 | + or |
| 54 | + not otherMethod.fromSource() and |
| 55 | + otherMethod.getAnException().getType().getASupertype*() instanceof CertificatException |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A configuration to model the flow of a `InsecureX509TrustManager` to an `SSLContext.init` call. |
| 61 | + */ |
| 62 | +class InsecureTrustManagerConfiguration extends TaintTracking::Configuration { |
| 63 | + InsecureTrustManagerConfiguration() { this = "InsecureTrustManagerConfiguration" } |
| 64 | + |
| 65 | + override predicate isSource(DataFlow::Node source) { |
| 66 | + source.asExpr().(ClassInstanceExpr).getConstructedType() instanceof InsecureX509TrustManager |
| 67 | + } |
| 68 | + |
| 69 | + override predicate isSink(DataFlow::Node sink) { |
| 70 | + exists(MethodAccess ma, Method m | |
| 71 | + m.hasName("init") and |
| 72 | + m.getDeclaringType() instanceof SSLContext and |
| 73 | + ma.getMethod() = m |
| 74 | + | |
| 75 | + ma.getArgument(1) = sink.asExpr() |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + override predicate isSanitizer(DataFlow::Node barrier) { |
| 80 | + // ignore nodes that are in functions that intentionally trust all certificates |
| 81 | + barrier |
| 82 | + .getEnclosingCallable() |
| 83 | + .getName() |
| 84 | + /* |
| 85 | + * Regex: (_)* : |
| 86 | + * some methods have underscores. |
| 87 | + * Regex: (no|ignore|disable)(strictssl|ssl|verify|verification) |
| 88 | + * noStrictSSL ignoreSsl |
| 89 | + * Regex: (set)?(accept|trust|ignore|allow)(all|every|any|selfsigned) |
| 90 | + * acceptAll trustAll ignoreAll setTrustAnyHttps |
| 91 | + * Regex: (use|do|enable)insecure |
| 92 | + * useInsecureSSL |
| 93 | + * Regex: (set|do|use)?no.*(check|validation|verify|verification) |
| 94 | + * setNoCertificateCheck |
| 95 | + * Regex: disable |
| 96 | + * disableChecks |
| 97 | + */ |
| 98 | + |
| 99 | + .regexpMatch("^(?i)(_)*((no|ignore|disable)(strictssl|ssl|verify|verification)" + |
| 100 | + "|(set)?(accept|trust|ignore|allow)(all|every|any|selfsigned)" + |
| 101 | + "|(use|do|enable)insecure|(set|do|use)?no.*(check|validation|verify|verification)|disable).*$") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +bindingset[result] |
| 106 | +private string getAFlagName() { |
| 107 | + result |
| 108 | + .regexpMatch("(?i).*(secure|disable|selfCert|selfSign|validat|verif|trust|ignore|nocertificatecheck).*") |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * A flag has to either be of type `String`, `boolean` or `Boolean`. |
| 113 | + */ |
| 114 | +private class FlagType extends Type { |
| 115 | + FlagType() { |
| 116 | + this instanceof TypeString |
| 117 | + or |
| 118 | + this instanceof BooleanType |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +private predicate isEqualsIgnoreCaseMethodAccess(MethodAccess ma) { |
| 123 | + ma.getMethod().hasName("equalsIgnoreCase") and |
| 124 | + ma.getMethod().getDeclaringType() instanceof TypeString |
| 125 | +} |
| 126 | + |
| 127 | +/** Holds if `source` should is considered a flag. */ |
| 128 | +private predicate isFlag(DataFlow::Node source) { |
| 129 | + exists(VarAccess v | v.getVariable().getName() = getAFlagName() | |
| 130 | + source.asExpr() = v and v.getType() instanceof FlagType |
| 131 | + ) |
| 132 | + or |
| 133 | + exists(StringLiteral s | s.getRepresentedString() = getAFlagName() | source.asExpr() = s) |
| 134 | + or |
| 135 | + exists(MethodAccess ma | ma.getMethod().getName() = getAFlagName() | |
| 136 | + source.asExpr() = ma and |
| 137 | + ma.getType() instanceof FlagType and |
| 138 | + not isEqualsIgnoreCaseMethodAccess(ma) |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +/** Holds if there is flow from `node1` to `node2` either due to local flow or due to custom flow steps. */ |
| 143 | +private predicate flagFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 144 | + DataFlow::localFlowStep(node1, node2) |
| 145 | + or |
| 146 | + exists(MethodAccess ma | ma.getMethod() = any(EnvReadMethod m) | |
| 147 | + ma = node2.asExpr() and ma.getAnArgument() = node1.asExpr() |
| 148 | + ) |
| 149 | + or |
| 150 | + exists(MethodAccess ma | |
| 151 | + ma.getMethod().hasName("parseBoolean") and |
| 152 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.lang", "Boolean") |
| 153 | + | |
| 154 | + ma = node2.asExpr() and ma.getAnArgument() = node1.asExpr() |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +/** Gets a guard that depends on a flag. */ |
| 159 | +private Guard getAGuard() { |
| 160 | + exists(DataFlow::Node source, DataFlow::Node sink | |
| 161 | + isFlag(source) and |
| 162 | + flagFlowStep*(source, sink) and |
| 163 | + sink.asExpr() = result |
| 164 | + ) |
| 165 | +} |
| 166 | + |
| 167 | +/** Holds if `node` is guarded by a flag that suggests an intentionally insecure feature. */ |
| 168 | +private predicate isNodeGuardedByFlag(DataFlow::Node node) { |
| 169 | + exists(Guard g | g.controls(node.asExpr().getBasicBlock(), _) | g = getAGuard()) |
| 170 | +} |
| 171 | + |
| 172 | +from |
| 173 | + DataFlow::PathNode source, DataFlow::PathNode sink, InsecureTrustManagerConfiguration cfg, |
| 174 | + RefType trustManager |
| 175 | +where |
| 176 | + cfg.hasFlowPath(source, sink) and |
| 177 | + not isNodeGuardedByFlag(sink.getNode()) and |
| 178 | + trustManager = source.getNode().asExpr().(ClassInstanceExpr).getConstructedType() |
| 179 | +select sink, source, sink, "$@ that is defined $@ and trusts any certificate, is used here.", |
| 180 | + source, "This trustmanager", trustManager, "here" |
0 commit comments