From 21f0ed50a224f19d083ef8e3b7b02b8f3dd31cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Wed, 20 Nov 2024 13:59:52 +0000 Subject: [PATCH] 8344215: Remove calls to SecurityManager and doPrivileged in java.net.Socket and java.net.ServerSocket after JEP 486 integration Reviewed-by: dfuchs, alanb, jpai --- .../share/classes/java/net/ServerSocket.java | 58 ++---------------- .../share/classes/java/net/Socket.java | 60 +------------------ 2 files changed, 6 insertions(+), 112 deletions(-) diff --git a/src/java.base/share/classes/java/net/ServerSocket.java b/src/java.base/share/classes/java/net/ServerSocket.java index c6560fee35a..945693ef65e 100644 --- a/src/java.base/share/classes/java/net/ServerSocket.java +++ b/src/java.base/share/classes/java/net/ServerSocket.java @@ -33,7 +33,6 @@ import java.util.Objects; import java.util.Set; import java.util.Collections; -import sun.security.util.SecurityConstants; import sun.net.PlatformSocketImpl; /** @@ -89,13 +88,6 @@ public class ServerSocket implements java.io.Closeable { // used to coordinate creating and closing underlying socket private final Object socketLock = new Object(); - /** - * Creates a server socket with the given {@code SocketImpl}. - */ - private ServerSocket(Void unused, SocketImpl impl) { - this.impl = Objects.requireNonNull(impl); - } - /** * Creates a server socket with a user-specified {@code SocketImpl}. * @@ -106,16 +98,7 @@ public class ServerSocket implements java.io.Closeable { * @since 12 */ protected ServerSocket(SocketImpl impl) { - this(checkPermission(), impl); - } - - private static Void checkPermission() { - @SuppressWarnings("removal") - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION); - } - return null; + this.impl = Objects.requireNonNull(impl); } /** @@ -237,7 +220,7 @@ public class ServerSocket implements java.io.Closeable { this.impl = createImpl(); try { bind(new InetSocketAddress(bindAddr, port), backlog); - } catch (IOException | SecurityException e) { + } catch (IOException e) { close(); throw e; } @@ -338,11 +321,6 @@ public class ServerSocket implements java.io.Closeable { if (backlog < 1) backlog = 50; - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) - security.checkListen(epoint.getPort()); - // SocketImpl bind+listen throw if already bound or closed SocketImpl impl = getImpl(); impl.bind(epoint.getAddress(), epoint.getPort()); @@ -364,14 +342,7 @@ public class ServerSocket implements java.io.Closeable { if (!isBound()) return null; try { - InetAddress in = getImpl().getInetAddress(); - @SuppressWarnings("removal") - SecurityManager sm = System.getSecurityManager(); - if (sm != null) - sm.checkConnect(in.getHostAddress(), -1); - return in; - } catch (SecurityException e) { - return InetAddress.getLoopbackAddress(); + return getImpl().getInetAddress(); } catch (SocketException e) { // nothing // If we're bound, the impl has been created @@ -631,17 +602,6 @@ public class ServerSocket implements java.io.Closeable { throw e; } - // check permission, close SocketImpl/connection if denied - @SuppressWarnings("removal") - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - try { - sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort()); - } catch (SecurityException se) { - si.close(); - throw se; - } - } } /** @@ -835,15 +795,10 @@ public class ServerSocket implements java.io.Closeable { * * @return a string representation of this socket. */ - @SuppressWarnings("removal") public String toString() { if (!isBound()) return "ServerSocket[unbound]"; - InetAddress in; - if (System.getSecurityManager() != null) - in = getInetAddress(); - else - in = impl.getInetAddress(); + InetAddress in = impl.getInetAddress(); return "ServerSocket[addr=" + in + ",localport=" + impl.getLocalPort() + "]"; } @@ -884,11 +839,6 @@ public class ServerSocket implements java.io.Closeable { if (factory != null) { throw new SocketException("factory already defined"); } - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkSetFactory(); - } factory = fac; } diff --git a/src/java.base/share/classes/java/net/Socket.java b/src/java.base/share/classes/java/net/Socket.java index 9825457aac1..83c0dec682c 100644 --- a/src/java.base/share/classes/java/net/Socket.java +++ b/src/java.base/share/classes/java/net/Socket.java @@ -28,7 +28,6 @@ package java.net; import jdk.internal.event.SocketReadEvent; import jdk.internal.event.SocketWriteEvent; import jdk.internal.invoke.MhUtil; -import sun.security.util.SecurityConstants; import java.io.InputStream; import java.io.InterruptedIOException; @@ -157,15 +156,6 @@ public class Socket implements java.io.Closeable { return (s & SHUT_OUT) != 0; } - /** - * Creates an unconnected socket with the given {@code SocketImpl}. - */ - private Socket(Void unused, SocketImpl impl) { - if (impl != null) { - this.impl = impl; - } - } - /** * Creates an unconnected Socket. *

@@ -212,22 +202,10 @@ public class Socket implements java.io.Closeable { : sun.net.ApplicationProxy.create(proxy); Proxy.Type type = p.type(); if (type == Proxy.Type.SOCKS || type == Proxy.Type.HTTP) { - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); InetSocketAddress epoint = (InetSocketAddress) p.address(); if (epoint.getAddress() != null) { checkAddress (epoint.getAddress(), "Socket"); } - if (security != null) { - if (epoint.isUnresolved()) - epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort()); - if (epoint.isUnresolved()) - security.checkConnect(epoint.getHostName(), epoint.getPort()); - else - security.checkConnect(epoint.getAddress().getHostAddress(), - epoint.getPort()); - } - // create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false); impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate) @@ -259,18 +237,9 @@ public class Socket implements java.io.Closeable { * @since 1.1 */ protected Socket(SocketImpl impl) throws SocketException { - this(checkPermission(impl), impl); - } - - private static Void checkPermission(SocketImpl impl) { if (impl != null) { - @SuppressWarnings("removal") - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION); - } + this.impl = impl; } - return null; } /** @@ -497,7 +466,7 @@ public class Socket implements java.io.Closeable { if (localAddr != null) bind(localAddr); connect(address); - } catch (IOException | IllegalArgumentException | SecurityException e) { + } catch (IOException | IllegalArgumentException e) { try { close(); } catch (IOException ce) { @@ -684,15 +653,6 @@ public class Socket implements java.io.Closeable { int port = epoint.getPort(); checkAddress(addr, "connect"); - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) { - if (epoint.isUnresolved()) - security.checkConnect(epoint.getHostName(), port); - else - security.checkConnect(addr.getHostAddress(), port); - } - try { getImpl().connect(epoint, timeout); } catch (SocketTimeoutException e) { @@ -743,11 +703,6 @@ public class Socket implements java.io.Closeable { InetAddress addr = epoint.getAddress(); int port = epoint.getPort(); checkAddress (addr, "bind"); - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkListen(port); - } getImpl().bind(addr, port); getAndBitwiseOrState(BOUND); } @@ -795,15 +750,9 @@ public class Socket implements java.io.Closeable { InetAddress in = null; try { in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); - @SuppressWarnings("removal") - SecurityManager sm = System.getSecurityManager(); - if (sm != null) - sm.checkConnect(in.getHostAddress(), -1); if (in.isAnyLocalAddress()) { in = InetAddress.anyLocalAddress(); } - } catch (SecurityException e) { - in = InetAddress.getLoopbackAddress(); } catch (Exception e) { in = InetAddress.anyLocalAddress(); // "0.0.0.0" } @@ -1855,11 +1804,6 @@ public class Socket implements java.io.Closeable { if (factory != null) { throw new SocketException("factory already defined"); } - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) { - security.checkSetFactory(); - } factory = fac; }