|
| 1 | +/** |
| 2 | + * @name InsecureRmiJmxAuthenticationEnvironment |
| 3 | + * @kind path-problem |
| 4 | + * @problem.severity error |
| 5 | + * @tags security |
| 6 | + * external/cwe/cwe-665 |
| 7 | + * @precision high |
| 8 | + * @id java/insecure-rmi-jmx-server-initalisation |
| 9 | + */ |
| 10 | + |
| 11 | +import java |
| 12 | +import semmle.code.java.dataflow.DataFlow |
| 13 | +import semmle.code.java.dataflow.DataFlow2 |
| 14 | +import semmle.code.java.Maps |
| 15 | +import DataFlow::PathGraph |
| 16 | +import semmle.code.java.dataflow.NullGuards |
| 17 | +import semmle.code.java.dataflow.Nullness |
| 18 | + |
| 19 | +/** predicate which detects vulnerable Constructors */ |
| 20 | +predicate isRmiOrJmxServerCreateConstructor(Constructor constructor) { |
| 21 | + constructor.getName() = "RMIConnectorServer" and |
| 22 | + constructor |
| 23 | + .getDeclaringType() |
| 24 | + .hasQualifiedName("javax.management.remote.rmi", "RMIConnectorServer") |
| 25 | +} |
| 26 | + |
| 27 | +/** Predicate which detects vulnerable server creations via methods */ |
| 28 | +predicate isRmiOrJmxServerCreateMethod(Method method) { |
| 29 | + method.getName() = "newJMXConnectorServer" and |
| 30 | + method.getDeclaringType().hasQualifiedName("javax.management.remote", "JMXConnectorServerFactory") |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Models flow from `new HashMap<>()` to a |
| 35 | + * `map.put("jmx.remote.rmi.server.credential.types", value)` call. |
| 36 | + */ |
| 37 | +class MapToPutCredentialstypeConfiguration extends DataFlow2::Configuration { |
| 38 | + MapToPutCredentialstypeConfiguration() { this = "MapToPutCredentialstypeConfiguration" } |
| 39 | + |
| 40 | + override predicate isSource(DataFlow::Node source) { |
| 41 | + source.asExpr().(ClassInstanceExpr).getConstructedType() instanceof MapType |
| 42 | + } |
| 43 | + |
| 44 | + override predicate isSink(DataFlow2::Node sink) { putsCredentialtypesKey(sink.asExpr()) } |
| 45 | + |
| 46 | + /** |
| 47 | + * Holds if a `put` call on `qualifier` puts a key match |
| 48 | + * into the map. |
| 49 | + */ |
| 50 | + private predicate putsCredentialtypesKey(Expr qualifier) { |
| 51 | + exists(MapPutCall put | |
| 52 | + put.getKey().(CompileTimeConstantExpr).getStringValue() = |
| 53 | + "jmx.remote.rmi.server.credential.types" or |
| 54 | + put.getKey().(CompileTimeConstantExpr).getStringValue() = |
| 55 | + "jmx.remote.rmi.server.credentials.filter.pattern" or |
| 56 | + put.getKey().toString() = "RMIConnectorServer.CREDENTIAL_TYPES" or // This can probably be solved more nicely |
| 57 | + put.getKey().toString() = "RMIConnectorServer.CREDENTIALS_FILTER_PATTERN" // This can probably be solved more nicely |
| 58 | + | |
| 59 | + put.getQualifier() = qualifier and |
| 60 | + put.getMethod().(MapMethod).getReceiverKeyType().getName() = "String" and |
| 61 | + put.getMethod().(MapMethod).getReceiverValueType().getName() = "Object" |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** Models flow from `new HashMap<>()` to the argument of a `TestConstructor` call. */ |
| 67 | +class MapToRmiServerInitConfiguration extends DataFlow::Configuration { |
| 68 | + MapToRmiServerInitConfiguration() { this = "MapToRmiServerInitConfiguration" } |
| 69 | + |
| 70 | + override predicate isSource(DataFlow::Node source) { |
| 71 | + source.asExpr().(ClassInstanceExpr).getConstructedType() instanceof MapType |
| 72 | + } |
| 73 | + |
| 74 | + override predicate isSink(DataFlow::Node sink) { |
| 75 | + exists(ConstructorCall ccall | |
| 76 | + sink.asExpr() = ccall.getArgument(1) and |
| 77 | + isRmiOrJmxServerCreateConstructor(ccall.getConstructor()) |
| 78 | + ) |
| 79 | + or |
| 80 | + exists(MethodAccess ma | |
| 81 | + sink.asExpr() = ma.getArgument(1) and |
| 82 | + isRmiOrJmxServerCreateMethod(ma.getMethod()) |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** Models if any JMX/RMI server are initialized with a null environment */ |
| 88 | +class FlowServerInitializedWithNullEnv extends DataFlow::Configuration { |
| 89 | + FlowServerInitializedWithNullEnv() { this = "FlowServerInitializedWithNullEnv" } |
| 90 | + |
| 91 | + override predicate isSource(DataFlow::Node source) { any() } |
| 92 | + |
| 93 | + override predicate isSink(DataFlow::Node sink) { |
| 94 | + exists(ConstructorCall ccall | |
| 95 | + sink.asExpr() = ccall and |
| 96 | + isRmiOrJmxServerCreateConstructor(ccall.getConstructor()) and |
| 97 | + ccall.getArgument(1) = alwaysNullExpr() |
| 98 | + ) |
| 99 | + or |
| 100 | + exists(MethodAccess ma | |
| 101 | + sink.asExpr() = ma and |
| 102 | + isRmiOrJmxServerCreateMethod(ma.getMethod()) and |
| 103 | + ma.getArgument(1) = alwaysNullExpr() |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** Returns true if within the passed PathNode a "jmx.remote.rmi.server.credential.types" is set. */ |
| 109 | +predicate mapFlowContainsCredentialtype(DataFlow::PathNode source) { |
| 110 | + exists(MapToPutCredentialstypeConfiguration conf | conf.hasFlow(source.getNode(), _)) |
| 111 | +} |
| 112 | + |
| 113 | +/** Returns result depending if the vulnerability is present due to a) a null environment b) an insecurely set environment map */ |
| 114 | +bindingset[source] |
| 115 | +string getRmiResult(DataFlow::PathNode source) { |
| 116 | + // We got a Map so we have a source and a sink node |
| 117 | + if source.getNode().getType() instanceof MapType |
| 118 | + then |
| 119 | + result = |
| 120 | + "RMI/JMX server initialized with insecure environment $@. The $@ never restricts accepted client objects to 'java.lang.String'. This exposes to deserialization attacks against the RMI authentication method." |
| 121 | + else |
| 122 | + // The environment is not a map so we most likely have a "null" environment and therefore only a sink |
| 123 | + result = |
| 124 | + "RMI/JMX server initialized with 'null' environment $@. Missing type restriction in RMI authentication method exposes the application to deserialization attacks." |
| 125 | +} |
| 126 | + |
| 127 | +/** Predicate returns true for any map flow paths with NO jmx.remote.rmi.server.credential.types set */ |
| 128 | +predicate hasVulnerableMapFlow(DataFlow::PathNode source, DataFlow::PathNode sink) { |
| 129 | + exists(MapToRmiServerInitConfiguration dataflow | |
| 130 | + dataflow.hasFlowPath(source, sink) and |
| 131 | + not mapFlowContainsCredentialtype(source) |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +from |
| 136 | + DataFlow::PathNode source, DataFlow::PathNode sink, |
| 137 | + FlowServerInitializedWithNullEnv initNullDataflow |
| 138 | +where |
| 139 | + // Check if server is created with null env |
| 140 | + initNullDataflow.hasFlowPath(source, sink) |
| 141 | + or |
| 142 | + // The map created by `new HashMap<String, Object>()` has to a) flow to the sink and b) there must not exist a (different) sink that would put `"jmx.remote.rmi.server.credential.types"` into `source`. */ |
| 143 | + hasVulnerableMapFlow(source, sink) |
| 144 | +select sink.getNode(), source, sink, getRmiResult(source), sink.getNode(), "here", source.getNode(), |
| 145 | + "source environment 'Map'" |
0 commit comments