Skip to content

Commit 3151aef

Browse files
committed
Enhance the query
1 parent 6a93099 commit 3151aef

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

java/ql/src/experimental/Security/CWE/CWE-522/InsecureLdapAuth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public DirContext ldapAuth(String ldapUserName, String password) {
1515
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
1616
environment.put(Context.PROVIDER_URL, ldapUrl);
1717
environment.put(Context.REFERRAL, "follow");
18-
env.put(Context.SECURITY_AUTHENTICATION, "simple");
18+
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
1919
environment.put(Context.SECURITY_PRINCIPAL, ldapUserName);
2020
environment.put(Context.SECURITY_CREDENTIALS, password);
2121
DirContext dirContext = new InitialDirContext(environment);

java/ql/src/experimental/Security/CWE/CWE-522/InsecureLdapAuth.ql

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class InsecureLdapUrl extends Expr {
7676
*/
7777
predicate isProviderUrlSetter(MethodAccess ma) {
7878
ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and
79-
(ma.getMethod().hasName("put") or ma.getMethod().hasName("setProperty")) and
79+
ma.getMethod().hasName(["put", "setProperty"]) and
8080
(
8181
ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "java.naming.provider.url"
8282
or
@@ -89,27 +89,48 @@ predicate isProviderUrlSetter(MethodAccess ma) {
8989
}
9090

9191
/**
92-
* Holds if `ma` sets `fieldValue` with attribute name `fieldName` to `envValue` in some `Hashtable`.
92+
* Holds if `ma` sets `fieldValue` to `envValue` in some `Hashtable`.
9393
*/
9494
bindingset[fieldValue, envValue]
95-
predicate hasEnvWithValue(MethodAccess ma, string fieldValue, string envValue) {
95+
predicate hasFieldValueEnv(MethodAccess ma, string fieldValue, string envValue) {
96+
// environment.put("java.naming.security.authentication", "simple")
9697
ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and
97-
(ma.getMethod().hasName("put") or ma.getMethod().hasName("setProperty")) and
98+
ma.getMethod().hasName(["put", "setProperty"]) and
9899
ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = fieldValue and
99100
ma.getArgument(1).(CompileTimeConstantExpr).getStringValue() = envValue
100101
}
101102

103+
/**
104+
* Holds if `ma` sets attribute name `fieldName` to `envValue` in some `Hashtable`.
105+
*/
106+
bindingset[fieldName, envValue]
107+
predicate hasFieldNameEnv(MethodAccess ma, string fieldName, string envValue) {
108+
// environment.put(Context.SECURITY_AUTHENTICATION, "simple")
109+
ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and
110+
ma.getMethod().hasName(["put", "setProperty"]) and
111+
exists(Field f |
112+
ma.getArgument(0) = f.getAnAccess() and
113+
f.hasName(fieldName) and
114+
f.getDeclaringType() instanceof TypeNamingContext
115+
) and
116+
ma.getArgument(1).(CompileTimeConstantExpr).getStringValue() = envValue
117+
}
118+
102119
/**
103120
* Holds if `ma` sets `java.naming.security.authentication` (also known as `Context.SECURITY_AUTHENTICATION`) to `simple` in some `Hashtable`.
104121
*/
105122
predicate isBasicAuthEnv(MethodAccess ma) {
106-
hasEnvWithValue(ma, "java.naming.security.authentication", "simple")
123+
hasFieldValueEnv(ma, "java.naming.security.authentication", "simple") or
124+
hasFieldNameEnv(ma, "SECURITY_AUTHENTICATION", "simple")
107125
}
108126

109127
/**
110128
* Holds if `ma` sets `java.naming.security.protocol` (also known as `Context.SECURITY_PROTOCOL`) to `ssl` in some `Hashtable`.
111129
*/
112-
predicate isSSLEnv(MethodAccess ma) { hasEnvWithValue(ma, "java.naming.security.protocol", "ssl") }
130+
predicate isSSLEnv(MethodAccess ma) {
131+
hasFieldValueEnv(ma, "java.naming.security.protocol", "ssl") or
132+
hasFieldNameEnv(ma, "SECURITY_PROTOCOL", "ssl")
133+
}
113134

114135
/**
115136
* A taint-tracking configuration for `ldap://` URL in LDAP authentication.
@@ -141,12 +162,12 @@ class InsecureUrlFlowConfig extends TaintTracking::Configuration {
141162
/**
142163
* A taint-tracking configuration for `simple` basic-authentication in LDAP configuration.
143164
*/
144-
class BasicAuthFlowConfig extends TaintTracking::Configuration {
165+
class BasicAuthFlowConfig extends DataFlow::Configuration {
145166
BasicAuthFlowConfig() { this = "InsecureLdapAuth:BasicAuthFlowConfig" }
146167

147168
/** Source of `simple` configuration. */
148169
override predicate isSource(DataFlow::Node src) {
149-
src.asExpr().(CompileTimeConstantExpr).getStringValue() = "simple"
170+
exists(MethodAccess ma | isBasicAuthEnv(ma) and ma.getQualifier() = src.asExpr())
150171
}
151172

152173
/** Sink of directory context creation. */
@@ -156,26 +177,17 @@ class BasicAuthFlowConfig extends TaintTracking::Configuration {
156177
sink.asExpr() = cc.getArgument(0)
157178
)
158179
}
159-
160-
/** Method call of `env.put()`. */
161-
override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) {
162-
exists(MethodAccess ma |
163-
pred.asExpr() = ma.getArgument(1) and
164-
isBasicAuthEnv(ma) and
165-
succ.asExpr() = ma.getQualifier()
166-
)
167-
}
168180
}
169181

170182
/**
171183
* A taint-tracking configuration for `ssl` configuration in LDAP authentication.
172184
*/
173-
class SSLFlowConfig extends TaintTracking::Configuration {
185+
class SSLFlowConfig extends DataFlow::Configuration {
174186
SSLFlowConfig() { this = "InsecureLdapAuth:SSLFlowConfig" }
175187

176188
/** Source of `ssl` configuration. */
177189
override predicate isSource(DataFlow::Node src) {
178-
src.asExpr().(CompileTimeConstantExpr).getStringValue() = "ssl"
190+
exists(MethodAccess ma | isSSLEnv(ma) and ma.getQualifier() = src.asExpr())
179191
}
180192

181193
/** Sink of directory context creation. */
@@ -185,28 +197,12 @@ class SSLFlowConfig extends TaintTracking::Configuration {
185197
sink.asExpr() = cc.getArgument(0)
186198
)
187199
}
188-
189-
/** Method call of `env.put()`. */
190-
override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) {
191-
exists(MethodAccess ma |
192-
pred.asExpr() = ma.getArgument(1) and
193-
isSSLEnv(ma) and
194-
succ.asExpr() = ma.getQualifier()
195-
)
196-
}
197200
}
198201

199-
from DataFlow::PathNode source, DataFlow::PathNode sink, InsecureUrlFlowConfig config, VarAccess va
202+
from DataFlow::PathNode source, DataFlow::PathNode sink, InsecureUrlFlowConfig config
200203
where
201204
config.hasFlowPath(source, sink) and
202-
sink.getNode().asExpr() = va and
203-
exists(BasicAuthFlowConfig bc, DataFlow::PathNode source2, DataFlow::PathNode sink2 |
204-
bc.hasFlowPath(source2, sink2) and
205-
sink2.getNode().asExpr() = va
206-
) and
207-
not exists(SSLFlowConfig sc, DataFlow::PathNode source3, DataFlow::PathNode sink3 |
208-
sc.hasFlowPath(source3, sink3) and
209-
sink3.getNode().asExpr() = va
210-
)
205+
exists(BasicAuthFlowConfig bc | bc.hasFlowTo(sink.getNode())) and
206+
not exists(SSLFlowConfig sc | sc.hasFlowTo(sink.getNode()))
211207
select sink.getNode(), source, sink, "Insecure LDAP authentication from $@.", source.getNode(),
212208
"LDAP connection string"

java/ql/src/semmle/code/java/frameworks/Networking.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class UrlOpenConnectionMethod extends Method {
132132

133133
/**
134134
* A string matching private host names of IPv4 and IPv6, which only matches the host portion therefore checking for port is not necessary.
135+
* Several examples are localhost, reserved IPv4 IP addresses including 127.0.0.1, 10.x.x.x, 172.16.x,x, 192.168.x,x, and reserved IPv6 addresses including [0:0:0:0:0:0:0:1] and [::1]
135136
*/
136137
class PrivateHostName extends string {
137138
bindingset[this]

0 commit comments

Comments
 (0)