|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.TaintTracking |
| 3 | + |
| 4 | +/** The class `javax.net.ssl.HostnameVerifier`. */ |
| 5 | +class TypeHostnameVerifier extends RefType { |
| 6 | + TypeHostnameVerifier() { hasQualifiedName("javax.net.ssl", "HostnameVerifier") } |
| 7 | +} |
| 8 | + |
| 9 | +/** The `javax.net.ssl.HostnameVerifier.verify` method. */ |
| 10 | +class HostnameVerifierVerifyMethod extends Method { |
| 11 | + HostnameVerifierVerifyMethod() { |
| 12 | + hasName("verify") and getDeclaringType() instanceof TypeHostnameVerifier |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** A method that overrides the `javax.net.ssl.HostnameVerifier.verify` method. */ |
| 17 | +class OverridenVerifyMethod extends Method { |
| 18 | + OverridenVerifyMethod() { |
| 19 | + exists(HostnameVerifierVerifyMethod m | this.overridesOrInstantiates*(m)) |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * The `hostname` parameter of this method. |
| 24 | + */ |
| 25 | + Parameter getHostnameParameter() { result = getParameter(0) } |
| 26 | + |
| 27 | + /** |
| 28 | + * The `session` parameter of this method. |
| 29 | + */ |
| 30 | + Parameter getSessionParameter() { result = getParameter(1) } |
| 31 | +} |
| 32 | + |
| 33 | +/** A return statement that returns `true`. */ |
| 34 | +class TrueReturnStmt extends ReturnStmt { |
| 35 | + TrueReturnStmt() { getResult().(BooleanLiteral).getBooleanValue() = true } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Holds if `m` always returns `true` ignoring any exceptional flow. |
| 40 | + */ |
| 41 | +private predicate alwaysReturnsTrue(OverridenVerifyMethod m) { |
| 42 | + forex(ReturnStmt rs | rs.getEnclosingCallable() = m | rs instanceof TrueReturnStmt) |
| 43 | +} |
| 44 | + |
| 45 | +/** A method that overrides the `javax.net.ssl.HostnameVerifier.verify` method in a dangerous way. */ |
| 46 | +abstract class DangerousVerifyMethod extends OverridenVerifyMethod { |
| 47 | + /** A `Stmt` that makes this `HostnameVerifier` dangerous. */ |
| 48 | + abstract Stmt getADangerousStmt(); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * A method that overrides the `javax.net.ssl.HostnameVerifier.verify` method and **always** returns `true`, thus |
| 53 | + * accepting any certificate despite a hostname mismatch. |
| 54 | + */ |
| 55 | +class AlwaysAcceptingVerifyMethod extends DangerousVerifyMethod { |
| 56 | + AlwaysAcceptingVerifyMethod() { alwaysReturnsTrue(this) } |
| 57 | + |
| 58 | + override Stmt getADangerousStmt() { |
| 59 | + exists(TrueReturnStmt r | r.getEnclosingCallable() = this | result = r) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** An access to any method whose name starts with `equals` and which is defined in the `String` class. */ |
| 64 | +class StringEqualsMethodAccess extends MethodAccess { |
| 65 | + StringEqualsMethodAccess() { |
| 66 | + getMethod().getName().matches("equals%") and |
| 67 | + getMethod().getDeclaringType() instanceof TypeString |
| 68 | + } |
| 69 | + |
| 70 | + // TODO there is probably a better name for "anEqualityPart" but I don't know any right now |
| 71 | + /** Returns a part of this equality check, i.e. in the case of `foo.equals(bar)` this returns `foo` and `bar`. */ |
| 72 | + Expr getAnEqualityPart() { result = getQualifier() or result = getArgument(0) } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Get the nearest enclosing if statement, if there exists one. |
| 77 | + * For example consider this case: |
| 78 | + * ```` |
| 79 | + * if(foo) { |
| 80 | + * if(bar) { |
| 81 | + * stmt |
| 82 | + * } |
| 83 | + * } |
| 84 | + * ```` |
| 85 | + * This query would then return the `bar` if statement and **not** the `foo` if statement. |
| 86 | + */ |
| 87 | +private IfStmt getNearestEnclosingIfStmt(Stmt stmt) { |
| 88 | + exists(IfStmt ifStmt | |
| 89 | + result = ifStmt and |
| 90 | + ifStmt.getAChild+() = stmt and |
| 91 | + /* |
| 92 | + * Ensure that the `IfStmt` we found does not contain another `IfStmt`. |
| 93 | + * If it contains one, the result can not be the nearest enclosing `IfStmt`. |
| 94 | + */ |
| 95 | + |
| 96 | + not exists(IfStmt enclosedIfStmt | enclosedIfStmt = ifStmt.getAChild+()) |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +/** The `javax.net.ssl.SSLSession.getPeerHost` method. */ |
| 101 | +private class SslSessionGetPeerHostMethodAccess extends MethodAccess { |
| 102 | + SslSessionGetPeerHostMethodAccess() { |
| 103 | + getMethod().hasName("getPeerHost") and |
| 104 | + getMethod().getDeclaringType().hasQualifiedName("javax.net.ssl", "SSLSession") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * A (dangerous) `StringEqualsMethodAccess` where the equality does: |
| 110 | + * - not depend on a expression that is dervied from the `session` parameter of the `verify` method. |
| 111 | + * - not depend on the value of `SSLSession.getPeerHost`, since the value is not authenticated. |
| 112 | + */ |
| 113 | +class SslSessionParameterIgnoringStringEqualsMethodAccess extends StringEqualsMethodAccess { |
| 114 | + SslSessionParameterIgnoringStringEqualsMethodAccess() { |
| 115 | + exists(Expr e, Parameter hostnameParameter, Parameter sslSessionParameter | |
| 116 | + hostnameParameter = getEnclosingCallable().(OverridenVerifyMethod).getHostnameParameter() and |
| 117 | + sslSessionParameter = getEnclosingCallable().(OverridenVerifyMethod).getSessionParameter() and |
| 118 | + getAnEqualityPart().getAChildExpr*() = e |
| 119 | + | |
| 120 | + not TaintTracking::localExprTaint(sslSessionParameter.getAnAccess(), e) and |
| 121 | + not e instanceof SslSessionGetPeerHostMethodAccess |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * A `verify` method that does not verify the `hostname` correctly. It is incorrect |
| 128 | + * because it does not validate the `hostname` against a |
| 129 | + * any expression that is dervied from the `session` parameter of the `verify` method. |
| 130 | + */ |
| 131 | +class IncorrectHostnameVerifyMethod extends DangerousVerifyMethod { |
| 132 | + Stmt dangerousStmt; |
| 133 | + |
| 134 | + IncorrectHostnameVerifyMethod() { |
| 135 | + /* We return `true` based on an if statement that verifies the `hostname` incorrectly.*/ |
| 136 | + exists(TrueReturnStmt r, IfStmt i | |
| 137 | + getBody().getAChild*() = r and i = getNearestEnclosingIfStmt(r) |
| 138 | + | |
| 139 | + i.getCondition().getAChildExpr*() instanceof |
| 140 | + SslSessionParameterIgnoringStringEqualsMethodAccess and |
| 141 | + dangerousStmt = i |
| 142 | + ) |
| 143 | + or |
| 144 | + /* We return the result of an `equals` call that verifies the `hostname` incorrectly.*/ |
| 145 | + exists(ReturnStmt r | getBody().getAChild*() = r | |
| 146 | + not r.getResult() instanceof BooleanLiteral and |
| 147 | + r.getResult().getAChildExpr*() instanceof SslSessionParameterIgnoringStringEqualsMethodAccess and |
| 148 | + dangerousStmt = r |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + override Stmt getADangerousStmt() { result = dangerousStmt } |
| 153 | +} |
0 commit comments