Skip to content

Commit da6b265

Browse files
mrserbgnu-andrew
authored andcommitted
8290367: Update default value and extend the scope of com.sun.jndi.ldap.object.trustSerialData system property
Reviewed-by: yan, mbalao, andrew Backport-of: 7765942aeee25cbeb5fd932a93b3d8f9d4ca3655
1 parent 3b077b8 commit da6b265

File tree

4 files changed

+202
-8
lines changed

4 files changed

+202
-8
lines changed

jdk/src/share/classes/com/sun/jndi/ldap/Obj.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -241,6 +241,10 @@ static Object decodeObject(Attributes attrs)
241241
ClassLoader cl = helper.getURLClassLoader(codebases);
242242
return deserializeObject((byte[])attr.get(), cl);
243243
} else if ((attr = attrs.get(JAVA_ATTRIBUTES[REMOTE_LOC])) != null) {
244+
// javaRemoteLocation attribute (RMI stub will be created)
245+
if (!VersionHelper12.isSerialDataAllowed()) {
246+
throw new NamingException("Object deserialization is not allowed");
247+
}
244248
// For backward compatibility only
245249
return decodeRmiObject(
246250
(String)attrs.get(JAVA_ATTRIBUTES[CLASSNAME]).get(),

jdk/src/share/classes/com/sun/jndi/ldap/VersionHelper12.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -40,13 +40,13 @@ final class VersionHelper12 extends VersionHelper {
4040
"com.sun.jndi.ldap.object.trustURLCodebase";
4141

4242
// System property to control whether classes are allowed to be loaded from
43-
// 'javaSerializedData' attribute
43+
// 'javaSerializedData', 'javaRemoteLocation' or 'javaReferenceAddress' attributes.
4444
private static final String TRUST_SERIAL_DATA_PROPERTY =
4545
"com.sun.jndi.ldap.object.trustSerialData";
4646

4747
/**
48-
* Determines whether objects may be deserialized from the content of
49-
* 'javaSerializedData' attribute.
48+
* Determines whether objects may be deserialized or reconstructed from a content of
49+
* 'javaSerializedData', 'javaRemoteLocation' or 'javaReferenceAddress' LDAP attributes.
5050
*/
5151
private static final boolean trustSerialData;
5252

@@ -56,7 +56,7 @@ final class VersionHelper12 extends VersionHelper {
5656
static {
5757
String trust = getPrivilegedProperty(TRUST_URL_CODEBASE_PROPERTY, "false");
5858
trustURLCodebase = "true".equalsIgnoreCase(trust);
59-
String trustSDString = getPrivilegedProperty(TRUST_SERIAL_DATA_PROPERTY, "true");
59+
String trustSDString = getPrivilegedProperty(TRUST_SERIAL_DATA_PROPERTY, "false");
6060
trustSerialData = "true".equalsIgnoreCase(trustSDString);
6161
}
6262

@@ -72,8 +72,9 @@ private static String getPrivilegedProperty(String propertyName, String defaultV
7272
VersionHelper12() {} // Disallow external from creating one of these.
7373

7474
/**
75-
* Returns true if deserialization of objects from 'javaSerializedData'
76-
* and 'javaReferenceAddress' LDAP attributes is allowed.
75+
* Returns true if deserialization or reconstruction of objects from
76+
* 'javaSerializedData', 'javaRemoteLocation' and 'javaReferenceAddress'
77+
* LDAP attributes is allowed.
7778
*
7879
* @return true if deserialization is allowed; false - otherwise
7980
*/
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.net.InetAddress;
25+
import java.net.InetSocketAddress;
26+
import java.net.ServerSocket;
27+
import java.net.SocketAddress;
28+
import java.util.Hashtable;
29+
import javax.naming.CommunicationException;
30+
import javax.naming.NamingException;
31+
import javax.naming.ServiceUnavailableException;
32+
import javax.naming.directory.DirContext;
33+
import javax.naming.directory.InitialDirContext;
34+
35+
import jdk.testlibrary.net.URIBuilder;
36+
37+
/**
38+
* @test
39+
* @bug 8290367
40+
* @summary Check if com.sun.jndi.ldap.object.trustSerialData covers the creation
41+
* of RMI remote objects from the 'javaRemoteLocation' LDAP attribute.
42+
* @modules java.naming/com.sun.jndi.ldap
43+
* @library /lib/testlibrary ../lib
44+
* @build LDAPServer LDAPTestUtils
45+
*
46+
* @run main/othervm RemoteLocationAttributeTest
47+
* @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData
48+
* RemoteLocationAttributeTest
49+
* @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=false
50+
* RemoteLocationAttributeTest
51+
* @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=true
52+
* RemoteLocationAttributeTest
53+
* @run main/othervm -Dcom.sun.jndi.ldap.object.trustSerialData=TrUe
54+
* RemoteLocationAttributeTest
55+
*/
56+
57+
public class RemoteLocationAttributeTest {
58+
59+
public static void main(String[] args) throws Exception {
60+
// Create unbound server socket
61+
ServerSocket serverSocket = new ServerSocket();
62+
63+
// Bind it to the loopback address
64+
SocketAddress sockAddr = new InetSocketAddress(
65+
InetAddress.getLoopbackAddress(), 0);
66+
serverSocket.bind(sockAddr);
67+
68+
// Construct the provider URL for LDAPTestUtils
69+
String providerURL = URIBuilder.newBuilder()
70+
.scheme("ldap")
71+
.loopback()
72+
.port(serverSocket.getLocalPort())
73+
.buildUnchecked().toString();
74+
75+
Hashtable<Object, Object> env;
76+
77+
// Initialize test environment variables
78+
env = LDAPTestUtils.initEnv(serverSocket, providerURL,
79+
RemoteLocationAttributeTest.class.getName(), args, false);
80+
81+
DirContext ctx = null;
82+
try {
83+
try {
84+
System.err.println(env);
85+
// connect to server
86+
ctx = new InitialDirContext(env);
87+
Object lookupResult = ctx.lookup("Test");
88+
System.err.println("Lookup result:" + lookupResult);
89+
// Test doesn't provide RMI registry running at 127.0.0.1:1097, but if
90+
// there is one running on test host successful result is valid for
91+
// cases when reconstruction allowed.
92+
if (!RECONSTRUCTION_ALLOWED) {
93+
throw new AssertionError("Unexpected successful lookup");
94+
}
95+
} finally {
96+
serverSocket.close();
97+
}
98+
} catch (ServiceUnavailableException | CommunicationException connectionException) {
99+
// The remote location was properly reconstructed but connection to
100+
// RMI endpoint failed:
101+
// ServiceUnavailableException - no open socket on 127.0.0.1:1097
102+
// CommunicationException - 127.0.0.1:1097 is open, but it is not RMI registry
103+
System.err.println("Got one of connection exceptions:" + connectionException);
104+
if (!RECONSTRUCTION_ALLOWED) {
105+
throw new AssertionError("Reconstruction not blocked, as expected");
106+
}
107+
} catch (NamingException ne) {
108+
String message = ne.getMessage();
109+
System.err.printf("Got NamingException with message: '%s'%n", message);
110+
if (RECONSTRUCTION_ALLOWED && EXPECTED_NAMING_EXCEPTION_MESSAGE.equals(message)) {
111+
throw new AssertionError("Reconstruction unexpectedly blocked");
112+
}
113+
if (!RECONSTRUCTION_ALLOWED && !EXPECTED_NAMING_EXCEPTION_MESSAGE.equals(message)) {
114+
throw new AssertionError("Reconstruction not blocked");
115+
}
116+
} finally {
117+
LDAPTestUtils.cleanup(ctx);
118+
}
119+
}
120+
121+
// Reconstruction of RMI remote objects is allowed if 'com.sun.jndi.ldap.object.trustSerialData'
122+
// is set to "true". If the system property is not specified it implies default "false" value
123+
private static final boolean RECONSTRUCTION_ALLOWED =
124+
Boolean.getBoolean("com.sun.jndi.ldap.object.trustSerialData");
125+
126+
// NamingException message when reconstruction is not allowed
127+
private static final String EXPECTED_NAMING_EXCEPTION_MESSAGE = "Object deserialization is not allowed";
128+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
#
5+
# This code is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License version 2 only, as
7+
# published by the Free Software Foundation.
8+
#
9+
# This code is distributed in the hope that it will be useful, but WITHOUT
10+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
# version 2 for more details (a copy is included in the LICENSE file that
13+
# accompanied this code).
14+
#
15+
# You should have received a copy of the GNU General Public License version
16+
# 2 along with this work; if not, write to the Free Software Foundation,
17+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
#
19+
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
# or visit www.oracle.com if you need additional information or have any
21+
# questions.
22+
#
23+
24+
################################################################################
25+
# Capture file for RemoteLocationAttributeTest.java
26+
#
27+
# NOTE: This hexadecimal dump of LDAP protocol messages was generated by
28+
# running the RemoteLocationAttributeTest application program against
29+
# a real LDAP server and setting the JNDI/LDAP environment property:
30+
# com.sun.jndi.ldap.trace.ber to activate LDAP message tracing.
31+
#
32+
################################################################################
33+
34+
# LDAP BindRequest
35+
0000: 30 0C 02 01 01 60 07 02 01 03 04 00 80 00 0....`........
36+
37+
# LDAP BindResponse
38+
0000: 30 0C 02 01 01 61 07 0A 01 00 04 00 04 00 0....a........
39+
40+
# LDAP SearchRequest
41+
0000: 30 46 02 01 02 63 24 04 04 54 65 73 74 0A 01 00 0F...c$..Test...
42+
0010: 0A 01 03 02 01 00 02 01 00 01 01 00 87 0B 6F 62 ..............ob
43+
0020: 6A 65 63 74 43 6C 61 73 73 30 00 A0 1B 30 19 04 jectClass0...0..
44+
0030: 17 32 2E 31 36 2E 38 34 30 2E 31 2E 31 31 33 37 .2.16.840.1.1137
45+
0040: 33 30 2E 33 2E 34 2E 32 30.3.4.2
46+
47+
# LDAP SearchResultEntry
48+
0000: 30 5E 02 01 02 64 59 04 04 54 65 73 74 30 51 30 0^...dY..Test0Q0
49+
0010: 16 04 0D 6A 61 76 61 43 6C 61 73 73 4E 61 6D 65 ...javaClassName
50+
0020: 31 05 04 03 66 6F 6F 30 37 04 12 6A 61 76 61 52 1...foo07..javaR
51+
0030: 65 6D 6F 74 65 4C 6F 63 61 74 69 6F 6E 31 21 04 emoteLocation1!.
52+
0040: 1F 72 6D 69 3A 2F 2F 31 32 37 2E 30 2E 30 2E 31 .rmi://127.0.0.1
53+
0050: 3A 31 30 39 37 2F 54 65 73 74 52 65 6D 6F 74 65 :1097/TestRemote
54+
55+
# LDAP SearchResultDone
56+
0000: 30 0C 02 01 02 65 07 0A 01 00 04 00 04 00 0....e........
57+
58+
# LDAP UnbindRequest
59+
0000: 30 22 02 01 03 42 00 A0 1B 30 19 04 17 32 2E 31 0"...B...0...2.1
60+
0010: 36 2E 38 34 30 2E 31 2E 31 31 33 37 33 30 2E 33 6.840.1.113730.3
61+
0020: 2E 34 2E 32 .4.2

0 commit comments

Comments
 (0)