Skip to content

Commit fb7b1ff

Browse files
committed
fix Fix CVE-2021-4104 aka issue REL-2)
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 3625b12 commit fb7b1ff

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

src/main/java/org/apache/log4j/net/JMSAppender.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import javax.jms.TopicSession;
3333
import javax.naming.Context;
3434
import javax.naming.InitialContext;
35-
import javax.naming.NameNotFoundException;
3635
import javax.naming.NamingException;
3736
import java.util.Properties;
3837

@@ -233,12 +232,12 @@ public void activateOptions() {
233232
}
234233

235234
protected Object lookup(Context ctx, String name) throws NamingException {
236-
try {
237-
return ctx.lookup(name);
238-
} catch (NameNotFoundException e) {
239-
LogLog.error("Could not find name [" + name + "].");
240-
throw e;
235+
Object result = JNDIUtil.lookupObject(ctx, name);
236+
if (result == null) {
237+
String msg = "Could not find name [" + name + "].";
238+
throw new NamingException(msg);
241239
}
240+
return result;
242241
}
243242

244243
protected boolean checkEntryConditions() {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.log4j.net;
18+
19+
import javax.naming.Context;
20+
import javax.naming.InitialContext;
21+
import javax.naming.NamingException;
22+
23+
public class JNDIUtil {
24+
25+
// See https://jakarta.ee/specifications/platform/8/platform-spec-8.html#a616
26+
// there are the java:comp, java:module, java:app, java:global namespaces
27+
public static final String JNDI_JAVA_NAMESPACE = "java:";
28+
29+
static final String RESTRICTION_MSG = "JNDI name must start with " + JNDI_JAVA_NAMESPACE + " but was ";
30+
31+
public static Object lookupObject(Context ctx, String name) throws NamingException {
32+
if (ctx == null) {
33+
return null;
34+
}
35+
36+
if (isNullOrEmpty(name)) {
37+
return null;
38+
}
39+
40+
jndiNameSecurityCheck(name);
41+
42+
Object lookup = ctx.lookup(name);
43+
return lookup;
44+
}
45+
46+
private static boolean isNullOrEmpty(String str) {
47+
return ((str == null) || str.trim().length() == 0);
48+
}
49+
50+
public static void jndiNameSecurityCheck(String name) throws NamingException {
51+
if (!name.startsWith(JNDI_JAVA_NAMESPACE)) {
52+
throw new NamingException(RESTRICTION_MSG + name);
53+
}
54+
}
55+
56+
// used for testing
57+
static Context getInitialContext() throws NamingException {
58+
return new InitialContext();
59+
}
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.log4j.net;
18+
19+
import static org.junit.Assert.fail;
20+
21+
import javax.naming.Context;
22+
import javax.naming.NamingException;
23+
24+
import org.junit.Test;
25+
26+
27+
/**
28+
* Test copied form the logback project with permission.
29+
*
30+
* @author Ceki Gulcu
31+
*
32+
*/
33+
public class JNDIUtilTest {
34+
35+
@Test
36+
public void ensureJavaNameSpace() throws NamingException {
37+
38+
try {
39+
Context ctxt = JNDIUtil.getInitialContext();
40+
JNDIUtil.lookupObject(ctxt, "ldap:...");
41+
} catch (NamingException e) {
42+
String excaptionMsg = e.getMessage();
43+
if (excaptionMsg.startsWith(JNDIUtil.RESTRICTION_MSG))
44+
return;
45+
else {
46+
fail("unexpected exception " + e);
47+
}
48+
}
49+
50+
fail("Should aNot yet implemented");
51+
}
52+
53+
54+
}

0 commit comments

Comments
 (0)