Skip to content

Commit e4553cb

Browse files
committedMar 25, 2019
8217997: Better socket support
Reviewed-by: alanb, ahgross, chegar, igerasim
1 parent 67a0aa7 commit e4553cb

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
 

‎src/java.base/share/classes/java/net/NetPermission.java

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@
145145
* </tr>
146146
*
147147
* <tr>
148+
* <th scope="row">setSocketImpl</th>
149+
* <td>The ability to create a sub-class of Socket or ServerSocket with a
150+
* user specified SocketImpl.</td>
151+
* <td>Malicious user-defined SocketImpls can change the behavior of
152+
* Socket and ServerSocket in surprising ways, by virtue of their
153+
* ability to access the protected fields of SocketImpl.</td>
154+
* </tr>
155+
*
156+
* <tr>
148157
* <th scope="row">specifyStreamHandler</th>
149158
* <td>The ability
150159
* to specify a stream handler when constructing a URL</td>

‎src/java.base/share/classes/java/net/ServerSocket.java

+13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Set;
3333
import java.util.Collections;
3434

35+
import sun.security.util.SecurityConstants;
3536
import sun.net.PlatformSocketImpl;
3637

3738
/**
@@ -73,13 +74,25 @@ class ServerSocket implements java.io.Closeable {
7374
*
7475
* @throws NullPointerException if impl is {@code null}.
7576
*
77+
* @throws SecurityException if a security manager is set and
78+
* its {@code checkPermission} method doesn't allow
79+
* {@code NetPermission("setSocketImpl")}.
7680
* @since 12
7781
*/
7882
protected ServerSocket(SocketImpl impl) {
7983
Objects.requireNonNull(impl);
84+
checkPermission();
8085
this.impl = impl;
8186
}
8287

88+
private static Void checkPermission() {
89+
SecurityManager sm = System.getSecurityManager();
90+
if (sm != null) {
91+
sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);
92+
}
93+
return null;
94+
}
95+
8396
/**
8497
* Creates an unbound server socket.
8598
*

‎src/java.base/share/classes/java/net/Socket.java

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package java.net;
2727

28+
import sun.security.util.SecurityConstants;
29+
2830
import java.io.InputStream;
2931
import java.io.OutputStream;
3032
import java.io.IOException;
@@ -182,12 +184,28 @@ public Socket(Proxy proxy) {
182184
*
183185
* @throws SocketException if there is an error in the underlying protocol,
184186
* such as a TCP error.
187+
*
188+
* @throws SecurityException if {@code impl} is non-null and a security manager is set
189+
* and its {@code checkPermission} method doesn't allow {@code NetPermission("setSocketImpl")}.
190+
*
185191
* @since 1.1
186192
*/
187193
protected Socket(SocketImpl impl) throws SocketException {
194+
checkPermission(impl);
188195
this.impl = impl;
189196
}
190197

198+
private static Void checkPermission(SocketImpl impl) {
199+
if (impl == null) {
200+
return null;
201+
}
202+
SecurityManager sm = System.getSecurityManager();
203+
if (sm != null) {
204+
sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);
205+
}
206+
return null;
207+
}
208+
191209
/**
192210
* Creates a stream socket and connects it to the specified port
193211
* number on the named host.

‎src/java.base/share/classes/sun/security/util/SecurityConstants.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, 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
@@ -97,6 +97,10 @@ private SecurityConstants () {
9797
public static final NetPermission GET_RESPONSECACHE_PERMISSION =
9898
new NetPermission("getResponseCache");
9999

100+
// java.net.ServerSocket, java.net.Socket
101+
public static final NetPermission SET_SOCKETIMPL_PERMISSION =
102+
new NetPermission("setSocketImpl");
103+
100104
// java.lang.SecurityManager, sun.applet.AppletPanel
101105
public static final RuntimePermission CREATE_CLASSLOADER_PERMISSION =
102106
new RuntimePermission("createClassLoader");

0 commit comments

Comments
 (0)
Failed to load comments.